modifications to add Cohens kappa

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/EcologicalEngineGeoSpatialExtension@76856 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Gianpaolo Coro 2013-06-07 11:07:05 +00:00
parent 900e8ba880
commit 2f172d69d4
1 changed files with 120 additions and 106 deletions

View File

@ -26,21 +26,21 @@ import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.XYSeriesCollection; import org.jfree.data.xy.XYSeriesCollection;
import org.opengis.metadata.Metadata; import org.opengis.metadata.Metadata;
public class MapsComparator extends StandardLocalExternalAlgorithm{ public class MapsComparator extends StandardLocalExternalAlgorithm {
static String layer1 = "LayerTitle_1"; static String layer1 = "LayerTitle_1";
static String layer2 = "LayerTitle_2"; static String layer2 = "LayerTitle_2";
static String zString = "Z"; static String zString = "Z";
static String t1 = "TimeIndex_1"; static String t1 = "TimeIndex_1";
static String t2 = "TimeIndex_2"; static String t2 = "TimeIndex_2";
static String valuesThr = "ValuesComparisonThreshold"; static String valuesThr = "ValuesComparisonThreshold";
protected SessionFactory dbConnection; protected SessionFactory dbConnection;
@Override @Override
public void init() throws Exception { public void init() throws Exception {
AnalysisLogger.getLogger().debug("Initialization"); AnalysisLogger.getLogger().debug("Initialization");
} }
@Override @Override
@ -58,174 +58,188 @@ public class MapsComparator extends StandardLocalExternalAlgorithm{
String valuesthr$ = getInputParameter(valuesThr); String valuesthr$ = getInputParameter(valuesThr);
String time1$ = getInputParameter(t1); String time1$ = getInputParameter(t1);
String time2$ = getInputParameter(t2); String time2$ = getInputParameter(t2);
int time1 = ((time1$!=null) && (time1$.trim().length()>0))? Integer.parseInt(time1$):0; int time1 = ((time1$ != null) && (time1$.trim().length() > 0)) ? Integer.parseInt(time1$) : 0;
int time2 = ((time2$!=null) && (time2$.trim().length()>0))? Integer.parseInt(time2$):0; int time2 = ((time2$ != null) && (time2$.trim().length() > 0)) ? Integer.parseInt(time2$) : 0;
if (time1<0) if (time1 < 0)
time1=0; time1 = 0;
if (time2<0) if (time2 < 0)
time2=0; time2 = 0;
double valuesthreshold = 0.1; double valuesthreshold = 0.1;
if ((valuesthr$!=null)&&(valuesthr$.trim().length()>0)) if ((valuesthr$ != null) && (valuesthr$.trim().length() > 0))
try{valuesthreshold =Double.parseDouble(valuesthr$);}catch(Exception ee){} try {
valuesthreshold = Double.parseDouble(valuesthr$);
} catch (Exception ee) {
}
double z = 0; double z = 0;
if ((z$!=null)&&(z$.trim().length()>0)) if ((z$ != null) && (z$.trim().length() > 0))
try{z =Double.parseDouble(z$);}catch(Exception ee){} try {
try{ z = Double.parseDouble(z$);
} catch (Exception ee) {
}
try {
String scope = config.getGcubeScope(); String scope = config.getGcubeScope();
AnalysisLogger.getLogger().debug("MapsComparator: Using Scope: "+scope+" Z: "+z+" Values Threshold: "+valuesthreshold+" Layer1: "+layerT1+" vs "+layerT2); AnalysisLogger.getLogger().debug("MapsComparator: Using Scope: " + scope + " Z: " + z + " Values Threshold: " + valuesthreshold + " Layer1: " + layerT1 + " vs " + layerT2);
GeoIntersector intersector = new GeoIntersector(scope, config.getConfigPath()); GeoIntersector intersector = new GeoIntersector(scope, config.getConfigPath());
AnalysisLogger.getLogger().debug("MapsComparator: GeoIntersector initialized"); AnalysisLogger.getLogger().debug("MapsComparator: GeoIntersector initialized");
double x1 = -180; double x1 = -180;
double x2 = 180; double x2 = 180;
double y1 = -90; double y1 = -90;
double y2 = 90; double y2 = 90;
status = 10; status = 10;
FeaturesManager fm = intersector.getFeaturer(); FeaturesManager fm = intersector.getFeaturer();
AnalysisLogger.getLogger().debug("MapsComparator: Taking info for the layer: "+layerT1); AnalysisLogger.getLogger().debug("MapsComparator: Taking info for the layer: " + layerT1);
Metadata meta1 = fm.checkForMetadatabyTitle(FeaturesManager.treatTitleForGN(layerT1), layerT1); Metadata meta1 = fm.getGNInfobyUUIDorName(layerT1);
double resolution1 = FeaturesManager.getResolution(meta1); if (meta1==null) throw new Exception("No Correspondence with Layer 1");
AnalysisLogger.getLogger().debug("MapsComparator: Resolution: "+resolution1); double resolution1 = 0;
try {
resolution1 = FeaturesManager.getResolution(meta1);
} catch (Exception e) {
AnalysisLogger.getLogger().debug("MapsComparator: Undefined resolution");
}
AnalysisLogger.getLogger().debug("MapsComparator: Resolution: " + resolution1);
status = 15; status = 15;
AnalysisLogger.getLogger().debug("MapsComparator: Taking info for the layer: "+layerT2); AnalysisLogger.getLogger().debug("MapsComparator: Taking info for the layer: " + layerT2);
Metadata meta2 = fm.checkForMetadatabyTitle(FeaturesManager.treatTitleForGN(layerT2), layerT2); AnalysisLogger.getLogger().debug("MapsComparator: Trying with UUID..." + layerT2);
double resolution2 = FeaturesManager.getResolution(meta2); Metadata meta2 = fm.getGNInfobyUUIDorName(layerT2);
AnalysisLogger.getLogger().debug("MapsComparator: Resolution: "+resolution2); if (meta2==null) throw new Exception("No Correspondence with Layer 2");
status = 20;
//take the lowest resolution to perform the comparison
double resolution = Math.max(resolution1, resolution2);
if (resolution==0)
resolution = 0.5d;
AnalysisLogger.getLogger().debug("MapsComparator: Evaluation Resolution: "+resolution);
double resolution2 = 0;
try {
resolution2 = FeaturesManager.getResolution(meta2);
} catch (Exception e) {
AnalysisLogger.getLogger().debug("MapsComparator: Undefined resolution");
}
AnalysisLogger.getLogger().debug("MapsComparator: Resolution: " + resolution2);
status = 20;
// take the lowest resolution to perform the comparison
double resolution = Math.max(resolution1, resolution2);
if (resolution == 0)
resolution = 0.5d;
AnalysisLogger.getLogger().debug("MapsComparator: Evaluation Resolution: " + resolution);
AnalysisLogger.getLogger().debug("MapsComparator: ****Rasterizing map 1****"); AnalysisLogger.getLogger().debug("MapsComparator: ****Rasterizing map 1****");
double[][] slice1 = intersector.takeTimeSlice(layerT1, time1, x1, x2, y1,y2,z,resolution,resolution); double[][] slice1 = intersector.takeTimeSlice(layerT1, time1, x1, x2, y1, y2, z, resolution, resolution);
AnalysisLogger.getLogger().debug("MapsComparator: Dumping map 1"); AnalysisLogger.getLogger().debug("MapsComparator: Dumping map 1");
status = 30; status = 30;
RasterTable raster1 = new RasterTable(x1, x2, y1, y2, z, resolution, resolution, slice1, config); RasterTable raster1 = new RasterTable(x1, x2, y1, y2, z, resolution, resolution, slice1, config);
raster1.dumpGeoTable(); raster1.dumpGeoTable();
String rastertable1 = raster1.getTablename(); String rastertable1 = raster1.getTablename();
AnalysisLogger.getLogger().debug("MapsComparator: Map 1 was dumped in table: "+rastertable1); AnalysisLogger.getLogger().debug("MapsComparator: Map 1 was dumped in table: " + rastertable1);
status = 40; status = 40;
AnalysisLogger.getLogger().debug("MapsComparator: ****Rasterizing map 2****"); AnalysisLogger.getLogger().debug("MapsComparator: ****Rasterizing map 2****");
double[][] slice2 = intersector.takeTimeSlice(layerT2, time2, x1, x2, y1,y2,z,resolution,resolution); double[][] slice2 = intersector.takeTimeSlice(layerT2, time2, x1, x2, y1, y2, z, resolution, resolution);
AnalysisLogger.getLogger().debug("MapsComparator: Dumping map 2"); AnalysisLogger.getLogger().debug("MapsComparator: Dumping map 2");
status = 50; status = 50;
RasterTable raster2 = new RasterTable(x1, x2, y1, y2, z, resolution, resolution, slice2, config); RasterTable raster2 = new RasterTable(x1, x2, y1, y2, z, resolution, resolution, slice2, config);
raster2.dumpGeoTable(); raster2.dumpGeoTable();
String rastertable2 = raster2.getTablename(); String rastertable2 = raster2.getTablename();
AnalysisLogger.getLogger().debug("MapsComparator: Map 2 was dumped in table: "+rastertable2); AnalysisLogger.getLogger().debug("MapsComparator: Map 2 was dumped in table: " + rastertable2);
status = 60; status = 60;
/* /*
String rastertable1 = "rstr909f60c1d3f1472e9de998e844990724"; * String rastertable1 = "rstr909f60c1d3f1472e9de998e844990724"; String rastertable2 = "rstre52e744c99224de3a1c5354263c6c8d8"; String resolution = "0.5";
String rastertable2 = "rstre52e744c99224de3a1c5354263c6c8d8"; */
String resolution = "0.5";
*/
config.setNumberOfResources(1); config.setNumberOfResources(1);
config.setParam("FirstTable", rastertable1); config.setParam("FirstTable", rastertable1);
config.setParam("SecondTable", rastertable2); config.setParam("SecondTable", rastertable2);
config.setParam("FirstTableCsquareColumn", RasterTable.csquareColumn); config.setParam("FirstTableCsquareColumn", RasterTable.csquareColumn);
config.setParam("SecondTableCsquareColumn",RasterTable.csquareColumn); config.setParam("SecondTableCsquareColumn", RasterTable.csquareColumn);
config.setParam("FirstTableProbabilityColumn", RasterTable.probabilityColumn); config.setParam("FirstTableProbabilityColumn", RasterTable.probabilityColumn);
config.setParam("SecondTableProbabilityColumn", RasterTable.probabilityColumn); config.setParam("SecondTableProbabilityColumn", RasterTable.probabilityColumn);
config.setParam("ComparisonThreshold", ""+valuesthreshold); config.setParam("ComparisonThreshold", "" + valuesthreshold);
AnalysisLogger.getLogger().debug("MapsComparator: Analyzing discrepancy between maps: "+rastertable1+" and "+rastertable2); AnalysisLogger.getLogger().debug("MapsComparator: Analyzing discrepancy between maps: " + rastertable1 + " and " + rastertable2);
DiscrepancyAnalysis da = new DiscrepancyAnalysis(); DiscrepancyAnalysis da = new DiscrepancyAnalysis();
da.setConfiguration(config); da.setConfiguration(config);
da.init(false); da.init(false);
outputParameters = da.analyze(); outputParameters = da.analyze();
outputParameters.put("Resolution", ""+resolution); outputParameters.put("Resolution", "" + resolution);
status = 80; status = 80;
AnalysisLogger.getLogger().debug("MapsComparator: Output: "+outputParameters); AnalysisLogger.getLogger().debug("MapsComparator: Output: " + outputParameters);
//delete the tables // delete the tables
dbConnection = DatabaseUtils.initDBSession(config); dbConnection = DatabaseUtils.initDBSession(config);
AnalysisLogger.getLogger().debug("MapsComparator: Deleting table "+rastertable1); AnalysisLogger.getLogger().debug("MapsComparator: Deleting table " + rastertable1);
DatabaseFactory.executeSQLUpdate(DatabaseUtils.dropTableStatement(rastertable1),dbConnection); DatabaseFactory.executeSQLUpdate(DatabaseUtils.dropTableStatement(rastertable1), dbConnection);
status = 90; status = 90;
AnalysisLogger.getLogger().debug("MapsComparator: Deleting table "+rastertable2); AnalysisLogger.getLogger().debug("MapsComparator: Deleting table " + rastertable2);
DatabaseFactory.executeSQLUpdate(DatabaseUtils.dropTableStatement(rastertable2),dbConnection); DatabaseFactory.executeSQLUpdate(DatabaseUtils.dropTableStatement(rastertable2), dbConnection);
AnalysisLogger.getLogger().debug("MapsComparator: Elapsed: Whole operation completed in "+((double)(System.currentTimeMillis()-t0)/1000d)+"s"); AnalysisLogger.getLogger().debug("MapsComparator: Elapsed: Whole operation completed in " + ((double) (System.currentTimeMillis() - t0) / 1000d) + "s");
}catch(Exception e){ } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
AnalysisLogger.getLogger().debug("MapsComparator: ERROR!: "+e.getLocalizedMessage()); AnalysisLogger.getLogger().debug("MapsComparator: ERROR!: " + e.getLocalizedMessage());
} } finally {
finally{
DatabaseUtils.closeDBConnection(dbConnection); DatabaseUtils.closeDBConnection(dbConnection);
status = 100; status = 100;
} }
} }
@Override @Override
protected void setInputParameters() { protected void setInputParameters() {
addStringInput(layer1,"First Layer Title: The title of a layer indexed in the e-Infrastructure on GeoNetwork","Sarda australis"); addStringInput(layer1, "First Layer Title or UUID: The title or the UUID (preferred) of a layer indexed in the e-Infrastructure on GeoNetwork - You can retrieve it from GeoExplorer", "Sarda australis");
addStringInput(layer2,"Second Layer Title: The title of a second layer indexed in the e-Infrastructure on GeoNetwork","Sarda orientalis"); addStringInput(layer2, "Second Layer Title or UUID: The title or the UUID (preferred) of a second layer indexed in the e-Infrastructure on GeoNetwork - You can retrieve it from GeoExplorer", "Sarda orientalis");
addNumberInput(zString,"value of Z. Default is 0, that means comparison will be at surface level","0"); addNumberInput(zString, "value of Z. Default is 0, that means comparison will be at surface level", "0");
addNumberInput(valuesThr,"A comparison threshold for the values in the map. Null equals to 0.1","0.1"); addNumberInput(valuesThr, "A comparison threshold for the values in the map. Null equals to 0.1", "0.1");
addNumberInput(t1,"First Layer Time Index. The default is the first","0"); addNumberInput(t1, "First Layer Time Index. The default is the first", "0");
addNumberInput(t2,"Second Layer Time Index. The default is the first","0"); addNumberInput(t2, "Second Layer Time Index. The default is the first", "0");
addNumberInput("KThreshold", "Threshold for K-Statistic: over this threshold values will be considered 1 for agreement calculation. Default is 0.5","0.5");
DatabaseType.addDefaultDBPars(inputs); DatabaseType.addDefaultDBPars(inputs);
} }
@Override @Override
public void shutdown() { public void shutdown() {
AnalysisLogger.getLogger().debug("Shutdown"); AnalysisLogger.getLogger().debug("Shutdown");
} }
@Override @Override
public StatisticalType getOutput() { public StatisticalType getOutput() {
//set the output map containing values // set the output map containing values
PrimitiveType p = new PrimitiveType(Map.class.getName(), PrimitiveType.stringMap2StatisticalMap(outputParameters), PrimitiveTypes.MAP, "Discrepancy Analysis",""); PrimitiveType p = new PrimitiveType(Map.class.getName(), PrimitiveType.stringMap2StatisticalMap(outputParameters), PrimitiveTypes.MAP, "Discrepancy Analysis", "");
AnalysisLogger.getLogger().debug("MapsComparator: Producing Gaussian Distribution for the errors"); AnalysisLogger.getLogger().debug("MapsComparator: Producing Gaussian Distribution for the errors");
//build image: // build image:
HashMap<String, Image> producedImages = new HashMap<String, Image>(); HashMap<String, Image> producedImages = new HashMap<String, Image>();
//gaussian // gaussian
XYSeriesCollection xyseriescollection = new XYSeriesCollection(); XYSeriesCollection xyseriescollection = new XYSeriesCollection();
double mean = Double.parseDouble(outputParameters.get("MEAN")); double mean = Double.parseDouble(outputParameters.get("MEAN"));
double variance = Double.parseDouble(outputParameters.get("VARIANCE")); double variance = Double.parseDouble(outputParameters.get("VARIANCE"));
if (variance==0) if (variance == 0)
variance=0.01; variance = 0.01;
AnalysisLogger.getLogger().debug("MapsComparator: Adopting mean:"+ mean+" and variance:"+variance); AnalysisLogger.getLogger().debug("MapsComparator: Adopting mean:" + mean + " and variance:" + variance);
NormalDistributionFunction2D normaldistributionfunction2d = new NormalDistributionFunction2D(mean,variance); NormalDistributionFunction2D normaldistributionfunction2d = new NormalDistributionFunction2D(mean, variance);
org.jfree.data.xy.XYSeries xyseries = DatasetUtilities.sampleFunction2DToSeries(normaldistributionfunction2d, (mean-(2*variance)), (mean+(2*variance)), 121, "Distribution of the Error"); org.jfree.data.xy.XYSeries xyseries = DatasetUtilities.sampleFunction2DToSeries(normaldistributionfunction2d, (mean - (2 * variance)), (mean + (2 * variance)), 121, "Distribution of the Error");
xyseriescollection.addSeries(xyseries); xyseriescollection.addSeries(xyseries);
//end gaussian // end gaussian
JFreeChart chart = GaussianDistributionGraph.createStaticChart(xyseriescollection, mean, variance); JFreeChart chart = GaussianDistributionGraph.createStaticChart(xyseriescollection, mean, variance);
Image image = ImageTools.toImage(chart.createBufferedImage(680, 420)); Image image = ImageTools.toImage(chart.createBufferedImage(680, 420));
producedImages.put("Gaussian Distribution of the Error", image); producedImages.put("Gaussian Distribution of the Error", image);
PrimitiveType images = new PrimitiveType(HashMap.class.getName(), producedImages, PrimitiveTypes.IMAGES, "ErrorRepresentation", "Graphical representation of the error spread"); PrimitiveType images = new PrimitiveType(HashMap.class.getName(), producedImages, PrimitiveTypes.IMAGES, "ErrorRepresentation", "Graphical representation of the error spread");
/* /*
GaussianDistributionGraph graph = new GaussianDistributionGraph("Error Distribution"); * GaussianDistributionGraph graph = new GaussianDistributionGraph("Error Distribution"); graph.mean=mean;graph.variance=variance; graph.render(xyseriescollection);
graph.mean=mean;graph.variance=variance; */
graph.render(xyseriescollection); // end build image
*/
//end build image
AnalysisLogger.getLogger().debug("MapsComparator: Gaussian Distribution Produced"); AnalysisLogger.getLogger().debug("MapsComparator: Gaussian Distribution Produced");
//collect all the outputs // collect all the outputs
LinkedHashMap<String, StatisticalType> map = new LinkedHashMap<String, StatisticalType>(); LinkedHashMap<String, StatisticalType> map = new LinkedHashMap<String, StatisticalType>();
map.put("Result", p); map.put("Result", p);
map.put("Images", images); map.put("Images", images);
//generate a primitive type for the collection // generate a primitive type for the collection
PrimitiveType output = new PrimitiveType(HashMap.class.getName(), map, PrimitiveTypes.MAP, "ResultsMap", "Results Map"); PrimitiveType output = new PrimitiveType(HashMap.class.getName(), map, PrimitiveTypes.MAP, "ResultsMap", "Results Map");
return output; return output;
} }