├── README.md ├── LICENSE ├── .gitignore ├── fmriprep_qc.tsv ├── 02 - Figure 4.ipynb ├── 02 - Figure SF4.ipynb ├── 02 - Figure 3.ipynb └── viz └── preproc.py /README.md: -------------------------------------------------------------------------------- 1 | # fmriprep-notebooks 2 | A repository for notebooks associated with FMRIPREP 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /fmriprep_qc.tsv: -------------------------------------------------------------------------------- 1 | dataset version overall t1_reconall t1_seg_brainmask t1_t1_2_mni bold_rois bold_bbr bold_syn_sdc 2 | DS000003 1.0.0 3.0 3.0 2.5 3.0 3.0 3.0 3.0 3 | DS000109 1.0.0 3.0 3.0 3.0 3.0 3.0 3.0 2.5 4 | DS000115 1.0.0 3.0 3.0 3.0 3.0 2.5 3.0 2.5 5 | DS000052 1.0.0 2.5 3.0 2.5 2.5 2.125 2.5 2.375 6 | DS000053 1.0.0 2.5 2.5 2.0 3.0 2.5 2.875 2.75 7 | DS000009 1.0.0 3.0 3.0 3.0 3.0 2.583333333333333 3.0 2.5 8 | DS000102 1.0.0 2.5 2.5 2.5 3.0 2.5 3.0 2.5 9 | DS000164 1.0.0 2.5 2.5 3.0 3.0 2.5 3.0 2.0 10 | DS000031 1.0.0 11 | DS000220 1.0.0 3.0 3.0 3.0 3.0 2.5 2.833333333333333 2.3333333333333335 12 | DS000007 1.0.0 2.0 3.0 2.5 3.0 1.8333333333333333 3.0 13 | DS000157 1.0.0 2.5 3.0 3.0 3.0 3.0 2.5 14 | DS000214 1.0.0 2.0 2.5 2.5 3.0 2.0 2.5 2.0 15 | DS000101 1.0.0 2.5 3.0 2.5 3.0 2.5 3.0 2.25 16 | DS000228 1.0.0 3.0 3.0 3.0 2.5 2.0 3.0 2.5 17 | DS000237 1.0.0 2.5 3.0 2.5 3.0 2.928571428571429 2.7 2.9 18 | DS000008 1.0.0 2.5 2.5 2.5 3.0 2.5 2.416666666666667 2.5 19 | DS000030 1.0.0 2.0 2.5 2.0 2.5 1.8125 2.75 2.4375 20 | DS000224 1.0.0 1.5 3.0 2.0 1.5 2.36875 2.1604166666666664 2.25 21 | DS000177 1.0.0 2.5 2.5 2.5 3.0 2.0 3.0 2.5 22 | DS000216 1.0.0 2.0 3.0 3.0 3.0 2.625 2.5 2.5 23 | DS000212 1.0.0 2.5 2.5 2.5 3.0 2.0 2.95 2.2 24 | DS000005 1.0.0 2.5 3.0 2.5 3.0 2.5 3.0 2.5 25 | DS000229 1.0.0 2.0 2.5 2.5 2.5 2.1666666666666665 2.0 2.833333333333333 26 | DS000213 1.0.0 2.5 2.5 2.5 3.0 2.5 3.0 2.5 27 | DS000205 1.0.0 3.0 3.0 3.0 2.5 3.0 3.0 2.833333333333333 28 | DS000002 1.0.0 2.0 2.5 2.5 2.5 2.0833333333333335 2.583333333333333 2.0 29 | DS000231 1.0.0 3.0 2.5 2.5 2.5 2.666666666666667 3.0 2.5 30 | DS000011 1.0.0 2.5 2.0 3.0 3.0 2.0 2.5 2.3333333333333335 31 | DS000168 1.0.0 3.0 3.0 3.0 2.5 3.0 3.0 2.5 32 | DS000001 1.0.0 3.0 3.0 3.0 3.0 3.0 3.0 3.0 33 | DS000219 1.0.0 2.5 2.5 2.0 3.0 2.875 2.875 2.333333333333333 34 | DS000208 1.0.0 3.0 2.0 2.0 2.0 2.0 3.0 3.0 35 | DS000116 1.0.0 1.5 1.0 2.5 3.0 2.583333333333333 3.0 3.0 36 | DS000158 1.0.0 2.5 2.5 3.0 3.0 3.0 3.0 2.0 37 | DS000121 1.0.0 2.5 3.0 3.0 3.0 2.25 3.0 2.5 38 | DS000119 1.0.0 2.5 2.5 2.0 3.0 2.375 3.0 2.0 39 | DS000107 1.0.0 3.0 2.5 3.0 2.5 2.5 2.75 3.0 40 | DS000243 1.0.0 2.5 2.0 3.0 3.0 2.5 3.0 2.5 41 | DS000140 1.0.0 2.5 3.0 3.0 3.0 2.4375 2.5 2.2777777777777777 42 | DS000233 1.0.0 2.5 3.0 2.5 3.0 2.4 3.0 2.45 43 | DS000110 1.0.0 2.5 3.0 3.0 3.0 2.3 2.9 2.5 44 | DS000051 1.0.0 2.5 3.0 3.0 2.5 2.5 2.875 2.5 45 | DS000171 1.0.0 1.5 3.0 3.0 3.0 2.5 2.6 46 | DS000221 1.0.0 1.5 1.5 0.0 0.0 2.3 2.15 2.375 47 | DS000170 1.0.0 2.0 3.0 3.0 3.0 2.0 2.0416666666666665 48 | DS000218 1.0.0 2.5 2.5 2.5 3.0 2.333333333333333 3.0 2.5 49 | DS000200 1.0.0 1.5 1.5 1.0 2.5 2.0 50 | DS000105 1.0.0 2.0 3.0 2.0 3.0 2.0 2.5 2.0265151515151514 51 | DS000133 1.0.0 2.5 2.5 2.5 3.0 2.4166666666666665 3.0 2.0 52 | DS000108 1.0.0 0.5 2.5 2.0 3.0 1.9166666666666665 2.0 53 | DS000120 1.0.0 2.5 2.5 2.0 2.5 2.5 2.833333333333333 2.3333333333333335 54 | DS000148 1.0.0 0.5 0.0 2.0 2.5 2.5 55 | DS000017 1.0.0 2.5 3.0 1.5 1.5 2.0 2.833333333333333 2.4444444444444446 56 | DS000114 1.0.0 2.0 3.0 3.0 3.0 2.2 2.8 2.15 57 | DS000003 1.0.7 3.0 3.0 3.0 3.0 3.0 3.0 3.0 58 | DS000109 1.0.7 3.0 3.0 3.0 3.0 3.0 3.0 3.0 59 | DS000115 1.0.7 3.0 3.0 3.0 3.0 3.0 3.0 2.9166666666666665 60 | DS000052 1.0.7 3.0 3.0 3.0 3.0 3.0 3.0 2.8125 61 | DS000053 1.0.7 3.0 3.0 3.0 3.0 3.0 3.0 2.75 62 | DS000009 1.0.7 3.0 3.0 3.0 3.0 3.0 3.0 2.7083333333333335 63 | DS000102 1.0.7 3.0 3.0 3.0 3.0 3.0 3.0 2.375 64 | DS000164 1.0.7 3.0 3.0 3.0 3.0 3.0 3.0 2.25 65 | DS000031 1.0.7 3.0 3.0 3.0 3.0 3.0 3.0 66 | DS000220 1.0.7 3.0 3.0 3.0 3.0 2.9166666666666665 2.9166666666666665 3.0 67 | DS000007 1.0.7 3.0 3.0 3.0 3.0 2.833333333333333 2.9583333333333335 2.5416666666666665 68 | DS000157 1.0.7 3.0 3.0 3.0 3.0 2.75 3.0 2.75 69 | DS000214 1.0.7 3.0 3.0 3.0 2.75 3.0 3.0 3.0 70 | DS000101 1.0.7 3.0 3.0 2.75 3.0 3.0 3.0 3.0 71 | DS000228 1.0.7 3.0 3.0 2.75 3.0 3.0 3.0 2.25 72 | DS000237 1.0.7 3.0 3.0 2.75 3.0 3.0 3.0 2.0142857142857142 73 | DS000008 1.0.7 3.0 3.0 2.75 3.0 2.9583333333333335 2.875 2.9083333333333337 74 | DS000030 1.0.7 3.0 3.0 1.5 2.25 2.8125 3.0 2.75 75 | DS000224 1.0.7 3.0 2.5 2.5 3.0 2.532061688311688 3.0 1.9844530996429732 76 | DS000177 1.0.7 2.75 3.0 3.0 3.0 3.0 2.5 2.5 77 | DS000216 1.0.7 2.75 3.0 3.0 3.0 2.8333333333333335 2.9375 2.25 78 | DS000212 1.0.7 2.75 3.0 3.0 3.0 2.825 2.8444444444444446 2.8499999999999996 79 | DS000005 1.0.7 2.75 3.0 2.75 3.0 3.0 2.875 3.0 80 | DS000229 1.0.7 2.75 3.0 2.75 3.0 3.0 2.8333333333333335 2.75 81 | DS000213 1.0.7 2.75 3.0 2.75 3.0 3.0 2.75 3.0 82 | DS000205 1.0.7 2.75 3.0 2.75 3.0 3.0 2.75 2.9166666666666665 83 | DS000002 1.0.7 2.75 3.0 2.75 3.0 2.875 2.875 2.75 84 | DS000231 1.0.7 2.75 3.0 2.75 2.25 2.75 2.5 3.0 85 | DS000011 1.0.7 2.75 3.0 2.5 3.0 2.8 2.9 2.666666666666667 86 | DS000168 1.0.7 2.75 3.0 2.25 3.0 3.0 3.0 3.0 87 | DS000001 1.0.7 2.75 2.75 3.0 3.0 2.8333333333333335 3.0 2.75 88 | DS000219 1.0.7 2.75 2.75 2.75 3.0 3.0 2.75 2.25 89 | DS000208 1.0.7 2.75 2.75 2.25 2.25 3.0 3.0 2.5 90 | DS000116 1.0.7 2.75 2.75 2.0 3.0 2.916666666666667 2.7916666666666665 2.4583333333333335 91 | DS000158 1.0.7 2.75 2.5 2.75 3.0 2.75 3.0 2.25 92 | DS000121 1.0.7 2.75 2.5 2.75 2.75 3.0 3.0 2.75 93 | DS000119 1.0.7 2.75 2.5 2.5 3.0 2.9375 2.9375 2.8125 94 | DS000107 1.0.7 2.75 2.25 3.0 3.0 3.0 3.0 3.0 95 | DS000243 1.0.7 2.5 3.0 3.0 3.0 3.0 2.5 2.25 96 | DS000140 1.0.7 2.5 3.0 3.0 3.0 2.9722222222222223 2.888888888888889 2.8055555555555554 97 | DS000233 1.0.7 2.5 3.0 3.0 3.0 2.775 2.7750000000000004 2.95 98 | DS000110 1.0.7 2.5 3.0 3.0 3.0 2.45 2.95 2.4499999999999997 99 | DS000051 1.0.7 2.5 3.0 2.5 3.0 2.5416666666666665 2.7083333333333335 2.75 100 | DS000171 1.0.7 2.5 2.75 3.0 3.0 2.9 2.7 2.8 101 | DS000221 1.0.7 2.5 2.75 2.25 3.0 2.9375 2.9375 2.95 102 | DS000170 1.0.7 2.5 2.5 3.0 3.0 2.9791666666666665 2.5833333333333335 2.604166666666667 103 | DS000218 1.0.7 2.5 2.25 2.75 3.0 3.0 2.75 2.8333333333333335 104 | DS000200 1.0.7 2.5 2.0 2.75 3.0 2.75 3.0 3.0 105 | DS000105 1.0.7 2.3333333333333335 3.0 2.6666666666666665 3.0 2.6363636363636362 2.881313131313131 2.25 106 | DS000133 1.0.7 2.25 3.0 2.75 3.0 3.0 2.333333333333333 1.875 107 | DS000108 1.0.7 2.25 3.0 2.25 3.0 2.05 2.875 2.5416666666666665 108 | DS000120 1.0.7 2.25 1.75 2.0 2.75 2.75 2.75 2.0833333333333335 109 | DS000148 1.0.7 1.75 3.0 3.0 3.0 3.0 3.0 2.9166666666666665 110 | DS000017 1.0.7 1.75 2.25 1.75 2.25 2.5777777777777775 111 | DS000114 1.0.7 1.25 2.75 0.75 0.75 3.0 2.925 2.575 112 | -------------------------------------------------------------------------------- /02 - Figure 4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stderr", 10 | "output_type": "stream", 11 | "text": [ 12 | "/home/oesteban/workspace/niworkflows/niworkflows/__init__.py:24: UserWarning: \n", 13 | "This call to matplotlib.use() has no effect because the backend has already\n", 14 | "been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,\n", 15 | "or matplotlib.backends is imported for the first time.\n", 16 | "\n", 17 | "The backend was *originally* set to 'pgf' by the following code:\n", 18 | " File \"/home/oesteban/.anaconda3/lib/python3.6/runpy.py\", line 193, in _run_module_as_main\n", 19 | " \"__main__\", mod_spec)\n", 20 | " File \"/home/oesteban/.anaconda3/lib/python3.6/runpy.py\", line 85, in _run_code\n", 21 | " exec(code, run_globals)\n", 22 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py\", line 16, in \n", 23 | " app.launch_new_instance()\n", 24 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/traitlets/config/application.py\", line 658, in launch_instance\n", 25 | " app.start()\n", 26 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/ipykernel/kernelapp.py\", line 477, in start\n", 27 | " ioloop.IOLoop.instance().start()\n", 28 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/zmq/eventloop/ioloop.py\", line 177, in start\n", 29 | " super(ZMQIOLoop, self).start()\n", 30 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/tornado/ioloop.py\", line 888, in start\n", 31 | " handler_func(fd_obj, events)\n", 32 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/tornado/stack_context.py\", line 277, in null_wrapper\n", 33 | " return fn(*args, **kwargs)\n", 34 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py\", line 440, in _handle_events\n", 35 | " self._handle_recv()\n", 36 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py\", line 472, in _handle_recv\n", 37 | " self._run_callback(callback, msg)\n", 38 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py\", line 414, in _run_callback\n", 39 | " callback(*args, **kwargs)\n", 40 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/tornado/stack_context.py\", line 277, in null_wrapper\n", 41 | " return fn(*args, **kwargs)\n", 42 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py\", line 283, in dispatcher\n", 43 | " return self.dispatch_shell(stream, msg)\n", 44 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py\", line 235, in dispatch_shell\n", 45 | " handler(stream, idents, msg)\n", 46 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py\", line 399, in execute_request\n", 47 | " user_expressions, allow_stdin)\n", 48 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/ipykernel/ipkernel.py\", line 196, in do_execute\n", 49 | " res = shell.run_cell(code, store_history=store_history, silent=silent)\n", 50 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/ipykernel/zmqshell.py\", line 533, in run_cell\n", 51 | " return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)\n", 52 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py\", line 2717, in run_cell\n", 53 | " interactivity=interactivity, compiler=compiler, result=result)\n", 54 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py\", line 2821, in run_ast_nodes\n", 55 | " if self.run_code(code, result):\n", 56 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py\", line 2881, in run_code\n", 57 | " exec(code_obj, self.user_global_ns, self.user_ns)\n", 58 | " File \"\", line 14, in \n", 59 | " from matplotlib import pyplot as plt\n", 60 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py\", line 71, in \n", 61 | " from matplotlib.backends import pylab_setup\n", 62 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/matplotlib/backends/__init__.py\", line 16, in \n", 63 | " line for line in traceback.format_stack()\n", 64 | "\n", 65 | "\n", 66 | " matplotlib.use('Agg')\n", 67 | "/home/oesteban/.anaconda3/lib/python3.6/importlib/_bootstrap.py:219: ImportWarning: can't resolve package from __spec__ or __package__, falling back on __name__ and __path__\n", 68 | " return f(*args, **kwds)\n" 69 | ] 70 | } 71 | ], 72 | "source": [ 73 | "%load_ext autoreload\n", 74 | "%autoreload 2\n", 75 | "# %matplotlib inline\n", 76 | "import os\n", 77 | "from pathlib import Path\n", 78 | "import warnings\n", 79 | "\n", 80 | "import numpy as np\n", 81 | "import nibabel as nb\n", 82 | "import pandas as pd\n", 83 | "\n", 84 | "import matplotlib as mpl\n", 85 | "mpl.use('pgf')\n", 86 | "from matplotlib import pyplot as plt\n", 87 | "from matplotlib import gridspec, colors\n", 88 | "import seaborn as sn\n", 89 | "import palettable\n", 90 | "\n", 91 | "from niworkflows.data import get_template\n", 92 | "\n", 93 | "from nilearn.image import concat_imgs, mean_img\n", 94 | "from nilearn import plotting\n", 95 | "\n", 96 | "warnings.simplefilter('ignore')\n", 97 | "\n", 98 | "DATA_HOME = Path(os.getenv('FMRIPREP_DATA_HOME', os.getcwd())).resolve()\n", 99 | "DS030_HOME = DATA_HOME / 'ds000030' / '1.0.3'\n", 100 | "DERIVS_HOME = DS030_HOME / 'derivatives'\n", 101 | "ATLAS_HOME = get_template('MNI152NLin2009cAsym')\n", 102 | "ANALYSIS_HOME = DERIVS_HOME / 'fmriprep_vs_feat_2.0-oe'\n", 103 | "\n", 104 | "fprep_home = DERIVS_HOME / 'fmriprep_1.0.8' / 'fmriprep'\n", 105 | "feat_home = DERIVS_HOME / 'fslfeat_5.0.10' / 'featbids'\n", 106 | "\n", 107 | "out_folder = Path(os.getenv('FMRIPREP_OUTPUTS') or '').resolve()\n", 108 | "\n", 109 | "# Load MNI152 nonlinear, asymmetric 2009c atlas\n", 110 | "atlas = nb.load(str(ATLAS_HOME / 'tpl-MNI152NLin2009cAsym_space-MNI_res-01_T1w.nii.gz'))\n", 111 | "mask1mm = nb.load(str(ATLAS_HOME / 'tpl-MNI152NLin2009cAsym_space-MNI_res-01_brainmask.nii.gz')).get_data() > 0\n", 112 | "mask2mm = nb.load(str(ATLAS_HOME / 'tpl-MNI152NLin2009cAsym_space-MNI_res-02_brainmask.nii.gz')).get_data() > 0\n", 113 | "data = atlas.get_data()\n", 114 | "data[~mask1mm] = data[~mask1mm].max()\n", 115 | "atlas = nb.Nifti1Image(data, atlas.affine, atlas.header)" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 2, 121 | "metadata": { 122 | "collapsed": true 123 | }, 124 | "outputs": [], 125 | "source": [ 126 | "# sn.set_style(\"whitegrid\", {\n", 127 | "# 'ytick.major.size': 5,\n", 128 | "# 'xtick.major.size': 5,\n", 129 | "# })\n", 130 | "# sn.set_context(\"notebook\", font_scale=1.5)\n", 131 | "\n", 132 | "# pgf_with_custom_preamble = {\n", 133 | "# 'ytick.major.size': 0,\n", 134 | "# 'xtick.major.size': 0,\n", 135 | "# 'font.size': 30,\n", 136 | "# 'font.sans-serif': ['HelveticaLTStd-Light'],\n", 137 | "# 'font.family': 'sans-serif', # use serif/main font for text elements\n", 138 | "# 'text.usetex': False, # use inline math for ticks\n", 139 | "# }\n", 140 | "# mpl.rcParams.update(pgf_with_custom_preamble)\n", 141 | "\n", 142 | "\n", 143 | "pgf_with_custom_preamble = {\n", 144 | " 'text.usetex': True, # use inline math for ticks\n", 145 | " 'pgf.rcfonts': False, # don't setup fonts from rc parameters\n", 146 | " 'pgf.texsystem': 'xelatex',\n", 147 | " 'verbose.level': 'debug-annoying',\n", 148 | " \"pgf.preamble\": [\n", 149 | " r\"\"\"\\usepackage{fontspec}\n", 150 | "\\setsansfont{HelveticaLTStd-Light}[\n", 151 | "Extension=.otf,\n", 152 | "BoldFont=HelveticaLTStd-Bold,\n", 153 | "ItalicFont=HelveticaLTStd-LightObl,\n", 154 | "BoldItalicFont=HelveticaLTStd-BoldObl,\n", 155 | "]\n", 156 | "\\setmainfont{HelveticaLTStd-Light}[\n", 157 | "Extension=.otf,\n", 158 | "BoldFont=HelveticaLTStd-Bold,\n", 159 | "ItalicFont=HelveticaLTStd-LightObl,\n", 160 | "BoldItalicFont=HelveticaLTStd-BoldObl,\n", 161 | "]\n", 162 | "\\setmonofont{Inconsolata-dz}\n", 163 | "\"\"\",\n", 164 | " r'\\renewcommand\\familydefault{\\sfdefault}',\n", 165 | "# r'\\setsansfont[Extension=.otf]{Helvetica-LightOblique}',\n", 166 | "# r'\\setmainfont[Extension=.ttf]{DejaVuSansCondensed}',\n", 167 | "# r'\\setmainfont[Extension=.otf]{FiraSans-Light}',\n", 168 | "# r'\\setsansfont[Extension=.otf]{FiraSans-Light}',\n", 169 | " ]\n", 170 | "}\n", 171 | "mpl.rcParams.update(pgf_with_custom_preamble)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 8, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [ 180 | "res_shape = np.array(mask1mm.shape[:3])\n", 181 | "bbox = np.argwhere(mask1mm)\n", 182 | "new_origin = np.clip(bbox.min(0) - 5, a_min=0, a_max=None)\n", 183 | "new_end = np.clip(bbox.max(0) + 5, a_min=0,\n", 184 | " a_max=res_shape - 1)\n", 185 | "\n", 186 | "# Find new origin, and set into new affine\n", 187 | "new_affine_4 = atlas.affine.copy()\n", 188 | "new_affine_4[:3, 3] = new_affine_4[:3, :3].dot(\n", 189 | " new_origin) + new_affine_4[:3, 3]\n", 190 | "\n", 191 | "cropped_atlas = atlas.__class__(\n", 192 | " atlas.get_data()[new_origin[0]:new_end[0], new_origin[1]:new_end[1], new_origin[2]:new_end[2]],\n", 193 | " new_affine_4,\n", 194 | " atlas.header\n", 195 | ")" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 11, 201 | "metadata": {}, 202 | "outputs": [ 203 | { 204 | "ename": "TypeError", 205 | "evalue": "draw_scale_bar() got multiple values for keyword argument 'bg_color'", 206 | "output_type": "error", 207 | "traceback": [ 208 | "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", 209 | "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", 210 | "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 19\u001b[0m disp.annotate(size=24, left_right=False, positions=False, scalebar=True,\n\u001b[1;32m 20\u001b[0m \u001b[0mloc\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0msize_vertical\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlabel_top\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mframeon\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mborderpad\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 21\u001b[0;31m bg_color='None')\n\u001b[0m\u001b[1;32m 22\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", 211 | "\u001b[0;32m/home/oesteban/workspace/nilearn/nilearn/plotting/displays.py\u001b[0m in \u001b[0;36mannotate\u001b[0;34m(self, left_right, positions, scalebar, size, **kwargs)\u001b[0m\n\u001b[1;32m 995\u001b[0m display_axis.draw_scale_bar(bg_color=bg_color,\n\u001b[1;32m 996\u001b[0m \u001b[0mfontsize\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0msize\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 997\u001b[0;31m **kwargs)\n\u001b[0m\u001b[1;32m 998\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 999\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mclose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", 212 | "\u001b[0;31mTypeError\u001b[0m: draw_scale_bar() got multiple values for keyword argument 'bg_color'" 213 | ] 214 | } 215 | ], 216 | "source": [ 217 | "plt.clf()\n", 218 | "fig = plt.gcf()\n", 219 | "_ = fig.set_size_inches(12, 10)\n", 220 | "gs = gridspec.GridSpec(4, 3, height_ratios=[15, 15, 4, 0.5], hspace=0.0, wspace=0.)\n", 221 | "ax1 = plt.subplot(gs[0, :])\n", 222 | "ax2 = plt.subplot(gs[1, :])\n", 223 | "\n", 224 | "cut_coords = [0, 15, 30]\n", 225 | "disp = plotting.plot_stat_map(str(ANALYSIS_HOME / 'acm_fpre.nii.gz'),\n", 226 | " bg_img=cropped_atlas, threshold=0.25, display_mode='z',\n", 227 | " cut_coords=cut_coords, vmax=0.8, alpha=0.8,\n", 228 | " axes=ax1, colorbar=False, annotate=False)\n", 229 | "disp.annotate(size=20, left_right=True, positions=True)\n", 230 | "\n", 231 | "disp = plotting.plot_stat_map(str(ANALYSIS_HOME / 'acm_feat.nii.gz'),\n", 232 | " bg_img=cropped_atlas, threshold=0.25, display_mode='z',\n", 233 | " cut_coords=cut_coords, vmax=0.8, alpha=0.8,\n", 234 | " axes=ax2, colorbar=False, annotate=False)\n", 235 | "disp.annotate(size=24, left_right=False, positions=False, scalebar=True,\n", 236 | " loc=3, size_vertical=2, label_top=False, frameon=True, borderpad=0.1,\n", 237 | " bg_color='None')\n", 238 | "\n", 239 | "\n", 240 | "ax1.annotate(\n", 241 | " 'fMRIPrep',\n", 242 | " xy=(0., .5), xycoords='axes fraction', xytext=(-40, .0),\n", 243 | " textcoords='offset points', va='center', color='k', size=24,\n", 244 | " rotation=90);\n", 245 | "\n", 246 | "ax2.annotate(\n", 247 | " r'\\texttt{feat}',\n", 248 | " xy=(0., .5), xycoords='axes fraction', xytext=(-40, .0),\n", 249 | " textcoords='offset points', va='center', color='k', size=24,\n", 250 | " rotation=90);\n", 251 | "\n", 252 | "ax3 = fig.add_subplot(gs[3, 2])\n", 253 | "\n", 254 | "cmap = plotting.cm.cold_hot\n", 255 | "gradient = np.linspace(-0.8, 0.8, cmap.N)\n", 256 | "\n", 257 | "# istart = int(norm(-offset, clip=True) * (our_cmap.N - 1))\n", 258 | "# istop = int(norm(offset, clip=True) * (our_cmap.N - 1))\n", 259 | "GRAY = (0.85, 0.85, 0.85, 1.)\n", 260 | "cmaplist = []\n", 261 | "for i in range(cmap.N):\n", 262 | " cmaplist += [cmap(i)] if not -0.25 < gradient[i] < 0.25 else [GRAY] # just an average gray color\n", 263 | "\n", 264 | "cmap = colors.LinearSegmentedColormap.from_list('Custom cmap', cmaplist, cmap.N)\n", 265 | "\n", 266 | "th_index = cmaplist.index(GRAY)\n", 267 | "\n", 268 | "gradient = np.vstack((gradient, gradient))\n", 269 | "ax3.imshow(gradient, aspect='auto', cmap=cmap)\n", 270 | "ax3.set_title(r'\\noindent\\parbox{7.5cm}{\\centering\\textbf{Fraction of participants} \\\\ with significant response}',\n", 271 | " size=18, position=(0.5, 3.0))\n", 272 | "ax3.xaxis.set_ticklabels(['80\\%', '25\\%', '25\\%', '80\\%'], size=20)\n", 273 | "ax3.xaxis.set_ticks([0, th_index, cmap.N - th_index - 1, cmap.N])\n", 274 | "ax3.yaxis.set_ticklabels([])\n", 275 | "ax3.yaxis.set_ticks([])\n", 276 | "for pos in ['top', 'bottom', 'left', 'right']:\n", 277 | "# ax3.spines[pos].set_visible(False)\n", 278 | " ax3.spines[pos].set_color(GRAY)\n", 279 | "\n", 280 | "# ax3.annotate(\n", 281 | "# r'\\noindent\\parbox{15cm}{'\n", 282 | "# r'Cold hues represent negative activation (response inhibition). '\n", 283 | "# r'Warm hues represent positive activation. '\n", 284 | "# r'Activation count maps are derived from N=257 biologically independent participants.}',\n", 285 | "# xy=(-2.05, 0.07), xycoords='axes fraction', xytext=(.0, 10.0),\n", 286 | "# textcoords='offset points', va='center', color='k', size=14,\n", 287 | "# )\n", 288 | "\n", 289 | "\n", 290 | "plt.savefig(str(out_folder / 'figure04.pdf'),\n", 291 | " format='pdf', bbox_inches='tight', pad_inches=0.2, dpi=300)" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": null, 297 | "metadata": { 298 | "collapsed": true 299 | }, 300 | "outputs": [], 301 | "source": [] 302 | } 303 | ], 304 | "metadata": { 305 | "kernelspec": { 306 | "display_name": "Python 3", 307 | "language": "python", 308 | "name": "python3" 309 | }, 310 | "language_info": { 311 | "codemirror_mode": { 312 | "name": "ipython", 313 | "version": 3 314 | }, 315 | "file_extension": ".py", 316 | "mimetype": "text/x-python", 317 | "name": "python", 318 | "nbconvert_exporter": "python", 319 | "pygments_lexer": "ipython3", 320 | "version": "3.6.4" 321 | } 322 | }, 323 | "nbformat": 4, 324 | "nbformat_minor": 2 325 | } 326 | -------------------------------------------------------------------------------- /02 - Figure SF4.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "%load_ext autoreload\n", 12 | "%autoreload 2\n", 13 | "# %matplotlib inline\n", 14 | "import os\n", 15 | "from pathlib import Path\n", 16 | "import warnings\n", 17 | "\n", 18 | "import numpy as np\n", 19 | "import nibabel as nb\n", 20 | "import pandas as pd\n", 21 | "\n", 22 | "import matplotlib as mpl\n", 23 | "mpl.use('pgf')\n", 24 | "from matplotlib import pyplot as plt\n", 25 | "from matplotlib import gridspec\n", 26 | "import seaborn as sn\n", 27 | "import palettable\n", 28 | "\n", 29 | "from nilearn.image import concat_imgs, mean_img\n", 30 | "from nilearn import plotting\n", 31 | "\n", 32 | "warnings.simplefilter('ignore')\n", 33 | "\n", 34 | "DATA_HOME = Path(os.getenv('FMRIPREP_DATA_HOME', os.getcwd())).resolve()\n", 35 | "DS030_HOME = DATA_HOME / 'ds000030' / '1.0.3'\n", 36 | "DERIVS_HOME = DS030_HOME / 'derivatives'\n", 37 | "ATLAS_HOME = DATA_HOME / 'templates' / 'mni_icbm152_nlin_asym_09c'\n", 38 | "ANALYSIS_HOME = DERIVS_HOME / 'fmriprep_vs_feat_2.0-oe'\n", 39 | "\n", 40 | "fprep_home = DERIVS_HOME / 'fmriprep_1.0.8' / 'fmriprep'\n", 41 | "feat_home = DERIVS_HOME / 'fslfeat_5.0.10' / 'featbids'\n", 42 | "\n", 43 | "out_folder = Path(os.getenv('FMRIPREP_OUTPUTS') or '').resolve()\n", 44 | "\n", 45 | "# Load MNI152 nonlinear, asymmetric 2009c atlas\n", 46 | "atlas = nb.load(str(ATLAS_HOME / '1mm_T1.nii.gz'))\n", 47 | "mask1mm = nb.load(str(ATLAS_HOME / '1mm_brainmask.nii.gz')).get_data() > 0\n", 48 | "mask2mm = nb.load(str(ATLAS_HOME / '2mm_brainmask.nii.gz')).get_data() > 0\n", 49 | "data = atlas.get_data()\n", 50 | "data[~mask1mm] = data[~mask1mm].max()\n", 51 | "atlas = nb.Nifti1Image(data, atlas.affine, atlas.header)" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 2, 57 | "metadata": { 58 | "collapsed": true 59 | }, 60 | "outputs": [], 61 | "source": [ 62 | "pgf_with_custom_preamble = {\n", 63 | " 'text.usetex': True, # use inline math for ticks\n", 64 | " 'pgf.rcfonts': False, # don't setup fonts from rc parameters\n", 65 | " 'pgf.texsystem': 'xelatex',\n", 66 | " 'verbose.level': 'debug-annoying',\n", 67 | " \"pgf.preamble\": [\n", 68 | " r\"\"\"\\usepackage{fontspec}\n", 69 | "\\setsansfont{HelveticaLTStd-Light}[\n", 70 | "Extension=.otf,\n", 71 | "BoldFont=HelveticaLTStd-Bold,\n", 72 | "ItalicFont=HelveticaLTStd-LightObl,\n", 73 | "BoldItalicFont=HelveticaLTStd-BoldObl,\n", 74 | "]\n", 75 | "\\setmainfont{HelveticaLTStd-Light}[\n", 76 | "Extension=.otf,\n", 77 | "BoldFont=HelveticaLTStd-Bold,\n", 78 | "ItalicFont=HelveticaLTStd-LightObl,\n", 79 | "BoldItalicFont=HelveticaLTStd-BoldObl,\n", 80 | "]\n", 81 | "\\setmonofont{Inconsolata-dz}\n", 82 | "\"\"\",\n", 83 | " r'\\renewcommand\\familydefault{\\sfdefault}',\n", 84 | "# r'\\setsansfont[Extension=.otf]{Helvetica-LightOblique}',\n", 85 | "# r'\\setmainfont[Extension=.ttf]{DejaVuSansCondensed}',\n", 86 | "# r'\\setmainfont[Extension=.otf]{FiraSans-Light}',\n", 87 | "# r'\\setsansfont[Extension=.otf]{FiraSans-Light}',\n", 88 | " ]\n", 89 | "}\n", 90 | "mpl.rcParams.update(pgf_with_custom_preamble)" 91 | ] 92 | }, 93 | { 94 | "cell_type": "code", 95 | "execution_count": 3, 96 | "metadata": { 97 | "collapsed": true 98 | }, 99 | "outputs": [], 100 | "source": [ 101 | "def mean_std_map(pipe_home, meanmask, force=False, lazy=False, maskval=1000):\n", 102 | " pipe_std = pipe_home / 'summary_stdev.nii.gz'\n", 103 | " pipe_mean = pipe_home / 'summary_means.nii.gz'\n", 104 | "\n", 105 | " if force or not pipe_mean.is_file():\n", 106 | " all_mus = []\n", 107 | " if lazy:\n", 108 | " all_mus = [nb.load(str(f)) for f in pipe_home.glob(\n", 109 | " 'sub-*/func/sub-*_task-stopsignal_bold_space-MNI152NLin2009cAsym_avgpreproc.nii.gz')]\n", 110 | " \n", 111 | " if not all_mus:\n", 112 | " print('Generating means file')\n", 113 | " pipe_files = list(pipe_home.glob(\n", 114 | " 'sub-*/func/sub-*_task-stopsignal_bold_space-MNI152NLin2009cAsym_preproc.nii.gz'))\n", 115 | " all_mus = []\n", 116 | " for f in pipe_files:\n", 117 | " mean = mean_img(str(f))\n", 118 | " data = mean.get_data()\n", 119 | " sigma = np.percentile(data[meanmask], 50) / maskval\n", 120 | " data /= sigma\n", 121 | " all_mus.append(nb.Nifti1Image(data, mean.affine, mean.header))\n", 122 | " \n", 123 | " meannii = concat_imgs(all_mus, auto_resample=False)\n", 124 | " meannii.to_filename(str(pipe_mean))\n", 125 | " force = True\n", 126 | "\n", 127 | " if force or not pipe_std.is_file():\n", 128 | " print('Generating standard deviation map')\n", 129 | " meannii = nb.load(str(pipe_mean))\n", 130 | " nb.Nifti1Image(meannii.get_data().std(3), meannii.affine, meannii.header).to_filename(str(pipe_std))\n", 131 | " \n", 132 | " return pipe_mean, pipe_std\n", 133 | "\n", 134 | "# Use the WM mask to normalize intensities of EPI means\n", 135 | "meanmask = nb.load(str(ATLAS_HOME / '2mm_tpm_wm.nii.gz')).get_data() > 0.9" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 4, 141 | "metadata": { 142 | "collapsed": true 143 | }, 144 | "outputs": [], 145 | "source": [ 146 | "# Calculate average and std\n", 147 | "fprep_mean, fprep_std = mean_std_map(fprep_home, meanmask)\n", 148 | "feat_mean, feat_std = mean_std_map(feat_home, meanmask)\n", 149 | "\n", 150 | "# Trick to avoid nilearn zooming in\n", 151 | "feat_nii = nb.load(str(feat_std))\n", 152 | "\n", 153 | "fd = feat_nii.get_data()\n", 154 | "fd[0, 0, :] = 50\n", 155 | "fd[0, -1, :] = 50\n", 156 | "fd[-1, 0, :] = 50\n", 157 | "fd[-1, -1, :] = 50\n", 158 | "\n", 159 | "nb.Nifti1Image(fd, feat_nii.affine, feat_nii.header).to_filename('newfeat.nii.gz')" 160 | ] 161 | }, 162 | { 163 | "cell_type": "code", 164 | "execution_count": 5, 165 | "metadata": {}, 166 | "outputs": [ 167 | { 168 | "data": { 169 | "text/plain": [ 170 | "(0.0, 0.009324209800697824, -13.084191226959231, 571.25822343826292)" 171 | ] 172 | }, 173 | "execution_count": 5, 174 | "metadata": {}, 175 | "output_type": "execute_result" 176 | } 177 | ], 178 | "source": [ 179 | "plt.clf()\n", 180 | "fig = plt.gcf()\n", 181 | "_ = fig.set_size_inches(15, 7)\n", 182 | "gs = gridspec.GridSpec(2, 2, width_ratios=[10, 1], height_ratios=[1, 1], hspace=0.0, wspace=0.03)\n", 183 | "\n", 184 | "b_ax1 = plt.subplot(gs[0, 0])\n", 185 | "b_ax2 = plt.subplot(gs[1, 0])\n", 186 | "\n", 187 | "thres = 20\n", 188 | "vmin = 50\n", 189 | "vmax = 200\n", 190 | "\n", 191 | "disp = plotting.plot_anat(str(fprep_std), display_mode='z', annotate=False,\n", 192 | " cut_coords=[-15, -5, 10, 20, 40], cmap='cividis', threshold=thres, vmin=vmin, vmax=vmax,\n", 193 | " axes=b_ax1, resampling_interpolation='nearest')\n", 194 | "disp.annotate(size=12, left_right=True, positions=True)\n", 195 | "disp.add_contours(str(ATLAS_HOME / '1mm_tpm_csf.nii.gz'), colors=['k'], levels=[0.8])\n", 196 | "disp.add_contours(str(ATLAS_HOME / '1mm_tpm_wm.nii.gz'), colors=['w'], levels=[0.8], linewidths=[1], alpha=0.7)\n", 197 | "disp.add_contours(str(ATLAS_HOME / '1mm_brainmask.nii.gz'), colors=['k'], levels=[0.8], linewidths=[3], alpha=.7)\n", 198 | "\n", 199 | "\n", 200 | "disp = plotting.plot_anat('newfeat.nii.gz', display_mode='z', annotate=False,\n", 201 | " cut_coords=[-15, -5, 10, 20, 40], cmap='cividis', threshold=thres, vmin=vmin, vmax=vmax,\n", 202 | " axes=b_ax2, resampling_interpolation='nearest')\n", 203 | "disp.annotate(size=12, left_right=False, positions=False)\n", 204 | "disp.annotate(size=12, left_right=False, positions=False, scalebar=True,\n", 205 | " loc=3, size_vertical=2, label_top=False, frameon=True, borderpad=0.1)\n", 206 | "\n", 207 | "disp.add_contours(str(ATLAS_HOME / '1mm_tpm_csf.nii.gz'), colors=['k'], levels=[0.8])\n", 208 | "disp.add_contours(str(ATLAS_HOME / '1mm_tpm_wm.nii.gz'), colors=['w'], levels=[0.8], linewidths=[1], alpha=0.7)\n", 209 | "disp.add_contours(str(ATLAS_HOME / '1mm_brainmask.nii.gz'), colors=['k'], levels=[0.8], linewidths=[3], alpha=.7)\n", 210 | "\n", 211 | "b_ax1.annotate(\n", 212 | " 'fMRIPrep',\n", 213 | " xy=(0., .5), xycoords='axes fraction', xytext=(-20, .0),\n", 214 | " textcoords='offset points', va='center', color='k', size=15,\n", 215 | " rotation=90)\n", 216 | "\n", 217 | "b_ax2.annotate(\n", 218 | " r'\\texttt{feat}',\n", 219 | " xy=(0., .5), xycoords='axes fraction', xytext=(-20, .0),\n", 220 | " textcoords='offset points', va='center', color='k', size=12,\n", 221 | " rotation=90)\n", 222 | "\n", 223 | "\n", 224 | "inner_grid = gridspec.GridSpecFromSubplotSpec(1, 2, width_ratios=[1.5, 15],\n", 225 | " subplot_spec=gs[:, -1], wspace=0.01)\n", 226 | "\n", 227 | "b_ax3 = fig.add_subplot(inner_grid[0])\n", 228 | "gradient = np.hstack((np.zeros((50,)), np.linspace(0, 1, 120), np.ones((130,))))[::-1]\n", 229 | "gradient = np.vstack((gradient, gradient))\n", 230 | "b_ax3.imshow(gradient.T, aspect='auto', cmap=plt.get_cmap('cividis'))\n", 231 | "b_ax3.xaxis.set_ticklabels([])\n", 232 | "b_ax3.xaxis.set_ticks([])\n", 233 | "b_ax3.yaxis.set_ticklabels([])\n", 234 | "b_ax3.yaxis.set_ticks([])\n", 235 | "\n", 236 | "b_ax4 = fig.add_subplot(inner_grid[1])\n", 237 | "sn.distplot(nb.load(str(fprep_std)).get_data()[mask2mm], label='fMRIPrep',\n", 238 | " vertical=True, ax=b_ax4, kde=False, norm_hist=True)\n", 239 | "sn.distplot(nb.load(str(feat_std)).get_data()[mask2mm], label=r'\\texttt{feat}', vertical=True,\n", 240 | " color='darkorange', ax=b_ax4, kde=False, norm_hist=True)\n", 241 | "\n", 242 | "# plt.gca().set_ylim((0, 300))\n", 243 | "plt.legend(prop={'size': 15}, edgecolor='none')\n", 244 | "\n", 245 | "b_ax4.xaxis.set_ticklabels([])\n", 246 | "b_ax4.xaxis.set_ticks([])\n", 247 | "b_ax4.yaxis.set_ticklabels([])\n", 248 | "b_ax4.yaxis.set_ticks([])\n", 249 | "\n", 250 | "plt.axis('off')\n", 251 | "b_ax3.axis('off')\n", 252 | "b_ax4.axis('off')" 253 | ] 254 | }, 255 | { 256 | "cell_type": "code", 257 | "execution_count": 18, 258 | "metadata": { 259 | "collapsed": true 260 | }, 261 | "outputs": [], 262 | "source": [ 263 | "feat_closeup = nb.load(str(feat_std))\n", 264 | "feat_data = feat_closeup.get_data()[15:82, 72:106, :]\n", 265 | "nb.Nifti1Image(feat_data, feat_closeup.affine, feat_closeup.header).to_filename('feat_clip.nii.gz')\n", 266 | "\n", 267 | "fprep_closeup = nb.load(str(fprep_std))\n", 268 | "fprep_data = fprep_closeup.get_data()[15:82, 72:106, :]\n", 269 | "nb.Nifti1Image(fprep_data, fprep_closeup.affine, fprep_closeup.header).to_filename('fprep_clip.nii.gz')\n", 270 | "\n", 271 | "\n", 272 | "msknii = nb.load(str(ATLAS_HOME / '1mm_tpm_csf.nii.gz'))\n", 273 | "fullbox = np.zeros(msknii.shape)\n", 274 | "fullbox[30:164, 142:213, 70:76] = 1\n", 275 | "fullbox[30:164, 142:213, 88:90] = 2\n", 276 | "fullbox[30:164, 142:213, 96:100] = 3\n", 277 | "nb.Nifti1Image(fullbox, msknii.affine, msknii.header).to_filename('box.nii.gz')\n", 278 | "\n", 279 | "newcsf = msknii.get_data()\n", 280 | "newcsf[:, :, 100:] = 0\n", 281 | "nb.Nifti1Image(newcsf, msknii.affine, msknii.header).to_filename('newcsfmsk.nii.gz')\n", 282 | "\n", 283 | "mskdat = msknii.get_data()[30:164, 142:213, :]\n", 284 | "nb.Nifti1Image(mskdat, msknii.affine, msknii.header).to_filename('csf_clip.nii.gz')\n", 285 | "msknii = nb.load(str(ATLAS_HOME / '1mm_tpm_wm.nii.gz'))\n", 286 | "mskdat = msknii.get_data()[30:164, 142:213, :]\n", 287 | "nb.Nifti1Image(mskdat, msknii.affine, msknii.header).to_filename('wm_clip.nii.gz')\n", 288 | "msknii = nb.load(str(ATLAS_HOME / '1mm_brainmask.nii.gz'))\n", 289 | "mskdat = msknii.get_data()[30:164, 142:213, :]\n", 290 | "nb.Nifti1Image(mskdat, msknii.affine, msknii.header).to_filename('bm_clip.nii.gz')\n", 291 | "\n", 292 | "mskbox = np.zeros_like(mskdat)\n", 293 | "mskbox[1:-1, 1:-1, :] = 1\n", 294 | "nb.Nifti1Image(mskbox, msknii.affine, msknii.header).to_filename('box_clip.nii.gz')" 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": 19, 300 | "metadata": { 301 | "collapsed": true 302 | }, 303 | "outputs": [], 304 | "source": [ 305 | "colorboxes = [\n", 306 | " (244/255, 3/255, 23/255),\n", 307 | " (73/255, 213/255, 253/255),\n", 308 | " (245/255, 29/255, 245/255)\n", 309 | "]\n", 310 | "fs = 25\n", 311 | "\n", 312 | "plt.clf()\n", 313 | "fig = plt.gcf()\n", 314 | "_ = fig.set_size_inches(15, 19.5)\n", 315 | "gs = gridspec.GridSpec(4, 2, width_ratios=[10, 1], height_ratios=[1, 1, 0.4, 3.7], hspace=0., wspace=0.03)\n", 316 | "\n", 317 | "b_ax1 = plt.subplot(gs[0, 0])\n", 318 | "b_ax2 = plt.subplot(gs[1, 0])\n", 319 | "\n", 320 | "thres = 20\n", 321 | "vmin = 50\n", 322 | "vmax = 200\n", 323 | "\n", 324 | "disp = plotting.plot_anat(str(fprep_std), display_mode='z', annotate=False,\n", 325 | " cut_coords=[-15, -5, 10, 20, 40], cmap='cividis', threshold=thres, vmin=vmin, vmax=vmax,\n", 326 | " axes=b_ax1, resampling_interpolation='nearest')\n", 327 | "disp.annotate(size=15, left_right=True, positions=True)\n", 328 | "disp.add_contours('newcsfmsk.nii.gz', colors=['k'], levels=[0.8])\n", 329 | "disp.add_contours(str(ATLAS_HOME / '1mm_tpm_wm.nii.gz'), colors=['w'], levels=[0.8], linewidths=[1], alpha=0.7)\n", 330 | "disp.add_contours(str(ATLAS_HOME / '1mm_brainmask.nii.gz'), colors=['k'], levels=[0.8], linewidths=[3], alpha=.7)\n", 331 | "disp.add_contours('box.nii.gz', colors=colorboxes, levels=[0.8, 1.8, 2.8], linewidths=[3], alpha=1., zorder=20)\n", 332 | "\n", 333 | "\n", 334 | "disp = plotting.plot_anat('newfeat.nii.gz', display_mode='z', annotate=False,\n", 335 | " cut_coords=[-15, -5, 10, 20, 40], cmap='cividis', threshold=thres, vmin=vmin, vmax=vmax,\n", 336 | " axes=b_ax2, resampling_interpolation='nearest')\n", 337 | "disp.annotate(size=15, left_right=False, positions=False, scalebar=True, scale_width=2.5,\n", 338 | " loc=3, size_vertical=2, label_top=False, frameon=True, borderpad=0.01)\n", 339 | "\n", 340 | "disp.add_contours('newcsfmsk.nii.gz', colors=['k'], levels=[0.8])\n", 341 | "disp.add_contours(str(ATLAS_HOME / '1mm_tpm_wm.nii.gz'), colors=['w'], levels=[0.8], linewidths=[1], alpha=0.7)\n", 342 | "disp.add_contours(str(ATLAS_HOME / '1mm_brainmask.nii.gz'), colors=['k'], levels=[0.8], linewidths=[3], alpha=.7)\n", 343 | "disp.add_contours('box.nii.gz', colors=colorboxes, levels=[0.8, 1.8, 2.8], linewidths=[3], alpha=1.)\n", 344 | "\n", 345 | "\n", 346 | "b_ax1.annotate(\n", 347 | " 'fMRIPrep',\n", 348 | " xy=(0., .5), xycoords='axes fraction', xytext=(-fs, .0),\n", 349 | " textcoords='offset points', va='center', color='k', size=fs,\n", 350 | " rotation=90)\n", 351 | "\n", 352 | "b_ax2.annotate(\n", 353 | " r'\\texttt{feat}',\n", 354 | " xy=(0., .5), xycoords='axes fraction', xytext=(-fs, .0),\n", 355 | " textcoords='offset points', va='center', color='k', size=fs,\n", 356 | " rotation=90)\n", 357 | "\n", 358 | "\n", 359 | "inner_grid = gridspec.GridSpecFromSubplotSpec(1, 2, width_ratios=[1.5, 15],\n", 360 | " subplot_spec=gs[:2, -1], wspace=0.01)\n", 361 | "\n", 362 | "b_ax3 = fig.add_subplot(inner_grid[0])\n", 363 | "gradient = np.hstack((np.zeros((50,)), np.linspace(0, 1, 120), np.ones((130,))))[::-1]\n", 364 | "gradient = np.vstack((gradient, gradient))\n", 365 | "b_ax3.imshow(gradient.T, aspect='auto', cmap=plt.get_cmap('cividis'))\n", 366 | "b_ax3.xaxis.set_ticklabels([])\n", 367 | "b_ax3.xaxis.set_ticks([])\n", 368 | "b_ax3.yaxis.set_ticklabels([])\n", 369 | "b_ax3.yaxis.set_ticks([])\n", 370 | "\n", 371 | "b_ax4 = fig.add_subplot(inner_grid[1])\n", 372 | "sn.distplot(nb.load(str(fprep_std)).get_data()[mask2mm], label='fMRIPrep',\n", 373 | " vertical=True, ax=b_ax4, kde=False, norm_hist=True)\n", 374 | "sn.distplot(nb.load(str(feat_std)).get_data()[mask2mm], label=r'\\texttt{feat}', vertical=True,\n", 375 | " color='darkorange', ax=b_ax4, kde=False, norm_hist=True)\n", 376 | "\n", 377 | "# plt.gca().set_ylim((0, 300))\n", 378 | "plt.legend(prop={'size': 15}, edgecolor='none')\n", 379 | "\n", 380 | "b_ax4.xaxis.set_ticklabels([])\n", 381 | "b_ax4.xaxis.set_ticks([])\n", 382 | "b_ax4.yaxis.set_ticklabels([])\n", 383 | "b_ax4.yaxis.set_ticks([])\n", 384 | "\n", 385 | "plt.axis('off')\n", 386 | "b_ax3.axis('off')\n", 387 | "b_ax4.axis('off')\n", 388 | "\n", 389 | "\n", 390 | "inner_grid = gridspec.GridSpecFromSubplotSpec(3, 2, width_ratios=[1, 1], hspace=0.04,\n", 391 | " subplot_spec=gs[3, :], wspace=0.01)\n", 392 | "\n", 393 | "thres = 20\n", 394 | "vmin = 40\n", 395 | "vmax = 200\n", 396 | "\n", 397 | "\n", 398 | "for i, coord in enumerate([-5, 10, 20]):\n", 399 | " ax1 = fig.add_subplot(inner_grid[i, 0])\n", 400 | " disp = plotting.plot_anat(\n", 401 | " 'fprep_clip.nii.gz', display_mode='z', annotate=False,\n", 402 | " cut_coords=[coord], cmap='cividis', threshold=thres, vmin=vmin, vmax=vmax,\n", 403 | " resampling_interpolation='nearest', axes=ax1)\n", 404 | " disp.add_contours('csf_clip.nii.gz', colors=['k'], levels=[0.8])\n", 405 | " disp.add_contours('wm_clip.nii.gz', colors=['w'], levels=[0.8], linewidths=[1], alpha=0.7)\n", 406 | " disp.add_contours('bm_clip.nii.gz', colors=['k'], levels=[0.8], linewidths=[3], alpha=.7)\n", 407 | "# disp.annotate(size=42, left_right=False, positions=True, scalebar=False)\n", 408 | " \n", 409 | " ax1.axis('on')\n", 410 | " ax1.set_xticklabels([])\n", 411 | " ax1.set_xticks([])\n", 412 | " ax1.set_yticklabels([])\n", 413 | " ax1.set_yticks([])\n", 414 | " ax1.set_ylabel(\"z = %d\" % coord, size=fs)\n", 415 | " for pos in ['top', 'bottom', 'left', 'right']:\n", 416 | " ax1.spines[pos].set_color(colorboxes[i])\n", 417 | " ax1.spines[pos].set_visible(True)\n", 418 | " ax1.spines[pos].set_linewidth(4)\n", 419 | " ax1.spines[pos].set_position(('outward', 2))\n", 420 | " \n", 421 | " ax2 = fig.add_subplot(inner_grid[i, 1])\n", 422 | " disp = plotting.plot_anat(\n", 423 | " 'feat_clip.nii.gz', display_mode='z', annotate=False,\n", 424 | " cut_coords=[coord], cmap='cividis', threshold=thres, vmin=vmin, vmax=vmax,\n", 425 | " resampling_interpolation='nearest', axes=ax2)\n", 426 | " disp.add_contours('csf_clip.nii.gz', colors=['k'], levels=[0.8])\n", 427 | " disp.add_contours('wm_clip.nii.gz', colors=['w'], levels=[0.8], linewidths=[1], alpha=0.7)\n", 428 | " disp.add_contours('bm_clip.nii.gz', colors=['k'], levels=[0.8], linewidths=[3], alpha=.7)\n", 429 | " \n", 430 | " ax2.axis('on')\n", 431 | " ax2.set_xticklabels([])\n", 432 | " ax2.set_xticks([])\n", 433 | " ax2.set_yticklabels([])\n", 434 | " ax2.set_yticks([])\n", 435 | " for pos in ['top', 'bottom', 'left', 'right']:\n", 436 | " ax2.spines[pos].set_color(colorboxes[i])\n", 437 | " ax2.spines[pos].set_visible(True)\n", 438 | " ax2.spines[pos].set_linewidth(4)\n", 439 | " ax2.spines[pos].set_position(('outward', 2))\n", 440 | " \n", 441 | " if i == 0:\n", 442 | " ax1.set_title('fMRIPrep', size=fs+2, position=(0.5, 1.02))\n", 443 | " ax2.set_title(r'\\texttt{feat}', size=fs+2, position=(0.5, 1.02))\n", 444 | "\n", 445 | "disp.annotate(size=32, left_right=False, positions=False, scalebar=True, scale_width=2.5,\n", 446 | " loc=4, size_vertical=1.2, label_top=False, frameon=True, borderpad=0.2)\n", 447 | "\n", 448 | "ax1.annotate(\n", 449 | " r'\\noindent\\parbox{25cm}{Variability mappings presented in this figure are derived from N=257 biologically independent participants}',\n", 450 | " xy=(0.12, -0.15), xycoords='axes fraction', xytext=(.0, .0),\n", 451 | " textcoords='offset points', va='center', color=(0.2, 0.2, 0.2), size=fs - 10,\n", 452 | ")\n", 453 | "\n", 454 | "plt.savefig(str(out_folder / 'SF04.pdf'),\n", 455 | " format='pdf', bbox_inches='tight', pad_inches=0.2, dpi=300)" 456 | ] 457 | }, 458 | { 459 | "cell_type": "code", 460 | "execution_count": null, 461 | "metadata": { 462 | "collapsed": true 463 | }, 464 | "outputs": [], 465 | "source": [] 466 | } 467 | ], 468 | "metadata": { 469 | "kernelspec": { 470 | "display_name": "Python 3", 471 | "language": "python", 472 | "name": "python3" 473 | }, 474 | "language_info": { 475 | "codemirror_mode": { 476 | "name": "ipython", 477 | "version": 3 478 | }, 479 | "file_extension": ".py", 480 | "mimetype": "text/x-python", 481 | "name": "python", 482 | "nbconvert_exporter": "python", 483 | "pygments_lexer": "ipython3", 484 | "version": "3.6.4" 485 | } 486 | }, 487 | "nbformat": 4, 488 | "nbformat_minor": 2 489 | } 490 | -------------------------------------------------------------------------------- /02 - Figure 3.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stderr", 10 | "output_type": "stream", 11 | "text": [ 12 | "/home/oesteban/workspace/niworkflows/niworkflows/__init__.py:24: UserWarning: \n", 13 | "This call to matplotlib.use() has no effect because the backend has already\n", 14 | "been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,\n", 15 | "or matplotlib.backends is imported for the first time.\n", 16 | "\n", 17 | "The backend was *originally* set to 'pgf' by the following code:\n", 18 | " File \"/home/oesteban/.anaconda3/lib/python3.6/runpy.py\", line 193, in _run_module_as_main\n", 19 | " \"__main__\", mod_spec)\n", 20 | " File \"/home/oesteban/.anaconda3/lib/python3.6/runpy.py\", line 85, in _run_code\n", 21 | " exec(code, run_globals)\n", 22 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py\", line 16, in \n", 23 | " app.launch_new_instance()\n", 24 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/traitlets/config/application.py\", line 658, in launch_instance\n", 25 | " app.start()\n", 26 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/ipykernel/kernelapp.py\", line 477, in start\n", 27 | " ioloop.IOLoop.instance().start()\n", 28 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/zmq/eventloop/ioloop.py\", line 177, in start\n", 29 | " super(ZMQIOLoop, self).start()\n", 30 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/tornado/ioloop.py\", line 888, in start\n", 31 | " handler_func(fd_obj, events)\n", 32 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/tornado/stack_context.py\", line 277, in null_wrapper\n", 33 | " return fn(*args, **kwargs)\n", 34 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py\", line 440, in _handle_events\n", 35 | " self._handle_recv()\n", 36 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py\", line 472, in _handle_recv\n", 37 | " self._run_callback(callback, msg)\n", 38 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py\", line 414, in _run_callback\n", 39 | " callback(*args, **kwargs)\n", 40 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/tornado/stack_context.py\", line 277, in null_wrapper\n", 41 | " return fn(*args, **kwargs)\n", 42 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py\", line 283, in dispatcher\n", 43 | " return self.dispatch_shell(stream, msg)\n", 44 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py\", line 235, in dispatch_shell\n", 45 | " handler(stream, idents, msg)\n", 46 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/ipykernel/kernelbase.py\", line 399, in execute_request\n", 47 | " user_expressions, allow_stdin)\n", 48 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/ipykernel/ipkernel.py\", line 196, in do_execute\n", 49 | " res = shell.run_cell(code, store_history=store_history, silent=silent)\n", 50 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/ipykernel/zmqshell.py\", line 533, in run_cell\n", 51 | " return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)\n", 52 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py\", line 2717, in run_cell\n", 53 | " interactivity=interactivity, compiler=compiler, result=result)\n", 54 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py\", line 2821, in run_ast_nodes\n", 55 | " if self.run_code(code, result):\n", 56 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py\", line 2881, in run_code\n", 57 | " exec(code_obj, self.user_global_ns, self.user_ns)\n", 58 | " File \"\", line 14, in \n", 59 | " from matplotlib import pyplot as plt\n", 60 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py\", line 71, in \n", 61 | " from matplotlib.backends import pylab_setup\n", 62 | " File \"/home/oesteban/.anaconda3/lib/python3.6/site-packages/matplotlib/backends/__init__.py\", line 16, in \n", 63 | " line for line in traceback.format_stack()\n", 64 | "\n", 65 | "\n", 66 | " matplotlib.use('Agg')\n", 67 | "/home/oesteban/.anaconda3/lib/python3.6/importlib/_bootstrap.py:219: ImportWarning: can't resolve package from __spec__ or __package__, falling back on __name__ and __path__\n", 68 | " return f(*args, **kwds)\n" 69 | ] 70 | } 71 | ], 72 | "source": [ 73 | "# %load_ext autoreload\n", 74 | "# %autoreload 2\n", 75 | "# %matplotlib inline\n", 76 | "import os\n", 77 | "from pathlib import Path\n", 78 | "import warnings\n", 79 | "\n", 80 | "import numpy as np\n", 81 | "import nibabel as nb\n", 82 | "import pandas as pd\n", 83 | "\n", 84 | "import matplotlib as mpl\n", 85 | "mpl.use('pgf')\n", 86 | "from matplotlib import pyplot as plt\n", 87 | "from matplotlib import gridspec\n", 88 | "import seaborn as sn\n", 89 | "import palettable\n", 90 | "\n", 91 | "from niworkflows.data import get_template\n", 92 | "\n", 93 | "from nilearn.image import concat_imgs, mean_img\n", 94 | "from nilearn import plotting\n", 95 | "\n", 96 | "warnings.simplefilter('ignore')\n", 97 | "\n", 98 | "DATA_HOME = Path(os.getenv('FMRIPREP_DATA_HOME', os.getcwd())).resolve()\n", 99 | "DS030_HOME = DATA_HOME / 'ds000030' / '1.0.3'\n", 100 | "DERIVS_HOME = DS030_HOME / 'derivatives'\n", 101 | "ATLAS_HOME = get_template('MNI152NLin2009cAsym')\n", 102 | "ANALYSIS_HOME = DERIVS_HOME / 'fmriprep_vs_feat_2.0-oe'\n", 103 | "\n", 104 | "fprep_home = DERIVS_HOME / 'fmriprep_1.0.8' / 'fmriprep'\n", 105 | "feat_home = DERIVS_HOME / 'fslfeat_5.0.10' / 'featbids'\n", 106 | "\n", 107 | "out_folder = Path(os.getenv('FMRIPREP_OUTPUTS') or '').resolve()\n", 108 | "\n", 109 | "# Load MNI152 nonlinear, asymmetric 2009c atlas\n", 110 | "atlas = nb.load(str(ATLAS_HOME / 'tpl-MNI152NLin2009cAsym_space-MNI_res-01_T1w.nii.gz'))\n", 111 | "mask1mm = nb.load(str(ATLAS_HOME / 'tpl-MNI152NLin2009cAsym_space-MNI_res-01_brainmask.nii.gz')).get_data() > 0\n", 112 | "mask2mm = nb.load(str(ATLAS_HOME / 'tpl-MNI152NLin2009cAsym_space-MNI_res-02_brainmask.nii.gz')).get_data() > 0\n", 113 | "data = atlas.get_data()\n", 114 | "data[~mask1mm] = data[~mask1mm].max()\n", 115 | "atlas = nb.Nifti1Image(data, atlas.affine, atlas.header)" 116 | ] 117 | }, 118 | { 119 | "cell_type": "code", 120 | "execution_count": 2, 121 | "metadata": { 122 | "collapsed": true 123 | }, 124 | "outputs": [], 125 | "source": [ 126 | "# sn.set_style(\"whitegrid\", {\n", 127 | "# 'ytick.major.size': 5,\n", 128 | "# 'xtick.major.size': 5,\n", 129 | "# })\n", 130 | "# sn.set_context(\"notebook\", font_scale=1.5)\n", 131 | "\n", 132 | "# pgf_with_custom_preamble = {\n", 133 | "# 'ytick.major.size': 0,\n", 134 | "# 'xtick.major.size': 0,\n", 135 | "# 'font.size': 30,\n", 136 | "# 'font.sans-serif': ['HelveticaLTStd-Light'],\n", 137 | "# 'font.family': 'sans-serif', # use serif/main font for text elements\n", 138 | "# 'text.usetex': False, # use inline math for ticks\n", 139 | "# }\n", 140 | "# mpl.rcParams.update(pgf_with_custom_preamble)\n", 141 | "\n", 142 | "\n", 143 | "pgf_with_custom_preamble = {\n", 144 | " 'text.usetex': True, # use inline math for ticks\n", 145 | " 'pgf.rcfonts': False, # don't setup fonts from rc parameters\n", 146 | " 'pgf.texsystem': 'xelatex',\n", 147 | " 'verbose.level': 'debug-annoying',\n", 148 | " \"pgf.preamble\": [\n", 149 | " r\"\"\"\\usepackage{fontspec}\n", 150 | "\\setsansfont{HelveticaLTStd-Light}[\n", 151 | "Extension=.otf,\n", 152 | "BoldFont=HelveticaLTStd-Bold,\n", 153 | "ItalicFont=HelveticaLTStd-LightObl,\n", 154 | "BoldItalicFont=HelveticaLTStd-BoldObl,\n", 155 | "]\n", 156 | "\\setmainfont{HelveticaLTStd-Light}[\n", 157 | "Extension=.otf,\n", 158 | "BoldFont=HelveticaLTStd-Bold,\n", 159 | "ItalicFont=HelveticaLTStd-LightObl,\n", 160 | "BoldItalicFont=HelveticaLTStd-BoldObl,\n", 161 | "]\n", 162 | "\\setmonofont{Inconsolata-dz}\n", 163 | "\"\"\",\n", 164 | " r'\\renewcommand\\familydefault{\\sfdefault}',\n", 165 | "# r'\\setsansfont[Extension=.otf]{Helvetica-LightOblique}',\n", 166 | "# r'\\setmainfont[Extension=.ttf]{DejaVuSansCondensed}',\n", 167 | "# r'\\setmainfont[Extension=.otf]{FiraSans-Light}',\n", 168 | "# r'\\setsansfont[Extension=.otf]{FiraSans-Light}',\n", 169 | " ]\n", 170 | "}\n", 171 | "mpl.rcParams.update(pgf_with_custom_preamble)" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": 3, 177 | "metadata": { 178 | "collapsed": true 179 | }, 180 | "outputs": [], 181 | "source": [ 182 | "def mean_std_map(pipe_home, meanmask, force=False, lazy=False, maskval=1000):\n", 183 | " pipe_std = pipe_home / 'summary_stdev.nii.gz'\n", 184 | " pipe_mean = pipe_home / 'summary_means.nii.gz'\n", 185 | "\n", 186 | " if force or not pipe_mean.is_file():\n", 187 | " print('Forced or %s not found' % pipe_mean)\n", 188 | " all_mus = []\n", 189 | " if lazy:\n", 190 | " all_mus = [nb.load(str(f)) for f in pipe_home.glob(\n", 191 | " 'sub-*/func/sub-*_task-stopsignal_bold_space-MNI152NLin2009cAsym_avgpreproc.nii.gz')]\n", 192 | " \n", 193 | " if not all_mus:\n", 194 | " print('Generating means file')\n", 195 | " pipe_files = list(pipe_home.glob(\n", 196 | " 'sub-*/func/sub-*_task-stopsignal_bold_space-MNI152NLin2009cAsym_preproc.nii.gz'))\n", 197 | " all_mus = []\n", 198 | " for f in pipe_files:\n", 199 | " mean = mean_img(str(f))\n", 200 | " data = mean.get_data()\n", 201 | " sigma = np.percentile(data[meanmask], 50) / maskval\n", 202 | " data /= sigma\n", 203 | " all_mus.append(nb.Nifti1Image(data, mean.affine, mean.header))\n", 204 | " \n", 205 | " meannii = concat_imgs(all_mus, auto_resample=False)\n", 206 | " meannii.to_filename(str(pipe_mean))\n", 207 | " force = True\n", 208 | "\n", 209 | " if force or not pipe_std.is_file():\n", 210 | " print('Generating standard deviation map')\n", 211 | " meannii = nb.load(str(pipe_mean))\n", 212 | " nb.Nifti1Image(meannii.get_data().std(3), meannii.affine, meannii.header).to_filename(str(pipe_std))\n", 213 | " \n", 214 | " return pipe_mean, pipe_std" 215 | ] 216 | }, 217 | { 218 | "cell_type": "code", 219 | "execution_count": 4, 220 | "metadata": { 221 | "collapsed": true 222 | }, 223 | "outputs": [], 224 | "source": [ 225 | "# Use the WM mask to normalize intensities of EPI means\n", 226 | "meanmask = nb.load(str(ATLAS_HOME / 'tpl-MNI152NLin2009cAsym_space-MNI_res-02_class-WM_probtissue.nii.gz')).get_data() > 0.9\n", 227 | "\n", 228 | "# Calculate average and std\n", 229 | "fprep_mean, fprep_std = mean_std_map(fprep_home, meanmask)\n", 230 | "feat_mean, feat_std = mean_std_map(feat_home, meanmask)\n", 231 | "\n", 232 | "# Trick to avoid nilearn zooming in\n", 233 | "feat_nii = nb.load(str(feat_std))\n", 234 | "\n", 235 | "fd = feat_nii.get_data()\n", 236 | "fd[0, 0, :] = 50\n", 237 | "fd[0, -1, :] = 50\n", 238 | "fd[-1, 0, :] = 50\n", 239 | "fd[-1, -1, :] = 50\n", 240 | "\n", 241 | "nb.Nifti1Image(fd, feat_nii.affine, feat_nii.header).to_filename('newfeat.nii.gz')" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": 5, 247 | "metadata": { 248 | "collapsed": true 249 | }, 250 | "outputs": [], 251 | "source": [ 252 | "df = pd.read_csv(str(ANALYSIS_HOME / 'smoothness.csv'))\n", 253 | "\n", 254 | "plt.clf()\n", 255 | "fig = plt.gcf()\n", 256 | "_ = fig.set_size_inches(15, 2 * 3.1)\n", 257 | "# gs = gridspec.GridSpec(2, 4, width_ratios=[38, 7, 60, 10], height_ratios=[1, 1], hspace=0.0, wspace=0.03)\n", 258 | "gs = gridspec.GridSpec(2, 3, width_ratios=[42, 9, 64], height_ratios=[1, 1], hspace=0.0, wspace=0.03)\n", 259 | "\n", 260 | "a_ax1 = plt.subplot(gs[0, 0])\n", 261 | "a_ax2 = plt.subplot(gs[1, 0])\n", 262 | "\n", 263 | "\n", 264 | "fmriprep_smooth = df[df.pipeline.str.contains('fmriprep')][['fwhm_pre', 'fwhm_post']]\n", 265 | "feat_smooth = df[df.pipeline.str.contains('feat')][['fwhm_pre', 'fwhm_post']]\n", 266 | "\n", 267 | "cols = palettable.tableau.ColorBlind_10.hex_colors\n", 268 | "sn.distplot(fmriprep_smooth.fwhm_post, color=cols[0], ax=a_ax2,\n", 269 | " axlabel='Smoothing', label='fMRIPrep')\n", 270 | "sn.distplot(feat_smooth.fwhm_post, color=cols[1], ax=a_ax2,\n", 271 | " axlabel='Smoothing', label=r'\\texttt{feat}')\n", 272 | "sn.distplot(fmriprep_smooth.fwhm_pre, color=cols[0], ax=a_ax1,\n", 273 | " axlabel='Smoothing', label='fMRIPrep')\n", 274 | "sn.distplot(feat_smooth.fwhm_pre, color=cols[1], ax=a_ax1,\n", 275 | " axlabel='Smoothing', label=r'\\texttt{feat}')\n", 276 | "\n", 277 | "a_ax2.set_xlim([3, 8.8])\n", 278 | "a_ax2.set_xticks([])\n", 279 | "a_ax2.set_xticklabels([])\n", 280 | "a_ax2.xaxis.tick_bottom()\n", 281 | "a_ax2.grid(False)\n", 282 | "a_ax2.set_xlabel('')\n", 283 | "\n", 284 | "a_ax2.set_ylim([-1.1, 9.9])\n", 285 | "a_ax2.set_yticks([])\n", 286 | "a_ax2.spines['left'].set_visible(False)\n", 287 | "\n", 288 | "a_ax1.set_ylabel(r'\\noindent\\parbox{4.8cm}{\\centering\\textbf{Before smoothing} fraction of images}',\n", 289 | " size=13)\n", 290 | "a_ax1.yaxis.set_label_coords(-0.1, 0.4)\n", 291 | "a_ax2.set_ylabel(r'\\noindent\\parbox{4.8cm}{\\centering\\textbf{After smoothing} fraction of images}',\n", 292 | " size=13)\n", 293 | "a_ax2.yaxis.set_label_coords(-0.1, 0.6)\n", 294 | "\n", 295 | "# ax4.spines['bottom'].set_position(('outward', 20))\n", 296 | "a_ax2.invert_yaxis()\n", 297 | "a_ax2.spines['top'].set_visible(False)\n", 298 | "a_ax2.spines['bottom'].set_visible(False)\n", 299 | "a_ax2.spines['left'].set_visible(False)\n", 300 | "a_ax2.spines['right'].set_visible(False)\n", 301 | "\n", 302 | "a_ax1.set_xlim([3, 8.8])\n", 303 | "a_ax1.set_ylim([-0.6, 10.4])\n", 304 | "a_ax1.grid(False)\n", 305 | "\n", 306 | "a_ax1.set_xlabel('(mm)')\n", 307 | "a_ax1.xaxis.set_label_coords(0.95, 0.1)\n", 308 | "a_ax1.set_yticks([])\n", 309 | "a_ax1.set_yticklabels([])\n", 310 | "a_ax1.set_xticks([3, 4, 5, 6 , 7, 8])\n", 311 | "a_ax1.set_xticklabels([3, 4, 5, 6 , 7, 8])\n", 312 | "a_ax1.tick_params(axis='x', zorder=100, direction='inout')\n", 313 | "a_ax1.spines['left'].set_visible(False)\n", 314 | "a_ax1.spines['right'].set_visible(False)\n", 315 | "a_ax1.spines['top'].set_visible(False)\n", 316 | "a_ax1.spines['bottom'].set_visible(True)\n", 317 | "a_ax1.zorder = 100\n", 318 | "\n", 319 | "# a_ax2.xaxis.set_label_position('top')\n", 320 | "# a_ax2.xaxis.set_label_coords(0.45, 0.95)\n", 321 | "a_ax1.annotate(\n", 322 | " r'\\noindent\\parbox{6.8cm}{\\centering\\textbf{Estimated smoothness} full~width~half~maximum~(mm)}',\n", 323 | " xy=(0.15, 0.8), xycoords='axes fraction', xytext=(.0, .0),\n", 324 | " textcoords='offset points', va='center', color='k', size=13,\n", 325 | ")\n", 326 | "\n", 327 | "legend = a_ax2.legend(ncol=2, loc='upper center', bbox_to_anchor=(0.5, 0.45), prop={'size': 15})\n", 328 | "legend.get_frame().set_facecolor('w')\n", 329 | "legend.get_frame().set_edgecolor('none')\n", 330 | "\n", 331 | "# a_ax2.annotate(\n", 332 | "# r'\\noindent\\parbox{15cm}{Panels A, B present statistics derived from N=257 biologically independent participants}',\n", 333 | "# xy=(-0.2, 0.03), xycoords='axes fraction', xytext=(.0, .0),\n", 334 | "# textcoords='offset points', va='center', color='k', size=11,\n", 335 | "# )\n", 336 | "\n", 337 | "###### PANEL B\n", 338 | "\n", 339 | "b_ax1 = plt.subplot(gs[0, 2])\n", 340 | "b_ax2 = plt.subplot(gs[1, 2])\n", 341 | "\n", 342 | "\n", 343 | "thres = 20\n", 344 | "vmin = 50\n", 345 | "vmax = 200\n", 346 | "\n", 347 | "disp = plotting.plot_anat(str(fprep_std), display_mode='z', annotate=False,\n", 348 | " cut_coords=[-5, 10, 20], cmap='cividis', threshold=thres, vmin=vmin, vmax=vmax,\n", 349 | " axes=b_ax1)\n", 350 | "disp.annotate(size=12, left_right=True, positions=True)\n", 351 | "disp.add_contours(str(ATLAS_HOME / 'tpl-MNI152NLin2009cAsym_space-MNI_res-01_class-CSF_probtissue.nii.gz'), colors=['k'], levels=[0.8])\n", 352 | "disp.add_contours(str(ATLAS_HOME / 'tpl-MNI152NLin2009cAsym_space-MNI_res-01_class-WM_probtissue.nii.gz'), colors=['w'], levels=[0.8], linewidths=[1], alpha=0.7)\n", 353 | "disp.add_contours(str(ATLAS_HOME / 'tpl-MNI152NLin2009cAsym_space-MNI_res-01_brainmask.nii.gz'), colors=['k'], levels=[0.8], linewidths=[3], alpha=.7)\n", 354 | "\n", 355 | "\n", 356 | "disp = plotting.plot_anat('newfeat.nii.gz', display_mode='z', annotate=False,\n", 357 | " cut_coords=[-5, 10, 20], cmap='cividis', threshold=thres, vmin=vmin, vmax=vmax,\n", 358 | " axes=b_ax2)\n", 359 | "disp.annotate(size=12, left_right=False, positions=False)\n", 360 | "disp.annotate(size=12, left_right=False, positions=False, scalebar=True,\n", 361 | " loc=3, size_vertical=2, label_top=False, frameon=True, borderpad=0.1)\n", 362 | "\n", 363 | "disp.add_contours(str(ATLAS_HOME / 'tpl-MNI152NLin2009cAsym_space-MNI_res-01_class-CSF_probtissue.nii.gz'), colors=['k'], levels=[0.8])\n", 364 | "disp.add_contours(str(ATLAS_HOME / 'tpl-MNI152NLin2009cAsym_space-MNI_res-01_class-WM_probtissue.nii.gz'), colors=['w'], levels=[0.8], linewidths=[1], alpha=0.7)\n", 365 | "disp.add_contours(str(ATLAS_HOME / 'tpl-MNI152NLin2009cAsym_space-MNI_res-01_brainmask.nii.gz'), colors=['k'], levels=[0.8], linewidths=[3], alpha=.7)\n", 366 | "\n", 367 | "b_ax1.annotate(\n", 368 | " 'fMRIPrep',\n", 369 | " xy=(0., .5), xycoords='axes fraction', xytext=(-20, .0),\n", 370 | " textcoords='offset points', va='center', color='k', size=15,\n", 371 | " rotation=90)\n", 372 | "\n", 373 | "b_ax2.annotate(\n", 374 | " r'\\texttt{feat}',\n", 375 | " xy=(0., .5), xycoords='axes fraction', xytext=(-20, .0),\n", 376 | " textcoords='offset points', va='center', color='k', size=12,\n", 377 | " rotation=90)\n", 378 | "\n", 379 | "\n", 380 | "# inner_grid = gridspec.GridSpecFromSubplotSpec(1, 2, width_ratios=[1, 15],\n", 381 | "# subplot_spec=gs[:, -1], wspace=0.01)\n", 382 | "\n", 383 | "# b_ax3 = fig.add_subplot(inner_grid[0])\n", 384 | "# gradient = np.hstack((np.zeros((50,)), np.linspace(0, 1, 120), np.ones((130,))))[::-1]\n", 385 | "# gradient = np.vstack((gradient, gradient))\n", 386 | "# b_ax3.imshow(gradient.T, aspect='auto', cmap=plt.get_cmap('cividis'))\n", 387 | "# b_ax3.xaxis.set_ticklabels([])\n", 388 | "# b_ax3.xaxis.set_ticks([])\n", 389 | "# b_ax3.yaxis.set_ticklabels([])\n", 390 | "# b_ax3.yaxis.set_ticks([])\n", 391 | "\n", 392 | "# b_ax4 = fig.add_subplot(inner_grid[1])\n", 393 | "# sn.distplot(nb.load(str(fprep_std)).get_data()[mask2mm], label='fMRIPrep',\n", 394 | "# vertical=True, ax=b_ax4, kde=False, norm_hist=True)\n", 395 | "# sn.distplot(nb.load(str(feat_std)).get_data()[mask2mm], label=r'\\texttt{feat}', vertical=True,\n", 396 | "# color='darkorange', ax=b_ax4, kde=False, norm_hist=True)\n", 397 | "\n", 398 | "# # plt.gca().set_ylim((0, 300))\n", 399 | "# plt.legend(prop={'size': 15}, edgecolor='none')\n", 400 | "\n", 401 | "# b_ax4.xaxis.set_ticklabels([])\n", 402 | "# b_ax4.xaxis.set_ticks([])\n", 403 | "# b_ax4.yaxis.set_ticklabels([])\n", 404 | "# b_ax4.yaxis.set_ticks([])\n", 405 | "\n", 406 | "# plt.axis('off')\n", 407 | "# b_ax3.axis('off')\n", 408 | "# b_ax4.axis('off')\n", 409 | "\n", 410 | "\n", 411 | "a_ax1.set_title('A', fontdict={'fontsize': 24}, loc='left', x=-0.2);\n", 412 | "b_ax1.set_title('B', fontdict={'fontsize': 24}, loc='left');\n", 413 | "\n", 414 | "plt.savefig(str(out_folder / 'figure03.pdf'),\n", 415 | " format='pdf', bbox_inches='tight', pad_inches=0.2, dpi=300)" 416 | ] 417 | }, 418 | { 419 | "cell_type": "code", 420 | "execution_count": null, 421 | "metadata": { 422 | "collapsed": true 423 | }, 424 | "outputs": [], 425 | "source": [ 426 | "coords = [-27, 0, 7]\n", 427 | "\n", 428 | "thres = 20\n", 429 | "vmin = 50\n", 430 | "vmax = 200\n", 431 | "\n", 432 | "\n", 433 | "# Plot\n", 434 | "plt.clf()\n", 435 | "fig = plt.figure(figsize=(20,10))\n", 436 | "\n", 437 | "plotting.plot_anat('newfeat.nii.gz', cut_coords=coords, colorbar=True, cmap='cividis',\n", 438 | " threshold=thres, vmin=vmin, vmax=vmax, title='feat',\n", 439 | " axes=plt.subplot(2,2,1)\n", 440 | ");\n", 441 | "plotting.plot_anat(str(fprep_std), cut_coords=coords, colorbar=True, cmap='cividis',\n", 442 | " threshold=thres, vmin=vmin, vmax=vmax, title='fmriprep',\n", 443 | " axes=plt.subplot(2,2,3)\n", 444 | ");\n", 445 | "plotting.plot_glass_brain(str(feat_std), threshold=200, colorbar=True, title='feat',\n", 446 | " axes=plt.subplot(2,2,2));\n", 447 | "plotting.plot_glass_brain(str(fprep_std), threshold=200, colorbar=True, title='fmriprep',\n", 448 | " axes=plt.subplot(2,2,4));" 449 | ] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "execution_count": null, 454 | "metadata": { 455 | "collapsed": true 456 | }, 457 | "outputs": [], 458 | "source": [] 459 | } 460 | ], 461 | "metadata": { 462 | "kernelspec": { 463 | "display_name": "Python 3", 464 | "language": "python", 465 | "name": "python3" 466 | }, 467 | "language_info": { 468 | "codemirror_mode": { 469 | "name": "ipython", 470 | "version": 3 471 | }, 472 | "file_extension": ".py", 473 | "mimetype": "text/x-python", 474 | "name": "python", 475 | "nbconvert_exporter": "python", 476 | "pygments_lexer": "ipython3", 477 | "version": "3.6.4" 478 | } 479 | }, 480 | "nbformat": 4, 481 | "nbformat_minor": 2 482 | } 483 | -------------------------------------------------------------------------------- /viz/preproc.py: -------------------------------------------------------------------------------- 1 | 2 | import os 3 | import glob 4 | import warnings; warnings.simplefilter('ignore') 5 | from pathlib import Path 6 | 7 | import matplotlib as mpl 8 | mpl.use('pgf') 9 | from matplotlib import pyplot as plt 10 | from matplotlib import gridspec 11 | 12 | import numpy as np 13 | import nibabel as nb 14 | import seaborn as sn 15 | from nilearn.image import concat_imgs, mean_img 16 | from nilearn import plotting 17 | 18 | sn.set_style("whitegrid", { 19 | 'ytick.major.size': 5, 20 | 'xtick.major.size': 5, 21 | }) 22 | sn.set_context("notebook", font_scale=1) 23 | 24 | pgf_with_custom_preamble = { 25 | 'ytick.major.size': 0, 26 | 'xtick.major.size': 0, 27 | 'font.sans-serif': ['HelveticaLTStd-Light'], 28 | 'font.family': 'sans-serif', # use serif/main font for text elements 29 | 'text.usetex': False, # use inline math for ticks 30 | } 31 | mpl.rcParams.update(pgf_with_custom_preamble) 32 | 33 | 34 | pgf_with_custom_preamble = { 35 | # 'font.sans-serif': ['Helvetica Light'], 36 | # 'font.family': 'sans-serif', # use serif/main font for text elements 37 | 'text.usetex': True, # use inline math for ticks 38 | 'pgf.rcfonts': False, # don't setup fonts from rc parameters 39 | 'pgf.texsystem': 'xelatex', 40 | 'verbose.level': 'debug-annoying', 41 | "pgf.preamble": [ 42 | # r'\renewcommand{\sfdefault}{phv}', 43 | # r'\usepackage[scaled=.92]{helvet}', 44 | r"""\usepackage{fontspec} 45 | \setsansfont{HelveticaLTStd-Light}[ 46 | Extension=.otf, 47 | BoldFont=HelveticaLTStd-Bold, 48 | ItalicFont=HelveticaLTStd-LightObl, 49 | BoldItalicFont=HelveticaLTStd-BoldObl, 50 | ] 51 | \setmainfont{HelveticaLTStd-Light}[ 52 | Extension=.otf, 53 | BoldFont=HelveticaLTStd-Bold, 54 | ItalicFont=HelveticaLTStd-LightObl, 55 | BoldItalicFont=HelveticaLTStd-BoldObl, 56 | ] 57 | \setmonofont{Inconsolata-dz} 58 | """, 59 | r'\renewcommand\familydefault{\sfdefault}', 60 | # r'\setsansfont[Extension=.otf]{Helvetica-LightOblique}', 61 | # r'\setmainfont[Extension=.ttf]{DejaVuSansCondensed}', 62 | # r'\setmainfont[Extension=.otf]{FiraSans-Light}', 63 | # r'\setsansfont[Extension=.otf]{FiraSans-Light}', 64 | ] 65 | } 66 | mpl.rcParams.update(pgf_with_custom_preamble) 67 | 68 | 69 | plt.clf() 70 | fig = plt.gcf() 71 | _ = fig.set_size_inches(15, 2 * 3.1) 72 | gs = gridspec.GridSpec(2, 2, width_ratios=[6, 1], height_ratios=[1, 1], hspace=0.0, wspace=0.03) 73 | 74 | thres = 20 75 | vmin = 50 76 | vmax = 200 77 | 78 | ax1 = plt.subplot(gs[0, :-1]) 79 | 80 | disp = plotting.plot_anat(str(fprep_std), display_mode='z', 81 | cut_coords=[-15, -5, 10, 20, 40], cmap='cividis', threshold=thres, vmin=vmin, vmax=vmax, 82 | axes=ax1) 83 | disp.add_contours(str(ATLAS_HOME / '1mm_tpm_csf.nii.gz'), colors=['k'], levels=[0.8]) 84 | disp.add_contours(str(ATLAS_HOME / '1mm_tpm_wm.nii.gz'), colors=['w'], levels=[0.8], linewidths=[1], alpha=0.7) 85 | disp.add_contours(str(ATLAS_HOME / '1mm_brainmask.nii.gz'), colors=['k'], levels=[0.8], linewidths=[3], alpha=.7) 86 | 87 | ax2 = plt.subplot(gs[1, :-1]) 88 | disp = plotting.plot_anat(str(feat_std), display_mode='z', 89 | cut_coords=[-15, -5, 10, 20, 40], cmap='cividis', threshold=thres, vmin=vmin, vmax=vmax, 90 | axes=ax2) 91 | disp.add_contours(str(ATLAS_HOME / '1mm_tpm_csf.nii.gz'), colors=['k'], levels=[0.8]) 92 | disp.add_contours(str(ATLAS_HOME / '1mm_tpm_wm.nii.gz'), colors=['w'], levels=[0.8], linewidths=[1], alpha=0.7) 93 | disp.add_contours(str(ATLAS_HOME / '1mm_brainmask.nii.gz'), colors=['k'], levels=[0.8], linewidths=[3], alpha=.7) 94 | 95 | ax1.annotate( 96 | 'fMRIPrep', 97 | xy=(0., .5), xycoords='axes fraction', xytext=(-24, .0), 98 | textcoords='offset points', va='center', color='k', size=24, 99 | rotation=90) 100 | 101 | ax2.annotate( 102 | r'\texttt{feat}', 103 | xy=(0., .5), xycoords='axes fraction', xytext=(-24, .0), 104 | textcoords='offset points', va='center', color='k', size=24, 105 | rotation=90) 106 | 107 | 108 | inner_grid = gridspec.GridSpecFromSubplotSpec(1, 2, width_ratios=[1, 15], 109 | subplot_spec=gs[:, 1], wspace=0.01) 110 | 111 | ax3 = fig.add_subplot(inner_grid[0]) 112 | gradient = np.hstack((np.zeros((50,)), np.linspace(0, 1, 120), np.ones((130,))))[::-1] 113 | gradient = np.vstack((gradient, gradient)) 114 | ax3.imshow(gradient.T, aspect='auto', cmap=plt.get_cmap('cividis')) 115 | ax3.xaxis.set_ticklabels([]) 116 | ax3.xaxis.set_ticks([]) 117 | ax3.yaxis.set_ticklabels([]) 118 | ax3.yaxis.set_ticks([]) 119 | 120 | ax4 = fig.add_subplot(inner_grid[1]) 121 | sn.distplot(nb.load(str(fprep_std)).get_data()[mask2mm], label='fMRIPrep', 122 | vertical=True, ax=ax4, kde=False, norm_hist=True) 123 | sn.distplot(nb.load(str(feat_std)).get_data()[mask2mm], label=r'\texttt{feat}', vertical=True, 124 | color='darkorange', ax=ax4, kde=False, norm_hist=True) 125 | 126 | # plt.gca().set_ylim((0, 300)) 127 | plt.legend(prop={'size': 20}, edgecolor='none') 128 | 129 | ax4.xaxis.set_ticklabels([]) 130 | ax4.xaxis.set_ticks([]) 131 | ax4.yaxis.set_ticklabels([]) 132 | ax4.yaxis.set_ticks([]) 133 | 134 | plt.axis('off') 135 | ax3.axis('off') 136 | ax4.axis('off') 137 | 138 | plt.savefig(str(out_folder / 'fmriprep-feat-std.pdf'), 139 | format='pdf', bbox_inches='tight', pad_inches=0.2, dpi=300) 140 | 141 | ################################################## Plotting close-ups 142 | plt.clf() 143 | for i, coords in enumerate([-5, 10, 20]): 144 | disp = plotting.plot_anat(str(fprep_std), display_mode='z', cut_coords=[coords], cmap='cividis', threshold=30, vmin=50, vmax=170) 145 | f = plt.gcf().set_size_inches(10, 20) 146 | disp.add_contours(str(ATLAS_HOME / '1mm_tpm_csf.nii.gz'), colors=['k'], levels=[0.8]) 147 | disp.add_contours(str(ATLAS_HOME / '1mm_tpm_wm.nii.gz'), colors=['w'], levels=[0.8], alpha=0.7) 148 | disp.add_contours(str(ATLAS_HOME / '1mm_brainmask.nii.gz'), colors=['k'], levels=[0.8], linewidths=[3], alpha=.7) 149 | plt.savefig(str(out_folder / ('fmriprep-std-closeup%03d.svg' % i)), format='svg', bbox_inches='tight') 150 | 151 | for i, coords in enumerate([-5, 10, 20]): 152 | disp = plotting.plot_anat(str(feat_std), display_mode='z', cut_coords=[coords], cmap='viridis', threshold=30, vmin=50, vmax=170) 153 | f = plt.gcf().set_size_inches(10, 20) 154 | disp.add_contours(str(ATLAS_HOME / '1mm_tpm_csf.nii.gz'), colors=['k'], levels=[0.8]) 155 | disp.add_contours(str(ATLAS_HOME / '1mm_tpm_wm.nii.gz'), colors=['w'], levels=[0.8], alpha=0.7) 156 | disp.add_contours(str(ATLAS_HOME / '1mm_brainmask.nii.gz'), colors=['k'], levels=[0.8], linewidths=[3], alpha=.7) 157 | plt.savefig(str(out_folder / ('feat-std-closeup%03d.svg' % i)), format='svg', bbox_inches='tight') 158 | 159 | 160 | 161 | ###################################################### Plotting 1st level 162 | 163 | plt.clf() 164 | 165 | sn.set_context("notebook", font_scale=1.3) 166 | sn.set_style("whitegrid", { 167 | 'xtick.major.size': 5, 168 | }) 169 | 170 | cut_coords = [0, 15, 30] 171 | 172 | df = pd.read_csv(str(basedir / 'smoothness.csv')) 173 | 174 | fig = plt.gcf() 175 | _ = fig.set_size_inches(15, 2 * 3.1) 176 | gs = gridspec.GridSpec(2, 2, width_ratios=[1, 2], height_ratios=[1, 1], hspace=0.0, wspace=0.6) 177 | 178 | ax3 = plt.subplot(gs[0, :-1]) 179 | ax4 = plt.subplot(gs[1, :-1]) 180 | ax1 = plt.subplot(gs[0, -1]) 181 | ax2 = plt.subplot(gs[1, -1]) 182 | 183 | disp = plotting.plot_stat_map(str(basedir / 'acm_fpre.nii.gz'), 184 | bg_img=atlas, threshold=0.25, display_mode='z', 185 | cut_coords=cut_coords, vmax=0.8, alpha=0.8, 186 | axes=ax1, colorbar=False) 187 | cbar = fig.colorbar(ax1, ticks=[0, 0.275, 0.725, 1.0]) 188 | cbar.ax.set_yticklabels([-0.8, -0.25, 0.25, 0.8]) 189 | # print(list(fig.axes[-1].get_yticks())) 190 | # fig.axes[-1].set_yticks([0, 0.275, 0.5, 0.725, 1.0]) 191 | # print(list(fig.axes[-1].get_yticks())) 192 | 193 | plotting.plot_stat_map(str(basedir / 'acm_feat.nii.gz'), 194 | bg_img=atlas, threshold=0.25, display_mode='z', 195 | cut_coords=cut_coords, vmax=0.8, alpha=0.8, 196 | axes=ax2, colorbar=False) 197 | 198 | ax1.annotate( 199 | 'fMRIPrep', 200 | xy=(0., .5), xycoords='axes fraction', xytext=(-40, .0), 201 | textcoords='offset points', va='center', color='k', size=24, 202 | rotation=90) 203 | 204 | ax2.annotate( 205 | r'\texttt{feat}', 206 | xy=(0., .5), xycoords='axes fraction', xytext=(-40, .0), 207 | textcoords='offset points', va='center', color='k', size=24, 208 | rotation=90) 209 | 210 | 211 | 212 | fmriprep_smooth = df[df.pipeline.str.contains('fmriprep')][['fwhm_pre', 'fwhm_post']] 213 | feat_smooth = df[df.pipeline.str.contains('feat')][['fwhm_pre', 'fwhm_post']] 214 | 215 | cols = palettable.tableau.ColorBlind_10.hex_colors 216 | sn.distplot(fmriprep_smooth.fwhm_pre, color=cols[0], ax=ax3, 217 | axlabel='Smoothing', label='fMRIPrep') 218 | sn.distplot(feat_smooth.fwhm_pre, color=cols[1], ax=ax3, 219 | axlabel='Smoothing', label=r'\texttt{feat}') 220 | 221 | sn.distplot(fmriprep_smooth.fwhm_post, color=cols[0], ax=ax4, 222 | axlabel='Smoothing', label='fMRIPrep') 223 | sn.distplot(feat_smooth.fwhm_post, color=cols[1], ax=ax4, 224 | axlabel=r'\noindent\parbox{6.8cm}{\centering\textbf{Estimated smoothness} full~width~half~maximum~(mm)}', 225 | label='feat') 226 | 227 | ax4.set_xlim([3, 8]) 228 | ax4.set_xticks([3, 4, 5, 6 , 7, 8]) 229 | ax4.set_xticklabels([3, 4, 5, 6 , 7, 8]) 230 | ax4.grid(False) 231 | 232 | sn.despine(offset=0, trim=True) 233 | 234 | ax4.set_ylim([-3, 8]) 235 | ax4.set_yticks([]) 236 | ax4.spines['left'].set_visible(False) 237 | 238 | ax3.set_ylabel(r'\noindent\parbox{4.8cm}{\centering\textbf{Before smoothing} fraction of images}') 239 | ax3.yaxis.set_label_coords(-0.1, 0.4) 240 | ax4.set_ylabel(r'\noindent\parbox{4.8cm}{\centering\textbf{After smoothing} fraction of images}') 241 | ax4.yaxis.set_label_coords(-0.1, 0.5) 242 | 243 | # ax4.spines['bottom'].set_position(('outward', 20)) 244 | ax4.invert_yaxis() 245 | ax4.xaxis.set_label_position('top') 246 | ax4.spines['bottom'].set_visible(False) 247 | ax4.spines['top'].set_visible(True) 248 | ax4.spines['top'].set_position(('data', 5)) 249 | ax4.xaxis.set_ticks_position('top') 250 | ax4.tick_params(axis='x', pad=-30) 251 | ax4.xaxis.set_label_coords(0.6, 0.85) 252 | 253 | 254 | ax3.set_xlim([3, 8]) 255 | ax3.set_xticks([]) 256 | ax3.set_xticklabels([]) 257 | ax3.set_ylim([0, 11]) 258 | ax3.set_yticks([]) 259 | ax3.set_yticklabels([]) 260 | ax3.grid(False) 261 | ax3.set_xlabel('') 262 | ax3.spines['bottom'].set_visible(False) 263 | ax3.spines['left'].set_visible(False) 264 | ax3.spines['right'].set_visible(False) 265 | ax3.spines['top'].set_visible(False) 266 | 267 | legend = ax3.legend(ncol=2, loc='upper right', bbox_to_anchor=(1.0,0.8)) 268 | legend.get_frame().set_facecolor('w') 269 | legend.get_frame().set_edgecolor('gray') 270 | 271 | ax3.set_title('A', fontdict={'fontsize': 24}, loc='left'); 272 | ax1.set_title('B', fontdict={'fontsize': 24}, loc='left'); 273 | 274 | plt.savefig(str(out_folder / 'fmriprep-feat-1stlevel-2.pdf'), 275 | format='pdf', bbox_inches='tight', pad_inches=0.2, dpi=300) 276 | 277 | 278 | ####################################################################### 2nd level 279 | 280 | %matplotlib inline 281 | import pandas as pd 282 | 283 | import os 284 | import glob 285 | import warnings; warnings.simplefilter('ignore') 286 | from pathlib import Path 287 | 288 | import matplotlib as mpl 289 | mpl.use('pgf') 290 | from matplotlib import pyplot as plt 291 | from matplotlib import gridspec 292 | 293 | import numpy as np 294 | import nibabel as nb 295 | import seaborn as sn 296 | from nilearn import plotting 297 | 298 | import pandas as pd 299 | import palettable 300 | 301 | pgf_with_custom_preamble = { 302 | 'text.usetex': True, # use inline math for ticks 303 | 'pgf.rcfonts': False, # don't setup fonts from rc parameters 304 | 'pgf.texsystem': 'xelatex', 305 | 'verbose.level': 'debug-annoying', 306 | "pgf.preamble": [ 307 | r"""\usepackage{fontspec} 308 | \setsansfont{HelveticaLTStd-Light}[ 309 | Extension=.otf, 310 | BoldFont=HelveticaLTStd-Bold, 311 | ItalicFont=HelveticaLTStd-LightObl, 312 | BoldItalicFont=HelveticaLTStd-BoldObl, 313 | ] 314 | \setmainfont{HelveticaLTStd-Light}[ 315 | Extension=.otf, 316 | BoldFont=HelveticaLTStd-Bold, 317 | ItalicFont=HelveticaLTStd-LightObl, 318 | BoldItalicFont=HelveticaLTStd-BoldObl, 319 | ] 320 | \setmonofont{Inconsolata-dz} 321 | \renewcommand\familydefault{\sfdefault} 322 | """,] 323 | } 324 | mpl.rcParams.update(pgf_with_custom_preamble) 325 | 326 | basedir = Path('/oak/stanford/groups/russpold/data/ds000030/1.0.3/derivatives/fmriprep_vs_feat_2.0-oe') 327 | 328 | # jokesdf = pd.read_csv(basedir.parent.parent / 'fmriprep_vs_feat' / 'results.csv', index_col=0) 329 | jokesdf = pd.read_csv(basedir.parent / 'fmriprep_vs_feat_2.0-jd' / 'results.csv', index_col=0) 330 | jokesdf.columns = ['IFG', 'PCG', 'STN', 'correlation', 'bdice', 'bdicemasked', 'experiment', 'fdice', 331 | 'fdicemasked', 'pipeline', 'preSMA', 'N'] 332 | jokesdf.pipeline = jokesdf.pipeline.str.replace('fslfeat_5.0.10', 'feat') 333 | jokesdf.pipeline = jokesdf.pipeline.str.replace('fmriprep_1.0.8', 'fmriprep') 334 | jokesdf.N = jokesdf.N.astype('uint8') 335 | 336 | basedir = Path('/oak/stanford/groups/russpold/data/ds000030/1.0.3/derivatives/fmriprep_vs_feat_2.0-oe') 337 | dataframe = pd.read_csv(basedir / 'l2' / 'group.csv', index_col=0) 338 | 339 | cols = palettable.tableau.ColorBlind_10.hex_colors 340 | sn.set_style("whitegrid") 341 | 342 | plt.clf() 343 | fig = plt.figure(figsize=(20,20)) 344 | plt.subplot(3,3,1) 345 | sn.boxplot(x="N", y="bdice", hue='pipeline', hue_order=['fmriprep', 'fslfeat'], 346 | data=dataframe, palette=cols, linewidth=0.6) 347 | # sn.pointplot(x="N", y="bdice", hue="pipeline", data=dataframe, 348 | # capsize=.2, palette=cols, size=0.5, aspect=.75, dodge=0.5) 349 | plt.ylabel("Binary Dice (OE)") 350 | plt.xlabel("Sample size $N$") 351 | 352 | plt.subplot(3,3,2) 353 | # sn.stripplot(x="N", y="fdice", hue="pipeline", data=dataframe) 354 | sn.boxplot(x="N", y="fdice", hue="pipeline", hue_order=['fmriprep', 'fslfeat'], 355 | data=dataframe, palette=cols, linewidth=.6) 356 | plt.ylabel("Fuzzy Dice (OE)") 357 | plt.xlabel("Sample size $N$") 358 | plt.subplot(3,3,3) 359 | sn.boxplot(x="N", y="correlation", hue="pipeline", hue_order=['fmriprep', 'fslfeat'], 360 | data=dataframe, palette=cols, linewidth=.6) 361 | # sn.stripplot(x="N", y="correlation", hue="pipeline", data=dataframe) 362 | plt.ylabel("Correlation (OE)") 363 | plt.xlabel("Sample size $N$") 364 | 365 | 366 | basedir = Path('/oak/stanford/groups/russpold/data/ds000030/1.0.3/derivatives/fmriprep_vs_feat_2.0-jd') 367 | jd1 = pd.read_csv(basedir / 'l2' / 'group.csv', index_col=0) 368 | plt.subplot(3,3,4) 369 | sn.boxplot(x="N", y="bdice", hue='pipeline', hue_order=['fmriprep', 'fslfeat'], 370 | data=jd1, palette=cols, linewidth=0.6) 371 | # sn.pointplot(x="N", y="bdice", hue="pipeline", data=dataframe, 372 | # capsize=.2, palette=cols, size=0.5, aspect=.75, dodge=0.5) 373 | plt.ylabel("Binary Dice (JD+OE)") 374 | plt.xlabel("Sample size $N$") 375 | 376 | plt.subplot(3,3,5) 377 | # sn.stripplot(x="N", y="fdice", hue="pipeline", data=dataframe) 378 | sn.boxplot(x="N", y="fdice", hue="pipeline", hue_order=['fmriprep', 'fslfeat'], 379 | data=jd1, palette=cols, linewidth=.6) 380 | plt.ylabel("Fuzzy Dice (JD+OE)") 381 | plt.xlabel("Sample size $N$") 382 | plt.subplot(3,3,6) 383 | sn.boxplot(x="N", y="correlation", hue="pipeline", hue_order=['fmriprep', 'fslfeat'], 384 | data=jd1, palette=cols, linewidth=.6) 385 | # sn.stripplot(x="N", y="correlation", hue="pipeline", data=dataframe) 386 | plt.ylabel("Correlation (JD+OE)") 387 | plt.xlabel("Sample size $N$") 388 | 389 | 390 | 391 | basedir = Path('/oak/stanford/groups/russpold/data/ds000030/1.0.3/derivatives/fmriprep_vs_feat_2.0-jd') 392 | dataframe = pd.read_csv(basedir / 'l2-jd' / 'group.csv', index_col=0) 393 | dataframe.columns = ['IFG', 'N', 'PCG', 'STN', 'correlation', 'bdice-old', 'bdice', 'fdice-old', 394 | 'fdice', 'pipeline', 'preSMA', 'repetition'] 395 | plt.subplot(3,3,7) 396 | sn.boxplot(x="N", y="bdice", hue='pipeline', hue_order=['fmriprep', 'fslfeat'], 397 | data=dataframe, palette=cols, linewidth=0.6) 398 | # sn.pointplot(x="N", y="bdice", hue="pipeline", data=dataframe, 399 | # capsize=.2, palette=cols, size=0.5, aspect=.75, dodge=0.5) 400 | plt.ylabel("Binary Dice (JD+OE)") 401 | plt.xlabel("Sample size $N$") 402 | 403 | plt.subplot(3,3,8) 404 | # sn.stripplot(x="N", y="fdice", hue="pipeline", data=dataframe) 405 | sn.boxplot(x="N", y="fdice", hue="pipeline", hue_order=['fmriprep', 'fslfeat'], 406 | data=dataframe, palette=cols, linewidth=.6) 407 | plt.ylabel("Fuzzy Dice (JD+OE)") 408 | plt.xlabel("Sample size $N$") 409 | plt.subplot(3,3,9) 410 | sn.boxplot(x="N", y="correlation", hue="pipeline", hue_order=['fmriprep', 'fslfeat'], 411 | data=dataframe, palette=cols, linewidth=.6) 412 | # sn.stripplot(x="N", y="correlation", hue="pipeline", data=dataframe) 413 | plt.ylabel("Correlation (JD+OE)") 414 | plt.xlabel("Sample size $N$") 415 | 416 | 417 | 418 | 419 | # fprepdf = dataframe[dataframe.pipeline.str.startswith('fmriprep')] 420 | # featdf = dataframe[dataframe.pipeline.str.contains('feat')] 421 | 422 | # samplesizes = sorted(list(set(dataframe.N.values.ravel()))) 423 | # fprepstds = [fprepdf[fprepdf.N == s].fdice.mad() for s in samplesizes] 424 | # featstds = [featdf[featdf.N == s].fdice.mad() for s in samplesizes] 425 | 426 | # ax.scatter(x=samplesizes, y=fprepstds, color=cols[0]) 427 | # ax.scatter(x=samplesizes, y=featstds, color=cols[1]) 428 | 429 | 430 | plt.savefig(str(out_folder / 'fmriprep-feat-2stlevel-jd_vs_oe.pdf'), 431 | format='pdf', pad_inches=0.2, dpi=300) 432 | 433 | 434 | 435 | cols = palettable.tableau.ColorBlind_10.hex_colors 436 | sn.set_style("whitegrid") 437 | 438 | plt.clf() 439 | fig = plt.figure(figsize=(20,16)) 440 | 441 | plt.subplot(2,3,1) 442 | # sn.stripplot(x="N", y="bdice", hue="pipeline", data=dataframe, jitter=0.3, alpha=0.5, cmap=cols, size=3) 443 | sn.boxplot(x="N", y="bdice", hue="pipeline", data=jokesdf, palette=cols, linewidth=1) 444 | plt.ylabel("Binary Dice (JD+OE)") 445 | plt.xlabel("Sample size $N$") 446 | plt.subplot(2,3,2) 447 | # sn.stripplot(x="N", y="fdice", hue="pipeline", data=dataframe) 448 | sn.boxplot(x="N", y="fdice", hue="pipeline", data=jokesdf, palette=cols, linewidth=1) 449 | plt.ylabel("Fuzzy Dice (JD+OE)") 450 | plt.xlabel("Sample size $N$") 451 | plt.subplot(2,3,3) 452 | sn.boxplot(x="N", y="correlation", hue="pipeline", data=jokesdf, palette=cols, linewidth=1) 453 | # sn.stripplot(x="N", y="correlation", hue="pipeline", data=dataframe) 454 | plt.ylabel("Correlation (JD+OE)") 455 | plt.xlabel("Sample size $N$") 456 | 457 | # dataframe = pd.read_csv(basedir.parent / 'fmriprep_vs_feat' / 'results.csv', index_col=0) 458 | # dataframe.columns = ['IFG', 'PCG', 'STN', 'correlation', 'bdice', 'experiment', 'fdice', 459 | # 'pipeline', 'preSMA', 'N'] 460 | # dataframe.pipeline = dataframe.pipeline.str.replace('fslfeat_5.0.9', 'feat') 461 | # dataframe.pipeline = dataframe.pipeline.str.replace('fmriprep-1.0.3', 'fmriprep') 462 | # dataframe.N = dataframe.N.astype('uint8') 463 | 464 | plt.subplot(2,3,4) 465 | # sn.stripplot(x="N", y="bdice", hue="pipeline", data=dataframe, jitter=0.3, alpha=0.5, cmap=cols, size=3) 466 | sn.boxplot(x="N", y="bdice", hue='pipeline', data=dataframe, palette=cols, linewidth=1) 467 | plt.ylabel("Binary Dice (OE)") 468 | plt.xlabel("Sample size $N$") 469 | plt.subplot(2,3,5) 470 | # sn.stripplot(x="N", y="fdice", hue="pipeline", data=dataframe) 471 | sn.boxplot(x="N", y="fdice", hue="pipeline", data=dataframe, palette=cols, linewidth=1) 472 | plt.ylabel("Fuzzy Dice (OE)") 473 | plt.xlabel("Sample size $N$") 474 | plt.subplot(2,3,6) 475 | sn.boxplot(x="N", y="correlation", hue="pipeline", data=dataframe, palette=cols, linewidth=1) 476 | # sn.stripplot(x="N", y="correlation", hue="pipeline", data=dataframe) 477 | plt.ylabel("Correlation (OE)") 478 | plt.xlabel("Sample size $N$") 479 | plt.savefig(str(out_folder / 'fmriprep-feat-2stlevel-jd_vs_oe.pdf'), 480 | format='pdf', pad_inches=0.2, dpi=300) 481 | 482 | 483 | %matplotlib inline 484 | cols = palettable.tableau.ColorBlind_10.hex_colors 485 | sn.set_style("whitegrid") 486 | 487 | plt.clf() 488 | fig = plt.figure(figsize=(20,8)) 489 | 490 | plt.subplot(1,3,1) 491 | # sn.stripplot(x="N", y="bdice", hue="pipeline", data=dataframe, jitter=0.3, alpha=0.5, cmap=cols, size=3) 492 | sn.boxplot(x="N", y="bdice", hue="pipeline", data=dataframe, palette=cols, linewidth=1) 493 | plt.ylabel("Binary Dice (OE)") 494 | plt.xlabel("Sample size $N$") 495 | plt.subplot(1,3,2) 496 | # sn.stripplot(x="N", y="fdice", hue="pipeline", data=dataframe) 497 | sn.boxplot(x="N", y="fdice", hue="pipeline", data=dataframe, palette=cols, linewidth=1) 498 | plt.ylabel("Fuzzy Dice (OE)") 499 | plt.xlabel("Sample size $N$") 500 | plt.subplot(1,3,3) 501 | sn.boxplot(x="N", y="correlation", hue="pipeline", data=dataframe, palette=cols, linewidth=1) 502 | # sn.stripplot(x="N", y="correlation", hue="pipeline", data=dataframe) 503 | plt.ylabel("Correlation (OE)") 504 | plt.xlabel("Sample size $N$") 505 | 506 | 507 | 508 | def read_rating(fname, rater=None): 509 | ds, sub = os.path.basename(os.path.splitext(fname)[0]).split('_') 510 | data = {'dataset': ds, 'subject': sub} 511 | if rater is not None: 512 | data['rater'] = rater 513 | 514 | with open(fname) as f: 515 | ratings = json.load(f) 516 | 517 | for reportlet in ratings['reports']: 518 | name = reportlet['name'] 519 | 520 | if name == 'overall': 521 | data[name] = int(reportlet['rating']) 522 | elif '_T1w_' in name: 523 | data['t1_%s' % name.split('_T1w_')[-1]] = int(reportlet['rating']) 524 | elif '_bold_' in name: 525 | repname = 'bold_%s' % name.split('_bold_')[-1] 526 | data.setdefault(repname, []).append(int(reportlet['rating'])) 527 | elif '_fieldmap_': 528 | repname = name.split('_fieldmap_')[-1] 529 | data.setdefault(repname, []).append(int(reportlet['rating'])) 530 | else: 531 | print('Unsupported field name "%s"' % name) 532 | 533 | return data 534 | 535 | def read_dataset(data_dir, fields=['overall', 't1_reconall', 't1_seg_brainmask', 't1_t1_2_mni', 536 | 'bold_rois', 'bold_bbr', 'bold_syn_sdc']): 537 | dataset = [read_rating(f, rater='rater_1') for f in data_dir.glob('rater_1/*.json')] 538 | dataset += [read_rating(f, rater='rater_2') for f in data_dir.glob('rater_2/*.json')] 539 | dataset += [read_rating(f, rater='rater_3') for f in data_dir.glob('rater_3/*.json')] 540 | 541 | infields = list(set([a for g in dataset for a in g.keys()])) 542 | infields.remove('dataset') 543 | infields.remove('subject') 544 | infields.remove('rater') 545 | headers = ['dataset'] + infields 546 | 547 | failed = [] 548 | unrated = [] 549 | 550 | # Average 551 | dfs = [] 552 | for i, d in enumerate(dataset): 553 | if 'bold_variant-hmcsdc_preproc' in d: 554 | d['bold_rois'] = list(d['bold_rois']) + list(d['bold_variant-hmcsdc_preproc']) 555 | del d['bold_variant-hmcsdc_preproc'] 556 | for k, v in d.items(): 557 | if k in ['dataset', 'subject', 'rater']: 558 | continue 559 | 560 | if isinstance(v, list): 561 | filtered = [vv for vv in v if int(vv) > 0] 562 | if filtered: 563 | d[k] = float(np.average(filtered)) 564 | else: 565 | d[k] = np.nan 566 | else: 567 | v = float(v) if int(v) > 0 else np.nan 568 | 569 | dfs.append(pd.DataFrame(d, columns=headers, index=[i])) 570 | 571 | # Merge raters 572 | allraters = pd.concat(dfs).sort_values(by='dataset') 573 | allraters[infields] = allraters[infields].clip(0.0) 574 | 575 | averaged = [] 576 | for ds in set(allraters.dataset.ravel().tolist()): 577 | d = {'dataset': ds.upper()} 578 | group = allraters[allraters.dataset.str.contains(ds)] 579 | groupavg = np.mean(group[headers[1:]].values, axis=0) 580 | d.update({k: v for k, v in zip(headers[1:], groupavg)}) 581 | averaged.append(pd.DataFrame(d, columns=headers, index=pd.Index([d['dataset']]))) 582 | dataframe = pd.concat(averaged).sort_values(by='dataset') 583 | dataframe.index.name = 'dataset' 584 | 585 | dataframe['bold_bbr'] = dataframe[['bold_bbr', 'bold_coreg']].mean(axis=1) 586 | # dataframe['bold_rois'] = dataframe[ 587 | # ['bold_rois'] + ['bold_variant-hmcsdc_preproc'] if 'bold_variant-hmcsdc_preproc' in headers else []].mean(axis=1) 588 | # 'fmap_mask', 'bold_fmap_reg', 'bold_fmap_reg_vsm', 589 | return dataframe[fields].sort_values(by=fields, ascending=False) 590 | 591 | 592 | # dataframe = pd.read_csv('fmriprep_qc.tsv', sep='\t') 593 | # dataframe0 = dataframe[dataframe.version.str.contains('1.0.0')] 594 | # dataframe = dataframe[dataframe.version.str.contains('1.0.7')] 595 | 596 | dataframe = read_dataset(Path.home().joinpath('tmp/fmriprep-reports-2')) 597 | dataframe0 = read_dataset(Path.home().joinpath('tmp/fmriprep-reports-1')) 598 | dataframe0 = dataframe0.reindex(dataframe.index) 599 | dataframe 600 | 601 | dataframe0['qc'] = [1] * len(dataframe0) 602 | dataframe['qc'] = [2] * len(dataframe) 603 | 604 | # dataframe0.to_csv('fmriprep_1.0.0.tsv', sep='\t', index=None) 605 | # dataframe.to_csv('fmriprep_1.0.7.tsv', sep='\t', index=None) 606 | 607 | new = dataframe0.append(dataframe) 608 | new['version'] = new.qc.map({1: '1.0.0', 2: '1.0.7'}) 609 | new['dataset'] = new.index.values 610 | new[['dataset', 'version', 'overall', 't1_reconall', 't1_seg_brainmask', 't1_t1_2_mni', 'bold_rois', 'bold_bbr', 'bold_syn_sdc']].to_csv('fmriprep_qc.tsv', sep='\t', index=None) 611 | --------------------------------------------------------------------------------