<commit_before>#include "image_loader.h"

#include "itkImageFileReader.h"
#include "itkImageSeriesReader.h"

#include <ostream>

#include <boost/filesystem.hpp>
#include <boost/regex.hpp>

#include "log4cxx/logger.h"

typedef itk::ImageFileReader< InputImageType > ImageReader;
typedef itk::ImageSeriesReader< InputImageType > ImageSeriesReader;

InputImageType::Pointer ImageLoader::load(const std::string filename)
{
	log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("main"));

	LOG4CXX_INFO(logger, "Loading image \"" << filename << "\"");

	try
	{
		boost::filesystem::path path(filename);

		if(boost::filesystem::exists(path)) {
			InputImageType::Pointer img;

			if(boost::filesystem::is_directory(path))
			{
				LOG4CXX_DEBUG(logger, path << " is a folder");

				img = loadImageSerie(filename);
			} else {
				LOG4CXX_DEBUG(logger, path << " is a file");

				img = loadImage(filename);
			}

			LOG4CXX_INFO(logger, "Image " << path << " loaded");

			return img;
		} else {
			std::stringstream err;
			err << "\"" << filename << "\" does not exists";

			LOG4CXX_FATAL(logger, err.str());

			throw ImageLoadingException(err.str());
		}
	} catch(boost::filesystem::filesystem_error &ex) {
		std::stringstream err;
		err << filename << " cannot be read (" << ex.what() << ")" << std::endl;
		throw ImageLoadingException(err.str());
	}
}

InputImageType::Pointer ImageLoader::loadImage(const std::string filename)
{
	typename ImageReader::Pointer reader = ImageReader::New();

	reader->SetFileName(filename);

	try {
		reader->Update();
	}
	catch( itk::ExceptionObject &ex )
	{
		std::stringstream err;
		err << "ITK is unable to load the image \"" << filename << "\" (" << ex.what() << ")";

		throw ImageLoadingException(err.str());
	}

	return reader->GetOutput();
}

InputImageType::Pointer ImageLoader::loadImageSerie(const std::string filename)
{
	typename ImageSeriesReader::Pointer reader = ImageSeriesReader::New();

	typename ImageSeriesReader::FileNamesContainer filenames;

	log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("main"));

	try
	{
		boost::filesystem::path path(filename);
		boost::filesystem::directory_iterator end_iter;
		boost::regex pattern(".*\\.((?:png)|(?:bmp)|(?:jpe?g))", boost::regex::icase);

		for( boost::filesystem::directory_iterator dir_iter(path) ; dir_iter != end_iter ; ++dir_iter)
		{
			boost::smatch match;
			if( !boost::regex_match( dir_iter->path().filename().string(), match, pattern ) ) continue;

			LOG4CXX_DEBUG(logger, "Loading \"" << boost::filesystem::absolute(dir_iter->path()).string() << "\"");

			filenames.push_back(boost::filesystem::absolute(dir_iter->path()).string());
		}
	}
	catch(boost::filesystem::filesystem_error &ex) {
		std::stringstream err;
		err << filename << " cannot be read (" << ex.what() << ")" << std::endl;

		throw ImageLoadingException(err.str());
	}

	std::sort(filenames.begin(), filenames.end());<commit_msg>Add sorting when loading a 3D file serie.<commit_after>#include "image_loader.h"

#include "itkImageFileReader.h"
#include "itkImageSeriesReader.h"

#include <ostream>
#include <algorithm>

#include <boost/filesystem.hpp>
#include <boost/regex.hpp>

#include "log4cxx/logger.h"

typedef itk::ImageFileReader< InputImageType > ImageReader;
typedef itk::ImageSeriesReader< InputImageType > ImageSeriesReader;

InputImageType::Pointer ImageLoader::load(const std::string filename)
{
	log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("main"));

	LOG4CXX_INFO(logger, "Loading image \"" << filename << "\"");

	try
	{
		boost::filesystem::path path(filename);

		if(boost::filesystem::exists(path)) {
			InputImageType::Pointer img;

			if(boost::filesystem::is_directory(path))
			{
				LOG4CXX_DEBUG(logger, path << " is a folder");

				img = loadImageSerie(filename);
			} else {
				LOG4CXX_DEBUG(logger, path << " is a file");

				img = loadImage(filename);
			}

			LOG4CXX_INFO(logger, "Image " << path << " loaded");

			return img;
		} else {
			std::stringstream err;
			err << "\"" << filename << "\" does not exists";

			LOG4CXX_FATAL(logger, err.str());

			throw ImageLoadingException(err.str());
		}
	} catch(boost::filesystem::filesystem_error &ex) {
		std::stringstream err;
		err << filename << " cannot be read (" << ex.what() << ")" << std::endl;
		throw ImageLoadingException(err.str());
	}
}

InputImageType::Pointer ImageLoader::loadImage(const std::string filename)
{
	typename ImageReader::Pointer reader = ImageReader::New();

	reader->SetFileName(filename);

	try {
		reader->Update();
	}
	catch( itk::ExceptionObject &ex )
	{
		std::stringstream err;
		err << "ITK is unable to load the image \"" << filename << "\" (" << ex.what() << ")";

		throw ImageLoadingException(err.str());
	}

	return reader->GetOutput();
}

InputImageType::Pointer ImageLoader::loadImageSerie(const std::string filename)
{
	typename ImageSeriesReader::Pointer reader = ImageSeriesReader::New();

	typename ImageSeriesReader::FileNamesContainer filenames;

	log4cxx::LoggerPtr logger(log4cxx::Logger::getLogger("main"));

	try
	{
		boost::filesystem::path path(filename);
		boost::regex pattern(".*\\.((?:png)|(?:bmp)|(?:jpe?g))", boost::regex::icase);

		typedef std::vector< boost::filesystem::path > path_list;
		path_list slices;
		std::copy(boost::filesystem::directory_iterator(path), boost::filesystem::directory_iterator(), std::back_inserter(slices));
		std::sort(slices.begin(), slices.end());

		for( path_list::const_iterator it(slices.begin()) ; it != slices.end() ; ++it)
		{
			boost::smatch match;
			if( !boost::regex_match( (*it).filename().string(), match, pattern ) ) continue;

			LOG4CXX_DEBUG(logger, "Loading \"" << boost::filesystem::absolute(*it).string() << "\"");

			filenames.push_back(boost::filesystem::absolute(*it).string());
		}
	}
	catch(boost::filesystem::filesystem_error &ex) {
		std::stringstream err;
		err << filename << " cannot be read (" << ex.what() << ")" << std::endl;

		throw ImageLoadingException(err.str());
	}

	std::sort(filenames.begin(), filenames.end());
{'fraction_non_alphanumeric': 0.17933432687531048, 'fraction_numerical': 0.0038085775790693823, 'mean_word_length': 5.51564185544768, 'pattern_counts': {'":': 0, '<': 119, '<?xml version=': 0, '>': 24, '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': '5583725', 'n_tokens_mistral': 2150, 'n_tokens_neox': 1951, 'n_words': 359}