void _nrrdMeasureMode(void *ans, int ansType, const void *_line, int lineType, size_t len, double axmin, double axmax) { Nrrd *nline, *nhist; void *line; AIR_UNUSED(axmin); AIR_UNUSED(axmax); line = calloc(len, nrrdTypeSize[lineType]); if (line) { memcpy(line, _line, len*nrrdTypeSize[lineType]); nline = nrrdNew(); if (nrrdWrap_va(nline, line, lineType, 1, len)) { free(biffGetDone(NRRD)); nrrdNix(nline); nrrdDStore[ansType](ans, AIR_NAN); return; } nhist = nrrdNew(); if (nrrdHisto(nhist, nline, NULL, NULL, nrrdStateMeasureModeBins, nrrdTypeInt)) { free(biffGetDone(NRRD)); nrrdNuke(nhist); nrrdNix(nline); nrrdDStore[ansType](ans, AIR_NAN); return; } /* now we pass this histogram off to histo-mode */ _nrrdMeasureHistoMode(ans, ansType, nhist->data, nrrdTypeInt, nrrdStateMeasureModeBins, nhist->axis[0].min, nhist->axis[0].max); nrrdNuke(nhist); nrrdNix(nline); } else { nrrdDStore[ansType](ans, 0); } return; }
int main(int argc, const char *argv[]) { const char *me; size_t vi, ii, qvalLen; Nrrd *nval, *nhist, *nimg, *nread, *ncorr; double aa, bb, *val; airArray *mop; char *corrname, explain[AIR_STRLEN_LARGE]; int differ; AIR_UNUSED(argc); me = argv[0]; mop = airMopNew(); qvalLen = 10*BINS; nrrdAlloc_va(nval=nrrdNew(), nrrdTypeDouble, 1, 4*qvalLen); airMopAdd(mop, nval, (airMopper)nrrdNuke, airMopAlways); val = AIR_CAST(double*, nval->data); airSrandMT(999); vi = 0; for (ii=0; ii<qvalLen; ii++) { airNormalRand(&aa, NULL); val[vi++] = aa; } for (ii=0; ii<qvalLen; ii++) { airNormalRand(NULL, &bb); val[vi++] = bb; } for (ii=0; ii<qvalLen; ii++) { airNormalRand(&aa, &bb); val[vi++] = aa; val[vi++] = bb; } nhist=nrrdNew(); airMopAdd(mop, nhist, (airMopper)nrrdNuke, airMopAlways); nimg=nrrdNew(); airMopAdd(mop, nimg, (airMopper)nrrdNuke, airMopAlways); if (nrrdHisto(nhist, nval, NULL, NULL, BINS, nrrdTypeInt) || nrrdHistoDraw(nimg, nhist, HGHT, AIR_TRUE, 0.0) || nrrdSave(THISNAME, nimg, NULL)) { char *err; airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); fprintf(stderr, "%s: trouble producing histo:\n%s", me, err); airMopError(mop); return 1; } nread = nrrdNew(); airMopAdd(mop, nread, (airMopper)nrrdNuke, airMopAlways); ncorr = nrrdNew(); airMopAdd(mop, ncorr, (airMopper)nrrdNuke, airMopAlways); corrname = testDataPathPrefix(CORRNAME); airMopAdd(mop, corrname, airFree, airMopAlways); if (nrrdLoad(ncorr, corrname, NULL) || nrrdLoad(nread, THISNAME, NULL)) { char *err; airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); fprintf(stderr, "%s: trouble reading:\n%s", me, err); airMopError(mop); return 1; } if (nrrdCompare(ncorr, nread, AIR_FALSE /* onlyData */, 0.0 /* epsilon */, &differ, explain)) { char *err; airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); fprintf(stderr, "%s: trouble comparing:\n%s", me, err); airMopError(mop); return 1; } if (differ) { fprintf(stderr, "%s: new and correct (%s) images differ: %s\n", me, corrname, explain); airMopError(mop); return 1; } else { printf("%s: all good\n", me); } airMopOkay(mop); return 0; }
int unrrdu_histoMain(int argc, const char **argv, const char *me, hestParm *hparm) { hestOpt *opt = NULL; char *out, *err; Nrrd *nin, *nout, *nwght; char *minStr, *maxStr; int type, pret, blind8BitRange; unsigned int bins; NrrdRange *range; airArray *mop; hestOptAdd(&opt, "b,bins", "num", airTypeUInt, 1, 1, &bins, NULL, "# of bins in histogram"); hestOptAdd(&opt, "w,weight", "nweight", airTypeOther, 1, 1, &nwght, "", "how to weigh contributions to histogram. By default " "(not using this option), the increment is one bin count per " "sample, but by giving a nrrd, the value in the nrrd at the " "corresponding location will be the bin count increment ", NULL, NULL, nrrdHestNrrd); hestOptAdd(&opt, "min,minimum", "value", airTypeString, 1, 1, &minStr, "nan", "Value at low end of histogram, given explicitly as a " "regular number, " "*or*, if the number is given with a \"" NRRD_MINMAX_PERC_SUFF "\" suffix, this " "minimum is specified in terms of the percentage of samples in " "input that are lower. " "By default (not using this option), the lowest value " "found in input nrrd."); hestOptAdd(&opt, "max,maximum", "value", airTypeString, 1, 1, &maxStr, "nan", "Value at high end of histogram, given " "explicitly as a regular number, " "*or*, if the number is given with " "a \"" NRRD_MINMAX_PERC_SUFF "\" suffix, " "this maximum is specified " "in terms of the percentage of samples in input that are higher. " "Defaults to highest value found in input nrrd."); hestOptAdd(&opt, "blind8", "bool", airTypeBool, 1, 1, &blind8BitRange, nrrdStateBlind8BitRange ? "true" : "false", "Whether to know the range of 8-bit data blindly " "(uchar is always [0,255], signed char is [-128,127])."); OPT_ADD_TYPE(type, "type to use for bins in output histogram", "uint"); OPT_ADD_NIN(nin, "input nrrd"); OPT_ADD_NOUT(out, "output nrrd"); mop = airMopNew(); airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways); USAGE(_unrrdu_histoInfoL); PARSE(); airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways); range = nrrdRangeNew(AIR_NAN, AIR_NAN); airMopAdd(mop, range, (airMopper)nrrdRangeNix, airMopAlways); nout = nrrdNew(); airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways); if (nrrdRangePercentileFromStringSet(range, nin, minStr, maxStr, 10*bins /* HEY magic */, blind8BitRange) || nrrdHisto(nout, nin, range, nwght, bins, type)) { airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); fprintf(stderr, "%s: error with range or quantizing:\n%s", me, err); airMopError(mop); return 1; } SAVE(out, nout, NULL); airMopOkay(mop); return 0; }
int unrrdu_histoMain(int argc, char **argv, char *me, hestParm *hparm) { hestOpt *opt = NULL; char *out, *err; Nrrd *nin, *nout, *nwght; int type, pret, blind8BitRange; unsigned int bins; double min, max; NrrdRange *range; airArray *mop; hestOptAdd(&opt, "b,bins", "num", airTypeUInt, 1, 1, &bins, NULL, "# of bins in histogram"); hestOptAdd(&opt, "w,weight", "nweight", airTypeOther, 1, 1, &nwght, "", "how to weigh contributions to histogram. By default " "(not using this option), the increment is one bin count per " "sample, but by giving a nrrd, the value in the nrrd at the " "corresponding location will be the bin count increment ", NULL, NULL, nrrdHestNrrd); hestOptAdd(&opt, "min,minimum", "value", airTypeDouble, 1, 1, &min, "nan", "Value at low end of histogram. Defaults to lowest value " "found in input nrrd."); hestOptAdd(&opt, "max,maximum", "value", airTypeDouble, 1, 1, &max, "nan", "Value at high end of histogram. Defaults to highest value " "found in input nrrd."); hestOptAdd(&opt, "blind8", "bool", airTypeBool, 1, 1, &blind8BitRange, nrrdStateBlind8BitRange ? "true" : "false", "Whether to know the range of 8-bit data blindly " "(uchar is always [0,255], signed char is [-128,127])."); OPT_ADD_TYPE(type, "type to use for bins in output histogram", "uint"); OPT_ADD_NIN(nin, "input nrrd"); OPT_ADD_NOUT(out, "output nrrd"); mop = airMopNew(); airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways); USAGE(_unrrdu_histoInfoL); PARSE(); airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways); nout = nrrdNew(); airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways); /* If the input nrrd never specified min and max, then they'll be AIR_NAN, and nrrdRangeSafeSet will find them, and will do so according to blind8BitRange */ range = nrrdRangeNew(min, max); airMopAdd(mop, range, (airMopper)nrrdRangeNix, airMopAlways); nrrdRangeSafeSet(range, nin, blind8BitRange); if (nrrdHisto(nout, nin, range, nwght, bins, type)) { err = biffGet(NRRD); fprintf(stderr, "%s: error calculating histogram:\n%s", me, err); free(err); return 1; } SAVE(out, nout, NULL); airMopOkay(mop); return 0; }