From e63a88ea567744d890d923d1d7aa46e583cbb0ec Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Tue, 5 Feb 2019 00:50:28 -0500 Subject: add km_set_print(), refactor main.c --- src/main.c | 119 +++++++++++++++++++++++++------------------------------------ 1 file changed, 49 insertions(+), 70 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 55056b7..88e4f94 100644 --- a/src/main.c +++ b/src/main.c @@ -44,6 +44,13 @@ typedef struct { size_t num_best; } ctx_t; +static inline size_t +ctx_get_max_best( + const ctx_t * const ctx +) { + return MIN(ctx->num_best, MAX_BEST); +} + static int best_score_cmp( const void * const ap, @@ -62,7 +69,7 @@ ctx_best_sort( // sort best sets by ascending score (worst to best) qsort( ctx->best, - MIN(ctx->num_best, MAX_BEST), + ctx_get_max_best(ctx), sizeof(best_item_t), best_score_cmp ); @@ -76,7 +83,7 @@ ctx_best_walk( ) { if (on_best) { // walk best sets and emit each one - for (size_t i = 0; i < MIN(ctx->num_best, MAX_BEST); i++) { + for (size_t i = 0; i < ctx_get_max_best(ctx); i++) { on_best(&(ctx->best[i].set), i, ctx->best[i].score, cb_data); } } @@ -213,42 +220,6 @@ FIND_CBS = { .on_best = find_on_best, }; -static void -ctx_csv_print_row( - const ctx_t * const ctx, - FILE * const fh, - const size_t i -) { - const size_t num_clusters = i + 2; - const float mean_distance = ctx->rows[i].distance / NUM_TESTS, - mean_cluster_size = ctx->rows[i].cluster_size / NUM_TESTS, - mean_empty = 1.0 * ctx->rows[i].num_empty / NUM_TESTS, - score = ctx->rows[i].silouette / NUM_TESTS; - - // print result - fprintf(fh, "%zu,%0.3f,%0.3f,%0.3f,%0.3f\n", - num_clusters, - score, - mean_distance, - mean_cluster_size, - mean_empty - ); -} - -static void -ctx_csv_print( - const ctx_t * const ctx, - FILE * const fh -) { - // print headers - fprintf(fh, "#,score,distance,size,empty\n"); - - // print rows - for (size_t i = 0; i < MAX_CLUSTERS - 2; i++) { - ctx_csv_print_row(ctx, fh, i); - } -} - // static image data buffer static uint8_t im_data[3 * IM_WIDTH * IM_HEIGHT]; @@ -260,17 +231,21 @@ save_on_best( void * const cb_data ) { const ctx_t * const ctx = cb_data; + const bool is_best = (rank == ctx_get_max_best(ctx) - 1); UNUSED(score); // convert rank to channel brightness - const uint8_t ch = 0x33 + (0xff - 0x33) * (1.0 * rank + 1) / (MAX_BEST); - const uint8_t shift = (rank == MIN(ctx->num_best, MAX_BEST) - 1) ? 8 : 16; - const uint32_t color = (ch & 0xff) << shift; - // const uint32_t color = 0xff0000; - // D("rank = %zu, score = %0.3f, size = %zu, color = %06x", rank, score, set->num_rows, color); - - // draw clusters - km_set_draw(set, im_data, IM_WIDTH, IM_HEIGHT, 3, color); + if (is_best) { + // draw best in green + km_set_draw(set, im_data, IM_WIDTH, IM_HEIGHT, 3, 0x00ff00); + } else if (false) { + // draw lower-ranked "best" results + const uint8_t ch = 0x33 + (0xff - 0x33) * (1.0 * rank + 1) / (MAX_BEST); + const uint32_t color = (ch & 0xff) << 16; + + // draw clusters + km_set_draw(set, im_data, IM_WIDTH, IM_HEIGHT, 2, color); + } } static void @@ -297,35 +272,39 @@ 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_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); + if (!km_set_print(&(ctx->best[ctx_get_max_best(ctx) - 1].set), fh)) { + die("km_set_print()"); + } } static const char USAGE_FORMAT[] = @@ -377,7 +356,7 @@ int main(int argc, char *argv[]) { } // print csv - ctx_csv_print(&ctx, stdout); + // ctx_csv_print(&ctx, stdout); // sort best results from lowest to highest ctx_best_sort(&ctx); -- cgit v1.2.3