def implement(self): dwi = self.getBackupImage('dwi') bEncs = self.getBackupImage('grad', None, 'b') bVals = self.getBackupImage('grad', None, 'bvals') bVecs = self.getBackupImage('grad', None, 'bvecs') (bEncs, bVecs, bVals) = self.__produceEncodingFiles(bEncs, bVecs, bVals, dwi) # Convert bvecs bval expectedLayout = self.get('stride_orientation') if not mriutil.isDataStridesOrientationExpected(dwi, expectedLayout) \ and self.get("force_realign_strides"): self.warning("Reorienting strides for image {}".format(dwi)) self.__stride4DImage(dwi, bEncs, bVecs, bVals, expectedLayout) else: self.info("Linking {} to {}".format( dwi, util.symlink(dwi, self.workingDir))) images = Images( (self.getBackupImage('anat'), 'high resolution'), (self.getBackupImage('b0_pa'), 'B0 posterior to anterior'), (self.getBackupImage('b0_ap'), 'B0 anterior to posterior'), (self.getBackupImage('mag'), 'MR magnitude'), (self.getBackupImage('phase'), 'MR phase '), (self.getBackupImage('aparc_aseg'), 'parcellation'), (self.getBackupImage('anat', 'freesurfer'), 'freesurfer anatomical'), (self.getBackupImage('lh_ribbon'), 'left hemisphere ribbon'), (self.getBackupImage('rh_ribbon'), 'right hemisphere ribbon'), (self.getBackupImage('brodmann'), 'brodmann'), ) for image, description in images.getData(): if image: if not mriutil.isDataStridesOrientationExpected(image, expectedLayout) \ and self.get("force_realign_strides"): self.info( mriutil.stride3DImage(image, self.buildName(image, "stride"), expectedLayout)) else: self.info("Found {} image, linking {} to {}".format( description, image, util.symlink(image, self.workingDir))) for directory in [ os.path.join(self.backupDir, directory) for directory in os.listdir(self.backupDir) if os.path.isdir(os.path.join(self.backupDir, directory)) ]: if mriutil.isAfreesurferStructure(directory): self.info( "{} seem\'s a valid freesurfer structure: linking to {} directory" .format(directory, self.workingDir)) util.symlink(directory, self.workingDir, self.get("parcellation", "id"))
def implement(self): dwi = self.getBackupImage('dwi') bEncs = self.getBackupImage('grad', None, 'b') bVals = self.getBackupImage('grad', None, 'bvals') bVecs = self.getBackupImage('grad', None, 'bvecs') (bEncs, bVecs, bVals) = self.__produceEncodingFiles(bEncs, bVecs, bVals, dwi) # Convert bvecs bval expectedLayout = self.get('stride_orientation') if not mriutil.isDataStridesOrientationExpected(dwi, expectedLayout) \ and self.get("force_realign_strides"): self.warning("Reorienting strides for image {}".format(dwi)) self.__stride4DImage(dwi, bEncs, bVecs, bVals, expectedLayout) else: self.info("Linking {} to {}".format(dwi, util.symlink(dwi, self.workingDir))) images = Images( (self.getBackupImage('anat'),'high resolution'), (self.getBackupImage('b0_pa'),'B0 posterior to anterior'), (self.getBackupImage('b0_ap'),'B0 anterior to posterior'), (self.getBackupImage('mag'),'MR magnitude'), (self.getBackupImage('phase'),'MR phase '), (self.getBackupImage('aparc_aseg'),'parcellation'), (self.getBackupImage('anat', 'freesurfer'),'freesurfer anatomical'), (self.getBackupImage('lh_ribbon'),'left hemisphere ribbon'), (self.getBackupImage('rh_ribbon'),'right hemisphere ribbon'), (self.getBackupImage('brodmann'),'brodmann'), ) for image, description in images.getData(): if image: if not mriutil.isDataStridesOrientationExpected(image, expectedLayout) \ and self.get("force_realign_strides"): self.info(mriutil.stride3DImage(image, self.buildName(image, "stride"), expectedLayout)) else: self.info("Found {} image, linking {} to {}".format(description, image, util.symlink(image, self.workingDir))) for directory in [os.path.join(self.backupDir, directory) for directory in os.listdir(self.backupDir) if os.path.isdir(os.path.join(self.backupDir, directory))]: if mriutil.isAfreesurferStructure(directory): self.info("{} seem\'s a valid freesurfer structure: linking to {} directory".format(directory, self.workingDir)) util.symlink(directory, self.workingDir, self.get("parcellation", "id"))
def __isAValidStructure(self): """Determine if the directory is a valid structure Returns: a Boolean that represent if the subject integrity test pass for that directory """ #Anatomical, Dwi and gradient encoding direction are mandatory input anat = util.getImage(self.config, self.workingDir, 'anat') if not anat: if util.getImage(self.config, self.workingDir, 'anat', None, 'nii'): self.warning("Found some uncompressed nifti images into {} directory. " "Please gzip those images and resubmit the pipeline again".format(self.workingDir)) return False self.warning("No high resolution image found into {} directory".format(self.workingDir)) return False dwi = util.getImage(self.config, self.workingDir, 'dwi') if not dwi: if util.getImage(self.config, self.workingDir, 'dwi', None, 'nii'): self.warning("Found some uncompressed nifti image into {} directory. " "Please gzip those images and resubmit the pipeline again".format(self.workingDir)) return False self.warning("No diffusion weight image found into {} directory".format(self.workingDir)) return False bEnc = util.getImage(self.config, self.workingDir,'grad', None, 'b') bVals = util.getImage(self.config, self.workingDir,'grad', None, 'bvals') bVecs = util.getImage(self.config, self.workingDir,'grad', None, 'bvecs') if (not bEnc) and (not bVals or not bVecs): self.warning("No valid .b encoding or (.bvals, .bvecs) files found in directory: {}".format(self.workingDir)) return False else: nbDirections = mriutil.getNbDirectionsFromDWI(dwi) if nbDirections <= 45: msg = "Found only {} directions into {} image. Hardi model will not be accurate with diffusion weighted image " \ "that contain less than 45 directions\n\n".format(nbDirections, dwi) self.warning(msg) #return False if bEnc and not self.__isValidEncoding(nbDirections, '.b'): self.warning("Encoding file {} is invalid".format(bEnc)) return False if bVals and not self.__isValidEncoding(nbDirections, '.bvals'): self.warning("Encoding file {} is invalid".format(bEnc)) return False if bVecs and not self.__isValidEncoding(nbDirections, '.bvecs'): self.warning("Encoding file {} is invalid".format(bVecs)) return False #Validate optionnal images images = { 'anat': (anat, 'high resolution'), 'dwi': (dwi,'diffusion weighted'), 'mag': (util.getImage(self.config, self.workingDir, 'mag'), 'MR magnitude '), 'phase': (util.getImage(self.config, self.workingDir, 'phase'), 'MR phase '), 'b0_ap': (util.getImage(self.config, self.workingDir, 'b0_ap'), "posterior to anterior b0 "), 'b0_pa': (util.getImage(self.config, self.workingDir, 'b0_pa'), "anterior to posterior b0")} if self.config.get('arguments', 'debug') == 'True': self.debug("Images found into {} directory: {}".format(self.workingDir, images)) if self.config.getboolean('arguments', 'prompt'): for key, (value, description) in images.iteritems(): if value: if not mriutil.isDataStridesOrientationExpected(value, self.config.get('preparation', 'stride_orientation'))\ and self.config.getboolean("preparation", "force_realign_strides"): msg = "Data strides layout for {} is unexpected and force_realign_strides is set to True.\n \ If you continue, all unexpected images will be realign accordingly.\n\ Only a copy of the original images will be alter.".format(value) if not util.displayYesNoMessage(msg): self.warning("Remove this subject from the list?") return False else: break #if one and only one b0 image is given, make sure that the b0 image is not on same direction than the dwi. if (not (images['b0_ap'][0] and images['b0_pa'][0])) and (images['b0_ap'][0] or images['b0_pa'][0]) \ and (self.config.get("correction", "ignore") == "False"): if ((self.config.get("correction", "phase_enc_dir") == "0") and images['b0_pa'][0]) \ or ((self.config.get("correction", "phase_enc_dir") == "1") and images['b0_ap'][0]): msg = "Found only one B0 image into the subject directory and that B0 is in " \ "the same phase encoding direction than the DWI.\n" \ "We recommend to remove the B0 image so at least a motion correction will be perform" if not util.displayYesNoMessage(msg): self.warning("Remove this subject from the list?") return False if images['mag'][0] and images['phase'][0] and (images['b0_ap'][0] or images['b0_pa'][0]): msg = "Found both Fieldmap and B0 images into the subject directory\n" \ "We recommend to disabled fieldmap correction?" if not util.displayYesNoMessage(msg): self.warning("Remove this subject from the list?") return False return True
def __isAValidStructure(self): """Determine if the directory is a valid structure Returns: a Boolean that represent if the subject integrity test pass for that directory """ #Anatomical, Dwi and gradient encoding direction are mandatory input anat = util.getImage(self.config, self.workingDir, 'anat') if not anat: if util.getImage(self.config, self.workingDir, 'anat', None, 'nii'): self.warning( "Found some uncompressed nifti images into {} directory. " "Please gzip those images and resubmit the pipeline again". format(self.workingDir)) return False self.warning( "No high resolution image found into {} directory".format( self.workingDir)) return False dwi = util.getImage(self.config, self.workingDir, 'dwi') if not dwi: if util.getImage(self.config, self.workingDir, 'dwi', None, 'nii'): self.warning( "Found some uncompressed nifti image into {} directory. " "Please gzip those images and resubmit the pipeline again". format(self.workingDir)) return False self.warning( "No diffusion weight image found into {} directory".format( self.workingDir)) return False bEnc = util.getImage(self.config, self.workingDir, 'grad', None, 'b') bVals = util.getImage(self.config, self.workingDir, 'grad', None, 'bvals') bVecs = util.getImage(self.config, self.workingDir, 'grad', None, 'bvecs') if (not bEnc) and (not bVals or not bVecs): self.warning( "No valid .b encoding or (.bvals, .bvecs) files found in directory: {}" .format(self.workingDir)) return False else: nbDirections = mriutil.getNbDirectionsFromDWI(dwi) if nbDirections <= 45: msg = "Found only {} directions into {} image. Hardi model will not be accurate with diffusion weighted image " \ "that contain less than 45 directions\n\n".format(nbDirections, dwi) self.warning(msg) #return False if bEnc and not self.__isValidEncoding(nbDirections, '.b'): self.warning("Encoding file {} is invalid".format(bEnc)) return False if bVals and not self.__isValidEncoding(nbDirections, '.bvals'): self.warning("Encoding file {} is invalid".format(bEnc)) return False if bVecs and not self.__isValidEncoding(nbDirections, '.bvecs'): self.warning("Encoding file {} is invalid".format(bVecs)) return False #Validate optionnal images images = { 'anat': (anat, 'high resolution'), 'dwi': (dwi, 'diffusion weighted'), 'mag': (util.getImage(self.config, self.workingDir, 'mag'), 'MR magnitude '), 'phase': (util.getImage(self.config, self.workingDir, 'phase'), 'MR phase '), 'b0_ap': (util.getImage(self.config, self.workingDir, 'b0_ap'), "posterior to anterior b0 "), 'b0_pa': (util.getImage(self.config, self.workingDir, 'b0_pa'), "anterior to posterior b0") } if self.config.get('arguments', 'debug') == 'True': self.debug("Images found into {} directory: {}".format( self.workingDir, images)) if self.config.getboolean('arguments', 'prompt'): for key, (value, description) in images.iteritems(): if value: if not mriutil.isDataStridesOrientationExpected(value, self.config.get('preparation', 'stride_orientation'))\ and self.config.getboolean("preparation", "force_realign_strides"): msg = "Data strides layout for {} is unexpected and force_realign_strides is set to True.\n \ If you continue, all unexpected images will be realign accordingly.\n\ Only a copy of the original images will be alter.".format( value) if not util.displayYesNoMessage(msg): self.warning("Remove this subject from the list?") return False else: break #if one and only one b0 image is given, make sure that the b0 image is not on same direction than the dwi. if (not (images['b0_ap'][0] and images['b0_pa'][0])) and (images['b0_ap'][0] or images['b0_pa'][0]) \ and (self.config.get("correction", "ignore") == "False"): if ((self.config.get("correction", "phase_enc_dir") == "0") and images['b0_pa'][0]) \ or ((self.config.get("correction", "phase_enc_dir") == "1") and images['b0_ap'][0]): msg = "Found only one B0 image into the subject directory and that B0 is in " \ "the same phase encoding direction than the DWI.\n" \ "We recommend to remove the B0 image so at least a motion correction will be perform" if not util.displayYesNoMessage(msg): self.warning("Remove this subject from the list?") return False if images['mag'][0] and images['phase'][0] and ( images['b0_ap'][0] or images['b0_pa'][0]): msg = "Found both Fieldmap and B0 images into the subject directory\n" \ "We recommend to disabled fieldmap correction?" if not util.displayYesNoMessage(msg): self.warning("Remove this subject from the list?") return False return True