def __implement(self): """Generic implementation of a tasks -A task should create and move into is own working space """ self.info("Launching implementation of {} task".format(self.getName())) if not os.path.exists(self.workingDir): self.info("Creating {} directory".format(self.workingDir)) os.mkdir(self.workingDir) if self.config.has_option("arguments", "stop_before_task"): if (self.config.get("arguments", "stop_before_task") == self.__name or self.config.get("arguments", "stop_before_task") == self.__moduleName.lower()): self.quit("Reach {} which is the value set by stop_before_task. Stopping the pipeline as user request" .format(self.config.get("arguments", "stop_before_task"))) os.chdir(self.workingDir) util.symlink(self.getLogFileName(), self.workingDir) if "qaSupplier" in dir(self): self.updateQaMenu() self.implement() self.info("Create and supply images to the qa report ") if "qaSupplier" in dir(self): self.createQaReport(self.qaSupplier()) else: self.info("task {} does not implement qaSupplier method".format(self.getName())) os.chdir(self.subjectDir)
def implement(self): images = {'aparc_aseg':self.getImage(self.dependDir,'aparc_aseg'), 'anat_freesurfer':self.getImage(self.dependDir,'anat_freesurfer'), 'brodmann':self.getImage(self.dependDir,'brodmann')} for key, value in images.iteritems(): if value: self.info("Found {} area image, create link from {} to {}".format(key, value, self.workingDir)) util.symlink(value, self.workingDir) if not (images['aparc_aseg'] and images['anat_freesurfer'] and images['brodmann']): self.info("Set SUBJECTS_DIR to {}".format(self.workingDir)) os.environ["SUBJECTS_DIR"] = self.workingDir anat = self.getImage(self.dependDir, 'anat') self.__reconAll(anat) self.__createBrodmannArea() dicts = {'anat_freesurfer': "{}/{}/mri/T1.mgz", 'aparc_aseg': "{}/{}/mri/aparc+aseg.mgz", 'brodmann': "{}/{}/mri/brodmann.mgz"} for key, value in dicts.iteritems(): images = glob.glob(value.format(self.workingDir, self.id)) if len(images) > 0: self.__mgz2nii(images.pop(), os.path.join(self.workingDir, self.get(key)))
def implement(self): dwi = self.__linkDwiImage() bVals= self.getCorrectionImage('grad', None, 'bvals') bVecs= self.getCorrectionImage('grad', None, 'bvecs') bEnc = self.getCorrectionImage('grad', None, 'benc') if not bVals or not bVecs: bVals= self.getPreparationImage('grad', None, 'bvals') bVecs= self.getPreparationImage('grad', None, 'bvecs') bEnc= self.getPreparationImage('grad', None, 'benc') bVals = util.symlink(bVals, self.workingDir) bVecs = util.symlink(bVecs, self.workingDir) bEnc = util.symlink(bEnc, self.workingDir) if self.get('skipUpsampling'): dwiUpsample = self.rename(os.path.join(self.workingDir, os.path.basename(dwi)), self.buildName(dwi, "upsample")) else: interp = self.get('interp') template = self.getParcellationImage('anat','freesurfer','nii.gz') voxelSize = mriutil.getMriVoxelSize(template) # Get t1 voxel size to upsample voxelSize = str(voxelSize).translate(None, '[],') # Remove specific caracteres dwiUpsample= self.__upsampling(dwi, voxelSize, interp, self.buildName(dwi, "upsample")) b0Upsample = os.path.join(self.workingDir, os.path.basename(dwiUpsample).replace(self.get("prefix", 'dwi'), self.get("prefix", 'b0'))) self.info(mriutil.extractFirstB0FromDwi(dwiUpsample, b0Upsample, bVals))
def __implement(self): """Generic implementation of a tasks -A task should create and move into is own working space """ self.info("Launching implementation of {} task".format(self.getName())) if not os.path.exists(self.workingDir): self.info("Creating {} directory".format(self.workingDir)) os.mkdir(self.workingDir) os.chdir(self.workingDir) util.symlink(self.getLogFileName(), self.workingDir) if "qaSupplier" in dir(self): self.updateQaMenu() self.implement() # Save ConfigRunning File configRunningPath = os.path.join(self.subjectDir, '00-backup', 'configRunning.cfg') self.subject.writeConfigRunning(configRunningPath) self.info("Create and supply images to the qa report ") if "qaSupplier" in dir(self): self.createQaReport(self.qaSupplier()) else: self.info("task {} does not implement qaSupplier method".format( self.getName())) os.chdir(self.subjectDir)
def __produceEncodingFiles(self, bEncs, bVecs, bVals, dwi): self.info("Produce .b .bvals and .bvecs gradient file if not existing") if not bEncs: bEncs = self.buildName(dwi, None, "b") self.info(mriutil.fslToMrtrixEncoding(dwi, bVecs, bVals, bEncs)) else: self.info("Linking {} to {}".format( bEncs, util.symlink(bEncs, self.workingDir))) if not bVecs or not bVals: self.info( mriutil.mrtrixToFslEncoding(dwi, bEncs, self.buildName(dwi, None, "bvecs"), self.buildName(dwi, None, "bvals"))) else: self.info("Linking {} to {}".format( bVecs, util.symlink(bVecs, self.workingDir))) self.info("Linking {} to {}".format( bVals, util.symlink(bVals, self.workingDir))) return (self.getImage('grad', None, 'b'), self.getImage('grad', None, 'bvecs'), self.getImage('grad', None, 'bvals'))
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): """Generic implementation of a tasks -A task should create and move into is own working space """ self.info("Launching implementation of {} task".format(self.getName())) if not os.path.exists(self.workingDir): self.info("Creating {} directory".format(self.workingDir)) os.mkdir(self.workingDir) os.chdir(self.workingDir) util.symlink(self.getLogFileName(), self.workingDir) if "qaSupplier" in dir(self): self.updateQaMenu() self.implement() # Save ConfigRunning File configRunningPath = os.path.join( self.subjectDir, '00-backup', 'configRunning.cfg') self.subject.writeConfigRunning(configRunningPath) self.info("Create and supply images to the qa report ") if "qaSupplier" in dir(self): self.createQaReport(self.qaSupplier()) else: self.info("task {} does not implement qaSupplier method".format(self.getName())) os.chdir(self.subjectDir)
def __getConfigFile(self, prefix, defaultFile): target = self.getPreparationImage(prefix, None, "json") if target: util.symlink(target, self.buildName(target, None, "json")) else: defaultFileName = "{}.json".format(defaultFile) defaultFileLink = os.path.join(self.toadDir, "templates", "tractometry", defaultFileName) target = defaultFileLink util.copy(defaultFileLink, self.workingDir, defaultFileName) return target
def __findAndLinkFreesurferStructure(self): """Look if a freesurfer structure already exists in the backup. freesurfer structure will be link and id will be update Returns: Return the linked directory name if a freesurfer structure is found, False otherwise """ freesurferStructure = os.path.join(self.preparationDir, self.id) if mriutil.isAfreesurferStructure(freesurferStructure): self.info("{} seem\'s a valid freesurfer structure: moving it to {} directory".format(freesurferStructure, self.workingDir)) util.symlink(freesurferStructure, self.id) return True return False
def __implement(self): """Generic implementation of a tasks -A task should create and move into is own working space """ if not os.path.exists(self.workingDir): self.info("Creating {} directory".format(self.workingDir)) os.mkdir(self.workingDir) currentDir = os.getcwd() os.chdir(self.workingDir) util.symlink(self.getLogFileName(), self.workingDir) self.implement() os.chdir(currentDir)
def __linkDwiImage(self): dwi = self.getCorrectionImage('dwi', 'corrected') if not dwi: dwi = self.getDenoisingImage('dwi', 'denoise') if not dwi: dwi = self.getPreparationImage('dwi') return util.symlink(dwi, self.workingDir)
def __getConfigFile(self, prefix, defaultFile): target = self.getPreparationImage(prefix, None, 'json') if target: util.symlink(target, self.buildName(target, None, 'json')) else: defaultFileName = '{}.json'.format(defaultFile) defaultFileLink = os.path.join( self.toadDir, "templates", "tractometry", defaultFileName, ) target = defaultFileLink util.copy(defaultFileLink, self.workingDir, defaultFileName) return target
def __findAndLinkFreesurferStructure(self): """Look if a freesurfer structure already exists in the backup. freesurfer structure will be link and id will be update Returns: Return the linked directory name if a freesurfer structure is found, False otherwise """ freesurferStructure = os.path.join(self.preparationDir, self.id) if mriutil.isAfreesurferStructure(freesurferStructure): self.info( "{} seem\'s a valid freesurfer structure: moving it to {} directory" .format(freesurferStructure, self.workingDir)) util.symlink(freesurferStructure, self.workingDir, self.id) return True return False
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.__linkDwiImage() bVals= self.getCorrectionImage('grad', None, 'bvals') bVecs= self.getCorrectionImage('grad', None, 'bvecs') bEnc = self.getCorrectionImage('grad', None, 'benc') if not bVals or not bVecs: bVals= self.getPreparationImage('grad', None, 'bvals') bVecs= self.getPreparationImage('grad', None, 'bvecs') bEnc= self.getPreparationImage('grad', None, 'benc') bVals = util.symlink(bVals, self.workingDir) bVecs = util.symlink(bVecs, self.workingDir) bEnc = util.symlink(bEnc, self.workingDir) dwiUpsample= self.__upsampling(dwi, self.get('voxel_size'), self.buildName(dwi, "upsample")) b0Upsample = os.path.join(self.workingDir, os.path.basename(dwiUpsample).replace(self.get("prefix", 'dwi'), self.get("prefix", 'b0'))) self.info(mriutil.extractFirstB0FromDwi(dwiUpsample, b0Upsample, bVals))
def __linkExistingImage(self, images): """ Create symbolic link for each existing input images into the current working directory. Args: images: A list of image Returns: A list of invalid images """ unlinkedImages = {} # look for existing map store into preparation and link it so they are not created for key, value in images.iteritems(): if value: self.info("Found {} area image, create link from {} to {}".format(key, value, self.workingDir)) util.symlink(value, self.workingDir) else: unlinkedImages[key] = value return unlinkedImages
def __produceEncodingFiles(self, bEncs, bVecs, bVals, dwi): self.info("Produce .b .bvals and .bvecs gradient file if not existing") if not bEncs: bEncs = self.buildName(dwi, None, "b") self.info(mriutil.fslToMrtrixEncoding(dwi, bVecs, bVals, bEncs)) else: self.info("Linking {} to {}".format(bEncs, util.symlink(bEncs, self.workingDir))) if not bVecs or not bVals: self.info(mriutil.mrtrixToFslEncoding(dwi, bEncs, self.buildName(dwi, None, "bvecs"), self.buildName(dwi, None, "bvals"))) else: self.info("Linking {} to {}".format(bVecs, util.symlink(bVecs, self.workingDir))) self.info("Linking {} to {}".format(bVals, util.symlink(bVals, self.workingDir))) return (self.getImage('grad', None, 'b'), self.getImage('grad', None, 'bvecs'), self.getImage('grad', None, 'bvals'))
def __linkExistingImage(self, images): """ Create symbolic link for each existing input images into the current working directory. Args: images: A list of image Returns: A list of invalid images """ unlinkedImages = {} # look for existing map store into preparation and link it so they are not created for key, value in images.iteritems(): if value: self.info( "Found {} area image, create link from {} to {}".format( key, value, self.workingDir)) util.symlink(value, self.workingDir) else: unlinkedImages[key] = value return unlinkedImages
def implement(self): images = { 'aparc_aseg': self.getImage(self.dependDir, 'aparc_aseg'), 'anat_freesurfer': self.getImage(self.dependDir, 'anat_freesurfer'), 'brodmann': self.getImage(self.dependDir, 'brodmann') } for key, value in images.iteritems(): if value: self.info( "Found {} area image, create link from {} to {}".format( key, value, self.workingDir)) util.symlink(value, self.workingDir) if not (images['aparc_aseg'] and images['anat_freesurfer'] and images['brodmann']): self.info("Set SUBJECTS_DIR to {}".format(self.workingDir)) os.environ["SUBJECTS_DIR"] = self.workingDir anat = self.getImage(self.dependDir, 'anat') self.__reconAll(anat) self.__createBrodmannArea() dicts = { 'anat_freesurfer': "{}/{}/mri/T1.mgz", 'aparc_aseg': "{}/{}/mri/aparc+aseg.mgz", 'brodmann': "{}/{}/mri/brodmann.mgz" } for key, value in dicts.iteritems(): images = glob.glob(value.format(self.workingDir, self.id)) if len(images) > 0: self.__mgz2nii( images.pop(), os.path.join(self.workingDir, self.get(key)))
def implement(self): self.info("Produce .b .bval and .bvec gradient file if not existing") self.__produceEncodingFiles() dwi = self.getImage(self.dependDir, 'dwi') b0PA = self.getImage(self.dependDir, 'b0PA') b0AP = self.getImage(self.dependDir, 'b0AP') if b0PA: self.info( "Found B0 posterior to anterior image, linking file {} to {}". format(b0AP, self.workingDir)) util.symlink(b0PA, self.workingDir) if b0AP: self.info( "Found B0 anterior to posterior image, linking file {} to {}". format(b0AP, self.workingDir)) util.symlink(b0AP, self.workingDir) if b0PA and (b0AP is False): #Produire la carte AP si elle n'existe pas self.info( "No anterior posterior image found, I will try to produce one") b0AP = self.__extractB0APSubVolumeFromDWI(dwi) images = { 'high resolution': self.getImage(self.dependDir, 'anat'), 'diffusion weighted': dwi, 'MR magnitude ': self.getImage(self.dependDir, 'mag'), 'MR phase ': self.getImage(self.dependDir, 'phase'), 'parcellation': self.getImage(self.dependDir, 'aparc_aseg'), 'anatomical': self.getImage(self.dependDir, 'anat_freesurfer'), 'brodmann': self.getImage(self.dependDir, 'brodmann') } for key, value in images.iteritems(): if value: self.info("Found {} image, linking file {} to {}".format( key, value, self.workingDir)) util.symlink(value, self.workingDir)
def __produceEncodingFiles(self): #produire les fichiers gradient encoding pour dipy ainsi que mrtrix bEnc = self.getImage(self.dependDir, 'grad', None, 'b') bVal = self.getImage(self.dependDir, 'grad', None, 'bval') bVec = self.getImage(self.dependDir, 'grad', None, 'bvec') if not bEnc: mriutil.bValBVec2BEnc(bVal, bVec, self.workingDir) else: util.symlink(bEnc, self.workingDir) if not bVal: mriutil.bEnc2BVal(bEnc, self.workingDir) else: util.symlink(bVal, self.workingDir) if not bVec: mriutil.bEnc2BVec(bEnc, self.workingDir) else: util.symlink(bVec, self.workingDir)