faskowit / app-fmri-2-mat

fmriprep outputs to connectivity matrices
MIT License
7 stars 5 forks source link

Input atlas must come from Multi-atlas transfer App #18

Open soichih opened 3 years ago

soichih commented 3 years ago

According to @faskowit, any atlas (like the one from freesurfer) should work with this App, but if a user feed parcellation input from freesurfer, it will fail with this error message

+ singularity exec -e docker://faskowit/app-fmri-2-mat:brnch-0.1.6 ./run.sh
INFO:    Using cached SIF image
/usr/local/lib/python3.6/dist-packages/nilearn/image/resampling.py:513: UserWarning: Casting data from int16 to float32
  warnings.warn("Casting data from %s to %s" % (data.dtype.name, aux))
/usr/local/lib/python3.6/dist-packages/nilearn/image/image.py:214: UserWarning: The parameter 'fwhm' for smoothing is specified as 0.0. Setting it to None (no smoothing will be performed)
  .format(fwhm))
Traceback (most recent call last):
  File "/N/project/brainlife/workdir/prod-bigred3/60aeff45979fdc0a0e82da2b/60b1b800979fdc5c208373d3//src/makemat.py", line 212, in <module>
    main()
  File "/N/project/brainlife/workdir/prod-bigred3/60aeff45979fdc0a0e82da2b/60b1b800979fdc5c208373d3//src/makemat.py", line 186, in main
    nogaptimeseries[:,([x-1 for x in regions])] = timeseries 
ValueError: shape mismatch: value array of shape (620,111) could not be broadcast to indexing result of shape (110,620)

620 in this case is the 4D size of the bold (time slices). I couldn't figure out where 111 or 110 come from. The input parcellation has 114 unique values inside parc.nii.gz

[   0    2    3    4    5    7    8   10   11   12   13   14   15   16
   17   18   24   26   28   30   31   41   42   43   44   46   47   49
   50   51   52   53   54   58   60   62   63   77   85  251  252  253
  254  255 1000 1001 1002 1003 1005 1006 1007 1008 1009 1010 1011 1012
 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
 1027 1028 1029 1030 1031 1032 1033 1034 1035 2000 2001 2002 2003 2005
 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019
 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033
 2034 2035]
114

But maybe it's ignoring a few parcellation? Whatever the case is, 110 v.s. 111 (of-by-one) smells fishy. It could be that app-freesurfer is not generating the parcellation/label.json correctly?

soichih commented 3 years ago

@giulia-berto @francopestilli @bhatiadheeraj Please feel free to jump in with troubleshooting this if you have time. I've seen this issue happening to several other users.

francopestilli commented 3 years ago

Hi @soichih @faskowit what happens if an roi is too small and is there a chance the number of boxes can be reduced down to 0 and rois dropped. For example 3 rois were dropped here? 114--->111,110?

giulia-berto commented 3 years ago

@soichi @faskowit @francopestilli I tested the App fMRI Timeseries Extraction with all the three parcellations outputs from FreeSurfer separately in one subject, and all the runs finished successfully. In the log I have: !!!WARNING!!! during resampling of label image, some of the ROIs (likely very small) were interpolated out. Please take care to note which ROIs are present in the output data. Also, as far as I understand, parcellation and bold data should be in the same space.

francopestilli commented 3 years ago

@faskowit precisely what we suspected. Is the output robust to the loss of ROIs due to interpaolation? @giulia-berto excellent, thanks

faskowit commented 3 years ago

yes yes I wrote that warning message for this case. This lines are the relevant lines of the code that are supposed to catch interpolated regions: https://github.com/faskowit/app-fmri-2-mat/blob/master/src/makemat.py#L56-L84 The app is supposed to be robust to interpolated out regions, based on these lines specifically: https://github.com/faskowit/app-fmri-2-mat/blob/master/src/makemat.py#L66-L72

The error that is still seen from the app seems to be an edge case, in which the code above doesn't see that the region will be interpolated out. I'm afraid there is a limitation for the nilearn function that I use NiftiLabelsMasker.fit_transform(): https://nilearn.github.io/modules/generated/nilearn.input_data.NiftiLabelsMasker.html as it will not tell you which timeseries corresponds to which region--it simply outputs an ordered matrix. It is because it is assumed that the input parcellation would have reasonably large regions.

This brings us to the case of the FreeSurfer aparcs being used as input. They seem to contain small regions with 100's of voxels (probably areas like 30 - Left-vessel or 31 Left-choroid-plexus) in the 1mm space that will get interpolated out when down-sampling to fmri-space. To solve this, I propose that FreeSurfer parcellations be "cleaned" in such a way, to only retain a set of larger regions (like the maTT app does, with the pre-specified set of subcortical areas... but for the aparc, it could be whatever larger regions you'd like). This could be a separate app, or done in the FreeSurfer app.

Another solution would be to "pre-downsample" the label image. I would have to test to see if that affects the quality of non-edge case usage in any way.