Search in sources :

Example 1 with Expression

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)));
}
Also used : Expression(org.hisp.dhis.expression.Expression) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) Category(org.junit.experimental.categories.Category) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisTest(org.hisp.dhis.DhisTest)

Example 2 with Expression

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());
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) Expression(org.hisp.dhis.expression.Expression) HashSet(java.util.HashSet) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisTest(org.hisp.dhis.DhisTest)

Example 3 with Expression

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();
}
Also used : Expression(org.hisp.dhis.expression.Expression) OrganisationUnitLevel(org.hisp.dhis.organisationunit.OrganisationUnitLevel) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Example 4 with Expression

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;
}
Also used : Expression(org.hisp.dhis.expression.Expression)

Example 5 with 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;
}
Also used : DataElementOperand(org.hisp.dhis.dataelement.DataElementOperand) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) User(org.hisp.dhis.user.User) Period(org.hisp.dhis.period.Period) DataElement(org.hisp.dhis.dataelement.DataElement) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Expression(org.hisp.dhis.expression.Expression) DataElementCategoryOptionCombo(org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)

Aggregations

Expression (org.hisp.dhis.expression.Expression)18 DataElementCategoryOptionCombo (org.hisp.dhis.dataelement.DataElementCategoryOptionCombo)5 DataElement (org.hisp.dhis.dataelement.DataElement)4 Test (org.junit.Test)4 HashSet (java.util.HashSet)3 DhisSpringTest (org.hisp.dhis.DhisSpringTest)2 DhisTest (org.hisp.dhis.DhisTest)2 IntegrationTest (org.hisp.dhis.IntegrationTest)2 MockCurrentUserService (org.hisp.dhis.mock.MockCurrentUserService)2 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)2 OrganisationUnitLevel (org.hisp.dhis.organisationunit.OrganisationUnitLevel)2 PeriodType (org.hisp.dhis.period.PeriodType)2 Predictor (org.hisp.dhis.predictor.Predictor)2 CurrentUserService (org.hisp.dhis.user.CurrentUserService)2 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)1 ArrayList (java.util.ArrayList)1 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)1 DataElementCategory (org.hisp.dhis.dataelement.DataElementCategory)1 DataElementCategoryOption (org.hisp.dhis.dataelement.DataElementCategoryOption)1 DataElementOperand (org.hisp.dhis.dataelement.DataElementOperand)1