<commit_before>/*=========================================================================
 *
 *  Copyright Leiden University Medical Center, Erasmus University Medical 
 *  Center and contributors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/

#include "selxElastixComponent.h"

namespace selx
{
  template<int Dimensionality, class TPixel>
  ElastixComponent< Dimensionality, TPixel>::ElastixComponent()
  {
    m_elastixFilter = ElastixFilterType::New();
    m_transformixFilter = TransformixFilterType::New();

    // TODO: Due to some issues with Criteria being propagated as elastix settings, I need to empty the selxparameterObject.
    elxParameterObjectPointer elxParameterObject = elxParameterObjectType::New();
    typename elxParameterObjectType::ParameterMapType defaultParameters = elxParameterObject->GetDefaultParameterMap("rigid");

    elxParameterObject->SetParameterMap(defaultParameters);
    m_elastixFilter->SetParameterObject(elxParameterObject);

    m_elastixFilter->LogToConsoleOn();
    
    m_transformixFilter->ComputeDeformationFieldOn();
    m_transformixFilter->LogToConsoleOn();
    m_transformixFilter->LogToFileOn();
    // In the current transformix filter an input image is required even if we want a deformation field only.
    m_transformixFilter->SetInput(m_elastixFilter->GetOutput());
    
    //TODO m_elastixFilter returns a nullptr GetTransformParameterObject instead of a valid object. However, we need this object to satisfy the input conditions of m_transformixFilter
    //m_transformixFilter->SetTransformParameterObject(m_elastixFilter->GetTransformParameterObject());
    m_transformixFilter->SetTransformParameterObject(elxParameterObject); // supply a dummy object

    //TODO: instantiating the filter in the constructor might be heavy for the use in component selector factory, since all components of the database are created during the selection process.
    // we could choose to keep the component light weighted (for checking criteria such as names and connections) until the settings are passed to the filter, but this requires an additional initialization step.
  }

  template<int Dimensionality, class TPixel>
  ElastixComponent< Dimensionality, TPixel>::~ElastixComponent()
  {
  }

  template<int Dimensionality, class TPixel>
  int ElastixComponent< Dimensionality, TPixel>::Set(itkImageFixedInterface<Dimensionality, TPixel>* component)
  {
    auto fixedImage = component->GetItkImageFixed();
    // connect the itk pipeline
    this->m_elastixFilter->SetFixedImage(fixedImage);
    return 0;
  }

  template<int Dimensionality, class TPixel>
  int ElastixComponent< Dimensionality, TPixel>::Set(itkImageMovingInterface<Dimensionality, TPixel>* component)
  {<commit_msg>BUG: elastix/transformix require output directory to be set explicitly<commit_after>/*=========================================================================
 *
 *  Copyright Leiden University Medical Center, Erasmus University Medical 
 *  Center and contributors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/

#include "selxElastixComponent.h"

namespace selx
{
  template<int Dimensionality, class TPixel>
  ElastixComponent< Dimensionality, TPixel>::ElastixComponent()
  {
    m_elastixFilter = ElastixFilterType::New();
    m_transformixFilter = TransformixFilterType::New();

    // TODO: Due to some issues with Criteria being propagated as elastix settings, I need to empty the selxparameterObject.
    elxParameterObjectPointer elxParameterObject = elxParameterObjectType::New();
    typename elxParameterObjectType::ParameterMapType defaultParameters = elxParameterObject->GetDefaultParameterMap("rigid");

    elxParameterObject->SetParameterMap(defaultParameters);
    m_elastixFilter->SetParameterObject(elxParameterObject);
    m_elastixFilter->LogToConsoleOn();
    m_elastixFilter->LogToFileOff();
    m_elastixFilter->SetOutputDirectory(".");

    m_transformixFilter->ComputeDeformationFieldOn();
    m_transformixFilter->LogToConsoleOn();
    m_transformixFilter->LogToFileOff();
    m_transformixFilter->SetOutputDirectory(".");

    // In the current transformix filter an input image is required even if we want a deformation field only.
    m_transformixFilter->SetInput(m_elastixFilter->GetOutput());
    
    //TODO m_elastixFilter returns a nullptr GetTransformParameterObject instead of a valid object. However, we need this object to satisfy the input conditions of m_transformixFilter
    //m_transformixFilter->SetTransformParameterObject(m_elastixFilter->GetTransformParameterObject());
    m_transformixFilter->SetTransformParameterObject(elxParameterObject); // supply a dummy object

    //TODO: instantiating the filter in the constructor might be heavy for the use in component selector factory, since all components of the database are created during the selection process.
    // we could choose to keep the component light weighted (for checking criteria such as names and connections) until the settings are passed to the filter, but this requires an additional initialization step.
  }

  template<int Dimensionality, class TPixel>
  ElastixComponent< Dimensionality, TPixel>::~ElastixComponent()
  {
  }

  template<int Dimensionality, class TPixel>
  int ElastixComponent< Dimensionality, TPixel>::Set(itkImageFixedInterface<Dimensionality, TPixel>* component)
  {
    auto fixedImage = component->GetItkImageFixed();
    // connect the itk pipeline
    this->m_elastixFilter->SetFixedImage(fixedImage);
    return 0;
  }

  template<int Dimensionality, class TPixel>
  int ElastixComponent< Dimensionality, TPixel>::Set(itkImageMovingInterface<Dimensionality, TPixel>* component)
  {
{'fraction_non_alphanumeric': 0.11398286626978366, 'fraction_numerical': 0.001452011035283868, 'mean_word_length': 5.227848101265823, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5107845', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1726, 'n_words': 575}