Skip to content

Commit

Permalink
WIP: Fix FFT factory names and templates_loaded flag
Browse files Browse the repository at this point in the history
  • Loading branch information
tbirdso committed Mar 2, 2022
1 parent 01740fd commit e5bd53e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
Expand Up @@ -16,8 +16,8 @@
*
*=========================================================================*/

#ifndef itkFFTImageFilterFactoryRegisterManager_h
#define itkFFTImageFilterFactoryRegisterManager_h
#ifndef itkFFTImageFilterInitFactoryRegisterManager_h
#define itkFFTImageFilterInitFactoryRegisterManager_h

namespace itk {

Expand All @@ -27,10 +27,10 @@ namespace itk {
//
@LIST_OF_FACTORIES_REGISTRATION@

class FFTImageFilterFactoryRegisterManager
class FFTImageFilterInitFactoryRegisterManager
{
public:
explicit FFTImageFilterFactoryRegisterManager(void (* const list[])(void))
explicit FFTImageFilterInitFactoryRegisterManager(void (* const list[])(void))
{
for(;*list != nullptr; ++list)
{
Expand All @@ -44,10 +44,10 @@ class FFTImageFilterFactoryRegisterManager
// application translation units. Note that this code will be expanded in the
// ITK-based applications and not in ITK itself.
//
void (* const FFTImageFilterFactoryRegisterRegisterList[])(void) = {
void (* const FFTImageFilterInitFactoryRegisterRegisterList[])(void) = {
@LIST_OF_FACTORY_NAMES@
nullptr};
const FFTImageFilterFactoryRegisterManager FFTImageFilterFactoryRegisterManagerInstance(FFTImageFilterFactoryRegisterRegisterList);
const FFTImageFilterInitFactoryRegisterManager FFTImageFilterInitFactoryRegisterManagerInstance(FFTImageFilterInitFactoryRegisterRegisterList);

}

Expand Down
4 changes: 2 additions & 2 deletions Modules/Filtering/FFT/itk-module.cmake
Expand Up @@ -5,10 +5,10 @@ computations of Fast Fourier Transforms based on
<a href=\"http://www.fftw.org\">FFTW</a>. Note that when using the FFTW
implementation you must comply with the GPL license.")

set(_fft_backends "FFTImageFilter::Vnl")
set(_fft_backends "FFTImageFilterInit::Vnl")
if(ITK_USE_FFTWF OR ITK_USE_FFTWD)
# Prepend so that FFTW constructor is preferred
list(PREPEND _fft_backends "FFTImageFilter::FFTW")
list(PREPEND _fft_backends "FFTImageFilterInit::FFTW")
endif()

itk_module(ITKFFT
Expand Down
19 changes: 13 additions & 6 deletions Wrapping/Generators/Python/itk/support/base.py
Expand Up @@ -80,11 +80,6 @@ def itk_load_swig_module(name: str, namespace=None):
namespace[k] = v
return

# We're definitely going to load the templates. We set templates_loaded
# here instead of at the end of the file to protect against cyclical
# dependencies that could kill the recursive lookup below.
this_module.__templates_loaded = True

# Now, we definitely need to load the template_feature instantiations from the
# named module, and possibly also load the underlying SWIG module. Before
# we can load the template_feature instantiations of this module, we need to load
Expand All @@ -101,6 +96,11 @@ def itk_load_swig_module(name: str, namespace=None):
for dep in deps:
itk_load_swig_module(dep, namespace)

# TODO check if module was loaded as part of underlying
# dependency load, such as the case where this module
# defined a factory overload for a dependency module
# and was manually loaded early.

if itkConfig.ImportCallback:
itkConfig.ImportCallback(name, 0)

Expand Down Expand Up @@ -206,6 +206,11 @@ def itk_load_swig_module(name: str, namespace=None):
f"exception:\n {e}"
)

# Indicate that templates have been fully loaded from the module.
# Any subsequent attempts to load the module will bail out early
# if this flag is set.
this_module.__templates_loaded = True

for snakeCaseFunction in l_data.get_snake_case_functions():
namespace[snakeCaseFunction] = getattr(l_module, snakeCaseFunction)
init_name = snakeCaseFunction + "_init_docstring"
Expand Down Expand Up @@ -321,12 +326,14 @@ def load_factories(factory_name: str) -> None:
# Only load factories once
if factory_name in _factories_loaded:
return
import importlib

import itk
for module_name, data in itk_base_global_module_data.items():
for name, factory_class_prefix in data.get_module_factories()[:2]:
if name == factory_name:
# Get the factory, loading new modules with itk_load_swig_module as necessary
factory = getattr(itk, f"{factory_class_prefix}{name}Factory")
# Static method initializes factory overrides without adding to auto_pipeline
factory.RegisterOneFactory()
_factories_loaded.append(factory_name)

Expand Down
8 changes: 0 additions & 8 deletions Wrapping/Generators/Python/itk/support/extras.py
Expand Up @@ -1040,8 +1040,6 @@ def meshwrite(
The writer is instantiated according to the type of the input mesh.
"""
import itk
from itk.support.base import load_factories
load_factories('MeshIO')

mesh.UpdateOutputInformation()
# don't put that writer in the automatic pipeline
Expand Down Expand Up @@ -1072,8 +1070,6 @@ def meshread(
wrapped).
"""
import itk
from itk.support.base import load_factories
load_factories('MeshIO')

if fallback_only:
if pixel_type is None:
Expand Down Expand Up @@ -1123,8 +1119,6 @@ def transformread(filename: fileiotype) -> List["itkt.TransformBase"]:
A Python list containing the transforms in the file.
"""
import itk
from itk.support.base import load_factories
load_factories('TransformIO')

reader = itk.TransformFileReaderTemplate[itk.D].New()
reader.SetFileName(f"{filename}")
Expand Down Expand Up @@ -1160,8 +1154,6 @@ def transformwrite(
Use compression, if the file format supports it.
"""
import itk
from itk.support.base import load_factories
load_factories('TransformIO')

writer = itk.TransformFileWriterTemplate[itk.D].New()
writer.SetFileName(f"{filename}")
Expand Down

0 comments on commit e5bd53e

Please sign in to comment.