Skip to content

Commit a6bcfdc

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 8579ce1 commit a6bcfdc

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

nvidia_nims/vista_3d_remote_nim.ipynb

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
"import zipfile\n",
110110
"\n",
111111
"import itk\n",
112-
"import pyvista as pv\n"
112+
"import pyvista as pv"
113113
]
114114
},
115115
{
@@ -133,28 +133,25 @@
133133
"metadata": {},
134134
"outputs": [],
135135
"source": [
136-
"def post_image_to_fileio(input_image:itk.Image) -> str:\n",
137-
" '''Post an image to file.io and return the link.'''\n",
136+
"def post_image_to_fileio(input_image: itk.Image) -> str:\n",
137+
" \"\"\"Post an image to file.io and return the link.\"\"\"\n",
138138
"\n",
139139
" # Save the image to a temporary file\n",
140140
" # Note: The Vista-3D NIM on NVAIE is limited to reading .nii.gz files\n",
141141
" tmp_dir = tempfile.mkdtemp()\n",
142142
" tmp_filename = os.path.join(tmp_dir, \"tmp.nii.gz\")\n",
143143
" itk.imwrite(input_image, tmp_filename)\n",
144-
" \n",
144+
"\n",
145145
" # Upload the file\n",
146146
" with open(os.path.join(tmp_filename), \"rb\") as f:\n",
147-
" res = requests.post(\n",
148-
" \"https://file.io/\",\n",
149-
" files={\"file\": f}\n",
150-
" )\n",
147+
" res = requests.post(\"https://file.io/\", files={\"file\": f})\n",
151148
" if res.status_code != 200:\n",
152149
" raise RuntimeError(f\"Cannot upload file. The response {res}.\")\n",
153-
" \n",
150+
"\n",
154151
" # Get the link\n",
155152
" res = res.json()\n",
156153
" link = res[\"link\"]\n",
157-
" \n",
154+
"\n",
158155
" return link"
159156
]
160157
},
@@ -177,49 +174,53 @@
177174
"metadata": {},
178175
"outputs": [],
179176
"source": [
180-
"def nvaie_vista3d_nim(api_key:str, input_image:itk.Image) -> list:\n",
181-
" '''Run the MONAI VISTA 3D model on the input image and return the result.'''\n",
177+
"def nvaie_vista3d_nim(api_key: str, input_image: itk.Image) -> list:\n",
178+
" \"\"\"Run the MONAI VISTA 3D model on the input image and return the result.\"\"\"\n",
182179
"\n",
183180
" # The API endpoint for the MONAI VISTA 3D model on NVIDIA AI Enterprise\n",
184181
" invoke_url = \"https://health.api.nvidia.com/v1/medicalimaging/nvidia/vista-3d\"\n",
185-
" \n",
182+
"\n",
186183
" # Check the input image\n",
187184
" assert len(input_image.GetSpacing()) == 3, \"The input image must be 3D.\"\n",
188-
" isotropy = ((input_image.GetSpacing()[1] / input_image.GetSpacing()[0]) +\n",
189-
" (input_image.GetSpacing()[2] / input_image.GetSpacing()[0])) / 2\n",
185+
" isotropy = (\n",
186+
" (input_image.GetSpacing()[1] / input_image.GetSpacing()[0])\n",
187+
" + (input_image.GetSpacing()[2] / input_image.GetSpacing()[0])\n",
188+
" ) / 2\n",
190189
" if isotropy < 0.9 or isotropy > 1.1 or input_image.GetSpacing()[0] != 1.5:\n",
191190
" print(\"WARNING: The input image should have 1.5 mm isotropic spacing. Performance will be degraded.\")\n",
192191
" print(\" The input image has spacing:\", input_image.GetSpacing())\n",
193192
" input_image_arr = itk.array_view_from_image(input_image)\n",
194193
" minv = input_image_arr.min()\n",
195194
" maxv = input_image_arr.max()\n",
196195
" if minv < -1024 or maxv > 3071:\n",
197-
" print(\"WARNING: The input image should have Hounsfield Units in the range [-1024, 3071]. Performance will be degraded.\")\n",
196+
" print(\n",
197+
" \"WARNING: The input image should have Hounsfield Units in the range [-1024, 3071]. Performance will be degraded.\"\n",
198+
" )\n",
198199
" print(\" The input image has Hounsfield Units in the range:\", [minv, maxv])\n",
199200
"\n",
200201
" # Post the image to file.io and get the link\n",
201202
" input_image_url = post_image_to_fileio(input_image)\n",
202203
"\n",
203204
" # Define the header and payload for the API call\n",
204205
" header = {\n",
205-
" \"Authorization\": \"Bearer \" + api_key, \n",
206+
" \"Authorization\": \"Bearer \" + api_key,\n",
206207
" }\n",
207-
" \n",
208+
"\n",
208209
" payload = {\n",
209210
" \"image\": input_image_url,\n",
210211
" # Optionally limited processing to specific classes\n",
211-
" #\"prompts\": {\n",
212+
" # \"prompts\": {\n",
212213
" # \"classes\": [\"liver\", \"spleen\"]\n",
213-
" #}\n",
214+
" # }\n",
214215
" }\n",
215216
"\n",
216217
" # Call the API\n",
217218
" session = requests.Session()\n",
218219
" response = session.post(invoke_url, headers=header, json=payload)\n",
219-
" \n",
220+
"\n",
220221
" # Check the response\n",
221222
" response.raise_for_status()\n",
222-
" \n",
223+
"\n",
223224
" # Get the result\n",
224225
" with tempfile.TemporaryDirectory() as temp_dir:\n",
225226
" z = zipfile.ZipFile(io.BytesIO(response.content))\n",
@@ -230,7 +231,7 @@
230231
" if os.path.isfile(filepath) and filename.endswith(\".nrrd\"):\n",
231232
" # SUCCESS: Return the results\n",
232233
" return itk.imread(filepath, pixel_type=itk.SS)\n",
233-
" \n",
234+
"\n",
234235
" # FAILURE: Return None\n",
235236
" return None"
236237
]
@@ -252,7 +253,7 @@
252253
"metadata": {},
253254
"outputs": [],
254255
"source": [
255-
"ngc_api_key = os.environ['NGC_API_KEY']"
256+
"ngc_api_key = os.environ[\"NGC_API_KEY\"]"
256257
]
257258
},
258259
{
@@ -280,13 +281,10 @@
280281
" os.makedirs(monai_data_directory, exist_ok=True)\n",
281282
" else:\n",
282283
" monai_data_directory = tempfile.mkdtemp()\n",
283-
" input_image_filename = os.path.join(\n",
284-
" monai_data_directory,\n",
285-
" \"vista3d-example-1.nii.gz\"\n",
286-
" )\n",
284+
" input_image_filename = os.path.join(monai_data_directory, \"vista3d-example-1.nii.gz\")\n",
287285
" if not os.path.exists(input_image_filename):\n",
288286
" resp = requests.get(\"https://assets.ngc.nvidia.com/products/api-catalog/vista3d/example-1.nii.gz\")\n",
289-
" with open(input_image_filename, \"wb\") as f: \n",
287+
" with open(input_image_filename, \"wb\") as f:\n",
290288
" f.write(resp.content)"
291289
]
292290
},
@@ -386,14 +384,14 @@
386384
],
387385
"source": [
388386
"# For visualization purposes, we will clip the contours half-way along the x-axis\n",
389-
"clipped = contours.clip('x')\n",
387+
"clipped = contours.clip(\"x\")\n",
390388
"\n",
391389
"# Render the clipped contours and the original image\n",
392390
"pl = pv.Plotter()\n",
393391
"pl.add_mesh(clipped, cmap=\"pink\", show_scalar_bar=False, opacity=1.0)\n",
394-
"pl.add_volume(image, clim=[50,800], cmap=\"pink\", show_scalar_bar=False)\n",
392+
"pl.add_volume(image, clim=[50, 800], cmap=\"pink\", show_scalar_bar=False)\n",
395393
"pl.set_background(\"black\")\n",
396-
"pl.camera_position = 'xz'\n",
394+
"pl.camera_position = \"xz\"\n",
397395
"pl.show()"
398396
]
399397
},
@@ -423,15 +421,15 @@
423421
"pl2 = pv.Plotter(shape=(3, 1))\n",
424422
"\n",
425423
"# The normal directions (dirs) of the slices and the positions (pos) of the camera for each view\n",
426-
"dirs = ['x', 'y', 'z']\n",
427-
"pos = ['yz', 'xz', 'xy']\n",
424+
"dirs = [\"x\", \"y\", \"z\"]\n",
425+
"pos = [\"yz\", \"xz\", \"xy\"]\n",
428426
"\n",
429427
"# Extract the slices and generate the views for each subplot\n",
430428
"for i, dir in enumerate(dirs):\n",
431429
" image_slice = image.slice(normal=dir)\n",
432430
" contours_slice = labels.slice(normal=dir, contour=True)\n",
433-
" pl2.subplot(i,0)\n",
434-
" pl2.add_mesh(image_slice, cmap=\"bone\", clim=[-200,500], show_scalar_bar=False, opacity=1.0)\n",
431+
" pl2.subplot(i, 0)\n",
432+
" pl2.add_mesh(image_slice, cmap=\"bone\", clim=[-200, 500], show_scalar_bar=False, opacity=1.0)\n",
435433
" pl2.add_mesh(contours_slice, cmap=\"viridis\", show_scalar_bar=False, opacity=1.0, line_width=5)\n",
436434
" pl2.camera_position = pos[i]\n",
437435
"\n",

0 commit comments

Comments
 (0)