Finish Completion Linear.
This commit is contained in:
95
src/ecoparasite/LectureEval.java
Normal file
95
src/ecoparasite/LectureEval.java
Normal file
@@ -0,0 +1,95 @@
|
||||
package ecoparasite;
|
||||
|
||||
import ecoparasite.input.InputFactory;
|
||||
import ecoparasite.input.InputFileException;
|
||||
import ecoparasite.input.RawData;
|
||||
import ecoparasite.input.RawDataOverflow;
|
||||
import ecoparasite.population.Population;
|
||||
import ecoparasite.population.PopulationArgInterval;
|
||||
import ecoparasite.population.PopulationArgs;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
|
||||
public class LectureEval {
|
||||
|
||||
public static HashSet<Population> parseEval( RawData popRaw ){
|
||||
|
||||
HashSet<Population> popEspece = new HashSet<>();
|
||||
|
||||
int index = 1;
|
||||
try {
|
||||
while(true){
|
||||
HashMap<String,String> fields = popRaw.getEntry(index);
|
||||
|
||||
String espece = fields.get("Espèce");
|
||||
|
||||
Population population = new Population(espece);
|
||||
|
||||
if( population.getTotal() == null ){
|
||||
population.setTotal( new PopulationArgs() );
|
||||
}
|
||||
for( String k: fields.keySet() ){
|
||||
if( k.equals("Espèce") )
|
||||
continue;
|
||||
|
||||
LectureEval.applyValueForPopEval( population.getTotal(), k, fields.get(k) );
|
||||
}
|
||||
|
||||
popEspece.add(population);
|
||||
index++;
|
||||
}
|
||||
} catch (RawDataOverflow e) {
|
||||
// Fin de la liste.
|
||||
}
|
||||
|
||||
return popEspece;
|
||||
}
|
||||
|
||||
public static void applyValueForPopEval( PopulationArgs popArgs, String column, String value ){
|
||||
|
||||
if( value == null || value == "" ) // On n'ajoute pas les valeurs nulles.
|
||||
return;
|
||||
|
||||
switch (column){
|
||||
case "zone":
|
||||
popArgs.setZone(value);
|
||||
break;
|
||||
case "N":
|
||||
popArgs.setNumber( Integer.parseInt(value) );
|
||||
break;
|
||||
case "Prevalence":
|
||||
popArgs.setPrevalence(PopulationArgInterval.fromString(value));
|
||||
break;
|
||||
case "LT mm":
|
||||
popArgs.setLength(PopulationArgInterval.fromString(value));
|
||||
break;
|
||||
case "Masse g":
|
||||
popArgs.setWidth(PopulationArgInterval.fromString(value));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws RawDataOverflow {
|
||||
|
||||
RawData popRaw;
|
||||
try {
|
||||
popRaw = InputFactory.readData("test3.csv", "," );
|
||||
} catch(InputFileException e) {
|
||||
System.out.println(e.getMessage());
|
||||
return;
|
||||
}
|
||||
|
||||
HashSet<Population> pop = parseEval(popRaw);
|
||||
|
||||
// System.out.println( popRaw.getEntry(1) );
|
||||
|
||||
for( Population p: pop){
|
||||
System.out.println(p);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -64,4 +64,61 @@ public class Completion {
|
||||
return mean / i;
|
||||
}
|
||||
|
||||
public static <T,V extends Number> HashSet<T> completeColumnsLinear(HashSet<T> list, Function<T,V> getX, Function<T,V> getY, BiConsumer<T,V> setY ){
|
||||
|
||||
double meanX = calculateMean(list, getX);
|
||||
double meanY = calculateMean(list, getY);
|
||||
|
||||
double a = calculateLinearA(list,getX,getY,meanX,meanY);
|
||||
double b = calculateLinearB(meanX,meanY,a);
|
||||
|
||||
for(T item : list){
|
||||
if( getY.apply(item) == null && getX.apply(item) != null ){
|
||||
Double value = a * getX.apply(item).doubleValue() + b;
|
||||
setY.accept( item, (V) value );
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet de calculer le coefficient A de notre regression linéaire.
|
||||
* @param list
|
||||
* @param getX
|
||||
* @param getY
|
||||
* @param meanX
|
||||
* @param meanY
|
||||
* @return
|
||||
* @param <T>
|
||||
* @param <V>
|
||||
*/
|
||||
public static <T,V extends Number> double calculateLinearA(
|
||||
HashSet<T> list,
|
||||
Function<T,V> getX,
|
||||
Function<T,V> getY,
|
||||
double meanX,
|
||||
double meanY
|
||||
){
|
||||
double numerateur = 0.0;
|
||||
double denominateur = 0.0;
|
||||
|
||||
for( T item : list ){
|
||||
if( getX.apply(item) == null || getY.apply(item) == null ){
|
||||
continue;
|
||||
}
|
||||
numerateur += ( getX.apply(item).doubleValue() - meanX ) * ( getY.apply(item).doubleValue() - meanY );
|
||||
denominateur += ( getX.apply(item).doubleValue() - meanX ) * ( getX.apply(item).doubleValue() - meanX );
|
||||
}
|
||||
return numerateur / denominateur;
|
||||
}
|
||||
|
||||
public static double calculateLinearB(
|
||||
double meanX,
|
||||
double meanY,
|
||||
double valueA
|
||||
){
|
||||
return meanY - valueA * meanX;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -115,4 +115,9 @@ public class Population implements DataParsing {
|
||||
return PopulationParsing.parseParasitesPeru(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format( "[%s] - Total: %s", id, total );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ public class PopulationArgInterval {
|
||||
Double interval = Double.parseDouble(numbers[1].trim());
|
||||
return new PopulationArgInterval(mean - interval, mean + interval, mean);
|
||||
|
||||
} else if( rawValue.contains( "-" ) ){ // Entre.
|
||||
} else if( rawValue.contains( " - " ) ){ // Entre.
|
||||
|
||||
String[] numbers = rawValue.split("-");
|
||||
Double min = Double.parseDouble(numbers[0].trim());
|
||||
|
||||
@@ -22,6 +22,12 @@ public class PopulationArgs {
|
||||
private Double intensity;
|
||||
private Double abondance;
|
||||
|
||||
/*
|
||||
Champs pour l'évaluation.
|
||||
*/
|
||||
|
||||
private String zone;
|
||||
|
||||
/**
|
||||
* Constructeur.
|
||||
* Toutes les données peuvent être préremplis dans ce constructeur sauf l'année.
|
||||
@@ -143,6 +149,8 @@ public class PopulationArgs {
|
||||
return abondance;
|
||||
}
|
||||
|
||||
public String getZone() { return zone; }
|
||||
|
||||
public void setNumber(int number) {
|
||||
this.number = number;
|
||||
}
|
||||
@@ -171,4 +179,23 @@ public class PopulationArgs {
|
||||
this.abondance = abondance;
|
||||
}
|
||||
|
||||
public void setZone(String zone) { this.zone = zone; }
|
||||
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return String.format( "Année: %d, N: %d, Length: %f, Width: %f, Prevalence: %f, IC: %f, Intensity: %f, Abondance: %f, Zone: %s",
|
||||
this.year,
|
||||
this.number,
|
||||
this.length != null ? this.length.transformToDouble() : 0.0,
|
||||
this.width != null ? this.width.transformToDouble() : 0.0,
|
||||
this.prevalence != null ? this.prevalence.transformToDouble() : 0.0,
|
||||
this.ic != null ? this.ic.transformToDouble() : 0.0,
|
||||
this.intensity,
|
||||
this.abondance,
|
||||
this.zone
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user