Exemplo n.º 1
0
    def transform(self, catalog, subjects_id):
        catalog_ = copy.deepcopy(catalog)
        study_dir = make_dir(self.data_dir, self.study_id, strict=False)
        if isinstance(self.subject_key_, dict):
            save_table(self.subject_key_,
                       os.path.join(study_dir, 'subject_key.txt'))
        save_table(self.task_key_,
                   os.path.join(study_dir, 'task_key.txt'),
                   merge=self.merge_tasks)
        save_table({'TR': catalog_[0]['tr']},
                   os.path.join(study_dir, 'scan_key.txt'))

        model_dir = make_dir(study_dir, 'models', self.model_id, strict=False)
        save_task_contrasts(model_dir, catalog_[0], merge=self.merge_tasks)
        save_condition_key(model_dir, catalog_[0], merge=self.merge_tasks)

        n_jobs = -1 if self.n_jobs != 1 else 1

        self.encoder_ = IntraEncoder(hrf_model=self.hrf_model,
                                     drift_model=self.drift_model,
                                     memory=self.memory,
                                     n_jobs=n_jobs)

        all_niimgs = self.encoder_.fit_transform(catalog_, subjects_id)

        if subjects_id is None:
            subjects_id = [doc['subject_id'] for doc in catalog]

        outputs = Parallel(n_jobs=self.n_jobs)(
            delayed(_compute_glm)(LinearModeler(
                masker=self.masker,
                reporter=os.path.join(study_dir, subject_id, 'model',
                                      self.model_id),
                glm_model=self.glm_model,
                hrf_model=self.hrf_model,
                contrast_type=self.contrast_type,
                output_z=self.output_z,
                output_stat=self.output_stat,
                output_effects=self.output_effects,
                output_variance=self.output_variance),
                                  niimgs=niimgs,
                                  design_matrices=design_matrices,
                                  contrasts=doc['contrasts'])
            for subject_id, doc, niimgs, design_matrices in zip(
                subjects_id, catalog_, all_niimgs,
                self.encoder_.design_matrices_))

        if self.resample:
            Parallel(n_jobs=n_jobs)(delayed(_resample_img)(
                doc[dtype][cid],
                self.target_affine,
                self.target_shape,
            ) for doc in outputs for dtype in doc for cid in doc[dtype])

        return outputs
Exemplo n.º 2
0
    def transform(self, catalog, subjects_id):
        catalog_ = copy.deepcopy(catalog)
        study_dir = make_dir(self.data_dir, self.study_id, strict=False)
        if isinstance(self.subject_key_, dict):
            save_table(self.subject_key_,
                       os.path.join(study_dir, 'subject_key.txt'))
        save_table(self.task_key_, os.path.join(study_dir, 'task_key.txt'),
                   merge=self.merge_tasks)
        save_table({'TR': catalog_[0]['tr']},
                   os.path.join(study_dir, 'scan_key.txt'))

        model_dir = make_dir(study_dir, 'models', self.model_id, strict=False)
        save_task_contrasts(model_dir, catalog_[0], merge=self.merge_tasks)
        save_condition_key(model_dir, catalog_[0], merge=self.merge_tasks)

        n_jobs = -1 if self.n_jobs != 1 else 1

        self.encoder_ = IntraEncoder(hrf_model=self.hrf_model,
                                     drift_model=self.drift_model,
                                     memory=self.memory,
                                     n_jobs=n_jobs)

        all_niimgs = self.encoder_.fit_transform(catalog_, subjects_id)

        if subjects_id is None:
            subjects_id = [doc['subject_id'] for doc in catalog]

        outputs = Parallel(n_jobs=self.n_jobs)(
            delayed(_compute_glm)(
                LinearModeler(masker=self.masker,
                              reporter=os.path.join(
                                  study_dir, subject_id,
                                  'model', self.model_id),
                              glm_model=self.glm_model,
                              hrf_model=self.hrf_model,
                              contrast_type=self.contrast_type,
                              output_z=self.output_z,
                              output_stat=self.output_stat,
                              output_effects=self.output_effects,
                              output_variance=self.output_variance),
                niimgs=niimgs,
                design_matrices=design_matrices,
                contrasts=doc['contrasts'])
                for subject_id, doc, niimgs, design_matrices in zip(
                    subjects_id,
                    catalog_,
                    all_niimgs,
                    self.encoder_.design_matrices_))

        if self.resample:
            Parallel(n_jobs=n_jobs)(
                delayed(_resample_img)(
                    doc[dtype][cid], self.target_affine, self.target_shape, )
                for doc in outputs for dtype in doc for cid in doc[dtype])

        return outputs
Exemplo n.º 3
0
class IntraStats(object):
    def __init__(self,
                 data_dir,
                 study_id,
                 model_id,
                 masker=None,
                 hrf_model='canonical with derivative',
                 drift_model='cosine',
                 glm_model='ar1',
                 contrast_type='t',
                 output_z=True,
                 output_stat=False,
                 output_effects=False,
                 output_variance=False,
                 merge_tasks=False,
                 resample=False,
                 target_affine=None,
                 target_shape=None,
                 memory=Memory(cachedir=None),
                 n_jobs=1):
        self.data_dir = data_dir
        self.study_id = study_id
        self.model_id = model_id
        if masker is None:
            self.masker = MultiNiftiMasker()
        else:
            self.masker = masker
        self.hrf_model = hrf_model
        self.drift_model = drift_model
        self.glm_model = glm_model
        self.contrast_type = contrast_type
        self.output_z = output_z
        self.output_stat = output_stat
        self.output_effects = output_effects
        self.output_variance = output_variance
        self.merge_tasks = merge_tasks
        self.resample = resample
        self.target_affine = target_affine
        self.target_shape = target_shape
        self.memory = memory
        self.n_jobs = n_jobs

    def fit(self, catalog, subjects_id):
        doc = catalog[0]
        self.task_contrasts_ = doc.get('contrasts')
        self.condition_key_ = check_experimental_conditions(catalog)
        self.run_key_ = doc.get('runs')
        self.task_key_ = doc.get('tasks')
        if subjects_id is not None:
            self.subject_key_ = dict(
                zip(subjects_id, [doc['subject_id'] for doc in catalog]))
        else:
            self.subject_key_ = [doc['subject_id'] for doc in catalog]
        return self

    def transform(self, catalog, subjects_id):
        catalog_ = copy.deepcopy(catalog)
        study_dir = make_dir(self.data_dir, self.study_id, strict=False)
        if isinstance(self.subject_key_, dict):
            save_table(self.subject_key_,
                       os.path.join(study_dir, 'subject_key.txt'))
        save_table(self.task_key_,
                   os.path.join(study_dir, 'task_key.txt'),
                   merge=self.merge_tasks)
        save_table({'TR': catalog_[0]['tr']},
                   os.path.join(study_dir, 'scan_key.txt'))

        model_dir = make_dir(study_dir, 'models', self.model_id, strict=False)
        save_task_contrasts(model_dir, catalog_[0], merge=self.merge_tasks)
        save_condition_key(model_dir, catalog_[0], merge=self.merge_tasks)

        n_jobs = -1 if self.n_jobs != 1 else 1

        self.encoder_ = IntraEncoder(hrf_model=self.hrf_model,
                                     drift_model=self.drift_model,
                                     memory=self.memory,
                                     n_jobs=n_jobs)

        all_niimgs = self.encoder_.fit_transform(catalog_, subjects_id)

        if subjects_id is None:
            subjects_id = [doc['subject_id'] for doc in catalog]

        outputs = Parallel(n_jobs=self.n_jobs)(
            delayed(_compute_glm)(LinearModeler(
                masker=self.masker,
                reporter=os.path.join(study_dir, subject_id, 'model',
                                      self.model_id),
                glm_model=self.glm_model,
                hrf_model=self.hrf_model,
                contrast_type=self.contrast_type,
                output_z=self.output_z,
                output_stat=self.output_stat,
                output_effects=self.output_effects,
                output_variance=self.output_variance),
                                  niimgs=niimgs,
                                  design_matrices=design_matrices,
                                  contrasts=doc['contrasts'])
            for subject_id, doc, niimgs, design_matrices in zip(
                subjects_id, catalog_, all_niimgs,
                self.encoder_.design_matrices_))

        if self.resample:
            Parallel(n_jobs=n_jobs)(delayed(_resample_img)(
                doc[dtype][cid],
                self.target_affine,
                self.target_shape,
            ) for doc in outputs for dtype in doc for cid in doc[dtype])

        return outputs

    def fit_transform(self, catalog, subjects_id):
        return self.fit(catalog, subjects_id).transform(catalog, subjects_id)
Exemplo n.º 4
0
class IntraStats(object):

    def __init__(self, data_dir, study_id, model_id,
                 masker=None,
                 hrf_model='canonical with derivative',
                 drift_model='cosine', glm_model='ar1',
                 contrast_type='t', output_z=True, output_stat=False,
                 output_effects=False, output_variance=False,
                 merge_tasks=False, resample=False, target_affine=None,
                 target_shape=None, memory=Memory(cachedir=None), n_jobs=1):
        self.data_dir = data_dir
        self.study_id = study_id
        self.model_id = model_id
        if masker is None:
            self.masker = MultiNiftiMasker()
        else:
            self.masker = masker
        self.hrf_model = hrf_model
        self.drift_model = drift_model
        self.glm_model = glm_model
        self.contrast_type = contrast_type
        self.output_z = output_z
        self.output_stat = output_stat
        self.output_effects = output_effects
        self.output_variance = output_variance
        self.merge_tasks = merge_tasks
        self.resample = resample
        self.target_affine = target_affine
        self.target_shape = target_shape
        self.memory = memory
        self.n_jobs = n_jobs

    def fit(self, catalog, subjects_id):
        doc = catalog[0]
        self.task_contrasts_ = doc.get('contrasts')
        self.condition_key_ = check_experimental_conditions(catalog)
        self.run_key_ = doc.get('runs')
        self.task_key_ = doc.get('tasks')
        if subjects_id is not None:
            self.subject_key_ = dict(
                zip(subjects_id, [doc['subject_id'] for doc in catalog]))
        else:
            self.subject_key_ = [doc['subject_id'] for doc in catalog]
        return self

    def transform(self, catalog, subjects_id):
        catalog_ = copy.deepcopy(catalog)
        study_dir = make_dir(self.data_dir, self.study_id, strict=False)
        if isinstance(self.subject_key_, dict):
            save_table(self.subject_key_,
                       os.path.join(study_dir, 'subject_key.txt'))
        save_table(self.task_key_, os.path.join(study_dir, 'task_key.txt'),
                   merge=self.merge_tasks)
        save_table({'TR': catalog_[0]['tr']},
                   os.path.join(study_dir, 'scan_key.txt'))

        model_dir = make_dir(study_dir, 'models', self.model_id, strict=False)
        save_task_contrasts(model_dir, catalog_[0], merge=self.merge_tasks)
        save_condition_key(model_dir, catalog_[0], merge=self.merge_tasks)

        n_jobs = -1 if self.n_jobs != 1 else 1

        self.encoder_ = IntraEncoder(hrf_model=self.hrf_model,
                                     drift_model=self.drift_model,
                                     memory=self.memory,
                                     n_jobs=n_jobs)

        all_niimgs = self.encoder_.fit_transform(catalog_, subjects_id)

        if subjects_id is None:
            subjects_id = [doc['subject_id'] for doc in catalog]

        outputs = Parallel(n_jobs=self.n_jobs)(
            delayed(_compute_glm)(
                LinearModeler(masker=self.masker,
                              reporter=os.path.join(
                                  study_dir, subject_id,
                                  'model', self.model_id),
                              glm_model=self.glm_model,
                              hrf_model=self.hrf_model,
                              contrast_type=self.contrast_type,
                              output_z=self.output_z,
                              output_stat=self.output_stat,
                              output_effects=self.output_effects,
                              output_variance=self.output_variance),
                niimgs=niimgs,
                design_matrices=design_matrices,
                contrasts=doc['contrasts'])
                for subject_id, doc, niimgs, design_matrices in zip(
                    subjects_id,
                    catalog_,
                    all_niimgs,
                    self.encoder_.design_matrices_))

        if self.resample:
            Parallel(n_jobs=n_jobs)(
                delayed(_resample_img)(
                    doc[dtype][cid], self.target_affine, self.target_shape, )
                for doc in outputs for dtype in doc for cid in doc[dtype])

        return outputs

    def fit_transform(self, catalog, subjects_id):
        return self.fit(catalog, subjects_id).transform(catalog, subjects_id)
Exemplo n.º 5
0
        contrast_values.append(session_con)

    return contrast_values


if __name__ == '__main__':
    from nignore.openfmri import Loader, glob_subjects_dirs
    from nignore.spm import IntraEncoder

    n_jobs = 1

    root_dir = '/media/ys218403/mobile/brainpedia/preproc'
    result_dir = '/home/ys218403/Data/intra_stats'

    loader = Loader(model_id='model001')
    encoder = IntraEncoder()
    masker = MultiNiftiMasker(mask='mask.nii.gz', standardize=True,
                              smoothing_fwhm=6, n_jobs=n_jobs)

    def sanitize_contrast(contrast, insert_derivative=True):
        angry_contrasts = {}
        for contrast_id in contrasts:
            if ('house_vs_baseline' in contrast_id
                    or 'face_vs_baseline' in contrast_id
                    or 'face_vs_house' in contrast_id):
                contrast = []
                for session_con in contrasts[contrast_id]:
                    if session_con is not None:
                        session_con = np.array(session_con)
                        session_con = np.insert(
                            session_con,