<commit_before>#ifndef STACKIOHELPERS_HHP_
#define STACKIOHELPERS_HHP_

#include <assert.h>
#include "boost/filesystem.hpp"

#include "itkTransformFileReader.h"
#include "itkTransformFileWriter.h"
#include "itkTranslationTransform.h"
#include "itkTransformFactory.h"

#include "Stack.hpp"
#include "StackTransforms.hpp"
#include "Dirs.hpp"

using namespace boost::filesystem;

// helper to construct transform file path
const string TransformFilePath(const string& imageFileName, const string& transformDirName) {
  path slicePath( imageFileName );
  string transformFileName( basename( slicePath.leaf() ) + ".meta" );
  path transformFilePath = path(transformDirName) / path(transformFileName);
  return transformFilePath.string();
}

// Stack Persistence
template <typename StackType>
void Save(StackType& stack, vector< string > fileNames, const string& dirName)
{
  typedef itk::TransformFileWriter WriterType;

  for(int slice_number=0; slice_number < stack.GetSize(); ++slice_number)
	{
    WriterType::Pointer writer = WriterType::New();
    writer->SetFileName( TransformFilePath(fileNames[slice_number], dirName).c_str() );
    writer->AddTransform( stack.GetTransform(slice_number) );
    
    try
    {
    	writer->Update();
    }
  	catch( itk::ExceptionObject & err )
  	{
      cerr << "ExceptionObject caught while saving transforms." << endl;
      cerr << err << endl;
  		std::abort();
  	}
  
  }
  
}


template <typename StackType>
void Load(StackType& stack, vector< string > fileNames, const string& dirName)
{
  typedef itk::TransformFileReader ReaderType;
  
  // Some transforms might not be registered
  // with the factory so we add them manually
  itk::TransformFactoryBase::RegisterDefaultTransforms();
  // TODO: below registers a new version of transform every time Load() is called
  itk::TransformFactory< itk::TranslationTransform< double, 2 > >::RegisterTransform();
  
  typename StackType::TransformVectorType newTransforms;
  
  for(unsigned int slice_number=0; slice_number<stack.GetSize(); ++slice_number)
  {
    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName( TransformFilePath(fileNames[slice_number], dirName).c_str() );
    
    try
    {
      reader->Update();
    }
    catch( itk::ExceptionObject & err )
    {
      std::cerr << "ExceptionObject caught while reading transforms." << std::endl;
      cerr << err << endl;
  		std::abort();
    }
    
    typename StackType::TransformType::Pointer transform = static_cast<typename StackType::TransformType*>( reader->GetTransformList()->begin()->GetPointer() );
    newTransforms.push_back( transform );
  }
  
  stack.SetTransforms(newTransforms);
}

template <typename StackType>
void ApplyAdjustments(StackType& stack, vector< string > fileNames, const string& dirName)
{
  typedef itk::TransformFileReader ReaderType;
  
  // Some transforms might not be registered
  // with the factory so we add them manually
  itk::TransformFactoryBase::RegisterDefaultTransforms();
  itk::TransformFactory< itk::TranslationTransform< double, 2 > >::RegisterTransform();
  
  typename StackType::TransformVectorType newTransforms;
  
  for(unsigned int slice_number=0; slice_number<stack.GetSize(); ++slice_number)
  {
    // construct path to config transform file
    // config/Rat28/LoRes_adustments/0053.meta
    string transformFilePath = TransformFilePath(fileNames[slice_number], dirName);
    
    if( exists(transformFilePath) )
    {
      ReaderType::Pointer reader = ReaderType::New();
      reader->SetFileName( transformFilePath.c_str() );
      
      try
      {
        reader->Update();
      }
      catch( itk::ExceptionObject & err )
      {
        std::cerr << "ExceptionObject caught while reading transforms." << std::endl;
        cerr << err << endl;
           std::abort();
      }
      
      // convert Array to Vector
      itk::Array< double > parameters( reader->GetTransformList()->begin()->GetPointer()->GetParameters() );
      itk::Vector< double, 2 > translation;
      translation[0] = parameters[0];
      translation[1] = parameters[1];
      
      // translate block image
      StackTransforms::Translate<StackType>(stack, translation, slice_number );
    }
  }
}

template <typename DataType>
void saveVectorToFiles(const vector< DataType >& values, const string& dirName, const vector< string >& fileNames)
{
  assert(values.size() == fileNames.size());
  
  path pathName = Dirs::ResultsDir() + dirName;
  create_directory(pathName);
  
  for(unsigned int i=0; i<values.size(); ++i)
  {
    path outPath = pathName / fileNames[i];
    ofstream outFile(outPath.string().c_str());
    outFile << values[i] << endl;
  }
}

template <typename DataType>
vector< DataType > loadVectorFromFiles(const string& dirName,  const vector< string >& fileNames)
{
  path pathName = Dirs::ResultsDir() + dirName;
  typename vector< DataType >::size_type numberOfFiles = fileNames.size();
  vector< DataType > values(numberOfFiles);
  
  for(unsigned int i=0; i<fileNames.size(); ++i)
  {
    path inPath = pathName / fileNames[i];
    ifstream inFile(inPath.string().c_str());
    assert(inFile.is_open());
    inFile >> values[i];
  }
  
  return values;
}


#endif<commit_msg>Vector IO removes filename extension.<commit_after>#ifndef STACKIOHELPERS_HHP_
#define STACKIOHELPERS_HHP_

#include <assert.h>
#include "boost/filesystem.hpp"

#include "itkTransformFileReader.h"
#include "itkTransformFileWriter.h"
#include "itkTranslationTransform.h"
#include "itkTransformFactory.h"

#include "Stack.hpp"
#include "StackTransforms.hpp"
#include "Dirs.hpp"

using namespace boost::filesystem;

// helper to construct transform file path
const string TransformFilePath(const string& imageFileName, const string& transformDirName) {
  path slicePath( imageFileName );
  string transformFileName( basename( slicePath.leaf() ) + ".meta" );
  path transformFilePath = path(transformDirName) / path(transformFileName);
  return transformFilePath.string();
}

// Stack Persistence
template <typename StackType>
void Save(StackType& stack, vector< string > fileNames, const string& dirName)
{
  typedef itk::TransformFileWriter WriterType;

  for(int slice_number=0; slice_number < stack.GetSize(); ++slice_number)
	{
    WriterType::Pointer writer = WriterType::New();
    writer->SetFileName( TransformFilePath(fileNames[slice_number], dirName).c_str() );
    writer->AddTransform( stack.GetTransform(slice_number) );
    
    try
    {
    	writer->Update();
    }
  	catch( itk::ExceptionObject & err )
  	{
      cerr << "ExceptionObject caught while saving transforms." << endl;
      cerr << err << endl;
  		std::abort();
  	}
  
  }
  
}


template <typename StackType>
void Load(StackType& stack, vector< string > fileNames, const string& dirName)
{
  typedef itk::TransformFileReader ReaderType;
  
  // Some transforms might not be registered
  // with the factory so we add them manually
  itk::TransformFactoryBase::RegisterDefaultTransforms();
  // TODO: below registers a new version of transform every time Load() is called
  itk::TransformFactory< itk::TranslationTransform< double, 2 > >::RegisterTransform();
  
  typename StackType::TransformVectorType newTransforms;
  
  for(unsigned int slice_number=0; slice_number<stack.GetSize(); ++slice_number)
  {
    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName( TransformFilePath(fileNames[slice_number], dirName).c_str() );
    
    try
    {
      reader->Update();
    }
    catch( itk::ExceptionObject & err )
    {
      std::cerr << "ExceptionObject caught while reading transforms." << std::endl;
      cerr << err << endl;
  		std::abort();
    }
    
    typename StackType::TransformType::Pointer transform = static_cast<typename StackType::TransformType*>( reader->GetTransformList()->begin()->GetPointer() );
    newTransforms.push_back( transform );
  }
  
  stack.SetTransforms(newTransforms);
}

template <typename StackType>
void ApplyAdjustments(StackType& stack, vector< string > fileNames, const string& dirName)
{
  typedef itk::TransformFileReader ReaderType;
  
  // Some transforms might not be registered
  // with the factory so we add them manually
  itk::TransformFactoryBase::RegisterDefaultTransforms();
  itk::TransformFactory< itk::TranslationTransform< double, 2 > >::RegisterTransform();
  
  typename StackType::TransformVectorType newTransforms;
  
  for(unsigned int slice_number=0; slice_number<stack.GetSize(); ++slice_number)
  {
    // construct path to config transform file
    // config/Rat28/LoRes_adustments/0053.meta
    string transformFilePath = TransformFilePath(fileNames[slice_number], dirName);
    
    if( exists(transformFilePath) )
    {
      ReaderType::Pointer reader = ReaderType::New();
      reader->SetFileName( transformFilePath.c_str() );
      
      try
      {
        reader->Update();
      }
      catch( itk::ExceptionObject & err )
      {
        std::cerr << "ExceptionObject caught while reading transforms." << std::endl;
        cerr << err << endl;
           std::abort();
      }
      
      // convert Array to Vector
      itk::Array< double > parameters( reader->GetTransformList()->begin()->GetPointer()->GetParameters() );
      itk::Vector< double, 2 > translation;
      translation[0] = parameters[0];
      translation[1] = parameters[1];
      
      // translate block image
      StackTransforms::Translate<StackType>(stack, translation, slice_number );
    }
  }
}

template <typename DataType>
void saveVectorToFiles(const vector< DataType >& values, const string& dirName, const vector< string >& fileNames)
{
  assert(values.size() == fileNames.size());
  
  path pathName = Dirs::ResultsDir() + dirName;
  create_directory(pathName);
  
  for(unsigned int i=0; i<values.size(); ++i)
  {
    path outPath = pathName / basename(fileNames[i]);
    ofstream outFile(outPath.string().c_str());
    outFile << values[i] << endl;
  }
}

template <typename DataType>
vector< DataType > loadVectorFromFiles(const string& dirName,  const vector< string >& fileNames)
{
  path pathName = Dirs::ResultsDir() + dirName;
  typename vector< DataType >::size_type numberOfFiles = fileNames.size();
  vector< DataType > values(numberOfFiles);
  
  for(unsigned int i=0; i<fileNames.size(); ++i)
  {
    path inPath = pathName / basename(fileNames[i]);
    ifstream inFile(inPath.string().c_str());
    assert(inFile.is_open());
    inFile >> values[i];
  }
  
  return values;
}


#endif
{'fraction_non_alphanumeric': 0.12409523809523809, 'fraction_numerical': 0.0034285714285714284, 'mean_word_length': 4.122439024390244, 'pattern_counts': {'":': 0, '<': 115, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5429173', 'n_tokens_mistral': 3132, 'n_tokens_neox': 3068, 'n_words': 729}