{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/Users/plab/Documents/gitDir/WMA_pyTools\n" ] } ], "source": [ "cd /Users/plab/Documents/gitDir/WMA_pyTools\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import WMA_pyFuncs\n", "import nibabel as nib\n", "import numpy as np\n", "from scipy.spatial.distance import cdist\n", "import itertools\n", " \n", " \n", "from dipy.segment.clustering import QuickBundles\n", "from dipy.segment.metric import ResampleFeature\n", "from dipy.segment.metric import AveragePointwiseEuclideanMetric" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "#streamlines=nib.streamlines.load('/Users/plab/Documents/ipynb/exampleData/smallTractogram.tck')\n", "streamlines=nib.streamlines.load('/Users/plab/Downloads/5c5d35e2f5d2a10052842847/track.tck')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "neckClusters=WMA_pyFuncs.neckmentation(streamlines.streamlines)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#get tractogram from the Tck holder\n", "sourceTractogram=streamlines.tractogram\n", "\n", "#quick and dirty tractogram subsetter by Brad Caron\n", "#https://github.com/bacaron\n", "def extractSubTractogram(sourceTractogram,indexes):\n", " #import relevant package\n", " import nibabel as nib\n", " #extrect the desired streamlines into a new streamline object\n", " streamlines = sourceTractogram.streamlines[indexes]\n", " #establish tractogram object\n", " out_tractogram = nib.streamlines.tractogram.Tractogram(streamlines)\n", " #adjust the relevant header fields\n", " #don't bother for now, header is only relevant to Tck file\n", " #for headerFields in ['total_count','count','nb_streamlines']:\n", " #nb_streamlines is an int, whereas the others are strings, for some reason\n", " # if headerFields == 'nb_streamlines':\n", " # out_tractogram.header[headerFields] = len(streamlines)\n", " # else:\n", " # out_tractogram.header[headerFields] = '%s' %len(streamlines)\n", " return out_tractogram\n", "\n", "#interactive plotting via niwidgets? \n", "#widget within a widget doesn't seem to work\n", "def plotParcellationConnectionWidget(subTractogram):\n", " #import widget\n", " from niwidgets import StreamlineWidget\n", " #set widget object\n", " \n", " sw = StreamlineWidget(streamlines=subTractogram)\n", " #set plotting characteristics\n", " style = {'axes': {'color': 'red',\n", " 'label': {'color': 'white'},\n", " 'ticklabel': {'color': 'white'},\n", " 'visible': False},\n", " 'background-color': 'black',\n", " 'box': {'visible': False}}\n", " #plot it\n", " sw.plot(display_fraction=1, width=500, height=500, style=style, percentile=80)\n", "\n", "def plotTract(tractIn):\n", " import numpy as np\n", " from dipy.viz import window, actor\n", " renderer = window.Scene()\n", " stream_actor = actor.line(tractIn)\n", " #renderer.set_camera(position=(-176.42, 118.52, 128.20),\n", " # focal_point=(113.30, 128.31, 76.56),\n", " # view_up=(0.18, 0.00, 0.98))\n", " %matplotlib inline\n", " renderer.add(stream_actor)\n", " \n", " window.show(renderer, size=(600, 600), reset_camera=True)\n", "\n", "def updateFunction(clusterIndex):\n", " \n", " currentIndexes=neckClusters.clusters[clusterIndex].indices\n", " subTractogram=extractSubTractogram(sourceTractogram,currentIndexes)\n", " %matplotlib inline\n", " plotParcellationConnectionWidget(subTractogram.streamlines)\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "streamCountVec=np.zeros(len(neckClusters)) \n", "for iClusters in range(len(neckClusters)):\n", " streamCountVec[iClusters]=len(neckClusters.clusters[iClusters].indices)\n", "print(np.argsort(streamCountVec))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import interact, interactive, fixed, interact_manual\n", "from ipywidgets import IntSlider\n", "\n", "#establish interactivity\n", "interact(updateFunction, \n", " clusterIndex=IntSlider(min=0, max=len(neckClusters), step=1,continuous_update=False)\n", " )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.7.5" } }, "nbformat": 4, "nbformat_minor": 2 }