def test_multiple_output_values(self): # pyhrf.verbose.set_verbosity(0) pyhrf.logger.setLevel(logging.WARNING) data = Pipeline({'e': foo_a, 'b': foo_default_arg, ('a', 'd'): foo_multiple_returns}) data.resolve()
def testGoodDepTreeInit(self): data = Pipeline(self.depTree) assert (data.get_value('b') == self.a + self.e).all() assert (data.get_value('c') == self.a ** 2).all() assert (data.get_value('d') == (self.g / self.e + self.a + self.e) * self.a ** 2).all() assert (data.get_value('f') == self.g / self.e).all()
def testBadDepTreeInit(self): try: data = Pipeline(self.badDepTree) except Exception: pass else: raise Exception()
def test_cached(self): try: from joblib import Memory mem = Memory(self.cache_dir) dep_tree = { 'a': 5, 'b': 6, 'c': mem.cache(slow_func), } data = Pipeline(dep_tree) t0 = time.time() data.resolve() delta = time.time() - t0 t0 = time.time() data.resolve() delta = time.time() - t0 assert delta < .1 except: pass
def test_cached(self): try: from joblib import Memory mem = Memory(self.cache_dir, verbose=0) dep_tree = { 'a': 5, 'b': 6, 'c': mem.cache(slow_func), } data = Pipeline(dep_tree) t0 = time.time() data.resolve() delta = time.time() - t0 t0 = time.time() data.resolve() delta = time.time() - t0 assert delta < .1 except: pass
def simulate_asl_physio_rfs(output_dir=None, noise_scenario='high_snr', spatial_size='tiny', v_noise=None): """ Generate ASL data according to a LTI system, with PRF and BRF generated from a physiological model. Args: - output_dir (str|None): path where to save outputs as nifti files. If None: no output files - noise_scenario ("high_snr"|"low_snr"): scenario defining the SNR - spatial_size ("tiny"|"normal") : scenario for the size of the map - "tiny" produces 2x2 maps - "normal" produces 20x20 maps Result: dict (<item_label (str)> : <simulated_item (np.ndarray)>) -> a dictionary mapping names of simulated items to their values WARNING: in this dict the 'bold' item is in fact the ASL signal. This name was used to be compatible with JDE which assumes that the functional time series is named "bold". TODO: rather use the more generic label 'fmri_signal'. """ drift_var = 10. dt = .5 dsf = 2 #down sampling factor #tr = dt * dsf tr = 3. if spatial_size == 'tiny': lmap1, lmap2, lmap3 = 'tiny_1', 'tiny_2', 'tiny_3' elif spatial_size == 'random_small': lmap1, lmap2, lmap3 = 'random_small', 'random_small', 'random_small' else: lmap1, lmap2, lmap3 = 'ghost', 'icassp13', 'house_sun' if noise_scenario == 'high_snr': v_noise = v_noise or 0.05 conditions = [ Condition(name='audio', perf_m_act=5., perf_v_act=.1, perf_v_inact=.2, bold_m_act=15., bold_v_act=.1, bold_v_inact=.2, label_map=lmap1), Condition(name='video', perf_m_act=5., perf_v_act=.11, perf_v_inact=.21, bold_m_act=14., bold_v_act=.11, bold_v_inact=.21, label_map=lmap2), Condition(name='damier', perf_m_act=12., perf_v_act=.12, perf_v_inact=.22, bold_m_act=20., bold_v_act=.12, bold_v_inact=.22, label_map=lmap3), ] elif noise_scenario == 'low_snr_low_prl': v_noise = v_noise or 7. scale = .3 conditions = [ Condition(name='audio', perf_m_act=1.6 * scale, perf_v_act=.1, perf_v_inact=.1, bold_m_act=2.2, bold_v_act=.3, bold_v_inact=.3, label_map=lmap1), Condition(name='video', perf_m_act=1.6 * scale, perf_v_act=.1, perf_v_inact=.1, bold_m_act=2.2, bold_v_act=.3, bold_v_inact=.3, label_map=lmap2), ] else: #low_snr v_noise = v_noise or 2. conditions = [ Condition(name='audio', perf_m_act=1.6, perf_v_act=.3, perf_v_inact=.3, bold_m_act=2.2, bold_v_act=.3, bold_v_inact=.3, label_map=lmap1), Condition(name='video', perf_m_act=1.6, perf_v_act=.3, perf_v_inact=.3, bold_m_act=2.2, bold_v_act=.3, bold_v_inact=.3, label_map=lmap2), ] simulation_steps = { 'dt': dt, 'dsf': dsf, 'tr': tr, 'condition_defs': conditions, # Paradigm 'paradigm': simbase.create_localizer_paradigm_avd, 'rastered_paradigm': simbase.rasterize_paradigm, # Labels 'labels_vol': simbase.create_labels_vol, 'labels': simbase.flatten_labels_vol, 'nb_voxels': lambda labels: labels.shape[1], # Physiological model (for generation of RFs) 'physiological_params': PHY_PARAMS_FRISTON00, # Brls 'brls': simbase.create_time_invariant_gaussian_brls, # Prls 'prls': simbase.create_time_invariant_gaussian_prls, # BRF 'primary_brf': create_physio_brf, 'brf': simbase.duplicate_brf, # PRF 'primary_prf': create_physio_prf, 'prf': simbase.duplicate_prf, # Perf baseline 'perf_baseline': simbase.create_perf_baseline, 'perf_baseline_mean': 1.5, 'perf_baseline_var': .4, # Stim induced 'bold_stim_induced': simbase.create_bold_stim_induced_signal, 'perf_stim_induced': simbase.create_perf_stim_induced_signal, # Noise 'v_gnoise': v_noise, 'noise': simbase.create_gaussian_noise_asl, # Drift 'drift_order': 4, 'drift_var': drift_var, 'drift_coeffs': simbase.create_drift_coeffs_asl, 'drift': simbase.create_polynomial_drift_from_coeffs_asl, # Bold # maybe rename as ASL (should be handled afterwards ... 'ctrl_tag_mat': simbase.build_ctrl_tag_matrix, 'asl_shape': simbase.calc_asl_shape, 'bold': simbase.create_asl_from_stim_induced, } simu_graph = Pipeline(simulation_steps) # Compute everything simu_graph.resolve() simulation = simu_graph.get_values() if output_dir is not None: #simu_graph.save_graph_plot(op.join(output_dir, 'simulation_graph.png')) simbase.simulation_save_vol_outputs(simulation, output_dir) # f = open(op.join(output_dir, 'simulation.pck'), 'w') # cPickle.dump(simulation, f) # f.close() return simulation
def simulate_asl_full_physio(output_dir=None, noise_scenario='high_snr', spatial_size='tiny'): """ Generate ASL data by integrating a physiological dynamical system. Ags: - output_dir (str|None): path where to save outputs as nifti files. If None: no output files - noise_scenario ("high_snr"|"low_snr"): scenario defining the SNR - spatial_size ("tiny"|"normal") : scenario for the size of the map - "tiny" produces 2x2 maps - "normal" produces 20x20 maps Result: dict (<item_label (str)> : <simulated_item (np.ndarray)>) -> a dictionary mapping names of simulated items to their values WARNING: in this dict the 'bold' item is in fact the ASL signal. This name was used to be compatible with JDE which assumes that the functional time series is named "bold". TODO: rather use the more generic label 'fmri_signal'. TODO: use magnetization model to properly simulate final ASL signal """ drift_var = 10. dt = .5 dsf = 2 #down sampling factor if spatial_size == 'tiny': lmap1, lmap2, lmap3 = 'tiny_1', 'tiny_2', 'tiny_3' elif spatial_size == 'random_small': lmap1, lmap2, lmap3 = 'random_small', 'random_small', 'random_small' else: lmap1, lmap2, lmap3 = 'icassp13', 'ghost', 'house_sun' if noise_scenario == 'high_snr': v_noise = 0.05 conditions = [ Condition(name='audio', m_act=10., v_act=.1, v_inact=.2, label_map=lmap1), Condition(name='video', m_act=11., v_act=.11, v_inact=.21, label_map=lmap2), Condition(name='damier', m_act=12., v_act=.12, v_inact=.22, label_map=lmap3), ] else: #low_snr v_noise = 2. conditions = [ Condition(name='audio', m_act=1.6, v_act=.3, v_inact=.3, label_map=lmap1), Condition(name='video', m_act=1.6, v_act=.3, v_inact=.3, label_map=lmap2), ] simulation_steps = { 'dt': dt, 'dsf': dsf, 'tr': dt * dsf, 'condition_defs': conditions, # Paradigm 'paradigm': simbase.create_localizer_paradigm_avd, # Labels 'labels_vol': simbase.create_labels_vol, 'labels': simbase.flatten_labels_vol, 'nb_voxels': lambda labels: labels.shape[1], # Neural efficacy 'neural_efficacies': create_tbg_neural_efficacies, # BRF 'primary_brf': create_physio_brf, 'brf': simbase.duplicate_brf, # PRF 'primary_prf': create_physio_prf, 'prf': simbase.duplicate_prf, # Physiological model 'physiological_params': PHY_PARAMS_FRISTON00, ('flow_induction', 'perf_stim_induced', 'cbv', 'hbr'): create_evoked_physio_signals, 'bold_stim_induced': create_bold_from_hbr_and_cbv, # Noise 'v_gnoise': v_noise, 'noise': simbase.create_gaussian_noise_asl, # Drift 'drift_order': 4, 'drift_var': drift_var, 'drift_coeffs': simbase.create_drift_coeffs_asl, 'drift': simbase.create_polynomial_drift_from_coeffs_asl, # ASL 'ctrl_tag_mat': simbase.build_ctrl_tag_matrix, 'asl_shape': simbase.calc_asl_shape, # Perf baseline #should be the inflow at rest ... #TODO 'perf_baseline': simbase.create_perf_baseline, 'perf_baseline_mean': 0., 'perf_baseline_var': 0., # maybe rename to ASL (should be also modified in JDE)#TODO 'bold': simbase.create_asl_from_stim_induced, } simu_graph = Pipeline(simulation_steps) # Compute everything simu_graph.resolve() simulation = simu_graph.get_values() if output_dir is not None: #simu_graph.save_graph_plot(op.join(output_dir, 'simulation_graph.png')) simbase.simulation_save_vol_outputs(simulation, output_dir) # f = open(op.join(output_dir, 'simulation.pck'), 'w') # cPickle.dump(simulation, f) # f.close() return simulation
def test_func_default_args(self): data = Pipeline({'a': foo_a, 'b': foo_default_arg, 'd': 7}) data.resolve()
def simulate_asl_physio_rfs(output_dir=None, noise_scenario='high_snr', spatial_size='tiny'): """ Generate ASL data according to a LTI system, with PRF and BRF generated from a physiological model. Args: - output_dir (str|None): path where to save outputs as nifti files. If None: no output files - noise_scenario ("high_snr"|"low_snr"): scenario defining the SNR - spatial_size ("tiny"|"normal") : scenario for the size of the map - "tiny" produces 2x2 maps - "normal" produces 20x20 maps Result: dict (<item_label (str)> : <simulated_item (np.ndarray)>) -> a dictionary mapping names of simulated items to their values WARNING: in this dict the 'bold' item is in fact the ASL signal. This name was used to be compatible with JDE which assumes that the functional time series is named "bold". TODO: rather use the more generic label 'fmri_signal'. """ drift_var = 10. dt = .5 dsf = 2 #down sampling factor if spatial_size == 'tiny': lmap1, lmap2, lmap3 = 'tiny_1', 'tiny_2', 'tiny_3' elif spatial_size == 'random_small': lmap1, lmap2, lmap3 = 'random_small', 'random_small', 'random_small' else: lmap1, lmap2, lmap3 = 'icassp13', 'ghost', 'house_sun' if noise_scenario == 'high_snr': v_noise = 0.05 conditions = [ Condition(name='audio', perf_m_act=5., perf_v_act=.1, perf_v_inact=.2, bold_m_act=15., bold_v_act=.1, bold_v_inact=.2, label_map=lmap1), Condition(name='video', perf_m_act=5., perf_v_act=.11, perf_v_inact=.21, bold_m_act=14., bold_v_act=.11, bold_v_inact=.21, label_map=lmap2), Condition(name='damier', perf_m_act=12., perf_v_act=.12, perf_v_inact=.22, bold_m_act=20., bold_v_act=.12, bold_v_inact=.22, label_map=lmap3), ] elif noise_scenario == 'low_snr_low_prl': v_noise = 7. scale = .3 print 'noise_scenario: low_snr_low_prl' conditions = [ Condition(name='audio', perf_m_act=1.6*scale, perf_v_act=.1, perf_v_inact=.1, bold_m_act=2.2, bold_v_act=.3, bold_v_inact=.3, label_map=lmap1), Condition(name='video', perf_m_act=1.6*scale, perf_v_act=.1, perf_v_inact=.1, bold_m_act=2.2, bold_v_act=.3, bold_v_inact=.3, label_map=lmap2), ] else: #low_snr v_noise = 2. conditions = [ Condition(name='audio', perf_m_act=1.6, perf_v_act=.3, perf_v_inact=.3, bold_m_act=2.2, bold_v_act=.3, bold_v_inact=.3, label_map=lmap1), Condition(name='video', perf_m_act=1.6, perf_v_act=.3, perf_v_inact=.3, bold_m_act=2.2, bold_v_act=.3, bold_v_inact=.3, label_map=lmap2), ] simulation_steps = { 'dt' : dt, 'dsf' : dsf, 'tr' : dt * dsf, 'condition_defs' : conditions, # Paradigm 'paradigm' : simbase.create_localizer_paradigm_avd, 'rastered_paradigm' : simbase.rasterize_paradigm, # Labels 'labels_vol' : simbase.create_labels_vol, 'labels' : simbase.flatten_labels_vol, 'nb_voxels': lambda labels: labels.shape[1], # Physiological model (for generation of RFs) 'physiological_params' : PHY_PARAMS_FRISTON00, # Brls 'brls' : simbase.create_time_invariant_gaussian_brls, # Prls 'prls' : simbase.create_time_invariant_gaussian_prls, # BRF 'primary_brf' : create_physio_brf, 'brf' : simbase.duplicate_brf, # PRF 'primary_prf' : create_physio_prf, 'prf' : simbase.duplicate_prf, # Perf baseline 'perf_baseline' : simbase.create_perf_baseline, 'perf_baseline_mean' : 1.5, 'perf_baseline_var': .4, # Stim induced 'bold_stim_induced' : simbase.create_bold_stim_induced_signal, 'perf_stim_induced' : simbase.create_perf_stim_induced_signal, # Noise 'v_gnoise' : v_noise, 'noise' : simbase.create_gaussian_noise_asl, # Drift 'drift_order' : 4, 'drift_var' : drift_var, 'drift_coeffs':simbase.create_drift_coeffs_asl, 'drift' : simbase.create_polynomial_drift_from_coeffs_asl, # Bold # maybe rename as ASL (should be handled afterwards ... 'ctrl_tag_mat' : simbase.build_ctrl_tag_matrix, 'asl_shape' : simbase.calc_asl_shape, 'bold' : simbase.create_asl_from_stim_induced, } simu_graph = Pipeline(simulation_steps) # Compute everything simu_graph.resolve() simulation = simu_graph.get_values() if output_dir is not None: #simu_graph.save_graph_plot(op.join(output_dir, 'simulation_graph.png')) simbase.simulation_save_vol_outputs(simulation, output_dir) # f = open(op.join(output_dir, 'simulation.pck'), 'w') # cPickle.dump(simulation, f) # f.close() return simulation
def simulate_asl_full_physio(output_dir=None, noise_scenario='high_snr', spatial_size='tiny'): """ Generate ASL data by integrating a physiological dynamical system. Ags: - output_dir (str|None): path where to save outputs as nifti files. If None: no output files - noise_scenario ("high_snr"|"low_snr"): scenario defining the SNR - spatial_size ("tiny"|"normal") : scenario for the size of the map - "tiny" produces 2x2 maps - "normal" produces 20x20 maps Result: dict (<item_label (str)> : <simulated_item (np.ndarray)>) -> a dictionary mapping names of simulated items to their values WARNING: in this dict the 'bold' item is in fact the ASL signal. This name was used to be compatible with JDE which assumes that the functional time series is named "bold". TODO: rather use the more generic label 'fmri_signal'. TODO: use magnetization model to properly simulate final ASL signal """ drift_var = 10. dt = .5 dsf = 2 #down sampling factor if spatial_size == 'tiny': lmap1, lmap2, lmap3 = 'tiny_1', 'tiny_2', 'tiny_3' elif spatial_size == 'random_small': lmap1, lmap2, lmap3 = 'random_small', 'random_small', 'random_small' else: lmap1, lmap2, lmap3 = 'icassp13', 'ghost', 'house_sun' if noise_scenario == 'high_snr': v_noise = 0.05 conditions = [ Condition(name='audio', m_act=10., v_act=.1, v_inact=.2, label_map=lmap1), Condition(name='video', m_act=11., v_act=.11, v_inact=.21, label_map=lmap2), Condition(name='damier', m_act=12., v_act=.12, v_inact=.22, label_map=lmap3), ] else: #low_snr v_noise = 2. conditions = [ Condition(name='audio', m_act=1.6, v_act=.3, v_inact=.3, label_map=lmap1), Condition(name='video', m_act=1.6, v_act=.3, v_inact=.3, label_map=lmap2), ] simulation_steps = { 'dt' : dt, 'dsf' : dsf, 'tr' : dt * dsf, 'condition_defs' : conditions, # Paradigm 'paradigm' : simbase.create_localizer_paradigm_avd, # Labels 'labels_vol' : simbase.create_labels_vol, 'labels' : simbase.flatten_labels_vol, 'nb_voxels': lambda labels: labels.shape[1], # Neural efficacy 'neural_efficacies' : create_tbg_neural_efficacies, # BRF 'primary_brf' : create_physio_brf, 'brf' : simbase.duplicate_brf, # PRF 'primary_prf' : create_physio_prf, 'prf' : simbase.duplicate_prf, # Physiological model 'physiological_params' : PHY_PARAMS_FRISTON00, ('flow_induction','perf_stim_induced','cbv','hbr') : create_evoked_physio_signals, 'bold_stim_induced' : create_bold_from_hbr_and_cbv, # Noise 'v_gnoise' : v_noise, 'noise' : simbase.create_gaussian_noise_asl, # Drift 'drift_order' : 4, 'drift_var' : drift_var, 'drift_coeffs': simbase.create_drift_coeffs_asl, 'drift' : simbase.create_polynomial_drift_from_coeffs_asl, # ASL 'ctrl_tag_mat' : simbase.build_ctrl_tag_matrix, 'asl_shape' : simbase.calc_asl_shape, # Perf baseline #should be the inflow at rest ... #TODO 'perf_baseline' : simbase.create_perf_baseline, 'perf_baseline_mean' : 0., 'perf_baseline_var': 0., # maybe rename to ASL (should be also modified in JDE)#TODO 'bold' : simbase.create_asl_from_stim_induced, } simu_graph = Pipeline(simulation_steps) # Compute everything simu_graph.resolve() simulation = simu_graph.get_values() if output_dir is not None: #simu_graph.save_graph_plot(op.join(output_dir, 'simulation_graph.png')) simbase.simulation_save_vol_outputs(simulation, output_dir) # f = open(op.join(output_dir, 'simulation.pck'), 'w') # cPickle.dump(simulation, f) # f.close() return simulation
from pyhrf import get_tmp_path import numpy as np import pyhrf.boldsynth.scenarios as simu from pyhrf.tools import Pipeline simulation_steps = { 'dt' : 0.6, 'dsf' : 4, #downsampling factor -> tr = dt * dsf = 2.4 'mask' : np.array([[[1,1,1,1,1,1,1]]]), 'labels' : np.array([[0,0,1,1,0,1,1]]), 'mean_act' : 3., 'var_act' : 0.5, 'mean_inact' : 0., 'var_inact' : 0.5, 'nrls' : simu.create_bigaussian_nrls, 'rastered_paradigm' : np.array([[0,0,1,0,0,0,1,0]]), 'hrf' : simu.create_canonical_hrf, 'v_noise' : 1., 'bold_shape' : simu.get_bold_shape, 'noise' : simu.create_gaussian_noise, 'stim_induced_signal' : simu.create_stim_induced_signal, 'bold' : simu.create_bold, } simulation = Pipeline(simulation_steps) simulation.resolve() simulation_items = simulation.get_values() output_dir = get_tmp_path() print 'Save simulation to:', output_dir simu.save_simulation(simulation_items, output_dir=output_dir)
def test_multiple_output_values(self): data = Pipeline({'e': foo_a, 'b': foo_default_arg, ('a', 'd'): foo_multiple_returns}) data.resolve()
def testRepr(self): data = Pipeline(self.depTree)
# Definition of the simulation pipeline simulation_steps = { 'spatial_shape': (10, 11, 12), 'mean_rls': 3., 'var_rls': 0.5, 'response_levels': generate_rls, 'rasteRed_paradigm': amplitude, 'hrf': np.array([0, .5, 1, 0.5, 0., 0]), 'noise_var': 1., 'noise': generate_noise, 'stim_induced_signal': create_stim_induced_signal, 'bold': create_bold, } simulation = Pipeline(simulation_steps) # Computation of all quantities in the pipeline and data saving simulation.resolve() simulation_items = simulation.get_values() #simulation_items['response_levels'].save(os.path.join(SIMULATED_INPUT_FOLDER,'mask_parcel.nii')) #simulation_items['stim_induced_signal'].save(os.path.join(SIMULATED_INPUT_FOLDER,'mask_parcel.nii')) simulation_items['bold'].save(os.path.join(SIMULATED_INPUT_FOLDER, 'bold.nii')) # Save a file contrib.nii to create the mask with seuillage_contrib.py simulation_items['bold'].save( os.path.join(SIMULATED_INPUT_FOLDER, 'contrib.nii')) # Writing paradigm in csv file row_csv = [] for i in range(len(amplitude)):