From 051ad61e26a8c0b282d1abbd26b0bfecda84f699 Mon Sep 17 00:00:00 2001 From: Bryn Lloyd Date: Tue, 12 Oct 2021 19:17:49 +0200 Subject: [PATCH] BUG: pythonic itk should understand pathlib.Path --- Wrapping/Generators/Python/Tests/extras.py | 5 +++-- .../Generators/Python/itk/support/extras.py | 22 +++++++++---------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Wrapping/Generators/Python/Tests/extras.py b/Wrapping/Generators/Python/Tests/extras.py index 4122ab0e33b..6d305cb0753 100644 --- a/Wrapping/Generators/Python/Tests/extras.py +++ b/Wrapping/Generators/Python/Tests/extras.py @@ -21,6 +21,7 @@ import sys import os import numpy as np +import pathlib import itk @@ -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] @@ -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 diff --git a/Wrapping/Generators/Python/itk/support/extras.py b/Wrapping/Generators/Python/itk/support/extras.py index 0745207fd1f..e690463bf0e 100644 --- a/Wrapping/Generators/Python/itk/support/extras.py +++ b/Wrapping/Generators/Python/itk/support/extras.py @@ -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: @@ -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}.") @@ -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: @@ -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() @@ -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 @@ -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 = [] @@ -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)