Skip to content

Commit

Permalink
Merge pull request #38 from INSIGNEO/develop
Browse files Browse the repository at this point in the history
Exception and configuration improvements
  • Loading branch information
ptooley authored May 1, 2019
2 parents 9625917 + 74b5b52 commit e355ca2
Show file tree
Hide file tree
Showing 29 changed files with 562 additions and 332 deletions.
27 changes: 12 additions & 15 deletions src/baseconfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,38 @@ const std::string ConfigurationBase::k_extension_token = "%ext%";
const std::string ConfigurationBase::k_outer_token = "%s%";
const std::string ConfigurationBase::k_inner_token = "%i%";

const config_map ConfigurationBase::default_config = {{"verbose", "false"},
{"registered", "registered.xdmf"}, {"map", "map.xdmf"}, {"lambda", "auto"}, {"mask", ""},
{"registered_h5_path", "/registered"}, {"map_h5_path", "/map"}, {"lambda_mult", "1.0"},
{"with_memory", "true"}, {"save_intermediate_frames", "false"},
const config_map ConfigurationBase::arg_options = {{"fixed", ""}, {"moved", ""},
{"nodespacing", ""}, {"registered", "registered.xdmf"}, {"map", "map.xdmf"},
{"lambda", "auto"}, {"mask", ""}, {"registered_h5_path", "/registered"},
{"map_h5_path", "/map"}, {"lambda_mult", "1.0"},
{"intermediate_template", "%name%-intermediate-%s%-%i%%ext%"},
{"intermediate_map_template", "%name%-intermediate-map-%s%-%i%%ext%"},
{"intermediate_directory", "intermediates"}, {"max_iterations", "100"}};

const config_map ConfigurationBase::bool_options = {
{"verbose", "false"}, {"with_memory", "true"}, {"save_intermediate_frames", "false"}};

const std::vector<std::string> ConfigurationBase::required_options = {
"fixed", "moved", "nodespacing"};

const std::vector<std::string> ConfigurationBase::arg_options = {"fixed", "moved", "mask",
"nodespacing", "registered", "map", "lambda", "lambda_mult", "intermediate_template",
"intermediate_directory", "max_iterations", "registered_h5_path", "map_h5_path"};

const std::vector<std::string> ConfigurationBase::bool_options = {
"verbose", "with_memory", "save_intermediate_frames"};

ConfigurationBase::ConfigurationBase(const int &argc, char const *const *argv)
: config(default_config), arguments(argv + 1, argv + argc),
invocation_name(get_invocation_name(argv[0]))
: arguments(argv + 1, argv + argc), invocation_name(get_invocation_name(argv[0]))
{
config.insert(arg_options.cbegin(), arg_options.cend());
config.insert(bool_options.cbegin(), bool_options.cend());
}

void ConfigurationBase::validate_config()
{
std::list<std::string> missing;
for (auto &req_it : ConfigurationBase::required_options)
{
if (config.find(req_it) == config.cend())
if (config.find(req_it) == config.cend() || config[req_it].empty())
{
missing.push_back(req_it);
}
}
if (missing.size() > 0)
if (!missing.empty())
{
std::ostringstream errmsg;
errmsg << "Missing required argument(s) \"";
Expand Down
48 changes: 38 additions & 10 deletions src/baseconfiguration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
namespace bf = boost::filesystem;

#include "types.hpp"
#include "exceptions.hpp"

using config_map = std::map<std::string, std::string>;

Expand All @@ -39,29 +40,57 @@ class ConfigurationBase {
template <typename T>
typename std::enable_if<std::is_same<T, bool>::value, T>::type grab(const std::string key) const
{
T val;
std::istringstream(config.at(key)) >> std::boolalpha >> val;
return val;
try
{
T val;
std::istringstream(config.at(key)) >> std::boolalpha >> val;
return val;
}
catch (std::out_of_range &err)
{
throw InternalError("Attempt to access non-existent configuration option", __FILE__, __LINE__);
}
}

template <typename T>
typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool>::value, T>::type
grab(const std::string key) const
{
return static_cast<T>(std::stoll(config.at(key)));
try
{
return static_cast<T>(std::stoll(config.at(key)));
}
catch (std::out_of_range &err)
{
throw InternalError("Attempt to access non-existent configuration option", __FILE__, __LINE__);
}
}

template <typename T>
typename std::enable_if<std::is_floating_point<T>::value && !std::is_same<T, bool>::value, T>::type
grab(const std::string key) const
{
return static_cast<T>(std::stold(config.at(key)));
}
try
{
return static_cast<T>(std::stold(config.at(key)));
}
catch (std::out_of_range &err)
{
throw InternalError("Attempt to access non-existent configuration option", __FILE__, __LINE__);
}
}

template <typename T>
typename std::enable_if<std::is_same<T, std::string>::value, T>::type grab(std::string key) const
{
return config.at(key);
try
{
return config.at(key);
}
catch (std::out_of_range &err)
{
throw InternalError("Attempt to access non-existent configuration option", __FILE__, __LINE__);
}
}

static std::string get_invocation_name(const std::string& argzero);
Expand All @@ -80,10 +109,9 @@ class ConfigurationBase {
std::vector<std::string> arguments;
std::string invocation_name;

static const config_map default_config;
static const std::vector<std::string> required_options;
static const std::vector<std::string> arg_options;
static const std::vector<std::string> bool_options;
static const config_map arg_options;
static const config_map bool_options;
};

#endif // CONFIGURATION_HPP
4 changes: 2 additions & 2 deletions src/baseloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bool BaseLoader::register_loader(const std::string &name, loader_creator loader)
}
std::ostringstream err;
err << "Loader named " << name << " already registered.";
throw std::runtime_error(err.str());
throw InternalError(err.str(), __FILE__, __LINE__);
}

BaseLoader_unique BaseLoader::find_loader(const std::string &path, MPI_Comm comm)
Expand All @@ -53,5 +53,5 @@ BaseLoader_unique BaseLoader::find_loader(const std::string &path, MPI_Comm comm
}
std::ostringstream err;
err << "Failed to find suitable loader for \"" << path << "\".";
throw std::runtime_error(err.str());
throw InvalidLoaderError(err.str());
}
8 changes: 4 additions & 4 deletions src/basewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ std::string BaseWriter::find_writer_for_filename(const std::string &filename)
}
std::ostringstream err;
err << "No suitable writer available for extension \"" << extension << "\".";
throw std::runtime_error(err.str());
throw BadConfigurationError(err.str());
}

BaseWriter_unique BaseWriter::get_writer_by_name(
Expand All @@ -68,7 +68,7 @@ BaseWriter_unique BaseWriter::get_writer_by_name(
}
std::ostringstream err;
err << "No registered writer named \"" << name << "\".";
throw std::runtime_error(err.str());
throw BadConfigurationError(err.str());
}

BaseWriter_unique BaseWriter::get_writer_for_filename(const std::string &filename, MPI_Comm comm)
Expand All @@ -78,12 +78,12 @@ BaseWriter_unique BaseWriter::get_writer_for_filename(const std::string &filenam
return BaseWriter::get_writer_by_name(
BaseWriter::find_writer_for_filename(filename), filename, comm);
}
catch (const std::runtime_error &errstr)
catch (const pFIREExpectedError &errstr)
{
std::ostringstream err;
err << "No suitable writer available for file \"" << filename << "\"."
<< "\n(" << errstr.what() << ")\n";
throw std::runtime_error(err.str());
throw BadConfigurationError(err.str());
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/basewriter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <mpi.h>

#include "types.hpp"
#include "exceptions.hpp"

class BaseWriter {
public:
Expand Down Expand Up @@ -107,7 +108,7 @@ BaseWriter::register_writer()
}
std::ostringstream err;
err << "Writer named " << WriterClass::writer_name << " already registered.";
throw std::runtime_error(err.str());
throw InternalError(err.str(), __FILE__, __LINE__);
}

#endif // BASEWRITER_HPP
13 changes: 7 additions & 6 deletions src/basis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <petscmat.h>

#include "debug.hpp"
#include "exceptions.hpp"
#include "petsc_debug.hpp"
#include "indexing.hpp"
#include "iterator_routines.hpp"
Expand Down Expand Up @@ -125,7 +126,7 @@ Mat_unique build_basis_matrix(
PetscErrorCode perr = MatCreateMPIAIJWithArrays(
comm, idxn.size() - 1, PETSC_DECIDE, m_size, n_size, idxn.data(), idxm.data(), mdat.data(),
m_basis.get());
CHKERRABORT(comm, perr);
CHKERRXX(perr);

#ifdef DEBUG_VERBOSE
matrix_dbg_print(comm, *m_basis, "Basis Matrix");
Expand All @@ -142,12 +143,12 @@ Mat_unique build_warp_matrix(
integer mat_size = std::accumulate(img_shape.begin(), img_shape.end(), 1, std::multiplies<>());
if (img_shape.size() < ndim)
{
throw std::runtime_error("image dimensions must match ndim");
throw InternalError("image dimensions must match ndim", __FILE__, __LINE__);
}

if (displacements.size() < ndim)
{
throw std::runtime_error("must have displacement vector for each image dimension");
throw InternalError("must have displacement vector for each image dimension", __FILE__, __LINE__);
}

intvector img_shape_trunc(ndim, 0);
Expand All @@ -173,7 +174,7 @@ Mat_unique build_warp_matrix(
// lambda needed here anyway to capture comm
auto get_raw_array = [comm](floating*& a, const Vec* v) -> void {
PetscErrorCode p = VecGetArray(*v, &a);
CHKERRABORT(comm, p);
CHKERRXX(p);
};
n_ary_for_each(get_raw_array, raw_arrs.begin(), raw_arrs.end(), displacements.begin());

Expand Down Expand Up @@ -236,15 +237,15 @@ Mat_unique build_warp_matrix(
// lambda needed here anyway to capture comm
auto restore_raw_array = [comm](floating*& a, const Vec* v) -> void {
PetscErrorCode p = VecRestoreArray(*v, &a);
CHKERRABORT(comm, p);
CHKERRXX(p);
};
n_ary_for_each(restore_raw_array, raw_arrs.begin(), raw_arrs.end(), displacements.begin());

Mat_unique warp = create_unique_mat();
perr = MatCreateMPIAIJWithArrays(
comm, idxn.size() - 1, idxn.size() - 1, mat_size, mat_size, idxn.data(), idxm.data(),
mdat.data(), warp.get());
CHKERRABORT(comm, perr);
CHKERRXX(perr);
debug_creation(*warp, "Warp matrix");

return warp;
Expand Down
8 changes: 4 additions & 4 deletions src/dcmloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ DCMLoader::DCMLoader(const std::string &path, MPI_Comm comm)
status = dataset->findAndGetLongInt(DcmTagKey(TG_IMG, TE_ROWS), tmp);
if (status.bad())
{
throw std::runtime_error("Failed to read image shape data");
throw InvalidLoaderError("Failed to read image shape data");
}
this->_shape[0] = tmp;
status = dataset->findAndGetLongInt(DcmTagKey(TG_IMG, TE_COLS), tmp);
if (status.bad())
{
throw std::runtime_error("Failed to read image shape data");
throw InvalidLoaderError("Failed to read image shape data");
}
this->_shape[1] = tmp;
status = dataset->findAndGetLongInt(DcmTagKey(TG_IMG, TE_FRAMES), tmp);
if (status.bad())
{
throw std::runtime_error("Failed to read image shape data");
throw InvalidLoaderError("Failed to read image shape data");
}
this->_shape[2] = tmp;
}
Expand Down Expand Up @@ -123,7 +123,7 @@ void DCMLoader::copy_scaled_chunk(
}
default:
{
throw std::runtime_error("Unhandled image bit depth.");
throw InvalidLoaderError("Unhandled image bit depth.");
break;
}
}
Expand Down
Loading

0 comments on commit e355ca2

Please sign in to comment.