Alien-libhisto

 view release on metacpan or  search on metacpan

bundled/tools/src/cmd_stats.c  view on Meta::CPAN


    uint32_t nx = histo2d_nbins_x(h);
    uint32_t ny = histo2d_nbins_y(h);

    if (strcmp(fmt, "json") == 0) {
        fprintf(out, "{\n");
        fprintf(out, "  \"type\": \"2d\",\n");
        fprintf(out, "  \"nbins_x\": %u,\n", nx);
        fprintf(out, "  \"nbins_y\": %u,\n", ny);
        fprintf(out, "  \"entries\": %llu,\n", (unsigned long long)s.n_entries);
        fprintf(out, "  \"total_weight\": %.8g,\n", s.total_weight);
        fprintf(out, "  \"mean_x\": %.8g,\n", s.mean_x);
        fprintf(out, "  \"mean_y\": %.8g,\n", s.mean_y);
        fprintf(out, "  \"variance_x\": %.8g,\n", s.variance_x);
        fprintf(out, "  \"variance_y\": %.8g,\n", s.variance_y);
        fprintf(out, "  \"std_dev_x\": %.8g,\n", s.std_dev_x);
        fprintf(out, "  \"std_dev_y\": %.8g,\n", s.std_dev_y);
        fprintf(out, "  \"covariance\": %.8g,\n", s.covariance);
        fprintf(out, "  \"correlation\": %.8g\n", s.correlation);
        fprintf(out, "}\n");
    } else if (strcmp(fmt, "tsv") == 0) {
        fprintf(out, "metric\tvalue\n");
        fprintf(out, "nbins_x\t%u\n", nx);
        fprintf(out, "nbins_y\t%u\n", ny);
        fprintf(out, "entries\t%llu\n", (unsigned long long)s.n_entries);
        fprintf(out, "total_weight\t%.8g\n", s.total_weight);
        fprintf(out, "mean_x\t%.8g\n", s.mean_x);
        fprintf(out, "mean_y\t%.8g\n", s.mean_y);
        fprintf(out, "variance_x\t%.8g\n", s.variance_x);
        fprintf(out, "variance_y\t%.8g\n", s.variance_y);
        fprintf(out, "std_dev_x\t%.8g\n", s.std_dev_x);
        fprintf(out, "std_dev_y\t%.8g\n", s.std_dev_y);
        fprintf(out, "covariance\t%.8g\n", s.covariance);
        fprintf(out, "correlation\t%.8g\n", s.correlation);
    } else {
        fprintf(out, "===============================================================\n");
        fprintf(out, " 2-DIMENSIONAL HISTOGRAM SUMMARY STATISTICS\n");
        fprintf(out, "===============================================================\n");
        fprintf(out, "  Grid Dimensions:  %u x %u bins\n", nx, ny);
        fprintf(out, "  Total Entries:    %llu\n", (unsigned long long)s.n_entries);
        fprintf(out, "  Total Weight:     %.6g\n", s.total_weight);
        fprintf(out, "  Rejected NaNs:    %llu\n", (unsigned long long)histo2d_nan_count(h));
        fprintf(out, "---------------------------------------------------------------\n");
        fprintf(out, "  X-Axis:  Range [%.4g, %.4g]\n", s.min_x, s.max_x);
        fprintf(out, "    Mean (X):       %-12.6g Std Dev (X):  %.6g\n", s.mean_x, s.std_dev_x);
        fprintf(out, "    Variance (X):   %-12.6g\n", s.variance_x);
        fprintf(out, "  Y-Axis:  Range [%.4g, %.4g]\n", s.min_y, s.max_y);
        fprintf(out, "    Mean (Y):       %-12.6g Std Dev (Y):  %.6g\n", s.mean_y, s.std_dev_y);
        fprintf(out, "    Variance (Y):   %-12.6g\n", s.variance_y);
        fprintf(out, "---------------------------------------------------------------\n");
        fprintf(out, "  Bivariate Correlation & Covariance:\n");
        fprintf(out, "    Covariance:     %-12.6g Pearson (rho): %.6f\n", s.covariance, s.correlation);
        fprintf(out, "===============================================================\n");
    }
}

#include "cli_opt.h"

int histo_cli_stats(int argc, char **argv, FILE *out, FILE *err) {
    if (!out) out = stdout;
    if (!err) err = stderr;

    const char *fmt = "table";
    bool all_metrics = true;

    const cli_opt_spec_t specs[] = {
        {'f', "format", NULL, CLI_OPT_TYPE_STRING, &fmt, NULL, 0, "FMT",
         "Output format: table (default), json, tsv", "table"},
        {'a', "all", NULL, CLI_OPT_TYPE_BOOL, &all_metrics, NULL, 0, NULL,
         "Compute all extended higher moments and peak metrics", NULL}
    };

    cli_opt_parser_t parser;
    cli_opt_init(&parser, specs, sizeof(specs) / sizeof(specs[0]),
                 "histo-stats", "[OPTIONS] [HISTOGRAM_FILE...]",
                 "Displays comprehensive statistical summary, moments, and robust metrics for 1D and 2D histograms.");

    int rc = cli_opt_parse(&parser, argc, argv, 1);
    if (rc < 0) {
        cli_opt_print_help(&parser, out);
        cli_opt_free(&parser);
        return 0;
    }
    if (rc > 0) {
        fprintf(err, "%s\n", cli_opt_error(&parser));
        cli_opt_free(&parser);
        return 1;
    }

    const char *default_files[] = {"-"};
    const char **files = (parser.num_positionals > 0) ? parser.positionals : default_files;
    int nfiles = (parser.num_positionals > 0) ? parser.num_positionals : 1;

    int status = 0;
    for (int f = 0; f < nfiles; ++f) {
        histo_t *h = NULL;
        histo2d_t *h2d = NULL;
        if (cli_read_any_histogram_from_file(files[f], &h, &h2d) == HISTO_OK) {
            if (h2d) {
                print_histo2d_stats(h2d, fmt, out);
                histo2d_destroy(h2d);
            } else if (h) {
                print_histogram_stats(h, fmt, out);
                histo_destroy(h);
            }
        } else {
            fprintf(err, "Error: Failed to read histogram from '%s'\n", files[f]);
            status = 1;
        }
    }

    cli_opt_free(&parser);
    return status;
}



( run in 0.850 second using v1.01-cache-2.11-cpan-5c0b1e786e0 )