Skip to content

Instantly share code, notes, and snippets.

@arokem
Created June 5, 2024 23:30
Show Gist options
  • Save arokem/167666bf4017a740a5321f97f38718d5 to your computer and use it in GitHub Desktop.
Save arokem/167666bf4017a740a5321f97f38718d5 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os.path as op\n",
"import awkward as ak\n",
"import vector\n",
"from dipy.io.streamline import load_tractogram\n",
"import numpy as np\n",
"import boto3"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"vector.register_awkward()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"s3 = boto3.client('s3')\n",
"subject = 100206\n",
"clean_fname = (f\"sub-{subject}_dwi_space-RASMM_model-CSD_desc-prob-afq-clean_tractography\")\n",
"if not op.exists(\"example.trk\"):\n",
" s3.download_file(\n",
" \"open-neurodata\",f\"rokem/hcp1200/afq/sub-{subject}/ses-01/{clean_fname}.trk\",\n",
" \"example.trk\")\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"trk = load_tractogram(\"example.trk\", reference=\"same\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14743325"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.sum(trk.streamlines._lengths)\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"((14743325, 3), (54477,), 14742970)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"trk.streamlines._data.shape, np.diff(trk.streamlines._offsets).shape, np.sum(np.diff(trk.streamlines._offsets))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"trk_ak = ak.unflatten(trk.streamlines._data, trk.streamlines._lengths)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"trk_ak = ak.zip({\"x\": trk_ak[:, :, 0], \"y\": trk_ak[:, :, 1], \"z\": trk_ak[:, :, 2]}, with_name=\"Vector3D\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# Is there any way to do this with a collection of points (instead of just one)?\n",
"is_close = ak.argmin(abs(trk_ak - vector.VectorObject3D(x=0, y=0, z=0)) < 10, axis=1)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(<Array [31678, 35741, 35745, 36700, ..., 37326, 37425, 54294] type='9 * int64'>,)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.where(is_close)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/arokem/miniconda3/envs/awkward/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"from dipy.segment.bundles import bundles_distances_mdf"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# Can we speed this up with Awkward?\n",
"distances = bundles_distances_mdf(trk.streamlines, trk.streamlines)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "awkward",
"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.11.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment