This commit is contained in:
Gianpaolo Coro 2012-03-05 16:57:28 +00:00
parent 95a6c80482
commit 413871c96b
3 changed files with 302 additions and 77 deletions

View File

@ -10,10 +10,12 @@ import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineCon
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.dataanalysis.ecoengine.configuration.AlgorithmConfiguration;
import org.gcube.dataanalysis.ecoengine.interfaces.Evaluator;
import org.gcube.dataanalysis.ecoengine.models.cores.aquamaps.Hspen;
import org.gcube.dataanalysis.ecoengine.processing.factories.EvaluatorsFactory;
import org.gcube.dataanalysis.ecoengine.utils.DatabaseFactory;
import org.gcube.dataanalysis.ecoengine.utils.DatabaseUtils;
import org.gcube.dataanalysis.ecoengine.utils.Operations;
import org.gcube.dataanalysis.ecoengine.utils.Tuple;
import org.hibernate.SessionFactory;
import org.jfree.data.category.DefaultCategoryDataset;
@ -22,23 +24,27 @@ public class BioClimateAnalysis {
private static String countHighProbabilityCells = "select count(*) from %1$s where probability>%2$s";
private static String countSeaCells = "select count(*) from %1$s as a join %2$s as b on a.oceanarea>0 and a.csquarecode=b.csquarecode and ((a.iceconann<b.iceconann-%3$s or a.iceconann>b.iceconann+%3$s) or " + "(a.salinitymean<b.salinitymean-%3$s or a.salinitymean>b.salinitymean+%3$s) or (a.sstanmean<b.sstanmean-%3$s or a.sstanmean>b.sstanmean+%3$s))";
private static String iceLeakage = "select count(*) from %1$s as a join %2$s as b on a.csquarecode=b.csquarecode and (a.iceconann<b.iceconann) and a.oceanarea>0";
private static String takeRangeOfDepths= "select distinct depthmin, max(depthmax) from %1$s group by depthmin order by depthmin";
private static String countNumberOfSpecies= "count(*) from %1$s where depthmin<%2$s and depthmin>=%3$s and depthmax<%4$s and depthmax>=%5$s";
// private static String takeRangeOfDepths = "select distinct depthmin, max(depthmax) from %1$s group by depthmin order by depthmin";
private static String takeRangeOfDepths = "select distinct depthmin, depthmax from %1$s order by depthmin";
private static String takeRangeOfParam = "select %1$s from %2$s where %1$s is not null %3$s order by %1$s";
private static String countNumberOfSpeciesPerRange = "select count(*) from %1$s where %4$s>%2$s and %4$s<=%3$s ";
private static String countNumberOfSpecies = "select count(*) from %1$s where depthmin<%2$s and depthmin>=%3$s and depthmax<%4$s and depthmax>=%5$s";
private static String countProbabilityPerArea = "select count(*) from %1$s as a join hcaf_s as b on b.%2$s = %3$s and a.probability > #THRESHOLD# and a.csquarecode=b.csquarecode";
public static String salinityDefaultRange = "salinitymin>27.44 and salinitymin<=36.57";
public static String salinityMinFeature = "salinitymin";
private static enum FIELD {
iceconann, sstanmean, salinitymean
};
private static String takeAvgSelection = "select avg(%1$s),%2$s from %3$s %4$s group by %2$s order by %2$s";
private static String [] selectionCriteria = {"faoaream", "lme"};
private static String [] timeseriesNames = {"Ice Conc.", "Sea Surface Temperature","Salinity"};
private static String [] quantitiesNames= {FIELD.iceconann.name(), FIELD.sstanmean.name(),FIELD.salinitymean.name()};
private static String [] selectionFilters= {"where faoaream>0", "where lme>0"};
private static String [] criteriaNames = {"FaoArea", "LME"};
private static String[] selectionCriteria = { "faoaream", "lme" };
private static String[] timeseriesNames = { "Ice Conc.", "Sea Surface Temperature", "Salinity" };
private static String[] quantitiesNames = { FIELD.iceconann.name(), FIELD.sstanmean.name(), FIELD.salinitymean.name() };
private static String[] criteriaFilters = { "where faoaream>0", "where lme>0" };
private static String[] criteriaNames = { "FaoArea", "LME" };
// private static String takeSubHspec = "select %1$s from #CLAUSE# order by %1$s";
private static String meanVal = "select avg(%2$s) from %1$s where oceanarea>0";
@ -71,6 +77,7 @@ public class BioClimateAnalysis {
static int width = 680;
static int height = 420;
static int defaultNumOfFeatureClusters = 10;
public static void main(String[] args) throws Exception {
String configPath = "./cfg/";
@ -81,7 +88,7 @@ public class BioClimateAnalysis {
bioClimate.produceGraphs2D();
}
private static String[] SERIES = { "High Probability Cells Trend (>0.8)", "Number of Changing Cells", "Reducing Ice Concentration Trend", "Average Discrepancy Between Distributions", "Average Trends", "Ice Concentration", "Sea Surface Temperature", "Salinity" };
private static String[] SERIES = { "High Probability Cells Trend (>%1$s)", "Number of Changing Cells", "Reducing Ice Concentration Trend", "Average Discrepancy Between Distributions", "Average Trends", "Ice Concentration", "Sea Surface Temperature", "Salinity" };
public void produceGraphs2D() throws Exception {
DefaultCategoryDataset testpoints = new DefaultCategoryDataset();
@ -98,14 +105,14 @@ public class BioClimateAnalysis {
lineg9.render(testpoints);
}
private List<Image> producedImages;
private HashMap<String,Image> producedImages;
public List<Image> getProducedImages() {
public HashMap<String,Image> getProducedImages() {
return producedImages;
}
private void produceGraphs(String [] csquareTableNames,String[] hspecTableNames) throws Exception {
producedImages = new ArrayList<Image>();
private void produceGraphs(String[] csquareTableNames, String[] hspecTableNames, float threshold) throws Exception {
producedImages = new HashMap<String,Image> ();
int numberOfTrends = highProbabilityCells.length;
// create the datasets...
@ -129,7 +136,6 @@ public class BioClimateAnalysis {
}
}
}
if (doHspecAn) {
@ -137,14 +143,14 @@ public class BioClimateAnalysis {
discrepancies[0] = min;
if (liveRender) {
BioClimateGraph lineg1 = new BioClimateGraph(SERIES[0], Operations.getMax(highProbabilityCells), Operations.getMin(highProbabilityCells));
BioClimateGraph lineg1 = new BioClimateGraph(String.format(SERIES[0],threshold), Operations.getMax(highProbabilityCells), Operations.getMin(highProbabilityCells));
BioClimateGraph lineg4 = new BioClimateGraph(SERIES[3], Operations.getMax(discrepancies), min);
lineg4.render(discrepanciesTrend);
lineg1.render(probabilityTrend);
}
producedImages.add(BioClimateGraph.renderStaticImgObject(width, height, probabilityTrend, SERIES[0], Operations.getMax(highProbabilityCells), Operations.getMin(highProbabilityCells)));
producedImages.add(BioClimateGraph.renderStaticImgObject(width, height, discrepanciesTrend, SERIES[3], Operations.getMax(discrepancies), min));
producedImages.put("Probability_Trend",BioClimateGraph.renderStaticImgObject(width, height, probabilityTrend, String.format(SERIES[0],threshold), Operations.getMax(highProbabilityCells), Operations.getMin(highProbabilityCells)));
producedImages.put("Probability_Discrepancies_Trend",BioClimateGraph.renderStaticImgObject(width, height, discrepanciesTrend, SERIES[3], Operations.getMax(discrepancies), min));
}
if (doHcafAn) {
@ -158,9 +164,9 @@ public class BioClimateAnalysis {
lineg8.render(avgSalinityD);
}
producedImages.add(BioClimateGraph.renderStaticImgObject(width, height, avgIceD, SERIES[5], Operations.getMax(avgIce), Operations.getMin(avgIce)));
producedImages.add(BioClimateGraph.renderStaticImgObject(width, height, avgSSTD, SERIES[6], Operations.getMax(avgSST), Operations.getMin(avgSST)));
producedImages.add(BioClimateGraph.renderStaticImgObject(width, height, avgSalinityD, SERIES[7], Operations.getMax(avgSalinity), Operations.getMin(avgSalinity)));
producedImages.put("Average_Ice_Concentration",BioClimateGraph.renderStaticImgObject(width, height, avgIceD, SERIES[5], Operations.getMax(avgIce), Operations.getMin(avgIce)));
producedImages.put("Average_SST",BioClimateGraph.renderStaticImgObject(width, height, avgSSTD, SERIES[6], Operations.getMax(avgSST), Operations.getMin(avgSST)));
producedImages.put("Average_Salinity",BioClimateGraph.renderStaticImgObject(width, height, avgSalinityD, SERIES[7], Operations.getMax(avgSalinity), Operations.getMin(avgSalinity)));
}
@ -168,44 +174,220 @@ public class BioClimateAnalysis {
}
public void hcafEvolutionAnalysis(String[] hcafTable,String[] hcafTableNames) throws Exception {
globalEvolutionAnalysis(hcafTable, null, hcafTableNames, null, null, null);
public void hcafEvolutionAnalysis(String[] hcafTable, String[] hcafTableNames) throws Exception {
globalEvolutionAnalysis(hcafTable, null, hcafTableNames, null, null, null,0f);
}
public void hspecEvolutionAnalysis(String[] hspecTables, String[] hspecTableNames, String probabilityColumn, String csquareColumn) throws Exception {
globalEvolutionAnalysis(null, hspecTables, null, hspecTableNames, probabilityColumn, csquareColumn);
public void hspecEvolutionAnalysis(String[] hspecTables, String[] hspecTableNames, String probabilityColumn, String csquareColumn,float threshold) throws Exception {
globalEvolutionAnalysis(null, hspecTables, null, hspecTableNames, probabilityColumn, csquareColumn, threshold);
}
private List<Image> producedCharts;
public void produceCharts(HashMap<String,HashMap<String,double []>> GeoMap,String[] hcafTablesNames){
//produce a char for each feature
producedCharts = new ArrayList<Image>();
for (String featurename:GeoMap.keySet()){
public void produceCharts(HashMap<String, HashMap<String, double[]>> GeoMap, String[] tablesNames) {
// produce a char for each feature
producedImages = new HashMap<String,Image> ();
for (String featurename : GeoMap.keySet()) {
DefaultCategoryDataset chart = new DefaultCategoryDataset();
HashMap<String,double []> timeseries = GeoMap.get(featurename);
HashMap<String, double[]> timeseries = GeoMap.get(featurename);
double absmax = -Double.MAX_VALUE;
double absmin = Double.MAX_VALUE;
for (String timeserie:timeseries.keySet()){
for (String timeserie : timeseries.keySet()) {
double[] points = timeseries.get(timeserie);
for (int i=0;i<points.length;i++){
if (points [i]>absmax)
for (int i = 0; i < points.length; i++) {
if (points[i] > absmax)
absmax = points[i];
if (points [i]<absmin)
if (points[i] < absmin)
absmin = points[i];
chart.addValue(points[i], timeserie,hcafTablesNames[i]);
chart.addValue(points[i], timeserie, tablesNames[i]);
}
}
if (liveRender) {
BioClimateGraph lineg1 = new BioClimateGraph(featurename, absmax, absmin);
lineg1.render(chart);
}
producedCharts.add(BioClimateGraph.renderStaticImgObject(width, height, chart, featurename, absmax, absmin));
producedImages.put(featurename.replace(" ", "_"),BioClimateGraph.renderStaticImgObject(width, height, chart, featurename, absmax, absmin));
}
}
/**
* Generates a chart for hspens in time according to a certain interval in the parameter
* e.g. : a chart for several salinity intervals
* @param hspenTables
* @param hspenTableNames
* @param parameterName
* @param condition
* @throws Exception
*/
public void speciesEvolutionAnalysis(String[] hspenTables, String[] hspenTableNames, String parameterName, String condition) throws Exception {
try {
referencedbConnection = DatabaseFactory.initDBConnection(configPath + AlgorithmConfiguration.defaultConnectionFile, config);
AnalysisLogger.getLogger().debug("ReferenceDB initialized");
status = 0f;
int numbOfTables = (hspenTables != null) ? hspenTables.length : 0;
if (numbOfTables > 0) {
// a map for each range of features: depth[0,100] ,depth [100,200]
HashMap<String, HashMap<String, double[]>> GeoMap = new HashMap<String, HashMap<String, double[]>>();
float statusstep = 80f / (float) numbOfTables;
if (condition != null && (condition.length() > 0))
condition = "and " + condition;
// take the spectrum of depths
AnalysisLogger.getLogger().trace("Range query: " + String.format(takeRangeOfParam, parameterName, hspenTables[0], condition));
List<Object> paramrange = DatabaseFactory.executeSQLQuery(String.format(takeRangeOfParam, parameterName, hspenTables[0], condition), referencedbConnection);
int numberOfParams = paramrange.size();
double absolutePMin = Double.parseDouble("" + ((Object) paramrange.get(0)));
double absolutePMax = Double.parseDouble("" + ((Object) paramrange.get(numberOfParams - 1)));
double step = (absolutePMax - absolutePMin) / (double) defaultNumOfFeatureClusters;
int pClusters[] = new int[defaultNumOfFeatureClusters + 1];
pClusters[0] = 0;
for (int i = 1; i < pClusters.length; i++) {
double pToFind = absolutePMin + step * (i + 1);
int k = 0;
for (Object row : paramrange) {
if (Double.parseDouble("" + ((Object) row)) > pToFind)
break;
k++;
}
if (k >= numberOfParams)
k = numberOfParams - 1;
pClusters[i] = k;
}
// for each table
for (int i = 0; i < numbOfTables; i++) {
double pmax = 0;
// for each cluster build up a chart
for (int j = 1; j < pClusters.length; j++) {
double prevpmax = MathFunctions.roundDecimal(Double.parseDouble("" + (Object) paramrange.get(pClusters[j - 1])),2);
pmax = MathFunctions.roundDecimal(Double.parseDouble("" + (Object) paramrange.get(pClusters[j])),2);
if (prevpmax != pmax) {
// take the number of elements for this range
String countSpeciesQuery = String.format(countNumberOfSpeciesPerRange, hspenTables[i], prevpmax, pmax, parameterName);
AnalysisLogger.getLogger().trace("count elements query: " + countSpeciesQuery);
List<Object> elementsInRange = DatabaseFactory.executeSQLQuery(countSpeciesQuery, referencedbConnection);
int nelements = (elementsInRange == null) ? 0 : Integer.parseInt("" + elementsInRange.get(0));
AnalysisLogger.getLogger().trace("Number of elements for " + hspenTables[i] + " in (" + prevpmax + " - " + pmax + ")" + " : " + nelements);
// take the chart for this range
String chartName = parameterName + " envelope for interval (" + prevpmax + " ; " + pmax + ")";
// build the chart
HashMap<String, double[]> submap = GeoMap.get(chartName);
if (submap == null) {
submap = new HashMap<String, double[]>();
GeoMap.put(chartName, submap);
}
String timeseries = "number of species";
double[] elements = submap.get(timeseries);
if (elements == null) {
elements = new double[numbOfTables];
submap.put(timeseries, elements);
}
elements[i] = nelements;
}
}
status = status + statusstep;
}
status = 80f;
produceCharts(GeoMap, hspenTableNames);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
status = 100f;
referencedbConnection.close();
}
}
/**
* Generates a chart for hspec probability > thr in each Fao Area and LME
* @param hspecTables
* @param hspecTablesNames
* @throws Exception
*/
public void speciesGeographicEvolutionAnalysis(String[] hspecTables, String[] hspecTablesNames, float threshold) throws Exception {
try {
referencedbConnection = DatabaseFactory.initDBConnection(configPath + AlgorithmConfiguration.defaultConnectionFile, config);
AnalysisLogger.getLogger().debug("ReferenceDB initialized");
status = 0f;
int numbOfTables = (hspecTables != null) ? hspecTables.length : 0;
if (numbOfTables > 0) {
// a map for each feature. each sub map contains a trend for faoaream, lme etc.
HashMap<String, HashMap<String, double[]>> GeoMap = new HashMap<String, HashMap<String, double[]>>();
float statusstep = 80f / (float) numbOfTables;
// for each table
for (int i = 0; i < numbOfTables; i++) {
// for each criterion to apply: fao area, lme etc.
for (int j = 0; j < criteriaNames.length; j++) {
List<Object> listCriterion = DatabaseFactory.executeSQLQuery(DatabaseUtils.getDinstictElements("hcaf_s", selectionCriteria[j],criteriaFilters[j]),referencedbConnection);
for (Object code: listCriterion){
String code$ = ""+code;
String query = String.format(countProbabilityPerArea,hspecTables[i],selectionCriteria[j],code$);
query = query.replace("#THRESHOLD#", ""+threshold);
AnalysisLogger.getLogger().trace("Executing query for counting probabilities: "+query);
List<Object> counts = DatabaseFactory.executeSQLQuery(query, referencedbConnection);
AnalysisLogger.getLogger().trace("Query Executed");
int countPerArea = (counts==null)?0:Integer.parseInt(""+counts.get(0));
String chartName = "Hspec (prob>0.8) for " + criteriaNames[j] + "_" + code$;
// put the code and the value in the timeseries associated to the feature name
HashMap<String, double[]> submap = GeoMap.get(chartName);
if (submap == null) {
submap = new HashMap<String, double[]>();
GeoMap.put(chartName, submap);
}
String timeseries = "number of occupied cells";
double[] elements = submap.get(timeseries);
if (elements == null) {
elements = new double[numbOfTables];
submap.put(timeseries, elements);
}
elements[i] = countPerArea;
}
}
status = status + statusstep;
}
status = 80f;
produceCharts(GeoMap, hspecTablesNames);
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
status = 100f;
referencedbConnection.close();
}
}
public void geographicEvolutionAnalysis(String[] hcafTable,String []hcafTableNames) throws Exception {
/**
* Generates a geographic trend for each hspec feature: ice con, salinity, sst in each fao area
* @param hcafTable
* @param hcafTableNames
* @throws Exception
*/
public void geographicEvolutionAnalysis(String[] hcafTable, String[] hcafTableNames) throws Exception {
try {
referencedbConnection = DatabaseFactory.initDBConnection(configPath + AlgorithmConfiguration.defaultConnectionFile, config);
AnalysisLogger.getLogger().debug("ReferenceDB initialized");
@ -217,40 +399,40 @@ public class BioClimateAnalysis {
int numbOfTables = (hcafTable != null) ? hcafTable.length : 0;
if (numbOfTables > 0) {
//a map for each feature. each sub map contains a trend for faoaream, lme etc.
HashMap<String,HashMap<String,double []>> GeoMap= new HashMap<String, HashMap<String,double[]>>();
// a map for each feature. each sub map contains a trend for faoaream, lme etc.
HashMap<String, HashMap<String, double[]>> GeoMap = new HashMap<String, HashMap<String, double[]>>();
float statusstep = 80f / (float) numbOfTables;
//for each table
// for each table
for (int i = 0; i < numbOfTables; i++) {
//for each criterion to apply: fao area, lme etc.
for (int j=0;j<criteriaNames.length;j++){
//for each quantity to display: ice concentration
for (int k=0;k<quantitiesNames.length;k++){
String query = String.format(takeAvgSelection, quantitiesNames[k],selectionCriteria[j],hcafTable[i],selectionFilters[j]);
AnalysisLogger.getLogger().debug("Query to be executed : "+query);
//take couples (avg,code)
// for each criterion to apply: fao area, lme etc.
for (int j = 0; j < criteriaNames.length; j++) {
// for each quantity to display: ice concentration
for (int k = 0; k < quantitiesNames.length; k++) {
String query = String.format(takeAvgSelection, quantitiesNames[k], selectionCriteria[j], hcafTable[i], criteriaFilters[j]);
AnalysisLogger.getLogger().debug("Query to be executed : " + query);
// take couples (avg,code)
List<Object> quantityCriterion = DatabaseFactory.executeSQLQuery(query, referencedbConnection);
//for each row
for (Object element: quantityCriterion){
Object [] row = (Object[]) element;
//take avg value
double value = (row[0]==null)?0:Double.parseDouble(""+row[0]);
//take code for criterion
String code = ""+row[1];
String chartName = timeseriesNames[k]+" for "+criteriaNames[j]+"_"+code;
//put the code and the value in the timeseries associated to the feature name
HashMap<String,double []> submap = GeoMap.get(chartName);
if (submap==null){
// for each row
for (Object element : quantityCriterion) {
Object[] row = (Object[]) element;
// take avg value
double value = (row[0] == null) ? 0 : Double.parseDouble("" + row[0]);
// take code for criterion
String code = "" + row[1];
String chartName = timeseriesNames[k] + " for " + criteriaNames[j] + "_" + code;
// put the code and the value in the timeseries associated to the feature name
HashMap<String, double[]> submap = GeoMap.get(chartName);
if (submap == null) {
submap = new HashMap<String, double[]>();
GeoMap.put(chartName, submap);
}
String timeseries = criteriaNames[j]+"_"+code;
double [] elements = submap.get(timeseries);
if (elements==null){
String timeseries = criteriaNames[j] + "_" + code;
double[] elements = submap.get(timeseries);
if (elements == null) {
elements = new double[numbOfTables];
submap.put(timeseries, elements);
}
@ -260,9 +442,9 @@ public class BioClimateAnalysis {
}
status = status + statusstep;
}
status = 80f;
produceCharts(GeoMap,hcafTableNames);
produceCharts(GeoMap, hcafTableNames);
}
} catch (Exception e) {
e.printStackTrace();
@ -273,7 +455,18 @@ public class BioClimateAnalysis {
}
}
public void globalEvolutionAnalysis(String[] hcafTable, String[] hspecTables,String[] hcafTablesNames, String [] hspecTableNames, String probabilityColumn, String csquareColumn) throws Exception {
/**
* Generates a chart for each hspec feature
* Generates a chart for hspec prob > thr and performs a discrepancy analysis on hspec
* @param hcafTable
* @param hspecTables
* @param hcafTablesNames
* @param hspecTableNames
* @param probabilityColumn
* @param csquareColumn
* @throws Exception
*/
public void globalEvolutionAnalysis(String[] hcafTable, String[] hspecTables, String[] hcafTablesNames, String[] hspecTableNames, String probabilityColumn, String csquareColumn,float threshold) throws Exception {
try {
referencedbConnection = DatabaseFactory.initDBConnection(configPath + AlgorithmConfiguration.defaultConnectionFile, config);
AnalysisLogger.getLogger().debug("ReferenceDB initialized");
@ -301,7 +494,7 @@ public class BioClimateAnalysis {
for (int i = 0; i < numbOfPoints; i++) {
if (doHspecAn)
highProbabilityCells[i] = calcHighProbabilityCells(hspecTables[i], 0.8);
highProbabilityCells[i] = calcHighProbabilityCells(hspecTables[i], threshold);
if (doHcafAn) {
avgIce[i] = avgValue(hcafTable[i], FIELD.iceconann.name());
@ -325,7 +518,7 @@ public class BioClimateAnalysis {
status = status + statusstep;
}
status = 80f;
produceGraphs(hcafTablesNames,hspecTableNames);
produceGraphs(hcafTablesNames, hspecTableNames,threshold);
} catch (Exception e) {
e.printStackTrace();
@ -362,8 +555,6 @@ public class BioClimateAnalysis {
return count;
}
public double avgValue(String hcaf1, String field) throws Exception {
List<Object> countage = DatabaseFactory.executeSQLQuery(String.format(meanVal, hcaf1, field), referencedbConnection);

View File

@ -101,6 +101,10 @@ public class DatabaseUtils {
return "drop table " + table;
}
public static String getDinstictElements(String table, String column,String filter) {
return "select distinct " + column + " from " + table + " " + filter+" order by " + column;
}
public static String getOrderedElements(String table, String key, String column) {
return "select " + key + "," + column + " from " + table + " order by " + key;
}

View File

@ -0,0 +1,30 @@
package org.gcube.dataanalysis.ecoengine.utils;
import java.io.Serializable;
import java.util.ArrayList;
public class Tuple <A> implements Serializable{
private static final long serialVersionUID = 1L;
ArrayList<A> elements;
public Tuple(A... elements){
this.elements = new ArrayList<A>();
for (A element:elements){
this.elements.add(element);
}
}
public ArrayList<A> getElements(){
return elements;
}
public String toString(){
String s = "";
for (A element:elements){
s+=element+" ";
}
return s;
}
}