diff --git a/blog/brain-fmri-to-3d-model-2019-08-23.markdown b/blog/brain-fmri-to-3d-model-2019-08-23.markdown new file mode 100644 index 0000000..5b5bf9e --- /dev/null +++ b/blog/brain-fmri-to-3d-model-2019-08-23.markdown @@ -0,0 +1,152 @@ +--- +title: How I Converted my Brain fMRI to a 3D Model +date: 2019-08-23 +--- + +# How I Converted my Brain fMRI to a 3D Model + +AUTHOR'S NOTE: I just want to start this out by saying I am not an expert, and +nothing in this blogpost should be construed as medical advice. I just wanted +to see what kind of pretty pictures I could get out of an fMRI data file. + +So this week I flew out to Stanford to participate in a study that involved a +fMRI of my brain while I was doing some things. I asked for (and recieved) a +data file from the fMRI so I could play with it and possibly 3D print it. This +blogpost is the record of my journey through various software to get a fully +usable 3D model out of the fMRI data file. + +## The Data File + +I was given [christine_brain.nii.gz][firstniifile] by the researcher who was +operating the fMRI. I looked around for some software to convert it to a 3D +model and [/r/3dprinting][r3dprinting] suggested the use of [FreeSurfer][freesurfer] +to generate a 3D model. I downloaded and installed the software then started +to look for something I could do in the meantime, as this was going to take +something on the order of 8 hours to process. + +### An Animated GIF + +I started looking for the file format on the internet by googling "nii.gz brain image" +and I stumbled across a program called [gif\_your\_nifti][gyn]. It looked to be +mostly pure python so I created a virtualenv and installed it in there: + +``` +$ git clone https://github.com/miykael/gif_your_nifti +$ cd gif_your_nifti +$ virtualenv -p python3 env +$ source env/bin/activate +(env) $ pip3 install -r requirements.txt +(env) $ python3 setup.py install +``` + +Then I ran it with the following settings to get [this first result][firstgif]: + +``` +(env) $ gif_your_nifti christine_brain.nii.gz --mode pseudocolor --cmap plasma +``` + +
+ +(sorry the video embed isn't working in safari) + +It looked weird though, that's because the fMRI scanner I used has a different +rotation to what's considered "normal". The gif\_your\_nifti repo mentioned a +program called `fslreorient2std` to reorient the fMRI image, so I set out to +install and run it. + +### FSL + +After some googling, I found [FSL's website][fsl] which included an installer +script and required registration. + +37 gigabytes of downloads and data later, I had the entire FSL suite installed +to a server of mine and ran the conversion command: + +``` +$ fslreorient2std christine_brain.nii.gz christine_brain_reoriented.nii.gz +``` + +This produced a slightly smaller [reoriented file][secondniifile]. + +I reran gif\_your\_nifti on this reoriented file and got [this result][secondgif] +which looked a _lot_ better: + +
+ +(sorry again the video embed isn't working in safari) + +### FreeSurfer + +By this time I had gotten back home and [FreeSurfer][freesurfer] was done installing, +so I registered for it (god bless the institution of None) and put its license key +in the place it expected. I copied the reoriented data file to my Mac and then +set up a `SUBJECTS_DIR` and had it start running the numbers and extracting the +brain surfaces: + +``` +$ cd ~/tmp +$ mkdir -p brain/subjects +$ cd brain +$ export SUBJECTS_DIR=$(pwd)/subjects +$ recon-all -i /path/to/christine_brain_reoriented.nii.gz -s christine -all +``` + +This step took 8 hours. Once I was done I had a bunch of data in +`$SUBJECTS_DIR/christine`. I opened my shell to that folder and went into the +`surf` subfolder: + +``` +$ mris_convert lh.pial lh.pial.stl +$ mris_convert rh.pial rh.pial.stl +``` + +Now I had standard stl files that I could stick into [Blender][blender]. + +### Blender + +Importing the stl files was really easy. I clicked on File, then Import, then +Stl. After guiding the browser to the subjects directory and finding the STL +files, I got a view that looked something like this: + +

BRAIN pic.twitter.com/kGSrPj0kgP

— Cadey Ratio 🌐 (@theprincessxena) August 22, 2019
+ +I had absolutely no idea what to do from here in Blender, so I exported the +whole thing to a stl file and sent it to a coworker for 3D printing (he said +it was going to be "the coolest thing he's ever printed"). + +I also exported an Unreal Engine 4 compatible model and sent it to a friend of +mine that does hobbyist game development. A few hours later I got this back: + +

pic.twitter.com/fXnwnSpMry

— Cadey Ratio 🌐 (@theprincessxena) August 23, 2019
+ +(Hint: it is a take on the famous [galaxy brain memes][galaxybrain]) + +## Conclusion + +Overall, this was fun! I got to play with many gigabytes of software that ran +my most powerful machine at full blast for 8 hours, I made a fully printable 3D +model out of it and I have some future plans for importing this data into +Minecraft (the NIFTI `.nii.gz` format has a limit of _256 layers_). + +I'll be sure to write more about this in the future! + +## Citations + +Here are my citations in [BibTex format][citations]. + +Special thanks goes to Micheal Lifshitz for organizing the study that I +participated in that got me this fMRI data file. It was one of the coolest +things I've ever done (if not the coolest) and I'm going to be able to get a +3D printed model of my brain out of it. + +[firstniifile]: https://xena.greedo.xeserv.us/files/christine_brain.nii.gz +[secondniifile]: https://xena.greedo.xeserv.us/files/christine_brain_reoriented.nii.gz +[r3dprinting]: https://www.reddit.com/r/3Dprinting/comments/2w0zxx/magnetic_resonance_image_nii_to_stl/ +[freesurfer]: https://surfer.nmr.mgh.harvard.edu/fswiki/FreeSurferWiki +[gyn]: https://github.com/miykael/gif_your_nifti +[firstgif]: /static/blog/christine-fmri-raw.mp4 +[secondgif]: /static/blog/christine-fmri-reoriented.mp4 +[fsl]: https://fsl.fmrib.ox.ac.uk/fsl/fslwiki/ +[blender]: https://www.blender.org +[galaxybrain]: https://knowyourmeme.com/memes/expanding-brain +[citations]: /static/blog/brainfmri-to-3d-model.bib \ No newline at end of file diff --git a/static/blog/brainfmri-to-3d-model.bib b/static/blog/brainfmri-to-3d-model.bib new file mode 100644 index 0000000..4d5a604 --- /dev/null +++ b/static/blog/brainfmri-to-3d-model.bib @@ -0,0 +1,308 @@ +@article{Desikan2006968, +title = "An automated labeling system for subdividing the human cerebral cortex on MRI scans into gyral based regions of interest", +journal = "NeuroImage", +volume = "31", +number = "3", +pages = "968 - 980", +year = "2006", +note = "", +issn = "1053-8119", +doi = "DOI: 10.1016/j.neuroimage.2006.01.021", +url = "http://www.sciencedirect.com/science/article/B6WNP-4JFHF4P-1/2/0ec667d4c17eafb0a7c52fa3fd5aef1c", +author = "Rahul S. Desikan and Florent SĂ©gonne and Bruce Fischl and Brian T. Quinn and Bradford C. Dickerson and Deborah Blacker and Randy L. Buckner and Anders M. Dale and R. Paul Maguire and Bradley T. Hyman and Marilyn S. Albert and Ronald J. Killiany" +} + +@article{Fischl01012004, + author = {Fischl, Bruce and van der Kouwe, AndrĂ© and Destrieux, Christophe and Halgren, Eric and SĂ©gonne, Florent and Salat, David H. and Busa, Evelina and Seidman, Larry J. and Goldstein, Jill and Kennedy, David and Caviness, Verne and Makris, Nikos and Rosen, Bruce and Dale, Anders M.}, + title = {{Automatically Parcellating the Human Cerebral Cortex}}, + volume = {14}, + number = {1}, + pages = {11-22}, + year = {2004}, + doi = {10.1093/cercor/bhg087}, + URL = {http://cercor.oxfordjournals.org/content/14/1/11.abstract}, + eprint = {http://cercor.oxfordjournals.org/content/14/1/11.full.pdf+html}, + journal = {Cerebral Cortex} +} + + +@article{Fischl2004S69, + title = "Sequence-independent segmentation of magnetic resonance images", + journal = "NeuroImage", + volume = "23", + number = "Supplement 1", + pages = "S69 - S84", + year = "2004", + note = "Mathematics in Brain Imaging", + issn = "1053-8119", + doi = "DOI: 10.1016/j.neuroimage.2004.07.016", + url = "http://www.sciencedirect.com/science/article/B6WNP-4DCMGVT-2/2/7eee26326dc63f931b826eac33becc8b", + author = "Bruce Fischl and David H. Salat and AndrĂ© J.W. van der Kouwe and Nikos Makris and Florent SĂ©gonne and Brian T. Quinn and Anders M. Dale", + keywords = "Morphometry", + keywords = "MRI", + keywords = "Alzheimer's disease" +} + + + + +@article{Fischl26092000, +author = {Fischl, Bruce and Dale, Anders M.}, +title = {{Measuring the thickness of the human cerebral cortex from magnetic resonance images}}, +volume = {97}, +number = {20}, +pages = {11050-11055}, +year = {2000}, +eprint = {http://www.pnas.org/content/97/20/11050.full.pdf+html}, +journal = {Proceedings of the National Academy of Sciences of the United States of America} +} + + + +@Article{FischlLiuDale, + author = {Fischl, B. and Liu, A. and Dale, A.~M.}, + title = {Automated manifold surgery: constructing geometrically accurate and topologically correct models of the human cerebral cortex}, + journal = {{IEEE} Medical Imaging}, + year = {2001}, + volume = {20}, + number = {1}, + pages = {70-80}, +} + + +@Article{FischlSalat2002, + author = {Fischl, B. and Salat, D.~H. and Busa, E. and Albert, M. and Dieterich, M. and Haselgrove, C. and {van der Kouwe}, A. and Killiany, R. and Kennedy, D. and Klaveness, S. and Montillo, A. and Makris, N. and Rosen, B. and Dale, A.~M.}, + title = {Whole brain segmentation: automated labeling of neuroanatomical structures in the human brain}, + journal = {Neuron}, + year = {2002}, + volume = {33}, + pages = {341-355} +} + + + +@article {HBM:HBM10, +author = {Fischl, Bruce and Sereno, Martin I. and Tootell, Roger B.H. and Dale, Anders M.}, +title = {High-resolution intersubject averaging and a coordinate system for the cortical surface}, +journal = {Human Brain Mapping}, +volume = {8}, +number = {4}, +publisher = {John Wiley & Sons, Inc.}, +issn = {1097-0193}, +url = {http://dx.doi.org/10.1002/(SICI)1097-0193(1999)8:4<272::AID-HBM10>3.0.CO;2-4}, +doi = {10.1002/(SICI)1097-0193(1999)8:4<272::AID-HBM10>3.0.CO;2-4}, +pages = {272--284}, +keywords = {intersubject averaging, coordinate systems, atlas}, +year = {1999}, +} + + + + +@article{Jovicich2006436, + title = "Reliability in multi-site structural MRI studies: Effects of gradient non-linearity correction on phantom and human data", + journal = "NeuroImage", + volume = "30", + number = "2", + pages = "436 - 443", + year = "2006", + note = "", + issn = "1053-8119", + doi = "DOI: 10.1016/j.neuroimage.2005.09.046", + url = "http://www.sciencedirect.com/science/article/B6WNP-4HM7S0B-2/2/4fa5ff26cad90ba3c9ed12b7e12ce3b6", + author = "Jorge Jovicich and Silvester Czanner and Douglas Greve and Elizabeth Haley and Andre van der Kouwe and Randy Gollub and David Kennedy and Franz Schmitt and Gregory Brown and James MacFall and Bruce Fischl and Anders Dale", + keywords = "Multi-site calibration", + keywords = "Gradient non-linearity distortions", + keywords = "Human structural MRI" +} + + +@Article{Kuperberg2003, + AUTHOR = "Kuperberg, G. R. and Broome, M. and McGuire, P. K. and David, A. S. and Eddy, M. and Ozawa, F. and Goff, D. and West, W. C. and Williams, S.C.R. and van der Kouwe, Andre and Salat, David and Dale, Anders and Fischl, Bruce", + TITLE = "Regionally localized thinning of the cerebral cortex in {S}chizophrenia", + YEAR = "2003", + JOURNAL = "Archives of General Psychiatry", + VOLUME = "60", + PAGES = "878-888" +} + +@article{Rosas12032002, +author = {Rosas, H. D. and Liu, A. K. and Hersch, S. and Glessner, M. and Ferrante, R. J. and Salat, D. H. and van der Kouwe, A. and Jenkins, B. G. and Dale, A. M. and Fischl, B.}, +title = {{Regional and progressive thinning of the cortical ribbon in Huntington’s disease}}, +volume = {58}, +number = {5}, +pages = {695-701}, +year = {2002}, +URL = {http://www.neurology.org/content/58/5/695.abstract}, +eprint = {http://www.neurology.org/content/58/5/695.full.pdf+html}, +journal = {Neurology} +} + + + + +@Article{Salat2004, + AUTHOR = "Salat, David and Buckner, R.L. and Snyder, A.Z. and Greve, Douglas N. and Desikan, R.S. and Busa, Evelina and Morris, J.C. and Dale, Anders and Fischl, Bruce", + TITLE = "Thinning of the cerebral cortex in aging", + YEAR = "2004", + JOURNAL = "Cerebral Cortex", + VOLUME = "14", + PAGES = "721-730" +} + +@article{Segonne20041060, +title = "A hybrid approach to the skull stripping problem in MRI", +journal = "NeuroImage", +volume = "22", +number = "3", +pages = "1060 - 1075", +year = "2004", +note = "", +issn = "1053-8119", +doi = "DOI: 10.1016/j.neuroimage.2004.03.032", +url = "http://www.sciencedirect.com/science/article/B6WNP-4CF5CNY-1/2/33cc73136f06f019b2c11023e7a95341", +author = "F. Segonne and A. M. Dale and E. Busa and M. Glessner and D. Salat and H. K. Hahn and B. Fischl", +keywords = "Skull stripping", +keywords = "Brain segmentation", +keywords = "Watershed transformation", +keywords = "Template deformation", +keywords = "Atlas-based segmentation" +} + + +@Article{dale:99, + AUTHOR = "Dale, Anders and Fischl, Bruce and Sereno, Martin I.", + TITLE = "Cortical Surface-Based Analysis: I. Segmentation and Surface Reconstruction", + YEAR = "1999", + JOURNAL = "NeuroImage", + VOLUME = "9", + NUMBER = "2", + PAGES = "179 - 194", + ANNOTE = "DOI: 10.1006/nimg.1998.0395" +} + +@Article{fischl:99, + AUTHOR = "Fischl, Bruce and Sereno, Martin I. and Dale, Anders", + TITLE = "Cortical Surface-Based Analysis: II: Inflation, Flattening, and a Surface-Based Coordinate System", + YEAR = "1999", + JOURNAL = "NeuroImage", + VOLUME = "9", + NUMBER = "2", + PAGES = "195 - 207", + ANNOTE = "DOI: 10.1006/nimg.1998.0396" +} + + + +@Article{han:06, + AUTHOR = "Han, Xiao and Jovicich, Jorge and Salat, David and van der Kouwe, Andre and Quinn, Brian and Czanner, Silvester and Busa, Evelina and Pacheco, Jenni and Albert, Marilyn and Killiany, Ronald and Maguire, Paul and Rosas, Diana and Makris, Nikos and Dale, Anders and Dickerson, Bradford and Fischl, Bruce", + TITLE = "Reliability of {MRI}-derived measurements of human cerebral cortical thickness: {T}he effects of field strength, scanner upgrade and manufacturer", + YEAR = "2006", + JOURNAL = "NeuroImage", + VOLUME = "32", + NUMBER = "1", + PAGES = "180-194", + KEYWORDS = {Longitudinal Analysis} +} + +@article{sledzijdenbos1998, + author = "{Sled}, J.G. and {Zijdenbos}, A.P. and {Evans}, A.C.", + year = "1998", + title = "A nonparametric method for automatic correction of intensity nonuniformity in MRI data", + journal = "IEEE Trans Med Imaging", + volume = "17", + pages = "87-97" +} + + +@article{spf2007, + author ="Segonne, F. and Pacheco, J. and Fischl, B.", + year = "2007", + title = "Geometrically accurate topology-correction of cortical surfaces using nonseparating loops", + journal = "IEEE Trans Med Imaging", + volume = "26", + pages = "518-529" +} + +@ARTICLE{reuter:robreg10, + AUTHOR="Martin Reuter and Herminia Diana Rosas and Bruce Fischl", + TITLE="Highly Accurate Inverse Consistent Registration: A Robust Approach", + JOURNAL="NeuroImage", + VOLUME="53", + NUMBER="4", + PAGES="1181--1196", + DOI="10.1016/j.neuroimage.2010.07.020", + URL="http://dx.doi.org/10.1016/j.neuroimage.2010.07.020", + YEAR="2010", +} + +@ARTICLE{reuter:bias11, + AUTHOR="Martin Reuter and Bruce Fischl", + TITLE="Avoiding Asymmetry-Induced Bias in Longitudinal Image Processing", + JOURNAL="NeuroImage", + VOLUME="57", + NUMBER= "1", + PAGES="19--21", + DOI="10.1016/j.neuroimage.2011.02.076", + URL="http://dx.doi.org/10.1016/j.neuroimage.2011.02.076", + YEAR="2011", +} + +@ARTICLE{reuter:long12, + AUTHOR="Martin Reuter and Nicholas J. Schmansky and Herminia Diana Rosas and Bruce Fischl", + TITLE="Within-Subject Template Estimation for Unbiased Longitudinal Image Analysis", + JOURNAL="NeuroImage", + VOLUME="61", + NUMBER="4", + PAGES="1402--1418", + DOI="10.1016/j.neuroimage.2012.02.084", + URL="http://dx.doi.org/10.1016/j.neuroimage.2012.02.084", + YEAR="2012", +} + + +@Article{pmid19059349, + Author="Woolrich, M. W. and Jbabdi, S. and Patenaude, B. and Chappell, M. and Makni, S. and Behrens, T. and Beckmann, C. and Jenkinson, M. and Smith, S. M. ", + Title="{{B}ayesian analysis of neuroimaging data in {F}{S}{L}}", + Journal="Neuroimage", + Year="2009", + Volume="45", + Number="1 Suppl", + Pages="S173--186", + Month="Mar", + Note={[DOI:\href{https://dx.doi.org/10.1016/j.neuroimage.2008.10.055}{10.1016/j.neuroimage.2008.10.055}] [PubMed:\href{https://www.ncbi.nlm.nih.gov/pubmed/19059349}{19059349}] } +} + +@Article{pmid15501092, + Author="Smith, S. M. and Jenkinson, M. and Woolrich, M. W. and Beckmann, C. F. and Behrens, T. E. and Johansen-Berg, H. and Bannister, P. R. and De Luca, M. and Drobnjak, I. and Flitney, D. E. and Niazy, R. K. and Saunders, J. and Vickers, J. and Zhang, Y. and De Stefano, N. and Brady, J. M. and Matthews, P. M. ", + Title="{{A}dvances in functional and structural {M}{R} image analysis and implementation as {F}{S}{L}}", + Journal="Neuroimage", + Year="2004", + Volume="23 Suppl 1", + Pages="S208--219", + Note={[DOI:\href{https://dx.doi.org/10.1016/j.neuroimage.2004.07.051}{10.1016/j.neuroimage.2004.07.051}] [PubMed:\href{https://www.ncbi.nlm.nih.gov/pubmed/15501092}{15501092}] } +} + +@Article{pmid21979382, + Author="Jenkinson, M. and Beckmann, C. F. and Behrens, T. E. and Woolrich, M. W. and Smith, S. M. ", + Title="{{F}{S}{L}}", + Journal="Neuroimage", + Year="2012", + Volume="62", + Number="2", + Pages="782--790", + Month="Aug", + Note={[DOI:\href{https://dx.doi.org/10.1016/j.neuroimage.2011.09.015}{10.1016/j.neuroimage.2011.09.015}] [PubMed:\href{https://www.ncbi.nlm.nih.gov/pubmed/21979382}{21979382}] } +} + +@misc{Notter2013, + author = {Michael Notter}, + title = {gif_your_nifti}, + year = {2018}, + publisher = {GitHub}, + journal = {GitHub repository}, + howpublished = {\url{https://github.com/miykael/gif_your_nifti}}, + commit = {66cc30f988c0aff96d3263385b83e209e48aa4cc} +} + + diff --git a/static/blog/christine-fmri-raw.mp4 b/static/blog/christine-fmri-raw.mp4 new file mode 100644 index 0000000..94d98be Binary files /dev/null and b/static/blog/christine-fmri-raw.mp4 differ diff --git a/static/blog/christine-fmri-reoriented.mp4 b/static/blog/christine-fmri-reoriented.mp4 new file mode 100644 index 0000000..63dae26 Binary files /dev/null and b/static/blog/christine-fmri-reoriented.mp4 differ