diff --git a/src/main/java/analysis/ExportSpeciesRange.java b/src/main/java/analysis/ExportSpeciesRange.java index f9ca25e32ea39e137681c74efa449fa160c21122..5f4bcfa1d98de8976bb894d1031bcc0bfb2d7fa9 100644 --- a/src/main/java/analysis/ExportSpeciesRange.java +++ b/src/main/java/analysis/ExportSpeciesRange.java @@ -27,74 +27,135 @@ import java.io.IOException; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; +import environment.RiverBasin; import environment.Time; import fr.cemagref.simaqualife.kernel.processes.AquaNismsGroupProcess; import fr.cemagref.simaqualife.pilot.Pilot; +import miscellaneous.TreeMapForCentile; import species.DiadromousFish; import species.DiadromousFishGroup; /** * */ +@Deprecated public class ExportSpeciesRange extends AquaNismsGroupProcess<DiadromousFish, DiadromousFishGroup> { - private String fileNameOutput = "range"; + /** + * centile to calculate the nothernmost range of species distribution + * + * @unit + */ + public double centileForRange = 0.95; + private transient BufferedWriter bW; - private transient String sep=";"; + private transient String sep = ";"; public static void main(String[] args) { - System.out.println((new XStream(new DomDriver())) - .toXML(new ExportSpeciesRange())); + System.out.println((new XStream(new DomDriver())).toXML(new ExportSpeciesRange())); } - /* (non-Javadoc) - * @see fr.cemagref.simaqualife.kernel.processes.AquaNismsGroupProcess#initTransientParameters(fr.cemagref.simaqualife.pilot.Pilot) + + /* + * (non-Javadoc) + * + * @see + * fr.cemagref.simaqualife.kernel.processes.AquaNismsGroupProcess#initTransientParameters(fr.cemagref.simaqualife. + * pilot.Pilot) */ @Override public void initTransientParameters(Pilot pilot) { super.initTransientParameters(pilot); - sep=";"; + sep = ";"; } - /* (non-Javadoc) + + /* + * (non-Javadoc) + * * @see fr.cemagref.simaqualife.kernel.processes.Process#doProcess(java.lang.Object) */ @Override public void doProcess(DiadromousFishGroup group) { - - if (bW==null){ - if (fileNameOutput != null){ - new File(group.getOutputPath()+fileNameOutput).getParentFile().mkdirs(); + + if (bW == null) { + if (fileNameOutput != null) { + new File(group.getOutputPath() + fileNameOutput).getParentFile().mkdirs(); try { - bW = new BufferedWriter(new FileWriter(new File(group.getOutputPath()+ - fileNameOutput +group.getSimulationId()+ ".csv"))); + bW = new BufferedWriter( + new FileWriter(new File(group.getOutputPath() + fileNameOutput + group.getSimulationId() + ".csv"))); - bW.write("timestep"+sep+"year"+sep+"season"+sep+"medianRange" - +sep+"southRange"+sep+"northRange"+sep+"centileRange"+"\n"); + bW.write("timestep" + sep + "year" + sep + "season" + sep + "medianRange" + sep + "southRange" + sep + + "northRange" + sep + "centileRange" + "\n"); } catch (IOException e) { e.printStackTrace(); } } } - + try { Time time = group.getEnvironment().getTime(); - bW.write(group.getPilot().getCurrentTime()+sep+time.getYear(group.getPilot())); - bW.write(sep+time.getSeason(group.getPilot())); - - for (double value : group.getRangeDistributionWithLat()){ - bW.write(sep+value); + bW.write(group.getPilot().getCurrentTime() + sep + time.getYear(group.getPilot())); + bW.write(sep + time.getSeason(group.getPilot())); + + for (double value : getRangeSpawnerDistributionWithLat(group)) { + bW.write(sep + value); } bW.write("\n"); - - if (group.getPilot().getCurrentTime()== group.getPilot().getSimBegin()+group.getPilot().getSimDuration()-1){ + + if (group.getPilot().getCurrentTime() == group.getPilot().getSimBegin() + group.getPilot().getSimDuration() - 1) { bW.close(); } } catch (IOException e) { e.printStackTrace(); } } + + + private Double[] getRangeSpawnerDistributionWithLat(DiadromousFishGroup group) { + + TreeMapForCentile latitudeEffective = new TreeMapForCentile(); + double southernLatitude = 90.; + double northernLatitude = 0.; + + // loop over riverBasin + for (RiverBasin riverBasin : group.getEnvironment().getRiverBasins()) { + long effective = (long) (riverBasin.getLastFemaleSpawnerNumber() + riverBasin.getLastMaleSpawnerNumber()); + + if (effective > 0) { + latitudeEffective.putWithAdding(riverBasin.getLatitude(), effective); + if (riverBasin.getLatitude() < southernLatitude) + southernLatitude = riverBasin.getLatitude(); + if (riverBasin.getLatitude() > northernLatitude) + northernLatitude = riverBasin.getLatitude(); + } + } + + // compute result + // (y, y-low, y-high) data item + Double[] rangeDistribution = new Double[4]; + if (!latitudeEffective.isEmpty()) { + // median + rangeDistribution[0] = latitudeEffective.calculateMedian(); + // southern + rangeDistribution[1] = southernLatitude; + // northern + rangeDistribution[2] = northernLatitude; + // centile + rangeDistribution[3] = latitudeEffective.calculateCentile(centileForRange); + } else { + // median + rangeDistribution[0] = (southernLatitude + northernLatitude) / 2.; + // southern + rangeDistribution[1] = rangeDistribution[0]; + // northern + rangeDistribution[2] = rangeDistribution[0]; + // centile + rangeDistribution[3] = rangeDistribution[0]; + } + return rangeDistribution; + } } diff --git a/src/main/java/analysis/TypeTrajectoryCV.java b/src/main/java/analysis/TypeTrajectoryCV.java index c76340cf5746a44fea5df49a358e5aad980f0ee3..a90fb6ce02d0bb843a120a535815153ceb8f6929 100644 --- a/src/main/java/analysis/TypeTrajectoryCV.java +++ b/src/main/java/analysis/TypeTrajectoryCV.java @@ -1,11 +1,5 @@ package analysis; -import com.thoughtworks.xstream.XStream; -import com.thoughtworks.xstream.io.xml.DomDriver; -import fr.cemagref.simaqualife.kernel.processes.AquaNismsGroupProcess; -import species.DiadromousFish; -import species.DiadromousFishGroup; - import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; @@ -15,208 +9,355 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.Scanner; +import java.util.TreeMap; import java.util.logging.Level; import java.util.logging.Logger; import java.util.regex.Pattern; +import com.thoughtworks.xstream.XStream; +import com.thoughtworks.xstream.io.xml.DomDriver; + +import environment.RIOBasinNetwork; +import environment.RiverBasin; +import fr.cemagref.simaqualife.kernel.processes.AquaNismsGroupProcess; +import fr.cemagref.simaqualife.pilot.Pilot; +import species.DiadromousFish; +import species.DiadromousFish.Gender; +import species.DiadromousFishGroup; + public class TypeTrajectoryCV extends AquaNismsGroupProcess<DiadromousFish, DiadromousFishGroup> { - private String fileNameOutput = "summary"; - private transient BufferedWriter bW; - - public static void main(String[] args) { - System.out.println((new XStream(new DomDriver())).toXML(new TypeTrajectoryCV())); - - //TypeTrajectoryCV trajectoryCV = new TypeTrajectoryCV(); -// trajectoryCV.computeKapa("data/input/reality/Obs1900.csv"); - } - - @Override - public void doProcess(DiadromousFishGroup group) { - - // int[] finalStates = group.getEnvironment().getFinalStates(); - // int[] finalStatesWithStoch = group.getEnvironment().getFinalStatesWithStochasticity(); - int[] finalStatesForKappa = group.getEnvironment().getFinalStatesForKappa(); - double[] geoMeanRecOverProdCap = group.getEnvironment().getGeoMeansLastRecsOverProdCaps(); - double[] meanLastRec = group.getEnvironment().getMeanLastRecruitments(); - double[] meanPercOfAut = group.getEnvironment().getMeanLastPercOfAut(); - double[] probOfNonNulRecruitmentDuringLastYears = group.getEnvironment().getProbOfNonNulRecruitmentDuringLastYears(); - long[] yearsOfFirstNonNulRep = group.getEnvironment().getYearsOfFirstNonNulRep(); - long[] yearsOfLastNulRep = group.getEnvironment().getYearsOfLastNulRep(); - String[] finalStatesNames = group.getEnvironment().getRiverBasinNames(); - double[] finalProbabilityOfPresence = group.getEnvironment().getFinalProbabilityOfPresence(); - - // System.out.println(group.getEnvironment().getMeanLastRecruitmentsBV2()); - //System.out.println(" nom des bv " + Arrays.deepToString(finalStatesNames) + "final states : " + Arrays.toString(finalStates)); - //System.out.println(" nom des bv " + Arrays.deepToString(finalStatesNames) + "final states wirth stoch : " + Arrays.toString(finalStatesWithStoch)); - //System.out.println(" nom des bv " + Arrays.deepToString(finalStatesNames) + "geo mean of last 10 years R/alpha : " + Arrays.toString(geoMeanRecOverProdCap)); - //System.out.println(" nom des bv " + Arrays.deepToString(finalStatesNames) + "mean last recruitment : " + Arrays.toString(meanLastRec)); - //System.out.println(" nom des bv " + Arrays.deepToString(finalStatesNames)); - //System.out.println(" nom des bv " + Arrays.deepToString(finalStatesNames) + "prob non nul rec : " + Arrays.toString(probOfNonNulRecruitmentDuringLastYears)); - //System.out.println("mean last percentage of autochtone : " + Arrays.toString(meanPercOfAut)); - //System.out.println(" nom des bv " + Arrays.deepToString(finalStatesNames) + "year of first non nul reproduction : " + Arrays.toString(yearsOfFirstNonNulRep)); - //System.out.println(" nom des bv " + Arrays.deepToString(finalStatesNames) + "year of last nul reproduction : " + Arrays.toString(yearsOfLastNulRep)); - //this.fireChangesToObservers(); - if (fileNameOutput != null) { - // create the subdirectorrys if necessary ? - new File(group.getOutputPath()+fileNameOutput).getParentFile().mkdirs(); - - try { - bW = new BufferedWriter(new FileWriter(new File(group.getOutputPath()+ - fileNameOutput+group.getSimulationId()+ ".csv"))); - int nbBV = finalStatesNames.length; - - bW.write("nom des bv"); - for (int i = 0; i < nbBV; i++) { - bW.write(";" + finalStatesNames[i]); - } - bW.write("\n"); - - bW.write("Final states"); - for (int i = 0; i < nbBV; i++) { - bW.write(";" + finalStatesForKappa[i]); - } - bW.write("\n"); - - bW.write("year of first non nul reproduction"); - for (int i = 0; i < nbBV; i++) { - bW.write(";" + yearsOfFirstNonNulRep[i]); - } - bW.write("\n"); - - bW.write("year of last nul reproduction"); - for (int i = 0; i < nbBV; i++) { - bW.write(";" + yearsOfLastNulRep[i]); - } - bW.write("\n"); - - bW.write("mean last recruitment"); - for (int i = 0; i < nbBV; i++) { - bW.write(";" + meanLastRec[i]); - } - bW.write("\n"); - - bW.write("geo mean of last 10 years R/alpha"); - for (int i = 0; i < nbBV; i++) { - bW.write(";" + geoMeanRecOverProdCap[i]); - } - bW.write("\n"); - - bW.write("mean last percentage of autochtone"); - for (int i = 0; i < nbBV; i++) { - bW.write(";" + meanPercOfAut[i]); - } - bW.write("\n"); - - bW.write("prob of non nul recruitment during last years"); - for (int i = 0; i < nbBV; i++) { - bW.write(";" + probOfNonNulRecruitmentDuringLastYears[i]); - } - bW.write("\n"); - - computeKapa(bW, "data/input/reality/Obs1900.csv", finalStatesForKappa, finalStatesNames); - bW.write("\n"); - - bW.write("likelihood;" + ((Double) group.computeLikelihood()).toString() + "\n"); - - bW.write("spawnersMatureAgeSumStat;" + group.computeFemaleSpawnerForFirstTimeSummaryStatistic() + "\n"); - - bW.write("higherPopulatedLatitude;" + group.getHigherPopulatedLatitude() + "\n"); - - bW.write("graine;" + group.getPilot().getParameters().getRngStatusIndex() + "\n"); - - bW.write("finalProbOfPres"); - for (int i = 0; i < nbBV; i++) { - bW.write(";" + finalProbabilityOfPresence[i]); - } - bW.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - public void computeKapa(BufferedWriter writer, String fileNameInputForInitialObservation, int[] finalStatesForKappa, String[] finalStatesNames) throws IOException { - - // 1 : read input file of observation - FileReader reader; - Scanner scanner; - Map<String, Integer> obs1900 = new HashMap<String, Integer>(); - try { - reader = new FileReader(fileNameInputForInitialObservation); - // Parsing the file - scanner = new Scanner(reader); - scanner.useLocale(Locale.ENGLISH); // to have a comma as decimal separator !!! - scanner.useDelimiter(Pattern.compile("[;\r]")); - - scanner.nextLine(); // to skip the file first line of entete - - while (scanner.hasNext()) { - obs1900.put(scanner.next().replaceAll("\n", ""), scanner.nextInt()); - } - reader.close(); - - } catch (IOException ex) { - Logger.getLogger(TypeTrajectoryCV.class.getName()).log(Level.SEVERE, null, ex); - } - - // 2 : compute the confusion matrix between observed and computed data - // obs 0 1 - // pred 0 VN FN - // 1 FP VP - double[][] confusionMatrix = new double[2][2]; - int computedVal; - int obsVal; - int nbVal = 0; - for (int i = 0; i < finalStatesNames.length; i++) { - if (obs1900.containsKey(finalStatesNames[i])) { - computedVal = (finalStatesForKappa[i] > 0) ? 1 : 0; - obsVal = obs1900.get(finalStatesNames[i]); - if (computedVal == 0) { - if (obsVal == 0) { - confusionMatrix[0][0]++; - } else { - confusionMatrix[0][1]++; - } - } else { - if (obsVal == 0) { - confusionMatrix[1][0]++; - } else { - confusionMatrix[1][1]++; - } - } - nbVal++; - } - } - // normalisation - confusionMatrix[0][0] = confusionMatrix[0][0] / nbVal; - confusionMatrix[0][1] = confusionMatrix[0][1] / nbVal; - confusionMatrix[1][0] = confusionMatrix[1][0] / nbVal; - confusionMatrix[1][1] = confusionMatrix[1][1] / nbVal; - - // 3 compute kapa - // kapa= (pra-pre)/(1-pre) - // with pra=VN+VP and pre=(VN+FN)*(VN+FP)+(FN+VP)*(FP+VP) - double VN = confusionMatrix[0][0]; - double FN = confusionMatrix[0][1]; - double FP = confusionMatrix[1][0]; - double VP = confusionMatrix[1][1]; - double pra = VN + VP; - double pre = (VN + FN) * (VN + FP) + (FN + VP) * (FP + VP); - double kapa = (pra - pre) / (1 - pre); - - writer.write("confusion matrix(VN,FN,FP,VP);"); - writer.write(((Double) confusionMatrix[0][0]).toString()); - writer.write(";"); - writer.write(((Double) confusionMatrix[0][1]).toString()); - writer.write(";"); - writer.write(((Double) confusionMatrix[1][0]).toString()); - writer.write(";"); - writer.write(((Double) confusionMatrix[1][1]).toString()); - writer.write("\nkappa;"); - writer.write(((Double) kapa).toString()); - - //System.out.println("kappa value: " + kapa); - } + private String fileNameOutput = "summary"; + + private String presenceFileName = "data/input/northeastamerica/nea_presence.csv"; + private String period = "obs_1751_1850"; + + private Double targetedAgeForFemaleSpawnerForFirstTime = 5.5; + private Double targetedAgeForMaleSpawnerForFirstTime = 4.5; + + /** + * The minimum number of recruits to consider a basin as populated + * + * @unit: # + */ + private int minimumRecruitsForPopulatedBasin = 50; + + private transient Map<String, Map<String, Integer>> presences; + + private transient BufferedWriter bW; + + public static void main(String[] args) { + System.out.println((new XStream(new DomDriver())).toXML(new TypeTrajectoryCV())); + + // TypeTrajectoryCV trajectoryCV = new TypeTrajectoryCV(); + // trajectoryCV.computeKapa("data/input/reality/Obs1900.csv"); + } + + + @Override + public void initTransientParameters(Pilot pilot) { + super.initTransientParameters(pilot); + // read the file of presence in river basin + try { + // open the file + FileReader reader = new FileReader(presenceFileName); + // Parsing the file + Scanner scanner = new Scanner(reader); + scanner.useLocale(Locale.ENGLISH); // to have a point as decimal + // separator !!! + // scanner.useDelimiter(Pattern.compile("[;,\r\n]")); + + // read the headers + String[] headers = scanner.nextLine().split(","); + presences = new TreeMap<String, Map<String, Integer>>(); + boolean periodTest = false; + for (int i = 2; i < headers.length; i++) { + if (period.equals(headers[i])) + periodTest = true; + presences.put(headers[i], new TreeMap<String, Integer>()); + } + + // TODO send the error + if (periodTest == false) + System.out.println("The period " + period + " is not present in the file " + presenceFileName); + // read the lines + while (scanner.hasNextLine()) { + String[] fields = scanner.nextLine().split(","); + // System.out.println(Arrays.toString(fields)); + + for (int j = 2; j < headers.length; j++) { + if (j >= fields.length) + presences.get(headers[j]).put(fields[1], -1); + else { + if (fields[j].compareTo("") == 0) + presences.get(headers[j]).put(fields[1], -1); + else + presences.get(headers[j]).put(fields[1], Integer.valueOf(fields[j])); + } + } + } + reader.close(); + scanner.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + @Override + public void doProcess(DiadromousFishGroup group) { + + // int[] finalStates = group.getEnvironment().getFinalStates(); + // int[] finalStatesWithStoch = group.getEnvironment().getFinalStatesWithStochasticity(); + int[] finalStatesForKappa = group.getEnvironment().getFinalStatesForKappa(); + double[] geoMeanRecOverProdCap = group.getEnvironment().getGeoMeansLastRecsOverProdCaps(); + double[] meanLastRec = group.getEnvironment().getMeanLastRecruitments(); + double[] meanPercOfAut = group.getEnvironment().getMeanLastPercOfAut(); + double[] probOfNonNulRecruitmentDuringLastYears = group.getEnvironment().getProbOfNonNulRecruitmentDuringLastYears(); + long[] yearsOfFirstNonNulRep = group.getEnvironment().getYearsOfFirstNonNulRep(); + long[] yearsOfLastNulRep = group.getEnvironment().getYearsOfLastNulRep(); + String[] finalStatesNames = group.getEnvironment().getRiverBasinNames(); + double[] finalProbabilityOfPresence = group.getEnvironment().getFinalProbabilityOfPresence(); + + // System.out.println(group.getEnvironment().getMeanLastRecruitmentsBV2()); + // System.out.println(" nom des bv " + Arrays.deepToString(finalStatesNames) + "final states : " + + // Arrays.toString(finalStates)); + // System.out.println(" nom des bv " + Arrays.deepToString(finalStatesNames) + "final states wirth stoch : " + + // Arrays.toString(finalStatesWithStoch)); + // System.out.println(" nom des bv " + Arrays.deepToString(finalStatesNames) + "geo mean of last 10 years + // R/alpha : " + Arrays.toString(geoMeanRecOverProdCap)); + // System.out.println(" nom des bv " + Arrays.deepToString(finalStatesNames) + "mean last recruitment : " + + // Arrays.toString(meanLastRec)); + // System.out.println(" nom des bv " + Arrays.deepToString(finalStatesNames)); + // System.out.println(" nom des bv " + Arrays.deepToString(finalStatesNames) + "prob non nul rec : " + + // Arrays.toString(probOfNonNulRecruitmentDuringLastYears)); + // System.out.println("mean last percentage of autochtone : " + Arrays.toString(meanPercOfAut)); + // System.out.println(" nom des bv " + Arrays.deepToString(finalStatesNames) + "year of first non nul + // reproduction : " + Arrays.toString(yearsOfFirstNonNulRep)); + // System.out.println(" nom des bv " + Arrays.deepToString(finalStatesNames) + "year of last nul reproduction : + // " + Arrays.toString(yearsOfLastNulRep)); + // this.fireChangesToObservers(); + if (fileNameOutput != null) { + // create the subdirectorrys if necessary ? + new File(group.getOutputPath() + fileNameOutput).getParentFile().mkdirs(); + + try { + bW = new BufferedWriter( + new FileWriter(new File(group.getOutputPath() + fileNameOutput + group.getSimulationId() + ".csv"))); + int nbBV = finalStatesNames.length; + + bW.write("nom des bv"); + for (int i = 0; i < nbBV; i++) { + bW.write(";" + finalStatesNames[i]); + } + bW.write("\n"); + + bW.write("Final states"); + for (int i = 0; i < nbBV; i++) { + bW.write(";" + finalStatesForKappa[i]); + } + bW.write("\n"); + + bW.write("year of first non nul reproduction"); + for (int i = 0; i < nbBV; i++) { + bW.write(";" + yearsOfFirstNonNulRep[i]); + } + bW.write("\n"); + + bW.write("year of last nul reproduction"); + for (int i = 0; i < nbBV; i++) { + bW.write(";" + yearsOfLastNulRep[i]); + } + bW.write("\n"); + + bW.write("mean last recruitment"); + for (int i = 0; i < nbBV; i++) { + bW.write(";" + meanLastRec[i]); + } + bW.write("\n"); + + bW.write("geo mean of last 10 years R/alpha"); + for (int i = 0; i < nbBV; i++) { + bW.write(";" + geoMeanRecOverProdCap[i]); + } + bW.write("\n"); + + bW.write("mean last percentage of autochtone"); + for (int i = 0; i < nbBV; i++) { + bW.write(";" + meanPercOfAut[i]); + } + bW.write("\n"); + + bW.write("prob of non nul recruitment during last years"); + for (int i = 0; i < nbBV; i++) { + bW.write(";" + probOfNonNulRecruitmentDuringLastYears[i]); + } + bW.write("\n"); + + computeKapa(bW, "data/input/reality/Obs1900.csv", finalStatesForKappa, finalStatesNames); + bW.write("\n"); + + bW.write("likelihood;" + ((Double) computeLikelihood(group)).toString() + "\n"); + + bW.write("spawnersMatureAgeSumStat;" + computeFemaleSpawnerForFirstTimeSummaryStatistic(group) + "\n"); + + bW.write("higherPopulatedLatitude;" + getNorthernmostPopulatedLatitude(group) + "\n"); + + bW.write("graine;" + group.getPilot().getParameters().getRngStatusIndex() + "\n"); + + bW.write("finalProbOfPres"); + for (int i = 0; i < nbBV; i++) { + bW.write(";" + finalProbabilityOfPresence[i]); + } + bW.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + + public void computeKapa(BufferedWriter writer, String fileNameInputForInitialObservation, int[] finalStatesForKappa, + String[] finalStatesNames) throws IOException { + + // 1 : read input file of observation + FileReader reader; + Scanner scanner; + Map<String, Integer> obs1900 = new HashMap<String, Integer>(); + try { + reader = new FileReader(fileNameInputForInitialObservation); + // Parsing the file + scanner = new Scanner(reader); + scanner.useLocale(Locale.ENGLISH); // to have a comma as decimal separator !!! + scanner.useDelimiter(Pattern.compile("[;\r]")); + + scanner.nextLine(); // to skip the file first line of entete + + while (scanner.hasNext()) { + obs1900.put(scanner.next().replaceAll("\n", ""), scanner.nextInt()); + } + reader.close(); + + } catch (IOException ex) { + Logger.getLogger(TypeTrajectoryCV.class.getName()).log(Level.SEVERE, null, ex); + } + + // 2 : compute the confusion matrix between observed and computed data + // obs 0 1 + // pred 0 VN FN + // 1 FP VP + double[][] confusionMatrix = new double[2][2]; + int computedVal; + int obsVal; + int nbVal = 0; + for (int i = 0; i < finalStatesNames.length; i++) { + if (obs1900.containsKey(finalStatesNames[i])) { + computedVal = (finalStatesForKappa[i] > 0) ? 1 : 0; + obsVal = obs1900.get(finalStatesNames[i]); + if (computedVal == 0) { + if (obsVal == 0) { + confusionMatrix[0][0]++; + } else { + confusionMatrix[0][1]++; + } + } else { + if (obsVal == 0) { + confusionMatrix[1][0]++; + } else { + confusionMatrix[1][1]++; + } + } + nbVal++; + } + } + // normalisation + confusionMatrix[0][0] = confusionMatrix[0][0] / nbVal; + confusionMatrix[0][1] = confusionMatrix[0][1] / nbVal; + confusionMatrix[1][0] = confusionMatrix[1][0] / nbVal; + confusionMatrix[1][1] = confusionMatrix[1][1] / nbVal; + + // 3 compute kapa + // kapa= (pra-pre)/(1-pre) + // with pra=VN+VP and pre=(VN+FN)*(VN+FP)+(FN+VP)*(FP+VP) + double VN = confusionMatrix[0][0]; + double FN = confusionMatrix[0][1]; + double FP = confusionMatrix[1][0]; + double VP = confusionMatrix[1][1]; + double pra = VN + VP; + double pre = (VN + FN) * (VN + FP) + (FN + VP) * (FP + VP); + double kapa = (pra - pre) / (1 - pre); + + writer.write("confusion matrix(VN,FN,FP,VP);"); + writer.write(((Double) confusionMatrix[0][0]).toString()); + writer.write(";"); + writer.write(((Double) confusionMatrix[0][1]).toString()); + writer.write(";"); + writer.write(((Double) confusionMatrix[1][0]).toString()); + writer.write(";"); + writer.write(((Double) confusionMatrix[1][1]).toString()); + writer.write("\nkappa;"); + writer.write(((Double) kapa).toString()); + + // System.out.println("kappa value: " + kapa); + } + + + public double computeLikelihood(DiadromousFishGroup group) { + int obsVal; + double sumLogWherePres = 0.; + double sumLogWhereAbs = 0.; + RIOBasinNetwork rioBasinNetwork = group.getEnvironment(); + final double[] probOfNonNulRecruitmentDuringLastYears = rioBasinNetwork.getProbOfNonNulRecruitmentDuringLastYears(); + final String[] riverBasinNames = rioBasinNetwork.getRiverBasinNames(); + + // TODO very risky since dependinf of basin order + for (int i = 0; i < riverBasinNames.length; i++) { + int presence = presences.get(period).get(riverBasinNames[i]); + if (presence == 0) { + sumLogWhereAbs += Math.log(1 - probOfNonNulRecruitmentDuringLastYears[i]); + } else { + sumLogWherePres += Math.log(probOfNonNulRecruitmentDuringLastYears[i]); + } + } + + return sumLogWhereAbs + sumLogWherePres; + } + + + public double getNorthernmostPopulatedLatitude(DiadromousFishGroup group) { + double latitude = 0; + + for (RiverBasin riverBasin : group.getEnvironment().getRiverBasins()) { + double meanLastRecruitment = riverBasin.getLastRecruitments().getMean(); + + if (meanLastRecruitment >= minimumRecruitsForPopulatedBasin) { + // the river basin is considered populated + + // northern edge + if (riverBasin.getLatitude() > latitude) { + // the basin is the new northern edge + latitude = riverBasin.getLatitude(); + } + } + } + return latitude; + } + + + public double computeFemaleSpawnerForFirstTimeSummaryStatisticWithTarget(double TARGET, DiadromousFishGroup group) { + double sum = 0; + for (RiverBasin riverBasin : group.getEnvironment().getRiverBasins()) { + if (riverBasin.getSpawnersForFirstTimeMeanAges(Gender.FEMALE).getMeanWithoutZero() > 0.) { + double val = riverBasin.getSpawnersForFirstTimeMeanAges(Gender.FEMALE).getMeanWithoutZero() - TARGET; + sum += val * val; + } + } + // System.out.println("sum female: " + sum); + return sum; + } + + public double computeFemaleSpawnerForFirstTimeSummaryStatistic(DiadromousFishGroup group) { + return computeFemaleSpawnerForFirstTimeSummaryStatisticWithTarget(targetedAgeForFemaleSpawnerForFirstTime, group); + } } diff --git a/src/main/java/species/DiadromousFishGroup.java b/src/main/java/species/DiadromousFishGroup.java index 78c25df5e1a5b9ccc3c0e55c1886401834b1c0ef..3169f364fc21fd64522fd4ad5f0bc9a749690529 100644 --- a/src/main/java/species/DiadromousFishGroup.java +++ b/src/main/java/species/DiadromousFishGroup.java @@ -14,8 +14,6 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Scanner; -import java.util.logging.Level; -import java.util.logging.Logger; import java.util.regex.Pattern; import org.openide.util.lookup.ServiceProvider; @@ -31,9 +29,7 @@ import fr.cemagref.simaqualife.kernel.AquaNismsGroup; import fr.cemagref.simaqualife.kernel.Processes; import fr.cemagref.simaqualife.pilot.Pilot; import miscellaneous.Duo; -import miscellaneous.TreeMapForCentile; import species.DiadromousFish.Gender; -import species.DiadromousFish.Stage; /** * @@ -58,15 +54,6 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin */ private NutrientRoutine nutrientRoutine; - public String fileNameInputForInitialObservation = "data/input/reality/Obs1900.csv"; - - /** - * centile to calcucale the range of species distribution - * - * @unit - */ - public double centileForRange = 0.95; - /** * file with the calibated parameters (from baysian approach) * @@ -179,23 +166,6 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin */ private transient List<Duo<Double, Double>> parameterSets; - // ================================================= - // calibration - // ================================================= - - @Deprecated - private Double targetedAgeForFemaleSpawnerForFirstTime = 5.5; - @Deprecated - private Double targetedAgeForMaleSpawnerForFirstTime = 4.5; - - /** - * map of observedoccurence in 1900 - * - * @unit - */ - @Deprecated - private transient Map<String, Integer> obs1900; - public static void main(String[] args) { DiadromousFishGroup diadromousFishGroup = new DiadromousFishGroup(new Pilot(), null, null); @@ -479,44 +449,6 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin e.printStackTrace(); } } - // ------------------------------------------------------------------ - // read occurence data file - // ----------------------------------------------------------- - FileReader reader; - Scanner scanner; - - obs1900 = new HashMap<String, Integer>(); - if (!fileNameInputForInitialObservation.isEmpty()) { - try { - reader = new FileReader(fileNameInputForInitialObservation); - // Parsing the file - scanner = new Scanner(reader); - scanner.useLocale(Locale.ENGLISH); // to have a comma as decimal separator !!! - scanner.useDelimiter(Pattern.compile("[;\r]")); - - scanner.nextLine(); // to skip the file first line of headers - - while (scanner.hasNextLine()) { - String[] fields = scanner.nextLine().split(","); - // System.out.println(Arrays.toString(fields)); - if (fields.length > 2) { - if (!fields[2].isEmpty()) - obs1900.put(fields[1], Integer.valueOf(fields[2])); - } - } - - reader.close(); - scanner.close(); - - } catch (IOException ex) { - Logger.getLogger(DiadromousFishGroup.class.getName()).log(Level.SEVERE, null, ex); - } - } - // defaut values - if (targetedAgeForFemaleSpawnerForFirstTime == null) - targetedAgeForFemaleSpawnerForFirstTime = 5.5; - if (targetedAgeForMaleSpawnerForFirstTime == null) - targetedAgeForMaleSpawnerForFirstTime = 4.5; // this.nutrientRoutine.getNutrientFluxesCollection().setBasinNames(this.getEnvironment().getRiverBasinNames()); this.nutrientRoutine.createNutrienFluxesCollections(this.getEnvironment().getRiverBasinNames()); @@ -614,6 +546,7 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin } + @Deprecated public double getStandardDeviationOfMatureFishLength() { double standardDeviationOfMatureFishLength = 0.; double sumOfSquareLength = 0.; @@ -692,264 +625,6 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin } - // ================================================================ - // statictis for calibration - // ================================================================ - @Deprecated - public double computeFemaleSpawnerForFirstTimeSummaryStatisticWithTarget(double TARGET) { - double sum = 0; - for (RiverBasin riverBasin : getEnvironment().getRiverBasins()) { - if (riverBasin.getSpawnersForFirstTimeMeanAges(Gender.FEMALE).getMeanWithoutZero() > 0.) { - double val = riverBasin.getSpawnersForFirstTimeMeanAges(Gender.FEMALE).getMeanWithoutZero() - TARGET; - sum += val * val; - } - } - // System.out.println("sum female: " + sum); - return sum; - } - - - @Deprecated - @Observable(description = "Female spawners For First Time Summary Statistic") - public double computeFemaleSpawnerForFirstTimeSummaryStatistic() { - return computeFemaleSpawnerForFirstTimeSummaryStatisticWithTarget(targetedAgeForFemaleSpawnerForFirstTime); - } - - - @Deprecated - @Observable(description = "mean age at first reproduction for female") - public double getMeanAgeOfFirstReprodutionForFemale() { - double sum = 0; - double nb = 0; - for (RiverBasin riverBasin : getEnvironment().getRiverBasins()) { - if (riverBasin.getSpawnersForFirstTimeMeanAges(Gender.FEMALE).getMeanWithoutZero() > 0.) { - nb++; - sum += riverBasin.getSpawnersForFirstTimeMeanAges(Gender.FEMALE).getMeanWithoutZero(); - } - } - return sum / nb; - } - - - @Deprecated - @Observable(description = "mean length of first reprodution for female") - public double getMeanLengthOfFirstReprodutionForFemale() { - double sum = 0; - double nb = 0; - for (RiverBasin riverBasin : getEnvironment().getRiverBasins()) { - if (riverBasin.getSpawnersForFirstTimeMeanLengths(Gender.FEMALE).getMeanWithoutZero() > 0.) { - nb++; - sum += riverBasin.getSpawnersForFirstTimeMeanLengths(Gender.FEMALE).getMeanWithoutZero(); - } - } - return sum / nb; - } - - - @Deprecated - public double computeMaleSpawnerForFirstTimeSummaryStatisticWithTarget(double TARGET) { - double sum = 0; - for (RiverBasin riverBasin : getEnvironment().getRiverBasins()) { - if (riverBasin.getSpawnersForFirstTimeMeanAges(Gender.MALE).getMeanWithoutZero() > 0.) { - double val = riverBasin.getSpawnersForFirstTimeMeanAges(Gender.MALE).getMeanWithoutZero() - TARGET; - sum += val * val; - } - } - // System.out.println("sum male: " + sum); - return sum; - } - - - @Deprecated - @Observable(description = "Male spawners For First Time Summary Statistic") - public double computeMaleSpawnerForFirstTimeSummaryStatistic() { - return computeMaleSpawnerForFirstTimeSummaryStatisticWithTarget(targetedAgeForFemaleSpawnerForFirstTime); - } - - - @Deprecated - @Observable(description = "mean age of first reprodution for male") - public double getMeanAgeOfFirstReprodutionForMale() { - double sum = 0; - double nb = 0; - for (RiverBasin riverBasin : getEnvironment().getRiverBasins()) { - if (riverBasin.getSpawnersForFirstTimeMeanAges(Gender.MALE).getMeanWithoutZero() > 0.) { - nb++; - sum += riverBasin.getSpawnersForFirstTimeMeanAges(Gender.MALE).getMeanWithoutZero(); - } - } - return sum / nb; - } - - - @Observable(description = "mean length of first reprodution for male") - public double getMeanLengthOfFirstReprodutionForMale() { - double sum = 0; - double nb = 0; - for (RiverBasin riverBasin : getEnvironment().getRiverBasins()) { - if (riverBasin.getSpawnersForFirstTimeMeanLengths(Gender.MALE).getMeanWithoutZero() > 0.) { - nb++; - sum += riverBasin.getSpawnersForFirstTimeMeanLengths(Gender.MALE).getMeanWithoutZero(); - } - } - return sum / nb; - } - - - @Deprecated - @Observable(description = "Likelihood Summary stat") - public double computeLikelihood() { - int obsVal; - double sumLogWherePres = 0.; - double sumLogWhereAbs = 0.; - final double[] probOfNonNulRecruitmentDuringLastYears = getEnvironment().getProbOfNonNulRecruitmentDuringLastYears(); - final String[] finalStatesNames = getEnvironment().getRiverBasinNames(); - for (int i = 0; i < finalStatesNames.length; i++) { - if (obs1900.containsKey(finalStatesNames[i])) { - obsVal = obs1900.get(finalStatesNames[i]); - if (obsVal == 0) { - sumLogWhereAbs += Math.log(1 - probOfNonNulRecruitmentDuringLastYears[i]); - } else { - sumLogWherePres += Math.log(probOfNonNulRecruitmentDuringLastYears[i]); - } - } - } - - return sumLogWhereAbs + sumLogWherePres; - } - - - // ======================================================== - // observer to explore fish distribution - // ======================================================== - @Deprecated - @Observable(description = "Higher Populated Latitude") - public double getHigherPopulatedLatitude() { - double latitude = 0.0; - RiverBasin[] basins = getEnvironment().getRiverBasins(); - int[] finalStates = getEnvironment().getFinalStates(); - for (int i = 0; i < finalStates.length; i++) { - if ((finalStates[i] == 1) && (basins[i].getLatitude() > latitude)) { - latitude = basins[i].getLatitude(); - } - } - return latitude; - } - - - @Deprecated - @Observable(description = "Number of colonized basins") - public double getNbColonizedBasins() { - int nb = 0; - for (Basin seaBasin : getEnvironment().getSeaBasins()) { - if (seaBasin.getFishs(this) != null) { - if (!seaBasin.getFishs(this).isEmpty()) { - nb++; - } - } - } - return nb; - } - - - @Deprecated - @Observable(description = "Northern colonized basins") - public double getNorthernBasins() { - int northernBasin = Integer.MAX_VALUE; - for (Basin seaBasin : getEnvironment().getSeaBasins()) { - if (seaBasin.getFishs(this) != null) { - if (!seaBasin.getFishs(this).isEmpty()) { - northernBasin = Math.min(northernBasin, getEnvironment().getAssociatedRiverBasin(seaBasin).getId()); - } - } - } - return northernBasin; - } - - - @Deprecated - @Observable(description = "Southern colonized basins") - public double getSouthernBasins() { - int southernBasin = Integer.MIN_VALUE; - for (Basin seaBasin : getEnvironment().getSeaBasins()) { - if (seaBasin.getFishs(this) != null) { - if (!seaBasin.getFishs(this).isEmpty()) { - southernBasin = Math.max(southernBasin, getEnvironment().getAssociatedRiverBasin(seaBasin).getId()); - } - } - } - return southernBasin; - } - - - @Deprecated - @Observable(description = "Range distribution with latitude") - public Double[] getRangeDistributionWithLat() { - // TODO keep the extreme latitudes from the catchment - double southernBasin = 35.; - double northernBasin = 60.; - RiverBasin riverBasin; - TreeMapForCentile latitudeEffective = new TreeMapForCentile(); - for (Basin seaBasin : getEnvironment().getSeaBasins()) { - if (seaBasin.getFishs(this) != null) { - if (!seaBasin.getFishs(this).isEmpty()) { - riverBasin = getEnvironment().getAssociatedRiverBasin(seaBasin); - long effective = 0; - for (DiadromousFish fish : seaBasin.getFishs(this)) { - effective += fish.getAmount(); - } - southernBasin = Math.max(southernBasin, riverBasin.getLatitude()); - latitudeEffective.putWithAdding(riverBasin.getLatitude(), effective); - northernBasin = Math.min(northernBasin, riverBasin.getLatitude()); - } - } - } - - Double[] rangeDistribution = new Double[4]; - - rangeDistribution[0] = (latitudeEffective.isEmpty() ? (southernBasin + northernBasin) / 2. - : latitudeEffective.calculateMedian()); - rangeDistribution[1] = Math.min(southernBasin, northernBasin); - rangeDistribution[2] = Math.max(southernBasin, northernBasin); - rangeDistribution[3] = latitudeEffective.calculateCentile(centileForRange); - return rangeDistribution; - } - - - @Deprecated - @Observable(description = "Range distribution") - public double[] getRangeDistribution() { - double southernBasin = 0; - double nbBasin = getEnvironment().getNbRiverBasin(); - double northernBasin = nbBasin; - Basin riverBasin; - TreeMapForCentile latitudeEffective = new TreeMapForCentile(); - for (Basin seaBasin : getEnvironment().getSeaBasins()) { - if (seaBasin.getFishs(this) != null) { - if (!seaBasin.getFishs(this).isEmpty()) { - riverBasin = getEnvironment().getAssociatedRiverBasin(seaBasin); - long effective = 0; - for (DiadromousFish fish : seaBasin.getFishs(this)) { - effective += fish.getAmount(); - } - latitudeEffective.putWithAdding(riverBasin.getId(), effective); - southernBasin = Math.max(southernBasin, riverBasin.getId()); - northernBasin = Math.min(northernBasin, riverBasin.getId()); - } - } - } - southernBasin = nbBasin - southernBasin; - northernBasin = nbBasin - northernBasin; - double[] rangeDistribution = new double[3]; - rangeDistribution[0] = (latitudeEffective.isEmpty() ? (southernBasin + northernBasin) / 2. - : nbBasin - latitudeEffective.calculateMedian()); - rangeDistribution[1] = Math.min(southernBasin, northernBasin); - rangeDistribution[2] = Math.max(southernBasin, northernBasin); - - return rangeDistribution; - } - - // ======================================================== // observer to explore fish abundance // ======================================================== @@ -970,63 +645,6 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin } - /** - * @return sum of spawner effectives in all the river basins - */ - @Observable(description = "Number of spawners in river basins") - @Deprecated - public double getSpawnerEffective() { - long eff = 0; - for (RiverBasin basin : this.getEnvironment().getRiverBasins()) { - if (basin.getFishs(this) != null) { - for (DiadromousFish fish : basin.getFishs(this)) { - if (fish.getStage() == Stage.MATURE) - eff += fish.getAmount(); - } - } - } - return eff; - } - - - /** - * @return sum of male spawner effectives in all basins - */ - @Observable(description = "Number of male spawners in all basins") - @Deprecated - public double getMaleSpawnerEffective() { - long eff = 0; - for (Basin basin : this.getEnvironment().getBasins()) { - if (basin.getFishs(this) != null) { - for (DiadromousFish fish : basin.getFishs(this)) { - if (fish.getStage() == Stage.MATURE && fish.getGender() == Gender.MALE) - eff += fish.getAmount(); - } - } - } - return eff; - } - - - /** - * @return sum of female spawner effectives in all basins - */ - @Observable(description = "Number of female spawners in all basins") - @Deprecated - public double getFemaleSpawnerEffective() { - long eff = 0; - for (Basin basin : this.getEnvironment().getBasins()) { - if (basin.getFishs(this) != null) { - for (DiadromousFish fish : basin.getFishs(this)) { - if (fish.getStage() == Stage.MATURE && fish.getGender() == Gender.FEMALE) - eff += fish.getAmount(); - } - } - } - return eff; - } - - @Override public void addAquaNism(DiadromousFish fish) { // avoid utilisation of global fishes list