Skip to content

Commit 1734b34

Browse files
committed
MNT simplify the pycortex database reload in colab
1 parent d2b43af commit 1734b34

15 files changed

Lines changed: 54 additions & 238 deletions

tutorials/movies_3T/00_setup_colab.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,8 @@
5252
os.environ['VOXELWISE_TUTORIALS_DATA'] = "/content"
5353

5454
import cortex
55-
filestore = "/content/pycortex/filestore/"
56-
cortex.options.config['basic']['filestore'] = filestore
55+
cortex.db.__init__("/content/pycortex/filestore/") # change filestore inplace
5756
cortex.options.config['webgl']['colormaps'] = "/content/pycortex/filestore/colormaps"
58-
cortex.database.db = cortex.database.Database(filestore)
59-
cortex.db = cortex.database.db
60-
cortex.utils.db = cortex.database.db
61-
cortex.dataset.braindata.db = cortex.database.db
6257

6358
import sklearn
6459
sklearn.set_config(assume_finite=True)

tutorials/movies_3T/01_plot_explainable_variance.py

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
plt.show()
113113

114114
###############################################################################
115-
# We see that many voxels have low explainable variance. This is
115+
# We see that many voxels have low explainable variance. This is
116116
# expected, since many voxels are not driven by a visual stimulus, and their
117117
# response changes over repeats of the same stimulus.
118118
# We also see that some voxels have high explainable variance (around 0.7). The
@@ -171,41 +171,17 @@
171171
# The second mapper we provide maps the voxel data to a Freesurfer
172172
# average surface ("fsaverage"), that can be used in ``pycortex``.
173173
#
174-
# If you are running the notebook on Colab, you might need to update the
175-
# pycortex filestore as following:
174+
# First, let's download the "fsaverage" surface.
176175

177176
import cortex
178-
try:
179-
import google.colab # noqa
180-
in_colab = True
181-
except ImportError:
182-
in_colab = False
183-
print(in_colab)
184-
185-
if in_colab:
186-
filestore = cortex.options.config['basic']['filestore']
187-
cortex.database.db = cortex.database.Database(filestore)
188-
cortex.db = cortex.database.db
189-
cortex.utils.db = cortex.database.db
190-
cortex.dataset.braindata.db = cortex.database.db
191-
cortex.quickflat.utils.db = cortex.database.db
192-
cortex.quickflat.composite.db = cortex.database.db
193-
194-
###############################################################################
195-
# Now, let's download the "fsaverage" surface.
196177

197178
surface = "fsaverage"
198179

199180
if not hasattr(cortex.db, surface):
200-
cortex.utils.download_subject(subject_id=surface)
201-
# reinitialize pycortex's database to update available subjects
202-
filestore = cortex.options.config['basic']['filestore']
203-
cortex.database.db = cortex.database.Database(filestore)
204-
cortex.db = cortex.database.db
205-
cortex.utils.db = cortex.database.db
206-
cortex.dataset.braindata.db = cortex.database.db
207-
cortex.quickflat.utils.db = cortex.database.db
208-
cortex.quickflat.composite.db = cortex.database.db
181+
cortex.utils.download_subject(subject_id=surface,
182+
pycortex_store=cortex.db.filestore)
183+
cortex.db._subjects = None # force filestore reload
184+
assert hasattr(cortex.db, surface)
209185

210186
###############################################################################
211187
# Then, we load the "fsaverage" mapper. The mapper is a matrix of shape
@@ -228,11 +204,17 @@
228204

229205
###############################################################################
230206
# To start an interactive 3D viewer in the browser, we can use the ``webshow``
231-
# function in pycortex.
232-
# If you are running the notebook on Colab, you first need to tunnel the pycortex
233-
# application out of Colab. To do so, use the following cell to start a tunnel
234-
# with ``ngrok`` and to get an address where the pycortex viewer will be made
235-
# accessible.
207+
# function in pycortex. If you are running the notebook on Colab, you first
208+
# need to tunnel the pycortex application out of Colab. To do so, use the
209+
# following cell to start a tunnel with ``ngrok`` and to get an address where
210+
# the pycortex viewer will be made accessible.
211+
212+
try:
213+
import google.colab # noqa
214+
in_colab = True
215+
except ImportError:
216+
in_colab = False
217+
print(in_colab)
236218

237219
if in_colab:
238220
from IPython import get_ipython
@@ -248,7 +230,6 @@
248230
f"{result}\n"
249231
"and not the one proposed by pycortex ('Open viewer: ...')\n")
250232

251-
252233
###############################################################################
253234
# Now you can start an interactive 3D viewer by changing ``run_webshow`` to
254235
# ``True`` and running the following cell. If you are using Colab, remember to
@@ -272,7 +253,6 @@
272253
with_rois=has_installed("inkscape"))
273254
plt.show()
274255

275-
276256
###############################################################################
277257
# References
278258
# ----------

tutorials/notebooks/movies_3T/00_download_vim5.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
"name": "python",
101101
"nbconvert_exporter": "python",
102102
"pygments_lexer": "ipython3",
103-
"version": "3.8.3"
103+
"version": "3.7.3"
104104
}
105105
},
106106
"nbformat": 4,

tutorials/notebooks/movies_3T/00_load_colab.ipynb

Lines changed: 0 additions & 104 deletions
This file was deleted.

tutorials/notebooks/movies_3T/00_setup_colab.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
},
7777
"outputs": [],
7878
"source": [
79-
"import os\nos.environ['VOXELWISE_TUTORIALS_DATA'] = \"/content\"\n\nimport cortex\nfilestore = \"/content/pycortex/filestore/\"\ncortex.options.config['basic']['filestore'] = filestore\ncortex.options.config['webgl']['colormaps'] = \"/content/pycortex/filestore/colormaps\"\ncortex.database.db = cortex.database.Database(filestore)\ncortex.db = cortex.database.db\ncortex.utils.db = cortex.database.db\ncortex.dataset.braindata.db = cortex.database.db\n\nimport sklearn\nsklearn.set_config(assume_finite=True)"
79+
"import os\nos.environ['VOXELWISE_TUTORIALS_DATA'] = \"/content\"\n\nimport cortex\ncortex.db.__init__(\"/content/pycortex/filestore/\") # change filestore inplace\ncortex.options.config['webgl']['colormaps'] = \"/content/pycortex/filestore/colormaps\"\n\nimport sklearn\nsklearn.set_config(assume_finite=True)"
8080
]
8181
},
8282
{
@@ -103,7 +103,7 @@
103103
"name": "python",
104104
"nbconvert_exporter": "python",
105105
"pygments_lexer": "ipython3",
106-
"version": "3.8.3"
106+
"version": "3.7.3"
107107
}
108108
},
109109
"nbformat": 4,

tutorials/notebooks/movies_3T/01_plot_explainable_variance.ipynb

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@
170170
"cell_type": "markdown",
171171
"metadata": {},
172172
"source": [
173-
"We see that many voxels have low explainable variance. This is \nexpected, since many voxels are not driven by a visual stimulus, and their\nresponse changes over repeats of the same stimulus.\nWe also see that some voxels have high explainable variance (around 0.7). The\nresponses in these voxels are highly consistent across repetitions of the\nsame stimulus. Thus, they are good targets for encoding models.\n\n"
173+
"We see that many voxels have low explainable variance. This is\nexpected, since many voxels are not driven by a visual stimulus, and their\nresponse changes over repeats of the same stimulus.\nWe also see that some voxels have high explainable variance (around 0.7). The\nresponses in these voxels are highly consistent across repetitions of the\nsame stimulus. Thus, they are good targets for encoding models.\n\n"
174174
]
175175
},
176176
{
@@ -209,7 +209,7 @@
209209
"cell_type": "markdown",
210210
"metadata": {},
211211
"source": [
212-
"## Map to \"fsaverage\"\n\nThe second mapper we provide maps the voxel data to a Freesurfer\naverage surface (\"fsaverage\"), that can be used in ``pycortex``.\n\nIf you are running the notebook on Colab, you might need to update the\npycortex filestore as following:\n\n"
212+
"## Map to \"fsaverage\"\n\nThe second mapper we provide maps the voxel data to a Freesurfer\naverage surface (\"fsaverage\"), that can be used in ``pycortex``.\n\nFirst, let's download the \"fsaverage\" surface.\n\n"
213213
]
214214
},
215215
{
@@ -220,25 +220,7 @@
220220
},
221221
"outputs": [],
222222
"source": [
223-
"import cortex\ntry:\n import google.colab # noqa\n in_colab = True\nexcept ImportError:\n in_colab = False\nprint(in_colab)\n\nif in_colab:\n filestore = cortex.options.config['basic']['filestore']\n cortex.database.db = cortex.database.Database(filestore)\n cortex.db = cortex.database.db\n cortex.utils.db = cortex.database.db\n cortex.dataset.braindata.db = cortex.database.db\n cortex.quickflat.utils.db = cortex.database.db\n cortex.quickflat.composite.db = cortex.database.db"
224-
]
225-
},
226-
{
227-
"cell_type": "markdown",
228-
"metadata": {},
229-
"source": [
230-
"Now, let's download the \"fsaverage\" surface.\n\n"
231-
]
232-
},
233-
{
234-
"cell_type": "code",
235-
"execution_count": null,
236-
"metadata": {
237-
"collapsed": false
238-
},
239-
"outputs": [],
240-
"source": [
241-
"surface = \"fsaverage\"\n\nif not hasattr(cortex.db, surface):\n cortex.utils.download_subject(subject_id=surface)\n # reinitialize pycortex's database to update available subjects\n filestore = cortex.options.config['basic']['filestore']\n cortex.database.db = cortex.database.Database(filestore)\n cortex.db = cortex.database.db\n cortex.utils.db = cortex.database.db\n cortex.dataset.braindata.db = cortex.database.db\n cortex.quickflat.utils.db = cortex.database.db\n cortex.quickflat.composite.db = cortex.database.db"
223+
"import cortex\n\nsurface = \"fsaverage\"\n\nif not hasattr(cortex.db, surface):\n cortex.utils.download_subject(subject_id=surface,\n pycortex_store=cortex.db.filestore)\n cortex.db._subjects = None # force filestore reload\n assert hasattr(cortex.db, surface)"
242224
]
243225
},
244226
{
@@ -281,7 +263,7 @@
281263
"cell_type": "markdown",
282264
"metadata": {},
283265
"source": [
284-
"To start an interactive 3D viewer in the browser, we can use the ``webshow``\nfunction in pycortex.\nIf you are running the notebook on Colab, you first need to tunnel the pycortex\napplication out of Colab. To do so, use the following cell to start a tunnel\nwith ``ngrok`` and to get an address where the pycortex viewer will be made\naccessible.\n\n"
266+
"To start an interactive 3D viewer in the browser, we can use the ``webshow``\nfunction in pycortex. If you are running the notebook on Colab, you first\nneed to tunnel the pycortex application out of Colab. To do so, use the\nfollowing cell to start a tunnel with ``ngrok`` and to get an address where\nthe pycortex viewer will be made accessible.\n\n"
285267
]
286268
},
287269
{
@@ -292,7 +274,7 @@
292274
},
293275
"outputs": [],
294276
"source": [
295-
"if in_colab:\n from IPython import get_ipython\n get_ipython().system_raw('./ngrok http 8050 &')\n plt.pause(1)\n\n command = \"\"\"\n curl -s http://localhost:4040/api/tunnels | python3 -c \\\n \"import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])\"\n \"\"\"\n result = get_ipython().getoutput(command, split=True)\n print(\"Use the following address to connect to the brain viewer:\\n\"\n f\"{result}\\n\"\n \"and not the one proposed by pycortex ('Open viewer: ...')\\n\")"
277+
"try:\n import google.colab # noqa\n in_colab = True\nexcept ImportError:\n in_colab = False\nprint(in_colab)\n\nif in_colab:\n from IPython import get_ipython\n get_ipython().system_raw('./ngrok http 8050 &')\n plt.pause(1)\n\n command = \"\"\"\n curl -s http://localhost:4040/api/tunnels | python3 -c \\\n \"import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])\"\n \"\"\"\n result = get_ipython().getoutput(command, split=True)\n print(\"Use the following address to connect to the brain viewer:\\n\"\n f\"{result}\\n\"\n \"and not the one proposed by pycortex ('Open viewer: ...')\\n\")"
296278
]
297279
},
298280
{
@@ -355,7 +337,7 @@
355337
"name": "python",
356338
"nbconvert_exporter": "python",
357339
"pygments_lexer": "ipython3",
358-
"version": "3.8.3"
340+
"version": "3.7.3"
359341
}
360342
},
361343
"nbformat": 4,

tutorials/notebooks/movies_3T/02_plot_wordnet_model.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@
653653
"name": "python",
654654
"nbconvert_exporter": "python",
655655
"pygments_lexer": "ipython3",
656-
"version": "3.8.3"
656+
"version": "3.7.3"
657657
}
658658
},
659659
"nbformat": 4,

tutorials/notebooks/movies_3T/03_plot_hemodynamic_response.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@
352352
"name": "python",
353353
"nbconvert_exporter": "python",
354354
"pygments_lexer": "ipython3",
355-
"version": "3.8.3"
355+
"version": "3.7.3"
356356
}
357357
},
358358
"nbformat": 4,

tutorials/notebooks/movies_3T/04_plot_motion_energy_model.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@
341341
"name": "python",
342342
"nbconvert_exporter": "python",
343343
"pygments_lexer": "ipython3",
344-
"version": "3.8.3"
344+
"version": "3.7.3"
345345
}
346346
},
347347
"nbformat": 4,

tutorials/notebooks/movies_3T/05_plot_banded_ridge_model.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@
510510
"name": "python",
511511
"nbconvert_exporter": "python",
512512
"pygments_lexer": "ipython3",
513-
"version": "3.8.3"
513+
"version": "3.7.3"
514514
}
515515
},
516516
"nbformat": 4,

0 commit comments

Comments
 (0)