A lot of things.

- Change tests directory location.
- Fix Merlu CSV UTF8 encoding.
- Make classes for ParasitesPeru2021.csv Parsing.
This commit is contained in:
2026-03-31 19:00:19 +02:00
parent 2bf0ca34df
commit 3a6968f40f
17 changed files with 441 additions and 59 deletions

View File

@@ -9,5 +9,7 @@ import java.util.HashSet;
*/
public interface DataParsing {
public HashSet<Poisson> parse(RawData data) throws RawDataOverflow;
public static HashSet<Poisson> parse(RawData data) throws RawDataOverflow {
return null;
}
}

View File

@@ -18,6 +18,7 @@ public class InputFactory {
final public static String DATA_MACKEREL = "Campagne/mackerel.97442.csv";
final public static String DATA_MERLU = "Campagne/merlu2018_75164.csv";
final public static String DATA_PARASITES_PERU = "Combinés/ParasitesPeru2021.csv";
/**
* Construit le chemin vers le fichier de données voulu.

View File

@@ -110,7 +110,7 @@ public class RawData {
HashMap<String,String> entry = new HashMap<>();
for( ArrayList<String> row : data ){
if( index > row.size()){
if( index >= row.size()){
throw new RawDataOverflow( index, row.size() );
}
entry.put( row.getFirst(), row.get( index ) );

View File

@@ -1,43 +0,0 @@
package ecoparasite.input;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import ecoparasite.input.RawData;
import java.util.HashSet;
class RawDataTest {
@Test
void getColumnsNames() throws InputFileException {
RawData test = InputFactory.readData("test.csv");
HashSet<String> d = new HashSet<>();
for (int i = 0; i < test.getData().size(); i++){
d.add(test.getData().get(i).getFirst());
}
System.out.println(test.getColumnsNames());
System.out.println(d);
assertIterableEquals(test.getColumnsNames(),d);
}
@Test
void getDataFromColumn() throws InputFileException {
RawData test = InputFactory.readData("test.csv");
System.out.println(test.getDataFromColumn("Sample_code"));
}
@Test
void getEntry() throws InputFileException, RawDataOverflow {
RawData test = InputFactory.readData("test.csv");
System.out.println(test.getEntry(2));
}
}

View File

@@ -29,8 +29,7 @@ public class Mackerel extends Poisson implements DataParsing {
* @param data
* @return tableau des poissons
*/
@Override
public HashSet<Poisson> parse(RawData data) throws RawDataOverflow {
public static HashSet<Poisson> parse(RawData data) throws RawDataOverflow {
HashMap<String,String> temp = new HashMap<>();

View File

@@ -29,8 +29,7 @@ public class Merlu extends Poisson implements DataParsing {
* @param data
* @return tableau des poissons
*/
@Override
public HashSet<Poisson> parse(RawData data) throws RawDataOverflow {
public static HashSet<Poisson> parse(RawData data) throws RawDataOverflow {
HashMap<String,String> temp = new HashMap<>();
@@ -39,7 +38,7 @@ public class Merlu extends Poisson implements DataParsing {
for (int i = 0; i < data.getData().getFirst().size(); i++) {
temp = data.getEntry(i);
Poisson newP = new Merlu(temp.get("nom.merlu"),valueOf(temp.get("m.size")),valueOf(temp.get("number of Anisakis sp L3")));
Poisson newP = new Merlu(temp.get("nom.merlu"),valueOf(temp.get("m.size (mm)")),valueOf(temp.get("number of Anisakis sp L3")));
fishSet.add(newP);
}

View File

@@ -67,7 +67,7 @@ public class Poisson{
*/
@Override
public String toString(){
String result = "[ %5s : %4d mm, %4d g, %4d taux d'infestation ]";
return String.format(result, this.getClass().getSimpleName(), this.getLength(), this.getWeight(), this.getInfestation());
String result = "[ %5s : %4f mm, %4f g, %4f taux d'infestation ]";
return String.format(result, this.getClass().getSimpleName(), this.getLength(), this.getWeight(), this.getInfestation() );
}
}

View File

@@ -0,0 +1,50 @@
package ecoparasite.population;
import java.util.HashMap;
import java.util.HashSet;
public class Population {
private String id;
private PopulationArgs total;
private HashMap<Integer,PopulationArgs> perYear;
public Population(String id, PopulationArgs total, HashMap<Integer,PopulationArgs> perYear) {
this.id = id;
this.total = total;
this.perYear = perYear;
}
public Population(String id, PopulationArgs total) {
this.id = id;
this.total = total;
this.perYear = new HashMap<>();
}
public Population(String id) {
this.id = id;
this.total = null;
this.perYear = new HashMap<>();
}
public String getId() {
return id;
}
public PopulationArgs getTotal() {
return total;
}
public HashMap<Integer,PopulationArgs> getPerYear() {
return perYear;
}
public void setTotal(PopulationArgs total) {
this.total = total;
}
public void setPerYear(HashMap<Integer,PopulationArgs> perYear) {
this.perYear = perYear;
}
}

View File

@@ -0,0 +1,60 @@
package ecoparasite.population;
public class PopulationArgInterval {
private Double min;
private Double max;
private Double mean;
public PopulationArgInterval(Double min, Double max, Double mean) {
this.min = min;
this.max = max;
this.mean = mean;
}
public PopulationArgInterval(Double min, Double max) {
this.min = min;
this.max = max;
this.mean = ( this.max + this.min ) / 2;
}
public Double getMin() {
return this.min;
}
public Double getMax() {
return this.max;
}
public Double getMean() {
return this.mean;
}
public Double transformToDouble(){
if( this.min == this.max )
return this.min;
return this.mean;
}
public static PopulationArgInterval fromString( String rawValue ){
if( rawValue.contains( "±" ) ){ // Plus ou moins.
String[] numbers = rawValue.split("±");
Double mean = Double.parseDouble(numbers[0].trim());
Double interval = Double.parseDouble(numbers[1].trim());
return new PopulationArgInterval(mean - interval, mean + interval, mean);
} else if( rawValue.contains( "-" ) ){ // Entre.
String[] numbers = rawValue.split("-");
Double min = Double.parseDouble(numbers[0].trim());
Double max = Double.parseDouble(numbers[1].trim());
return new PopulationArgInterval(min, max);
} else {
Double number = Double.parseDouble(rawValue);
return new PopulationArgInterval(number, number);
}
}
}

View File

@@ -0,0 +1,124 @@
package ecoparasite.population;
public class PopulationArgs {
private int year;
private int number;
private PopulationArgInterval length;
private PopulationArgInterval width;
private PopulationArgInterval prevalence;
private PopulationArgInterval ic;
private Double intensity;
private Double abondance;
public PopulationArgs(
int N,
PopulationArgInterval length,
PopulationArgInterval width,
PopulationArgInterval prevalence,
PopulationArgInterval ic,
Double intensity,
Double abondance
){
this.year = 0;
this.number = N;
this.length = length;
this.width = width;
this.prevalence = prevalence;
this.ic = ic;
this.intensity = intensity;
this.abondance = abondance;
}
public PopulationArgs(
int year,
int N,
PopulationArgInterval length,
PopulationArgInterval width,
PopulationArgInterval prevalence,
PopulationArgInterval ic,
Double intensity,
Double abondance
){
this.year = year;
this.number = N;
this.length = length;
this.width = width;
this.prevalence = prevalence;
this.ic = ic;
this.intensity = intensity;
this.abondance = abondance;
}
public PopulationArgs(
int year
){
this.year = year;
}
public PopulationArgs(){
this.year = 0;
}
public int getYear() {
return year;
}
public int getNumber() {
return number;
}
public PopulationArgInterval getLength() {
return length;
}
public PopulationArgInterval getWidth() {
return width;
}
public PopulationArgInterval getPrevalence() {
return prevalence;
}
public PopulationArgInterval getIc() {
return ic;
}
public Double getIntensity() {
return intensity;
}
public Double getAbondance() {
return abondance;
}
public void setNumber(int number) {
this.number = number;
}
public void setLength(PopulationArgInterval length) {
this.length = length;
}
public void setWidth(PopulationArgInterval width) {
this.width = width;
}
public void setPrevalence(PopulationArgInterval prevalence) {
this.prevalence = prevalence;
}
public void setIc(PopulationArgInterval ic) {
this.ic = ic;
}
public void setIntensity(Double intensity) {
this.intensity = intensity;
}
public void setAbondance(Double abondance) {
this.abondance = abondance;
}
}

View File

@@ -0,0 +1,97 @@
package ecoparasite.population;
import ecoparasite.input.RawData;
import ecoparasite.input.RawDataOverflow;
import java.util.HashMap;
import java.util.HashSet;
public class PopulationParsing {
public static HashMap<String,Population> parseParasitesPeru(RawData peruRawData ){
HashMap<String,Population> response = new HashMap<>();
int index = 1;
try {
while(true){ // Tant que l'on ne fait pas de débordements d'entrées.
HashMap<String,String> fields = peruRawData.getEntry(index);
String espece = fields.get("Espèce");
System.out.println(espece);
String parametre = fields.get("Paramètre");
Population population = null;
if( response.containsKey( espece ) ){
population = response.get(espece);
} else {
population = new Population(espece);
response.put(espece, population);
}
// Traiter le total
if( fields.containsKey( "Total" ) ){
if( population.getTotal() == null ){
population.setTotal( new PopulationArgs() );
}
PopulationParsing.applyValueForParasitesPeru( population.getTotal(), parametre, fields.get("Total") );
}
// Traiter les années.
for( String k: fields.keySet() ){
if( k.equals( "Total" ) || k.equals("Paramètre") || k.equals("Espèce") ) // Déjà traité. Pas des années.
continue;
Integer year = Integer.parseInt(k);
PopulationArgs popArgsYear = null;
if( !population.getPerYear().containsKey(year) ){
popArgsYear = new PopulationArgs( year );
population.getPerYear().put(year, popArgsYear);
} else {
popArgsYear = population.getPerYear().get(year);
}
PopulationParsing.applyValueForParasitesPeru( popArgsYear, parametre, fields.get(k) );
}
response.put( espece, population );
index++;
}
} catch (RawDataOverflow e){
// Stop.
}
return response;
}
private static void applyValueForParasitesPeru( PopulationArgs populationArgs, String column, String value ){
switch( column ){
case "N":
populationArgs.setNumber(Integer.parseInt(value));
break;
case "Longueur moyenne ± SD (cm)":
populationArgs.setLength( PopulationArgInterval.fromString( value ) );
break;
case "Poids moyen ± SD (g)":
populationArgs.setWidth( PopulationArgInterval.fromString( value ) );
break;
case "Prévalence (%)":
populationArgs.setPrevalence( PopulationArgInterval.fromString( value ) );
break;
case "IC 95%":
populationArgs.setIc( PopulationArgInterval.fromString( value ) );
break;
case "Intensité moyenne (étendue)":
populationArgs.setIntensity( Double.parseDouble( value.split( " " )[0] ) );
break;
case "Abondance moyenne":
populationArgs.setAbondance( Double.parseDouble( value ) );
break;
default:
break;
}
}
}

View File

@@ -1,36 +0,0 @@
package ecoparasite.test;
import static ecoparasite.input.InputFactory.*;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import ecoparasite.input.InputFactory;
import ecoparasite.input.InputFileException;
import ecoparasite.input.RawData;
import org.junit.jupiter.api.Test;
class InputFactoryTest{
@org.junit.jupiter.api.Test
void buildDataPath() {
String d = "data/Campagne/mackerel.97442.csv";
String e = "data/Campagne/merlu2018_75164.csv";
assertArrayEquals(d.toCharArray(), InputFactory.buildDataPath("Campagne/mackerel.97442.csv").toCharArray());
assertArrayEquals(e.toCharArray(), InputFactory.buildDataPath("Campagne/merlu2018_75164.csv").toCharArray());
}
@org.junit.jupiter.api.Test
void readData() throws InputFileException {
RawData test = InputFactory.readData("test.csv");
System.out.println(test.getData());
}
@org.junit.jupiter.api.Test
void testReadData() {
}
}