Commit 0a9161c8 authored by Poulet Camille's avatar Poulet Camille
Browse files

Merge branch 'develop' of gitlab-ssh.irstea.fr:SimAquaLife/GR3D into develop

parents c0427eac 35f96f6c
......@@ -333,6 +333,16 @@
<exportSeason>SPRING</exportSeason>
<fileNameOutput>biomassFluxes</fileNameOutput>
</species.WriteBiomassFluxes>
<species.IdentifyPopulation>
<synchronisationMode>ASYNCHRONOUS</synchronisationMode>
<consoleDisplay>false</consoleDisplay>
<years>
<long>1813</long>
</years>
<fluxesSeason>SPRING</fluxesSeason>
<fileNameOutput>effectiveFluxes</fileNameOutput>
</species.IdentifyPopulation>
</processesAtEnd>
</processes>
<useCemetery>false</useCemetery>
......
library(tidyverse)
computeAutochtonousRate = function(data){
data %>% filter(migrationBasin == originBasin) %>%
select(-originBasin) %>%
inner_join(data %>% group_by(year, migrationBasin) %>%
summarise(totalRun = sum(effective), .groups = 'drop'), by = c('migrationBasin', 'year')) %>%
mutate(autochtonousRate = effective / totalRun) %>%
select(year, migrationBasin, autochtonousRate)
}
computeHomingRate = function(data){
data %>% filter(migrationBasin == originBasin) %>%
select(-migrationBasin) %>%
inner_join(data %>% group_by(year, originBasin) %>%
summarise(production = sum(effective), .groups = 'drop'), by = c('originBasin', 'year')) %>%
mutate(homingRate = effective / production) %>%
select(year, originBasin, homingRate)
}
# comparison between autochtonous rate and homing rate
# computeAutochtonousRate(exchangesData) %>%
# inner_join(computeHomingRate(data = exchangesData), by = c('migrationBasin' = 'originBasin', 'year'))
identyMetapopulation = function(exchangesData, threshold = .95, verbose = FALSE) {
exchangesDataUpdated = exchangesData
# initialise the connection table between basin and metapopulation
metapopulation = exchangesDataUpdated %>%
distinct(year, basin = migrationBasin) %>%
mutate(metapop = basin)
iteration = 1
# find the basin with the minimum autochtonous rate for the first time,
basinWithMinAutochtonousRate = computeAutochtonousRate(data = exchangesDataUpdated) %>%
group_by(year) %>%
slice(which.min(autochtonousRate))
# loop while autochtonousRate is still < 0.95
while (basinWithMinAutochtonousRate$autochtonousRate < threshold ) {
# while (iteration <= 4 ) {
if (verbose) cat(iteration, ": ", basinWithMinAutochtonousRate$autochtonousRate, '\n' )
# basinWithMinAutochtonousRate will be merged with origin basin sending the maximum number of fish in this basin
metapopsToBeMerged <- exchangesDataUpdated %>%
inner_join(basinWithMinAutochtonousRate %>% select(-autochtonousRate),
by = c('year', 'migrationBasin' )) %>%
filter(migrationBasin != originBasin) %>% # to avoid self merging
group_by(year) %>%
slice(which.max(effective)) %>%
ungroup() %>%
select(year, migrationBasin, originBasin)
# update the connection table between basin and metapopulation by merging metapopulation
for (i in 1:nrow(metapopsToBeMerged)) {
mergedName = paste0("M", iteration, '_',i)
metapopsToBeMerged_i <- metapopsToBeMerged %>% slice(i)
if (verbose) cat("\t", metapopsToBeMerged_i$migrationBasin, ' + ', metapopsToBeMerged_i$originBasin, ' = ', mergedName, "\n")
metapopulation <- metapopulation %>%
mutate(metapop = if_else(year == metapopsToBeMerged_i$year &
metapop %in% (metapopsToBeMerged_i %>% select(ends_with("Basin")) %>% unlist(use.names = FALSE)),
mergedName,
metapop))
}
# sum by migration and origin basins according to updated metapopulations
# computed with initial exchangesData
exchangesDataUpdated <- exchangesData %>%
#sum up on migration basins
inner_join(metapopulation,
by = c('year', 'migrationBasin' = 'basin')) %>%
group_by(year, metapop, originBasin) %>%
summarise(effective = sum(effective), .groups = 'drop') %>%
rename(migrationBasin = metapop) %>%
# sum on origine basins
inner_join(metapopulation,
by = c('year', 'originBasin' = 'basin')) %>%
group_by(year, metapop, migrationBasin) %>%
summarise(effective = sum(effective), .groups = 'drop') %>%
rename(originBasin = metapop)
# minimum of autochtonous rate
basinWithMinAutochtonousRate = computeAutochtonousRate(data = exchangesDataUpdated) %>%
group_by(year) %>%
slice(which.min(autochtonousRate))
iteration = iteration + 1
}
return(list(metapopulation = metapopulation, exchangesData = exchangesDataUpdated))
}
# upload data =====
# longer table of effective of different origin basins in each migration basin
exchangesData <- read_csv("../../data/output/northeastamerica/effectiveFluxes_1-observed.csv")
exchangesData %>% group_by(year, basin = originBasin) %>%
summarise(production = sum(effective), .groups = 'drop') %>%
inner_join(exchangesData %>% group_by(year, basin = migrationBasin) %>%
summarise(totalRun = sum(effective), .groups = 'drop'), by = c('year', 'basin')) %>%
arrange(totalRun, production) %>% print(n = Inf)
#TODO explain how it is possible to have a production >0 and spawners run = 0: PB in JAVA or
# no reproductive success some years
exchangesData %>% filter(migrationBasin == 'Musquodoboit' ) %>% print(n = Inf)
exchangesData %>% filter(originBasin == 'Musquodoboit') %>% print(n = Inf)
# remove basin with no run of spawner
selectedBasins <- exchangesData %>% group_by(year, basin = originBasin) %>%
summarise(production = sum(effective), .groups = 'drop') %>%
inner_join(exchangesData %>% group_by(year, basin = migrationBasin) %>%
summarise(totalRun = sum(effective), .groups = 'drop'), by = c('year', 'basin')) %>%
filter(totalRun > 0) %>% select(year, basin)
selectedExchangesData <- exchangesData %>%
inner_join(selectedBasins, by = c('year', 'migrationBasin' = 'basin')) %>%
inner_join(selectedBasins, by = c('year', 'originBasin' = 'basin'))
# results ====
metapopulations = identyMetapopulation(selectedExchangesData, threshold = .95)
metapopulations$metapopulation %>% group_by(metapop) %>% summarise(n_basin = n(), .groups = 'drop') %>% arrange(desc(n_basin)) %>%
mutate(nb_basin = sum(n_basin)) %>%
print(n = Inf)
computeAutochtonousRate(exchangesData)
computeAutochtonousRate(metapopulations$exchangesData) %>% print(n = Inf)
# =================================
# idées abandonnées
# =================================
# # compute ratio strayer-from over production
# # the basin with the minimum of autochonous (maximum of allotochnous) in the spawner run is merged with the origin basin that sends
# # the maximum strayers accornding to its production
# metapopsToBeMerged <- exchangesData %>%
# inner_join(basinWithMinAutochtonousRate, by = c('year', 'migrationBasin')) %>%
# #joint with production in each catchment
# inner_join(exchangesData %>% group_by(year, originBasin) %>%
# summarise(production = sum(effective))
# , by = c('year', 'originBasin')) %>%
# filter(migrationBasin != originBasin) %>%
# mutate(ratioStrayerProdution = effective / production) %>%
# group_by(year) %>%
# slice(which.max(ratioStrayerProdution)) %>%
# ungroup() %>%
# mutate(mergedName = paste0("M", iteration, '_',row_number()))
\ No newline at end of file
......@@ -32,13 +32,11 @@ timeStepDuration =1
seed = 1
parametersNames = c("processes.processesEachStep.8.tempMinRep",
"processes.processesEachStep.8.ratioS95_S50",
"processes.processesEachStep.6.pHomingAfterEquil")
thetas = c(10, 2, 0.7)
arguments = c('','-simDuration',simDuration, '-simBegin',simBegin,
'-timeStepDuration',timeStepDuration,
'-RNGStatusIndex', format(seed,scientific = FALSE),
......@@ -46,12 +44,21 @@ arguments = c('','-simDuration',simDuration, '-simBegin',simBegin,
'-env',"data/input/northeastamerica/RIOBNneaBasins_Rjava.xml",
'-observers',"data/input/northeastamerica/RIO_obs_empty.xml")
outputFile = paste0("Avirer",seed)
.jinit(classpath = "", force.init = TRUE)
outputFile = paste0("SA/",seed)
.jinit(classpath = jarfile, force.init = TRUE)
.jcall("miscellaneous.EasyRun","V","runSimulation",arguments, outputFile,.jarray(parametersNames),.jarray(thetas))
.jcall("miscellaneous.EasyRun","[D","getValuesFromEnvironement","getMeanLastPercOfAut")
.jcall("miscellaneous.EasyRun","[D","getValuesFromAquanismGroup","getRangeDistribution")
# truc = .jcall("miscellaneous.EasyRun","Ljava/util/List;",
# "getAListFromAquanismGroupProcess","processes.processesAtEnd.1.getRecords")
#
# trucMieux = .jcast(truc, "ljava/util/ArrayList", convert.array = FALSE)
#
# i <- .jnew("java/lang/Integer", as.integer(10))
# print(i)
# print(.jsimplify(i))
# .jcall(trucMieux, "java/lang/Object", "get", i)
#
# with(trucMieux, {get(0)})
# ArrayList = J("java.util.ArrayList")
# .jclassPath()
......@@ -139,15 +139,15 @@
</configuration>
</plugin>
<!-- <plugin>
uses the previously generated Manifest file (with maven-bundle-plugin)
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin> -->
<!-- <plugin> -->
<!-- uses the previously generated Manifest file (with maven-bundle-plugin) -->
<!-- <artifactId>maven-jar-plugin</artifactId> -->
<!-- <configuration> -->
<!-- <archive> -->
<!-- <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile> -->
<!-- </archive> -->
<!-- </configuration> -->
<!-- </plugin> -->
<plugin>
<!-- plugin used for merging the various GeoTools META-INF/services files (specific transformer below) -->
......
/*
* Copyright (C) 2014 dumoulin
*
* Copyright (C) 2021 lambert Dumoulin
* *
* 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
......@@ -17,6 +17,7 @@
package miscellaneous;
import java.util.Arrays;
import java.util.List;
import fr.cemagref.simaqualife.extensions.pilot.BatchRunner;
import fr.cemagref.simaqualife.pilot.Pilot;
......@@ -25,7 +26,6 @@ public class EasyRun {
static Pilot pilot;
public static void runSimulation(String[] batchArgs, String outputfilename, String[] paramNames, double[] paramValues)
throws Exception {
......@@ -55,13 +55,18 @@ public class EasyRun {
}
public static double[] getValuesFromEnvironement(String outputName) throws Exception {
return (double[]) ReflectUtils.getValueFromPath(pilot.getAquaticWorld().getEnvironment(), outputName);
public static double[] getValuesFromEnvironement(String targetName) throws Exception {
return (double[]) ReflectUtils.getValueFromPath(pilot.getAquaticWorld().getEnvironment(), targetName);
}
public static double[] getValuesFromAquanismGroup(String targetName) throws Exception {
return (double[]) ReflectUtils.getValueFromPath(pilot.getAquaticWorld().getAquaNismsGroupsList().get(0), targetName);
}
public static double[] getValuesFromAquanismGroup(String outputName) throws Exception {
return (double[]) ReflectUtils.getValueFromPath(pilot.getAquaticWorld().getAquaNismsGroupsList().get(0), outputName);
public static List getAListFromAquanismGroupProcess(String targetPath) throws Exception {
return (List) ReflectUtils.getValueFromPath(pilot.getAquaticWorld().getAquaNismsGroupsList().get(0), targetPath);
}
......@@ -78,6 +83,10 @@ public class EasyRun {
runSimulation(batchArguments, "tsointsoin", parameterNames, parameterValues);
List truc = getAListFromAquanismGroupProcess("processes.processesAtEnd.1.getRecords");
System.out.println(truc.get(0).toString());
// System.out.println(getAListFromAquanismGroupProcess("processes.processesAtEnd.1.getRecords"));
System.out.println(Arrays.toString(getValuesFromEnvironement("getMeanLastPercOfAut")));
System.out.println(Arrays.toString(getValuesFromAquanismGroup("getRangeDistribution")));
......
......@@ -23,12 +23,13 @@ import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import environment.RIOBasinNetwork;
import environment.RiverBasin;
import environment.Time;
import environment.Time.Season;
......@@ -39,17 +40,76 @@ import fr.cemagref.simaqualife.kernel.processes.AquaNismsGroupProcess;
*/
public class IdentifyPopulation extends AquaNismsGroupProcess<DiadromousFish, DiadromousFishGroup> {
/**
*
*/
public class Record implements Serializable {
long year;
String migrationBasin;
String originBasin;
long effective;
public Record(long year, String migrationBasin, String originBasin, long effective) {
super();
this.year = year;
this.migrationBasin = migrationBasin;
this.originBasin = originBasin;
this.effective = effective;
}
@Override
public String toString() {
return "[" + this.year + "; " + this.migrationBasin + "; " + this.originBasin + "; " + this.effective + "]";
}
/**
* @return the year
*/
public long getYear() {
return this.year;
}
/**
* @return the migrationBasin
*/
public String getMigrationBasin() {
return this.migrationBasin;
}
/**
* @return the originBasin
*/
public String getOriginBasin() {
return this.originBasin;
}
/**
* @return the effective
*/
public long getEffective() {
return this.effective;
}
}
private boolean consoleDisplay = false;
private List<Long> years;
private Season fluxesSeason = Season.SPRING;
private String fileNameOutput = "data/output/fluxes";
private String fileNameOutput = "effectiveFluxes";
private transient List<Record> records;
private transient BufferedWriter bW;
private String sep;
private transient String sep;
// @Observable(description = "fluxes")
// private transient long[] fluxes;
public static void main(String[] args) {
System.out.println((new XStream(new DomDriver())).toXML(new IdentifyPopulation()));
}
......@@ -64,18 +124,13 @@ public class IdentifyPopulation extends AquaNismsGroupProcess<DiadromousFish, Di
public void doProcess(DiadromousFishGroup group) {
if (bW == null) {
if (fileNameOutput != null) {
sep = ";";
sep = ",";
try {
new File(group.getOutputPath() + fileNameOutput).getParentFile().mkdirs();
bW = new BufferedWriter(
new FileWriter(new File(group.getOutputPath() + fileNameOutput + group.getSimulationId() + ".csv")));
// BasinNetworkReal nbr= (BasinNetworkReal) pilot.getAquaticWorld().getEnvironment();
RIOBasinNetwork nbr = group.getEnvironment();
bW.write("year" + sep + "migrationBasin");
for (String basinName : nbr.getRiverBasinNames()) {
bW.write(sep + basinName);
}
bW.write("year" + sep + "migrationBasin" + sep + "originBasin" + sep + "effective");
bW.write("\n");
} catch (IOException e) {
e.printStackTrace();
......@@ -87,20 +142,33 @@ public class IdentifyPopulation extends AquaNismsGroupProcess<DiadromousFish, Di
String[] basinNames = group.getEnvironment().getRiverBasinNames();
// put in hasmap
if (records == null)
records = new ArrayList<Record>();
for (RiverBasin basin : group.getEnvironment().getRiverBasins()) {
for (RiverBasin originBasin : group.getEnvironment().getRiverBasins()) {
long effective = Math.round(basin.getSpawnerOrigins().getMeans().get(originBasin.getName()));
records.add(new Record(time.getYear(group.getPilot()), basin.getName(), originBasin.getName(), effective));
}
}
// write in file
if (fileNameOutput != null) {
try {
for (RiverBasin basin : group.getEnvironment().getRiverBasins()) {
bW.write(time.getYear(group.getPilot()) + sep + basin.getName());
for (String basinName : basinNames) {
bW.write(sep + Math.round(basin.getSpawnerOrigins().getMeans().get(basinName)));
for (RiverBasin migrationBasin : group.getEnvironment().getRiverBasins()) {
for (String originBasinName : basinNames) {
bW.write(time.getYear(group.getPilot()) + sep + migrationBasin.getName() + sep + originBasinName);
bW.write(sep + Math.round(migrationBasin.getSpawnerOrigins().getMeans().get(originBasinName)));
bW.write("\n");
}
bW.write("\n");
}
bW.flush();
} catch (IOException e) {
e.printStackTrace();
}
// display in console
if (consoleDisplay) {
System.out.print("MIGRATION" + "\t");
for (String basinName : basinNames) {
......@@ -128,4 +196,13 @@ public class IdentifyPopulation extends AquaNismsGroupProcess<DiadromousFish, Di
}
}
}
/**
* @return the records
*/
public List<Record> getRecords() {
return this.records;
}
}
......@@ -40,7 +40,6 @@ import species.DiadromousFish.Stage;
/**
*
*/
@Deprecated
public class WriteEffectivesFluxes extends AquaNismsGroupProcess<DiadromousFish, DiadromousFishGroup> {
private Season exportSeason = Season.SPRING;
......@@ -50,7 +49,6 @@ public class WriteEffectivesFluxes extends AquaNismsGroupProcess<DiadromousFish,
private transient BufferedWriter bW;
private transient String sep = ";";
public static void main(String[] args) {
System.out.println((new XStream(new DomDriver())).toXML(new WriteEffectivesFluxes()));
}
......
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