Finish Nettoyage Moyenne
This commit is contained in:
@@ -1,80 +1,90 @@
|
|||||||
package ecoparasite.nettoyage;
|
package ecoparasite.nettoyage;
|
||||||
|
|
||||||
|
import ecoparasite.completion.Completion;
|
||||||
import ecoparasite.poisson.Poisson;
|
import ecoparasite.poisson.Poisson;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Définissant les méthodes statics de Nettoyage des données
|
* Class Définissant les méthodes statics de Nettoyage des données
|
||||||
*/
|
*/
|
||||||
public class Nettoyage {
|
public class Nettoyage {
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* Methode Static de nettoyage des valeurs abérrante et absurde
|
|
||||||
* @param tablePoisson Un Hashset de Poisson contenant nos données
|
|
||||||
* @return Le Hashset de Poisson une fois qu'il est nettoyé
|
|
||||||
*/
|
|
||||||
public static HashSet<Poisson> nettoiePoissonMean(HashSet<Poisson> tablePoisson){
|
public static HashSet<Poisson> nettoiePoissonMean(HashSet<Poisson> tablePoisson){
|
||||||
|
|
||||||
Double mean = mean(tablePoisson); //Moyenne
|
Double mean = Completion.calculateMean(tablePoisson,Poisson::getInfestation); //Moyenne
|
||||||
Double ecart = ecartType(tablePoisson); // Ecart Type
|
|
||||||
|
|
||||||
Double z = 0.0;
|
ArrayList<Double> infest = new ArrayList<>();
|
||||||
|
for (Poisson p : tablePoisson) {
|
||||||
|
if (p.getInfestation() != null){ //Test des valeurs null pour les Tests Unitaires. Je ne devrais pas en avoir.
|
||||||
|
infest.add(p.getInfestation());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Collections.sort(infest);
|
||||||
|
|
||||||
|
int quartIndex = infest.size()/4;
|
||||||
|
Double firstQuart = infest.get(quartIndex);
|
||||||
|
Double thirdQuart = infest.get(quartIndex *3);
|
||||||
|
Double IQR = thirdQuart - firstQuart;
|
||||||
|
|
||||||
for (Poisson p : tablePoisson) {
|
for (Poisson p : tablePoisson) {
|
||||||
z = ( p.getInfestation() - mean ) / ecart;
|
|
||||||
|
|
||||||
if ( z >= 2.5 ) {
|
if (p.getInfestation() == null) {
|
||||||
|
p.setInfestation(mean); //Fonction codé en dur pour éviter des problèmes dans les Tests Unitaires : Completion devrais etre fait et valeur null ne devrait pas exister
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (p.getInfestation() < firstQuart - (IQR * 1.5) || p.getInfestation() > thirdQuart + (IQR * 1.5)) {
|
||||||
p.setInfestation(mean);
|
p.setInfestation(mean);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return tablePoisson;
|
return tablePoisson;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Methode Privée permettant de calculer la moyenne
|
* Permet de remplacer les valeurs inexistantes d'un paramètre d'un HashSet par la moyenne des autres valeurs (non nulles).
|
||||||
* @param tablePoisson Un Hashset de Poisson contenant nos données
|
* Exemple d'utilisation : T = Poisson, V = Double, getValue = Poisson::getInfestation, setValue = Poisson::setInfestation.
|
||||||
* @return Un Double correspondant à la moyenne
|
*
|
||||||
|
* @param list La liste de données cobaye.
|
||||||
|
* @param getValue La fonction (Getter) qui permet d'obtenir la valeur que l'on veut vérifier
|
||||||
|
* @param setValue La fonction (Setter) qui permet de remplacer la valeur si null.
|
||||||
|
* @return Le HashSet avec les valeurs remplacés.
|
||||||
|
* @param <T> Le type de données cobaye. Exemple : Poisson, Population
|
||||||
|
* @param <V> Le type de la donnée à vérifier, doit être un Wrapper Number. Exemple : Double.
|
||||||
*/
|
*/
|
||||||
private static Double mean(HashSet<Poisson> tablePoisson){
|
public static <T,V extends Number> HashSet<T> nettoieColumnsMoyenne(HashSet<T> list, Function<T,V> getValue, BiConsumer<T,V> setValue ){
|
||||||
|
|
||||||
Double mean = 0.0;
|
Double mean = Completion.calculateMean(list, getValue);
|
||||||
|
|
||||||
for (Poisson p : tablePoisson){
|
ArrayList<Double> array = new ArrayList<>();
|
||||||
mean += p.getInfestation();
|
for ( T item : list) {
|
||||||
|
if (getValue.apply(item)!= null){ //Test des valeurs null pour les Tests Unitaires. Je ne devrais pas en avoir.
|
||||||
|
array.add(getValue.apply(item).doubleValue());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return mean / tablePoisson.size();
|
Collections.sort(array);
|
||||||
|
|
||||||
|
int quartIndex = array.size()/4;
|
||||||
|
Double firstQuart = array.get(quartIndex);
|
||||||
|
Double thirdQuart = array.get(quartIndex *3);
|
||||||
|
Double IQR = thirdQuart - firstQuart;
|
||||||
|
|
||||||
|
for(T item : list){
|
||||||
|
if( getValue.apply(item).doubleValue() < firstQuart - (IQR * 1.5) || getValue.apply(item).doubleValue() > thirdQuart + (IQR * 1.5)){
|
||||||
|
setValue.accept( item, (V) mean);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
return list;
|
||||||
* Methode Privée permettant de calculer la variance
|
|
||||||
* @param tablePoisson Un Hashset de Poisson contenant nos données
|
|
||||||
* @return Un Double correspondant à la variance
|
|
||||||
*/
|
|
||||||
private static Double variance(HashSet<Poisson> tablePoisson){
|
|
||||||
|
|
||||||
Double vari = 0.0;
|
|
||||||
Double mean = mean(tablePoisson);
|
|
||||||
|
|
||||||
for (Poisson p : tablePoisson) {
|
|
||||||
vari += Math.pow( (p.getInfestation() - mean), 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
return vari / tablePoisson.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Methode Privée permettant de calculer l'écart-type
|
|
||||||
* @param tablePoisson Un Hashset de Poisson contenant nos données
|
|
||||||
* @return Un Double correspondant à l'écart-type
|
|
||||||
*/
|
|
||||||
private static Double ecartType(HashSet<Poisson> tablePoisson){
|
|
||||||
|
|
||||||
Double vari = variance(tablePoisson);
|
|
||||||
|
|
||||||
return Math.sqrt(vari);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
91
src/ecoparasite/poisson/MackerelSerra.java
Normal file
91
src/ecoparasite/poisson/MackerelSerra.java
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
package ecoparasite.poisson;
|
||||||
|
|
||||||
|
import ecoparasite.input.DataParsing;
|
||||||
|
import ecoparasite.input.InvalidParsingException;
|
||||||
|
import ecoparasite.input.RawData;
|
||||||
|
import ecoparasite.input.RawDataOverflow;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import static java.lang.Double.valueOf;
|
||||||
|
|
||||||
|
public class MackerelSerra extends Poisson implements DataParsing {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructeur de MackerelSerra
|
||||||
|
* @param length
|
||||||
|
* @param infestation
|
||||||
|
*/
|
||||||
|
public MackerelSerra(String id, Double length, Double infestation) {
|
||||||
|
super(id, length, null, infestation);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implémentation de la fonction parse de DataParsing.
|
||||||
|
* Renvoie un tableau de poissons à partir d'un RawData.
|
||||||
|
*
|
||||||
|
* @param data Notre RawData
|
||||||
|
* @param parseTypeId L'ID du type de parsing, ignoré ici.
|
||||||
|
* @return Le tableau de poissons.
|
||||||
|
* @throws RawDataOverflow Si on a un dépassement de données dans notre RawData.
|
||||||
|
* @throws InvalidParsingException
|
||||||
|
*/
|
||||||
|
public static HashSet<Poisson> parse(RawData data, int parseTypeId) throws RawDataOverflow, InvalidParsingException {
|
||||||
|
return MackerelSerra.parse(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implémentation de la fonction parse de Dataparsing
|
||||||
|
* @param data Notre RawData.
|
||||||
|
* @return tableau des poissons
|
||||||
|
* @throws RawDataOverflow Si on a un dépassement de données dans notre RawDataOverflow.
|
||||||
|
*/
|
||||||
|
public static HashSet<Poisson> parse(RawData data) throws RawDataOverflow {
|
||||||
|
|
||||||
|
HashMap<String,String> temp = new HashMap<>();
|
||||||
|
|
||||||
|
HashSet<Poisson> fishSet;
|
||||||
|
fishSet = new HashSet<>();
|
||||||
|
|
||||||
|
for (int i = 0; i < data.getData().getFirst().size(); i++) {
|
||||||
|
|
||||||
|
temp = data.getEntry(i);
|
||||||
|
|
||||||
|
String id = !Objects.equals(temp.get("id"), "") ? (temp.get("id")) : null;
|
||||||
|
Double size = !Objects.equals(temp.get("LT"), "") ? valueOf(temp.get("LT")) : null;
|
||||||
|
Double infes = !Objects.equals(temp.get("Total"), "") ? valueOf(temp.get("Total")) : null;
|
||||||
|
|
||||||
|
Poisson newP = new MackerelSerra(id,size,infes);
|
||||||
|
newP.setFishParts( MackerelSerra.parsePartiePoisson(temp) );
|
||||||
|
fishSet.add(newP);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return fishSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param entry
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private static HashSet<PartiePoisson> parsePartiePoisson(HashMap<String,String> entry){
|
||||||
|
|
||||||
|
HashSet<PartiePoisson> response = new HashSet<>();
|
||||||
|
|
||||||
|
|
||||||
|
for( String k: entry.keySet() ){
|
||||||
|
if( k.contains("Foie") || k.contains("Abdomen") || k.contains("Visceres") || k.contains("Autres")){
|
||||||
|
String bodyPart = k;
|
||||||
|
Double value = !Objects.equals(entry.get(k), "") ? valueOf(entry.get(k)) : null;
|
||||||
|
PartiePoisson p = new PartiePoisson(bodyPart, value);
|
||||||
|
response.add(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,23 +6,33 @@ import ecoparasite.input.InputFileException;
|
|||||||
import ecoparasite.input.RawData;
|
import ecoparasite.input.RawData;
|
||||||
import ecoparasite.input.RawDataOverflow;
|
import ecoparasite.input.RawDataOverflow;
|
||||||
import ecoparasite.poisson.Mackerel;
|
import ecoparasite.poisson.Mackerel;
|
||||||
|
import ecoparasite.poisson.MackerelSerra;
|
||||||
import ecoparasite.poisson.Poisson;
|
import ecoparasite.poisson.Poisson;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.function.BiConsumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
class NettoyageTest {
|
class NettoyageTest {
|
||||||
|
|
||||||
@org.junit.jupiter.api.Test
|
@org.junit.jupiter.api.Test
|
||||||
void nettoiePoissonMean() throws InputFileException, RawDataOverflow {
|
void nettoiePoissonMean() throws InputFileException, RawDataOverflow {
|
||||||
|
|
||||||
RawData test = InputFactory.readData("testNettoie.csv");
|
RawData test = InputFactory.readData("test2.csv", ",");
|
||||||
|
|
||||||
HashSet<Poisson> testp = Mackerel.parse(test);
|
HashSet<Poisson> testp = MackerelSerra.parse(test);
|
||||||
|
|
||||||
System.out.println(testp);
|
System.out.println(testp);
|
||||||
|
|
||||||
testp = Nettoyage.nettoiePoissonMean(testp);
|
Function<Poisson,Double> getInfes = Poisson::getInfestation;
|
||||||
|
BiConsumer<Poisson,Double> setInfes = Poisson::setInfestation;
|
||||||
|
|
||||||
|
testp = Completion.completeColumnsMoyenne( testp, getInfes, setInfes );
|
||||||
|
|
||||||
|
System.out.println(testp);
|
||||||
|
|
||||||
|
testp = Nettoyage.nettoieColumnsMoyenne( testp, getInfes, setInfes );
|
||||||
|
|
||||||
System.out.println(testp);
|
System.out.println(testp);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user