Commit 35bdda67 authored by patrick.lambert's avatar patrick.lambert
Browse files

two new process for sensitivi analisys

No related merge requests found
Showing with 262 additions and 0 deletions
+262 -0
/**
* 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 species;
import java.io.FileReader;
import java.util.Locale;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import environment.RiverBasin;
import fr.cemagref.simaqualife.kernel.processes.AquaNismsGroupProcess;
import fr.cemagref.simaqualife.pilot.Pilot;
/**
*
*/
public class AnalyseLikelihoodOfPresence extends AquaNismsGroupProcess<DiadromousFish, DiadromousFishGroup> {
private String presenceFileName = "data/input/northeastamerica/nea_presence.csv";
private String period = "obs_1751_1850";
/**
* The minimum number of recruits to consider a basin as populated
*
* @unit: #
*/
private int minimumRecruitsForPopulatedBasin = 50;
private double epsilon = 1e-3;
// map with presence
private transient Map<String, Map<String, Integer>> presences;
// logLikelihood
private transient double logLikelihood;
@Override
public void initTransientParameters(Pilot pilot) {
super.initTransientParameters(pilot);
if (Double.isNaN(epsilon))
epsilon = 1e-3;
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>>();
for (int i = 2; i < headers.length; i++) {
presences.put(headers[i], new TreeMap<String, Integer>());
}
// 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) {
logLikelihood = 0.;
for (RiverBasin riverBasin : group.getEnvironment().getRiverBasins()) {
double p_predit = (double) riverBasin.getLastRecruitments()
.getNumberOfStriclyHigherValue(minimumRecruitsForPopulatedBasin)
/ (double) riverBasin.getLastRecruitments().size();
int presence = presences.get(riverBasin.getName()).get(period);
if (presence == 1)
logLikelihood += Math.log(p_predit) + epsilon;
else
logLikelihood += 1 - Math.log(p_predit) + epsilon;
}
}
/**
* @return the logLikelihood
*/
public double getLogLikelihood() {
return this.logLikelihood;
}
public static void main(String[] args) {
System.out.println((new XStream(new DomDriver())).toXML(new AnalyseLikelihoodOfPresence()));
}
}
/**
* 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 species;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import environment.RiverBasin;
import fr.cemagref.simaqualife.kernel.processes.AquaNismsGroupProcess;
import fr.cemagref.simaqualife.pilot.Pilot;
import miscellaneous.QueueMemory;
import species.DiadromousFish.Gender;
/**
*
*/
public class AnalyseSpawnerFeatures extends AquaNismsGroupProcess<DiadromousFish, DiadromousFishGroup> {
private int memorySize = 30;
private transient Map<Integer, QueueMemory<Double>> femaleAgeMemories;
private transient Map<Integer, QueueMemory<Double>> maleAgeMemories;
private transient Map<Integer, QueueMemory<Double>> primiparousMemories;
@Override
public void initTransientParameters(Pilot pilot) {
super.initTransientParameters(pilot);
}
@Override
public void doProcess(DiadromousFishGroup group) {
// first passage
if (femaleAgeMemories == null) {
femaleAgeMemories = new HashMap<Integer, QueueMemory<Double>>();
for (RiverBasin riverBasin : group.getEnvironment().getRiverBasins()) {
femaleAgeMemories.put(riverBasin.getId(), new QueueMemory<>(memorySize));
}
}
for (RiverBasin riverBasin : group.getEnvironment().getRiverBasins()) {
if (riverBasin.getFishs(group) != null) {
double meanAgeForFemale = 0.;
double nbAgeForFemale = 0.;
double meanAgeForMale = 0.;
double nbAgeForMale = 0.;
double nbOfSpawners = 0.;
double nbOfPrimiparous = 0.;
for (DiadromousFish fish : riverBasin.getFishs(group)) {
if (fish.isMature()) {
// mean age
if (fish.getGender() == Gender.FEMALE) {
meanAgeForFemale += fish.getAge() * fish.getAmount();
nbAgeForFemale += fish.getAmount();
} else if (fish.getGender() == Gender.MALE) {
meanAgeForMale += fish.getAge() * fish.getAmount();
nbAgeForMale += fish.getAmount();
}
// primiparous
nbOfSpawners += fish.getAmount();
if (fish.getNumberOfReproduction() == 0)
nbOfPrimiparous += fish.getAmount();
}
}
// add value in the quues
femaleAgeMemories.get(riverBasin.getBasin_id()).add(meanAgeForFemale / nbAgeForFemale);
maleAgeMemories.get(riverBasin.getBasin_id()).add(meanAgeForMale / nbAgeForMale);
primiparousMemories.get(riverBasin.getBasin_id()).add(nbOfPrimiparous / nbOfSpawners);
}
}
}
public double[][] exportToR() {
int nbBasin = femaleAgeMemories.size();
double[][] result = new double[nbBasin][4];
int i = 0;
for (Entry<Integer, QueueMemory<Double>> entry : femaleAgeMemories.entrySet()) {
result[i][0] = entry.getKey();
result[i][1] = entry.getValue().getMean();
result[i][2] = maleAgeMemories.get(entry.getKey()).getMean();
result[i][3] = primiparousMemories.get(entry.getKey()).getMean();
}
return result;
}
public static void main(String[] args) {
System.out.println((new XStream(new DomDriver())).toXML(new AnalyseSpawnerFeatures()));
}
}
Supports Markdown
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