use of org.hisp.dhis.expression.Expression in project dhis2-core by dhis2.
the class PredictorServiceTest method testGetPredictionsSeasonalWithOutbreak.
@Test
@Category(IntegrationTest.class)
public void testGetPredictionsSeasonalWithOutbreak() {
setupTestData();
String auid = dataElementA.getUid();
Predictor p = createPredictor(dataElementX, altCombo, "GetPredictionsSeasonalWithOutbreak", new Expression("AVG(#{" + auid + "})+1.5*STDDEV(#{" + auid + "})", "descriptionA"), new Expression("#{" + dataElementB.getUid() + "}", "outbreak"), periodTypeMonthly, orgUnitLevel1, 3, 1, 2);
assertEquals(99, predictorService.predict(p, monthStart(2001, 7), monthStart(2005, 12)));
assertEquals(new Double(5.0), getDataValue(dataElementX, altCombo, sourceA, makeMonth(2001, 8)));
assertEquals(new Double(5.5), getDataValue(dataElementX, altCombo, sourceA, makeMonth(2001, 9)));
assertEquals(new Double(7.0), getDataValue(dataElementX, altCombo, sourceA, makeMonth(2003, 1)));
assertEquals(new Double(8.75), getDataValue(dataElementX, altCombo, sourceA, makeMonth(2003, 3)));
assertEquals(new Double(10.088860517433634), getDataValue(dataElementX, altCombo, sourceA, makeMonth(2004, 7)));
assertEquals(new Double(10.095418256997062), getDataValue(dataElementX, altCombo, sourceA, makeMonth(2004, 8)));
// This value is derived from organisation units beneath the actual *sourceB*.
assertEquals(new Double(15.754231583479712), getDataValue(dataElementX, altCombo, sourceB, makeMonth(2004, 7)));
}
use of org.hisp.dhis.expression.Expression in project dhis2-core by dhis2.
the class PredictorServiceTest method testGetPredictorCount.
@Test
public void testGetPredictorCount() {
Set<DataElement> dataElementsA = new HashSet<>();
dataElementsA.add(dataElementA);
dataElementsA.add(dataElementB);
Set<DataElement> dataElementsB = new HashSet<>();
dataElementsB.add(dataElementC);
dataElementsB.add(dataElementD);
Set<DataElement> dataElementsD = new HashSet<>();
dataElementsD.addAll(dataElementsA);
dataElementsD.addAll(dataElementsB);
Expression expression1 = new Expression("Expression1", "Expression1");
Expression expression2 = new Expression("Expression2", "Expression2");
Expression expression3 = new Expression("Expression3", "Expression3");
expressionService.addExpression(expression1);
expressionService.addExpression(expression2);
expressionService.addExpression(expression3);
Predictor predictorA = createPredictor(dataElementX, altCombo, "A", expressionA, expressionB, periodTypeMonthly, orgUnitLevel1, 6, 1, 0);
Predictor predictorB = createPredictor(dataElementX, defaultCombo, "B", expressionA, expressionB, periodTypeMonthly, orgUnitLevel1, 6, 1, 0);
Predictor predictorC = createPredictor(dataElementX, altCombo, "C", expressionA, expressionB, periodTypeMonthly, orgUnitLevel1, 6, 1, 0);
predictorService.addPredictor(predictorA);
predictorService.addPredictor(predictorB);
predictorService.addPredictor(predictorC);
assertNotNull(predictorService.getPredictorCount());
assertEquals(3, predictorService.getPredictorCount());
}
use of org.hisp.dhis.expression.Expression in project dhis2-core by dhis2.
the class PredictorStoreTest method setUpTest.
// -------------------------------------------------------------------------
// Fixture
// -------------------------------------------------------------------------
@Override
public void setUpTest() throws Exception {
orgUnitLevel1 = new OrganisationUnitLevel(1, "Level1");
organisationUnitService.addOrganisationUnitLevel(orgUnitLevel1);
dataElementA = createDataElement('A');
dataElementB = createDataElement('B');
dataElementC = createDataElement('C');
dataElementD = createDataElement('D');
dataElementX = createDataElement('X');
dataElementService.addDataElement(dataElementA);
dataElementService.addDataElement(dataElementB);
dataElementService.addDataElement(dataElementC);
dataElementService.addDataElement(dataElementD);
dataElementService.addDataElement(dataElementX);
DataElementCategoryOptionCombo categoryOptionCombo = categoryService.getDefaultDataElementCategoryOptionCombo();
defaultCombo = categoryOptionCombo;
optionCombos = new HashSet<>();
optionCombos.add(categoryOptionCombo);
expressionA = new Expression("expressionA", "descriptionA");
expressionB = new Expression("expressionB", "descriptionB");
expressionService.addExpression(expressionB);
expressionService.addExpression(expressionA);
periodType = PeriodType.getAvailablePeriodTypes().iterator().next();
}
use of org.hisp.dhis.expression.Expression in project dhis2-core by dhis2.
the class DhisConvenienceTest method createExpression2.
/**
* @param uniqueCharacter A unique character to identify the object.
* @param expressionString The expression string.
* @param dataElementsInExpression A collection of the data elements
* entering into the expression.
*/
public static Expression createExpression2(char uniqueCharacter, String expressionString) {
Expression expression = new Expression();
expression.setExpression(expressionString);
expression.setDescription("Description" + uniqueCharacter);
return expression;
}
use of org.hisp.dhis.expression.Expression in project dhis2-core by dhis2.
the class DefaultPredictorService method predict.
// -------------------------------------------------------------------------
// Predictor run
// -------------------------------------------------------------------------
@Override
public int predict(Predictor predictor, Date startDate, Date endDate) {
log.info("Predicting for " + predictor.getName() + " from " + startDate.toString() + " to " + endDate.toString());
Expression generator = predictor.getGenerator();
Expression skipTest = predictor.getSampleSkipTest();
DataElement outputDataElement = predictor.getOutput();
Set<String> aggregates = expressionService.getAggregatesInExpression(generator.getExpression());
Map<String, Double> constantMap = constantService.getConstantMap();
List<Period> outputPeriods = getPeriodsBetweenDates(predictor.getPeriodType(), startDate, endDate);
ListMap<Period, Period> samplePeriodsMap = getSamplePeriodsMap(outputPeriods, predictor);
Set<Period> allSamplePeriods = samplePeriodsMap.uniqueValues();
Set<DataElementOperand> dataElementOperands = getDataElementOperands(aggregates, skipTest);
User currentUser = currentUserService.getCurrentUser();
DataElementCategoryOptionCombo outputOptionCombo = predictor.getOutputCombo() == null ? categoryService.getDefaultDataElementCategoryOptionCombo() : predictor.getOutputCombo();
List<OrganisationUnit> orgUnits = organisationUnitService.getOrganisationUnitsAtOrgUnitLevels(predictor.getOrganisationUnitLevels(), currentUser.getOrganisationUnits());
int predictionCount = 0;
for (OrganisationUnit orgUnit : orgUnits) {
MapMapMap<Period, String, DimensionalItemObject, Double> dataMap = dataElementOperands.isEmpty() ? null : dataValueService.getDataElementOperandValues(dataElementOperands, allSamplePeriods, orgUnit);
applySkipTest(dataMap, skipTest, constantMap);
for (Period period : outputPeriods) {
ListMapMap<String, String, Double> aggregateSampleMap = getAggregateSamples(dataMap, aggregates, samplePeriodsMap.get(period), constantMap);
for (String aoc : aggregateSampleMap.keySet()) {
ListMap<String, Double> aggregateValueMap = aggregateSampleMap.get(aoc);
Double value = expressionService.getExpressionValue(generator, new HashMap<>(), constantMap, null, period.getDaysInPeriod(), aggregateValueMap);
if (value != null && !value.isNaN() && !value.isInfinite()) {
writeDataValue(outputDataElement, period, orgUnit, outputOptionCombo, categoryService.getDataElementCategoryOptionCombo(aoc), value.toString(), currentUser.getUsername());
predictionCount++;
}
}
}
}
log.info("Generated " + predictionCount + " predictions for " + predictor.getName() + " from " + startDate.toString() + " to " + endDate.toString());
return predictionCount;
}
Aggregations