Example #1
0
 def testNumpy(self):
     ##        print 'Numpy ..'
     obj = DummyNumpy()
     sXml = xmlio.toXML(obj)
     ##        print 'Xml:'
     ##        print sXml
     xmlio.fromXML(sXml)
Example #2
0
 def testSimple(self):
     ##        print 'Simple ..'
     obj = Dummy()
     sXml = xmlio.toXML(obj)
     ##        print 'xml:'
     ##        print sXml
     xmlio.fromXML(sXml)
Example #3
0
    def testNested(self):

        ##        print 'Nested objects ..'
        obj = DummyNested()
        sXml = xmlio.toXML(obj)
        ##        print 'Xml:'
        ##        print sXml
        xmlio.fromXML(sXml)
Example #4
0
    def testSimple(self):

        dictObj = {"clef1": "val1", "clef2": 2}
        sXml = xmlio.toXML(dictObj)
        dictObj2 = xmlio.fromXML(sXml)
        assert dictObj2 == dictObj

        obj = ["ggg", 5, (677, 56), "ff"]
        sXml = xmlio.toXML(obj)
        obj2 = xmlio.fromXML(sXml)
        assert obj2 == obj
Example #5
0
    def testNumpy(self):

        obj = {"array1": np.random.randn(10), "val1": np.int(5), "array2D": np.ones((4, 4))}
        sXml = xmlio.toXML(obj, handler=xmlio.xmlnumpy.NumpyXMLHandler())
        obj2 = xmlio.fromXML(sXml)
        assert obj2.keys() == obj.keys()
        assert np.allclose(obj2["array1"], obj["array1"])
Example #6
0
    def setDummyInputData(self, xmlFile):

        f = open(xmlFile, "r")
        xml = f.read()
        t = xmlio.fromXML(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.toXML(t, handler=NumpyXMLHandler()))
        f.close()
Example #7
0
    def test_xml_from_init(self):
        b = B()
        # print 'b.obj:', b.obj
        xml = xmlio.toXML(b, handler=xmlio.xmlnumpy.NumpyXMLHandler(), pretty=True)
        # print 'xml:'
        # print xml

        b2 = xmlio.fromXML(xml)
Example #8
0
 def test_value_override(self):
     a = A()
     a.override_init("obj_t", T.from_param_c)
     # print 'after override ...'
     # print a._init_parameters
     xml = xmlio.toXML(a, handler=xmlio.xmlnumpy.NumpyXMLHandler(), pretty=True)
     # print xml
     new_a = xmlio.fromXML(xml)
Example #9
0
 def clean_output_files(self, output_dir):
     # Clean all formatted outputs
     if self.outFile is not None and output_dir is not None:
         out_file = op.join(output_dir, self.outFile)
         if op.exists(self.outFile):
             allOuts = xmlio.fromXML(open(out_file).read())
             for c in allOuts.itervalues():
                 c.cleanFiles()
             os.remove(out_file)
Example #10
0
    def makeQuietOutputs(self, xmlFile):

        from pyhrf import xmlio
        from pyhrf.xmlio.xmlnumpy import NumpyXMLHandler
        t = xmlio.fromXML(file(xmlFile).read())
        t.set_init_param('output_dir', None)
        f = open(xmlFile, 'w')
        f.write(xmlio.toXML(t, handler=NumpyXMLHandler()))
        f.close()
Example #11
0
    def makeQuietOutputs(self, xmlFile):

        # print 'makeQuietOutputs ...'
        # print 'xmlFile:', xmlFile
        t = xmlio.fromXML(file(xmlFile).read())
        t.set_init_param("output_dir", None)
        f = open(xmlFile, "w")
        f.write(xmlio.toXML(t, handler=NumpyXMLHandler()))
        f.close()
Example #12
0
    def test_xml_from_classmethod_init(self):
        b = B.from_stuff(a=4, b=3)
        # print 'b.obj:', b.obj
        np.testing.assert_equal(b.obj, [4, 3])
        xml = xmlio.toXML(b, handler=xmlio.xmlnumpy.NumpyXMLHandler(), pretty=True)
        # print 'xml:'
        # print xml

        b2 = xmlio.fromXML(xml)
        # print 'b2.obj:', b2.obj
        np.testing.assert_equal(b2.obj, [4, 3])
Example #13
0
    def test_JDEMCMCAnalyzerXML(self):

        from pyhrf.ui.jde import JDEMCMCAnalyser
        from pyhrf.jde.models import BOLDGibbsSampler

        a = JDEMCMCAnalyser(sampler=BOLDGibbsSampler({"nbIterations": 42}))
        # print 'a -- nbIterations:', a.sampler.nbIterations
        axml = xmlio.toXML(a, handler=xmlio.xmlnumpy.NumpyXMLHandler(), pretty=True)
        # print 'axml:'
        # print axml

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

        f = open(xmlFile, "r")
        xml = f.read()
        t = xmlio.fromXML(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.toXML(t, handler=NumpyXMLHandler())
        # print 'sxml:'
        # print sxml
        f.write(sxml)
        f.close()
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.toXML(d)
        # print 'sXml:'
        # print sXml
        d2 = xmlio.fromXML(sXml)

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

        # pyhrf.verbose.set_verbosity(6)
        from pyhrf.ui.jde import JDEMCMCAnalyser
        from pyhrf.jde.models import BOLDGibbsSampler
        from pyhrf.ui.treatment import FMRITreatment

        a = JDEMCMCAnalyser(sampler=BOLDGibbsSampler({"nbIterations": 42}))
        t = FMRITreatment(analyser=a)
        # print 't -- nbIterations:', t.analyser.sampler.nbIterations
        txml = xmlio.toXML(t, handler=xmlio.xmlnumpy.NumpyXMLHandler(), pretty=True)
        # print 'txml:'
        # print txml

        t2 = xmlio.fromXML(txml)
        # print 't2 -- nbIterations:', t2.analyser.sampler.nbIterations
        self.assertEqual(t2.analyser.sampler.nbIterations, 42)
Example #17
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.fromXML(sXml)
            if 0:
                sXml = xmlio.toXML(treatment,
                                   handler=xmlio.xmlnumpy.NumpyXMLHandler())
                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))