Skip to content

Commit

Permalink
BUG: pythonic itk should understand pathlib.Path
Browse files Browse the repository at this point in the history
  • Loading branch information
dyollb committed Oct 12, 2021
1 parent 94dfea4 commit 051ad61
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
5 changes: 3 additions & 2 deletions Wrapping/Generators/Python/Tests/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import sys
import os
import numpy as np
import pathlib

import itk

Expand Down Expand Up @@ -142,7 +143,7 @@ def custom_callback(name, progress):
itk.imwrite(reader, sys.argv[4], True)

# test read
image = itk.imread(filename)
image = itk.imread(pathlib.Path(filename))
assert type(image) == itk.Image[itk.RGBPixel[itk.UC], 2]
image = itk.imread(filename, itk.F)
assert type(image) == itk.Image[itk.F, 2]
Expand Down Expand Up @@ -206,7 +207,7 @@ def custom_callback(name, progress):
assert png_io.GetFileName() == filename

# test reading image series
series_reader = itk.ImageSeriesReader.New(FileNames=[filename, filename])
series_reader = itk.ImageSeriesReader.New(FileNames=[pathlib.Path(filename), pathlib.Path(filename)])
series_reader.Update()
assert series_reader.GetOutput().GetImageDimension() == 3
assert series_reader.GetOutput().GetLargestPossibleRegion().GetSize()[2] == 2
Expand Down
22 changes: 11 additions & 11 deletions Wrapping/Generators/Python/itk/support/extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ def imwrite(
tmp_auto_pipeline = auto_pipeline.current
auto_pipeline.current = None
writer = itk.ImageFileWriter[type(img)].New(
Input=img, FileName=filename, UseCompression=compression
Input=img, FileName=f"{filename}", UseCompression=compression
)
auto_pipeline.current = tmp_auto_pipeline
if imageio:
Expand Down Expand Up @@ -942,7 +942,7 @@ def imread(
names_generator = itk.GDCMSeriesFileNames.New()
names_generator.SetUseSeriesDetails(True)
names_generator.AddSeriesRestriction("0008|0021") # Series Date
names_generator.SetDirectory(filename)
names_generator.SetDirectory(f"{filename}")
series_uid = names_generator.GetSeriesUIDs()
if len(series_uid) == 0:
raise FileNotFoundError(f"no DICOMs in: {filename}.")
Expand All @@ -954,14 +954,14 @@ def imread(
filename = names_generator.GetFileNames(series_identifier)
if type(filename) in [list, tuple]:
template_reader_type = itk.ImageSeriesReader
io_filename = filename[0]
io_filename = f"{filename[0]}"
increase_dimension = True
kwargs = {"FileNames": filename}
kwargs = {"FileNames": [f"{f}" for f in filename]}
else:
template_reader_type = itk.ImageFileReader
io_filename = filename
io_filename = f"{filename}"
increase_dimension = False
kwargs = {"FileName": filename}
kwargs = {"FileName": f"{filename}"}
if imageio:
kwargs["ImageIO"] = imageio
if pixel_type:
Expand Down Expand Up @@ -1006,7 +1006,7 @@ def meshwrite(
tmp_auto_pipeline = auto_pipeline.current
auto_pipeline.current = None
writer = itk.MeshFileWriter[type(mesh)].New(
Input=mesh, FileName=filename, UseCompression=compression
Input=mesh, FileName=f"{filename}", UseCompression=compression
)
auto_pipeline.current = tmp_auto_pipeline
writer.Update()
Expand Down Expand Up @@ -1041,9 +1041,9 @@ def meshread(
except (KeyError, itk.TemplateTypeError):
pass
TemplateReaderType = itk.MeshFileReader
io_filename = filename
io_filename = f"{filename}"
increase_dimension = False
kwargs = {"FileName": filename}
kwargs = {"FileName": f"{filename}"}
if pixel_type:
meshIO = itk.MeshIOFactory.CreateMeshIO(
io_filename, itk.CommonEnums.IOFileMode_ReadMode
Expand Down Expand Up @@ -1081,7 +1081,7 @@ def transformread(filename: fileiotype) -> List["itkt.TransformBase"]:
import itk

reader = itk.TransformFileReaderTemplate[itk.D].New()
reader.SetFileName(str(filename))
reader.SetFileName(f"{filename}")
reader.Update()

transforms = []
Expand Down Expand Up @@ -1116,7 +1116,7 @@ def transformwrite(
import itk

writer = itk.TransformFileWriterTemplate[itk.D].New()
writer.SetFileName(filename)
writer.SetFileName(f"{filename}")
writer.SetUseCompression(compression)
for transform in transforms:
writer.AddTransform(transform)
Expand Down

0 comments on commit 051ad61

Please sign in to comment.