Created
August 8, 2024 17:15
-
-
Save NicerNewerCar/42952057bb9a911a8c14b584160b3811 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import numpy as np\n", | |
| "import nrrd\n", | |
| "from tqdm.notebook import tqdm" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "ct, header = nrrd.read(\"./Data/tal.nrrd\")\n", | |
| "scale = [\n", | |
| " header[\"space directions\"][0,0],\n", | |
| " header[\"space directions\"][1,1],\n", | |
| " header[\"space directions\"][2,2]\n", | |
| "]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "application/vnd.jupyter.widget-view+json": { | |
| "model_id": "21437654228746b083de6708761ff9c0", | |
| "version_major": 2, | |
| "version_minor": 0 | |
| }, | |
| "text/plain": [ | |
| " 0%| | 0/118 [00:00<?, ?it/s]" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| } | |
| ], | |
| "source": [ | |
| "x_dim, y_dim, z_dim = ct.shape\n", | |
| "density_sum = 0\n", | |
| "avg_location = np.array([0,0,0], dtype=np.uint64)\n", | |
| "for i in tqdm(range(x_dim)):\n", | |
| " for j in range(y_dim):\n", | |
| " for k in range(z_dim):\n", | |
| " density_sum += ct[i,j,k]\n", | |
| " avg_location = avg_location + (np.array([i,j,k]) * ct[i,j,k])" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "20.232262548793365 21.750312875643534 24.423974407400138\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "COM_IDX = avg_location/density_sum\n", | |
| "print(COM_IDX[0] *scale[0], COM_IDX[1] * scale[1], COM_IDX[2] * scale[2])" | |
| ] | |
| } | |
| ], | |
| "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.10.11" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment