Converting Freesurfer segmentation to LayNii rim format required by LN2_LAYERS
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
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
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
Dear @SMScottLee ,
No worries, feel free to continue asking questions here. A few points:
- Yes, you need to probably upsample your data (assuming that you have 0.8 mm iso. res.) for 20 layers.
- However, I would recommend you to understand and think about using the
metricoutput file of LN2_LAYERS. - You can see that https://layerfmri.com/2018/03/11/quick-layering/ has 2018/03/11 indicating that this is an older tutorial.
- More recent tutorials (2022) are available as a playlist: https://youtube.com/playlist?list=PLs_umVHtShfadNm8brOweXHUSmqVDTk4q . I highly recommend you to watch these.
- 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.
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.
I see, thanks @SMScottLee , I will be back to you in a few days with an example python script.
Hi @SMScottLee , Sorry that it took a bit of time for me to get back to this. Quickly:
-
I have added 2 new test files that will be used in the plotting examples below:
- Input 1 scalar map: https://github.com/layerfMRI/LAYNII/blob/devel/test_data/Ding2016_occip_T2starweighted_filtered_for_tests.nii.gz
- Input 2 is a
equivol_metricfile (I ran LN2_LAYER on the already available rim file: https://github.com/layerfMRI/LAYNII/blob/devel/test_data/Ding2016_occip_rim.nii.gz ) - Input 3 ROI mask: https://github.com/layerfMRI/LAYNII/blob/devel/test_data/Ding2016_occip_ROI.nii.gz
-
I have added a simple layer profile scatter plotting script to LayNii_extras repository:
- https://github.com/ofgulban/LAYNII_extras/blob/master/demo-plot_layer_profiles/01_layer_profiles_scatterplot.py
- Gives the following result:
-
When there are too many voxels, the scatter plot becomes too crowded. Therefore, plotting 2D histogram would be useful:
- https://github.com/ofgulban/LAYNII_extras/blob/master/demo-plot_layer_profiles/02_layer_profiles_2Dhistogram.py
- Gives the following result:
-
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 ):
-
https://github.com/ofgulban/LAYNII_extras/blob/master/demo-plot_layer_profiles/03_layer_profiles_lineplot.py
-
This gives the following result with 7 layer quantization:
-
Number of layers can be adjusted in the plotting script. For instance below is ran with 21 layers instead of 7:
-
Hope these pointers will be useful for you to clarify how to plot layer profiles.
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
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.
Yes, you are right. The dimensions of my aseg_rim_metric_equidist file are 320x320x320, but contrast and ROI files are 157x189x156.
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
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 .
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.
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
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.
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.
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:
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.
I have made videos addressing 2 issues encountered in this conversation. Putting here for future reference:
- Reslicing nifti images to the same dimension and resolution: https://youtu.be/vchtaEC2zMk
- Plotting layer profiles: https://youtu.be/7YJPP81NKI4
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
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
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
Best, Scott
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.