Author: chatellier Date: 2010-11-09 13:06:36 +0000 (Tue, 09 Nov 2010) New Revision: 193 Log: Add sampling effort and occurence algorithms Modified: trunk/coser-business/pom.xml trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java trunk/coser-business/src/test/java/fr/ifremer/coser/services/ProjectServiceTest.java trunk/pom.xml Modified: trunk/coser-business/pom.xml =================================================================== --- trunk/coser-business/pom.xml 2010-11-08 16:35:46 UTC (rev 192) +++ trunk/coser-business/pom.xml 2010-11-09 13:06:36 UTC (rev 193) @@ -55,6 +55,10 @@ <artifactId>commons-lang</artifactId> </dependency> <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-math</artifactId> + </dependency> + <dependency> <groupId>net.sf.opencsv</groupId> <artifactId>opencsv</artifactId> </dependency> Modified: trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java =================================================================== --- trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java 2010-11-08 16:35:46 UTC (rev 192) +++ trunk/coser-business/src/main/java/fr/ifremer/coser/services/ProjectService.java 2010-11-09 13:06:36 UTC (rev 193) @@ -55,6 +55,9 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.commons.math.util.MathUtils; +import org.nuiton.math.matrix.MatrixFactory; +import org.nuiton.math.matrix.MatrixND; import fr.ifremer.coser.CoserBusinessConfig; import fr.ifremer.coser.CoserBusinessException; @@ -1263,9 +1266,58 @@ * * @param project * @param selection + * @return sampling effort as matrix */ - public void getSamplingEffort(Project project, Selection selection) { + public MatrixND getSamplingEffort(Project project, Selection selection) { + + // map strate > "year,integer" + Map<String, Map<String, Double>> dynMatrix = new HashMap<String, Map<String,Double>>(); + Set<String> lineNames = new HashSet<String>(); + Set<String> columns = new HashSet<String>(); + Iterator<String[]> itData = selection.getHaul().iterator(); + itData.next(); // skip header + + while (itData.hasNext()) { + String[] tuple = itData.next(); + + String year = tuple[Haul.INDEX_YEAR]; + String strata = tuple[Haul.INDEX_STRATUM]; + lineNames.add(strata); + columns.add(year); + + Map<String, Double> haulCountForStrata = dynMatrix.get(strata); + if (haulCountForStrata == null) { + haulCountForStrata = new HashMap<String, Double>(); + haulCountForStrata.put(year, 1.0); + dynMatrix.put(strata, haulCountForStrata); + } + else { + Double haulCountForYear = haulCountForStrata.get(year); + if (haulCountForYear == null) { + haulCountForStrata.put(year, 1.0); + } + else { + haulCountForYear++; + haulCountForStrata.put(year, haulCountForYear); + } + } + } + + List<String> lineNames2 = new ArrayList<String>(lineNames); + Collections.sort(lineNames2); + List<String> columnsNames2 = new ArrayList<String>(columns); + Collections.sort(columnsNames2); + MatrixND matrix = MatrixFactory.getInstance().create("sampling effort", new List<?>[] { + lineNames2 , columnsNames2}); + + for (Map.Entry<String, Map<String, Double>> dynMatrixEntry : dynMatrix.entrySet()) { + for (Map.Entry<String, Double> haulCountForYearEntry : dynMatrixEntry.getValue().entrySet()) { + matrix.setValue(dynMatrixEntry.getKey(), haulCountForYearEntry.getKey(), haulCountForYearEntry.getValue()); + } + } + + return matrix; } /** @@ -1294,9 +1346,80 @@ * * @param project * @param selection + * @return occurence as matrix */ - public void getOccurence(Project project, Selection selection) { + public MatrixND getOccurence(Project project, Selection selection) { + + // map species > "year, hauls" + Map<String, Map<String, Set<String>>> dynMatrix = new HashMap<String, Map<String, Set<String>>>(); + Set<String> lineNames = new HashSet<String>(); + Set<String> columns = new HashSet<String>(); + Map<String, Set<String>> traitsCountPerYear = new HashMap<String, Set<String>>(); + + Iterator<String[]> itData = selection.getCatch().iterator(); + itData.next(); // skip header + + while (itData.hasNext()) { + String[] tuple = itData.next(); + + String year = tuple[Catch.INDEX_YEAR]; + String haul = tuple[Catch.INDEX_HAUL]; + String species = tuple[Catch.INDEX_SPECIES]; + lineNames.add(species); + columns.add(year); + + // count all traits + Set<String> traitCountPerYear = traitsCountPerYear.get(year); + if (traitCountPerYear == null) { + Set<String> hauls = new HashSet<String>(); + hauls.add(haul); + traitsCountPerYear.put(year, hauls); + } + else { + traitCountPerYear.add(haul); + } + + // count for species and year + Map<String, Set<String>> haulCountForStrata = dynMatrix.get(species); + if (haulCountForStrata == null) { + haulCountForStrata = new HashMap<String, Set<String>>(); + Set<String> hauls = new HashSet<String>(); + hauls.add(haul); + haulCountForStrata.put(year, hauls); + dynMatrix.put(species, haulCountForStrata); + } + else { + Set<String> traitCountPerYear2 = haulCountForStrata.get(year); + if (traitCountPerYear2 == null) { + Set<String> hauls = new HashSet<String>(); + hauls.add(haul); + haulCountForStrata.put(year, hauls); + } + else { + traitCountPerYear2.add(haul); + } + } + } + + List<String> lineNames2 = new ArrayList<String>(lineNames); + Collections.sort(lineNames2); + List<String> columnsNames2 = new ArrayList<String>(columns); + Collections.sort(columnsNames2); + MatrixND matrix = MatrixFactory.getInstance().create("occurence", new List<?>[] { + lineNames2 , columnsNames2}); + for (Map.Entry<String, Map<String, Set<String>>> dynMatrixEntry : dynMatrix.entrySet()) { + for (Map.Entry<String, Set<String>> haulCountForYearEntry : dynMatrixEntry.getValue().entrySet()) { + double occurence = (double)haulCountForYearEntry.getValue().size() / + (double) traitsCountPerYear.get(haulCountForYearEntry.getKey()).size(); + occurence = MathUtils.round(occurence * 100.0, 2); + + matrix.setValue(dynMatrixEntry.getKey(), haulCountForYearEntry.getKey(), occurence); + } + } + + return matrix; + } /** @@ -1334,8 +1457,9 @@ * * @param project project * @param selection selection + * @return density as matrix */ - public void getDensity(Project project, Selection selection) { - + public MatrixND getDensity(Project project, Selection selection) { + return null; } } Modified: trunk/coser-business/src/test/java/fr/ifremer/coser/services/ProjectServiceTest.java =================================================================== --- trunk/coser-business/src/test/java/fr/ifremer/coser/services/ProjectServiceTest.java 2010-11-08 16:35:46 UTC (rev 192) +++ trunk/coser-business/src/test/java/fr/ifremer/coser/services/ProjectServiceTest.java 2010-11-09 13:06:36 UTC (rev 193) @@ -35,6 +35,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.nuiton.math.matrix.MatrixND; import fr.ifremer.coser.CoserBusinessException; import fr.ifremer.coser.CoserConstants; @@ -298,4 +299,32 @@ commandService.undoAction(project, project.getControl()); Assert.assertEquals(30, project.getControl().getLength().size()); } + + /** + * Test de la method sampling effort. + * + * @throws CoserBusinessException + */ + @Test + public void testSamplingEffort() throws CoserBusinessException { + Project project = createTestProject(projectService); + Selection selection = projectService.initProjectSelection(project); + selection.setName("test"); + MatrixND matrix = projectService.getSamplingEffort(project, selection); + Assert.assertNotNull(matrix); + } + + /** + * Test de la méthode occurence. + * + * @throws CoserBusinessException + */ + @Test + public void getOccurence() throws CoserBusinessException { + Project project = createTestProject(projectService); + Selection selection = projectService.initProjectSelection(project); + selection.setName("test"); + MatrixND matrix = projectService.getOccurence(project, selection); + Assert.assertNotNull(matrix); + } } Modified: trunk/pom.xml =================================================================== --- trunk/pom.xml 2010-11-08 16:35:46 UTC (rev 192) +++ trunk/pom.xml 2010-11-09 13:06:36 UTC (rev 193) @@ -53,6 +53,13 @@ </dependency> <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-math</artifactId> + <version>2.1</version> + <scope>compile</scope> + </dependency> + + <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.3</version>