8000 fmrisim notebook example: wrong dimensions · Issue #541 · brainiak/brainiak · GitHub
[go: up one dir, main page]

Skip to content

fmrisim notebook example: wrong dimensions #541

Open

Description

@marcelzwiers

I have been running the fmrisim jupyter notebook using my own data. It went well until I hit this cell:

# Specify the parameters for signal
signal_method = 'CNR_Amp/Noise-SD'
signal_magnitude = [0.5]

# Where in the brain are there stimulus evoked voxels
signal_idxs = np.where(signal_volume == 1)

# Pull out the voxels corresponding to the noise volume
noise_func = noise[signal_idxs[0], signal_idxs[1], signal_idxs[2], :].T
# Compute the signal appropriate scaled
signal_func_scaled = fmrisim.compute_signal_change(signal_func,
                                                  noise_func,
                                                  noise_dict,
                                                  magnitude=signal_magnitude,
                                                  method=signal_method)

That gave me this error:

ValueError                                Traceback (most recent call last)
/scratch/marzwi/slurm_job_55885/ipykernel_2104266/2965812987.py in <module>
     13                                                   noise_dict,
     14                                                   magnitude=signal_magnitude,
---> 15                                                   method=signal_method)

~/PycharmProjects/wp15/BIDScramble/bidscramble/fmrisim.py in compute_signal_change(signal_function, noise_function, noise_dict, magnitude, method)
   2856     if signal_function.shape != noise_function.shape:
   2857         msg = 'noise_function is not the same size as signal_function'
-> 2858         raise ValueError(msg)
   2859 
   2860     # Scale all signals that to have a range of -1 to 1. This is so that any values less than this will be scaled appropriately

ValueError: noise_function is not the same size as signal_function

If I compare it to this input signal size I see that signal_func is one element too long:

signal_func.shape, noise_func.shape, nii.shape
((591, 27), (590, 27), (104, 104, 66, 590))

Any suggestions on how to fix this

Activity

  1. marcelzwiers commented on Jul 4, 2024

    @marcelzwiers
    Author

    I am using the latest code from github

  2. lcnature commented on Jul 4, 2024

    @lcnature
    Contributor

    Hi @marcelzwiers can you show how signal_func is produced before this part of code?

  3. marcelzwiers commented on Jul 4, 2024

    @marcelzwiers
    Author
    signal_func = fmrisim.convolve_hrf(stimfunction=stimfunc_weighted,
                                       tr_duration=tr,
                                       temporal_resolution=temporal_res,
                                       scale_function=True)
  4. lcnature commented on Jul 4, 2024

    @lcnature
    Contributor

    Thanks! What is the shape of stimfunc_weighted, the values of tr and temporal_res?

  5. marcelzwiers commented on Jul 5, 2024

    @marcelzwiers
    Author
    stimfunc_weighted.shape: (5900, 27)
    tr: 1.0
    temporal_res: 10.0
    

    However, if I set burn_in = 0 in the brainiak fmrisim example notebook (in the In[19] cell), then the dimensions are correct. That seems like a bug / the root of the problem, right?

    # Set up stimulus event time course parameters
    event_duration = 2  # How long is each event
    isi = 7  # What is the time between each event
    burn_in = 1  # How long before the first event
  6. changed the title [-]fmrisim wrong dimensions[/-] [+]fmrisim notebook example: wrong dimensions[/+] on Jul 5, 2024
  7. lcnature commented on Jul 5, 2024

    @lcnature
    Contributor

    Yes, I think this may be a bug (in the demo). I think this line total_time = int(dim[3] * tr) + burn_in should not include burn_in or it should be removed when feeding to total_time of generate_stimfunction`. For your own task, I suggest using the timing of your own design instead of strictly following the demo.

    @CameronTEllis Would you mind taking a look at the Jupyter notebook demo and confirm whether simply replacing total_time = int(dim[3] * tr) + burn_in with total_time = int(dim[3] * tr) or making burn_in = 0 should be correct?

  8. CameronTEllis commented on Jul 5, 2024

    @CameronTEllis
    Contributor

    Hi @marcelzwiers and @lcnature, thanks for working on this, you are right: burn_in should be set to zero in this code. Previously, the compute_signal_change didn't have the shape size check, so we didn't have this issue previously.

    I have made a new PR (#542) for the code with no burn_in. I could have changed the code so that there is an equivalent burn in for generating the noise; however, that is a step of complexity that is unnecessary.

  9. marcelzwiers commented on Jul 8, 2024

    @marcelzwiers
    Author

    In the same notebook it is stated:

    When SNR is low, Rician noise is a good estimate of background noise data (Gudbjartsson, & Patz, 1995). However if you look at the distribution of non-brain voxel values averaged across time (i.e., the template) then you see that this is also rician, suggesting that most of the rician noise is a result of the structure in the background of the brain (e.g. the baseline MR of the head coil or skull). If you subtract this baseline then the noise becomes approximately gaussian, especially in the regions far from the brain (which is what the calc_noise algorithm considers when calculating SNR). Hence the machine noise here is Gaussian added to an inherently Rician baseline.

    I cannot make any sense of this? Rician noise in MR is well understood, which is as you may well know just Gaussian noise during the acquisition of the (complex) k-space datapoints (the total signal being the vector sum of the MR signal vector + the random noise vector). After Fourier transformation of the k-space data to (magnitude) image space, the complex Gaussian noise emerges as Rician magnitude noise. The higher the SNR, the larger the distance between the reference point and the center of the bivariate Rice distribution in the image domain, and then the distribution looks more and more like a Gaussian one. There are excellent methods out there to estimate (and transform) the Rician noise level in MR (especially in the diffusion-weighted MR literature, which always suffers heavily from Rician noise in their brain signals). Is fmrisim still being developed?

  10. CameronTEllis commented on Jul 8, 2024

    @CameronTEllis
    Contributor

    Hi Marcel,

    This is a great point. I agree that we took a heuristic shortcut and that there are better methods to directly relate SNR to the noise distribution.

    Yes, fmrisim is still in development, albeit slowly. There are a few additions that I intend to make in the near term. I welcome any suggestions you have, or better yet, any contributions you wish to make. If you would like to talk about it, I am happy to chat over zoom.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0