-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #230 from clEsperanto/maximum_position
Added maximum position function
- Loading branch information
Showing
11 changed files
with
742 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,384 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "870625e2-dcd1-4dc1-9e87-a6f9773238d7", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import scipy.ndimage as ndi\n", | ||
"import numpy as np\n", | ||
"import pyclesperanto_prototype as cle" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "a430434b-177c-41fa-b39b-7e5b37531df4", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"array3d = np.random.rand(100, 100, 100)\n", | ||
"array3d[11, 6, 0] = 1\n", | ||
"array3d[10, 48, 0] = 1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "c21e7352-28ff-4bd5-bdce-a62d0d363a8a", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"(10, 48, 0)\n", | ||
"(10, 48, 0)\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(ndi.maximum_position(array3d))\n", | ||
"print(cle.maximum_position(array3d))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "f577db23-9530-4a24-b3f3-463ec4bc84ba", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"array2d = np.random.rand(100, 80)\n", | ||
"array2d[89, 2] = 1\n", | ||
"array2d[90, 40] = 1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "0ca92364-832f-412d-885c-3f4778c7203b", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"(89, 2)\n", | ||
"(89, 2)\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(ndi.maximum_position(array2d))\n", | ||
"print(cle.maximum_position(array2d))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "f68acd41-2ee1-4700-a4e2-be921eff31f2", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"array1d = np.random.rand(100)\n", | ||
"array1d[56] = 1\n", | ||
"array1d[20] = 1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 7, | ||
"id": "42e0a94f-497a-4efd-8fd3-0fa947c352ff", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"(20,)\n", | ||
"(20,)\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(ndi.maximum_position(array1d))\n", | ||
"print(cle.maximum_position(array1d))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 8, | ||
"id": "baba9ca4-1c35-410b-86bc-ddb7650add1c", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"[30 50 20]\n", | ||
"[1000 20 1]\n", | ||
"[10, 20979]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"array = np.random.randint(0, 10, (30, 50, 20))\n", | ||
"array[20, 48, 19] = 10\n", | ||
"\n", | ||
"dims = np.array(np.asarray(array).shape)\n", | ||
"# see numpy.unravel_index to understand this line.\n", | ||
"dim_prod = np.cumprod([1] + list(dims[:0:-1]))[::-1]\n", | ||
"\n", | ||
"print(dims)\n", | ||
"print(dim_prod)\n", | ||
"positions = np.arange(array.size).reshape(array.shape)\n", | ||
"\n", | ||
"result = []\n", | ||
"result += [array.max()]\n", | ||
"result += [positions[array == array.max()][0]]\n", | ||
"\n", | ||
"print(result)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 9, | ||
"id": "87d554c1-a0d4-46c1-adbe-5603c21f8937", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"(5, 8, 3)\n", | ||
"[[[7 4 1]\n", | ||
" [3 9 4]\n", | ||
" [9 3 2]\n", | ||
" [2 7 2]\n", | ||
" [1 3 6]\n", | ||
" [1 2 7]\n", | ||
" [0 4 7]\n", | ||
" [3 6 2]]\n", | ||
"\n", | ||
" [[4 9 2]\n", | ||
" [1 4 8]\n", | ||
" [9 7 3]\n", | ||
" [2 9 4]\n", | ||
" [3 3 6]\n", | ||
" [7 3 8]\n", | ||
" [1 3 7]\n", | ||
" [5 9 0]]\n", | ||
"\n", | ||
" [[9 9 3]\n", | ||
" [3 3 9]\n", | ||
" [3 7 0]\n", | ||
" [6 2 2]\n", | ||
" [6 1 7]\n", | ||
" [0 9 8]\n", | ||
" [5 6 4]\n", | ||
" [1 6 7]]\n", | ||
"\n", | ||
" [[5 5 5]\n", | ||
" [7 0 0]\n", | ||
" [3 0 5]\n", | ||
" [5 4 6]\n", | ||
" [8 6 9]\n", | ||
" [1 1 8]\n", | ||
" [7 2 7]\n", | ||
" [1 2 1]]\n", | ||
"\n", | ||
" [[2 6 7]\n", | ||
" [8 6 9]\n", | ||
" [0 1 7]\n", | ||
" [8 4 5]\n", | ||
" [8 7 1]\n", | ||
" [6 4 0]\n", | ||
" [4 3 4]\n", | ||
" [8 3 9]]]\n", | ||
"[[7. 9. 9. 5. 7.]\n", | ||
" [9. 8. 9. 7. 9.]\n", | ||
" [9. 9. 7. 5. 7.]\n", | ||
" [7. 9. 6. 6. 8.]\n", | ||
" [6. 6. 7. 9. 8.]\n", | ||
" [7. 8. 9. 8. 6.]\n", | ||
" [7. 7. 6. 7. 4.]\n", | ||
" [6. 9. 7. 2. 9.]]\n", | ||
"[[9. 9. 7. 7. 7.]\n", | ||
" [9. 9. 8. 8. 8.]\n", | ||
" [9. 9. 9. 9. 9.]\n", | ||
" [8. 6. 9. 9. 9.]\n", | ||
" [8. 7. 9. 9. 9.]\n", | ||
" [8. 7. 9. 9. 9.]\n", | ||
" [8. 7. 9. 9. 9.]\n", | ||
" [8. 7. 9. 9. 9.]]\n", | ||
"[[9. 9. 7. 7. 7.]\n", | ||
" [8. 9. 9. 9. 9.]\n", | ||
" [9. 7. 7. 7. 7.]\n", | ||
" [8. 9. 6. 6. 6.]\n", | ||
" [8. 7. 9. 9. 9.]\n", | ||
" [7. 9. 8. 8. 8.]\n", | ||
" [7. 6. 7. 7. 7.]\n", | ||
" [8. 9. 9. 9. 9.]]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"array = np.random.randint(0, 10, (5, 8, 3))\n", | ||
"dimensionality = array.shape\n", | ||
"print(dimensionality)\n", | ||
"print(array)\n", | ||
"temp_max = cle.create([1, dimensionality[1], dimensionality[0]])\n", | ||
"pos_x = cle.create([1, dimensionality[1], dimensionality[0]])\n", | ||
"\n", | ||
"temp_max = cle.maximum_x_projection(array)\n", | ||
"cle.x_position_of_maximum_x_projection(array, pos_x)\n", | ||
"print(temp_max)\n", | ||
"\n", | ||
"source = temp_max\n", | ||
"pos_y = cle.create([1, 1, dimensionality[0]])\n", | ||
"temp_max = cle.maximum_y_projection(array, temp_max)\n", | ||
"cle.y_position_of_maximum_y_projection(array, pos_y)\n", | ||
"print(temp_max)\n", | ||
"\n", | ||
"source = temp_max\n", | ||
"pos_z = cle.create([1, 1, 1])\n", | ||
"temp_max = cle.maximum_z_projection(array, temp_max)\n", | ||
"cle.z_position_of_maximum_z_projection(array, pos_z)\n", | ||
"print(temp_max)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"id": "5bd99c52-a7a6-4dd9-abd3-4a5d4d5b28b1", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"[[9 2 2 2 2 5 4 9]\n", | ||
" [3 4 3 1 1 5 1 6]\n", | ||
" [7 7 1 2 6 3 8 4]\n", | ||
" [5 4 9 2 7 9 6 4]\n", | ||
" [8 6 5 8 4 2 3 5]\n", | ||
" [3 3 5 2 3 6 5 2]]\n", | ||
"[[9.]\n", | ||
" [6.]\n", | ||
" [8.]\n", | ||
" [9.]\n", | ||
" [8.]\n", | ||
" [6.]]\n", | ||
"[[9.]\n", | ||
" [6.]\n", | ||
" [8.]\n", | ||
" [9.]\n", | ||
" [8.]\n", | ||
" [6.]]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"array = np.random.randint(0, 10, (6, 8))\n", | ||
"dimensionality = array.shape\n", | ||
"\n", | ||
"print(array)\n", | ||
"\n", | ||
"temp_max = cle.create([dimensionality[0], 1])\n", | ||
"pos_x = cle.create([dimensionality[0], 1])\n", | ||
"\n", | ||
"print(cle.maximum_x_projection(array))\n", | ||
"cle.maximum_x_projection(array, temp_max)\n", | ||
"print(temp_max)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"id": "82ee9f36-c393-4ca7-8e0d-a2ea5b2aea0c", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"[[7.]\n", | ||
" [9.]\n", | ||
" [6.]\n", | ||
" [9.]\n", | ||
" [9.]\n", | ||
" [9.]]\n", | ||
"[[9.]]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"array = np.random.randint(0, 10, (6, 8))\n", | ||
"max_ = cle.maximum_x_projection(array)\n", | ||
"print(max_)\n", | ||
"\n", | ||
"max_ = cle.maximum_y_projection(max_)\n", | ||
"print(max_)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 12, | ||
"id": "9e240c69-181d-45ba-81c3-3b10f2336b13", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"4 2 0\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"array3d = np.random.randint(0, 10, (6, 4, 3))\n", | ||
"array3d[4, 2, 0] = 10\n", | ||
"\n", | ||
"temp = cle.maximum_x_projection(array3d)\n", | ||
"x_pos = cle.x_position_of_maximum_x_projection(array3d)\n", | ||
"source = temp\n", | ||
"\n", | ||
"temp = cle.maximum_y_projection(source)\n", | ||
"y_pos = cle.y_position_of_maximum_y_projection(source)\n", | ||
"source = temp\n", | ||
"\n", | ||
"z_pos = cle.x_position_of_maximum_x_projection(source)\n", | ||
"\n", | ||
"z_coord = int(z_pos[0][0])\n", | ||
"y_coord = int(y_pos[0][z_coord])\n", | ||
"x_coord = int(x_pos[y_coord][z_coord])\n", | ||
"\n", | ||
"print(z_coord, y_coord, x_coord)" | ||
] | ||
} | ||
], | ||
"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.9.15" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains 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
Oops, something went wrong.