Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve output error handling #39

Merged
merged 1 commit into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 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 BadConfigurationError(err.str());
throw InvalidWriterError(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 BadConfigurationError(err.str());
throw InvalidWriterError(err.str());
}

BaseWriter_unique BaseWriter::get_writer_for_filename(const std::string &filename, MPI_Comm comm)
Expand All @@ -83,7 +83,7 @@ BaseWriter_unique BaseWriter::get_writer_for_filename(const std::string &filenam
std::ostringstream err;
err << "No suitable writer available for file \"" << filename << "\"."
<< "\n(" << errstr.what() << ")\n";
throw BadConfigurationError(err.str());
throw InvalidWriterError(err.str());
}
}

Expand Down
65 changes: 49 additions & 16 deletions src/pfire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void mainflow(std::shared_ptr<ConfigurationBase> config)
std::unique_ptr<Image> fixed;
try
{
fixed = Image::load_file(config->grab<std::string>("flixed"));
fixed = Image::load_file(config->grab<std::string>("fixed"));
}
catch (const pFIREExpectedError &e)
{
Expand Down Expand Up @@ -121,6 +121,23 @@ void mainflow(std::shared_ptr<ConfigurationBase> config)

floatvector nodespacing(fixed->ndim(), config->grab<integer>("nodespacing"));

// Check file writer is valid
std::vector<std::string> configs_to_check = {"registered", "map"};
for(const auto &handle : configs_to_check)
{
std::string filename = config->grab<std::string>(handle);
try
{
BaseWriter::find_writer_for_filename(filename);
}
catch (const InvalidWriterError &e)
{
std::cerr << "Error: No suitable writer for requested output filename \""
<< filename << "\"" << std::endl;
return;
}
}

// explain_memory(fixed->shape(), Map::calculate_map_shape(fixed->shape(), nodespacing));

fixed->normalize();
Expand All @@ -144,19 +161,35 @@ void mainflow(std::shared_ptr<ConfigurationBase> config)
}

Elastic reg(*fixed, *moved, *mask, nodespacing, *config);
reg.autoregister();

std::string outfile = config->grab<std::string>("registered");
std::string h5group = config->grab<std::string>("registered_h5_path");
std::string output_path = outfile + ":" + h5group;
BaseWriter_unique wtr = BaseWriter::get_writer_for_filename(output_path, fixed->comm());
output_path = wtr->write_image(*reg.registered());
PetscPrintf(PETSC_COMM_WORLD, "Saving registered image to %s\n", output_path.c_str());

outfile = config->grab<std::string>("map");
h5group = config->grab<std::string>("map_h5_path");
output_path = outfile + ":" + h5group;
wtr = BaseWriter::get_writer_for_filename(output_path, fixed->comm());
output_path = wtr->write_map(*reg.m_p_map);
PetscPrintf(PETSC_COMM_WORLD, "Saving map to %s\n", output_path.c_str());
try
{
reg.autoregister();
}
catch (const pFIREExpectedError &e)
{
std::cerr << "Error: Registration failed: " << e.what() << std::endl;
return;
}

try
{
std::string outfile = config->grab<std::string>("registered");
std::string h5group = config->grab<std::string>("registered_h5_path");
std::string output_path = outfile + ":" + h5group;
BaseWriter_unique wtr = BaseWriter::get_writer_for_filename(output_path, fixed->comm());
output_path = wtr->write_image(*reg.registered());
PetscPrintf(PETSC_COMM_WORLD, "Saved registered image to %s\n", output_path.c_str());

outfile = config->grab<std::string>("map");
h5group = config->grab<std::string>("map_h5_path");
output_path = outfile + ":" + h5group;
wtr = BaseWriter::get_writer_for_filename(output_path, fixed->comm());
output_path = wtr->write_map(*reg.m_p_map);
PetscPrintf(PETSC_COMM_WORLD, "Saved map to %s\n", output_path.c_str());
}
catch (const pFIREExpectedError &e)
{
std::cerr << "Error: Failed to save results: " << e.what() << std::endl;
return;
}
}