From cb6103a7c13445b005163070bc8ffa0211e9dde7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Hendrik=20M=C3=BCller?= <44469195+kolibril13@users.noreply.github.com> Date: Wed, 7 Aug 2024 22:04:05 +0200 Subject: [PATCH 1/9] add notebooks --- examples/01_download_all_data.ipynb | 228 ++++++++++++++++++ examples/additive_voxels.ipynb | 92 +++---- examples/basic_multiplanar.ipynb | 73 +++--- examples/mesh_and_volume.ipynb | 62 +++++ examples/mesh_layers.ipynb | 70 ++---- ...eeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb | 57 +++++ examples/trajectory.ipynb | 46 ++++ 7 files changed, 494 insertions(+), 134 deletions(-) create mode 100644 examples/01_download_all_data.ipynb create mode 100644 examples/mesh_and_volume.ipynb create mode 100644 examples/meshes_(GIfTI, FreeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb create mode 100644 examples/trajectory.ipynb diff --git a/examples/01_download_all_data.ipynb b/examples/01_download_all_data.ipynb new file mode 100644 index 0000000..cf24b64 --- /dev/null +++ b/examples/01_download_all_data.ipynb @@ -0,0 +1,228 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Fetching contents from https://api.github.com/repos/niivue/niivue/contents/demos/images?ref=main...\n", + "Downloading AIL.nii.gz...\n", + "Downloading BrainMesh_ICBM152.lh.curv...\n", + "Downloading BrainMesh_ICBM152.lh.motor.mz3...\n", + "Downloading BrainMesh_ICBM152.lh.mz3...\n", + "Downloading CIT168.mz3...\n", + "Downloading ColumnMajorOrder.gii...\n", + "Downloading Conte69.L.inflated.32k_fs_LR.surf.gii...\n", + "Downloading Conte69.MyelinAndCorrThickness.32k_fs_LR.dtseries.nii...\n", + "Downloading Conte69.R.inflated.32k_fs_LR.surf.gii...\n", + "Downloading DoG.png...\n", + "Downloading FA.nii.gz...\n", + "Downloading FLAIR.nii.gz...\n", + "Downloading FLAIR.nrrd...\n", + "Downloading FreeSurferControlPoints.json...\n", + "Downloading Human.colin.Cerebral.R.VERY_INFLATED.71723.surf.gii...\n", + "Downloading Human.colin.R.FUNCTIONAL.71723.func.gii...\n", + "Downloading MNI152_2009_template.nii.gz...\n", + "Downloading MolView-sticks-color_38.x3d...\n", + "Downloading TR_S_R.tt.gz...\n", + "Downloading V1.nii.gz...\n", + "Downloading aal.json...\n", + "Downloading aal.mz3...\n", + "Downloading aal.nii.gz...\n", + "Entering directory afni...\n", + "Fetching contents from https://api.github.com/repos/niivue/niivue/contents/demos/images/afni?ref=main...\n", + "Downloading DT_FA+orig.BRIK.gz...\n", + "Downloading DT_FA+orig.HEAD...\n", + "Downloading DT_V1+orig.BRIK.gz...\n", + "Downloading DT_V1+orig.HEAD...\n", + "Downloading anat_final.FT+tlrc.BRIK...\n", + "Downloading anat_final.FT+tlrc.HEAD...\n", + "Downloading avg152T1_RL_nifti.nii.gz...\n", + "Downloading boggle.lh.annot...\n", + "Downloading cactus.nii.gz...\n", + "Downloading colby.trk...\n", + "Downloading colby.trx...\n", + "Downloading complex.nii.gz...\n", + "Downloading connectome.jcon...\n", + "Downloading connectome2.jcon...\n", + "Downloading cope1.nii.gz...\n", + "Downloading ct_perfusion.nii.gz...\n", + "Downloading dpsv.trx...\n", + "Downloading draw.nii.gz...\n", + "Downloading dsistudio.fib.gz...\n", + "Downloading dsistudio.src.gz...\n", + "Downloading enh.dcm...\n", + "Downloading example4d+orig.BRIK.gz...\n", + "Downloading example4d+orig.HEAD...\n", + "Entering directory fs...\n", + "Fetching contents from https://api.github.com/repos/niivue/niivue/contents/demos/images/fs?ref=main...\n", + "Downloading brainmask.mgz...\n", + "Downloading lh.pial...\n", + "Downloading lh.white...\n", + "Downloading rh.pial...\n", + "Downloading rh.white...\n", + "Downloading wm.mgz...\n", + "Downloading fs_LR.32k.L.inflated.surf.gii...\n", + "Downloading fs_LR.32k.LR.curvature.dscalar.nii...\n", + "Downloading fslmean.nii.gz...\n", + "Downloading fslt.nii.gz...\n", + "Downloading hippo.nii.gz...\n", + "Downloading hippolr.nii.gz...\n", + "Downloading inia19-NeuroMaps.json...\n", + "Downloading inia19-NeuroMaps.nii.gz...\n", + "Downloading inia19-t1-brain.nii.gz...\n", + "Downloading labels256.nii.gz...\n", + "Downloading lesion.nii.gz...\n", + "Downloading lh.Yeo2011.gii...\n", + "Downloading lh.Yeo2011.mgz...\n", + "Downloading lh.curv...\n", + "Downloading lh.pial...\n", + "Downloading low_res.nii.gz...\n", + "Downloading mean_func.nii.gz...\n", + "Downloading minimal.nii.gz...\n", + "Downloading mni152.SLF1_R.tsf...\n", + "Downloading mni152.nii.gz...\n", + "Downloading mni152_pveseg.nii.gz...\n", + "Downloading mni_format.obj.gz...\n", + "Downloading motor_4t95mesh.rh.mz3...\n", + "Entering directory mz3...\n", + "Fetching contents from https://api.github.com/repos/niivue/niivue/contents/demos/images/mz3?ref=main...\n", + "Downloading 11ScalarMesh.mz3...\n", + "Downloading 16DoubleOverlay_5124x2.mz3...\n", + "Downloading 3Mesh.mz3...\n", + "Downloading README.md...\n", + "Downloading cortex_5124.mz3...\n", + "Downloading narps-4735_50GV-hypo1_unthresh.nii.gz...\n", + "Downloading narps-4965_9U7M-hypo1_unthresh.nii.gz...\n", + "Downloading niivue-drawing.nvd...\n", + "Downloading niivue-with-clip-plane.nvd...\n", + "Downloading niivue.basic.nvd...\n", + "Downloading niivue.drawing.nvd...\n", + "Downloading niivue.mesh.nvd...\n", + "Downloading niivue1-no-hippo.nvd...\n", + "Downloading niivue1.nvd...\n", + "Downloading otsu.nii.gz...\n", + "Downloading pcasl.nii.gz...\n", + "Downloading pcasl.png...\n", + "Downloading pval.LH.nii.gz...\n", + "Downloading scaled+tlrc.BRIK.gz...\n", + "Downloading scaled+tlrc.HEAD...\n", + "Downloading shear.nii.gz...\n", + "Downloading simplify_brain.obj...\n", + "Downloading spmMotor.nii.gz...\n", + "Downloading stats.FT+tlrc.BRIK...\n", + "Downloading stats.FT+tlrc.HEAD...\n", + "Downloading stats.nv_demo_mskd.nii.gz...\n", + "Downloading sub-01_ses-01_dwi_desc-b0_dwi.nii.gz...\n", + "Downloading sub-01_ses-01_dwi_space-RASMM_model-probCSD_algo-AFQ_tractography.trx...\n", + "Downloading sub-test02_left_hemisphere.srf.gz...\n", + "Downloading sub-test02_left_hemisphere_4_curvature_maps.smp.gz...\n", + "Downloading template.mif.gz...\n", + "Downloading torso.nii.gz...\n", + "Downloading torsoLabel.nii.gz...\n", + "Downloading tract.FAT_R.vtk...\n", + "Downloading tract.IFOF_R.trk...\n", + "Downloading tract.SLF1_R.tck...\n", + "Entering directory trix...\n", + "Fetching contents from https://api.github.com/repos/niivue/niivue/contents/demos/images/trix?ref=main...\n", + "Downloading fa.mif...\n", + "Downloading mask_1bit.mif...\n", + "Downloading tensors.mif...\n", + "Downloading v1.mif...\n", + "Downloading tstat1.nii.gz...\n", + "Downloading vr_base_min_outlier+orig.BRIK...\n", + "Downloading vr_base_min_outlier+orig.HEAD...\n", + "Downloading wm.mgz...\n", + "Downloading xd.mz3...\n", + "Downloading yd.mz3...\n", + "Downloading yeh2022.trx...\n", + "Downloading zd.mz3...\n", + "All files and subdirectories have been downloaded to images.\n" + ] + } + ], + "source": [ + "# Import necessary libraries\n", + "import os\n", + "import requests\n", + "import json\n", + "\n", + "# GitHub API URL for the base folder\n", + "BASE_API_URL = \"https://api.github.com/repos/niivue/niivue/contents\"\n", + "FOLDER_PATH = \"demos/images\"\n", + "REF = \"main\"\n", + "\n", + "# Folder where files will be saved\n", + "DEST_FOLDER = \"images\"\n", + "\n", + "# Function to fetch and download files recursively\n", + "def fetch_and_download(api_url, dest_folder):\n", + " print(f\"Fetching contents from {api_url}...\")\n", + " os.makedirs(dest_folder, exist_ok=True)\n", + " response = requests.get(api_url)\n", + " if response.status_code != 200:\n", + " print(f\"Failed to fetch {api_url}: {response.status_code}\")\n", + " return\n", + " \n", + " file_list = response.json()\n", + " for item in file_list:\n", + " item_type = item['type']\n", + " download_url = item.get('download_url', '') if item_type == 'file' else ''\n", + " name = item['name']\n", + " path = item['path']\n", + " html_url = item['html_url']\n", + "\n", + " if item_type == 'file':\n", + " print(f\"Downloading {name}...\")\n", + " file_response = requests.get(download_url)\n", + " if file_response.status_code == 200:\n", + " with open(os.path.join(dest_folder, name), 'wb') as f:\n", + " f.write(file_response.content)\n", + " else:\n", + " print(f\"Failed to download {name}: {file_response.status_code}\")\n", + " elif item_type == 'dir':\n", + " print(f\"Entering directory {name}...\")\n", + " subfolder = os.path.join(dest_folder, name)\n", + " sub_api_url = f\"{BASE_API_URL}/{path}?ref={REF}\"\n", + " fetch_and_download(sub_api_url, subfolder)\n", + "\n", + "# Start the recursive downloading\n", + "fetch_and_download(f\"{BASE_API_URL}/{FOLDER_PATH}?ref={REF}\", DEST_FOLDER)\n", + "\n", + "print(f\"All files and subdirectories have been downloaded to {DEST_FOLDER}.\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "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.12.4" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/examples/additive_voxels.ipynb b/examples/additive_voxels.ipynb index a12d0c8..62df99a 100644 --- a/examples/additive_voxels.ipynb +++ b/examples/additive_voxels.ipynb @@ -2,35 +2,39 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], - "source": [ - "!mkdir -p ../images/\n", - "!wget https://github.com/niivue/niivue/raw/main/demos/images/narps-4735_50GV-hypo1_unthresh.nii.gz -P ../images/\n", - "!wget https://github.com/niivue/niivue/raw/main/demos/images/narps-4965_9U7M-hypo1_unthresh.nii.gz -P ../images/\n", - "!wget https://github.com/niivue/niivue/raw/main/demos/images/mni152.nii.gz -P ../images/" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "8e77b0fab1204e5191dbca06414c09a4", + "version_major": 2, + "version_minor": 1 + }, + "text/plain": [ + "NiiVue(height=300)" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "from ipyniivue import NiiVue, SliceType\n", "# based on https://niivue.github.io/niivue/features/additive.voxels.html\n", "\n", "volumes = [\n", - " { \"path\": \"../images/mni152.nii.gz\" },\n", + " { \"path\": \"images/mni152.nii.gz\" },\n", " {\n", - " \"path\": \"../images/narps-4965_9U7M-hypo1_unthresh.nii.gz\",\n", + " \"path\": \"images/narps-4965_9U7M-hypo1_unthresh.nii.gz\",\n", " \"colormap\": \"red\",\n", " \"cal_min\": 2,\n", " \"cal_max\": 4,\n", " },\n", " {\n", - " \"path\": \"../images/narps-4735_50GV-hypo1_unthresh.nii.gz\",\n", + " \"path\": \"images/narps-4735_50GV-hypo1_unthresh.nii.gz\",\n", " \"colormap\": \"green\",\n", " \"cal_min\": 2,\n", " \"cal_max\": 4,\n", @@ -47,9 +51,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "cb5b42ba9d774dd69a613e644326eb2f", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "HBox(children=(FloatSlider(value=0.2, max=0.4, min=0.1, step=0.01), FloatSlider(value=0.2, max=0.4, min=0.1, s…" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import ipywidgets\n", "\n", @@ -60,34 +80,16 @@ "ipywidgets.link((sgreen, \"value\"), (nv.volumes[2], \"cal_min\"))\n", "ipywidgets.HBox([sred, sgreen])" ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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" - }, - "widgets": { - "application/vnd.jupyter.widget-state+json": { - "state": {}, - "version_major": 2, - "version_minor": 0 - } + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } - }, + ], + "metadata": {}, "nbformat": 4, "nbformat_minor": 4 } diff --git a/examples/basic_multiplanar.ipynb b/examples/basic_multiplanar.ipynb index 5408122..acf8a4d 100644 --- a/examples/basic_multiplanar.ipynb +++ b/examples/basic_multiplanar.ipynb @@ -2,27 +2,32 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], - "source": [ - "!mkdir -p ../images/\n", - "!wget https://github.com/niivue/niivue/raw/main/demos/images/mni152.nii.gz -P ../images/\n", - "!wget https://github.com/niivue/niivue/raw/main/demos/images/hippo.nii.gz -P ../images/" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "2f34ccf232f34ed784a01d32e5ae570a", + "version_major": 2, + "version_minor": 1 + }, + "text/plain": [ + "NiiVue(height=300)" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "from ipyniivue import NiiVue, SliceType\n", "# based on https://niivue.github.io/niivue/features/basic.multiplanar.html\n", "\n", "volumes = [\n", - " { \"path\": \"../images/mni152.nii.gz\", \"colormap\": \"gray\", \"visible\": True, \"opacity\": 1.0 },\n", - " { \"path\": \"../images/hippo.nii.gz\", \"colormap\": \"red\", \"visible\": True, \"opacity\": 1 },\n", + " { \"path\": \"images/mni152.nii.gz\", \"colormap\": \"gray\", \"visible\": True, \"opacity\": 1.0 },\n", + " { \"path\": \"images/hippo.nii.gz\", \"colormap\": \"red\", \"visible\": True, \"opacity\": 1 },\n", "\n", "]\n", "nv = NiiVue(slice_type=SliceType.MULTIPLANAR)\n", @@ -32,7 +37,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -42,7 +47,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -53,34 +58,16 @@ "nv.is_colorbar = True\n", "nv.view_mode_hot_key = \"KeyN\"" ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.12.3" - }, - "widgets": { - "application/vnd.jupyter.widget-state+json": { - "state": {}, - "version_major": 2, - "version_minor": 0 - } + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] } - }, + ], + "metadata": {}, "nbformat": 4, "nbformat_minor": 4 } diff --git a/examples/mesh_and_volume.ipynb b/examples/mesh_and_volume.ipynb new file mode 100644 index 0000000..5ffb515 --- /dev/null +++ b/examples/mesh_and_volume.ipynb @@ -0,0 +1,62 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "id": "71c2f1ec-fdbf-4aae-8d09-77fd5988e11c", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "b19c2298162e4e06b73f44b43f747ba9", + "version_major": 2, + "version_minor": 1 + }, + "text/plain": [ + "NiiVue(height=300)" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from ipyniivue import NiiVue, SliceType\n", + "# based on https://github.com/niivue/ipyniivue/blob/main/original_gallery.md#tractography-tck-trk-trx-vtk\n", + "\n", + "volumes = [\n", + " { \"path\": \"images/mni152.nii.gz\", \"colormap\": \"gray\", \"visible\": True, \"opacity\": 1.0 },\n", + "\n", + "]\n", + "\n", + "nv = NiiVue(slice_type=SliceType.MULTIPLANAR)\n", + "nv.load_volumes(volumes)\n", + "\n", + "nv.load_meshes(\n", + " [\n", + " {\n", + " \"path\": \"images/BrainMesh_ICBM152.lh.mz3\",\n", + " \"rgba255\": [255, 255, 255, 255],\n", + " }\n", + " ]\n", + ")\n", + "\n", + "\n", + "nv" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "593d466e-6bf6-47df-ba63-23b627f41da0", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/examples/mesh_layers.ipynb b/examples/mesh_layers.ipynb index baa2f05..f545c5d 100644 --- a/examples/mesh_layers.ipynb +++ b/examples/mesh_layers.ipynb @@ -2,23 +2,26 @@ "cells": [ { "cell_type": "code", - "execution_count": null, - "id": "6606a337-3243-4041-a7b7-ec64136f245c", - "metadata": {}, - "outputs": [], - "source": [ - "!mkdir -p ../images/\n", - "!wget https://github.com/niivue/niivue/raw/main/demos/images/BrainMesh_ICBM152.lh.mz3 -P ../images/\n", - "!wget https://github.com/niivue/niivue/raw/main/demos/images/CIT168.mz3 -P ../images/\n", - "!wget https://github.com/niivue/niivue/raw/main/demos/images/BrainMesh_ICBM152.lh.motor.mz3 -P ../images/" - ] - }, - { - "cell_type": "code", - "execution_count": null, + "execution_count": 4, "id": "36d80248-e8a6-4f27-92c5-98dabf2aec81", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "17ebee0428a44ef5a75fb9f9319c8764", + "version_major": 2, + "version_minor": 1 + }, + "text/plain": [ + "NiiVue(height=300)" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "from ipyniivue import NiiVue\n", "\n", @@ -27,7 +30,7 @@ ")\n", "\n", "mesh_layer = {\n", - " \"path\": \"../images/BrainMesh_ICBM152.lh.motor.mz3\",\n", + " \"path\": \"images/BrainMesh_ICBM152.lh.motor.mz3\",\n", " \"cal_min\": 0.5,\n", " \"cal_max\": 5.5,\n", " \"useNegativeCmap\": True,\n", @@ -37,11 +40,11 @@ "nv.load_meshes(\n", " [\n", " {\n", - " \"path\": \"../images/BrainMesh_ICBM152.lh.mz3\",\n", + " \"path\": \"images/BrainMesh_ICBM152.lh.mz3\",\n", " \"rgba255\": [255, 255, 255, 255],\n", " \"layers\": [mesh_layer],\n", " },\n", - " {\"path\": \"../images/CIT168.mz3\", \"rgba255\": [0, 0, 255, 255]},\n", + " {\"path\": \"images/CIT168.mz3\", \"rgba255\": [0, 0, 255, 255]},\n", " ]\n", ")\n", "\n", @@ -50,7 +53,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "956fe2ef-3180-44cc-a81e-05efc783ee05", "metadata": {}, "outputs": [], @@ -61,38 +64,13 @@ { "cell_type": "code", "execution_count": null, - "id": "9b204adc-74a2-4091-be4f-2e0dd888d4c6", + "id": "69a4c0b3", "metadata": {}, "outputs": [], "source": [] } ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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" - }, - "widgets": { - "application/vnd.jupyter.widget-state+json": { - "state": {}, - "version_major": 2, - "version_minor": 0 - } - } - }, + "metadata": {}, "nbformat": 4, "nbformat_minor": 5 } diff --git a/examples/meshes_(GIfTI, FreeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb b/examples/meshes_(GIfTI, FreeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb new file mode 100644 index 0000000..f05a4cd --- /dev/null +++ b/examples/meshes_(GIfTI, FreeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb @@ -0,0 +1,57 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "05bb313319804b9fa0dd2166c1526597", + "version_major": 2, + "version_minor": 1 + }, + "text/plain": [ + "NiiVue(height=300)" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from ipyniivue import NiiVue\n", + "# based on https://github.com/niivue/ipyniivue/blob/main/original_gallery.md#meshes-gifti-freesurfer-mz3-obj-stl-legacy-vtk\n", + "nv = NiiVue(\n", + " show_3d_crosshair=True, back_color=(1, 1, 1, 1), mesh_x_ray=0.3, is_colorbar=True\n", + ")\n", + "\n", + "nv.load_meshes(\n", + " [\n", + " {\n", + " \"path\": \"images/BrainMesh_ICBM152.lh.mz3\",\n", + " \"rgba255\": [222, 164, 164, 255],\n", + " },\n", + " { \"path\": \"images/CIT168.mz3\", \"rgba255\": [0, 0, 255, 255] },\n", + " ]\n", + ")\n", + "\n", + "nv\n", + "\n", + "# nv1.setMeshShader(nv1.meshes[0].id, \"Outline\"); does not seem to exist yet." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/examples/trajectory.ipynb b/examples/trajectory.ipynb new file mode 100644 index 0000000..612b676 --- /dev/null +++ b/examples/trajectory.ipynb @@ -0,0 +1,46 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from ipyniivue import NiiVue, SliceType\n", + "# based on https://github.com/niivue/ipyniivue/blob/main/original_gallery.md#tractography-tck-trk-trx-vtk\n", + "\n", + "volumes = [\n", + " { \"path\": \"images/mni152.nii.gz\", \"colormap\": \"gray\", \"visible\": True, \"opacity\": 1.0 },\n", + "\n", + "]\n", + "\n", + "nv = NiiVue(slice_type=SliceType.MULTIPLANAR)\n", + "#nv.load_volumes(volumes)\n", + "\n", + "\n", + "nv.load_meshes(\n", + " [\n", + " {\n", + " \"path\": \"images/dpsv.trx\", # this does currently not load\n", + " \"rgba255\": [0, 142, 0, 255],\n", + " }\n", + " ]\n", + ")\n", + "\n", + "\n", + "\n", + "nv" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 2 +} From dbc249887db9c21b603466b3ac5b2bd85a0dddd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Hendrik=20M=C3=BCller?= <44469195+kolibril13@users.noreply.github.com> Date: Wed, 14 Aug 2024 15:34:10 +0200 Subject: [PATCH 2/9] add torso notebook --- examples/torso_regions.ipynb | 70 ++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 examples/torso_regions.ipynb diff --git a/examples/torso_regions.ipynb b/examples/torso_regions.ipynb new file mode 100644 index 0000000..8523244 --- /dev/null +++ b/examples/torso_regions.ipynb @@ -0,0 +1,70 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "id": "71c2f1ec-fdbf-4aae-8d09-77fd5988e11c", + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "fb1fdbd65e0845118d7955f94e149b24", + "version_major": 2, + "version_minor": 1 + }, + "text/plain": [ + "NiiVue(height=300)" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from ipyniivue import NiiVue, SliceType\n", + "# based on https://github.com/niivue/ipyniivue/blob/main/original_gallery.md#torso-regions\n", + "\n", + "volumes = [\n", + " { \"path\": \"images/torso.nii.gz\", \"colormap\": \"gray\", \"visible\": True, \"opacity\": 1.0 },\n", + "\n", + "]\n", + "\n", + "nv = NiiVue(slice_type=SliceType.MULTIPLANAR)\n", + "nv.load_volumes(volumes)\n", + "\n", + "nv" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "593d466e-6bf6-47df-ba63-23b627f41da0", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "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.12.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 2d1195669ce4dc13eee1e7706630c8d29ba49fd4 Mon Sep 17 00:00:00 2001 From: alford Date: Sat, 2 Nov 2024 01:24:38 -0400 Subject: [PATCH 3/9] rearranged file structure and fixed lint issues --- examples/01_download_all_data.ipynb | 14 +-- examples/additive_voxels.ipynb | 3 +- examples/basic_multiplanar.ipynb | 27 +++++- examples/mesh_and_volume.ipynb | 24 +++++- examples/mesh_layers.ipynb | 30 +++++-- ...eeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb | 23 ++++- examples/prototypes/torso_regions.ipynb | 86 +++++++++++++++++++ examples/{ => prototypes}/trajectory.ipynb | 26 +++++- examples/torso_regions.ipynb | 70 --------------- src/ipyniivue/_utils.py | 1 + 10 files changed, 210 insertions(+), 94 deletions(-) rename examples/{ => prototypes}/meshes_(GIfTI, FreeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb (74%) create mode 100644 examples/prototypes/torso_regions.ipynb rename examples/{ => prototypes}/trajectory.ipynb (65%) delete mode 100644 examples/torso_regions.ipynb diff --git a/examples/01_download_all_data.ipynb b/examples/01_download_all_data.ipynb index cf24b64..c2c261a 100644 --- a/examples/01_download_all_data.ipynb +++ b/examples/01_download_all_data.ipynb @@ -89,6 +89,7 @@ "Downloading mni152_pveseg.nii.gz...\n", "Downloading mni_format.obj.gz...\n", "Downloading motor_4t95mesh.rh.mz3...\n", + "Downloading mpld_asl.nii.gz...\n", "Entering directory mz3...\n", "Fetching contents from https://api.github.com/repos/niivue/niivue/contents/demos/images/mz3?ref=main...\n", "Downloading 11ScalarMesh.mz3...\n", @@ -148,8 +149,8 @@ "source": [ "# Import necessary libraries\n", "import os\n", + "\n", "import requests\n", - "import json\n", "\n", "# GitHub API URL for the base folder\n", "BASE_API_URL = \"https://api.github.com/repos/niivue/niivue/contents\"\n", @@ -159,22 +160,21 @@ "# Folder where files will be saved\n", "DEST_FOLDER = \"images\"\n", "\n", - "# Function to fetch and download files recursively\n", "def fetch_and_download(api_url, dest_folder):\n", + " \"\"\"Fetch and download files recursively.\"\"\"\n", " print(f\"Fetching contents from {api_url}...\")\n", " os.makedirs(dest_folder, exist_ok=True)\n", " response = requests.get(api_url)\n", " if response.status_code != 200:\n", " print(f\"Failed to fetch {api_url}: {response.status_code}\")\n", " return\n", - " \n", + "\n", " file_list = response.json()\n", " for item in file_list:\n", " item_type = item['type']\n", " download_url = item.get('download_url', '') if item_type == 'file' else ''\n", " name = item['name']\n", " path = item['path']\n", - " html_url = item['html_url']\n", "\n", " if item_type == 'file':\n", " print(f\"Downloading {name}...\")\n", @@ -206,7 +206,7 @@ ], "metadata": { "kernelspec": { - "display_name": ".venv", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -220,9 +220,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.4" + "version": "3.11.6" } }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/examples/additive_voxels.ipynb b/examples/additive_voxels.ipynb index 62df99a..fadb562 100644 --- a/examples/additive_voxels.ipynb +++ b/examples/additive_voxels.ipynb @@ -22,7 +22,8 @@ } ], "source": [ - "from ipyniivue import NiiVue, SliceType\n", + "from ipyniivue import NiiVue\n", + "\n", "# based on https://niivue.github.io/niivue/features/additive.voxels.html\n", "\n", "volumes = [\n", diff --git a/examples/basic_multiplanar.ipynb b/examples/basic_multiplanar.ipynb index acf8a4d..8b52b65 100644 --- a/examples/basic_multiplanar.ipynb +++ b/examples/basic_multiplanar.ipynb @@ -23,11 +23,14 @@ ], "source": [ "from ipyniivue import NiiVue, SliceType\n", + "\n", "# based on https://niivue.github.io/niivue/features/basic.multiplanar.html\n", "\n", "volumes = [\n", - " { \"path\": \"images/mni152.nii.gz\", \"colormap\": \"gray\", \"visible\": True, \"opacity\": 1.0 },\n", - " { \"path\": \"images/hippo.nii.gz\", \"colormap\": \"red\", \"visible\": True, \"opacity\": 1 },\n", + " { \"path\": \"images/mni152.nii.gz\", \"colormap\": \"gray\",\n", + " \"visible\": True, \"opacity\": 1.0 },\n", + " { \"path\": \"images/hippo.nii.gz\", \"colormap\": \"red\",\n", + " \"visible\": True, \"opacity\": 1 },\n", "\n", "]\n", "nv = NiiVue(slice_type=SliceType.MULTIPLANAR)\n", @@ -67,7 +70,25 @@ "source": [] } ], - "metadata": {}, + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.6" + } + }, "nbformat": 4, "nbformat_minor": 4 } diff --git a/examples/mesh_and_volume.ipynb b/examples/mesh_and_volume.ipynb index 5ffb515..22a3d45 100644 --- a/examples/mesh_and_volume.ipynb +++ b/examples/mesh_and_volume.ipynb @@ -24,10 +24,12 @@ ], "source": [ "from ipyniivue import NiiVue, SliceType\n", + "\n", "# based on https://github.com/niivue/ipyniivue/blob/main/original_gallery.md#tractography-tck-trk-trx-vtk\n", "\n", "volumes = [\n", - " { \"path\": \"images/mni152.nii.gz\", \"colormap\": \"gray\", \"visible\": True, \"opacity\": 1.0 },\n", + " { \"path\": \"images/mni152.nii.gz\", \"colormap\": \"gray\",\n", + " \"visible\": True, \"opacity\": 1.0 },\n", "\n", "]\n", "\n", @@ -56,7 +58,25 @@ "source": [] } ], - "metadata": {}, + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.6" + } + }, "nbformat": 4, "nbformat_minor": 5 } diff --git a/examples/mesh_layers.ipynb b/examples/mesh_layers.ipynb index f545c5d..d7f9b38 100644 --- a/examples/mesh_layers.ipynb +++ b/examples/mesh_layers.ipynb @@ -2,14 +2,14 @@ "cells": [ { "cell_type": "code", - "execution_count": 4, + "execution_count": 1, "id": "36d80248-e8a6-4f27-92c5-98dabf2aec81", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "17ebee0428a44ef5a75fb9f9319c8764", + "model_id": "f049f7486a904ee785cf1699cc6a02b6", "version_major": 2, "version_minor": 1 }, @@ -17,7 +17,7 @@ "NiiVue(height=300)" ] }, - "execution_count": 4, + "execution_count": 1, "metadata": {}, "output_type": "execute_result" } @@ -53,12 +53,12 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "id": "956fe2ef-3180-44cc-a81e-05efc783ee05", "metadata": {}, "outputs": [], "source": [ - "nv.meshes[0].rgba255 = [0, 0, 0, 0]" + "nv.meshes[0].rgba255 = [200, 200, 200, 0]" ] }, { @@ -70,7 +70,25 @@ "source": [] } ], - "metadata": {}, + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.6" + } + }, "nbformat": 4, "nbformat_minor": 5 } diff --git a/examples/meshes_(GIfTI, FreeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb b/examples/prototypes/meshes_(GIfTI, FreeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb similarity index 74% rename from examples/meshes_(GIfTI, FreeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb rename to examples/prototypes/meshes_(GIfTI, FreeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb index f05a4cd..55bcc9d 100644 --- a/examples/meshes_(GIfTI, FreeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb +++ b/examples/prototypes/meshes_(GIfTI, FreeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb @@ -8,7 +8,7 @@ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "05bb313319804b9fa0dd2166c1526597", + "model_id": "ab4d7045b8204843842d751f2bfe810f", "version_major": 2, "version_minor": 1 }, @@ -23,6 +23,7 @@ ], "source": [ "from ipyniivue import NiiVue\n", + "\n", "# based on https://github.com/niivue/ipyniivue/blob/main/original_gallery.md#meshes-gifti-freesurfer-mz3-obj-stl-legacy-vtk\n", "nv = NiiVue(\n", " show_3d_crosshair=True, back_color=(1, 1, 1, 1), mesh_x_ray=0.3, is_colorbar=True\n", @@ -51,7 +52,25 @@ "source": [] } ], - "metadata": {}, + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.6" + } + }, "nbformat": 4, "nbformat_minor": 4 } diff --git a/examples/prototypes/torso_regions.ipynb b/examples/prototypes/torso_regions.ipynb new file mode 100644 index 0000000..20edbdb --- /dev/null +++ b/examples/prototypes/torso_regions.ipynb @@ -0,0 +1,86 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "71c2f1ec-fdbf-4aae-8d09-77fd5988e11c", + "metadata": {}, + "outputs": [], + "source": [ + "from ipyniivue import NiiVue, SliceType\n", + "\n", + "# based on https://github.com/niivue/ipyniivue/blob/main/original_gallery.md#torso-regions\n", + "\n", + "volumes = [\n", + " { \"path\": \"images/torso.nii.gz\", \"colormap\": (0, 0, 0, 0),\n", + " \"visible\": True, \"opacity\": 1.0 },\n", + "\n", + "]\n", + "\n", + "nv = NiiVue(slice_type=SliceType.MULTIPLANAR)\n", + "nv.load_volumes(volumes)\n", + "\n", + "nv" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "593d466e-6bf6-47df-ba63-23b627f41da0", + "metadata": {}, + "outputs": [], + "source": [ + "cmap = {\n", + " \"R\": [0, 0, 185, 185, 252, 0, 103, 216, 127, 127, 0, 222],\n", + " \"G\": [0, 20, 102, 102, 0, 255, 76, 132, 0, 127, 255, 154],\n", + " \"B\": [0, 152, 83, 83, 0, 0, 71, 105, 127, 0, 255, 132],\n", + " \"A\": [0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],\n", + " \"labels\": [\n", + " \"background\",\n", + " \"1spleen\",\n", + " \"2kidneyR\",\n", + " \"3kidneyL\",\n", + " \"4gallbladder\",\n", + " \"5esophagus\",\n", + " \"6Liver\",\n", + " \"7stomach\",\n", + " \"8aorta\",\n", + " \"9inferiorvenacava\",\n", + " \"10pancreas\",\n", + " \"11bladder\",\n", + " ],\n", + " }\n", + "nv.setDrawColormap(cmap);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "94a1c95a-308b-4259-ac29-7512c48239b2", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/examples/trajectory.ipynb b/examples/prototypes/trajectory.ipynb similarity index 65% rename from examples/trajectory.ipynb rename to examples/prototypes/trajectory.ipynb index 612b676..c1b6f76 100644 --- a/examples/trajectory.ipynb +++ b/examples/prototypes/trajectory.ipynb @@ -7,10 +7,12 @@ "outputs": [], "source": [ "from ipyniivue import NiiVue, SliceType\n", + "\n", "# based on https://github.com/niivue/ipyniivue/blob/main/original_gallery.md#tractography-tck-trk-trx-vtk\n", "\n", "volumes = [\n", - " { \"path\": \"images/mni152.nii.gz\", \"colormap\": \"gray\", \"visible\": True, \"opacity\": 1.0 },\n", + " { \"path\": \"images/mni152.nii.gz\", \"colormap\": \"gray\",\n", + " \"visible\": True, \"opacity\": 1.0 },\n", "\n", "]\n", "\n", @@ -40,7 +42,25 @@ "source": [] } ], - "metadata": {}, + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.6" + } + }, "nbformat": 4, - "nbformat_minor": 2 + "nbformat_minor": 4 } diff --git a/examples/torso_regions.ipynb b/examples/torso_regions.ipynb deleted file mode 100644 index 8523244..0000000 --- a/examples/torso_regions.ipynb +++ /dev/null @@ -1,70 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 2, - "id": "71c2f1ec-fdbf-4aae-8d09-77fd5988e11c", - "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "fb1fdbd65e0845118d7955f94e149b24", - "version_major": 2, - "version_minor": 1 - }, - "text/plain": [ - "NiiVue(height=300)" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "from ipyniivue import NiiVue, SliceType\n", - "# based on https://github.com/niivue/ipyniivue/blob/main/original_gallery.md#torso-regions\n", - "\n", - "volumes = [\n", - " { \"path\": \"images/torso.nii.gz\", \"colormap\": \"gray\", \"visible\": True, \"opacity\": 1.0 },\n", - "\n", - "]\n", - "\n", - "nv = NiiVue(slice_type=SliceType.MULTIPLANAR)\n", - "nv.load_volumes(volumes)\n", - "\n", - "nv" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "593d466e-6bf6-47df-ba63-23b627f41da0", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": ".venv", - "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.12.5" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/src/ipyniivue/_utils.py b/src/ipyniivue/_utils.py index a07f6c0..29d6558 100644 --- a/src/ipyniivue/_utils.py +++ b/src/ipyniivue/_utils.py @@ -25,3 +25,4 @@ def mesh_layers_serializer(instance: list, widget: object): def serialize_options(instance: dict, widget: object): # serialize enums as their value return {k: v.value if isinstance(v, enum.Enum) else v for k, v in instance.items()} + From 2c558143a1003ad517167cf7f7f4d4c59a2bc3c5 Mon Sep 17 00:00:00 2001 From: alford Date: Sat, 2 Nov 2024 01:26:10 -0400 Subject: [PATCH 4/9] clearned notebook outputs --- examples/01_download_all_data.ipynb | 145 +--------------------------- 1 file changed, 2 insertions(+), 143 deletions(-) diff --git a/examples/01_download_all_data.ipynb b/examples/01_download_all_data.ipynb index c2c261a..b057508 100644 --- a/examples/01_download_all_data.ipynb +++ b/examples/01_download_all_data.ipynb @@ -2,150 +2,9 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Fetching contents from https://api.github.com/repos/niivue/niivue/contents/demos/images?ref=main...\n", - "Downloading AIL.nii.gz...\n", - "Downloading BrainMesh_ICBM152.lh.curv...\n", - "Downloading BrainMesh_ICBM152.lh.motor.mz3...\n", - "Downloading BrainMesh_ICBM152.lh.mz3...\n", - "Downloading CIT168.mz3...\n", - "Downloading ColumnMajorOrder.gii...\n", - "Downloading Conte69.L.inflated.32k_fs_LR.surf.gii...\n", - "Downloading Conte69.MyelinAndCorrThickness.32k_fs_LR.dtseries.nii...\n", - "Downloading Conte69.R.inflated.32k_fs_LR.surf.gii...\n", - "Downloading DoG.png...\n", - "Downloading FA.nii.gz...\n", - "Downloading FLAIR.nii.gz...\n", - "Downloading FLAIR.nrrd...\n", - "Downloading FreeSurferControlPoints.json...\n", - "Downloading Human.colin.Cerebral.R.VERY_INFLATED.71723.surf.gii...\n", - "Downloading Human.colin.R.FUNCTIONAL.71723.func.gii...\n", - "Downloading MNI152_2009_template.nii.gz...\n", - "Downloading MolView-sticks-color_38.x3d...\n", - "Downloading TR_S_R.tt.gz...\n", - "Downloading V1.nii.gz...\n", - "Downloading aal.json...\n", - "Downloading aal.mz3...\n", - "Downloading aal.nii.gz...\n", - "Entering directory afni...\n", - "Fetching contents from https://api.github.com/repos/niivue/niivue/contents/demos/images/afni?ref=main...\n", - "Downloading DT_FA+orig.BRIK.gz...\n", - "Downloading DT_FA+orig.HEAD...\n", - "Downloading DT_V1+orig.BRIK.gz...\n", - "Downloading DT_V1+orig.HEAD...\n", - "Downloading anat_final.FT+tlrc.BRIK...\n", - "Downloading anat_final.FT+tlrc.HEAD...\n", - "Downloading avg152T1_RL_nifti.nii.gz...\n", - "Downloading boggle.lh.annot...\n", - "Downloading cactus.nii.gz...\n", - "Downloading colby.trk...\n", - "Downloading colby.trx...\n", - "Downloading complex.nii.gz...\n", - "Downloading connectome.jcon...\n", - "Downloading connectome2.jcon...\n", - "Downloading cope1.nii.gz...\n", - "Downloading ct_perfusion.nii.gz...\n", - "Downloading dpsv.trx...\n", - "Downloading draw.nii.gz...\n", - "Downloading dsistudio.fib.gz...\n", - "Downloading dsistudio.src.gz...\n", - "Downloading enh.dcm...\n", - "Downloading example4d+orig.BRIK.gz...\n", - "Downloading example4d+orig.HEAD...\n", - "Entering directory fs...\n", - "Fetching contents from https://api.github.com/repos/niivue/niivue/contents/demos/images/fs?ref=main...\n", - "Downloading brainmask.mgz...\n", - "Downloading lh.pial...\n", - "Downloading lh.white...\n", - "Downloading rh.pial...\n", - "Downloading rh.white...\n", - "Downloading wm.mgz...\n", - "Downloading fs_LR.32k.L.inflated.surf.gii...\n", - "Downloading fs_LR.32k.LR.curvature.dscalar.nii...\n", - "Downloading fslmean.nii.gz...\n", - "Downloading fslt.nii.gz...\n", - "Downloading hippo.nii.gz...\n", - "Downloading hippolr.nii.gz...\n", - "Downloading inia19-NeuroMaps.json...\n", - "Downloading inia19-NeuroMaps.nii.gz...\n", - "Downloading inia19-t1-brain.nii.gz...\n", - "Downloading labels256.nii.gz...\n", - "Downloading lesion.nii.gz...\n", - "Downloading lh.Yeo2011.gii...\n", - "Downloading lh.Yeo2011.mgz...\n", - "Downloading lh.curv...\n", - "Downloading lh.pial...\n", - "Downloading low_res.nii.gz...\n", - "Downloading mean_func.nii.gz...\n", - "Downloading minimal.nii.gz...\n", - "Downloading mni152.SLF1_R.tsf...\n", - "Downloading mni152.nii.gz...\n", - "Downloading mni152_pveseg.nii.gz...\n", - "Downloading mni_format.obj.gz...\n", - "Downloading motor_4t95mesh.rh.mz3...\n", - "Downloading mpld_asl.nii.gz...\n", - "Entering directory mz3...\n", - "Fetching contents from https://api.github.com/repos/niivue/niivue/contents/demos/images/mz3?ref=main...\n", - "Downloading 11ScalarMesh.mz3...\n", - "Downloading 16DoubleOverlay_5124x2.mz3...\n", - "Downloading 3Mesh.mz3...\n", - "Downloading README.md...\n", - "Downloading cortex_5124.mz3...\n", - "Downloading narps-4735_50GV-hypo1_unthresh.nii.gz...\n", - "Downloading narps-4965_9U7M-hypo1_unthresh.nii.gz...\n", - "Downloading niivue-drawing.nvd...\n", - "Downloading niivue-with-clip-plane.nvd...\n", - "Downloading niivue.basic.nvd...\n", - "Downloading niivue.drawing.nvd...\n", - "Downloading niivue.mesh.nvd...\n", - "Downloading niivue1-no-hippo.nvd...\n", - "Downloading niivue1.nvd...\n", - "Downloading otsu.nii.gz...\n", - "Downloading pcasl.nii.gz...\n", - "Downloading pcasl.png...\n", - "Downloading pval.LH.nii.gz...\n", - "Downloading scaled+tlrc.BRIK.gz...\n", - "Downloading scaled+tlrc.HEAD...\n", - "Downloading shear.nii.gz...\n", - "Downloading simplify_brain.obj...\n", - "Downloading spmMotor.nii.gz...\n", - "Downloading stats.FT+tlrc.BRIK...\n", - "Downloading stats.FT+tlrc.HEAD...\n", - "Downloading stats.nv_demo_mskd.nii.gz...\n", - "Downloading sub-01_ses-01_dwi_desc-b0_dwi.nii.gz...\n", - "Downloading sub-01_ses-01_dwi_space-RASMM_model-probCSD_algo-AFQ_tractography.trx...\n", - "Downloading sub-test02_left_hemisphere.srf.gz...\n", - "Downloading sub-test02_left_hemisphere_4_curvature_maps.smp.gz...\n", - "Downloading template.mif.gz...\n", - "Downloading torso.nii.gz...\n", - "Downloading torsoLabel.nii.gz...\n", - "Downloading tract.FAT_R.vtk...\n", - "Downloading tract.IFOF_R.trk...\n", - "Downloading tract.SLF1_R.tck...\n", - "Entering directory trix...\n", - "Fetching contents from https://api.github.com/repos/niivue/niivue/contents/demos/images/trix?ref=main...\n", - "Downloading fa.mif...\n", - "Downloading mask_1bit.mif...\n", - "Downloading tensors.mif...\n", - "Downloading v1.mif...\n", - "Downloading tstat1.nii.gz...\n", - "Downloading vr_base_min_outlier+orig.BRIK...\n", - "Downloading vr_base_min_outlier+orig.HEAD...\n", - "Downloading wm.mgz...\n", - "Downloading xd.mz3...\n", - "Downloading yd.mz3...\n", - "Downloading yeh2022.trx...\n", - "Downloading zd.mz3...\n", - "All files and subdirectories have been downloaded to images.\n" - ] - } - ], + "outputs": [], "source": [ "# Import necessary libraries\n", "import os\n", From b3f5cf3eb0faa93e793afd15daafe805b3875ccc Mon Sep 17 00:00:00 2001 From: alford Date: Thu, 7 Nov 2024 21:50:59 -0500 Subject: [PATCH 5/9] created file download function py file --- examples/01_download_all_data.ipynb | 51 +++++------------------------ examples/basic_multiplanar.ipynb | 28 ++++------------ src/ipyniivue/__init__.py | 1 + src/ipyniivue/download_images.py | 34 +++++++++++++++++++ 4 files changed, 50 insertions(+), 64 deletions(-) create mode 100644 src/ipyniivue/download_images.py diff --git a/examples/01_download_all_data.ipynb b/examples/01_download_all_data.ipynb index b057508..9378389 100644 --- a/examples/01_download_all_data.ipynb +++ b/examples/01_download_all_data.ipynb @@ -3,54 +3,21 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [], "source": [ - "# Import necessary libraries\n", - "import os\n", - "\n", - "import requests\n", + "from pathlib import Path\n", + "import ipyniivue\n", + "from ipyniivue import DownloadImages\n", "\n", "# GitHub API URL for the base folder\n", - "BASE_API_URL = \"https://api.github.com/repos/niivue/niivue/contents\"\n", - "FOLDER_PATH = \"demos/images\"\n", - "REF = \"main\"\n", - "\n", - "# Folder where files will be saved\n", - "DEST_FOLDER = \"images\"\n", - "\n", - "def fetch_and_download(api_url, dest_folder):\n", - " \"\"\"Fetch and download files recursively.\"\"\"\n", - " print(f\"Fetching contents from {api_url}...\")\n", - " os.makedirs(dest_folder, exist_ok=True)\n", - " response = requests.get(api_url)\n", - " if response.status_code != 200:\n", - " print(f\"Failed to fetch {api_url}: {response.status_code}\")\n", - " return\n", - "\n", - " file_list = response.json()\n", - " for item in file_list:\n", - " item_type = item['type']\n", - " download_url = item.get('download_url', '') if item_type == 'file' else ''\n", - " name = item['name']\n", - " path = item['path']\n", - "\n", - " if item_type == 'file':\n", - " print(f\"Downloading {name}...\")\n", - " file_response = requests.get(download_url)\n", - " if file_response.status_code == 200:\n", - " with open(os.path.join(dest_folder, name), 'wb') as f:\n", - " f.write(file_response.content)\n", - " else:\n", - " print(f\"Failed to download {name}: {file_response.status_code}\")\n", - " elif item_type == 'dir':\n", - " print(f\"Entering directory {name}...\")\n", - " subfolder = os.path.join(dest_folder, name)\n", - " sub_api_url = f\"{BASE_API_URL}/{path}?ref={REF}\"\n", - " fetch_and_download(sub_api_url, subfolder)\n", + "BASE_API_URL = \"https://api.github.com/repos/niivue/niivue/contents/demos/images\"\n", + "DEST_FOLDER = \"../images\"\n", "\n", "# Start the recursive downloading\n", - "fetch_and_download(f\"{BASE_API_URL}/{FOLDER_PATH}?ref={REF}\", DEST_FOLDER)\n", + "DownloadImages(f\"{BASE_API_URL}\", DEST_FOLDER)\n", "\n", "print(f\"All files and subdirectories have been downloaded to {DEST_FOLDER}.\")" ] diff --git a/examples/basic_multiplanar.ipynb b/examples/basic_multiplanar.ipynb index 8b52b65..1286c64 100644 --- a/examples/basic_multiplanar.ipynb +++ b/examples/basic_multiplanar.ipynb @@ -2,34 +2,18 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "2f34ccf232f34ed784a01d32e5ae570a", - "version_major": 2, - "version_minor": 1 - }, - "text/plain": [ - "NiiVue(height=300)" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "from ipyniivue import NiiVue, SliceType\n", "\n", "# based on https://niivue.github.io/niivue/features/basic.multiplanar.html\n", "\n", "volumes = [\n", - " { \"path\": \"images/mni152.nii.gz\", \"colormap\": \"gray\",\n", + " { \"path\": \"../images/mni152.nii.gz\", \"colormap\": \"gray\",\n", " \"visible\": True, \"opacity\": 1.0 },\n", - " { \"path\": \"images/hippo.nii.gz\", \"colormap\": \"red\",\n", + " { \"path\": \"../images/hippo.nii.gz\", \"colormap\": \"red\",\n", " \"visible\": True, \"opacity\": 1 },\n", "\n", "]\n", @@ -40,7 +24,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -50,7 +34,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ diff --git a/src/ipyniivue/__init__.py b/src/ipyniivue/__init__.py index 627b4e4..0bc5787 100644 --- a/src/ipyniivue/__init__.py +++ b/src/ipyniivue/__init__.py @@ -4,5 +4,6 @@ from ._constants import DragMode, MuliplanarType, SliceType # noqa: F401 from ._widget import NiiVue # noqa: F401 +from .download_images import DownloadImages __version__ = importlib.metadata.version("ipyniivue") diff --git a/src/ipyniivue/download_images.py b/src/ipyniivue/download_images.py new file mode 100644 index 0000000..6426fec --- /dev/null +++ b/src/ipyniivue/download_images.py @@ -0,0 +1,34 @@ +# Import necessary libraries +import os +import ipyniivue +import requests + +def DownloadImages(api_url, dest_folder): + """Fetch and download files recursively.""" + print(f"Fetching contents from {api_url}...") + os.makedirs(dest_folder, exist_ok=True) + response = requests.get(api_url) + if response.status_code != 200: + print(f"Failed to fetch {api_url}: {response.status_code}") + return + + file_list = response.json() + for item in file_list: + item_type = item['type'] + download_url = item.get('download_url', '') if item_type == 'file' else '' + name = item['name'] + path = item['path'] + + if item_type == 'file': + print(f"Downloading {name}...") + file_response = requests.get(download_url) + if file_response.status_code == 200: + with open(os.path.join(dest_folder, name), 'wb') as f: + f.write(file_response.content) + else: + print(f"Failed to download {name}: {file_response.status_code}") + elif item_type == 'dir': + print(f"Entering directory {name}...") + subfolder = os.path.join(dest_folder, name) + sub_api_url = f"{api_url}/{name}" + DownloadImages(sub_api_url, subfolder) \ No newline at end of file From c7f18fe396b0bc4f44fc4d875c8d9b14e6aeec99 Mon Sep 17 00:00:00 2001 From: alford Date: Mon, 2 Dec 2024 11:05:37 -0500 Subject: [PATCH 6/9] implements data download into each example --- examples/01_download_all_data.ipynb | 54 ---------- examples/additive_voxels.ipynb | 98 ++++++++++--------- examples/basic_multiplanar.ipynb | 32 ++++-- examples/mesh_and_volume.ipynb | 57 +++++------ examples/mesh_layers.ipynb | 57 ++++++----- src/ipyniivue/__init__.py | 2 +- ...download_images.py => download_dataset.py} | 6 +- 7 files changed, 138 insertions(+), 168 deletions(-) delete mode 100644 examples/01_download_all_data.ipynb rename src/ipyniivue/{download_images.py => download_dataset.py} (86%) diff --git a/examples/01_download_all_data.ipynb b/examples/01_download_all_data.ipynb deleted file mode 100644 index 9378389..0000000 --- a/examples/01_download_all_data.ipynb +++ /dev/null @@ -1,54 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "from pathlib import Path\n", - "import ipyniivue\n", - "from ipyniivue import DownloadImages\n", - "\n", - "# GitHub API URL for the base folder\n", - "BASE_API_URL = \"https://api.github.com/repos/niivue/niivue/contents/demos/images\"\n", - "DEST_FOLDER = \"../images\"\n", - "\n", - "# Start the recursive downloading\n", - "DownloadImages(f\"{BASE_API_URL}\", DEST_FOLDER)\n", - "\n", - "print(f\"All files and subdirectories have been downloaded to {DEST_FOLDER}.\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "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.6" - } - }, - "nbformat": 4, - "nbformat_minor": 4 -} diff --git a/examples/additive_voxels.ipynb b/examples/additive_voxels.ipynb index fadb562..7bf3fcb 100644 --- a/examples/additive_voxels.ipynb +++ b/examples/additive_voxels.ipynb @@ -2,40 +2,47 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "8e77b0fab1204e5191dbca06414c09a4", - "version_major": 2, - "version_minor": 1 - }, - "text/plain": [ - "NiiVue(height=300)" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "from ipyniivue import NiiVue\n", + "from pathlib import Path\n", + "from ipyniivue import NiiVue, download_dataset\n", + "import ipyniivue\n", "\n", + "# GitHub API URL for the base folder\n", + "BASE_API_URL = \"https://api.github.com/repos/niivue/niivue/contents/packages/niivue/demos/images\"\n", + "DATA_FOLDER = Path(ipyniivue.__file__).parent / \"images\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Downloads all data for examples\n", + "download_dataset(f\"{BASE_API_URL}\", DEST_FOLDER)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "# based on https://niivue.github.io/niivue/features/additive.voxels.html\n", "\n", "volumes = [\n", - " { \"path\": \"images/mni152.nii.gz\" },\n", + " { \"path\": DATA_FOLDER / \"mni152.nii.gz\" },\n", " {\n", - " \"path\": \"images/narps-4965_9U7M-hypo1_unthresh.nii.gz\",\n", + " \"path\": DATA_FOLDER / \"narps-4965_9U7M-hypo1_unthresh.nii.gz\",\n", " \"colormap\": \"red\",\n", " \"cal_min\": 2,\n", " \"cal_max\": 4,\n", " },\n", " {\n", - " \"path\": \"images/narps-4735_50GV-hypo1_unthresh.nii.gz\",\n", + " \"path\": DATA_FOLDER / \"narps-4735_50GV-hypo1_unthresh.nii.gz\",\n", " \"colormap\": \"green\",\n", " \"cal_min\": 2,\n", " \"cal_max\": 4,\n", @@ -52,25 +59,9 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "cb5b42ba9d774dd69a613e644326eb2f", - "version_major": 2, - "version_minor": 0 - }, - "text/plain": [ - "HBox(children=(FloatSlider(value=0.2, max=0.4, min=0.1, step=0.01), FloatSlider(value=0.2, max=0.4, min=0.1, s…" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "import ipywidgets\n", "\n", @@ -81,16 +72,27 @@ "ipywidgets.link((sgreen, \"value\"), (nv.volumes[2], \"cal_min\"))\n", "ipywidgets.HBox([sred, sgreen])" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], - "metadata": {}, + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.6" + } + }, "nbformat": 4, "nbformat_minor": 4 } diff --git a/examples/basic_multiplanar.ipynb b/examples/basic_multiplanar.ipynb index 1286c64..3c88371 100644 --- a/examples/basic_multiplanar.ipynb +++ b/examples/basic_multiplanar.ipynb @@ -6,8 +6,31 @@ "metadata": {}, "outputs": [], "source": [ - "from ipyniivue import NiiVue, SliceType\n", + "from pathlib import Path\n", + "from ipyniivue import NiiVue, download_dataset\n", + "import ipyniivue\n", "\n", + "# GitHub API URL for the base folder\n", + "BASE_API_URL = \"https://api.github.com/repos/niivue/niivue/contents/packages/niivue/demos/images\"\n", + "DATA_FOLDER = Path(ipyniivue.__file__).parent / \"images\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Downloads all data for examples\n", + "download_dataset(f\"{BASE_API_URL}\", DEST_FOLDER)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ "# based on https://niivue.github.io/niivue/features/basic.multiplanar.html\n", "\n", "volumes = [\n", @@ -45,13 +68,6 @@ "nv.is_colorbar = True\n", "nv.view_mode_hot_key = \"KeyN\"" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/examples/mesh_and_volume.ipynb b/examples/mesh_and_volume.ipynb index 22a3d45..07d37ff 100644 --- a/examples/mesh_and_volume.ipynb +++ b/examples/mesh_and_volume.ipynb @@ -2,29 +2,38 @@ "cells": [ { "cell_type": "code", - "execution_count": 2, - "id": "71c2f1ec-fdbf-4aae-8d09-77fd5988e11c", + "execution_count": null, + "id": "593d466e-6bf6-47df-ba63-23b627f41da0", "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "b19c2298162e4e06b73f44b43f747ba9", - "version_major": 2, - "version_minor": 1 - }, - "text/plain": [ - "NiiVue(height=300)" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ - "from ipyniivue import NiiVue, SliceType\n", + "from pathlib import Path\n", + "from ipyniivue import NiiVue, download_dataset\n", + "import ipyniivue\n", "\n", + "# GitHub API URL for the base folder\n", + "BASE_API_URL = \"https://api.github.com/repos/niivue/niivue/contents/packages/niivue/demos/images\"\n", + "DATA_FOLDER = Path(ipyniivue.__file__).parent / \"images\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a09b7f2a-d0f7-4337-bfcc-ce5152fa9e79", + "metadata": {}, + "outputs": [], + "source": [ + "# Downloads all data for examples\n", + "download_dataset(f\"{BASE_API_URL}\", DEST_FOLDER)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "71c2f1ec-fdbf-4aae-8d09-77fd5988e11c", + "metadata": {}, + "outputs": [], + "source": [ "# based on https://github.com/niivue/ipyniivue/blob/main/original_gallery.md#tractography-tck-trk-trx-vtk\n", "\n", "volumes = [\n", @@ -48,14 +57,6 @@ "\n", "nv" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "593d466e-6bf6-47df-ba63-23b627f41da0", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/examples/mesh_layers.ipynb b/examples/mesh_layers.ipynb index d7f9b38..e1cb38d 100644 --- a/examples/mesh_layers.ipynb +++ b/examples/mesh_layers.ipynb @@ -2,26 +2,37 @@ "cells": [ { "cell_type": "code", - "execution_count": 1, + "execution_count": null, + "id": "69a4c0b3", + "metadata": {}, + "outputs": [], + "source": [ + "from pathlib import Path\n", + "from ipyniivue import NiiVue, download_dataset\n", + "import ipyniivue\n", + "\n", + "# GitHub API URL for the base folder\n", + "BASE_API_URL = \"https://api.github.com/repos/niivue/niivue/contents/packages/niivue/demos/images\"\n", + "DATA_FOLDER = Path(ipyniivue.__file__).parent / \"images\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eaca0a33-87c7-437f-b9b5-a0c967a44faf", + "metadata": {}, + "outputs": [], + "source": [ + "# Downloads all data for examples\n", + "download_dataset(f\"{BASE_API_URL}\", DEST_FOLDER)" + ] + }, + { + "cell_type": "code", + "execution_count": null, "id": "36d80248-e8a6-4f27-92c5-98dabf2aec81", "metadata": {}, - "outputs": [ - { - "data": { - "application/vnd.jupyter.widget-view+json": { - "model_id": "f049f7486a904ee785cf1699cc6a02b6", - "version_major": 2, - "version_minor": 1 - }, - "text/plain": [ - "NiiVue(height=300)" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "from ipyniivue import NiiVue\n", "\n", @@ -53,21 +64,13 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "956fe2ef-3180-44cc-a81e-05efc783ee05", "metadata": {}, "outputs": [], "source": [ "nv.meshes[0].rgba255 = [200, 200, 200, 0]" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "69a4c0b3", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { diff --git a/src/ipyniivue/__init__.py b/src/ipyniivue/__init__.py index 0bc5787..7a2e958 100644 --- a/src/ipyniivue/__init__.py +++ b/src/ipyniivue/__init__.py @@ -4,6 +4,6 @@ from ._constants import DragMode, MuliplanarType, SliceType # noqa: F401 from ._widget import NiiVue # noqa: F401 -from .download_images import DownloadImages +from .download_images import download_dataset __version__ = importlib.metadata.version("ipyniivue") diff --git a/src/ipyniivue/download_images.py b/src/ipyniivue/download_dataset.py similarity index 86% rename from src/ipyniivue/download_images.py rename to src/ipyniivue/download_dataset.py index 6426fec..ea6a6af 100644 --- a/src/ipyniivue/download_images.py +++ b/src/ipyniivue/download_dataset.py @@ -3,7 +3,7 @@ import ipyniivue import requests -def DownloadImages(api_url, dest_folder): +def download_dataset(api_url, dest_folder): """Fetch and download files recursively.""" print(f"Fetching contents from {api_url}...") os.makedirs(dest_folder, exist_ok=True) @@ -31,4 +31,6 @@ def DownloadImages(api_url, dest_folder): print(f"Entering directory {name}...") subfolder = os.path.join(dest_folder, name) sub_api_url = f"{api_url}/{name}" - DownloadImages(sub_api_url, subfolder) \ No newline at end of file + download_dataset(sub_api_url, subfolder) + + print(f"All files and subdirectories have been downloaded to {DEST_FOLDER}.") \ No newline at end of file From 881baefc1befa04c2d6caacc33c93fc89064837a Mon Sep 17 00:00:00 2001 From: bcalford Date: Mon, 2 Dec 2024 13:16:24 -0500 Subject: [PATCH 7/9] lint fixes --- examples/additive_voxels.ipynb | 5 +++-- examples/basic_multiplanar.ipynb | 9 +++++---- examples/mesh_and_volume.ipynb | 17 +++++++++++++---- examples/mesh_layers.ipynb | 12 ++++++------ src/ipyniivue/__init__.py | 2 +- src/ipyniivue/download_dataset.py | 8 +++++--- 6 files changed, 33 insertions(+), 20 deletions(-) diff --git a/examples/additive_voxels.ipynb b/examples/additive_voxels.ipynb index 7bf3fcb..7d364a2 100644 --- a/examples/additive_voxels.ipynb +++ b/examples/additive_voxels.ipynb @@ -7,8 +7,9 @@ "outputs": [], "source": [ "from pathlib import Path\n", - "from ipyniivue import NiiVue, download_dataset\n", + "\n", "import ipyniivue\n", + "from ipyniivue import NiiVue, download_dataset\n", "\n", "# GitHub API URL for the base folder\n", "BASE_API_URL = \"https://api.github.com/repos/niivue/niivue/contents/packages/niivue/demos/images\"\n", @@ -22,7 +23,7 @@ "outputs": [], "source": [ "# Downloads all data for examples\n", - "download_dataset(f\"{BASE_API_URL}\", DEST_FOLDER)" + "download_dataset(f\"{BASE_API_URL}\", DATA_FOLDER)" ] }, { diff --git a/examples/basic_multiplanar.ipynb b/examples/basic_multiplanar.ipynb index 3c88371..d009948 100644 --- a/examples/basic_multiplanar.ipynb +++ b/examples/basic_multiplanar.ipynb @@ -7,8 +7,9 @@ "outputs": [], "source": [ "from pathlib import Path\n", - "from ipyniivue import NiiVue, download_dataset\n", + "\n", "import ipyniivue\n", + "from ipyniivue import NiiVue, SliceType, download_dataset\n", "\n", "# GitHub API URL for the base folder\n", "BASE_API_URL = \"https://api.github.com/repos/niivue/niivue/contents/packages/niivue/demos/images\"\n", @@ -22,7 +23,7 @@ "outputs": [], "source": [ "# Downloads all data for examples\n", - "download_dataset(f\"{BASE_API_URL}\", DEST_FOLDER)" + "download_dataset(f\"{BASE_API_URL}\", DATA_FOLDER)" ] }, { @@ -34,9 +35,9 @@ "# based on https://niivue.github.io/niivue/features/basic.multiplanar.html\n", "\n", "volumes = [\n", - " { \"path\": \"../images/mni152.nii.gz\", \"colormap\": \"gray\",\n", + " { \"path\": DATA_FOLDER / \"mni152.nii.gz\", \"colormap\": \"gray\",\n", " \"visible\": True, \"opacity\": 1.0 },\n", - " { \"path\": \"../images/hippo.nii.gz\", \"colormap\": \"red\",\n", + " { \"path\": DATA_FOLDER / \"hippo.nii.gz\", \"colormap\": \"red\",\n", " \"visible\": True, \"opacity\": 1 },\n", "\n", "]\n", diff --git a/examples/mesh_and_volume.ipynb b/examples/mesh_and_volume.ipynb index 07d37ff..479e6c3 100644 --- a/examples/mesh_and_volume.ipynb +++ b/examples/mesh_and_volume.ipynb @@ -8,8 +8,9 @@ "outputs": [], "source": [ "from pathlib import Path\n", - "from ipyniivue import NiiVue, download_dataset\n", + "\n", "import ipyniivue\n", + "from ipyniivue import NiiVue, SliceType, download_dataset\n", "\n", "# GitHub API URL for the base folder\n", "BASE_API_URL = \"https://api.github.com/repos/niivue/niivue/contents/packages/niivue/demos/images\"\n", @@ -24,7 +25,7 @@ "outputs": [], "source": [ "# Downloads all data for examples\n", - "download_dataset(f\"{BASE_API_URL}\", DEST_FOLDER)" + "download_dataset(f\"{BASE_API_URL}\", DATA_FOLDER)" ] }, { @@ -37,7 +38,7 @@ "# based on https://github.com/niivue/ipyniivue/blob/main/original_gallery.md#tractography-tck-trk-trx-vtk\n", "\n", "volumes = [\n", - " { \"path\": \"images/mni152.nii.gz\", \"colormap\": \"gray\",\n", + " { \"path\": DATA_FOLDER / \"mni152.nii.gz\", \"colormap\": \"gray\",\n", " \"visible\": True, \"opacity\": 1.0 },\n", "\n", "]\n", @@ -48,7 +49,7 @@ "nv.load_meshes(\n", " [\n", " {\n", - " \"path\": \"images/BrainMesh_ICBM152.lh.mz3\",\n", + " \"path\": DATA_FOLDER / \"BrainMesh_ICBM152.lh.mz3\",\n", " \"rgba255\": [255, 255, 255, 255],\n", " }\n", " ]\n", @@ -57,6 +58,14 @@ "\n", "nv" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "851a559d-59e8-40bc-b1f0-9380f9844855", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/examples/mesh_layers.ipynb b/examples/mesh_layers.ipynb index e1cb38d..400bc14 100644 --- a/examples/mesh_layers.ipynb +++ b/examples/mesh_layers.ipynb @@ -8,8 +8,9 @@ "outputs": [], "source": [ "from pathlib import Path\n", - "from ipyniivue import NiiVue, download_dataset\n", + "\n", "import ipyniivue\n", + "from ipyniivue import NiiVue, download_dataset\n", "\n", "# GitHub API URL for the base folder\n", "BASE_API_URL = \"https://api.github.com/repos/niivue/niivue/contents/packages/niivue/demos/images\"\n", @@ -24,7 +25,7 @@ "outputs": [], "source": [ "# Downloads all data for examples\n", - "download_dataset(f\"{BASE_API_URL}\", DEST_FOLDER)" + "download_dataset(f\"{BASE_API_URL}\", DATA_FOLDER)" ] }, { @@ -34,14 +35,13 @@ "metadata": {}, "outputs": [], "source": [ - "from ipyniivue import NiiVue\n", "\n", "nv = NiiVue(\n", " show_3d_crosshair=True, back_color=(1, 1, 1, 1), mesh_x_ray=0.3, is_colorbar=True\n", ")\n", "\n", "mesh_layer = {\n", - " \"path\": \"images/BrainMesh_ICBM152.lh.motor.mz3\",\n", + " \"path\": DATA_FOLDER / \"BrainMesh_ICBM152.lh.motor.mz3\",\n", " \"cal_min\": 0.5,\n", " \"cal_max\": 5.5,\n", " \"useNegativeCmap\": True,\n", @@ -51,11 +51,11 @@ "nv.load_meshes(\n", " [\n", " {\n", - " \"path\": \"images/BrainMesh_ICBM152.lh.mz3\",\n", + " \"path\": DATA_FOLDER / \"BrainMesh_ICBM152.lh.mz3\",\n", " \"rgba255\": [255, 255, 255, 255],\n", " \"layers\": [mesh_layer],\n", " },\n", - " {\"path\": \"images/CIT168.mz3\", \"rgba255\": [0, 0, 255, 255]},\n", + " {\"path\": DATA_FOLDER / \"CIT168.mz3\", \"rgba255\": [0, 0, 255, 255]},\n", " ]\n", ")\n", "\n", diff --git a/src/ipyniivue/__init__.py b/src/ipyniivue/__init__.py index 7a2e958..9e81652 100644 --- a/src/ipyniivue/__init__.py +++ b/src/ipyniivue/__init__.py @@ -4,6 +4,6 @@ from ._constants import DragMode, MuliplanarType, SliceType # noqa: F401 from ._widget import NiiVue # noqa: F401 -from .download_images import download_dataset +from .download_images import download_dataset # noqa: F401 __version__ = importlib.metadata.version("ipyniivue") diff --git a/src/ipyniivue/download_dataset.py b/src/ipyniivue/download_dataset.py index ea6a6af..cf65b23 100644 --- a/src/ipyniivue/download_dataset.py +++ b/src/ipyniivue/download_dataset.py @@ -1,8 +1,11 @@ +"""Downloads all data needed for provided example images.""" + # Import necessary libraries import os -import ipyniivue + import requests + def download_dataset(api_url, dest_folder): """Fetch and download files recursively.""" print(f"Fetching contents from {api_url}...") @@ -17,7 +20,6 @@ def download_dataset(api_url, dest_folder): item_type = item['type'] download_url = item.get('download_url', '') if item_type == 'file' else '' name = item['name'] - path = item['path'] if item_type == 'file': print(f"Downloading {name}...") @@ -33,4 +35,4 @@ def download_dataset(api_url, dest_folder): sub_api_url = f"{api_url}/{name}" download_dataset(sub_api_url, subfolder) - print(f"All files and subdirectories have been downloaded to {DEST_FOLDER}.") \ No newline at end of file + print(f"All files and subdirectories have been downloaded to {dest_folder}.") From 44487b99efcde35b546bc5c704be17627da9c7fe Mon Sep 17 00:00:00 2001 From: bcalford Date: Mon, 2 Dec 2024 13:24:18 -0500 Subject: [PATCH 8/9] restructure and rename download_images to download_dataset --- examples/additive_voxels.ipynb | 6 ++- examples/basic_multiplanar.ipynb | 21 ++++++--- examples/mesh_and_volume.ipynb | 13 +++-- examples/mesh_layers.ipynb | 5 +- ...eeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb | 2 +- examples/prototypes/torso_regions.ipynb | 47 ++++++++++--------- examples/prototypes/trajectory.ipynb | 14 +++--- package-lock.json | 2 +- src/ipyniivue/__init__.py | 2 +- src/ipyniivue/_utils.py | 1 - src/ipyniivue/download_dataset.py | 12 ++--- 11 files changed, 73 insertions(+), 52 deletions(-) diff --git a/examples/additive_voxels.ipynb b/examples/additive_voxels.ipynb index 2876038..b727df9 100644 --- a/examples/additive_voxels.ipynb +++ b/examples/additive_voxels.ipynb @@ -12,7 +12,9 @@ "from ipyniivue import NiiVue, download_dataset\n", "\n", "# GitHub API URL for the base folder\n", - "BASE_API_URL = \"https://api.github.com/repos/niivue/niivue/contents/packages/niivue/demos/images\"\n", + "BASE_API_URL = (\n", + " \"https://api.github.com/repos/niivue/niivue/contents/packages/niivue/demos/images\"\n", + ")\n", "DATA_FOLDER = Path(ipyniivue.__file__).parent / \"images\"" ] }, @@ -35,7 +37,7 @@ "# based on https://niivue.github.io/niivue/features/additive.voxels.html\n", "\n", "volumes = [\n", - " { \"path\": DATA_FOLDER / \"mni152.nii.gz\" },\n", + " {\"path\": DATA_FOLDER / \"mni152.nii.gz\"},\n", " {\n", " \"path\": DATA_FOLDER / \"narps-4965_9U7M-hypo1_unthresh.nii.gz\",\n", " \"colormap\": \"red\",\n", diff --git a/examples/basic_multiplanar.ipynb b/examples/basic_multiplanar.ipynb index 80db689..4a5df3a 100644 --- a/examples/basic_multiplanar.ipynb +++ b/examples/basic_multiplanar.ipynb @@ -12,7 +12,9 @@ "from ipyniivue import NiiVue, SliceType, download_dataset\n", "\n", "# GitHub API URL for the base folder\n", - "BASE_API_URL = \"https://api.github.com/repos/niivue/niivue/contents/packages/niivue/demos/images\"\n", + "BASE_API_URL = (\n", + " \"https://api.github.com/repos/niivue/niivue/contents/packages/niivue/demos/images\"\n", + ")\n", "DATA_FOLDER = Path(ipyniivue.__file__).parent / \"images\"" ] }, @@ -35,11 +37,18 @@ "# based on https://niivue.github.io/niivue/features/basic.multiplanar.html\n", "\n", "volumes = [\n", - " { \"path\": DATA_FOLDER / \"mni152.nii.gz\", \"colormap\": \"gray\",\n", - " \"visible\": True, \"opacity\": 1.0 },\n", - " { \"path\": DATA_FOLDER / \"hippo.nii.gz\", \"colormap\": \"red\",\n", - " \"visible\": True, \"opacity\": 1 },\n", - "\n", + " {\n", + " \"path\": DATA_FOLDER / \"mni152.nii.gz\",\n", + " \"colormap\": \"gray\",\n", + " \"visible\": True,\n", + " \"opacity\": 1.0,\n", + " },\n", + " {\n", + " \"path\": DATA_FOLDER / \"hippo.nii.gz\",\n", + " \"colormap\": \"red\",\n", + " \"visible\": True,\n", + " \"opacity\": 1,\n", + " },\n", "]\n", "nv = NiiVue(slice_type=SliceType.MULTIPLANAR)\n", "nv.load_volumes(volumes)\n", diff --git a/examples/mesh_and_volume.ipynb b/examples/mesh_and_volume.ipynb index 479e6c3..bedc582 100644 --- a/examples/mesh_and_volume.ipynb +++ b/examples/mesh_and_volume.ipynb @@ -13,7 +13,9 @@ "from ipyniivue import NiiVue, SliceType, download_dataset\n", "\n", "# GitHub API URL for the base folder\n", - "BASE_API_URL = \"https://api.github.com/repos/niivue/niivue/contents/packages/niivue/demos/images\"\n", + "BASE_API_URL = (\n", + " \"https://api.github.com/repos/niivue/niivue/contents/packages/niivue/demos/images\"\n", + ")\n", "DATA_FOLDER = Path(ipyniivue.__file__).parent / \"images\"" ] }, @@ -38,9 +40,12 @@ "# based on https://github.com/niivue/ipyniivue/blob/main/original_gallery.md#tractography-tck-trk-trx-vtk\n", "\n", "volumes = [\n", - " { \"path\": DATA_FOLDER / \"mni152.nii.gz\", \"colormap\": \"gray\",\n", - " \"visible\": True, \"opacity\": 1.0 },\n", - "\n", + " {\n", + " \"path\": DATA_FOLDER / \"mni152.nii.gz\",\n", + " \"colormap\": \"gray\",\n", + " \"visible\": True,\n", + " \"opacity\": 1.0,\n", + " },\n", "]\n", "\n", "nv = NiiVue(slice_type=SliceType.MULTIPLANAR)\n", diff --git a/examples/mesh_layers.ipynb b/examples/mesh_layers.ipynb index fc46cbf..e222f2c 100644 --- a/examples/mesh_layers.ipynb +++ b/examples/mesh_layers.ipynb @@ -13,7 +13,9 @@ "from ipyniivue import NiiVue, download_dataset\n", "\n", "# GitHub API URL for the base folder\n", - "BASE_API_URL = \"https://api.github.com/repos/niivue/niivue/contents/packages/niivue/demos/images\"\n", + "BASE_API_URL = (\n", + " \"https://api.github.com/repos/niivue/niivue/contents/packages/niivue/demos/images\"\n", + ")\n", "DATA_FOLDER = Path(ipyniivue.__file__).parent / \"images\"" ] }, @@ -35,7 +37,6 @@ "metadata": {}, "outputs": [], "source": [ - "\n", "nv = NiiVue(\n", " show_3d_crosshair=True, back_color=(1, 1, 1, 1), mesh_x_ray=0.3, is_colorbar=True\n", ")\n", diff --git a/examples/prototypes/meshes_(GIfTI, FreeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb b/examples/prototypes/meshes_(GIfTI, FreeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb index 55bcc9d..8d5be81 100644 --- a/examples/prototypes/meshes_(GIfTI, FreeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb +++ b/examples/prototypes/meshes_(GIfTI, FreeSurfer, MZ3, OBJ, STL, legacy VTK).ipynb @@ -35,7 +35,7 @@ " \"path\": \"images/BrainMesh_ICBM152.lh.mz3\",\n", " \"rgba255\": [222, 164, 164, 255],\n", " },\n", - " { \"path\": \"images/CIT168.mz3\", \"rgba255\": [0, 0, 255, 255] },\n", + " {\"path\": \"images/CIT168.mz3\", \"rgba255\": [0, 0, 255, 255]},\n", " ]\n", ")\n", "\n", diff --git a/examples/prototypes/torso_regions.ipynb b/examples/prototypes/torso_regions.ipynb index 20edbdb..597a1e2 100644 --- a/examples/prototypes/torso_regions.ipynb +++ b/examples/prototypes/torso_regions.ipynb @@ -12,9 +12,12 @@ "# based on https://github.com/niivue/ipyniivue/blob/main/original_gallery.md#torso-regions\n", "\n", "volumes = [\n", - " { \"path\": \"images/torso.nii.gz\", \"colormap\": (0, 0, 0, 0),\n", - " \"visible\": True, \"opacity\": 1.0 },\n", - "\n", + " {\n", + " \"path\": \"images/torso.nii.gz\",\n", + " \"colormap\": (0, 0, 0, 0),\n", + " \"visible\": True,\n", + " \"opacity\": 1.0,\n", + " },\n", "]\n", "\n", "nv = NiiVue(slice_type=SliceType.MULTIPLANAR)\n", @@ -31,25 +34,25 @@ "outputs": [], "source": [ "cmap = {\n", - " \"R\": [0, 0, 185, 185, 252, 0, 103, 216, 127, 127, 0, 222],\n", - " \"G\": [0, 20, 102, 102, 0, 255, 76, 132, 0, 127, 255, 154],\n", - " \"B\": [0, 152, 83, 83, 0, 0, 71, 105, 127, 0, 255, 132],\n", - " \"A\": [0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],\n", - " \"labels\": [\n", - " \"background\",\n", - " \"1spleen\",\n", - " \"2kidneyR\",\n", - " \"3kidneyL\",\n", - " \"4gallbladder\",\n", - " \"5esophagus\",\n", - " \"6Liver\",\n", - " \"7stomach\",\n", - " \"8aorta\",\n", - " \"9inferiorvenacava\",\n", - " \"10pancreas\",\n", - " \"11bladder\",\n", - " ],\n", - " }\n", + " \"R\": [0, 0, 185, 185, 252, 0, 103, 216, 127, 127, 0, 222],\n", + " \"G\": [0, 20, 102, 102, 0, 255, 76, 132, 0, 127, 255, 154],\n", + " \"B\": [0, 152, 83, 83, 0, 0, 71, 105, 127, 0, 255, 132],\n", + " \"A\": [0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255],\n", + " \"labels\": [\n", + " \"background\",\n", + " \"1spleen\",\n", + " \"2kidneyR\",\n", + " \"3kidneyL\",\n", + " \"4gallbladder\",\n", + " \"5esophagus\",\n", + " \"6Liver\",\n", + " \"7stomach\",\n", + " \"8aorta\",\n", + " \"9inferiorvenacava\",\n", + " \"10pancreas\",\n", + " \"11bladder\",\n", + " ],\n", + "}\n", "nv.setDrawColormap(cmap);" ] }, diff --git a/examples/prototypes/trajectory.ipynb b/examples/prototypes/trajectory.ipynb index c1b6f76..3dff821 100644 --- a/examples/prototypes/trajectory.ipynb +++ b/examples/prototypes/trajectory.ipynb @@ -11,26 +11,28 @@ "# based on https://github.com/niivue/ipyniivue/blob/main/original_gallery.md#tractography-tck-trk-trx-vtk\n", "\n", "volumes = [\n", - " { \"path\": \"images/mni152.nii.gz\", \"colormap\": \"gray\",\n", - " \"visible\": True, \"opacity\": 1.0 },\n", - "\n", + " {\n", + " \"path\": \"images/mni152.nii.gz\",\n", + " \"colormap\": \"gray\",\n", + " \"visible\": True,\n", + " \"opacity\": 1.0,\n", + " },\n", "]\n", "\n", "nv = NiiVue(slice_type=SliceType.MULTIPLANAR)\n", - "#nv.load_volumes(volumes)\n", + "# nv.load_volumes(volumes)\n", "\n", "\n", "nv.load_meshes(\n", " [\n", " {\n", - " \"path\": \"images/dpsv.trx\", # this does currently not load\n", + " \"path\": \"images/dpsv.trx\", # this does currently not load\n", " \"rgba255\": [0, 142, 0, 255],\n", " }\n", " ]\n", ")\n", "\n", "\n", - "\n", "nv" ] }, diff --git a/package-lock.json b/package-lock.json index b2d5f06..fa2b96d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "bca-ipyniivue", + "name": "ipyniivue", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/src/ipyniivue/__init__.py b/src/ipyniivue/__init__.py index 19cff6e..aef56b3 100644 --- a/src/ipyniivue/__init__.py +++ b/src/ipyniivue/__init__.py @@ -4,6 +4,6 @@ from ._constants import DragMode, MuliplanarType, SliceType # noqa: F401 from ._widget import NiiVue, WidgetObserver # noqa: F401 -from .download_images import download_dataset # noqa: F401 +from .download_dataset import download_dataset # noqa: F401 __version__ = importlib.metadata.version("ipyniivue") diff --git a/src/ipyniivue/_utils.py b/src/ipyniivue/_utils.py index 29d6558..a07f6c0 100644 --- a/src/ipyniivue/_utils.py +++ b/src/ipyniivue/_utils.py @@ -25,4 +25,3 @@ def mesh_layers_serializer(instance: list, widget: object): def serialize_options(instance: dict, widget: object): # serialize enums as their value return {k: v.value if isinstance(v, enum.Enum) else v for k, v in instance.items()} - diff --git a/src/ipyniivue/download_dataset.py b/src/ipyniivue/download_dataset.py index cf65b23..aba7906 100644 --- a/src/ipyniivue/download_dataset.py +++ b/src/ipyniivue/download_dataset.py @@ -17,19 +17,19 @@ def download_dataset(api_url, dest_folder): file_list = response.json() for item in file_list: - item_type = item['type'] - download_url = item.get('download_url', '') if item_type == 'file' else '' - name = item['name'] + item_type = item["type"] + download_url = item.get("download_url", "") if item_type == "file" else "" + name = item["name"] - if item_type == 'file': + if item_type == "file": print(f"Downloading {name}...") file_response = requests.get(download_url) if file_response.status_code == 200: - with open(os.path.join(dest_folder, name), 'wb') as f: + with open(os.path.join(dest_folder, name), "wb") as f: f.write(file_response.content) else: print(f"Failed to download {name}: {file_response.status_code}") - elif item_type == 'dir': + elif item_type == "dir": print(f"Entering directory {name}...") subfolder = os.path.join(dest_folder, name) sub_api_url = f"{api_url}/{name}" From d339246aa983fe4dc072e476b72342cb50a6113a Mon Sep 17 00:00:00 2001 From: Bradley Alford <143644192+bcalford@users.noreply.github.com> Date: Thu, 5 Dec 2024 12:20:11 -0500 Subject: [PATCH 9/9] Update src/ipyniivue/download_dataset.py Provides default values for download_dataset Co-authored-by: Christian O'Reilly --- src/ipyniivue/download_dataset.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/ipyniivue/download_dataset.py b/src/ipyniivue/download_dataset.py index aba7906..993d156 100644 --- a/src/ipyniivue/download_dataset.py +++ b/src/ipyniivue/download_dataset.py @@ -6,7 +6,23 @@ import requests -def download_dataset(api_url, dest_folder): +from pathlib import Path + +# GitHub API URL for the base folder +BASE_API_URL = ( + "https://api.github.com/repos/niivue/niivue/contents/packages/niivue/demos/images" +) + +DATA_FOLDER = Path(ipyniivue.__file__).parent / "images" + + +def download_dataset(api_url=None, dest_folder=None): + """ Download the datasets used for demos and testing. """ + if api_url is None: + api_url = BASE_API_URL + if dest_folder is None: + dest_folder = DATA_FOLDER + """Fetch and download files recursively.""" print(f"Fetching contents from {api_url}...") os.makedirs(dest_folder, exist_ok=True)