Skip to content

Commit

Permalink
very ugly but kind of functional database command -- still needs debu…
Browse files Browse the repository at this point in the history
…gging but most content is there
  • Loading branch information
karenhaining committed Nov 20, 2021
1 parent b31f1f8 commit 7e11205
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 67 deletions.
17 changes: 16 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"files.eol": "\n"
"files.eol": "\n",
"bitset": "cpp",
"chrono": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"set": "cpp",
"ratio": "cpp",
"regex": "cpp",
"future": "cpp",
"iomanip": "cpp",
"mutex": "cpp",
"shared_mutex": "cpp",
"thread": "cpp",
"variant": "cpp"
}
}
86 changes: 59 additions & 27 deletions src/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ std::string NextCliArg() {
return std::string("You incompetent fool!");
}

PromptedOutputStream::PromptedOutputStream() {
std::string filePath = Prompt<std::string>("Output file (or - for stdout)");
PromptedOutputStream::PromptedOutputStream(std::string filePath) {
//std::string filePath = Prompt<std::string>("Output file (or - for stdout)");
if (filePath == "-") {
stream = &std::cout;
isFstream = false;
Expand Down Expand Up @@ -241,7 +241,6 @@ void SurfacePlot(cairo_surface_t *cairoSurface,
}

// ALGORITHM PROMPTERS

typedef CentroidAlgorithm *(*CentroidAlgorithmFactory)();

CentroidAlgorithm *DummyCentroidAlgorithmPrompt() {
Expand Down Expand Up @@ -294,10 +293,25 @@ Catalog PromptNarrowedCatalog(const Catalog &catalog) {
return NarrowCatalog(catalog, maxMagnitude, maxStars);
}

void PromptKVectorDatabaseBuilder(MultiDatabaseBuilder &builder, const Catalog &catalog) {
float minDistance = DegToRad(Prompt<float>("Min distance (deg)"));
float maxDistance = DegToRad(Prompt<float>("Max distance (deg)"));
long numBins = Prompt<long>("Number of distance bins");
// float minDistance = DegToRad(Prompt<float>("Min distance (deg)"));
// float maxDistance = DegToRad(Prompt<float>("Max distance (deg)"));
// long numBins = Prompt<long>("Number of distance bins");

// // TODO: calculating the length of the vector duplicates a lot of the work, slowing down
// // database generation
// long length = SerializeLengthPairDistanceKVector(catalog, minDistance, maxDistance, numBins);
// unsigned char *buffer = builder.AddSubDatabase(PairDistanceKVectorDatabase::kMagicValue, length);
// if (buffer == NULL) {
// std::cerr << "No room for another database." << std::endl;
// }
// SerializePairDistanceKVector(catalog, minDistance, maxDistance, numBins, buffer);

// // TODO: also parse it and print out some stats before returning

void PromptKVectorDatabaseBuilder(MultiDatabaseBuilder &builder, const Catalog &catalog, float minDistance, float maxDistance, long numBins) {
// float minDistance = DegToRad(Prompt<float>("Min distance (deg)"));
// float maxDistance = DegToRad(Prompt<float>("Max distance (deg)"));
// long numBins = Prompt<long>("Number of distance bins");

// TODO: calculating the length of the vector duplicates a lot of the work, slowing down
// database generation
Expand All @@ -309,21 +323,39 @@ void PromptKVectorDatabaseBuilder(MultiDatabaseBuilder &builder, const Catalog &
SerializePairDistanceKVector(catalog, minDistance, maxDistance, numBins, buffer);

// TODO: also parse it and print out some stats before returning

}

void PromptDatabases(MultiDatabaseBuilder &builder, const Catalog &catalog) {
InteractiveChoice<DbBuilder> dbBuilderChoice;
dbBuilderChoice.Register("kvector", "K-Vector (geometric voting & pyramid)", PromptKVectorDatabaseBuilder);
dbBuilderChoice.Register("done", "Exit", NULL);
while (true) {
DbBuilder choice = dbBuilderChoice.Prompt("Choose database builder");
if (choice == NULL) {
break;
}
(*choice)(builder, catalog);

void GenerateDatabases(MultiDatabaseBuilder &builder, const Catalog &catalog, std::map<std::string,std::string> values) {

if (values["databaseBuilder"].compare("kvector") == 0) {
float minDistance = DegToRad(stof(values["kvector-min-distance"]));
float maxDistance = DegToRad(stof(values["kvector-max-distance"]));
long numBins = DegToRad(stof(values["kvector-distance-bins"]));
PromptKVectorDatabaseBuilder(builder, catalog, minDistance, maxDistance, numBins);
}

}

// void PromptDatabases(MultiDatabaseBuilder &builder, const Catalog &catalog) {
// InteractiveChoice<DbBuilder> dbBuilderChoice;

// adds these as options

// dbBuilderChoice.Register("kvector", "K-Vector (geometric voting & pyramid)", PromptKVectorDatabaseBuilder);
// dbBuilderChoice.Register("done", "Exit", NULL);
// gets the prompt and does (choice)(builder, catalog)

// while (true) {
// DbBuilder choice = dbBuilderChoice.Prompt("Choose database builder");
// if (choice == NULL) {
// break;
// }
// (*choice)(builder, catalog);
// }
// }

// PIPELINE INPUT STUFF

cairo_surface_t *PipelineInput::InputImageSurface() const {
Expand Down Expand Up @@ -1046,16 +1078,16 @@ void PromptPipelineComparison(const PipelineInputList &expected,
}

comparatorChoice.Register("done", "No more comparisons", NULL);

while (true) {
PipelineComparator comparator = comparatorChoice.Prompt("What to do with output");
if (comparator == NULL) {
break;
}

PromptedOutputStream pos;
comparator(pos.Stream(), expected, actual);
}
//TODO: fix
// while (true) {
// PipelineComparator comparator = comparatorChoice.Prompt("What to do with output");
// if (comparator == NULL) {
// break;
// }

// PromptedOutputStream pos;
// comparator(pos.Stream(), expected, actual);
// }
}

}
7 changes: 4 additions & 3 deletions src/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ S InteractiveChoice<S>::Prompt(const std::string &prompt) const {

class PromptedOutputStream {
public:
PromptedOutputStream();
PromptedOutputStream(std::string filePath);
~PromptedOutputStream();
std::ostream &Stream() { return *stream; };
private:
Expand Down Expand Up @@ -261,8 +261,9 @@ void PromptPipelineComparison(const PipelineInputList &expected,
Catalog PromptNarrowedCatalog(const Catalog &);

// unlike the other algorithm prompters, db builders aren't a
typedef void (*DbBuilder)(MultiDatabaseBuilder &, const Catalog &);
void PromptDatabases(MultiDatabaseBuilder &, const Catalog &);
// typedef void (*DbBuilder)(MultiDatabaseBuilder &, const Catalog &);
void GenerateDatabases(MultiDatabaseBuilder &, const Catalog &, std::map<std::string,std::string> values);
// void PromptDatabases(MultiDatabaseBuilder &, const Catalog &);

}

Expand Down
163 changes: 127 additions & 36 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,153 @@
#include <iostream>
#include <fstream>
#include <chrono>
#include <getopt.h>
#include <cstring>
#include <map>

#include "databases.hpp"
#include "centroiders.hpp"
#include "io.hpp"

namespace lost {

static void DatabaseBuild() {
Catalog narrowedCatalog = PromptNarrowedCatalog(CatalogRead());
static void DatabaseBuild(std::map<std::string,std::string> values) {
Catalog narrowedCatalog = NarrowCatalog(CatalogRead(),stoi(values["maxMagnitude"]),stoi(values["maxStars"]));
std::cerr << "Narrowed catalog has " << narrowedCatalog.size() << " stars." << std::endl;

MultiDatabaseBuilder builder;
unsigned char *catalogBuffer = builder.AddSubDatabase(kCatalogMagicValue,
// TODO: allow magnitude and weird
SerializeLengthCatalog(narrowedCatalog, false, true));
// TODO: allow magnitude and weird
unsigned char *catalogBuffer = builder.AddSubDatabase(kCatalogMagicValue,SerializeLengthCatalog(narrowedCatalog, false, true));
SerializeCatalog(narrowedCatalog, false, true, catalogBuffer);

PromptDatabases(builder, narrowedCatalog);
GenerateDatabases(builder, narrowedCatalog, values);

std::cerr << "Generated database with " << builder.BufferLength() << " bytes" << std::endl;
PromptedOutputStream pos;
PromptedOutputStream pos = PromptedOutputStream(values["path"]);
pos.Stream().write((char *)builder.Buffer(), builder.BufferLength());
}

static void PipelineRun() {
PipelineInputList input = PromptPipelineInput();
Pipeline pipeline = PromptPipeline();
std::vector<PipelineOutput> outputs = pipeline.Go(input);
PromptPipelineComparison(input, outputs);
}

static void PipelineBenchmark() {
PipelineInputList input = PromptPipelineInput();
Pipeline pipeline = PromptPipeline();
int iterations = Prompt<int>("Times to run the pipeline");
std::cerr << "Benchmarking..." << std::endl;

// TODO: we can do better than this :| maybe include mean time, 99% time, or allow a vector of
// input and determine which one took the longest
auto startTime = std::chrono::high_resolution_clock::now();
for (int i = 0; i < iterations; i++) {
pipeline.Go(input);
}
auto endTime = std::chrono::high_resolution_clock::now();
auto totalTime = std::chrono::duration<double, std::milli>(endTime - startTime);
std::cout << "total_ms " << totalTime.count() << std::endl;
}
// static void DatabaseBuild(int maxMagnitude, int maxStars) {
// Catalog narrowedCatalog = NarrowCatalog(CatalogRead(),maxMagnitude,maxStars);
// std::cerr << "Narrowed catalog has " << narrowedCatalog.size() << " stars." << std::endl;

// MultiDatabaseBuilder builder;
// unsigned char *catalogBuffer = builder.AddSubDatabase(kCatalogMagicValue,
// // TODO: allow magnitude and weird
// SerializeLengthCatalog(narrowedCatalog, false, true));
// SerializeCatalog(narrowedCatalog, false, true, catalogBuffer);

// PromptDatabases(builder, narrowedCatalog);

// std::cerr << "Generated database with " << builder.BufferLength() << " bytes" << std::endl;
// PromptedOutputStream pos;
// pos.Stream().write((char *)builder.Buffer(), builder.BufferLength());
// }

// static void PipelineRun() {
// PipelineInputList input = PromptPipelineInput();
// Pipeline pipeline = PromptPipeline();
// std::vector<PipelineOutput> outputs = pipeline.Go(input);
// PromptPipelineComparison(input, outputs);
// }

// static void PipelineBenchmark() {
// PipelineInputList input = PromptPipelineInput();
// Pipeline pipeline = PromptPipeline();
// int iterations = Prompt<int>("Times to run the pipeline");
// std::cerr << "Benchmarking..." << std::endl;

// // TODO: we can do better than this :| maybe include mean time, 99% time, or allow a vector of
// // input and determine which one took the longest
// auto startTime = std::chrono::high_resolution_clock::now();
// for (int i = 0; i < iterations; i++) {
// pipeline.Go(input);
// }
// auto endTime = std::chrono::high_resolution_clock::now();
// auto totalTime = std::chrono::duration<double, std::milli>(endTime - startTime);
// std::cout << "total_ms " << totalTime.count() << std::endl;
// }

}

int main(int argc, char **argv) {
lost::RegisterCliArgs(argc, argv);
std::cerr << "LOST: Open-source Star Tracker" << std::endl;
lost::InteractiveChoice<void (*)()> mainChoices;
mainChoices.Register("pipeline", "Run a pipeline", &lost::PipelineRun);
mainChoices.Register("benchmark", "Benchmark a pipeline", &lost::PipelineBenchmark);
mainChoices.Register("build_database", "Build database from catalog", &lost::DatabaseBuild);
(*mainChoices.Prompt("Choose action"))();

if (strcmp(argv[1], "database") == 0) {

static struct option long_options[] =
{
{"mag", required_argument, 0, 'm'},

This comment has been minimized.

Copy link
@markasoftware

markasoftware Nov 29, 2021

Member

These arguments are quite specific to database generation and probably do not need short options, it will just end up conflicting with short options elsewhere.

With that in mind, I'd recommend an enum for the vals instead of characters.

{"stars", required_argument, 0, 's'},
{"kvector", no_argument, 0, 'k'},
{"kvector-min-distance", required_argument, 0, 'a'},
{"kvector-max-distance", required_argument, 0, 'z'},
{"kvector-distance-bins", required_argument, 0, 'b'},
{0, 0, 0, 0}
};

// default values/flags
std::map<std::string,std::string> parsedValues = {
{"maxMagnitude","1000"},
{"maxStars","10000"},
{"databaseBuilder",""},
{"kvector-min-distance","0"},

This comment has been minimized.

Copy link
@markasoftware

markasoftware Nov 29, 2021

Member

Reasonable min-distance is DegToRad(0.5)

{"kvector-max-distance","0"},

This comment has been minimized.

Copy link
@markasoftware

markasoftware Nov 29, 2021

Member

reasonable max-distance is DegToRad(15)

{"kvector-distance-bins","0"},

This comment has been minimized.

Copy link
@markasoftware

markasoftware Nov 29, 2021

Member

Reasonable kvector-distance-bins is 10000

{"path",""}
};

int index;
int option;
while (optind < argc) {
if ((option = getopt_long(argc, argv, "m:s:ka:z:b:", long_options, &index)) != -1) {
switch (option) {
case 'm' :
std::cout << "You raised the maginude to " << optarg << std::endl;
parsedValues["maxMagnitude"] = optarg;
break;
case 's' :
std::cout << "You lowered the stars to " << optarg << std::endl;
parsedValues["maxStars"] = optarg;
break;
case 'k' :
std::cout << "You picked the kvector version! " << std::endl;
parsedValues["databaseBuilder"] = "kvector";
break;
case 'a' :
std::cout << "You set the min distance to " << optarg << std::endl;
parsedValues["kvector-min-distance"] = optarg;
break;
case 'z' :
std::cout << "You set the max distance to " << optarg << std::endl;
parsedValues["kvector-max-distance"] = optarg;
break;
case 'b' :
std::cout << "You set the number of bins to " << optarg << std::endl;
parsedValues["kvector-distance-bins"] = optarg;
break;
default :
std::cout << "Illegal flag" << std::endl;
exit(1);
}
} else {
parsedValues["path"] = optarg;
std::cout << "You set the path to " << optarg << std::endl;

optind++;
}
}

lost::DatabaseBuild(parsedValues);

}
return 0;
}

// lost::RegisterCliArgs(argc, argv);
// std::cerr << "LOST: Open-source Star Tracker" << std::endl;
// lost::InteractiveChoice<void (*)()> mainChoices;
// mainChoices.Register("pipeline", "Run a pipeline", &lost::PipelineRun);
// mainChoices.Register("benchmark", "Benchmark a pipeline", &lost::PipelineBenchmark);
// mainChoices.Register("build_database", "Build database from catalog", &lost::DatabaseBuild);
// (*mainChoices.Prompt("Choose action"))();

0 comments on commit 7e11205

Please sign in to comment.