Commit df011376 authored by patrick.lambert's avatar patrick.lambert
Browse files

move methof for analyses from DiadromousFishGroup analysis package

parent 287ac708
......@@ -49,8 +49,11 @@ public class AnalyseSpawnerFeatures extends AquaNismsGroupProcess<DiadromousFish
private int memorySize = 30;
private Season analysisSeason = Season.SPRING;
private transient Map<RiverBasin, QueueMemory<Double>> femaleAgeMemories;
private transient Map<RiverBasin, QueueMemory<Double>> maleAgeMemories;
private transient Map<RiverBasin, QueueMemory<Double>> ageOfFemaleMemories;
private transient Map<RiverBasin, QueueMemory<Double>> ageOfMaleMemories;
private transient Map<RiverBasin, QueueMemory<Double>> ageOfPrimiparousFemaleMemories;
private transient Map<RiverBasin, QueueMemory<Double>> ageOfPrimiparousMaleMemories;
private transient Map<RiverBasin, QueueMemory<Double>> primiparousMemories;
protected static transient ObservablesHandler cobservable;
......@@ -59,13 +62,17 @@ public class AnalyseSpawnerFeatures extends AquaNismsGroupProcess<DiadromousFish
public void initTransientParameters(Pilot pilot) {
super.initTransientParameters(pilot);
femaleAgeMemories = new HashMap<RiverBasin, QueueMemory<Double>>();
maleAgeMemories = new HashMap<RiverBasin, QueueMemory<Double>>();
ageOfFemaleMemories = new HashMap<RiverBasin, QueueMemory<Double>>();
ageOfMaleMemories = new HashMap<RiverBasin, QueueMemory<Double>>();
ageOfPrimiparousFemaleMemories = new HashMap<RiverBasin, QueueMemory<Double>>();
ageOfPrimiparousMaleMemories = new HashMap<RiverBasin, QueueMemory<Double>>();
primiparousMemories = new HashMap<RiverBasin, QueueMemory<Double>>();
for (RiverBasin riverBasin : ((RIOBasinNetwork) pilot.getAquaticWorld().getEnvironment()).getRiverBasins()) {
femaleAgeMemories.put(riverBasin, new QueueMemory<>(memorySize));
maleAgeMemories.put(riverBasin, new QueueMemory<>(memorySize));
ageOfFemaleMemories.put(riverBasin, new QueueMemory<>(memorySize));
ageOfMaleMemories.put(riverBasin, new QueueMemory<>(memorySize));
ageOfPrimiparousFemaleMemories.put(riverBasin, new QueueMemory<>(memorySize));
ageOfPrimiparousMaleMemories.put(riverBasin, new QueueMemory<>(memorySize));
primiparousMemories.put(riverBasin, new QueueMemory<>(memorySize));
}
......@@ -78,8 +85,11 @@ public class AnalyseSpawnerFeatures extends AquaNismsGroupProcess<DiadromousFish
@Override
public void doProcess(DiadromousFishGroup group) {
if (cobservable == null)
initTransientParameters(group.getPilot());
// first passage
if (femaleAgeMemories == null) {
if (ageOfPrimiparousFemaleMemories == null) {
this.initTransientParameters(group.getPilot());
}
......@@ -88,9 +98,13 @@ public class AnalyseSpawnerFeatures extends AquaNismsGroupProcess<DiadromousFish
if (riverBasin.getFishs(group) != null) {
double meanAgeForPrimiparousFemale = 0.;
double nbAgeForPrimiparousFemale = 0.;
double meanAgeForFemale = 0.;
double nbAgeForFemale = 0.;
double meanAgeForPrimiparousMale = 0.;
double nbAgeForPrimiparousMale = 0.;
double meanAgeForMale = 0.;
double nbAgeForMale = 0.;
......@@ -111,19 +125,39 @@ public class AnalyseSpawnerFeatures extends AquaNismsGroupProcess<DiadromousFish
// primiparous
nbOfSpawners += fish.getAmount();
if (fish.getNumberOfReproduction() == 0)
if (fish.getNumberOfReproduction() == 0) {
nbOfPrimiparous += fish.getAmount();
// mean age
if (fish.getGender() == Gender.FEMALE) {
meanAgeForPrimiparousFemale += fish.getAge() * fish.getAmount();
nbAgeForPrimiparousFemale += fish.getAmount();
} else if (fish.getGender() == Gender.MALE) {
meanAgeForPrimiparousMale += fish.getAge() * fish.getAmount();
nbAgeForPrimiparousMale += fish.getAmount();
}
}
}
}
// add value in the queues
if (nbAgeForFemale > 0.)
femaleAgeMemories.get(riverBasin).push(meanAgeForFemale / nbAgeForFemale);
ageOfFemaleMemories.get(riverBasin).push(meanAgeForFemale / nbAgeForFemale);
else
femaleAgeMemories.get(riverBasin).push(Double.NaN);
ageOfFemaleMemories.get(riverBasin).push(Double.NaN);
if (nbAgeForMale > 0.)
maleAgeMemories.get(riverBasin).push(meanAgeForMale / nbAgeForMale);
ageOfMaleMemories.get(riverBasin).push(meanAgeForMale / nbAgeForMale);
else
ageOfMaleMemories.get(riverBasin).push(Double.NaN);
if (nbAgeForPrimiparousFemale > 0.)
ageOfPrimiparousFemaleMemories.get(riverBasin)
.push(meanAgeForPrimiparousFemale / nbAgeForPrimiparousFemale);
else
maleAgeMemories.get(riverBasin).push(Double.NaN);
ageOfPrimiparousFemaleMemories.get(riverBasin).push(Double.NaN);
if (nbAgeForPrimiparousMale > 0.)
ageOfPrimiparousMaleMemories.get(riverBasin).push(meanAgeForPrimiparousMale / nbAgeForPrimiparousMale);
else
ageOfPrimiparousMaleMemories.get(riverBasin).push(Double.NaN);
if (nbOfSpawners > 0.)
primiparousMemories.get(riverBasin).push(nbOfPrimiparous / nbOfSpawners);
else
......@@ -134,13 +168,41 @@ public class AnalyseSpawnerFeatures extends AquaNismsGroupProcess<DiadromousFish
// --------------------------------------------------------------------------------------------------
// update the observers
// ------------------------------------------------------------------------------------------------------
cobservable.fireChanges(this, pilot.getCurrentTime());
cobservable.fireChanges(this, group.getPilot().getCurrentTime());
}
}
@Observable(description = "mean age at first reproduction for female")
public double getMeanAgeOfPrimiparousFemale() {
double sum = 0;
double nb = 0;
for (RiverBasin riverBasin : ageOfPrimiparousFemaleMemories.keySet()) {
if (riverBasin.getSpawnersForFirstTimeMeanAges(Gender.FEMALE).getMeanWithoutZero() > 0.) {
nb++;
sum += riverBasin.getSpawnersForFirstTimeMeanAges(Gender.FEMALE).getMeanWithoutZero();
}
}
return sum / nb;
}
@Observable(description = "mean length of first reprodution for female")
public double getMeanLengthOfPrimiparousFemale() {
double sum = 0;
double nb = 0;
for (RiverBasin riverBasin : ageOfPrimiparousFemaleMemories.keySet()) {
if (riverBasin.getSpawnersForFirstTimeMeanLengths(Gender.FEMALE).getMeanWithoutZero() > 0.) {
nb++;
sum += riverBasin.getSpawnersForFirstTimeMeanLengths(Gender.FEMALE).getMeanWithoutZero();
}
}
return sum / nb;
}
@Observable(description = "PrimarousRate versus latitude")
public List<Record> getPrimarousRateVsLatitude() {
@Observable(description = "PrimiparousRate versus latitude")
public List<Record> getPrimiparousrousRateVsLatitude() {
List<Record> records = new ArrayList<Record>();
......@@ -155,22 +217,22 @@ public class AnalyseSpawnerFeatures extends AquaNismsGroupProcess<DiadromousFish
public String[][] exportToR() {
int nbBasin = femaleAgeMemories.size();
int nbBasin = ageOfFemaleMemories.size();
String[][] result = new String[nbBasin + 1][5];
// headers
result[0][0] = "basin_id";
result[0][1] = "basin_name";
result[0][2] = "mean_age_female";
result[0][3] = "mean_age _male";
result[0][2] = "mean_age_female"; // primiparous and multiparous
result[0][3] = "mean_age _male"; // primiparous and multiparous
result[0][4] = "pct_primiparous";
int i = 1;
for (Entry<RiverBasin, QueueMemory<Double>> entry : femaleAgeMemories.entrySet()) {
for (Entry<RiverBasin, QueueMemory<Double>> entry : ageOfFemaleMemories.entrySet()) {
result[i][0] = String.valueOf(entry.getKey().getBasin_id());
result[i][1] = entry.getKey().getName();
result[i][2] = String.valueOf(entry.getValue().getMean());
result[i][3] = String.valueOf(maleAgeMemories.get(entry.getKey()).getMean());
result[i][3] = String.valueOf(ageOfFemaleMemories.get(entry.getKey()).getMean());
result[i][4] = String.valueOf(primiparousMemories.get(entry.getKey()).getMean());
i++;
......
/**
* patrick
* @author Patrick Lambert
* @copyright Copyright (c) 2020, INRAE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package analysis;
import java.io.FileReader;
import java.util.Locale;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import environment.RIOBasinNetwork;
import environment.RiverBasin;
import fr.cemagref.observation.kernel.Observable;
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 ComputeStatisticForABCCalibration extends AquaNismsGroupProcess<DiadromousFish, DiadromousFishGroup> {
private Double targetedAgeForFemaleSpawnerForFirstTime = 5.5;
private Double targetedAgeForMaleSpawnerForFirstTime = 4.5;
private String presenceFileName = "data/input/northeastamerica/nea_presence.csv";
private String period = "obs_1751_1850";
private transient RIOBasinNetwork rioBasinNetwork;
private transient Map<String, Map<String, Integer>> presences;
@Override
public void initTransientParameters(Pilot pilot) {
super.initTransientParameters(pilot);
rioBasinNetwork = (RIOBasinNetwork) pilot.getAquaticWorld().getEnvironment();
// 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) {
if (rioBasinNetwork == null)
initTransientParameters(group.getPilot());
}
public double computeFemaleSpawnerForFirstTimeSummaryStatisticWithTarget(double TARGET) {
double sum = 0;
for (RiverBasin riverBasin : rioBasinNetwork.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;
}
@Observable(description = "Female spawners For First Time Summary Statistic")
public double computeFemaleSpawnerForFirstTimeSummaryStatistic() {
return computeFemaleSpawnerForFirstTimeSummaryStatisticWithTarget(targetedAgeForFemaleSpawnerForFirstTime);
}
public double computeMaleSpawnerForFirstTimeSummaryStatisticWithTarget(double TARGET) {
double sum = 0;
for (RiverBasin riverBasin : rioBasinNetwork.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;
}
@Observable(description = "Male spawners For First Time Summary Statistic")
public double computeMaleSpawnerForFirstTimeSummaryStatistic() {
return computeMaleSpawnerForFirstTimeSummaryStatisticWithTarget(targetedAgeForFemaleSpawnerForFirstTime);
}
@Observable(description = "Likelihood Summary stat")
public double computeLikelihood() {
int obsVal;
double sumLogWherePres = 0.;
double sumLogWhereAbs = 0.;
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;
}
}
......@@ -183,7 +183,9 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin
// calibration
// =================================================
@Deprecated
private Double targetedAgeForFemaleSpawnerForFirstTime = 5.5;
@Deprecated
private Double targetedAgeForMaleSpawnerForFirstTime = 4.5;
/**
......@@ -191,6 +193,7 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin
*
* @unit
*/
@Deprecated
private transient Map<String, Integer> obs1900;
public static void main(String[] args) {
......@@ -334,11 +337,6 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin
}
public DiadromousFishGroup() {
super();
}
public double getPattractive(String basinName) {
// TODO pass in argument a Basin
// remove "-s" of the sea basin name
......@@ -697,6 +695,7 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin
// ================================================================
// statictis for calibration
// ================================================================
@Deprecated
public double computeFemaleSpawnerForFirstTimeSummaryStatisticWithTarget(double TARGET) {
double sum = 0;
for (RiverBasin riverBasin : getEnvironment().getRiverBasins()) {
......@@ -710,12 +709,14 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin
}
@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;
......@@ -730,6 +731,7 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin
}
@Deprecated
@Observable(description = "mean length of first reprodution for female")
public double getMeanLengthOfFirstReprodutionForFemale() {
double sum = 0;
......@@ -744,6 +746,7 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin
}
@Deprecated
public double computeMaleSpawnerForFirstTimeSummaryStatisticWithTarget(double TARGET) {
double sum = 0;
for (RiverBasin riverBasin : getEnvironment().getRiverBasins()) {
......@@ -757,12 +760,14 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin
}
@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;
......@@ -791,6 +796,7 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin
}
@Deprecated
@Observable(description = "Likelihood Summary stat")
public double computeLikelihood() {
int obsVal;
......@@ -831,6 +837,7 @@ public class DiadromousFishGroup extends AquaNismsGroup<DiadromousFish, RIOBasin
}
@Deprecated
@Observable(description = "Number of colonized basins")
public double getNbColonizedBasins() {
int nb = 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment