Skip to content

Commit

Permalink
Merge pull request #8 from davidmsibley/GCMON-277
Browse files Browse the repository at this point in the history
Gcmon 277
  • Loading branch information
David M Sibley committed Apr 2, 2015
2 parents 8a77178 + a65d7a4 commit e84f3b7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gov.usgs.cida.gcmrcservices.nude;

import com.google.common.collect.Range;
import com.google.common.collect.Ranges;
import gov.usgs.cida.nude.column.Column;
import gov.usgs.cida.nude.column.ColumnGrouping;
import gov.usgs.cida.nude.column.SimpleColumn;
Expand All @@ -12,10 +14,10 @@
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Set;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.slf4j.Logger;
Expand All @@ -29,11 +31,13 @@ public class BedSedAverageResultSet extends PeekingResultSet {
private static final Logger log = LoggerFactory.getLogger(BedSedAverageResultSet.class);

protected static final BigDecimal cutoffMassInGrams = new BigDecimal("20.000");
protected static final SortedSet<SampleSetRule> rules = new TreeSet(Arrays.asList(new SampleSetRule[] {
new SampleSetRule(cutoffMassInGrams, 3, 2),
new SampleSetRule(cutoffMassInGrams, 4, 3)
protected static final Set<SampleSetRule> rules = new HashSet<>(Arrays.asList(new SampleSetRule[] {
new SampleSetRule(Ranges.singleton(1), Ranges.atLeast(1)),
new SampleSetRule(Ranges.singleton(3), Ranges.atLeast(2)),
new SampleSetRule(Ranges.atLeast(4), Ranges.atLeast(3))
}));


protected final ResultSet in;
protected final LinkedList<TableRow> queuedRows;
protected final Column timeColumn;
Expand Down Expand Up @@ -139,12 +143,8 @@ public static TableRow averageRow(LinkedList<TableRow> groupedSampleSet, Column
int sampleSetSize = groupedSampleSet.size();
boolean isValid = false;
for (SampleSetRule rule : rules) {
if ((!rule.equals(rules.last()) && sampleSetSize >= rules.first().sampleSetSize
&& sampleSetSize <= rule.sampleSetSize
&& validSamples.size() >= rule.minValidSamples)
|| (rule.equals(rules.last()) && sampleSetSize >= rules.first().sampleSetSize
&& sampleSetSize >= rule.sampleSetSize
&& validSamples.size() >= rule.minValidSamples)) {
if (rule.sampleSetSize.contains(sampleSetSize)
&& rule.minValidSamples.contains(validSamples.size())) {
isValid = true;
}
}
Expand Down Expand Up @@ -334,30 +334,22 @@ public String getCursorName() throws SQLException {
return this.in.getCursorName();
}

public static final class SampleSetRule implements Comparable<SampleSetRule> {
public final BigDecimal cutoffMass;
public final int sampleSetSize;
public final int minValidSamples;

public SampleSetRule(BigDecimal cutoffMass, int sampleSetSize, int minValidSamples) {
this.cutoffMass = cutoffMass;
public static final class SampleSetRule {
public final Range<Integer> sampleSetSize;
public final Range<Integer> minValidSamples;

public SampleSetRule(Range<Integer> sampleSetSize, Range<Integer> minValidSamples) {
this.sampleSetSize = sampleSetSize;
this.minValidSamples = minValidSamples;
}

@Override
public int compareTo(SampleSetRule o) {
return Integer.compare(this.sampleSetSize, o.sampleSetSize);
}

@Override
public boolean equals(Object obj) {
if (obj == null) { return false; }
if (obj == this) { return true; }
if (obj instanceof SampleSetRule) {
SampleSetRule rhs = (SampleSetRule) obj;
return new EqualsBuilder()
.append(this.cutoffMass, rhs.cutoffMass)
.append(this.sampleSetSize, rhs.sampleSetSize)
.append(this.minValidSamples, rhs.minValidSamples)
.isEquals();
Expand All @@ -368,7 +360,6 @@ public boolean equals(Object obj) {
@Override
public int hashCode() {
return new HashCodeBuilder()
.append(this.cutoffMass)
.append(this.sampleSetSize)
.append(this.minValidSamples)
.toHashCode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,28 @@ public String transform(TableRow tr) {
try {
BigDecimal avgSizeValue = null;
if (null != tr.getValue(valueColumn)) {
avgSizeValue = new BigDecimal(tr.getValue(valueColumn));
}
BigDecimal conf95Value = null;
if (null != tr.getValue(conf95Column)) {
conf95Value = new BigDecimal(tr.getValue(conf95Column));
}
BigDecimal lowerConfValue = avgSizeValue.subtract(conf95Value, new MathContext(conf95Value.precision(), RoundingMode.HALF_EVEN));
String lowerConfResult = null;
if (null != lowerConfValue) {
lowerConfResult = lowerConfValue.toPlainString();
}

BigDecimal upperConfValue = avgSizeValue.add(conf95Value, new MathContext(conf95Value.precision(), RoundingMode.HALF_EVEN));
String upperConfResult = null;
if (null != upperConfValue) {
upperConfResult = upperConfValue.toPlainString();
avgSizeValue = new BigDecimal(tr.getValue(valueColumn));
//Keep this around in case there's no error bars
result = avgSizeValue.toPlainString();

BigDecimal conf95Value = null;
if (null != tr.getValue(conf95Column)) {
conf95Value = new BigDecimal(tr.getValue(conf95Column));
BigDecimal lowerConfValue = avgSizeValue.subtract(conf95Value, new MathContext(conf95Value.precision(), RoundingMode.HALF_EVEN));
String lowerConfResult = null;
if (null != lowerConfValue) {
lowerConfResult = lowerConfValue.toPlainString();
}

BigDecimal upperConfValue = avgSizeValue.add(conf95Value, new MathContext(conf95Value.precision(), RoundingMode.HALF_EVEN));
String upperConfResult = null;
if (null != upperConfValue) {
upperConfResult = upperConfValue.toPlainString();
}

result = lowerConfResult + DELIMITER + avgSizeValue.toPlainString() + DELIMITER + upperConfResult;
}
}

result = lowerConfResult + DELIMITER + avgSizeValue.toPlainString() + DELIMITER + upperConfResult;
} catch (Exception e) {
log.trace("could not calculate upper and lower 95% limits");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ public void testBugReportTable() throws SQLException {
public static void setUpClass() throws Exception {
timeColumn = new SimpleColumn("time");
sampleSetColumn = new SimpleColumn("sampleset");
valueColumn = new SimpleColumn("param1");
sampleMassColumn = new SimpleColumn("param2");
errorColumn = new SimpleColumn("param3");
conf95Column = new SimpleColumn("param4");
valueColumn = new SimpleColumn("value");
sampleMassColumn = new SimpleColumn("sampleMass");
errorColumn = new SimpleColumn("error");
conf95Column = new SimpleColumn("conf95");

incomingSampleJiraColGroup = new ColumnGrouping(Arrays.asList(new Column[] {
timeColumn,
Expand All @@ -92,6 +92,7 @@ public static void setUpClass() throws Exception {
new String[] {"1081","2","60","30"},
new String[] {"1092","3","30","50"},
new String[] {"1103","4","40","40"},
new String[] {"1104","4","44","42"},
new String[] {"1114","5","50","20"}
});
expectedSampleJiraColGroup = new ColumnGrouping(Arrays.asList(new Column[] {
Expand All @@ -105,6 +106,8 @@ public static void setUpClass() throws Exception {
expectedSampleJiraDataset = ResultSetUtils.createTableRows(expectedSampleJiraColGroup, new String[][] {
new String[] {"1020","1","80","50","6", "11.7"},
new String[] {"1061","2","40","54","7", "13.6"},
new String[] {"1092","3","30","50",null, null},
new String[] {"1114","5","50","20",null, null}
});

DateTimeFormatter dtf = ISODateTimeFormat.dateTimeNoMillis();
Expand Down Expand Up @@ -146,7 +149,8 @@ public static void setUpClass() throws Exception {
new String[] {"09404200","" + dtf.parseDateTime("2001-09-13T14:00:00-07:00").getMillis(),"9","398.00","0.157"},
new String[] {"09404200","" + dtf.parseDateTime("2001-10-10T12:50:00-07:00").getMillis(),"10","432.09","0.362"},
new String[] {"09404200","" + dtf.parseDateTime("2001-10-10T13:00:00-07:00").getMillis(),"10","434.86","0.364"},
new String[] {"09404200","" + dtf.parseDateTime("2001-10-10T13:05:00-07:00").getMillis(),"10","425.40","0.394"}
new String[] {"09404200","" + dtf.parseDateTime("2001-10-10T13:05:00-07:00").getMillis(),"10","425.40","0.394"},
new String[] {"09404200","" + dtf.parseDateTime("2001-10-10T14:05:00-07:00").getMillis(),"11","425.40","0.394"}
});

expectedRealWorldColGroup = new ColumnGrouping(timeColumn, Arrays.asList(new Column[] {
Expand All @@ -163,11 +167,11 @@ public static void setUpClass() throws Exception {
new String[] {"" + dtf.parseDateTime("1997-11-06T11:30:00-07:00").getMillis(),"3","0.370","309.4","0.0234","0.0456"},
new String[] {"" + dtf.parseDateTime("1997-11-07T10:00:00-07:00").getMillis(),"4","0.349","410.9","0.0265","0.0517"},
new String[] {"" + dtf.parseDateTime("2001-07-23T17:10:00-07:00").getMillis(),"5","0.400","412.30","0.0121","0.0236"},
// new String[] {"" + dtf.parseDateTime("2001-08-08T13:21:00-07:00").getMillis(),"6","0.335","398.83","0.0160","0.0312"},
new String[] {"" + dtf.parseDateTime("2001-08-15T14:35:00-07:00").getMillis(),"7","0.298","449.40","0.0150","0.0292"},
new String[] {"" + dtf.parseDateTime("2001-09-04T15:30:00-07:00").getMillis(),"8","0.342","407.35","0.0086","0.0168"},
new String[] {"" + dtf.parseDateTime("2001-09-13T13:45:00-07:00").getMillis(),"9","0.282","406.73","0.068","0.133"},
new String[] {"" + dtf.parseDateTime("2001-10-10T12:58:20-07:00").getMillis(),"10","0.373","430.78","0.0105","0.0205"}
new String[] {"" + dtf.parseDateTime("2001-10-10T12:58:20-07:00").getMillis(),"10","0.373","430.78","0.0105","0.0205"},
new String[] {"" + dtf.parseDateTime("2001-10-10T14:05:00-07:00").getMillis(),"11","0.394","425.40",null,null}
});

incomingBugReportColGroup = new ColumnGrouping(timeColumn, Arrays.asList(new Column[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,28 @@ public static void setUpClass() throws Exception {

inputSampleDataset = ResultSetUtils.createTableRows(sampleColGroup, new String[][] {
new String[] {"1020","1","80","50","6","11.7"},
new String[] {"1061","2","40","54","7","13.6"},
new String[] {"1062","3",null,"54","7","13.6"},
new String[] {"1063","4","40","54","7",null},
new String[] {"1064","5",null,"54","7",null},
new String[] {"1071","6","10","64","10","10.1"}
new String[] {"1062","2","40","54","7","13.6"},
new String[] {"1063","3",null,"54","7","13.6"},
new String[] {"1064","4","40","54","7",null},
new String[] {"1065","5",null,"54","7",null},
new String[] {"1066","6","40","54",null,null},
new String[] {"1067","7",null,"54",null,null},
new String[] {"1068","8","40","54",null,"13.6"},
new String[] {"1069","9",null,"54",null,"13.6"},
new String[] {"1071","10","10","64","10","10.1"}
});

expectedSampleDataset = ResultSetUtils.createTableRows(sampleColGroup, new String[][] {
new String[] {"1020","1","68.3;80;91.7","50","6","11.7"},
new String[] {"1061","2","26.4;40;53.6","54","7","13.6"},
new String[] {"1062","3",null,"54","7","13.6"},
new String[] {"1063","4",null,"54","7",null},
new String[] {"1064","5",null,"54","7",null},
new String[] {"1071","6","-0.1;10;20.1","64","10","10.1"}
new String[] {"1062","2","26.4;40;53.6","54","7","13.6"},
new String[] {"1063","3",null,"54","7","13.6"},
new String[] {"1064","4","40","54","7",null},
new String[] {"1065","5",null,"54","7",null},
new String[] {"1066","6","40","54",null,null},
new String[] {"1067","7",null,"54",null,null},
new String[] {"1068","8","26.4;40;53.6","54",null,"13.6"},
new String[] {"1069","9",null,"54",null,"13.6"},
new String[] {"1071","10","-0.1;10;20.1","64","10","10.1"}
});

}
Expand Down

0 comments on commit e84f3b7

Please sign in to comment.