Example #1
0
    def __isValidStructure(self):
        """Determine if the directory is a valid structure

        Returns:
            a Boolean that represent if validation is required for that directory

        """
        if not self.__validateImage(self.config.get('prefix', 'anat')):
            return False

        if not self.__validateImage(self.config.get('prefix', 'dwi')):
            return False

        dwi = util.getImage(self.config, self.workingDir, 'dwi')

        #@TODO fix data layout incomprehesion
        #make sure that diffusion image Z scale layout is oriented correctly
        #if not mriutil.isDataLayoutValid(dwiImage):
        #    self.error("Data layout for {} image is unexpected. "
        #                        "Only data layout = [ +0 +1 +2 +3 ] could be process".format(dwiImage))

        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, self.dwi)
            if self.config.getboolean('arguments', 'prompt'):
                util.displayYesNoMessage(msg)
            else:
                self.warning(msg)

        bEnc = util.getImage(self.config, self.workingDir, 'grad', None, 'b')
        bVal = util.getImage(self.config, self.workingDir, 'grad', None,
                             'bval')
        bVec = util.getImage(self.config, self.workingDir, 'grad', None,
                             'bvec')

        if (not bEnc) and (not bVal or not bVec):
            self.logger.warning("No valid .b encoding or pair of .bval, .bvec"
                                " file found in directory: {}".format(
                                    self.workingDir))
            return False
        #@TODO uncomment and fix not all zero bvec value in the first line
        """
        if bEnc and not self.__isValidEncoding(nbDirections, '.b'):
            self.logger.warning("Encoding file {} is invalid".format(bEnc))
            return False

        if bVal and not self.__isValidEncoding(nbDirections, '.bval'):
            self.logger.warning("Encoding file {} is invalid".format(bEnc))
            return False
        if bVec and not self.__isValidEncoding(nbDirections, '.bvec'):
            self.logger.warning("Encoding file {} is invalid".format(bVec))
            return False
        """
        return True
Example #2
0
    def __isValidStructure(self):
        """Determine if the directory is a valid structure

        Returns:
            a Boolean that represent if validation is required for that directory

        """
        if not self.__validateImage(self.config.get('prefix','anat')):
            return False

        if not self.__validateImage(self.config.get('prefix','dwi')):
            return False


        dwi = util.getImage(self.config, self.workingDir,'dwi')

        #@TODO fix data layout incomprehesion
        #make sure that diffusion image Z scale layout is oriented correctly
        #if not mriutil.isDataLayoutValid(dwiImage):
        #    self.error("Data layout for {} image is unexpected. "
        #                        "Only data layout = [ +0 +1 +2 +3 ] could be process".format(dwiImage))

        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, self.dwi)
            if self.config.getboolean('arguments', 'prompt'):
                util.displayYesNoMessage(msg)
            else:
                self.warning(msg)


        bEnc = util.getImage(self.config, self.workingDir,'grad', None, 'b')
        bVal = util.getImage(self.config, self.workingDir,'grad', None, 'bval')
        bVec = util.getImage(self.config, self.workingDir,'grad', None, 'bvec')

        if (not bEnc) and (not bVal or not bVec):
            self.logger.warning("No valid .b encoding or pair of .bval, .bvec"
                                " file found in directory: {}".format(self.workingDir))
            return False
        #@TODO uncomment and fix not all zero bvec value in the first line
        """
        if bEnc and not self.__isValidEncoding(nbDirections, '.b'):
            self.logger.warning("Encoding file {} is invalid".format(bEnc))
            return False

        if bVal and not self.__isValidEncoding(nbDirections, '.bval'):
            self.logger.warning("Encoding file {} is invalid".format(bEnc))
            return False
        if bVec and not self.__isValidEncoding(nbDirections, '.bvec'):
            self.logger.warning("Encoding file {} is invalid".format(bVec))
            return False
        """
        return True
Example #3
0
    def __reinitialize(self, subjects):
        """Reinitialize the study and every subjects into the pipeline

        Move every files and images back to the subjects directory and remove
        all subdirectory that may have been created by the pipeline.
        Remove logs directory created into the root directory of the study

        Args:
            subjects:  a list of subjects

        """

        if self.config.getboolean('arguments', 'prompt'):
            if util.displayYesNoMessage("Are you sure you want to reinitialize your data at is initial stage"):
                self.info("Reinitializing all subjects")
            else:
                self.quit("Exit the pipeline now as user request")
        else:
            self.warning("Prompt message have been disabled, reinitialisation will be force")

        for subject in subjects:
            tasksmanager = TasksManager(subject)
            for task in tasksmanager.getTasks():
                task.cleanup()
            
            print "Clean up subject log"
            subject.removeLogDir()
Example #4
0
    def __reinitialize(self, subjects):
        """Reinitialize the study and every subjects into the pipeline

        Move every files and images back to the subjects directory and remove
        all subdirectory that may have been created by the pipeline.
        Remove logs directory created into the root directory of the study

        Args:
            subjects:  a list of subjects

        """

        if self.config.getboolean('arguments', 'prompt'):
            if util.displayYesNoMessage("Are you sure you want to reinitialize your data at is initial stage"):
                self.info("Reinitializing all subjects")
            else:
                self.quit("Exit the pipeline now as user request")
        else:
            self.warning("Prompt message have been disabled, reinitialisation will be force")

        for subject in subjects:
            tasksmanager = TasksManager(subject)
            for task in tasksmanager.getTasks():
                task.cleanup()
            
            print "Clean up subject log"
            subject.removeLogDir()
Example #5
0
    def __subjectsFactory(self, directories):
        """Return a list of directories who qualified for the pipeline

        Args:
            directories: a list of directory to validate

        Returns:
            subjects: a list of directory containing qualified subjects

        """
        subjects=[]
        for directory in directories:
            subject = Subject(self.__copyConfig(directory))
            if subject.isAToadSubject():
                subject.activateLogDir()
                self.info("{} seem\'s a valid toad subject entry".format(directory))
                if self.config.getboolean('arguments', 'validation'):
                    if subject.isValidForPipeline():
                        self.info("{} is a valid subject, adding it to the list.".format(subject))
                        subjects.append(subject)

                    elif self.config.getboolean('arguments', 'prompt'):
                            msg = "It seem\'m like {} is having an issue and will probably fail!".format(subject)
                            if util.displayYesNoMessage(msg, "Would you like to remove it from the list (y or n)"):
                                self.info("Removing subject {} from the submitting list\n".format(subject))
                            else:
                                self.warning("Keeping {} even if we found issues will probably make the pipeline failing\n"
                                .format(subject))
                                subjects.append(subject)
                    else:
                        self.warning("Command prompt disabled, this submit will be submit anyway")
                        subjects.append(subject)
                else:
                    self.warning("Skipping validation have been requested, this is a unwise and dangerous decision")
                    subjects.append(subject)
        return subjects
Example #6
0
    def __subjectsFactory(self, directories):
        """Return a list of directories who qualified for the pipeline

        Args:
            directories: a list of directory to validate

        Returns:
            subjects: a list of directory containing qualified subjects

        """
        subjects=[]
        for directory in directories:
            subject = Subject(self.__copyConfig(directory))
            if subject.isAToadSubject():
                subject.activateLogDir()
                self.info("{} seem\'s a valid toad subject entry".format(directory))
                if self.config.getboolean('arguments', 'validation'):
                    if subject.isValidForPipeline():
                        self.info("{} is a valid subject, adding it to the list.".format(subject))
                        subjects.append(subject)

                    elif self.config.getboolean('arguments', 'prompt'):
                            msg = "It seem\'m like {} is having an issue and will probably fail!".format(subject)
                            if util.displayYesNoMessage(msg, "Would you like to remove it from the list (y or n)"):
                                self.info("Removing subject {} from the submitting list\n".format(subject))
                            else:
                                self.warning("Keeping {} even if we found issues will probably make the pipeline failing\n"
                                .format(subject))
                                subjects.append(subject)
                    else:
                        self.warning("Command prompt disabled, this submit will be submit anyway")
                        subjects.append(subject)
                else:
                    self.warning("Skipping validation have been requested, this is a unwise and dangerous decision")
                    subjects.append(subject)
        return subjects
Example #7
0
    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
Example #8
0
    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