Example #1
0
    def test_basic_types(self):

        dictObj = {'clef1': 'val1', 'clef2': 2}
        sXml = xmlio.to_xml(dictObj)
        dictObj2 = xmlio.from_xml(sXml)
        assert dictObj2 == dictObj

        obj = ['ggg', 5, (677, 56), 4.5, 'ff']
        sXml = xmlio.to_xml(obj)
        obj2 = xmlio.from_xml(sXml)
        assert obj2 == obj
Example #2
0
    def test_basic_types(self):

        dictObj = {'clef1' : 'val1', 'clef2': 2}
        sXml = xmlio.to_xml(dictObj)
        dictObj2 = xmlio.from_xml(sXml)
        assert dictObj2 == dictObj

        obj = ['ggg', 5, (677,56), 4.5, 'ff']
        sXml = xmlio.to_xml(obj)
        obj2 = xmlio.from_xml(sXml)
        assert obj2 == obj
Example #3
0
    def setDummyInputData(self, xmlFile):

        f = open(xmlFile, 'r')
        xml = f.read()
        t = xmlio.from_xml(xml)
        if t.data.data_type == 'volume':
            dataFn = pyhrf.get_data_file_name('dummySmallBOLD.nii.gz')
            maskFn = pyhrf.get_data_file_name('dummySmallMask.nii.gz')
            sd = FMRISessionVolumicData(bold_file=dataFn)
            t.set_init_param('fmri_data',
                             FmriData.from_vol_ui(mask_file=maskFn,
                                                  sessions_data=[sd]))

        elif t.data.data_type == 'surface':
            fn = 'real_data_surf_tiny_bold.gii'
            dataFn = pyhrf.get_data_file_name(fn)
            fn = 'real_data_surf_tiny_parcellation.gii'
            maskFn = pyhrf.get_data_file_name(fn)
            fn = 'real_data_surf_tiny_mesh.gii'
            meshFn = pyhrf.get_data_file_name(fn)
            sd = FMRISessionSurfacicData(bold_file=dataFn)
            t.set_init_param('fmri_data',
                             FmriData.from_surf_ui(mask_file=maskFn,
                                                   mesh_file=meshFn,
                                                   sessions_data=[sd]))
        else:
            raise Exception('Unsupported class ... todo')

        f = open(xmlFile, 'w')
        f.write(xmlio.to_xml(t))
        f.close()
Example #4
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)
Example #5
0
    def setDummyInputData(self, xmlFile):

        f = open(xmlFile, 'r')
        xml = f.read()
        t = xmlio.from_xml(xml)
        if t.data.data_type == 'volume':
            dataFn = pyhrf.get_data_file_name('dummySmallBOLD.nii.gz')
            maskFn = pyhrf.get_data_file_name('dummySmallMask.nii.gz')
            sd = FMRISessionVolumicData(bold_file=dataFn)
            t.set_init_param(
                'fmri_data',
                FmriData.from_vol_ui(mask_file=maskFn, sessions_data=[sd]))

        elif t.data.data_type == 'surface':
            fn = 'real_data_surf_tiny_bold.gii'
            dataFn = pyhrf.get_data_file_name(fn)
            fn = 'real_data_surf_tiny_parcellation.gii'
            maskFn = pyhrf.get_data_file_name(fn)
            fn = 'real_data_surf_tiny_mesh.gii'
            meshFn = pyhrf.get_data_file_name(fn)
            sd = FMRISessionSurfacicData(bold_file=dataFn)
            t.set_init_param(
                'fmri_data',
                FmriData.from_surf_ui(mask_file=maskFn,
                                      mesh_file=meshFn,
                                      sessions_data=[sd]))
        else:
            raise Exception('Unsupported class ... todo')

        f = open(xmlFile, 'w')
        f.write(xmlio.to_xml(t))
        f.close()
Example #6
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)
Example #7
0
    def makeQuietOutputs(self, xmlFile):

        from pyhrf import xmlio
        t = xmlio.from_xml(file(xmlFile).read())
        t.set_init_param('output_dir', None)
        f = open(xmlFile, 'w')
        f.write(xmlio.to_xml(t))
        f.close()
Example #8
0
    def makeQuietOutputs(self, xmlFile):

        from pyhrf import xmlio
        t = xmlio.from_xml(file(xmlFile).read())
        t.set_init_param('output_dir', None)
        f = open(xmlFile, 'w')
        f.write(xmlio.to_xml(t))
        f.close()
Example #9
0
    def makeQuietOutputs(self, xmlFile):

        # print 'makeQuietOutputs ...'
        # print 'xmlFile:', xmlFile
        t = xmlio.from_xml(file(xmlFile).read())
        t.set_init_param('output_dir', None)
        f = open(xmlFile, 'w')
        f.write(xmlio.to_xml(t))
        f.close()
Example #10
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)
Example #11
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)
Example #12
0
 def test_set_init_param(self):
         a = A()
         a.set_init_param('c', 'new_c')
         # print 'after override ...'
         # print a._init_parameters
         xml = xmlio.to_xml(a, pretty=True)
         # print xml
         new_a = xmlio.from_xml(xml)
         self.assertEqual(new_a.param2, 'new_c')
Example #13
0
    def testNumpy(self):

        obj = \
            {'array1': np.random.randn(10),
             'val1': np.int(5),
             'array2D': np.ones((4, 4)),
             }
        sXml = xmlio.to_xml(obj)
        obj2 = xmlio.from_xml(sXml)
        assert obj2.keys() == obj.keys()
        assert np.allclose(obj2["array1"], obj["array1"])
Example #14
0
    def testNumpy(self):

        obj = \
            { 'array1' : np.random.randn(10),
              'val1' : np.int(5),
              'array2D' : np.ones((4,4)),
              }
        sXml = xmlio.to_xml(obj)
        obj2 = xmlio.from_xml(sXml)
        assert obj2.keys() == obj.keys()
        assert np.allclose(obj2["array1"],obj["array1"])
Example #15
0
    def test_ordered_dict(self):

        try:
            from collections import OrderedDict
        except ImportError:
            from pyhrf.tools.backports import OrderedDict

        d = OrderedDict([('a', 1), ('b', 2)])
        sXml = xmlio.to_xml(d)
        d2 = xmlio.from_xml(sXml)

        self.assertEqual(d, d2)
Example #16
0
    def test_ordered_dict(self):

        try:
            from collections import OrderedDict
        except ImportError:
            from pyhrf.tools.backports import OrderedDict

        d = OrderedDict([('a', 1), ('b', 2)])
        sXml = xmlio.to_xml(d)
        d2 = xmlio.from_xml(sXml)

        self.assertEqual(d, d2)
Example #17
0
    def test_JDEMCMCAnalyzerXML(self):
        pyhrf.verbose.set_verbosity(0)
        from pyhrf.ui.jde import JDEMCMCAnalyser
        from pyhrf.jde.models import BOLDGibbsSampler
        a = JDEMCMCAnalyser(sampler=BOLDGibbsSampler(nb_iterations=42))
        # print 'a -- nbIterations:', a.sampler.nbIterations
        axml = xmlio.to_xml(a, pretty=True)
        # print 'axml:'
        # print axml

        a2 = xmlio.from_xml(axml)
        # print 'a2 -- nbIterations:', a2.sampler.nbIterations
        self.assertEqual(a2.sampler.nbIterations, 42)
Example #18
0
    def setSimulationData(self, xmlFile, simu_file):

        f = open(xmlFile, 'r')
        xml = f.read()
        t = xmlio.from_xml(xml)
        sd = FMRISessionSimulationData(simulation_file=simu_file)
        t.set_init_param(
            'fmri_data', FmriData.from_simu_ui(sessions_data=[sd]))

        f = open(xmlFile, 'w')
        sxml = xmlio.to_xml(t)
        f.write(sxml)
        f.close()
Example #19
0
    def setSimulationData(self, xmlFile, simu_file):

        f = open(xmlFile, 'r')
        xml = f.read()
        t = xmlio.from_xml(xml)
        sd = FMRISessionSimulationData(simulation_file=simu_file)
        t.set_init_param('fmri_data',
                         FmriData.from_simu_ui(sessions_data=[sd]))

        f = open(xmlFile, 'w')
        sxml = xmlio.to_xml(t)
        f.write(sxml)
        f.close()
Example #20
0
    def test_TreatmentXML(self):

        pyhrf.verbose.set_verbosity(0)
        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)
        # print 't -- nbIterations:', t.analyser.sampler.nbIterations
        txml = xmlio.to_xml(t, pretty=True)
        # print 'txml:'
        # print txml

        t2 = xmlio.from_xml(txml)
        # print 't2 -- nbIterations:', t2.analyser.sampler.nbIterations
        self.assertEqual(t2.analyser.sampler.nbIterations, 42)
Example #21
0
def create_treatment(boldFiles, parcelFile, dt, tr, paradigmFile,
                     nbIterations=4000,
                     writeXmlSetup=True, parallelize=False,
                     outputDir=None, outputSuffix=None, outputPrefix=None,
                     contrasts=None, beta=.6, estimBeta=True,
                     pfMethod='ps', estimHrf=True, hrfVar=.01,
                     roiIds=None,
                     nbClasses=2,gzip_rdump=False, make_outputs=True,
                     vbjde=False, simulation_file=None):
    if roiIds is None:
        roiIds = np.array([],dtype=int)

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

    fmri_data = FmriData.from_vol_files(parcelFile, paradigmFile, boldFiles, tr)

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

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

    if(vbjde):
        analyser = VBJDE(dt=dt)
    else:
        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('Model with 3 classes not maintained')
            # from pyhrf.jde.models import GGG_BOLDGibbsSampler as BG3
            # sampler = BG3({
            #                 BG.P_NB_ITERATIONS : nbIterations,
            #                 # level of spatial correlation = beta
            #                 BG.P_BETA : BS({
            #                         BS.P_VAL_INI : np.array([beta]),
            #                         BS.P_SAMPLE_FLAG : estimBeta,
            #                         BS.P_PARTITION_FUNCTION_METH : pfMethod,
            #                         }),
            #                 # HRF
            #                 BG.P_HRF : HS({
            #                         HS.P_SAMPLE_FLAG : estimHrf,
            #                         }),
            #                 # HRF variance
            #                 BG.P_RH : HVS({
            #                         HVS.P_SAMPLE_FLAG : False,
            #                         HVS.P_VAL_INI : np.array([hrfVar]),
            #                         }),
            #                 # neural response levels (stimulus-induced effects)
            #                 BG.P_NRLS : NS3({
            #                         NS.P_CONTRASTS : cons,
            #                         }),
            #                 })

        analyser = JDEMCMCAnalyser(sampler, dt=dt)

    tjde = FMRITreatment(fmri_data, analyser, outputDir)

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

    return tjde, outSetupXml
Example #22
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)
        pyhrf.verbose(1, "Writing XML setup to: " + outSetupXml )
        f = open(outSetupXml, 'w')
        f.write(sxml)
        f.close()
    else:
        outSetupXml = None

    return tjde, outSetupXml
Example #23
0
 def test_set_init_param(self):
     a = A()
     a.set_init_param('c', 'new_c')
     xml = xmlio.to_xml(a, pretty=True)
     new_a = xmlio.from_xml(xml)
     self.assertEqual(new_a.param2, 'new_c')
Example #24
0
def run_pyhrf_cmd_treatment(cfg_cmd, exec_cmd, default_cfg_file,
                            default_profile_file, label_for_cluster):


    usage = 'usage: %%prog [options]'

    description = 'Manage a joint detection-estimation treatment of fMRI data.' \
                'This command runs the treatment defined in an xml '\
                'parameter file. See pyhrf_jde_buildcfg command to build a'\
                'template of such a file. If no xml file found, then runs a '\
                'default example analysis.'

    parser = OptionParser(usage=usage, description=description)

    parser.add_option('-c','--input-cfg-file', metavar='XMLFILE', dest='cfgFile',
                    default=default_cfg_file,
                    help='Configuration file: XML file containing parameters'\
                    ' defining input data and analysis to perform.')

    parser.add_option('-r','--roi-data', metavar='PICKLEFILE', dest='roidata',
                    default=None, help='Input fMRI ROI data. The data '\
                    'definition part in the config file is ignored.')

    parser.add_option('-t','--treatment_pck',
                      metavar='PICKLEFILE', dest='treatment_pck',
                      default=None, help='Input treatment as a pickle dump.' \
                          'The XML cfg file is ignored')

    parser.add_option('-s','--stop-on-error', dest='stop_on_error',
                      action='store_true',
                    default=False, help='For debug: do not continue if error' \
                          ' during one ROI analysis')


    parser.add_option('-v','--verbose',dest='verbose',metavar='INTEGER',
                    type='int',default=0,
                    help=dictToString(pyhrf.verboseLevels))

    parser.add_option('-p','--profile',action='store_true', default=False,
                    help='Enable profiling of treatment. Store profile data in '\
                        '%s. NOTE: not avalaible in parallel mode.'\
                    %default_profile_file)

    parallel_choices = ['LAN','local','cluster']
    parser.add_option('-x','--parallel', choices=parallel_choices,
                    help='Parallel processing. Choices are %s'\
                        %string.join(parallel_choices,', '))


    (options,args) = parser.parse_args()

    pyhrf.verbose.set_verbosity(options.verbose)

    t0 = time.time()

    if options.treatment_pck is not None:
        f = open(options.treatment_pck)
        treatment = cPickle.load(f)
        f.close()
    else:
        if not os.path.exists(options.cfgFile):
            print 'Error: could not find default configuration file "%s"\n'\
                'Consider running "%s" to generate it.' \
                %(options.cfgFile, cfg_cmd)
            sys.exit(1)
        else:
            pyhrf.verbose(1, 'Loading configuration from: "%s" ...' \
                              %options.cfgFile)
            f = open(options.cfgFile, 'r')
            sXml = string.join(f.readlines())
            f.close()
            treatment = xmlio.from_xml(sXml)
            if 0:
                sXml = xmlio.to_xml(treatment)
                f = './treatment_cmd.xml'
                fOut = open(f,'w')
                fOut.write(sXml)
                fOut.close()
            #f = open(fOut, 'w')
            #cPickle.dump(treatment, f)
            #f.close()


    treatment.analyser.set_pass_errors(not options.stop_on_error)

    if options.parallel is not None:

        # tmpDir = tempfile.mkdtemp(prefix='pyhrf',
        #                           dir=pyhrf.cfg['global']['tmp_path'])
        # pyhrf.verbose(1, 'Tmpdir: %s' %tmpDir)

        treatment.run(parallel=options.parallel)

    else:
        if options.roidata is not None:
            #treatment.set_roidata(options.roidata)
            pyhrf.verbose(1, 'Loading ROI data from: "%s" ...' \
                              %options.roidata)

            roidata = cPickle.load(open(options.roidata))
            roidata.verbosity = pyhrf.verbose.verbosity
            if pyhrf.verbose > 1:
                print roidata.getSummary()
            #TODO: enable profiling
            pyhrf.verbose(1, 'Launching analysis ...')
            if options.profile:
                cProfile.runctx("result = treatment.analyser(roidata)",
                                globals(),
                                {'treatment':treatment,'roidata': roidata},
                                default_profile_file)
            else:
                result = treatment.analyser(roidata)
            outPath = op.dirname(op.abspath(options.roidata))
            fOut = op.join(outPath,"result_%04d.pck" %roidata.get_roi_id())
            pyhrf.verbose(1, 'Dumping results to %s ...' %fOut)
            f = open(fOut, 'w')
            cPickle.dump(result, f)
            f.close()
        else:
            pyhrf.verbose(1, 'ROI data is none')
            if options.profile:
                cProfile.runctx("treatment.run()", globals(),
                                {'treatment':treatment}, default_profile_file)
            else:
                #print 'treatment:', treatment
                treatment.run()

    pyhrf.verbose(1, 'Estimation done, took %s' %format_duration(time.time() - t0))
Example #25
0
def save_treatment(t,f):
    sXml = xmlio.to_xml(t, handler=xmlio.xmlnumpy.NumpyXMLHandler())
    fOut = open(f,'w')
    fOut.write(sXml)
    fOut.close()
Example #26
0
def run_pyhrf_cmd_treatment(cfg_cmd, exec_cmd, default_cfg_file,
                            default_profile_file, label_for_cluster):

    usage = 'usage: %%prog [options]'

    description = 'Manage a joint detection-estimation treatment of fMRI data.' \
        'This command runs the treatment defined in an xml '\
        'parameter file. See pyhrf_jde_buildcfg command to build a'\
        'template of such a file. If no xml file found, then runs a '\
        'default example analysis.'

    parser = OptionParser(usage=usage, description=description)

    parser.add_option('-c',
                      '--input-cfg-file',
                      metavar='XMLFILE',
                      dest='cfgFile',
                      default=default_cfg_file,
                      help='Configuration file: XML file containing parameters'
                      ' defining input data and analysis to perform.')

    parser.add_option('-r',
                      '--roi-data',
                      metavar='PICKLEFILE',
                      dest='roidata',
                      default=None,
                      help='Input fMRI ROI data. The data '
                      'definition part in the config file is ignored.')

    parser.add_option('-t',
                      '--treatment_pck',
                      metavar='PICKLEFILE',
                      dest='treatment_pck',
                      default=None,
                      help='Input treatment as a pickle dump.'
                      'The XML cfg file is ignored')

    parser.add_option('-s',
                      '--stop-on-error',
                      dest='stop_on_error',
                      action='store_true',
                      default=False,
                      help='For debug: do not continue if error'
                      ' during one ROI analysis')

    parser.add_option('-v',
                      '--verbose',
                      dest='verbose',
                      metavar='INTEGER',
                      type='int',
                      default=0,
                      help=pformat(pyhrf.verbose_levels))

    parser.add_option(
        '-p',
        '--profile',
        action='store_true',
        default=False,
        help='Enable profiling of treatment. Store profile data in '
        '%s. NOTE: not avalaible in parallel mode.' % default_profile_file)

    parallel_choices = ['LAN', 'local', 'cluster']
    parser.add_option('-x',
                      '--parallel',
                      choices=parallel_choices,
                      help='Parallel processing. Choices are %s' %
                      string.join(parallel_choices, ', '))

    (options, args) = parser.parse_args()

    # pyhrf.verbose.set_verbosity(options.verbose)
    pyhrf.logger.setLevel(options.verbose)

    t0 = time.time()

    if options.treatment_pck is not None:
        f = open(options.treatment_pck)
        treatment = cPickle.load(f)
        f.close()
    else:
        if not os.path.exists(options.cfgFile):
            print 'Error: could not find default configuration file "%s"\n'\
                'Consider running "%s" to generate it.' \
                % (options.cfgFile, cfg_cmd)
            sys.exit(1)
        else:
            logger.info('Loading configuration from: "%s" ...',
                        options.cfgFile)
            f = open(options.cfgFile, 'r')
            sXml = string.join(f.readlines())
            f.close()
            treatment = xmlio.from_xml(sXml)
            if 0:
                sXml = xmlio.to_xml(treatment)
                f = './treatment_cmd.xml'
                fOut = open(f, 'w')
                fOut.write(sXml)
                fOut.close()

    treatment.analyser.set_pass_errors(not options.stop_on_error)

    if options.parallel is not None:

        treatment.run(parallel=options.parallel)

    else:
        if options.roidata is not None:
            logger.info('Loading ROI data from: "%s" ...', options.roidata)

            roidata = cPickle.load(open(options.roidata))
            roidata.verbosity = logger.getEffectiveLevel()
            if logger.getEffectiveLevel() <= logging.INFO:
                print roidata.getSummary()
            # TODO: enable profiling
            logger.info('Launching analysis ...')
            if options.profile:
                cProfile.runctx("result = treatment.analyser(roidata)",
                                globals(), {
                                    'treatment': treatment,
                                    'roidata': roidata
                                }, default_profile_file)
            else:
                result = treatment.analyser(roidata)
            outPath = op.dirname(op.abspath(options.roidata))
            fOut = op.join(outPath, "result_%04d.pck" % roidata.get_roi_id())
            logger.info('Dumping results to %s ...', fOut)
            f = open(fOut, 'w')
            cPickle.dump(result, f)
            f.close()
        else:
            logger.info('ROI data is none')
            if options.profile:
                cProfile.runctx("treatment.run()", globals(),
                                {'treatment': treatment}, default_profile_file)
            else:
                treatment.run()

    logger.info('Estimation done, took %s', format_duration(time.time() - t0))
Example #27
0
 def _test_xml_bijection(self, o):
     xml = to_xml(o)
     pyhrf.verbose(6, 'xml from %s:' % str(o))
     pyhrf.verbose(6, xml)
     o2 = from_xml(xml)
     self.assertEqual(o, o2)
Example #28
0
 def test_set_init_param(self):
     a = A()
     a.set_init_param('c', 'new_c')
     xml = xmlio.to_xml(a, pretty=True)
     new_a = xmlio.from_xml(xml)
     self.assertEqual(new_a.param2, 'new_c')
Example #29
0
 def test_simple_bijection(self):
     a = A(c='e')
     a_xml = to_xml(a)
     self.assertEqual(a, from_xml(a_xml))
Example #30
0
 def _test_xml_bijection(self, o):
     xml = to_xml(o)
     logger.debug('xml from %s:', str(o))
     logger.debug(xml)
     o2 = from_xml(xml)
     self.assertEqual(o, o2)
Example #31
0
 def test_simple_bijection(self):
     a = A(c='e')
     a_xml = to_xml(a)
     self.assertEqual(a, from_xml(a_xml))
Example #32
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
Example #33
0
 def _test_xml_bijection(self, o):
     xml = to_xml(o)
     logger.debug('xml from %s:', str(o))
     logger.debug(xml)
     o2 = from_xml(xml)
     self.assertEqual(o, o2)