1
package org.hisp.dhis.dataanalysis;
4
* Copyright (c) 2004-2007, University of Oslo
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* * Redistributions of source code must retain the above copyright notice, this
10
* list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
* * Neither the name of the HISP project nor the names of its contributors may
15
* be used to endorse or promote products derived from this software without
16
* specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
import java.util.ArrayList;
31
import java.util.Collection;
32
import java.util.HashSet;
35
import org.hisp.dhis.DhisTest;
36
import org.hisp.dhis.dataelement.DataElement;
37
import org.hisp.dhis.dataelement.DataElementCategoryCombo;
38
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
39
import org.hisp.dhis.dataelement.DataElementCategoryService;
40
import org.hisp.dhis.dataelement.DataElementService;
41
import org.hisp.dhis.dataset.DataSetService;
42
import org.hisp.dhis.datavalue.DataValue;
43
import org.hisp.dhis.datavalue.DataValueService;
44
import org.hisp.dhis.minmax.MinMaxDataElement;
45
import org.hisp.dhis.minmax.MinMaxDataElementService;
46
import org.hisp.dhis.organisationunit.OrganisationUnit;
47
import org.hisp.dhis.organisationunit.OrganisationUnitService;
48
import org.hisp.dhis.period.MonthlyPeriodType;
49
import org.hisp.dhis.period.Period;
50
import org.hisp.dhis.period.PeriodService;
51
import org.junit.Test;
55
* @version $Id: MinMaxOutlierAnalysisServiceTest.java 883 2009-05-15 00:42:45Z daghf $
57
@SuppressWarnings( "unused" )
58
public class MinMaxOutlierAnalysisServiceTest
61
private DataAnalysisService minMaxOutlierAnalysisService;
63
private MinMaxDataElementService minMaxDataElementService;
65
private DataElement dataElementA;
66
private DataElement dataElementB;
67
private DataElement dataElementC;
68
private DataElement dataElementD;
70
private DataValue dataValueA;
71
private DataValue dataValueB;
73
private Set<DataElement> dataElementsA = new HashSet<DataElement>();
74
private Set<DataElement> dataElementsB = new HashSet<DataElement>();
75
private Set<DataElement> dataElementsC = new HashSet<DataElement>();
77
private DataElementCategoryCombo categoryCombo;
79
private DataElementCategoryOptionCombo categoryOptionCombo;
81
private Period periodA;
82
private Period periodB;
83
private Period periodC;
84
private Period periodD;
85
private Period periodE;
86
private Period periodF;
87
private Period periodG;
88
private Period periodH;
89
private Period periodI;
90
private Period periodJ;
92
private OrganisationUnit organisationUnitA;
94
private MinMaxDataElement minMaxDataElement;
96
// ----------------------------------------------------------------------
98
// ----------------------------------------------------------------------
101
public void setUpTest()
104
minMaxOutlierAnalysisService = (DataAnalysisService) getBean( "org.hisp.dhis.dataanalysis.MinMaxOutlierAnalysisService" );
106
dataElementService = (DataElementService) getBean( DataElementService.ID );
108
minMaxDataElementService = (MinMaxDataElementService) getBean( MinMaxDataElementService.ID );
110
categoryService = (DataElementCategoryService) getBean( DataElementCategoryService.ID );
112
dataSetService = (DataSetService) getBean( DataSetService.ID );
114
organisationUnitService = (OrganisationUnitService) getBean( OrganisationUnitService.ID );
116
dataValueService = (DataValueService) getBean( DataValueService.ID );
118
periodService = (PeriodService) getBean( PeriodService.ID );
120
categoryCombo = categoryService.getDataElementCategoryComboByName( DataElementCategoryCombo.DEFAULT_CATEGORY_COMBO_NAME );
122
dataElementA = createDataElement( 'A', categoryCombo );
123
dataElementB = createDataElement( 'B', categoryCombo );
124
dataElementC = createDataElement( 'C', categoryCombo );
125
dataElementD = createDataElement( 'D', categoryCombo );
127
dataElementService.addDataElement( dataElementA );
128
dataElementService.addDataElement( dataElementB );
129
dataElementService.addDataElement( dataElementC );
130
dataElementService.addDataElement( dataElementD );
132
dataElementsA.add( dataElementA );
133
dataElementsA.add( dataElementB );
134
dataElementsB.add( dataElementC );
135
dataElementsB.add( dataElementD );
136
dataElementsC.add( dataElementB );
138
categoryOptionCombo = categoryCombo.getOptionCombos().iterator().next();
140
periodA = createPeriod( new MonthlyPeriodType(), getDate( 2000, 3, 1 ), getDate( 2000, 3, 31 ) );
141
periodB = createPeriod( new MonthlyPeriodType(), getDate( 2000, 4, 1 ), getDate( 2000, 4, 30 ) );
142
periodC = createPeriod( new MonthlyPeriodType(), getDate( 2000, 5, 1 ), getDate( 2000, 5, 30 ) );
143
periodD = createPeriod( new MonthlyPeriodType(), getDate( 2000, 6, 1 ), getDate( 2000, 6, 30 ) );
144
periodE = createPeriod( new MonthlyPeriodType(), getDate( 2000, 7, 1 ), getDate( 2000, 7, 30 ) );
145
periodF = createPeriod( new MonthlyPeriodType(), getDate( 2000, 8, 1 ), getDate( 2000, 8, 30 ) );
146
periodG = createPeriod( new MonthlyPeriodType(), getDate( 2000, 9, 1 ), getDate( 2000, 9, 30 ) );
147
periodH = createPeriod( new MonthlyPeriodType(), getDate( 2000, 10, 1 ), getDate( 2000, 10, 30 ) );
148
periodI = createPeriod( new MonthlyPeriodType(), getDate( 2000, 11, 1 ), getDate( 2000, 11, 30 ) );
149
periodJ = createPeriod( new MonthlyPeriodType(), getDate( 2000, 12, 1 ), getDate( 2000, 12, 30 ) );
151
organisationUnitA = createOrganisationUnit( 'A' );
153
organisationUnitService.addOrganisationUnit( organisationUnitA );
157
public boolean emptyDatabaseAfterTest()
162
// ----------------------------------------------------------------------
163
// Business logic tests
164
// ----------------------------------------------------------------------
167
public void testGetFindOutliers()
169
// testvalues = [5, 5, -5, -5, 10, -10, 13, -13, 41, -41]
170
// mean(testvalues) = 0.0
171
// std(testvalues) = 20.0
173
dataValueA = createDataValue( dataElementA, periodI, organisationUnitA, "41", categoryOptionCombo );
174
dataValueB = createDataValue( dataElementA, periodJ, organisationUnitA, "-41", categoryOptionCombo );
176
dataValueService.addDataValue( createDataValue( dataElementA, periodA, organisationUnitA, "5", categoryOptionCombo ) );
177
dataValueService.addDataValue( createDataValue( dataElementA, periodB, organisationUnitA, "-5", categoryOptionCombo ) );
178
dataValueService.addDataValue( createDataValue( dataElementA, periodC, organisationUnitA, "5", categoryOptionCombo ) );
179
dataValueService.addDataValue( createDataValue( dataElementA, periodD, organisationUnitA, "-5", categoryOptionCombo ) );
180
dataValueService.addDataValue( createDataValue( dataElementA, periodE, organisationUnitA, "10", categoryOptionCombo ) );
181
dataValueService.addDataValue( createDataValue( dataElementA, periodF, organisationUnitA, "-10", categoryOptionCombo ) );
182
dataValueService.addDataValue( createDataValue( dataElementA, periodG, organisationUnitA, "13", categoryOptionCombo ) );
183
dataValueService.addDataValue( createDataValue( dataElementA, periodH, organisationUnitA, "-13", categoryOptionCombo ) );
185
dataValueService.addDataValue( dataValueA );
186
dataValueService.addDataValue( dataValueB );
188
minMaxDataElement = new MinMaxDataElement( organisationUnitA, dataElementA, categoryOptionCombo, -40, 40, false );
189
minMaxDataElementService.addMinMaxDataElement( minMaxDataElement );
191
Collection<Period> periods = new ArrayList<Period>();
192
periods.add( periodI );
193
periods.add( periodJ );
194
periods.add( periodA );
195
periods.add( periodE );
197
//Collection<DeflatedDataValue> result = minMaxOutlierAnalysisService.findOutliers( organisationUnitA, dataElementsA, periods, null );
199
//assertEquals( 2, result.size() );