Initial Initial Experience(sExperience(s))
Developing S4 Classes for Medical Imaging Data
Brandon Whitcher, PhDGlaxoSmithKline
Clinical Imaging Centre
2
AcknowledgementsAcknowledgements
••
S4 Classes and MethodsS4 Classes and Methods––
Slides by F. Slides by F. LeischLeisch
and P. Murrelland P. Murrell
––
Software for Data AnalysisSoftware for Data Analysis
by J. Chambersby J. Chambers––
R Programming for BioinformaticsR Programming for Bioinformatics
by R. Gentlemenby R. Gentlemen
––
R packagesR packages••
nlmenlme
••
kernlabkernlab••
EBImageEBImage
3
OOP and Classes in ROOP and Classes in R
••
One identifies real objects, and the operations on them, One identifies real objects, and the operations on them, that are interesting.that are interesting.––
These operations can be systematically implemented.These operations can be systematically implemented.
••
A basic principle (hope) is that by faithfully A basic principle (hope) is that by faithfully representing the objects we get easierrepresenting the objects we get easier--toto--implement implement functions.functions.
••
A class is an abstract definition of a concrete realA class is an abstract definition of a concrete real--world world object.object.
••
A class system is a software infrastructure that is A class system is a software infrastructure that is designed to help construct classes and to provide designed to help construct classes and to provide programmatic support for dealing with classes.programmatic support for dealing with classes.
4
Why (S4) Classes for Medical Why (S4) Classes for Medical Imaging Data?Imaging Data?
••
Context of imaging dataContext of imaging data••
Interface with data standards and thirdInterface with data standards and third--party party softwaresoftware
••
Numerous processing steps involvedNumerous processing steps involved––
Multiple information sourcesMultiple information sources
••
Audit trail (internal and external agencies)Audit trail (internal and external agencies)••
Too easy to make mistakes!Too easy to make mistakes!
5
Data Analysis PipelineData Analysis Pipeline
Pre-processing MathematicalModelling
StatisticalAnalysis
Data
Results
Motion correctionWarping
Co-registration
Model buildingParameter estimation
Test statisticsp-valuesMultiple
comparisons
6
Medical Imaging DataMedical Imaging DataSlice Thickness; e.g. 5mm
Spatial ResolutionSize = X × Y; e.g. 256×256
X
Y
e.g. 0.4mm
Chosen parameters depend on many factors: application, scanner, local practice, radiologist preferences, etc.
X
Z
7
Composition of Structural DataComposition of Structural Data
convert
DICOM
XY
Z
NIfTI 3D
8
Composition of Dynamic DataComposition of Dynamic Data
XY
Z
t = 1
XY
Z
t = T
•••
•••
•••NIfTI 4D
9
The The niftinifti
ClassClass
••
Inherits from class Inherits from class arrayarray••
Contains 44 “slots” (not counting Contains 44 “slots” (not counting .Data.Data))––
both numeric and character strings [348 bytes]both numeric and character strings [348 bytes]
••
Also created function Also created function niftinifti()()
for userfor user--defined slots.defined slots.••
Similar class has been defined for ANALYZE data.Similar class has been defined for ANALYZE data.
setClass(“nifti”, representation(“sizeof_hdr”=“numeric”, “data_type”=“character”, ...),
prototype(“sizeof_hdr”=348,“data_type”=“”,...),
contains=“array”)
10
Method “show” for Method “show” for niftinifti
ClassClass
setMethod(“show”, “nifti”, function(object) {cat(“NIfTI-1 file format”, fill=TRUE)cat(...)cat(...)
})
> ffdNIfTI-1 formatType : niftiData Type : 4 (INT16)Bits per Pixel : 16Slice Code : 0 (Unknown)Intent Code : 0 (None)Qform Code : 1 (Scanner_Anat)Sform Code : 0 (Unknown)Dimension : 64 x 64 x 21 x 180Pixel Dimension : 4 x 4 x 6 x 3Voxel Units : mmTime Units : sec
11
Validity Checking for Validity Checking for niftinifti
ClassClass
setValidity(“nifti”, function(object) {retval <- NULL...if (object@”sizeof_hdr” != 348) retval <- c(retval, “sizeof_hdr != 348”)
if (!object@datatype %in% c(2^(1:11),768,1280,1536,1792))retval <- c(retval, “datatype not recognized”)
...if (!all(object@”dim_”[indices] == dim([email protected])))retval <- c(retval, “dim/img mismatch”)
...if (is.null(retval))return(TRUE)
elsereturn(retval)
})
12
Manipulation of SlotsManipulation of Slots
••
The The niftinifti
header allows limited text informationheader allows limited text information––
descripdescrip
(80 characters) (80 characters) and and aux_fileaux_file
(24 characters)(24 characters)
––
Replacement functionsReplacement functions
Acessor
functions
> aux.file(mniLR)[1] "none "> descrip(mniLR)[1] "FSL3.2beta"
Slots
> mniLR@”aux_file”[1] "none "> mni@”descrip”[1] "FSL3.2beta"
setMethod(“descrip”, “nifti”, function(object) { object@descrip })setGeneric(“descrip<-”, function(x, value) { standardGeneric(“descrip<-”) })setReplaceMethod(“descrip”, “nifti”,
function(x, value) {x@descrip <- valuex
})
13
Additional MethodsAdditional Methods
••
readNIfTIreadNIfTI() () andand
writeNIfTIwriteNIfTI()()––
IO functions for NIfTI data (100% IO functions for NIfTI data (100% RR))
––
Affine transformationsAffine transformations••
image()image()––
Forces S3 method into proper generic methodForces S3 method into proper generic method
––
Equivalent to a “Equivalent to a “lightboxlightbox” representation” representation••
overlay()overlay()––
Essentially, the Essentially, the imageimage
function for two input arraysfunction for two input arrays
••
orthographic()orthographic()––
22××2 matrix of plots: coronal, sagittal, axial 2 matrix of plots: coronal, sagittal, axial
––
Utilizes “pixel dimension” slotUtilizes “pixel dimension” slot
14
ffd <- readNIfTI(“filtered_func_data.nii.gz”)image(ffd, zlim=range(ffd)) # Figure 1zstat1 <- readNIfti(“zstat1.nii.gz”)overlay(ffd, ifelse(abs(zstat1) > 5, zstat1, NA), zlim.x=range(ffd)) # Figure 2
15
mni <- readNIfTI(“avg152T1_LT_nifti.nii.gz”)orthographic(mni, zlim=range(mni))> mniLRNIfTI-1 formatType : niftiData Type : 2 (UINT8)Bits per Pixel : 8Slice Code : 0 (Unknown)Intent Code : 0 (None)Qform Code : 0 (Unknown)Sform Code : 4 (MNI_152)Dimension : 91 x 109 x 91Pixel Dimension : 2 x 2 x 2Voxel Units : mmTime Units : sec
16
Coercion and ExtensionsCoercion and Extensions
••
Coercion Coercion ––
Implicit since Implicit since niftinifti
class is built on the class is built on the arrayarray
class.class.
––
But... it will fail But... it will fail validObjectvalidObject()()..
••
Allow extensions via the NIfTIAllow extensions via the NIfTI--1.1 mechanism.1.1 mechanism.––
Define new class Define new class ““niftiExtensionniftiExtension””
––
Implement full audit trailImplement full audit trail••
XML document XML document ––
library(“XMLlibrary(“XML”)”)
> A <- array(1:8^3, rep(8,3))> B <- as(A, “nifti”)> dim(B)[1] 8 8 8> B@”dim_”[1] 0 0 0 0 0 0 0 0
17
ConclusionsConclusions
••
S4 classes are powerfulS4 classes are powerful––
Robust frameworkRobust framework
––
Generics and methodsGenerics and methods––
Validity checkingValidity checking
––
InheritanceInheritance••
... and difficult... and difficult––
Documentation (lack thereof)Documentation (lack thereof)
––
Examples (lack thereof)Examples (lack thereof)––
Slower than S3 (?)Slower than S3 (?)
18
TodoTodo ListList
••
Integrate S4 class into image analysis functions.Integrate S4 class into image analysis functions.––
Modification of Modification of .Data.Data
causes modification of causes modification of slot(sslot(s))
••
Use Use cniftilibcniftilib
to be 100% NIfTI compliantto be 100% NIfTI compliant––
RniftilibRniftilib
by Oliver by Oliver GranertGranert
<<[email protected]@neurologie.uni--kiel.dekiel.de>>
••
Medical_ImagingMedical_Imaging
task viewtask view––
Seven packages in current version.Seven packages in current version.