{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n# BUAN Bundle Shape Similarity Score\n\nThis example explains how we can use BUAN [Chandio2020]_ to calculate shape\nsimilarity between two given bundles. Where, shape similarity score of 1 means\ntwo bundles are extremely close in shape and 0 implies no shape similarity\nwhatsoever.\n\nShape similarity score can be used to compare populations or individuals.\nIt can also serve as a quality assurance metric, to validate streamline\nregistration quality, bundle extraction quality by calculating output with a\nreference bundle or other issues with pre-processing by calculating shape\ndissimilarity with a reference bundle.\n\nFirst import the necessary modules.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\nfrom dipy.viz import window, actor\nfrom dipy.segment.bundles import bundle_shape_similarity\nfrom dipy.segment.bundles import select_random_set_of_streamlines\nfrom dipy.data import two_cingulum_bundles" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To show the concept we will use two pre-saved cingulum bundle.\nLet's start by fetching the data.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "cb_subj1, _ = two_cingulum_bundles()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's create two streamline sets (bundles) from same bundle cb_subj1 by\nrandomly selecting 60 streamlines two times.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "rng = np.random.RandomState()\nbundle1 = select_random_set_of_streamlines(cb_subj1, 60, rng=None)\nbundle2 = select_random_set_of_streamlines(cb_subj1, 60, rng=None)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, let's visualize two bundles.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def show_both_bundles(bundles, colors=None, show=True, fname=None):\n\n scene = window.Scene()\n scene.SetBackground(1., 1, 1)\n for (i, bundle) in enumerate(bundles):\n color = colors[i]\n streamtube_actor = actor.streamtube(bundle, color, linewidth=0.3)\n streamtube_actor.RotateX(-90)\n streamtube_actor.RotateZ(90)\n scene.add(streamtube_actor)\n if show:\n window.show(scene)\n if fname is not None:\n window.record(scene, n_frames=1, out_path=fname, size=(900, 900))\n\n\nshow_both_bundles([bundle1, bundle2], colors=[(1, 0, 0), (0, 1, 0)],\n show=False, fname=\"two_bundles.png\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. figure:: two_bundles.png\n :align: center\n\n Two Cingulum Bundles.\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate shape similarity score between two bundles.\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "0 cluster_thr because we want to use all streamlines and not the centroids of\nclusters.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "clust_thr = [0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Threshold indicates how strictly we want two bundles to be similar in shape.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "threshold = 5\n\nba_score = bundle_shape_similarity(bundle1, bundle2, rng, clust_thr, threshold)\nprint(\"Shape similarity score = \", ba_score)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's change the value of threshold to 10.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "threshold = 10\n\nba_score = bundle_shape_similarity(bundle1, bundle2, rng, clust_thr, threshold)\nprint(\"Shape similarity score = \", ba_score)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Higher value of threshold gives us higher shape similarity score as it is\nmore lenient.\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## References\n\n.. [Chandio2020] Chandio, B.Q., Risacher, S.L., Pestilli, F.,\n Bullock, D., Yeh, FC., Koudoro, S., Rokem, A., Harezlak, J., and\n Garyfallidis, E. Bundle analytics, a computational framework for\n investigating the shapes and profiles of brain pathways across\n populations. Sci Rep 10, 17149 (2020)\n\n\n" ] } ], "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.9.13" } }, "nbformat": 4, "nbformat_minor": 0 }