Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converting Freesurfer segmentation to LayNii rim format required by LN2_LAYERS #78

Open
SMScottLee opened this issue Jun 12, 2023 · 20 comments

Comments

@SMScottLee
Copy link

SMScottLee commented Jun 12, 2023

Hi all,

I've went through some of your tutorial articles and videos, but still don't know how to get the rim.nii as an input file to LN2_LAYERS. I saw that you're planning to add an option in LN2_RIMIFY to convert Freesurfer segmentation to rim.nii. But in the meantime, could you point me to the resources or tell me how to get the rim.nii after Freesurfer segmentation?

Thanks very much,
Scott

@ofgulban
Copy link
Collaborator

ofgulban commented Jun 12, 2023

Dear @SMScottLee ,

Thanks for your question. I would be happy to point you to a python script that converts Freesurfer's aseg format to rim format:

  • See this video: https://youtu.be/y5MqN0jGIBU . In the video description you can find a reference to a python script that does the conversion: "- Freesurfer aseg file to LayNii rim file conversion: link here"

NOTE: The script above assumes the user already converted from aseg.mgz format to nifti. One way of doing this mgz to nii conversion is available at: https://surfer.nmr.mgh.harvard.edu/fswiki/mri_convert

@ofgulban ofgulban changed the title rim.nii for LN2_LAYERS Converting Freesurfer asegmentations to rim format required by LN2_LAYERS Jun 12, 2023
@ofgulban ofgulban changed the title Converting Freesurfer asegmentations to rim format required by LN2_LAYERS Converting Freesurfer segmentation to LayNii rim format required by LN2_LAYERS Jun 12, 2023
@SMScottLee
Copy link
Author

Hi ofgulban,

Thanks for your suggestion and it seems to work well!

But I still have questions about the following steps. After the conversion and using LN2_LAYERS to label different numbers of layers, how do I get the final layer fMRI profiles? Are the four steps in this page (https://layerfmri.com/2018/03/11/quick-layering/) I should do? If I use LN2_LAYERS to have e.g., 20 layers, do I need to do the first step, upscaling?

Sorry that after going through some of the articles and videos, I still couldn't figure out the exact steps/pipeline for using LayNii. Could you please again point me to the right resource and direction?

Thanks for your help. If you think some of my questions could be a new issue, I'm happy to create it.

Best regards,
Scott

@ofgulban
Copy link
Collaborator

Dear @SMScottLee ,

No worries, feel free to continue asking questions here. A few points:

  1. Yes, you need to probably upsample your data (assuming that you have 0.8 mm iso. res.) for 20 layers.
  2. However, I would recommend you to understand and think about using the metric output file of LN2_LAYERS.
  3. You can see that https://layerfmri.com/2018/03/11/quick-layering/ has 2018/03/11 indicating that this is an older tutorial.
  4. More recent tutorials (2022) are available as a playlist: https://youtube.com/playlist?list=PLs_umVHtShfadNm8brOweXHUSmqVDTk4q . I highly recommend you to watch these.
  5. Maybe also see the final ~15 of this video (2023), that talks about LN2_LAYERS: https://youtu.be/tIuKG3rtVk4

Maybe you can elaborate further what you mean by "final layer fMRI profiles" (different people mean different things by this). Do you have an example figure in mind? I can give you more specific directions if you clarify which inputs you currently have and what type of output/figure you are envisioning to have.

@SMScottLee
Copy link
Author

Thanks for the suggestions. I'll watch them.
The final layer fMRI profile I mean would be like the first figure on the website I posted above, with the x-axis from csf to wm and y-axis the activity.

@ofgulban
Copy link
Collaborator

I see, thanks @SMScottLee , I will be back to you in a few days with an example python script.

@ofgulban
Copy link
Collaborator

ofgulban commented Jun 26, 2023

Hi @SMScottLee ,
Sorry that it took a bit of time for me to get back to this. Quickly:

  1. I have added 2 new test files that will be used in the plotting examples below:

  2. I have added a simple layer profile scatter plotting script to LayNii_extras repository:

  3. When there are too many voxels, the scatter plot becomes too crowded. Therefore, plotting 2D histogram would be useful:

  4. Sometimes it is handy to simplify the data into a line plot by compressing the x axis to a number of bins. Conventional approach is the line plot, however I think that line plots runs the risk of hiding some patterns due(tagging @mrneont ):

Hope these pointers will be useful for you to clarify how to plot layer profiles.

@SMScottLee
Copy link
Author

Hi @ofgulban ,

Thank you so much! All the materials are very helpful and I'm following through them.

I currently have the equivol_metric file, contrast images generated from SPM, and an ROI defined by statistical result. But I get an error regarding index match when taking the voxels from the ROI mask in your script. Is it because my equivol_metric file is whole-brain but the mask is an ROI? How could I get an equivol_metric image with the size of ROI, like the example in your comments and tutorials?

Many thanks,
Scott

@ofgulban
Copy link
Collaborator

ofgulban commented Jul 5, 2023

Hi @SMScottLee ,

I am guessing that probably ypu have your nifti images at different resolutions. Which is why indexing is giving you trouble (probably you are trying to do logical indexing based on e.g. 100x100x100 array applied to 256x256x256).

I recommend you to carefully check your input data dimensions. You can use several programs to understand your input dimensions (niimath, fslinfo, fslhd, or custom script in python e.g. 'print(myArray.shape')). Could you check this and let me know of your input image dimensions? Then I could better recommend you how to proceed.

@SMScottLee
Copy link
Author

Yes, you are right. The dimensions of my aseg_rim_metric_equidist file are 320x320x320, but contrast and ROI files are 157x189x156.

@SMScottLee
Copy link
Author

Hi @ofgulban ,

The previous contrast and ROI files have been normalized to a template. But the unnormalized ones also have different dimensions (240x234x54), because of different FOVs and resolutions of structural and functional images. Do you have any recommendations for this?

Thank you,
Scott

@ofgulban
Copy link
Collaborator

ofgulban commented Jul 10, 2023

Hi @SMScottLee ,
sorry I have been really busy over the last week. You need to use resampling to make your data dimensions equal. For instance, I commonly use Convert3D (http://www.itksnap.org/pmwiki/pmwiki.php?n=Convert3D.Convert3D) to reslice the data. You can check -reslice-matrix: Resample image using affine transform section from https://sourceforge.net/p/c3d/git/ci/master/tree/doc/c3d.md#-reslice-matrix-resample-image-using-affine-transform .

@ofgulban
Copy link
Collaborator

In such cases, it might be easier for me to give you a recommendation based on your images. So, you can consider sending me a few images that you want to reslice, and I might be able to return back to you with a more concrete set of instructions.

@SMScottLee
Copy link
Author

Thank you! I attach my rim file (aseg_rim.nii.gz), equivol_metric file (aseg_rim_metric_equidist.nii.gz), scalar map (con_0002.nii.gz), and ROI mask (rFFA_FWE05.nii.gz).
aseg_rim.nii.gz
aseg_rim_metric_equidist.nii.gz
con_0002.nii.gz
rFFA_FWE05.nii.gz

@SMScottLee
Copy link
Author

I've resliced the metric_equidist file (attached) into the same dimensions as scalar map and ROI.
aseg_rim_metric_equidist_reslice.nii.gz

The script for scatter plotting now works but I'm not sure the result is right.
scatterplot_con0002

One possibility is that the con_0002 image is the difference between the activity of two conditions (in a face image paradigm). But when I plot only for the activity of one condition, the results look similar (dots appear across all extents of value and depth).
Another possibility is that my ROI is too big? But when I apply a smaller ROI, the results look similar.
Any clues?
Thank you.

@ofgulban
Copy link
Collaborator

ofgulban commented Jul 12, 2023

After having a look at your inputs, yes your ROI is too large. Specifically, I would take a conjunction between your ROI and the gray matter that you have segmented. All the dark spots at depth = 0 comes from outside of the gray matter voxels. For reference, this is how the plot looks using the files you have attached above and after gray matter masking:

Figure_1

Figure_2

As an additional note, your statistical map that you have attached looks very very smooth. So I am not sure if the preprocessing or data acquisition details are suitable for doing laminar investigations. As an example data, it works perfectly fine though.

@ofgulban
Copy link
Collaborator

I have made videos addressing 2 issues encountered in this conversation. Putting here for future reference:

  1. Reslicing nifti images to the same dimension and resolution: https://youtu.be/vchtaEC2zMk
  2. Plotting layer profiles: https://youtu.be/7YJPP81NKI4

@SMScottLee
Copy link
Author

Hi @ofgulban ,

Thanks for the explanations and the two videos. They are very helpful!

Yes, the statistical map was smoothed with 8mm FWHM to define ROIs. I'm now using LN_LAYER_SMOOTH for smoothing the unsmoothed data within layers but getting message "There are 0 layers/masks to smooth within".
LN_LAYER_SMOOTH -layer_file aseg_rim_metric_equidist.nii.gz -input con_0002_resliced.nii.gz -FWHM 0.75
0.75 is the current voxel size of layer and input files. Or what FWHM should I use?

Best,
Scott

@layerfMRI
Copy link
Owner

Hi Scott,

The LN_LAYERS_MOOTH program smoothes signal from voxels that are in the same layer. Whether or not a voxel is within a given layer is a binary criteria. With additional weighting of geodesic difference.

Thus, the input file should be the layer file (not the metric file). I would even recommend to using the file with the layer bins (output of LN2LAYER with -equalbins option).

There might be a way to achieve layer smoothing with LN2_UVD filter, that uses the metric file. But I am not sure if this is generally doable. I suspect it 'only' work within local patches.

Best, renzo

@SMScottLee
Copy link
Author

Hi @ofgulban ,

Thanks! I've smoothed the layer file and would like to plot layer profile of the smoothed image.
However I got an error
File "01_layer_profiles_scatterplot.py", line 43, in
plt.ylim(np.percentile(data_y, (0, 100)))
...
raise ValueError("Axis limits cannot be NaN or Inf")
ValueError: Axis limits cannot be NaN or Inf
Do you have any suggestions?

Best,
Scott

@ofgulban
Copy link
Collaborator

ofgulban commented Aug 19, 2023

Dear @SMScottLee ,

This error indicates that you have nans or infinities in your data. You need to trace where these values are coming from. As a workaround you can replace the nans and inf with another value (eg 0). Maybe read about numpy's isfinite function.

In addition, note that this issue is related to common programming knowledge and not a direct LayNii issue. Therefore I advise you to read up on nans and infinities and ways of dealing with them or preventing them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants