Read an fdf file in python

I’m trying to read an .fdf file in python. I installed the relevant package via:
pip install itk-iofdf
I then tried the following:

import itk
from itk import IOFDF
x = itk.image_file_reader(filename="M0slice002.fdf" )

which resulted in:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jtduda/miniconda3/lib/python3.7/site-packages/itk/support/itkHelpers.py", line 211, in image_filter_wrapper
    return image_filter(*args, **kwargs)
  File "/Users/jtduda/miniconda3/lib/python3.7/site-packages/itk/itkImageFileReaderPython.py", line 5694, in image_file_reader
    instance = itk.ImageFileReader.New(*args, **kwargs)
  File "/Users/jtduda/miniconda3/lib/python3.7/site-packages/itk/support/itkTemplate.py", line 653, in New
    itk.ImageFileReader, False, "FileName", *args, **kwargs
  File "/Users/jtduda/miniconda3/lib/python3.7/site-packages/itk/support/itkTemplate.py", line 147, in _NewImageReader
    raise RuntimeError("No FileName specified.")
RuntimeError: No FileName specified.

I also tried:
x = itk.image_file_reader(filename="M0slice002.fdf",imageio=itk.FDFImageIO.New() )
which resulted in:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/jtduda/miniconda3/lib/python3.7/site-packages/itk/itkFDFImageIOPython.py", line 118, in New
    import itkTemplate
ModuleNotFoundError: No module named 'itkTemplate'

Any advice on how I can use the package to read in an fdf file would be greatly appreciated.

Hi @jtduda ,

A new version of itk-iofdf was released based on itk-5.2rc3. It can be installed with:

pip install --upgrade --pre itk-iofdf

Then,

import itk

imageio = itk.FDFImageIO.New()
image = itk.imread("M0slice002.fdf", imageio=imageio)

Thanks for your response, I tried what you said, and now after imageio = itk.FDFImageIO.New() I get the following error:
Traceback (most recent call last):
File “”, line 1, in
File “/Users/jtduda/miniconda3/lib/python3.7/site-packages/itk/itkFDFImageIOPython.py”, line 118, in New
import itkTemplate
ModuleNotFoundError: No module named ‘itkTemplate’

If I try the same thing, but with a built in IO type it seems to work fine, e.g. imageio = itk.NiftiImageIO.New()

Perhaps a fresh conda environment is required to ensure all the required package versions are installed?

Looks like it is a python version issue. I get the same error on a clean environment with python=3.7.3, but it works on a clean environment with python=3.6. Thanks for your help with this.

2 Likes