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
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
def test_bijection_from_init(self): pyhrf.verbose.set_verbosity(0) b = B() xml = b.to_ui_node('bobj').to_xml(pretty=True) b2 = xmlio.from_xml(xml) # print 'b2.obj:', b2.obj self.assertEqual(b, b2)
def test_bijection_from_init_no_arg(self): pyhrf.verbose.set_verbosity(0) o = C() xml = o.to_ui_node('bobj').to_xml(pretty=True) o2 = xmlio.from_xml(xml) # print 'b2.obj:', b2.obj self.assertEqual(o, o2)
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()
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)
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()
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()
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)
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.from_xml(open(out_file).read()) for c in allOuts.itervalues(): c.cleanFiles() os.remove(out_file)
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()
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')
def test_bijection_from_classmethod_init(self): pyhrf.verbose.set_verbosity(0) b = B.from_stuff(a=4,b=3) # print 'b.obj:', b.obj xml = b.to_ui_node('bobj').to_xml(pretty=True) #print 'xml:' #print xml b2 = xmlio.from_xml(xml) # print 'b2.obj:', b2.obj self.assertEqual(b, b2)
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"])
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"])
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)
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()
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()
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)
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)
def test_bijection_from_init(self): b = B() xml = b.to_ui_node('bobj').to_xml(pretty=True) b2 = xmlio.from_xml(xml) self.assertEqual(b, b2)
def test_bijection_from_init_no_arg(self): o = C() xml = o.to_ui_node('bobj').to_xml(pretty=True) o2 = xmlio.from_xml(xml) self.assertEqual(o, o2)
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)
def test_bijection_from_classmethod_init(self): b = B.from_stuff(a=4, b=3) xml = b.to_ui_node('bobj').to_xml(pretty=True) b2 = xmlio.from_xml(xml) self.assertEqual(b, b2)
def test_simple_bijection(self): a = A(c='e') a_xml = to_xml(a) self.assertEqual(a, from_xml(a_xml))
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)
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))
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')
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))