Fork me on GitHub

pyhrf.sandbox.physio module

pyhrf.sandbox.physio.buildOrder1FiniteDiffMatrix_central(size, dt)

returns a toeplitz matrix for central differences to correct for errors on the first and last points (due to the fact that there is no rf[-1] or rf[size] to average with):

  • uses the last point to calcuate the first and vis-versa
  • this is acceptable bc the rf is assumed to begin & end at steady state (thus the first and last points should both be zero)
pyhrf.sandbox.physio.calc_linear_rfs(simu_brf, simu_prf, phy_params, dt, normalized_rfs=True)

Calculate ‘prf given brf’ and ‘brf given prf’ based on the a linearization around steady state of the physiological model as described in Friston 2000.

Input:
  • simu_brf, simu_prf: brf and prf from the physiological simulation
    from which you wish to calculate the respective prf and brf. Assumed to be of size (1,hrf.size)
  • phy_params
  • normalized_rfs: set to True if simu_hrfs are normalized
Output:
  • calc_brf, calc_prf: np.arrays of shape (hrf.size, 1)
  • q_linear, v_linear: q and v calculated according to the linearized model

Note: These calculations do not account for any rescaling between brf and prf. This means the input simu_brf, simu_prf should NOT be rescaled.

** Warning**:
  • this function assumes prf.size == brf.size and uses this to build D, I
  • if making modifications: calc_brf, calc_prf have a truncation error (due to the finite difference matrix used) on the order of O(dt)^2. If for any reason a hack is later implemented to set the y-intecepts of brf_calc, prf_calc to zero by setting the first row of X4, X3 = 0, this will raise a singular matrix error in the calculation of calc_prf (due to X.I command), so this error is helpful in this case
pyhrf.sandbox.physio.create_asl_from_stim_induced(bold_stim_induced_rescaled, perf_stim_induced, ctrl_tag_mat, dsf, perf_baseline, noise, drift=None, outliers=None)

Downsample stim_induced signal according to downsampling factor ‘dsf’ and add noise and drift (nuisance signals) which has to be at downsampled temporal resolution.

pyhrf.sandbox.physio.create_bold_from_hbr_and_cbv(physiological_params, hbr, cbv)

Compute BOLD signal from HbR and blood volume variations obtained by a physiological model

pyhrf.sandbox.physio.create_evoked_physio_signals(physiological_params, paradigm, neural_efficacies, dt, integration_step=0.05)

Generate evoked hemodynamics signals by integrating a physiological model.

Parameters:
  • physiological_params (dict (<pname (str)> : <pvalue (float)>)) – parameters of the physiological model. In jde.sandbox.physio see PHY_PARAMS_FRISTON00, PHY_PARAMS_FMRII …
  • paradigm (pyhrf.paradigm.Paradigm) – the experimental paradigm
  • neural_efficacies (np.ndarray (nb_conditions, nb_voxels, float)) – neural efficacies involved in flow inducing signal.
  • dt (float) – temporal resolution of the output signals, in second
  • integration_step (float) – time step used for integration, in second
Returns:

All generated signals, indexes of the first axis correspond to:

  • 0: flow inducing
  • 1: inflow
  • 2: blood volume
  • 3: [HbR]

Return type:

np.array((nb_signals, nb_scans, nb_voxels), float)

pyhrf.sandbox.physio.create_omega_prf(primary_brf, dt, physiological_params)
pyhrf.sandbox.physio.create_physio_brf(physiological_params, response_dt=0.5, response_duration=25.0, return_brf_q_v=False)

Generate a BOLD response function by integrating a physiological model and setting its driving input signal to a single impulse.

Parameters:
  • physiological_params (-) – <pvalue (float)>)): parameters of the physiological model. In jde.sandbox.physio see PHY_PARAMS_FRISTON00, PHY_PARAMS_FMRII …
  • response_dt (-) – temporal resolution of the response, in second
  • response_duration (-) – duration of the response, in second
Returns:

  • np.array(nb_time_coeffs, float) -> the BRF (normalized)
  • also return brf_not_normalized, q, v when return_prf_q_v=True (for error checking of v and q generation in calc_hrfs)

pyhrf.sandbox.physio.create_physio_prf(physiological_params, response_dt=0.5, response_duration=25.0, return_prf_q_v=False)

Generate a perfusion response function by setting the input driving signal of the given physiological model with a single impulse.

Parameters:
  • physiological_params (-) – <pvalue (float)>)): parameters of the physiological model. In jde.sandbox.physio see PHY_PARAMS_FRISTON00, PHY_PARAMS_FMRII …
  • response_dt (-) – temporal resolution of the response, in second
  • response_duration (-) – duration of the response, in second
Returns:

  • np.array(nb_time_coeffs, float) -> the PRF
  • also return brf_not_normalized, q, v when return_prf_q_v=True (for error checking of v and q generation in calc_hrfs)

pyhrf.sandbox.physio.create_tbg_neural_efficacies(physiological_params, condition_defs, labels)

Create neural efficacies from a truncated bi-Gaussian mixture.

TODO: settle how to relate brls and prls to neural efficacies

Parameters:
  • physiological_params (dict (<param_name> : <param_value>)) – parameters of the physiological model
  • condition_defs (list of pyhrf.Condition) –

    list of condition definitions. Each item should have the following fields (moments of the mixture):

    • m_act (0<=float<eff_max): mean of activating component
    • v_act (0<float): variance of activating component
    • v_inact (0<float): variance of non-activating component
  • labels (np.array((nb_cond, nb_vox), int)) – binary activation states
Returns:

the generated neural efficacies

Return type:

np.array(np.array((nb_cond, nb_vox), float))

pyhrf.sandbox.physio.linear_rf_operator(rf_size, phy_params, dt, calculating_brf=False)
Calculates the linear operator A needed to convert brf to prf & vis-versa
prf = (A^{-1})brf brf = (A)prf
Inputs:
  • size of the prf and/or brf (assumed to be same)
  • physiological parameters
  • time resolution of data:
  • if you wish to calculate brf (return A), or prf (return inverse of A)
Outputs:
  • np.array of size (hrf_size,1) linear operator to convert hrfs
pyhrf.sandbox.physio.phy_integrate_euler(phy_params, tstep, stim, epsilon, Y0=None)

Integrate the ODFs of the physiological model with the Euler method.

TODO: should the output signals be rescaled wrt their value at rest?

Parameters:
  • phy_params (dict (<param_name> : <param_value>)) – parameters of the physiological model
  • tstep (float) – time step of the integration, in seconds.
  • stim (np.array(nb_steps, float)) – stimulation sequence with a temporal resolution equal to the time step of the integration
  • epsilon (float) – neural efficacy
  • Y0 (np.array(4, float) | None) – initial values for the physiologica signals. If None: [0, 1, 1, 1.] s f_in q v
Returns:

the integrated physiological signals, where indexes of the first axis correspond to:

  • 0 : flow inducing
  • 1 : inflow
  • 2 : HbR
  • 3 : blood volume

Return type:

np.array((4, nb_steps), float)

pyhrf.sandbox.physio.plot_calc_hrf(hrf1_simu, hrf1_simu_name, hrf1_calc, hrf1_calc_name, hrf2_simu, hrf2_simu_name, dt)
pyhrf.sandbox.physio.rescale_bold_over_perf(bold_stim_induced, perf_stim_induced, bold_perf_ratio=5.0)
pyhrf.sandbox.physio.run_calc_linear_rfs()

Choose physio parameters. Choose to generate simu_rfs from multiple or single stimulus.

TODO:

  • figure out why there is an issue that perf_stim_induced is much greater than bold_stim_induced
  • figure out why when simu_brf`=`bold_stim_induced_rescaled, calc_brf is so small it appears to be 0
pyhrf.sandbox.physio.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

pyhrf.sandbox.physio.simulate_asl_phylin_prf(output_dir=None, noise_scenario='high_snr', spatial_size='tiny')

Generate ASL data according to a LTI system, with canonical BRF and PRF = Omega.BRF.

Parameters:
  • output_dir (-) – path where to save outputs as nifti files. If None: no output files
  • noise_scenario (-) – scenario defining the SNR
  • spatial_size (-) – 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’.
pyhrf.sandbox.physio.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.

Parameters:
  • output_dir (-) – path where to save outputs as nifti files. If None: no output files
  • noise_scenario (-) – scenario defining the SNR
  • spatial_size (-) – 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’.