aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c44
1 files changed, 40 insertions, 4 deletions
diff --git a/main.c b/main.c
index ed46af7..55056b7 100644
--- a/main.c
+++ b/main.c
@@ -69,7 +69,7 @@ ctx_best_sort(
}
static void
-ctx_best_each(
+ctx_best_walk(
const ctx_t * const ctx,
void (*on_best)(const km_set_t * const, const size_t, const float, void *),
void * const cb_data
@@ -176,11 +176,13 @@ find_on_best(
) {
ctx_t * const ctx = cb_data;
- D("best score = %0.3f, num_clusters = %zu", score, cs->num_rows);
+ D("new best: score = %0.3f, num_clusters = %zu", score, cs->num_rows);
// get pointer to destination set
// (note: data->best is a ring buffer)
- km_set_t * const dst = &(ctx->best[ctx->num_best % MAX_BEST].set);
+ const size_t ofs = ctx->num_best % MAX_BEST;
+ ctx->best[ofs].score = score;
+ km_set_t * const dst = &(ctx->best[ofs].set);
if (ctx->num_best >= MAX_BEST) {
// finalize old best data set
@@ -287,7 +289,7 @@ ctx_save_png(
}
// draw best cluster points
- ctx_best_each(ctx, save_on_best, (void*) ctx);
+ ctx_best_walk(ctx, save_on_best, (void*) ctx);
// save png
if (!stbi_write_png(png_path, IM_WIDTH, IM_HEIGHT, 3, im_data, IM_STRIDE)) {
@@ -295,6 +297,37 @@ ctx_save_png(
}
}
+static void
+ctx_best_print_on_best(
+ const km_set_t * const set,
+ const size_t rank,
+ const float score,
+ void * const cb_data
+) {
+ FILE * const fh = cb_data;
+
+ fprintf(fh, "rank = %zu, score = %0.3f, num_clusters = %zu: [\n", rank, score, set->num_rows);
+ for (size_t i = 0; i < set->num_rows; i++) {
+ const float * const vals = km_set_get_row(set, i);
+ fprintf(fh, " [");
+
+ for (size_t j = 0; j < set->shape.num_floats; j++) {
+ fprintf(fh, "%s%0.3f", (j > 0) ? ", " : "", vals[j]);
+ }
+
+ fprintf(fh, "], (%d rows)\n", set->ints[i]);
+ }
+ fprintf(fh, "]\n");
+}
+
+static void
+ctx_best_print(
+ const ctx_t * const ctx,
+ FILE * const fh
+) {
+ ctx_best_walk(ctx, ctx_best_print_on_best, fh);
+}
+
static const char USAGE_FORMAT[] =
"Usage: %s [init] [data_path] <png_path>\n"
"\n"
@@ -349,6 +382,9 @@ int main(int argc, char *argv[]) {
// sort best results from lowest to highest
ctx_best_sort(&ctx);
+ // print best
+ ctx_best_print(&ctx, stdout);
+
if (png_path) {
// save png of normalized data set and best clusters
ctx_save_png(&ctx, png_path, &set);