Skip to content

Commit

Permalink
fix: Paths are accepted now
Browse files Browse the repository at this point in the history
  • Loading branch information
Karol-G committed Aug 19, 2024
1 parent f89a0ce commit 4a82dc0
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion medvol/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = "0.0.11"
__version__ = "0.0.12"

from medvol.medvol import MedVol
7 changes: 4 additions & 3 deletions medvol/medvol.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import SimpleITK as sitk
from typing import Dict, Optional, Union, List, Tuple
import numpy as np
from pathlib import Path

# TODO:
# - Enable user to set affine
Expand All @@ -23,7 +24,7 @@ def __init__(self,
is_seg: Optional[bool] = None,
copy: Optional['MedVol'] = None) -> None:
# Validate array: Must be a 2D, 3D or 4D array
if not ((isinstance(array, np.ndarray) and (array.ndim == 2 or array.ndim == 3 or array.ndim == 4)) or isinstance(array, str)):
if not ((isinstance(array, np.ndarray) and (array.ndim == 2 or array.ndim == 3 or array.ndim == 4)) or isinstance(array, (str, Path))):
raise ValueError("Array must be a 2D, 3D or 4D numpy array or a filepath string.")
elif isinstance(array, str) and (spacing is not None or origin is not None or direction is not None or header is not None or is_seg is not None or copy is not None):
raise RuntimeError("Spacing, origin, direction, header, is_seg or copy cannot be set if array is a string to load an image.")
Expand Down Expand Up @@ -129,7 +130,7 @@ def _copy_fields_from(self, other: 'MedVol'):
self.is_seg = other.is_seg

def _load(self, filepath):
image_sitk = sitk.ReadImage(filepath)
image_sitk = sitk.ReadImage(str(filepath))
array = sitk.GetArrayFromImage(image_sitk)
ndims = len(array.shape)
metadata_ndims = len(image_sitk.GetSpacing())
Expand Down Expand Up @@ -170,4 +171,4 @@ def save(self, filepath):
if self.header is not None:
for key, value in self.header.items():
image_sitk.SetMetaData(key, value)
sitk.WriteImage(image_sitk, filepath, useCompression=True)
sitk.WriteImage(image_sitk, str(filepath), useCompression=True)

0 comments on commit 4a82dc0

Please sign in to comment.