/** * Adjust the state of this operation. * * @param chromosomeArrayData Chromosome array data that will modify the internal state * @throws AnalyticException if there is a computational error */ public final void adjustState(final ChromosomeArrayData chromosomeArrayData) throws AnalyticException { for (ArrayDatum ad : chromosomeArrayData.getArrayData()) { if (this.operation == MEAN) { this.sum += ad.getValue(); this.numDatum++; } else if (this.operation == MEDIAN) { this.values.add(ad.getValue()); } } this.sorted = false; }
/** * Perform operation. * * @param input Input data * @return Output data * @throws AnalyticException if an error occurs during this operation */ public final ChromosomeArrayData perform(final ChromosomeArrayData input) throws AnalyticException { // Make sure mean or median can be calculated if (this.operation == MEAN) { if (this.numDatum == 0) { throw new AnalyticException("Cannot compute mean over 0 values"); } } else if (this.operation == MEDIAN) { if (this.values.size() == 0) { throw new RuntimeException("Cannot compute median over 0 values"); } } // Calculate mean or median, as appropriate float stat = (float) 0.0; if (this.operation == MEAN) { stat = this.sum / (float) this.numDatum; } else if (this.operation == MEDIAN) { if (!this.sorted) { Collections.sort(this.values); this.sorted = true; } stat = this.values.get(this.values.size() / 2); } // Iterate over datum and subtract mean or median ChromosomeArrayData output = new ChromosomeArrayData(input.getChromosome()); for (ArrayDatum ad : input.getArrayData()) { ArrayDatum newAd = new ArrayDatum(ad); newAd.setValue(ad.getValue() - stat); output.add(newAd); } return output; }