Skip to content

Instantly share code, notes, and snippets.

@jasongrout
Created April 3, 2017 13:47
Show Gist options
  • Save jasongrout/2ae29e9d2a84c4f06cb664e5df229273 to your computer and use it in GitHub Desktop.
Save jasongrout/2ae29e9d2a84c4f06cb664e5df229273 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"import mne\n",
"import nibabel\n",
"\n",
"# TODO:\n",
"# 1. FIX .clim setting breakage\n",
"# 2. add mne_analyze_colormap as colormap\n",
"# 3. add brain behind STC\n",
"\n",
"data_path = mne.datasets.sample.data_path()\n",
"subjects_dir = data_path + '/subjects'\n",
"subj_dir = subjects_dir + '/sample/surf/'\n",
"stc = mne.read_source_estimate(data_path + '/MEG/sample/sample_audvis-meg-eeg')\n",
"stc.crop(0.09, 0.09)\n",
"stc = stc.morph('sample', grade=None, smooth=10, subjects_dir=subjects_dir,\n",
" subject_from='sample')\n",
"\n",
"azimuths = dict(lh=dict(lat=270, med=90), rh=dict(lat=90, med=270))\n",
"hemis = ['lh']\n",
"views = ['lat']\n",
"hemi_data = dict(lh=stc.data[:len(stc.vertices[0]), 0],\n",
" rh=stc.data[len(stc.vertices[0]):, 0])\n",
"\n",
"for hi, hemi in enumerate(hemis):\n",
" data = hemi_data[hemi]\n",
" data /= 23\n",
" surf = subj_dir + '%s.inflated' % hemi\n",
" curv = subj_dir + '%s.curv' % hemi\n",
" rr, tris = nibabel.freesurfer.read_geometry(surf)\n",
" tris = tris.astype(np.uint32)\n",
" assert len(data) == len(rr)\n",
" curv = nibabel.freesurfer.read_morph_data(curv)\n",
" curv = (curv > 0).astype(float)\n",
" curv = (curv - 0.5) / 3 + 0.5\n",
" curv = curv[:, np.newaxis] * [1, 1, 1]\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rr.shape, tris.shape, curv.shape"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from pythreejs import *\n",
"import numpy as np\n",
"from IPython.display import display"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"vertices = rr\n",
"faces = tris\n",
"vertexcolors = curv\n",
"geom = PlainBufferGeometry(vertices=vertices, faces=faces, colors=vertexcolors)\n",
"mesh = Mesh(geometry=geom, material=PhongMaterial(shininess=0,vertexColors='VertexColors'))\n",
"camera = PerspectiveCamera(position=[500, 500, 500], fov=20,\n",
" children=[DirectionalLight(color='#ffffff',\n",
" position=[-3, 5, 1],\n",
" intensity=0.5)])\n",
"scene = Scene(children=[mesh, AmbientLight(color='#dddddd')])\n",
"\n",
"renderer = Renderer(camera=camera, background='black', background_opacity=1,\n",
" scene=scene, controls=[OrbitControls(controlling=camera)])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"display(renderer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"norm = np.linalg.norm(vertices, axis=1)\n",
"norm /= norm.max()\n",
"def color(n):\n",
" newcolor=curv.copy()\n",
" newcolor[:,0] = np.abs(np.sin(norm*n))\n",
" newcolor[:,2] = np.abs(np.cos(norm*n))\n",
" geom.colors = newcolor"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"color(5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from ipywidgets import FloatSlider, VBox\n",
"x = FloatSlider(min=0, max=12)\n",
"def slide(change):\n",
" color(x.value)\n",
"x.observe(slide)\n",
"display(VBox([x, renderer]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"camera.position"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment