IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 24, 2007, 11:12:34 AM (18 years ago)
Author:
eugene
Message:

moved stats calculation out of pmMaskFlagSuspectPixels; added options for per-readout vs per-chip stats

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ppMerge/src/ppMergeMask.c

    r15862 r15913  
    4343        pmChip *chipIn;                 // Input chip of interest
    4444        while ((chipIn = pmFPAviewNextChip(view, fpaIn, 1))) {
     45
     46            // handle chip vs cell statistics & avoid reading the data twice
     47
     48            // load the data of all cells
    4549            pmCell *cellIn;             // Input cell of interest
    4650            while ((cellIn = pmFPAviewNextCell(view, fpaIn, 1))) {
    47                 if (!pmCellRead(cellIn, fits, config->database)) {
    48                     continue;
    49                 }
    50                 if (cellIn->readouts->n == 0) {
    51                     continue;
    52                 }
    53 
    54                 pmCell *cellOut = pmFPAviewThisCell(view, fpaOut); // Output cell
    55                 // Suspect pixels image
    56                 psImage *suspect = psMetadataLookupPtr(NULL, cellOut->analysis, "MASK.SUSPECT");
    57                 bool first = suspect ? false : true;
    58 
    59                 pmReadout *roIn;        // Input readout of interest
    60                 while ((roIn = pmFPAviewNextReadout(view, fpaIn, 1))) {
    61                     if (!roIn->image) {
    62                         continue;
    63                     }
    64                     psTrace("ppMerge", 4, "Flagging suspect pixels in chip %d, cell %d, ro %d\n",
    65                             view->chip, view->cell, view->readout);
    66                     float frac = options->sample / (float)(roIn->image->numCols * roIn->image->numRows);
    67                     suspect = pmMaskFlagSuspectPixels(suspect, roIn, options->maskSuspect,
    68                                                       options->combine->maskVal, frac, rng);
    69                 }
    70 
    71                 if (first) {
    72                     psMetadataAddImage(cellOut->analysis, PS_LIST_TAIL, "MASK.SUSPECT", 0,
    73                                        "Suspect pixels", suspect);
    74                     psFree(suspect);
    75                 }
    76 
     51                if (!pmCellRead(cellIn, fits, config->database)) continue;
     52                if (cellIn->readouts->n == 0) continue;
     53            }
     54
     55            // calculate the readout statistics either for each readout, or across the entire chip
     56            if (options->statsByChip) {
     57                ppMergeMaskChipStats (chipIn, options, rng);
     58            } else {
     59                // calculate the stats for each cell independently
     60                while ((cellIn = pmFPAviewNextCell(view, fpaIn, 1))) {
     61                    if (cellIn->readouts->n == 0) continue;
     62                    pmReadout *roIn;        // Input readout of interest
     63                    while ((roIn = pmFPAviewNextReadout(view, fpaIn, 1))) {
     64                        if (!roIn->image) continue;
     65                        psTrace("ppMerge", 4, "Measure statistics for chip %d, cell %d, ro %d\n",
     66                                view->chip, view->cell, view->readout);
     67                        ppMergeMaskReadoutStats (roIn, options, rng);
     68                    }
     69                }
     70            }
     71
     72            // apply the measured statistics to determine the outliers to be masked
     73            while ((cellIn = pmFPAviewNextCell(view, fpaIn, 1))) {
     74                if (cellIn->readouts->n == 0) continue;
     75
     76                pmCell *cellOut = pmFPAviewThisCell(view, fpaOut); // Output cell
     77                // Suspect pixels image
     78                psImage *suspect = psMetadataLookupPtr(NULL, cellOut->analysis, "MASK.SUSPECT");
     79                bool first = suspect ? false : true;
     80
     81                pmReadout *roIn;        // Input readout of interest
     82                while ((roIn = pmFPAviewNextReadout(view, fpaIn, 1))) {
     83                    if (!roIn->image) {
     84                        continue;
     85                    }
     86                    psTrace("ppMerge", 4, "Flagging suspect pixels in chip %d, cell %d, ro %d\n",
     87                            view->chip, view->cell, view->readout);
     88                    suspect = pmMaskFlagSuspectPixels(suspect, roIn, options->maskSuspect, options->combine->maskVal);
     89                }
     90
     91                if (first) {
     92                    psMetadataAddImage(cellOut->analysis, PS_LIST_TAIL, "MASK.SUSPECT", 0,
     93                                       "Suspect pixels", suspect);
     94                    psFree(suspect);
     95                }
    7796                pmCellFreeData(cellIn);
    7897            }
     
    195214    return true;
    196215}
     216
     217bool ppMergeMaskReadoutStats(const pmReadout *readout,
     218                             ppMergeOptions *options, // Options
     219                             psRandom *rng)
     220{
     221    PS_ASSERT_PTR_NON_NULL(readout, false);
     222    PS_ASSERT_IMAGE_NON_NULL(readout->image, false);
     223    PS_ASSERT_IMAGE_NON_EMPTY(readout->image, false);
     224    PS_ASSERT_IMAGE_TYPE(readout->image, PS_TYPE_F32, false);
     225    if (readout->mask) {
     226        PS_ASSERT_IMAGE_NON_EMPTY(readout->mask, false);
     227        PS_ASSERT_IMAGES_SIZE_EQUAL(readout->image, readout->mask, false);
     228        PS_ASSERT_IMAGE_TYPE(readout->mask, PS_TYPE_MASK, false);
     229    }
     230
     231    psImage *image = readout->image;    // Image of interest
     232    psImage *mask = readout->mask;      // Corresponding mask
     233
     234    if (rng) {
     235        psMemIncrRefCounter(rng);
     236    } else {
     237        rng = psRandomAlloc(PS_RANDOM_TAUS, 0);
     238    }
     239
     240    // XXX note that this now will accept any of several stats options
     241    psStats *stats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV);
     242    stats->nSubsample = options->sample;
     243
     244    if (!psImageBackground(stats, NULL, image, mask, options->combine->maskVal, rng)) {
     245        psError(PS_ERR_UNKNOWN, false, "Failure to measure image statistics.\n");
     246        psFree(stats);
     247        psFree(rng);
     248        return false;
     249    }
     250    if (!isfinite(stats->robustMedian) || !isfinite(stats->robustUQ) || !isfinite(stats->robustLQ)) {
     251        psError(PS_ERR_UNKNOWN, false, "invalide image statistics (nan).\n");
     252        psFree(stats);
     253        psFree(rng);
     254        return false;
     255    }
     256
     257    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "READOUT.MEDIAN", PS_META_REPLACE, "image stats", stats->robustMedian);
     258    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "READOUT.STDEV",  PS_META_REPLACE, "image stats", stats->robustStdev);
     259
     260    psFree(rng);
     261    return true;
     262}
     263
     264bool ppMergeMaskChipStats (const pmChip *chip,
     265                           ppMergeOptions *options,
     266                           psRandom *rng)
     267{
     268    PS_ASSERT_PTR_NON_NULL(chip, false);
     269
     270    // XXX note that this now will accept any of several stats options
     271    psStats *stats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV);
     272
     273    if (rng) {
     274        psMemIncrRefCounter(rng);
     275    } else {
     276        rng = psRandomAlloc(PS_RANDOM_TAUS, 0);
     277    }
     278
     279    // accumulate a vector of data values using options->sample per readout
     280    psVector *values = psVectorAllocEmpty(options->sample, PS_TYPE_F32); // Vector containing subsample
     281
     282    for (int nCell = 0; nCell < chip->cells->n; nCell++) {
     283        pmCell *cell = chip->cells->data[nCell];
     284        if (cell->readouts->n == 0) continue;
     285
     286        for (int nReadout = 0; nReadout < cell->readouts->n; nReadout++) {
     287            pmReadout *readout = cell->readouts->data[nReadout];
     288            if (!readout->image) continue;
     289
     290            psTrace("ppMerge", 4, "Measure statistics for cell %d, readout %d\n", nCell, nReadout);
     291
     292            psImage *image = readout->image;    // Image of interest
     293            psImage *mask = readout->mask;      // Corresponding mask
     294
     295            // Size of image
     296            long nx = image->numCols;
     297            long ny = image->numRows;
     298            const int Npixels = nx*ny;  // Total number of pixels
     299            const int Nsubset = PS_MIN(options->sample, Npixels); // Number of pixels in subset
     300           
     301            values = psVectorRealloc (values, values->n + Nsubset); // make sure we have enough space
     302
     303            // select a subset of the image pixels to measure the stats
     304            for (long i = 0; i < Nsubset; i++) {
     305                double frnd = psRandomUniform(rng);
     306                int pixel = Npixels * frnd;
     307                int ix = pixel % nx;
     308                int iy = pixel / nx;
     309
     310                if (!isfinite(image->data.F32[iy][ix])) continue;
     311                if (mask && (mask->data.U8[iy][ix] & options->combine->maskVal)) continue;
     312
     313                float value = image->data.F32[iy][ix];
     314                values->data.F32[values->n] = value;
     315                values->n ++;
     316            }
     317        }
     318    }
     319
     320    // calculate the statistics
     321    if (!psVectorStats (stats, values, NULL, NULL, 0)) {
     322        psError(PS_ERR_UNKNOWN, false, "Unable to measure statistics for chip");
     323        psFree(values);
     324        psFree(stats);
     325        psFree(rng);
     326        return false;
     327    }
     328    if (!isfinite(stats->robustMedian) || !isfinite(stats->robustUQ) || !isfinite(stats->robustLQ)) {
     329        psError(PS_ERR_UNKNOWN, false, "invalide image statistics (nan).\n");
     330        psFree(values);
     331        psFree(stats);
     332        psFree(rng);
     333        return false;
     334    }
     335
     336    // supply the stats to the readout analysis metadata
     337    for (int nCell = 0; nCell < chip->cells->n; nCell++) {
     338        pmCell *cell = chip->cells->data[nCell];
     339        if (cell->readouts->n == 0) continue;
     340
     341        for (int nReadout = 0; nReadout < cell->readouts->n; nReadout++) {
     342            pmReadout *readout = cell->readouts->data[nReadout];
     343            if (!readout->image) continue;
     344
     345            psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "READOUT.MEDIAN", PS_META_REPLACE, "image stats", stats->robustMedian);
     346            psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "READOUT.STDEV",  PS_META_REPLACE, "image stats", stats->robustStdev);
     347        }
     348    }
     349
     350    psFree(values);
     351    psFree(stats);
     352    psFree(rng);
     353    return true;
     354}
Note: See TracChangeset for help on using the changeset viewer.