Beispiel #1
0
 def test_JDEMCMCAnalyzerXML(self):
     from pyhrf.ui.jde import JDEMCMCAnalyser
     from pyhrf.jde.models import BOLDGibbsSampler
     a = JDEMCMCAnalyser(sampler=BOLDGibbsSampler(nb_iterations=42))
     axml = xmlio.to_xml(a, pretty=True)
     a2 = xmlio.from_xml(axml)
     self.assertEqual(a2.sampler.nbIterations, 42)
Beispiel #2
0
 def test_TreatmentXML(self):
     from pyhrf.ui.jde import JDEMCMCAnalyser
     from pyhrf.jde.models import BOLDGibbsSampler
     from pyhrf.ui.treatment import FMRITreatment
     a = JDEMCMCAnalyser(sampler=BOLDGibbsSampler(nb_iterations=42))
     t = FMRITreatment(analyser=a)
     txml = xmlio.to_xml(t, pretty=True)
     t2 = xmlio.from_xml(txml)
     self.assertEqual(t2.analyser.sampler.nbIterations, 42)
Beispiel #3
0
    def test_JDEMCMCAnalyzer_Uinode_bijection(self):
        from pyhrf.ui.jde import JDEMCMCAnalyser
        from pyhrf.jde.models import BOLDGibbsSampler
        a = JDEMCMCAnalyser(sampler=BOLDGibbsSampler(nb_iterations=42))
        a_ui = xmlio.UiNode.from_py_object('analyse', a)
        it_node = a_ui.get_children()[0].get_children()[0].get_children()[0]
        self.assertEqual(it_node.label(), str(42))

        a2 = xmlio.Initable.from_ui_node(a_ui)
        self.assertEqual(a2.sampler.nbIterations, 42)
Beispiel #4
0
def jde_analyse(fdata, contrasts, output_dir):
    from pyhrf.jde.models import BOLDGibbsSampler as BG
    from pyhrf.jde.hrf import RHSampler
    from pyhrf.jde.nrl.bigaussian import NRLSampler

    sampler = BG(nb_iterations=250,
                 hrf_var=RHSampler(do_sampling=False, val_ini=np.array([0.05])),
                 response_levels=NRLSampler(contrasts=contrasts))

    analyser = JDEMCMCAnalyser(sampler=sampler)
    analyser.set_gzip_outputs(True)
    tt = FMRITreatment(fdata, analyser, output_dir=output_dir)
    tt.run(parallel='local')
Beispiel #5
0
    def test_default_jde_small_simulation(self):
        """ Test JDE multi-sessions sampler on small
        simulation with small nb of iterations.
        Estimation accuracy is not tested.
        """

        sampler = BMSS()

        analyser = JDEMCMCAnalyser(sampler=sampler, osfMax=4, dtMin=.4,
                                   dt=.5, driftParam=4, driftType='polynomial',
                                   outputPrefix='jde_MS_mcmc_',
                                   randomSeed=9778946)

        treatment = FMRITreatment(fmri_data=self.data_simu,
                                  analyser=analyser)

        treatment.run()
Beispiel #6
0
    def test_default_jde_small_simulation(self):
        """ Test ASL sampler on small simulation with small nb of iterations.
        Estimation accuracy is not tested.
        """

        simu = simulate_asl(spatial_size='random_small')
        fdata = FmriData.from_simulation_dict(simu)

        sampler = jde_asl.ASLSampler()

        analyser = JDEMCMCAnalyser(sampler=sampler, osfMax=4, dtMin=.4,
                                   dt=.5, driftParam=4, driftType='polynomial',
                                   outputPrefix='jde_mcmc_', randomSeed=None)

        treatment = FMRITreatment(fmri_data=fdata, analyser=analyser,
                                  output_dir=None)

        treatment.run()
Beispiel #7
0
    def test_default_jde_small_simulation(self):
        """ Test ASL Physio sampler on small simulation with small nb of
        iterations. Estimation accuracy is not tested.
        """

        sampler_params = {
            'nb_iterations': 3,
            'smpl_hist_pace': 1,
            'obs_hist_pace': 1,
            'brf': jde_asl_physio.PhysioBOLDResponseSampler(zero_constraint=False),
            'brf_var': jde_asl_physio.PhysioBOLDResponseVarianceSampler(val_ini=np.array([1e-3])),
            'prf': jde_asl_physio.PhysioPerfResponseSampler(zero_constraint=False),
            'prf_var': jde_asl_physio.PhysioPerfResponseVarianceSampler(val_ini=np.array([1e-3])),
            'noise_var': jde_asl_physio.NoiseVarianceSampler(),
            'drift_var': jde_asl_physio.DriftVarianceSampler(),
            'drift': jde_asl_physio.DriftCoeffSampler(),
            'bold_response_levels': jde_asl_physio.BOLDResponseLevelSampler(),
            'perf_response_levels': jde_asl_physio.PerfResponseLevelSampler(),
            'bold_mixt_params': jde_asl_physio.BOLDMixtureSampler(),
            'perf_mixt_params': jde_asl_physio.PerfMixtureSampler(),
            'labels': jde_asl_physio.LabelSampler(),
            'perf_baseline': jde_asl_physio.PerfBaselineSampler(),
            'perf_baseline_var': jde_asl_physio.PerfBaselineVarianceSampler(),
            'check_final_value': None,
        }

        sampler = jde_asl_physio.ASLPhysioSampler(**sampler_params)

        simu_items = phym.simulate_asl_physio_rfs(spatial_size='random_small')
        simu_fdata = FmriData.from_simulation_dict(simu_items)

        dt = simu_items['dt']
        analyser = JDEMCMCAnalyser(sampler=sampler, osfMax=4, dtMin=.4,
                                   dt=dt, driftParam=4, driftType='polynomial',
                                   outputPrefix='jde_mcmc_')

        treatment = FMRITreatment(fmri_data=simu_fdata, analyser=analyser,
                                  output_dir=None)
        treatment.run()
Beispiel #8
0
    def __init__(self,
                 fmri_data=FmriData.from_vol_ui(),
                 analyser=JDEMCMCAnalyser(),
                 output_dir='./',
                 make_outputs=True,
                 result_dump_file=DEFAULT_DUMP_FILE):

        xmlio.XmlInitable.__init__(self)

        self.analyser = analyser
        self.output_dir = output_dir

        if result_dump_file is None:
            if self.output_dir is not None and DEFAULT_DUMP_FILE is not None:
                self.result_dump_file = op.join(self.output_dir,
                                                DEFAULT_DUMP_FILE)
            else:
                self.result_dump_file = None
        else:
            self.result_dump_file = op.join(self.output_dir, result_dump_file)

        self.data = fmri_data

        self.make_outputs = make_outputs
Beispiel #9
0
    def _test_specific_samplers(self, sampler_names, fdata,
                                nb_its=None, use_true_val=None,
                                save_history=False, check_fv=None,
                                normalize_brf=1., normalize_prf=1.,
                                normalize_mu=1., prf_prior_type='regularized',
                                brf_prior_type='regularized',
                                mu_prior_type='regularized'):
        """
        Test specific samplers.
        """
        if use_true_val is None:
            use_true_val = dict((n, False) for n in sampler_names)

        logger.info('_test_specific_samplers %s ...', str(sampler_names))

        params = deepcopy(self.sampler_params_for_single_test)

        # Loop over given samplers to enable them
        for var_name in sampler_names:
            var_class = params[var_name].__class__
            use_tval = use_true_val[var_name]

            # special case for HRF -> normalization and prior type
            if var_class == jaslh.PhysioBOLDResponseSampler:
                params[var_name] = \
                    jaslh.PhysioBOLDResponseSampler(do_sampling=True,
                                                    use_true_value=use_tval,
                                                    normalise=normalize_brf,
                                                    zero_constraint=False,
                                                    prior_type=brf_prior_type)
            elif var_class == jaslh.PhysioPerfResponseSampler:
                params[var_name] = \
                    jaslh.PhysioPerfResponseSampler(do_sampling=True,
                                                    use_true_value=use_tval,
                                                    normalise=normalize_brf,
                                                    zero_constraint=False,
                                                    prior_type=prf_prior_type)
            elif var_class == jaslh.PhysioTrueBOLDResponseSampler:
                params[var_name] = \
                    jaslh.PhysioTrueBOLDResponseSampler(do_sampling=True,
                                                        use_true_value=use_tval,
                                                        normalise=normalize_mu,
                                                        zero_constraint=False,
                                                        prior_type=mu_prior_type)

            else:
                params[var_name] = var_class(do_sampling=True,
                                             use_true_value=use_tval)

        if nb_its is not None:
            params['nb_iterations'] = nb_its

        if save_history:
            params['smpl_hist_pace'] = 1
            params['obs_hist_pace'] = 1

        if check_fv is not None:
            params['check_final_value'] = check_fv

        sampler = jaslh.ASLPhysioSampler(**params)

        output_dir = self.tmp_dir

        analyser = JDEMCMCAnalyser(sampler=sampler, osfMax=4, dtMin=.4,
                                   dt=fdata.simulation[0]['dt'], driftParam=4,
                                   driftType='polynomial',
                                   outputPrefix='jde_mcmc_',
                                   pass_error=False)

        treatment = FMRITreatment(fmri_data=fdata, analyser=analyser,
                                  output_dir=output_dir)

        outputs = treatment.run()
        print 'out_dir:', output_dir
        return outputs
    def _test_specific_samplers(self,
                                sampler_names,
                                simu,
                                nb_its=None,
                                use_true_val=None,
                                save_history=False,
                                check_fv=None,
                                normalize_hrf=1.,
                                hrf_prior_type='singleHRF',
                                normalize_hrf_group=1.,
                                hrf_group_prior_type='singleHRF',
                                reg_hgroup=True):
        """
        Test specific samplers.
        """
        if use_true_val is None:
            use_true_val = dict((n, False) for n in sampler_names)

        logger.info('_test_specific_samplers %s ...', str(sampler_names))

        params = deepcopy(self.sampler_params_for_single_test)

        # Loop over given samplers to enable them
        for var_name in sampler_names:
            var_class = params[var_name].__class__
            use_tval = use_true_val[var_name]

            # special case for HRF -> normalization and prior type
            if var_class == jms.HRF_Sampler:
                params[var_name] = jms.HRF_Sampler(do_sampling=True,
                                                   use_true_value=use_tval,
                                                   normalise=normalize_hrf,
                                                   prior_type=hrf_prior_type,
                                                   zero_contraint=False)
            elif var_class == jms.HRF_Group_Sampler:
                ptype = hrf_group_prior_type
                nhg = normalize_hrf_group
                shg = jms.HRF_Group_Sampler(do_sampling=True,
                                            use_true_value=use_tval,
                                            normalise=nhg,
                                            prior_type=ptype,
                                            zero_contraint=False,
                                            regularise=reg_hgroup)
                params[var_name] = shg
            else:
                params[var_name] = var_class(do_sampling=True,
                                             use_true_value=use_tval)

        if nb_its is not None:
            params['nb_iterations'] = nb_its

        if save_history:
            params['smpl_hist_pace'] = 1
            params['obs_hist_pace'] = 1

        if check_fv is not None:
            params['check_final_value'] = check_fv

        sampler = BMSS(**params)

        output_dir = self.simu_dir

        analyser = JDEMCMCAnalyser(sampler=sampler,
                                   osfMax=4,
                                   dtMin=.4,
                                   dt=.6,
                                   driftParam=4,
                                   driftType='polynomial',
                                   outputPrefix='jde_mcmc_',
                                   randomSeed=5421087,
                                   pass_error=False)

        treatment = FMRITreatment(fmri_data=simu,
                                  analyser=analyser,
                                  output_dir=output_dir)

        outputs = treatment.run()
        return outputs
Beispiel #11
0
def create_treatment_surf(boldFiles,
                          parcelFile,
                          meshFile,
                          dt,
                          tr,
                          paradigmFile,
                          nbIterations=4000,
                          writeXmlSetup=True,
                          parallelize=False,
                          outputDir=None,
                          outputSuffix=None,
                          outputPrefix=None,
                          contrasts=';',
                          beta=.6,
                          estimBeta=True,
                          pfMethod='ps',
                          estimHrf=True,
                          hrfVar=.01,
                          roiIds=None,
                          nbClasses=2,
                          gzip_rdump=False,
                          simulation_file=None,
                          make_outputs=True):
    if roiIds is None:
        roiIds = np.array([], dtype=int)

    outDump = make_outfile(DEFAULT_DUMP_FILE, outputDir, outputPrefix,
                           outputSuffix)
    if gzip_rdump:
        outDump += '.gz'

    if contrasts is not None:
        cons = dict(
            ("con_%d" % i, ce) for i, ce in enumerate(";".split(contrasts)))
    else:
        cons = {}

    if nbClasses == 2:
        sampler = BG(
            **{
                'nb_iterations':
                nbIterations,
                # level of spatial correlation = beta
                'beta':
                BS(
                    **{
                        'val_ini': np.array([beta]),
                        'do_sampling': estimBeta,
                        'pf_method': pfMethod,
                    }),
                # HRF
                'hrf':
                HS(**{
                    'do_sampling': estimHrf,
                }),
                # HRF variance
                'hrf_var':
                HVS(**{
                    'do_sampling': False,
                    'val_ini': np.array([hrfVar]),
                }),
                # neural response levels (stimulus-induced effects)
                'response_levels':
                NS(**{
                    'contrasts': cons,
                }),
            })

    elif nbClasses == 3:
        raise NotImplementedError('3 class model not maintained')

    analyser = JDEMCMCAnalyser(sampler, dt=dt)

    fmri_data = FmriData.from_surf_files(paradigmFile, boldFiles, tr, meshFile,
                                         parcelFile)

    if simulation_file is not None:
        f_simu = open(simulation_file)
        simulation = cPickle.load(f_simu)
        f_simu.close()
        fmri_data.simulation = simulation

    tjde = FMRITreatment(fmri_data, analyser, outputDir)
    # print 'make_outputs:', make_outputs

    sxml = xmlio.to_xml(tjde)
    if writeXmlSetup is not None and outputDir is not None:
        outSetupXml = make_outfile(DEFAULT_CFG_FILE_JDE, outputDir,
                                   outputPrefix, outputSuffix)
        logger.info("Writing XML setup to: %s", outSetupXml)
        f = open(outSetupXml, 'w')
        f.write(sxml)
        f.close()
    else:
        outSetupXml = None

    return tjde, outSetupXml