def setUp(self): self.test_case = self.create_test_case() self.test_case.create_study() self.study = self.test_case.study self.subject = Subject(self.test_case.subjectnames[0], self.test_case.groupnames[0], self.test_case.filenames[0])
def add_subjects_from_filenames(self, filenames, groupname): parent = QtCore.QModelIndex() start_index = self.rowCount() end_index = start_index + len(filenames) self.beginInsertRows(parent, start_index, end_index) for filename in filenames: subject = Subject.from_filename(filename, groupname) self._add_subject(subject) self.endInsertRows()
def _assert_subjects_exist(self, study): for filename in self.test_case.filenames: subject = Subject.from_filename(filename) subject.groupname = Subject.DEFAULT_GROUP subject_id = subject.id() self.assert_(subject_id in study.subjects) study_subject = study.subjects[subject_id] self.assert_(os.path.exists(study_subject.filename)) self.assert_(study_subject.filename.startswith(self.test_case.output_directory))
def unserialize(cls, serialized, output_directory): try: version = serialized['study_format_version'] except: msg = "unknown study format version" raise StudySerializationError(msg) if version != STUDY_FORMAT_VERSION: msg = "find unsupported study format version '%s'" % version raise StudySerializationError(msg) study = cls(analysis_type=serialized['analysis_type'], study_name=serialized['study_name'], output_directory=output_directory) serialized_dict = dict( [(key, value) for key, value in six.iteritems(serialized) if key not in ('subjects', 'study_format_version', 'analysis_type', 'inputs', 'outputs')]) study.set_study_configuration(serialized_dict) for subject_id, serialized_subject in \ six.iteritems(serialized['subjects']): subject = Subject.unserialize( serialized_subject, study.output_directory) study.subjects[subject_id] = subject if 'parameters' not in serialized: raise StudySerializationError( "Cannot find parameters section in study file") for subject_id, subject in six.iteritems(study.subjects): if subject_id not in serialized['parameters']: raise StudySerializationError( "Cannot find params for subject %s" % subject_id) analysis = study._create_analysis() analysis.subject = subject parameters = serialized['parameters'][subject_id] analysis.parameters = Study.unserialize_paths( parameters, study.output_directory) study.analyses[subject_id] = analysis return study
class TestStudy(unittest.TestCase): def setUp(self): self.test_case = self.create_test_case() self.test_case.create_study() self.study = self.test_case.study self.subject = Subject(self.test_case.subjectnames[0], self.test_case.groupnames[0], self.test_case.filenames[0]) def tearDown(self): shutil.rmtree(self.study.output_directory) def create_test_case(self): test_case = MockStudyTestCase() return test_case def test_subject_exists_error(self): self.study.add_subject(self.subject) self.assertRaises(SubjectExistsError, self.study.add_subject, self.subject) def test_save_load_study(self): self.test_case.add_subjects() studyfilepath = self.study.backup_filepath studyfilepath2 = studyfilepath + "_2" if os.path.isfile(studyfilepath): os.remove(studyfilepath) if os.path.isfile(studyfilepath2): os.remove(studyfilepath2) self.study.save_to_backup_file() shutil.move(studyfilepath, studyfilepath2) loaded_study = Study.from_file(studyfilepath2) loaded_study.save_to_backup_file() self.assert_(filecmp.cmp(studyfilepath, studyfilepath2)) def test_has_subjects(self): self.assert_(not self.study.has_subjects()) self.study.add_subject(self.subject) self.assert_(self.study.has_subjects()) def test_has_some_results(self): self.test_case.add_subjects() self.test_case.create_some_output_files() self.assert_(self.study.has_some_results()) def test_remove_subject(self): self.study.add_subject(self.subject) self.study.remove_subject_from_id(self.subject.id()) self.assert_(not self.study.has_subjects()) def test_create_study_from_organized_directory(self): self.test_case.add_subjects() if self.study.analysis_type == 'MockAnalysis': new_study = MockStudy.from_organized_directory( self.study.analysis_type, self.study.output_directory) else: new_study = self.study.from_organized_directory( self.study.analysis_type, self.study.output_directory) self._assert_same_studies(new_study, self.study) def _assert_same_studies(self, study_a, study_b): self.assert_(study_a.output_directory == study_b.output_directory) self.assert_(len(study_a.subjects) == len(study_b.subjects)) for subject_id, subject_a in six.iteritems(study_a.subjects): subject_b = study_b.subjects[subject_id] self.assert_(subject_a == subject_b)
def get_subjects_from_pattern(self, exact_match=False, progress_callback=None): if progress_callback is not None: if isinstance(progress_callback, tuple): progress_callback, init_progress, scl_progess \ = progress_callback else: init_progress = 0. scl_progess = 1. subjects = [] MODALITY = 't1mri' ACQUISITION = 'default_acquisition' any_dir = "([^/\\\\]+)" re_sep = os.sep.replace('\\', '\\\\') if progress_callback: progress_callback(init_progress) if exact_match: glob_pattern = os.path.join( self.output_directory, "*", "*", MODALITY, ACQUISITION, "*.nii") regexp = re.compile( "^" + self.output_directory.replace('\\', '\\\\') + re_sep + any_dir + re_sep + any_dir + re_sep + MODALITY + re_sep + ACQUISITION + re_sep + "\\2\.(nii)$") else: glob_pattern = os.path.join( self.output_directory, "*", "*", MODALITY, "*", "*.*") regexp = re.compile( "^" + self.output_directory.replace('\\', '\\\\') + re_sep + any_dir + re_sep + any_dir + re_sep + MODALITY + re_sep + any_dir + re_sep + "\\2\.((?:nii(?:\.gz)?)|(?:ima))$") vol_format = None formats_dict = self.modules_data.fom_atp['input'].foms.formats ext_dict = dict([(ext, name) for name, ext in six.iteritems(formats_dict) if not name.lower().startswith('series ')]) # fix ambiguities in formats/ext dict ext_dict.update({ "nii.gz": "NIFTI gz", "nii": "NIFTI", "ima": "GIS", "mnc": "MINC", "gii": "GIFTI", "mesh": "MESH", "ply": "PLY", }) subjects_ids = set() files_list = list(glob.iglob(glob_pattern)) nfiles = len(files_list) progress = 0.5 for n, filename in enumerate(files_list): if progress_callback: progress_callback( init_progress + (progress + (1. - progress) * n / nfiles) * scl_progess) match = regexp.match(filename) if match: groupname = match.group(1) subjectname = match.group(2) if exact_match: format_ext = match.group(3) else: format_ext = match.group(4) format_name = ext_dict.get(format_ext, 'NIFTI') if vol_format is None: vol_format = format_name self.volumes_format = vol_format elif vol_format != format_name: print('Warning: subject %s input MRI does not have ' 'the expected format: %s, expecting %s' % (subjectname, format_name, vol_format)) continue # skip this subject subject = Subject(subjectname, groupname, filename) subject_id = subject.id() if subject_id in subjects_ids: print('Warning: %s / %s exists several times (in ' 'different acquisitions probably) - keeping only ' 'one.' % (subjectname, groupname)) continue subjects_ids.add(subject_id) subjects.append(subject) if progress_callback: progress_callback(init_progress + scl_progess) return subjects
def test_subjects_different_id(self): subject1 = Subject(self.subjectname, self.groupname, self.filename) subject2 = Subject(self.subjectname+"2", self.groupname, self.filename+"2") self.assert_(subject1.id() != subject2.id())
def test_subjects_same_id(self): subject1 = Subject(self.subjectname, self.groupname, self.filename) subject2 = Subject(self.subjectname, self.groupname, self.filename+"2") self.assert_(subject1.id() == subject2.id())
def test_copy_subject(self): subject1 = Subject(self.subjectname, self.groupname, self.filename) subject2 = subject1.copy() self.assert_(subject1 == subject2)