Skip to content

Commit

Permalink
Merge pull request #568 from Deep-MI/numpy2
Browse files Browse the repository at this point in the history
Numpy2
  • Loading branch information
m-reuter authored Sep 2, 2024
2 parents 37a0b56 + 24054e3 commit 609f9e0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
10 changes: 6 additions & 4 deletions FastSurferCNN/data_loader/conform.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,8 @@ def conform(
# Pxyz is the center of the image in world coords

# target scalar type and dtype
sctype = np.uint8 if dtype is None else np.obj2sctype(dtype, default=np.uint8)
#sctype = np.uint8 if dtype is None else np.obj2sctype(dtype, default=np.uint8)
sctype = np.uint8 if dtype is None else np.dtype(dtype).type
target_dtype = np.dtype(sctype)

src_min, scale = 0, 1.0
Expand Down Expand Up @@ -761,7 +762,7 @@ def is_conform(
raise ValueError(f"ERROR: Multiple input frames ({ishape[3]}) not supported!")

checks = {
f"Number of Dimensions 3": (len(ishape) == 3, f"image ndim {img.ndim}")
"Number of Dimensions 3": (len(ishape) == 3, f"image ndim {img.ndim}")
}
# check dimensions
if Criteria.FORCE_IMG_SIZE in criteria:
Expand All @@ -775,7 +776,7 @@ def is_conform(
_vox_sizes = conformed_vox_size if is_correct_vox_size else izoom[:3]
if Criteria.FORCE_ISO_VOX in criteria:
vox_size_criteria = f"Voxel Size {'x'.join([str(conformed_vox_size)] * 3)}"
image_vox_size = f"image " + "x".join(map(str, izoom))
image_vox_size = "image " + "x".join(map(str, izoom))
checks[vox_size_criteria] = (is_correct_vox_size, image_vox_size)

# check orientation LIA
Expand All @@ -795,7 +796,8 @@ def is_conform(
if dtype is None or (isinstance(dtype, str) and dtype.lower() == "uchar"):
dtype = "uint8"
else: # assume obj
dtype = np.dtype(np.obj2sctype(dtype)).name
#dtype = np.dtype(np.obj2sctype(dtype)).name
dtype = np.dtype(dtype).type.__name__
is_correct_dtype = img.get_data_dtype() == dtype
checks[f"Dtype {dtype}"] = (is_correct_dtype, f"dtype {img.get_data_dtype()}")

Expand Down
13 changes: 6 additions & 7 deletions Tutorial/Tutorial_FastSurferCNN_QuickSeg.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@
"#@title Here we first setup the environment by downloading the open source deep-mi/fastsurfer project and the required packages\n",
"import os\n",
"import sys\n",
"from os.path import exists, join, basename, splitext\n",
"from os.path import exists, basename, splitext\n",
"\n",
"print(\"Starting setup. This could take a few minutes\")\n",
"print(\"----------------------------------------------\")\n",
Expand Down Expand Up @@ -474,12 +474,12 @@
"source": [
"#@title Click this run button, if you would prefer to download the segmentation in nifti-format\n",
"import nibabel as nib\n",
"from google.colab import files\n",
"# conversion to nifti\n",
"data = nib.load(f'{SETUP_DIR}fastsurfer_seg/Tutorial/mri/aparc.DKTatlas+aseg.deep.mgz')\n",
"img_nifti = nib.Nifti1Image(data.get_fdata(), data.affine, header=nib.Nifti1Header())\n",
"nib.nifti1.save(img_nifti, f'{SETUP_DIR}fastsurfer_seg/Tutorial/mri/aparc.DKTatlas+aseg.deep.nii.gz')\n",
"\n",
"from google.colab import files\n",
"files.download(f'{SETUP_DIR}fastsurfer_seg/Tutorial/mri/aparc.DKTatlas+aseg.deep.nii.gz')\n"
]
},
Expand Down Expand Up @@ -519,12 +519,12 @@
"source": [
"#@title Click this run button, if you would prefer to download the image in nifti-format\n",
"import nibabel as nib\n",
"from google.colab import files\n",
"# conversion to nifti\n",
"data = nib.load(f\"{SETUP_DIR}140_orig.mgz\")\n",
"img_nifti = nib.Nifti1Image(data.get_fdata(), data.affine, header=nib.Nifti1Header())\n",
"nib.nifti1.save(img_nifti, f\"{SETUP_DIR}140_orig.nii.gz\")\n",
"\n",
"from google.colab import files\n",
"files.download(f\"{SETUP_DIR}140_orig.nii.gz\")\n"
]
},
Expand Down Expand Up @@ -612,11 +612,11 @@
"%matplotlib inline\n",
"import nibabel as nib\n",
"import matplotlib.pyplot as plt\n",
"plt.style.use('seaborn-v0_8-whitegrid')\n",
"from skimage import color\n",
"import torch\n",
"import numpy as np\n",
"from skimage import color\n",
"from torchvision import utils\n",
"plt.style.use('seaborn-v0_8-whitegrid')\n",
"\n",
"def plot_predictions(image, pred):\n",
" \"\"\"\n",
Expand Down Expand Up @@ -676,7 +676,6 @@
"from ipywidgets import widgets\n",
"import matplotlib.pyplot as plt\n",
"import nibabel as nib\n",
"import numpy as np\n",
"#from mpl_toolkits.mplot3d.art3d import Poly3DCollection\n",
"from skimage import measure\n",
"\n",
Expand Down Expand Up @@ -853,7 +852,7 @@
"def plot_3d_plotly_shape(structure, hemisphere, show_mesh=True, crop=True, grid=True):\n",
" import plotly.graph_objects as go\n",
" label = label_lookups(structure, hemisphere)\n",
" test_cond = np.in1d(pred_data, label).reshape(pred_data.shape)\n",
" test_cond = np.isin(pred_data, label).reshape(pred_data.shape)\n",
" roi = np.where(test_cond, 1, 0)\n",
" vert_p, faces_p, normals_p, values_p = measure.marching_cubes(roi, 0, spacing=(1, 1, 1))\n",
"\n",
Expand Down
6 changes: 3 additions & 3 deletions recon_surf/N4_bias_correct.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import logging
import sys
from pathlib import Path
from typing import Optional, cast, Literal, TypeVar, Callable, get_args, Union
from typing import Optional, cast, Literal, TypeVar, Callable

# Group 2: External modules
import SimpleITK as sitk
Expand Down Expand Up @@ -499,9 +499,9 @@ def read_talairach_xfm(fname: Path | str) -> np.ndarray:
try:
transform_iter = iter(lines)
# advance transform_iter to linear header
_ = next(l for l in transform_iter if l.lower().startswith("linear_"))
_ = next(ln for ln in transform_iter if ln.lower().startswith("linear_"))
# return the next 3 lines in transform_lines
transform_lines = (l for l, _ in zip(transform_iter, range(3)))
transform_lines = (ln for ln, _ in zip(transform_iter, range(3)))
tal_str = [ln.replace(";", " ") for ln in transform_lines]
tal = np.genfromtxt(tal_str)
tal = np.vstack([tal, [0, 0, 0, 1]])
Expand Down

0 comments on commit 609f9e0

Please sign in to comment.