Skip to content

Commit

Permalink
Merge pull request #101 from xoltar/betti-save
Browse files Browse the repository at this point in the history
Allow optional output file argument with --betti
  • Loading branch information
mlwright84 committed Jul 28, 2017
2 parents 109dc51 + 338416b commit 6ce3c9a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 29 deletions.
71 changes: 48 additions & 23 deletions console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ static const char USAGE[] =
rivet_console --version
rivet_console <input_file> --identify
rivet_console <input_file> --betti [-H <dimension>] [-V <verbosity>] [-x <xbins>] [-y <ybins>]
rivet_console <input_file> <output_file> --betti [-H <dimension>] [-V <verbosity>] [-x <xbins>] [-y <ybins>]
rivet_console <precomputed_file> --bounds
rivet_console <precomputed_file> --barcodes <line_file>
rivet_console <input_file> <output_file> [-H <dimension>] [-V <verbosity>] [-x <xbins>] [-y <ybins>] [-f <format>] [--binary]
Expand Down Expand Up @@ -362,7 +363,7 @@ int main(int argc, char* argv[])
std::clog << "Wrote arrangement to " << params.outputFile << std::endl;
}
});
computation.template_points_ready.connect([&points_message, &binary, &betti_only, &verbosity](TemplatePointsMessage message) {
computation.template_points_ready.connect([&points_message, &binary, &betti_only, &verbosity, &params](TemplatePointsMessage message) {
points_message.reset(new TemplatePointsMessage(message));

if (binary) {
Expand Down Expand Up @@ -397,8 +398,27 @@ int main(int argc, char* argv[])
print_dims(message, std::cout);
std::cout << std::endl;
print_betti(message, std::cout);
std::cout.flush();

//if an output file has been specified, then save the Betti numbers in an arrangement file (with no barcode templates)
if (!params.outputFile.empty()) {
std::ofstream file(params.outputFile);
if (file.is_open()) {
std::vector<exact> emptyvec;
std::shared_ptr<Arrangement> temp_arrangement = std::make_shared<Arrangement>(emptyvec, emptyvec, verbosity);
std::shared_ptr<ArrangementMessage> temp_am = std::make_shared<ArrangementMessage>(*temp_arrangement);
if (verbosity > 0) {
debug() << "Writing file:" << params.outputFile;
}
write_boost_file(params, *points_message, *temp_am);
} else {
std::stringstream ss;
ss << "Error: Unable to write file:" << params.outputFile;
throw std::runtime_error(ss.str());
}
}

//TODO: this seems a little abrupt...
std::cout.flush();
exit(0);
}
});
Expand All @@ -413,19 +433,21 @@ int main(int argc, char* argv[])
}
std::unique_ptr<ComputationResult> result;

std::unique_ptr<InputData> input;

std::shared_ptr<Arrangement> arrangement;

if (barcodes || bounds) {
result = load_from_precomputed(params.fileName);
if (barcodes) {
if (!slices.empty()) {
process_barcode_queries(slices, *result);
return 0;
}
} else {
process_bounds(*result);
}
} else {

std::unique_ptr<InputData> input;
try {
input = inputManager.start(progress);
} catch (const std::exception& e) {
Expand All @@ -441,31 +463,34 @@ int main(int argc, char* argv[])
if (params.verbosity >= 2) {
debug() << "Computation complete; augmented arrangement ready.";
}
auto arrangement = result->arrangement;
arrangement = result->arrangement;
if (params.verbosity >= 4) {
arrangement->print_stats();
}

//if an output file has been specified, then save the arrangement
if (!params.outputFile.empty()) {
std::ofstream file(params.outputFile);
if (file.is_open()) {
if (verbosity > 0) {
debug() << "Writing file:" << params.outputFile;
}
if (params.outputFormat == "R0") {
FileWriter fw(params, *input, *(arrangement), result->template_points);
fw.write_augmented_arrangement(file);
} else if (params.outputFormat == "R1") {
write_boost_file(params, *points_message, *arrangement_message);
} else {
throw std::runtime_error("Unsupported output format: " + params.outputFormat);
}
}
//if an output file has been specified, then save the arrangement
if (!params.outputFile.empty()) {
std::ofstream file(params.outputFile);
if (file.is_open()) {
if (arrangement == nullptr) {
arrangement.reset(new Arrangement());
}
if (verbosity > 0) {
debug() << "Writing file:" << params.outputFile;
}
if (params.outputFormat == "R0") {
FileWriter fw(params, *input, *(arrangement), result->template_points);
fw.write_augmented_arrangement(file);
} else if (params.outputFormat == "R1") {
write_boost_file(params, *points_message, *arrangement_message);
} else {
std::stringstream ss;
ss << "Error: Unable to write file:" << params.outputFile;
throw std::runtime_error(ss.str());
throw std::runtime_error("Unsupported output format: " + params.outputFormat);
}
} else {
std::stringstream ss;
ss << "Error: Unable to write file:" << params.outputFile;
throw std::runtime_error(ss.str());
}
}
if (params.verbosity > 2) {
Expand Down
5 changes: 5 additions & 0 deletions dcel/arrangement_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,8 @@ Arrangement ArrangementMessage::to_arrangement() const
// std::clog << "All anchors identical in to_arrangement" << std::endl;
return arrangement;
}

bool ArrangementMessage::is_empty() const
{
return ( x_exact.empty() && y_exact.empty() );
}
2 changes: 2 additions & 0 deletions dcel/arrangement_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class ArrangementMessage {

Arrangement to_arrangement() const;

bool is_empty() const;

private:
friend class boost::serialization::access;

Expand Down
7 changes: 5 additions & 2 deletions interface/progressdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,18 @@ void ProgressDialog::updateProgress(unsigned current)
void ProgressDialog::setComputationFinished()
{
computation_finished = true;
close();
QThread::msleep(200); //seems to prevent ProgressDialog from sticking around after it is supposed to have been closed
done(0);
}

void ProgressDialog::closeEvent(QCloseEvent* event)
{
if (!computation_finished)
event->ignore();
else
else {
event->accept();
QWidget::closeEvent(event);
}
}

QLabel* ProgressDialog::getLabel(unsigned i)
Expand Down
8 changes: 4 additions & 4 deletions visualizationwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ void VisualizationWindow::augmented_arrangement_ready(std::shared_ptr<Arrangemen
//receive the arrangement
this->arrangement = arrangement;

if(arrangement->is_empty()) { //e.g. the arrangement contains only Betti numbers and no barcode templates
return;
}

//TESTING: print arrangement info and verify consistency
// arrangement->print_stats();
// arrangement->test_consistency();
Expand Down Expand Up @@ -286,12 +290,8 @@ void VisualizationWindow::update_persistence_diagram()
BarcodeTemplate dbc = arrangement->get_barcode_template(angle_precise, offset_precise);
barcode = dbc.rescale(angle_precise, offset_precise, template_points->template_points, grades);

qDebug() << "Unshifted barcode:";
barcode->print();

//shift the barcode so that "zero" is where the selected line crosses the bottom or left side of the viewing window
double ll_corner = rivet::numeric::project_to_line(angle_precise, offset_precise, grades.x[0], grades.y[0]); //lower-left corner of line selection window
qDebug() << "ll_corner: " << ll_corner;
barcode = barcode->shift(-1*ll_corner);

//TESTING
Expand Down

0 comments on commit 6ce3c9a

Please sign in to comment.