This commit is contained in:
Gianpaolo Coro 2012-02-23 17:10:56 +00:00
parent f3febed58f
commit 297676de1e
89 changed files with 12686 additions and 1232843 deletions

View File

@ -2,24 +2,33 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre6"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/lexicalMatcher1.2.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/log4j-1.2.16.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/hibernate3.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/jaxen-1.1.2.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/slf4j-log4j12-1.6.0.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/slf4j-api-1.6.0.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/c3p0-0.9.1.2.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/hibernate-spatial-postgis-1.0-M2.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/hibernate-spatial-1.0-M2.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/jta-1.1.jar"/>
<classpathentry kind="lib" path="C:/Users/coro/Desktop/WorkFolder/Workspace/StatisticalLibSupportLibraries/lib/jts-1.10.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/postgis-2.0.0SVN.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/commons-collections-3.1.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/commons-logging-1.0.4.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/postgresql-8.4-702.jdbc4.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/gson-1.7.1.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/StatisticsExtractor.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/RapidMiner_Wasat"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/c3p0-0.9.1.2.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/commons-collections-3.1.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/commons-httpclient-3.1.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/commons-logging-1.0.4.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/gson-1.7.1.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/hibernate-spatial-1.0-M2.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/hibernate-spatial-postgis-1.0-M2.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/hibernate3.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/jaxen-1.1.2.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/jcommon-1.0.16.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/jfreechart-1.0.13.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/jgraph.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/jgrapht-jdk-1.5.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/jta-1.1.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/jts-1.10.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/log4j-1.2.16.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/mysql-connector-java-5.1.12-bin.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/postgis-2.0.0SVN.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/postgis-driver-1.0.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/postgresql-8.4-702.jdbc4.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/rapidMiner.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/slf4j-api-1.6.0.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/slf4j-log4j12-1.6.0.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/TChartSupport.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/TGGraphLayout.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/xpp3_min-1.1.4c.jar"/>
<classpathentry kind="lib" path="/StatisticalLibSupportLibraries/lib/EcologicalEngine/xstream-1.3.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1 +1 @@
Used as a Web service in the gCube Framework
Used as a library in the gCube Framework

2
README
View File

@ -14,7 +14,7 @@ Authors
Version and Release Date
------------------------
version 1.0.0 (20-04-2011)
version 1.0.0 (23-02-2012)
Description
--------------------

View File

2620
cfg/operators.xml Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,7 @@
<ReleaseNotes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="xsd/changelog.xsd">
<Changeset component="org.gcube.content-management.ecological-modelling.1-0-0"
date="2011-04-20">
<Changeset component="org.gcube.content-management.ecological-engine.1-0-0"
date="2012-02-23">
<Change>First Release</Change>
</Changeset>
</ReleaseNotes>

View File

@ -1,4 +1,4 @@
name = EcologicalModelling
package = org.gcube.application.aquamaps.ecomodelling.generators
# Where the jar will be stored
lib.dir = Dependencies/org.gcube.application.aquamaps.ecomodelling.generators
lib.dir = Dependencies/org.gcube.application.aquamaps.ecologicalengine

View File

@ -4,15 +4,15 @@
<ID />
<Type>Library</Type>
<Profile>
<Description>Ecological Modelling Library</Description>
<Description>Ecological Engine Library</Description>
<Class>ContentManagement</Class>
<Name>EcologicalModelling</Name>
<Name>EcologicalEngine</Name>
<Version>1.0.0</Version>
<Packages>
<Software>
<Description>Ecological Modelling Library</Description>
<Name>EcologicalModelling</Name>
<Version>1.2.0</Version>
<Description>Ecological Engine Library</Description>
<Name>EcologicalEngine</Name>
<Version>1.0.0</Version>
<Dependencies>
<Dependency>
<Service>
@ -102,28 +102,6 @@
<Scope level="GHN" />
<Optional>false</Optional>
</Dependency>
<Dependency>
<Service>
<Class>ContentManagement</Class>
<Name>LexicalMatcher</Name>
<Version>1.0.0</Version>
</Service>
<Package>LexicalMatcher</Package>
<Version>1.2.0</Version>
<Scope level="GHN" />
<Optional>false</Optional>
</Dependency>
<Dependency>
<Service>
<Class>ContentManagement</Class>
<Name>StatisticsExtractor</Name>
<Version>1.0.0</Version>
</Service>
<Package>StatisticsExtractor</Package>
<Version>1.2.0</Version>
<Scope level="GHN"/>
<Optional>false</Optional>
</Dependency>
<Dependency>
<Service>
<Class>ExternalSoftware</Class>
@ -168,6 +146,17 @@
<Scope level="GHN"/>
<Optional>false</Optional>
</Dependency>
<Dependency>
<Service>
<Class>ExternalSoftware</Class>
<Name>jfreechart</Name>
<Version>1.0.13</Version>
</Service>
<Package>jfreechart</Package>
<Version>1.0.13</Version>
<Scope level="GHN"/>
<Optional>false</Optional>
</Dependency>
</Dependencies>
<Files>
<File>lib/org.gcube.application.aquamaps.ecomodelling.generators.jar</File>

View File

@ -0,0 +1,12 @@
package org.gcube.contentmanagement.graphtools.abstracts;
import java.util.Map;
public interface GenericDBExtractor {
public SamplesTable getMonoDimTable (String table, String column);
public Map<String, SamplesTable> getMultiDimTemporalTables(String table, String xDimension, String groupDimension, String yValue, String speciesColumn, String... yFilters);
}

View File

@ -0,0 +1,185 @@
package org.gcube.contentmanagement.graphtools.abstracts;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import org.gcube.contentmanagement.graphtools.data.conversions.ImageTools;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphData;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
import org.gcube.portlets.user.timeseries.charts.support.types.Point;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.Dataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/*
* Converts a GraphData into a graphicable structure DataSet
* GenericStandaloneGraph: GraphData -> DataSet
*/
public abstract class GenericStandaloneGraph extends ApplicationFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
protected boolean big;
public GenericStandaloneGraph(String title) {
super(title);
big = false;
}
abstract protected Dataset generateDataset();
abstract protected JFreeChart createChart(Dataset dataset);
abstract protected GenericStandaloneGraph getInstance(String title);
public void render(Dataset set) {
render(-1, -1, set);
}
public void render(double x, double y, Dataset set) {
JFreeChart chart = createChart(set);
JPanel jp = new ChartPanel(chart);
this.setContentPane(jp);
this.pack();
if (big)
this.setBounds(0, 0, (int) this.getBounds().getWidth() * 2, (int) this.getBounds().getHeight() * 2);
if ((x == -1) || (y == -1))
RefineryUtilities.centerFrameOnScreen(this);
else
RefineryUtilities.positionFrameOnScreen(this, x, y);
this.setVisible(true);
}
public List<Image> renderGraphGroupImage(int width, int height, GraphGroups graphgroups) {
ArrayList<Image> images = new ArrayList<Image>();
Map<String, GraphData> graphmap = graphgroups.getGraphs();
double x = 0;
double y = 0;
double max = 1;
// int numberOfGraphs = graphmap.size();
for (String key : graphmap.keySet()) {
GenericStandaloneGraph graph = getInstance(key);
Dataset datas = graph.convert2Dataset(graphmap.get(key));
images.add(graph.renderImgObject(width, height, datas));
x += 0.1;
y += 0.1;
if (x > max || y > max) {
x = 0;
y = 0;
}
}
return images;
}
public void renderImages(String filePath, int width, int height, GraphGroups graphgroups) {
List<Image> images = renderGraphGroupImage(width,height,graphgroups);
int i=0;
for (Image img:images){
BufferedImage bimage = ImageTools.toBufferedImage(img);
File outputfile = new File(filePath+"_"+i+".png");
try{
ImageIO.write(bimage, "png", outputfile);
}catch(Exception e){
AnalysisLogger.getLogger().error("renderImages->Error in writing files ",e);
}
i++;
}
}
public Image renderImgObject(int width, int height, Dataset set) {
JFreeChart chart = createChart(set);
/*
JPanel jp = new ChartPanel(chart);
this.setContentPane(jp);
this.pack();
*/
// Image image = this.createImage(width, height);
Image image = ImageTools.toImage(chart.createBufferedImage(width, height));
return image;
}
public void renderGraphGroup(GraphGroups graphgroups) {
Map<String, GraphData> graphmap = graphgroups.getGraphs();
double x = 0;
double y = 0;
double max = 1;
// int numberOfGraphs = graphmap.size();
for (String key : graphmap.keySet()) {
GenericStandaloneGraph graph = getInstance(key);
Dataset datas = graph.convert2Dataset(graphmap.get(key));
graph.render(x, y, datas);
x += 0.1;
y += 0.1;
if (x > max || y > max) {
x = 0;
y = 0;
}
}
}
protected Dataset convert2Dataset(GraphData st) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
List<Point<? extends Number, ? extends Number>> pointslist = st.getData();
// NOTE: after the graph generation graphs are inverted in x and y
int numbOfRows = pointslist.size();
if (numbOfRows > 0) {
int numbOfCols = pointslist.get(0).getEntries().size();
for (int x = 0; x < numbOfRows; x++) {
String xlabel = pointslist.get(x).getLabel();
for (int y = 0; y < numbOfCols; y++) {
double value = pointslist.get(x).getEntries().get(y).getValue().doubleValue();
String ylabel = pointslist.get(x).getEntries().get(y).getLabel();
// System.out.println("ADDING : "+value+" , "+ylabel+" , "+xlabel);
dataset.addValue(value, xlabel, ylabel);
}
}
}
return dataset;
}
}

View File

@ -0,0 +1,156 @@
package org.gcube.contentmanagement.graphtools.abstracts;
import java.util.LinkedList;
import java.util.List;
import com.rapidminer.example.Attribute;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.example.table.AttributeFactory;
import com.rapidminer.example.table.DoubleArrayDataRow;
import com.rapidminer.example.table.MemoryExampleTable;
import com.rapidminer.tools.Ontology;
public abstract class SamplesTable {
// attributes = columns of numbers
public double minY = 0;
public double maxY = 0;
public double minX = 0;
public double maxX = 0;
public ExampleSet generateExampleSet() {
// create attribute list
List<Attribute> attributes = new LinkedList<Attribute>();
// generate columns for attributes
for (int a = 0; a < getNumOfAttributes(); a++) {
attributes.add(AttributeFactory.createAttribute("att" + a, Ontology.REAL));
}
// add a label column
Attribute label = AttributeFactory.createAttribute("label", Ontology.NOMINAL);
attributes.add(label);
// create table
MemoryExampleTable table = new MemoryExampleTable(attributes);
// fill table (here : only real values )
for (int d = 0; d < getNumOfDataRows(); d++) {
// generate a row of double values
double[] data = new double[attributes.size()];
// fill rows data
for (int a = 0; a < getNumOfAttributes(); a++) {
// all with proper data here
data[a] = getValue(d, a);
}
// maps the nominal classifcation to a double value
data[data.length - 1] = label.getMapping().mapString(getClassification(d));
// add data row
table.addDataRow(new DoubleArrayDataRow(data));
}
// create example set
ExampleSet exampleSet = table.createExampleSet(label);
return exampleSet;
}
public void generateSampleTable(ExampleSet es) {
MemoryExampleTable met = (MemoryExampleTable) es.getExampleTable();
int numofcolumns = met.getAttributeCount();
int numofrows = met.size();
// System.out.println("COL "+numofcolumns+" ROWS "+numofrows);
for (int i = 0; i < numofrows; i++) {
Attribute labelAtt = met.getAttribute(numofcolumns - 1);
int index = (int) met.getDataRow(i).get(labelAtt);
String label = labelAtt.getMapping().mapIndex(index);
addSampleRow(label, 0);
// addLabel(i,label);
for (int j = 0; j < numofcolumns - 1; j++) {
Attribute att = AttributeFactory.createAttribute("att" + j, Ontology.REAL);
att.setTableIndex(j);
// System.out.println("ADDING TO " + i+","+j);
DoubleArrayDataRow dadr = (DoubleArrayDataRow) met.getDataRow(i);
double element = dadr.get(att);
addSample(i, j, element);
}
}
}
abstract public int getNumOfAttributes();
abstract public int getNumOfDataRows();
abstract public double getValue(int d, int a);
abstract public String getClassification(int d);
public String toString() {
StringBuffer bs = new StringBuffer();
bs.append("NUMBER OF ROWS: " + getNumOfDataRows() + "\n");
bs.append("NUMBER OF COLUMNS: " + getNumOfAttributes() + "\n");
for (int i = 0; i < getNumOfDataRows(); i++) {
bs.append("ROW " + i + " : ");
bs.append("LABEL " + getClassification(i) + " : ");
for (int j = 0; j < getNumOfAttributes(); j++) {
bs.append(getValue(i, j) + "\t");
}
bs.append("\n");
}
return bs.toString();
}
public void calculateBounds() {
int Ylen = getNumOfAttributes();
int Xlen = getNumOfDataRows();
for (int i = 0; i < Xlen; i++) {
for (int j = 0; j < Ylen; j++) {
double localmin = minY;
double localmax = maxY;
if (j == 0) {
localmin = minX;
localmax = maxX;
}
double point = getValue(i, j);
if (point < localmin) {
localmin = point;
} else if (point > localmax) {
localmax = point;
}
if (j == 0) {
minX = localmin;
maxX = localmax;
} else {
minY = localmin;
maxY = localmax;
}
}
}
}
abstract public void addLabel(int i, String label);
abstract public void addSample(int i, int j, double value);
abstract public void addSampleRow(String label, double... values);
}

View File

@ -0,0 +1,192 @@
package org.gcube.contentmanagement.graphtools.core;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.gcube.contentmanagement.graphtools.abstracts.SamplesTable;
import org.gcube.contentmanagement.graphtools.core.filters.Filter;
import org.gcube.contentmanagement.graphtools.data.GraphSamplesTable;
import org.gcube.contentmanagement.graphtools.data.conversions.GraphConverter2D;
import org.gcube.contentmanagement.graphtools.data.databases.CommonDBExtractor;
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.contentmanagement.lexicalmatcher.utils.DatabaseFactory;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphData;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
import org.gcube.portlets.user.timeseries.charts.support.types.Point;
import org.hibernate.SessionFactory;
import com.rapidminer.RapidMiner;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.operator.preprocessing.sampling.AbsoluteSampling;
import com.rapidminer.tools.OperatorService;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class StatisticsGenerator {
public static void main(String[] args) throws Exception {
String table = "ts_161efa00_2c32_11df_b8b3_aa10916debe6";
String xDimension = "field5";
String yDimension = "field6";
String groupDimension = "field1";
String speciesColumn = "field3";
String filter1 = "Brown seaweeds";
String filter2 = "River eels";
StatisticsGenerator stg = new StatisticsGenerator();
LexicalEngineConfiguration conf = new LexicalEngineConfiguration();
// database Parameters
conf.setDatabaseUserName("root");
// conf.setDatabasePassword("password");
conf.setDatabaseDriver("com.mysql.jdbc.Driver");
conf.setDatabaseURL("jdbc:mysql://localhost/timeseries");
conf.setDatabaseDialect("org.hibernate.dialect.MySQLDialect");
conf.setDatabaseAutomaticTestTable("connectiontesttable");
conf.setDatabaseIdleConnectionTestPeriod("3600");
// stg.init("./cfg/");
stg.init("./cfg/", conf);
stg.generateGraphs(3, table, xDimension, yDimension, groupDimension, speciesColumn, filter1, filter2);
}
SessionFactory referenceDBSession;
CommonDBExtractor extractor;
private static final String LogFile = "ALog.properties";
private static final String HibFile = "hibernate.cfg.xml";
private static final String OperatorsFile = "operators.xml";
private List<Filter> ColumnFilters;
private Filter XRangeFilter;
private Filter YRangeFilter;
private XStream xStream;
public void init(String cfgPath) throws Exception {
init(cfgPath, null);
}
public SessionFactory getDBSession() {
return this.referenceDBSession;
}
public void init(String cfgPath, LexicalEngineConfiguration config) throws Exception {
AnalysisLogger.setLogger(cfgPath + "/" + LogFile);
if (config == null)
referenceDBSession = DatabaseFactory.initDBConnection(cfgPath + HibFile);
else
referenceDBSession = DatabaseFactory.initDBConnection(cfgPath + HibFile, config);
ColumnFilters = new ArrayList<Filter>();
extractor = new CommonDBExtractor(referenceDBSession);
AnalysisLogger.getLogger().info("StatisticsGenerator->initialization complete");
System.setProperty("rapidminer.init.operators", cfgPath + OperatorsFile);
xStream = new XStream(new DomDriver());
RapidMiner.init();
}
public void resetFilters(){
ColumnFilters = new ArrayList<Filter>();
}
public void addColumnFilter(String column, String element, String operator) {
ColumnFilters.add(new Filter(column, element, operator));
}
public void addColumnFilter(String column, String element) {
ColumnFilters.add(new Filter(column, element));
}
public void addXRangeFilter(String xmin, String xmax) {
XRangeFilter = new Filter(xmin, xmax);
}
public void addYRangeFilter(String ymin, String ymax) {
YRangeFilter = new Filter(ymin, ymax);
}
public GraphGroups generateGraphs(int maxElements, String timeSeriesTable, String xDimension, String yDimension, String groupDimension, String speciesColumn, String... filters) throws Exception {
Map<String, SamplesTable> samplesMap = extractor.getMultiDimTemporalTables(ColumnFilters, YRangeFilter, timeSeriesTable, xDimension, groupDimension, yDimension, speciesColumn, filters);
AnalysisLogger.getLogger().info("StatisticsGenerator-> samplesMap has been generated");
AnalysisLogger.getLogger().trace(samplesMap.toString());
// setup Absolute Sampling operator
AbsoluteSampling asop = (AbsoluteSampling) OperatorService.createOperator("AbsoluteSampling");
asop.setParameter("sample_size", "" + maxElements);
asop.setParameter("local_random_seed", "-1");
// setup graphgroups
GraphGroups graphgroups = new GraphGroups();
int i = 1;
// for each samples table perform processing
for (String key : samplesMap.keySet()) {
// get samples table
SamplesTable stable = samplesMap.get(key);
// transform samples table into a list of points
List<Point<? extends Number, ? extends Number>> singlegraph = GraphConverter2D.transformTable(stable);
// filter XRange if necessary
if (XRangeFilter != null) {
singlegraph = GraphConverter2D.filterXRange(singlegraph, XRangeFilter.getFirstElement(), XRangeFilter.getSecondElement());
}
// setup the graph samples table to perform mining processing
GraphSamplesTable graphSamples = new GraphSamplesTable(singlegraph);
// if there are too many samples, perform downsampling
if (graphSamples.getNumOfDataRows() > maxElements) {
// generate an Example Set for Rapid Miner
ExampleSet es = graphSamples.generateExampleSet();
// apply Sampling
es = asop.apply(es);
// generate a new graph samples table
graphSamples = new GraphSamplesTable();
graphSamples.generateSampleTable(es);
// get the points list from the graph samples table
singlegraph = graphSamples.getGraph();
AnalysisLogger.getLogger().trace("Regeneration\n" + graphSamples.toString());
}
// reorder the elements of the points list
// this steps performs re-enumeration and reordering of the rows after the sampling operations
singlegraph = GraphConverter2D.reorder(singlegraph);
AnalysisLogger.getLogger().trace("Reordering\n" + singlegraph.toString());
if ((singlegraph != null)&&(singlegraph.size()>0)) {
// build up the GraphData for visualization
GraphData grd = new GraphData(singlegraph, true);
// calculate the bounds of the graph
graphSamples.calculateBounds();
// set the bounds
grd.setMaxY(graphSamples.maxY);
grd.setMinY(graphSamples.minY);
// add the points list
graphgroups.addGraph("Distribution for " + key, grd);
AnalysisLogger.getLogger().trace("StatisticsGenerator-> graphgroup " + i + " generated with key: " + key);
i++;
}
}
AnalysisLogger.getLogger().info("StatisticsGenerator-> graphgroups have been generated");
return graphgroups;
}
public String generateStringGraphs(int maxElements, String timeSeriesTable, String xDimension, String yDimension, String groupDimension, String speciesColumn, String... filters) throws Exception {
GraphGroups gg = generateGraphs(maxElements, timeSeriesTable, xDimension, yDimension, groupDimension, speciesColumn, filters);
return xStream.toXML(gg);
}
}

View File

@ -0,0 +1,71 @@
package org.gcube.contentmanagement.graphtools.core.filters;
public class Filter {
String firstElement;
String secondElement;
String operator;
public Filter(String first,String second, String operator){
setFirstElement(first);
setSecondElement(second);
setOperator(operator);
}
public Filter(String first,String second){
setFirstElement(first);
setSecondElement(second);
setOperator("=");
}
public void setFirstElement(String first){
firstElement = first;
}
public void setSecondElement(String second){
secondElement = second;
}
public void setOperator(String oper){
operator = oper;
}
public double getFirstNumber() {
double d = 0;
try {
d = Double.parseDouble(firstElement);
} catch (Exception e) {
}
return d;
}
public double getSecondNumber() {
double d = 0;
try {
d = Double.parseDouble(secondElement);
} catch (Exception e) {
}
return d;
}
public String getFirstElement() {
return firstElement;
}
public String getSecondElement() {
return secondElement;
}
public String toString(String logicoperator){
return logicoperator+" "+firstElement+operator+"'"+secondElement+"' ";
}
public String toString(){
return "or "+firstElement+operator+"'"+secondElement+"' ";
}
}

View File

@ -0,0 +1,85 @@
package org.gcube.contentmanagement.graphtools.data;
import java.util.HashMap;
import org.gcube.contentmanagement.graphtools.abstracts.SamplesTable;
public class BigSamplesTable extends SamplesTable {
BigSparseTable table;
HashMap<Integer, String> classifications;
Integer currentIndex;
public BigSamplesTable() {
table = new BigSparseTable();
classifications = new HashMap<Integer, String>();
currentIndex = 0;
}
@Override
public int getNumOfAttributes() {
return table.width().intValue();
}
@Override
public int getNumOfDataRows() {
return table.size().intValue();
}
@Override
public double getValue(int d, int a) {
return table.get(d, a);
}
@Override
public String getClassification(int d) {
return classifications.get(d);
}
public void addSampleRow(String label, double... values) {
classifications.put(currentIndex, label);
int j = 0;
for (Double value : values) {
table.add(currentIndex, j, value);
j++;
}
currentIndex = currentIndex + 1;
}
public void addSample(int i, int j, double value) {
if (i < currentIndex)
table.add(i, j, value);
}
public void addLabel(int i, String label) {
if (i < currentIndex)
classifications.put(i, label);
}
public static void main(String[] args){
BigSamplesTable bst = new BigSamplesTable();
bst.addSampleRow("prova 1", 10, 12,13,14,15);
bst.addSampleRow("prova 2", 20, 15,14,15);
bst.addSampleRow("prova 3", 30, 11,110,150);
bst.addSample(0, -1,150);
System.out.println(bst.toString());
bst.generateExampleSet();
}
}

View File

@ -0,0 +1,82 @@
package org.gcube.contentmanagement.graphtools.data;
import java.util.HashMap;
public class BigSparseTable {
HashMap<Integer, HashMap<Integer, Double>> table;
Integer tableSize;
Integer tableWidth;
public BigSparseTable() {
table = new HashMap<Integer, HashMap<Integer, Double>>();
tableSize = 0;
tableWidth = 0;
}
public Integer size() {
return tableSize;
}
public Integer width() {
return tableWidth;
}
public void add(Integer i, Integer j, double value) {
// System.out.println("ADDING " + i + "," + j);
if ((i<0)||(j<0))
return;
double val = get(i, j);
if (val != 0) {
table.get(i).put(j, value);
}
else {
HashMap<Integer, Double> row;
// if size<=i create a new hashmap
if (tableSize <= i) {
row = new HashMap<Integer, Double>();
table.put(i, row);
tableSize = i + 1;
}
// else get i-th hashmap
else {
row = table.get(i);
if (row == null) {
row = new HashMap<Integer, Double>();
table.put(i, row);
}
}
row.put(j, value);
if (tableWidth <= j)
tableWidth = j + 1;
}
}
// default is 0
public double get(Integer i, Integer j) {
Double value = null;
if (tableSize.compareTo(i) > 0) {
value = table.get(i).get(j);
}
if (value == null)
value = Double.valueOf(0);
return value;
}
}

View File

@ -0,0 +1,178 @@
package org.gcube.contentmanagement.graphtools.data;
import java.util.ArrayList;
import java.util.List;
import org.gcube.contentmanagement.graphtools.abstracts.SamplesTable;
import org.gcube.portlets.user.timeseries.charts.support.types.Point;
import org.gcube.portlets.user.timeseries.charts.support.types.ValueEntry;
/*
* Represents a graphicable SamplesTable to be converted into a GraphData
* Performs transformation from GraphData to SamplesTable
* GraphConverter2D : SamplesTable -> GraphData
* GraphSamplesTable: GraphData -> SamplesTable
*/
/*
*
* Structure of a transposed graph coming from common structure library
*
* Point 1 (Series1, DefaultValue1) -> (y1,xLabel1) (y2,xLabel2) (y3,xLabel3) ..
* Point 2 (Series2, DefaultValue2) -> (y1,xLabel1) (y2,xLabel2) (y3,xLabel3) ..
* ...
*
* Structure of a not-transposed graph coming from database
*
* Point 1 (xLabel1,EnumeratedValue1) -> (y1,Series1) (y2,Series2) (y3,Series3) ..
* Point 2 (xLabel2,EnumeratedValue2) -> (y1,Series1) (y2,Series2) (y3,Series3) ..
* ...
*/
public class GraphSamplesTable extends SamplesTable {
List<Point<? extends Number, ? extends Number>> singlegraph;
public GraphSamplesTable(List<Point<? extends Number, ? extends Number>> graph) {
super();
singlegraph = graph;
}
public GraphSamplesTable() {
super();
singlegraph = new ArrayList<Point<? extends Number, ? extends Number>>();
}
// builds up a graph from a set of values and lables
// used for building up graphs from simple data
public GraphSamplesTable(String seriesName, List<String> xLables, List<Double> yValues,boolean invert) {
super();
singlegraph = new ArrayList<Point<? extends Number, ? extends Number>>();
int size = xLables.size();
try {
if (invert){
Point p = new Point<Number, Number>(seriesName, Double.valueOf(0));
for (int i = 0; i < size; i++) {
ValueEntry v = new ValueEntry(xLables.get(i), yValues.get(i));
p.addEntry(v);
}
singlegraph.add(p);
}
else{
for (int i = 0; i < size; i++) {
Point p = new Point<Number, Number>(xLables.get(i),new Double(i));
ValueEntry v = new ValueEntry("series1", yValues.get(i));
p.addEntry(v);
singlegraph.add(p);
}
}
} catch (Exception e) {
}
}
@Override
public int getNumOfAttributes() {
if (singlegraph.size() > 0)
return singlegraph.get(0).getEntries().size() + 1;
else
return 0;
}
@Override
public int getNumOfDataRows() {
return singlegraph.size();
}
@Override
public double getValue(int d, int a) {
Double doub;
if (a == 0)
doub = (Double) singlegraph.get(d).getValue();
else
doub = (Double) singlegraph.get(d).getEntries().get(a - 1).getValue();
return doub.doubleValue();
}
@Override
public String getClassification(int d) {
StringBuffer sb = new StringBuffer();
int numbOfColumns = getNumOfAttributes();
sb.append(singlegraph.get(d).getLabel() + ";");
for (int i = 0; i < numbOfColumns - 1; i++) {
sb.append(singlegraph.get(d).getEntries().get(i).getLabel());
if (i < numbOfColumns - 2)
sb.append(";");
}
return sb.toString();
}
@Override
public void addLabel(int i, String label) {
singlegraph.get(i).setLabel(label);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void addSample(int i, int j, double value) {
try {
if (j == 0) {
Point p = singlegraph.get(i);
p.setValue(Double.valueOf(value));
} else {
Point<Double, Double> p = (Point<Double, Double>) singlegraph.get(i);
p.getEntries().get(j - 1).setValue(new Double(value));
}
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
@Override
public void addSampleRow(String label, double... values) {
try {
Double x = values[0];
Double y1 = Double.valueOf(0);
if (values.length > 1)
y1 = values[1];
String[] lables = label.split(";");
int labsize = lables.length;
String labelx = lables[0];
String labely1 = lables[1];
ValueEntry<Double> ve = new ValueEntry<Double>(labely1, y1);
Point<Double, Double> p = new Point<Double, Double>(labelx, x, ve);
for (int j = 2; j < labsize; j++) {
Double y = new Double(0);
if (values.length > j)
y = values[j];
p.getEntries().add(new ValueEntry<Double>(lables[j], y));
}
singlegraph.add(p);
} catch (Exception e) {
e.printStackTrace();
}
}
public List<Point<? extends Number, ? extends Number>> getGraph() {
return singlegraph;
}
}

View File

@ -0,0 +1,410 @@
package org.gcube.contentmanagement.graphtools.data.conversions;
import java.util.ArrayList;
import java.util.List;
import org.gcube.contentmanagement.graphtools.abstracts.SamplesTable;
import org.gcube.contentmanagement.graphtools.utils.MathFunctions;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphData;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
import org.gcube.portlets.user.timeseries.charts.support.types.Point;
import org.gcube.portlets.user.timeseries.charts.support.types.ValueEntry;
/*
* Performs Operations on Lists of Points
* Helps in transforming a SamplesTable to a GraphData
*/
public class GraphConverter2D {
private static Point<? extends Number, ? extends Number> searchPoint(Point<? extends Number, ? extends Number> x, List<Point<? extends Number, ? extends Number>> samples) {
Point<? extends Number, ? extends Number> novelpoint = null;
for (Point<? extends Number, ? extends Number> point : samples) {
// if (point.getValue().equals(x.getValue())) {
if (point.getLabel().equals(x.getLabel())) {
novelpoint = point;
break;
}
}
return novelpoint;
}
// optimizes the dimensions of the sample table, ordering the x and y entries
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List<Point<? extends Number, ? extends Number>> reduceDimension(List<Point<? extends Number, ? extends Number>> samples) {
ArrayList<Point<? extends Number, ? extends Number>> novelSamples = new ArrayList<Point<? extends Number, ? extends Number>>();
int novelCounter = 0;
try {
for (Point<? extends Number, ? extends Number> pointsample : samples) {
// search the current point in the new built list
Point<? extends Number, ? extends Number> novelPoint = searchPoint(pointsample, novelSamples);
int index = 0;
ValueEntry pointValue = null;
// if it is not the first insertion then find the optimal index for the y value of the current point
// that is: find the column to insert the value
if (novelCounter > 0) {
// find column index
pointValue = pointsample.getEntries().get(0);
List<?> referencevalues = novelSamples.get(0).getEntries();
int i = 0;
index = referencevalues.size();
// get the best column
for (Object val : referencevalues) {
if (((ValueEntry) val).getLabel().equals(pointValue.getLabel())) {
index = i;
break;
}
i++;
}
}
// if the point has not been inserted yet (there isn't another point with the same label previously inserted)
if (novelPoint == null) {
// generate a new y
ValueEntry ve = new ValueEntry(pointsample.getEntries().get(0).getLabel(), pointsample.getEntries().get(0).getValue());
// generate a new (x,y)
novelPoint = new Point(pointsample.getLabel(), pointsample.getValue());
// the number of columns to fill with 0s corresponds to all the columns
int numofcolumns = index;
if (novelCounter > 0)
numofcolumns = novelSamples.get(0).getEntries().size();
// fill all the columns with 0s
for (int j = 0; j < numofcolumns; j++) {
novelPoint.getEntries().add(j, new ValueEntry(novelSamples.get(0).getEntries().get(j).getLabel(), Double.valueOf(0)));
}
// add the y at the right column according to the calculated index
if (index >= novelPoint.getEntries().size())
novelPoint.getEntries().add(index, ve);
else
novelPoint.getEntries().set(index, ve);
// add the new point in the list
novelSamples.add(novelPoint);
novelCounter++;
} else {
// if we found a previous element update it
if (index >= novelPoint.getEntries().size())
// if the index is higher than the y size, add the column at the end
novelPoint.getEntries().add(index, pointValue);
else
// otherwise substitute the current index value
novelPoint.getEntries().set(index, pointValue);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return novelSamples;
}
// converts a bidimensional sampleTable to a list of bidimensional Points
@SuppressWarnings("unchecked")
public static List<Point<? extends Number, ? extends Number>> convert(SamplesTable sampleTable) {
ArrayList<Point<? extends Number, ? extends Number>> pointsList = new ArrayList<Point<? extends Number, ? extends Number>>();
try {
// every point has a label and a list of associated y points
int rows = sampleTable.getNumOfDataRows();
for (int rowIndex = 0; rowIndex < rows; rowIndex++) {
// take the label: it is separated in two parts separated by ';'
String label = sampleTable.getClassification(rowIndex);
int commaIndex = label.indexOf(";");
String xlabel = label;
String ylabel = "";
if (commaIndex > 0) {
xlabel = label.substring(0, commaIndex);
ylabel = label.substring(commaIndex + 1);
}
double x = sampleTable.getValue(rowIndex, 0);
double y = sampleTable.getValue(rowIndex, 1);
ValueEntry<Double> ve = new ValueEntry<Double>(ylabel, y);
Point<Double, Double> p = new Point<Double, Double>(xlabel, x, ve);
pointsList.add(p);
}
} catch (Exception e) {
e.printStackTrace();
}
return pointsList;
}
@SuppressWarnings("rawtypes")
public static List<Point<? extends Number, ? extends Number>> deleteHeaders(List<Point<? extends Number, ? extends Number>> samples) {
int size = samples.size();
for (int i = 0; i < size; i++) {
Point p = samples.get(i);
if (p.getLabel().equals("header")) {
samples.remove(i);
size--;
i--;
}
}
return samples;
}
// performs a complete transformation
public static List<Point<? extends Number, ? extends Number>> transformTable(SamplesTable sampleTable) {
List<Point<? extends Number, ? extends Number>> singlegraph = convert(sampleTable);
singlegraph = reduceDimension(singlegraph);
singlegraph = deleteHeaders(singlegraph);
return singlegraph;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List<Point<? extends Number, ? extends Number>> reorder(List<Point<? extends Number, ? extends Number>> samples) {
List<Point<? extends Number, ? extends Number>> orderedsamples = new ArrayList<Point<? extends Number, ? extends Number>>();
// check and reorder points
for (Point p : samples) {
int index = 0;
for (Point ordP : orderedsamples) {
if (ordP.getValue().doubleValue() > p.getValue().doubleValue()) {
break;
}
index++;
}
orderedsamples.add(index, p);
}
// re-enumerate x dimension
int i = 0;
for (Point ordP : orderedsamples) {
try {
ordP.setValue(Double.valueOf(i));
} catch (Exception e) {
}
i++;
}
return orderedsamples;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List<Point<? extends Number, ? extends Number>> filterXRange(List<Point<? extends Number, ? extends Number>> samples, String minX, String maxX) {
List<Point<? extends Number, ? extends Number>> filteredsamples = new ArrayList<Point<? extends Number, ? extends Number>>();
boolean copy = false;
for (Point p : samples) {
if (p.getLabel().equals(minX)) {
copy = true;
}
if (copy) {
filteredsamples.add(p);
}
if (p.getLabel().equals(maxX)) {
break;
}
}
return filteredsamples;
}
public static final String SPIKE = "STATIONARY";
public static void anotateStationaryPoints(GraphGroups gg) {
for (String key : gg.getGraphs().keySet()) {
GraphData graph = gg.getGraphs().get(key);
// for each series
int trends = graph.getData().size();
int yvalues = graph.getData().get(0).getEntries().size();
// System.out.println("yvalues "+yvalues);
// System.out.println("trends "+trends);
for (int i = 0; i < trends; i++) {
double[] points = MathFunctions.points2Double(graph.getData(), i, yvalues);
double[] derivative = MathFunctions.derivative(points);
boolean[] spikes = MathFunctions.findSpikes(derivative, threshold);
for (int k = 0; k < yvalues; k++) {
if (spikes[k]) {
String label = graph.getData().get(i).getEntries().get(k).getLabel();
String newLabel = label + ";" + SPIKE;
graph.getData().get(i).getEntries().get(k).setLabel(newLabel);
}
}
}
}
// return gg;
}
private static double threshold = 0.001;
public static void anotateStationaryPoints(GraphGroups gg, List<String> lables) {
for (String key : gg.getGraphs().keySet()) {
GraphData graph = gg.getGraphs().get(key);
// for each series
int trends = graph.getData().size();
int yvalues = graph.getData().get(0).getEntries().size();
int spikeslablessize = lables.size();
// System.out.println("yvalues "+yvalues);
// System.out.println("trends "+trends);
for (int i = 0; i < trends; i++) {
double[] points = MathFunctions.points2Double(graph.getData(), i, yvalues);
double[] derivative = MathFunctions.derivative(points);
boolean[] spikes = MathFunctions.findSpikes(derivative, threshold);
int spikecounter = 0;
for (int k = 0; k < yvalues; k++) {
if (spikes[k]) {
String label = graph.getData().get(i).getEntries().get(k).getLabel();
String spikelable = SPIKE;
if (spikecounter < spikeslablessize)
spikelable = lables.get(spikecounter);
String newLabel = label + ";" + spikelable;
graph.getData().get(i).getEntries().get(k).setLabel(newLabel);
spikecounter++;
}
}
}
}
// return gg;
}
public static void anotatePoints(GraphGroups gg, List<Integer> pointsIndexes, List<String> lables) {
for (String key : gg.getGraphs().keySet()) {
GraphData graph = gg.getGraphs().get(key);
// for each series
int trends = graph.getData().size();
for (int i = 0; i < trends; i++) {
int progressive = 0;
for (Integer index : pointsIndexes) {
String label = graph.getData().get(i).getEntries().get(index.intValue()).getLabel();
String addinglabel = lables.get(progressive);
String newLabel = label;
if (addinglabel != null)
newLabel += ";" + addinglabel;
graph.getData().get(i).getEntries().get(index.intValue()).setLabel(newLabel);
progressive++;
}
}
}
// return gg;
}
// works a single trend in the graph
public static List<Point> getStationaryPoints(GraphData graph) throws Exception {
List<Point> st = new ArrayList<Point>();
Point p = graph.getData().get(0);
st.add(new Point(p.getLabel(), p.getValue()));
int yvalues = graph.getData().get(0).getEntries().size();
double[] points = MathFunctions.points2Double(graph.getData(), 0, yvalues);
double[] derivative = MathFunctions.derivative(points);
boolean[] spikes = MathFunctions.findSpikes(derivative, threshold);
for (int k = 0; k < yvalues; k++) {
if (spikes[k]) {
String label = graph.getData().get(0).getEntries().get(k).getLabel();
Double val = points[k];
ValueEntry v = new ValueEntry(label, val);
st.get(0).addEntry(v);
}
}
return st;
}
// works a single trend in the graph
public static List<String> getLablesFromPoints(Point points) throws Exception {
List<String> lables = new ArrayList<String>();
for (Object v : points.getEntries()) {
lables.add(((ValueEntry) v).getLabel());
}
return lables;
}
// works the first trend in the graph: takes a list of points according to a list of indexes
public static List<String> getLabelsfromIndexes(List<Point<? extends Number, ? extends Number>> points, List<Integer> indexes) throws Exception {
List<String> lables = new ArrayList<String>();
int size = indexes.size();
for (Integer index : indexes) {
Object v = points.get(0).getEntries().get(index);
lables.add(((ValueEntry) v).getLabel());
}
return lables;
}
// works the first trend in the graph: takes a list of points according to a list of indexes
public static void sampleAnotationBySameFollower(List<Point<? extends Number, ? extends Number>> samples) throws Exception {
String previousLabel = null;
// check and reorder points
for (Point p : samples) {
for (Object v : p.getEntries()) {
String label = ((ValueEntry) v).getLabel();
int indexcomma = label.indexOf(";");
if (indexcomma >= 0) {
String labelcountry = label.substring(indexcomma + 1);
// AnalysisLogger.getLogger().debug("sampleAnotationBySameFollower-> comparing "+labelcountry+" vs "+previousLabel+" ORIGINAL "+label);
if ((previousLabel != null) && (labelcountry.equals(previousLabel))) {
label = label.substring(0, indexcomma);
// AnalysisLogger.getLogger().debug("sampleAnotationBySameFollower-> ELIMINATING LABEL!!!");
((ValueEntry) v).setLabel(label);
} else
previousLabel = labelcountry;
}
}
}
}
// works the first trend in the graph: takes a list of points according to a list of indexes
public static void sampleAnotationByRange(List<Point<? extends Number, ? extends Number>> samples, int range) throws Exception {
if (range > 0) {
// check and reorder points
for (Point p : samples) {
int partialCounter = 0;
for (Object v : p.getEntries()) {
String label = ((ValueEntry) v).getLabel();
int indexcomma = label.indexOf(";");
if (indexcomma >= 0) {
//if not enough time has passed delete the label otherwise reset counter
if (partialCounter <= range) {
String labelcountry = label.substring(indexcomma + 1);
// AnalysisLogger.getLogger().debug("sampleAnotationByRange-> partial counter "+partialCounter+ " label "+ label);
label = label.substring(0, indexcomma);
// AnalysisLogger.getLogger().debug("sampleAnotationByRange-> ELIMINATING LABEL!!!");
((ValueEntry) v).setLabel(label);
}
else{
partialCounter = 0;
}
}
partialCounter++;
}
}
}
}
}

View File

@ -0,0 +1,79 @@
package org.gcube.contentmanagement.graphtools.data.conversions;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import javax.swing.ImageIcon;
public class ImageTools {
public static Image toImage(BufferedImage bufferedImage) {
return Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource());
}
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {return (BufferedImage)image;}
// This code ensures that all the pixels in the image are loaded
image = new ImageIcon(image).getImage();
// Determine if the image has transparent pixels
boolean hasAlpha = hasAlpha(image);
// Create a buffered image with a format that's compatible with the screen
BufferedImage bimage = null;
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try {
// Determine the type of transparency of the new buffered image
int transparency = Transparency.OPAQUE;
if (hasAlpha == true) {transparency = Transparency.BITMASK;}
// Create the buffered image
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
}
catch (HeadlessException e) {} //No screen
if (bimage == null) {
// Create a buffered image using the default color model
int type = BufferedImage.TYPE_INT_RGB;
if (hasAlpha == true) {type = BufferedImage.TYPE_INT_ARGB;}
bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}
// Copy image to buffered image
Graphics g = bimage.createGraphics();
// Paint the image onto the buffered image
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
public static boolean hasAlpha(Image image) {
// If buffered image, the color model is readily available
if (image instanceof BufferedImage) {return ((BufferedImage)image).getColorModel().hasAlpha();}
// Use a pixel grabber to retrieve the image's color model;
// grabbing a single pixel is usually sufficient
PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
try {pg.grabPixels();} catch (InterruptedException e) {}
// Get the image's color model
return pg.getColorModel().hasAlpha();
}
}

View File

@ -0,0 +1,207 @@
package org.gcube.contentmanagement.graphtools.data.databases;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.gcube.contentmanagement.graphtools.abstracts.GenericDBExtractor;
import org.gcube.contentmanagement.graphtools.abstracts.SamplesTable;
import org.gcube.contentmanagement.graphtools.core.filters.Filter;
import org.gcube.contentmanagement.graphtools.data.BigSamplesTable;
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.DataTypeRecognizer;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.contentmanagement.lexicalmatcher.utils.DatabaseFactory;
import org.hibernate.SessionFactory;
public class CommonDBExtractor implements GenericDBExtractor {
SessionFactory dbSession;
public CommonDBExtractor(SessionFactory DbSession) {
dbSession = DbSession;
}
// produces a mono-dimensional table
public SamplesTable getMonoDimTable(String table, String column) {
BigSamplesTable monoSamples = new BigSamplesTable();
String query = "select distinct " + column + " from " + table + ";";
List<Object> resultSet = DatabaseFactory.executeSQLQuery(query, dbSession);
for (Object result : resultSet) {
try {
Double resultcount = (Double) result;
monoSamples.addSampleRow(column, resultcount);
} catch (Exception e) {
}
}
return monoSamples;
}
private String generateSelectionString(String... columns) {
String delimiter = " , ";
StringBuffer sb = new StringBuffer();
int numbOfColumns = columns.length;
for (int i = 0; i < numbOfColumns; i++) {
String column = columns[i];
sb.append(column);
if (i < numbOfColumns - 1)
sb.append(delimiter);
}
return sb.toString();
}
// SELECT field1,field5,field6,field3 FROM ts_161efa00_2c32_11df_b8b3_aa10916debe6 t where field3='Brown seaweeds';
private static final String staticQuery = "select distinct %1$s from %2$s where (%3$s) ";
private static final String staticOrderBy = " order by %1$s;";
private static final String descriptionQuery = "SELECT ordinal_position,column_name,data_type FROM information_schema.COLUMNS WHERE table_name ='%1$s'";
// produces a bi-dimensional table, where for a single x, multiple y are allowed
// transforms db column types to java types
private Map<String, String> getTypes(SessionFactory dbSession, String table) {
HashMap<String, String> typesMap = new HashMap<String, String>();
String queryDesc = String.format(descriptionQuery, table);
AnalysisLogger.getLogger().trace("Query for Description: " + queryDesc);
List<Object> resultSet = DatabaseFactory.executeSQLQuery(queryDesc, dbSession);
for (Object result : resultSet) {
Object[] resultArray = (Object[]) result;
String column_name = (String) resultArray[1];
String data_type = (String) resultArray[2];
typesMap.put(column_name, DataTypeRecognizer.transformTypeFromDB(data_type));
}
return typesMap;
}
private void updateSequence(SamplesTable sequence, Map<String, String> columnTypes, String xDimension, Object[] row, int index) {
// set correct x label and value
String xLabel = "";
Double xValue = Double.valueOf(0);
// String Type = columnTypes.get(xDimension);
// if it is a string set the label as the entry and the value as the index
// NOTE OLD CODE: the x axis is always meant to be in linear scale, for now
/*
* if (Type.equals(BigDecimal.class.getName())) { xLabel = "" + row[0]; xValue = Double.valueOf(index); } else {
*
* xLabel = xDimension; xValue = Double.valueOf("" + row[0]); }
*/
xLabel = "" + row[0];
xValue = Double.valueOf(index);
String label = xLabel + ";";
// record the y value by taking the row 3 as label and row 2 as the value
String yLabel = "";
Double yValue = Double.valueOf(0);
try {
yLabel += (String) row[3];
yValue = Double.valueOf("" + row[2]);
} catch (Exception e) {
}
label += yLabel;
sequence.addSampleRow(label, xValue, yValue);
}
// makes a query on the db and produces a HashMap of bi-dimensional tables including more parallel graphs
// each group represents a graph
// each graph has a xDimension and a label for each x (taken from x value)
// each x can have more than one y and label
// yValue is the column with y numeric values
// speciesColumn is the resulting column containing the labels for the ys
public Map<String, SamplesTable> getMultiDimTemporalTables(List<Filter> filters, Filter YRangeFilter, String table, String xDimension, String groupDimension, String yValue, String speciesColumn, String... yFilters) {
LinkedHashMap<String, SamplesTable> temporalSequence = new LinkedHashMap<String, SamplesTable>();
String selection = generateSelectionString(xDimension, groupDimension, yValue, speciesColumn);
StringBuffer whereclause = new StringBuffer();
int i = 0;
for (String columnFilter : yFilters) {
whereclause.append(speciesColumn + "='" + columnFilter + "'");
if (i < yFilters.length - 1)
whereclause.append(" or ");
i++;
}
String query = String.format(staticQuery, selection, table, whereclause);
if ((filters != null) && (filters.size() > 0)) {
query+="and (";
int kk =0;
for (Filter f : filters) {
// query += f.toString();
if (kk==0){
query += f.toString("");
}
else
query += f.toString("or");
kk++;
}
query+=")";
}
if (YRangeFilter != null) {
query += "and " + yValue + ">" + YRangeFilter.getFirstNumber() + " and " + yValue + "<" + YRangeFilter.getSecondNumber() + " ";
}
query += String.format(staticOrderBy, xDimension);
AnalysisLogger.getLogger().trace("Query: " + query);
List<Object> resultSet = DatabaseFactory.executeSQLQuery(query, dbSession);
// xdim, group, quantity, yLabel
Map<String, String> columnTypes = getTypes(dbSession, table);
if (resultSet != null) {
// for each result row
for (Object result : resultSet) {
// take the single row
Object[] resultArray = (Object[]) result;
// for each temporal dimension, generate a table
String temporalInfo = (String) resultArray[1];
// take the table for the temporal sequence
SamplesTable sequence = temporalSequence.get(temporalInfo);
// if table does not exist create a new table and add it to the sequence
if (sequence == null) {
sequence = new BigSamplesTable();
temporalSequence.put(temporalInfo, sequence);
// set fake elements as headers in order to understand the labels
for (String columnFilter : yFilters) {
sequence.addSampleRow("header;" + columnFilter, Double.NEGATIVE_INFINITY, 0);
}
}
// update the rows of the bidimensional table. Use as index the following: take the length of the sequence of values, then subtract the number of headers
updateSequence(sequence, columnTypes, xDimension, resultArray, sequence.getNumOfDataRows() - yFilters.length);
}
}
return temporalSequence;
}
public Map<String, SamplesTable> getMultiDimTemporalTables(String table, String xDimension, String groupDimension, String yValue, String speciesColumn, String... yFilters) {
return getMultiDimTemporalTables(null, null, table, xDimension, groupDimension, yValue, speciesColumn, yFilters);
}
}

View File

@ -0,0 +1,35 @@
package org.gcube.contentmanagement.graphtools.examples;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
public class ExampleExternalConfig {
public static void main(String[] args) throws Exception{
String table = "ts_161efa00_2c32_11df_b8b3_aa10916debe6";
String xDimension = "field5";
String yDimension = "field6";
String groupDimension = "field1";
String speciesColumn = "field3";
String filter1 = "Brown seaweeds";
String filter2 = "River eels";
StatisticsGenerator stg = new StatisticsGenerator();
LexicalEngineConfiguration conf = new LexicalEngineConfiguration();
//database Parameters
conf.setDatabaseUserName("root");
// conf.setDatabasePassword("password");
conf.setDatabaseDriver("com.mysql.jdbc.Driver");
conf.setDatabaseURL("jdbc:mysql://localhost/timeseries");
conf.setDatabaseDialect("org.hibernate.dialect.MySQLDialect");
conf.setDatabaseAutomaticTestTable("connectiontesttable");
conf.setDatabaseIdleConnectionTestPeriod("3600");
stg.init("./cfg/",conf);
stg.generateGraphs(3, table, xDimension, yDimension, groupDimension, speciesColumn, filter1, filter2);
}
}

View File

@ -0,0 +1,35 @@
package org.gcube.contentmanagement.graphtools.examples;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
public class ExampleExternalConfigPostGress {
public static void main(String[] args) throws Exception{
String table = "ts_7ab1d700_18d9_11e0_b703_c9d7e969ced7";
String xDimension = "field3"; // the dates
String yDimension = "field5"; // values on Y
String groupDimension = "field2"; // group names
String speciesColumn = "field4"; // lines labels
String filter1 = "Perciformes";
String filter2 = "Osteichthyes";
StatisticsGenerator stg = new StatisticsGenerator();
LexicalEngineConfiguration conf = new LexicalEngineConfiguration();
//database Parameters
conf.setDatabaseUserName("lucio");
conf.setDatabasePassword("d4science");
conf.setDatabaseDriver("org.postgresql.Driver");
conf.setDatabaseURL("jdbc:postgresql://dlib29.isti.cnr.it/timeseries");
conf.setDatabaseDialect("org.hibernate.dialect.PostgreSQLDialect");
conf.setDatabaseAutomaticTestTable("connectiontesttable");
conf.setDatabaseIdleConnectionTestPeriod("3600");
stg.init("./cfg/",conf);
stg.generateGraphs(200, table, xDimension, yDimension, groupDimension, speciesColumn, filter1, filter2);
}
}

View File

@ -0,0 +1,42 @@
package org.gcube.contentmanagement.graphtools.examples;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
public class ExampleExternalConfigPostGressProd {
public static void main(String[] args) throws Exception{
String table = "ts_c4bdfaa0_6c16_11e0_bb1f_fb760af5afc7";
String xDimension = "field4"; // the dates
String yDimension = "field6"; // values on Y
String groupDimension = "field1"; // group names
String speciesColumn = "field2"; // lines labels
String filter1 = "ABW";
// String filter2 = "Osteichthyes";
StatisticsGenerator stg = new StatisticsGenerator();
AnalysisLogger.setLogger("./cfg/ALog.properties");
LexicalEngineConfiguration conf = new LexicalEngineConfiguration();
//database Parameters
conf.setDatabaseUserName("gcube1");
conf.setDatabasePassword("d4science");
conf.setDatabaseDriver("org.postgresql.Driver");
conf.setDatabaseURL("jdbc:postgresql://node28.p.d4science.research-infrastructures.eu/timeseries");
// conf.setDatabaseDialect("org.hibernate.dialect.MySQLDialect");
conf.setDatabaseDialect("org.hibernate.dialect.PostgreSQLDialect");
conf.setDatabaseAutomaticTestTable("connectiontesttable");
conf.setDatabaseIdleConnectionTestPeriod("3600");
stg.init("./cfg/",conf);
// stg.generateGraphs(200, table, xDimension, yDimension, groupDimension, speciesColumn, filter1, filter2);
stg.generateGraphs(200, table, xDimension, yDimension, groupDimension, speciesColumn,filter1);
System.out.println();
}
}

View File

@ -0,0 +1,51 @@
package org.gcube.contentmanagement.graphtools.examples;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.plotting.graphs.HistogramGraph;
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
public class ExampleFilters {
public static void main(String[] args) throws Exception{
String table = "ts_161efa00_2c32_11df_b8b3_aa10916debe6";
String xDimension = "field5";
String yDimension = "field6";
String groupDimension = "field1";
String speciesColumn = "field3";
String filter1 = "Brown seaweeds";
String filter2 = "River eels";
StatisticsGenerator stg = new StatisticsGenerator();
LexicalEngineConfiguration conf = new LexicalEngineConfiguration();
//database Parameters
conf.setDatabaseUserName("root");
// conf.setDatabasePassword("password");
conf.setDatabaseDriver("com.mysql.jdbc.Driver");
conf.setDatabaseURL("jdbc:mysql://localhost/timeseries");
conf.setDatabaseDialect("org.hibernate.dialect.MySQLDialect");
conf.setDatabaseAutomaticTestTable("connectiontesttable");
conf.setDatabaseIdleConnectionTestPeriod("3600");
stg.init("./cfg/",conf);
// stg.addColumnFilter("field4", "F");
// stg.addColumnFilter("field5", "2005","<");
// stg.addXRangeFilter("2004", "2005");
// stg.addYRangeFilter("100", "300");
stg.addColumnFilter("field1", "Africa - Inland waters");
stg.addColumnFilter("field1", "Atlantic, Northeast");
GraphGroups gg = stg.generateGraphs(3, table, xDimension, yDimension, groupDimension, speciesColumn, filter1, filter2);
HistogramGraph series = new HistogramGraph("");
series.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,52 @@
package org.gcube.contentmanagement.graphtools.examples;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.plotting.graphs.HistogramGraph;
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
public class ExampleFilters2 {
public static void main(String[] args) throws Exception{
String table = "ts_161efa00_2c32_11df_b8b3_aa10916debe6";
String xDimension = "field5";
String yDimension = "field6";
String groupDimension = "field1";
String speciesColumn = "field3";
String filter1 = "Brown seaweeds";
String filter2 = "River eels";
StatisticsGenerator stg = new StatisticsGenerator();
LexicalEngineConfiguration conf = new LexicalEngineConfiguration();
//database Parameters
conf.setDatabaseUserName("root");
// conf.setDatabasePassword("password");
conf.setDatabaseDriver("com.mysql.jdbc.Driver");
conf.setDatabaseURL("jdbc:mysql://localhost/timeseries");
conf.setDatabaseDialect("org.hibernate.dialect.MySQLDialect");
conf.setDatabaseAutomaticTestTable("connectiontesttable");
conf.setDatabaseIdleConnectionTestPeriod("3600");
stg.init("./cfg/",conf);
// stg.addColumnFilter("field4", "F");
// stg.addColumnFilter("field5", "2005","<");
//stg.addXRangeFilter("2004", "2005");
stg.addYRangeFilter("5600000", "7000000");
/*
stg.addColumnFilter("field1", "Africa - Inland waters");
stg.addColumnFilter("field1", "Atlantic, Northeast");
*/
GraphGroups gg = stg.generateGraphs(3, table, xDimension, yDimension, groupDimension, speciesColumn, filter1);
HistogramGraph series = new HistogramGraph("");
series.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,45 @@
package org.gcube.contentmanagement.graphtools.examples;
import java.util.List;
import java.util.Map;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.plotting.graphs.GaussianDistributionGraph;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
import org.jfree.data.function.NormalDistributionFunction2D;
public class ExampleMeanVariance {
public static void main(String[] args) throws Exception{
String table = "ts_161efa00_2c32_11df_b8b3_aa10916debe6";
String xDimension = "field5";
String yDimension = "field6";
String groupDimension = "field1";
String speciesColumn = "field3";
String filter1 = "Brown seaweeds";
String filter2 = "River eels";
// String filter2 = "Osteichthyes";
StatisticsGenerator stg = new StatisticsGenerator();
stg.init("./cfg/");
GraphGroups gg = stg.generateGraphs(200, table, xDimension, yDimension, groupDimension, speciesColumn, filter1, filter2);
Map<String,List<NormalDistributionFunction2D>> normalsmap = GaussianDistributionGraph.graphs2Normals(gg);
//show normals
for (String key:normalsmap.keySet()){
List<NormalDistributionFunction2D> normals = normalsmap.get(key);
System.out.println("Means and Variances for distribution named: "+key);
for (NormalDistributionFunction2D gaussian:normals){
System.out.print("("+gaussian.getMean()+" ; "+gaussian.getStandardDeviation()+") ");
}
System.out.println();
}
// AnalysisLogger.getLogger().debug("Generated! "+normalsmap);
}
}

View File

@ -0,0 +1,42 @@
package org.gcube.contentmanagement.graphtools.examples;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.plotting.graphs.RadarGraph;
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
public class ExamplePostGressLocalRadar {
public static void main(String[] args) throws Exception{
AnalysisLogger.setLogger("./cfg/ALog.properties");
String table = "ts_3bdaf790_edbe_11e0_93e3_f6a9821baa29";
String xDimension = "field2"; // the dates
String yDimension = "field4"; // values on Y
String groupDimension = "field0"; // group names
String speciesColumn = "field3"; // lines labels
String filter2 = "Perciformes";
String filter1 = "Boregadus";
StatisticsGenerator stg = new StatisticsGenerator();
LexicalEngineConfiguration conf = new LexicalEngineConfiguration();
//database Parameters
conf.setDatabaseUserName("gcube");
conf.setDatabasePassword("d4science2");
conf.setDatabaseDriver("org.postgresql.Driver");
conf.setDatabaseURL("jdbc:postgresql://dbtest.next.research-infrastructures.eu/timeseries");
conf.setDatabaseDialect("org.hibernate.dialect.PostgreSQLDialect");
stg.init("./cfg/",conf);
GraphGroups gg = stg.generateGraphs(200, table, xDimension, yDimension, groupDimension, speciesColumn, filter1);
RadarGraph radar = new RadarGraph("");
radar.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,29 @@
package org.gcube.contentmanagement.graphtools.examples;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.plotting.graphs.RadarGraph;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
public class ExampleStaticConfig {
public static void main(String[] args) throws Exception{
String table = "ts_161efa00_2c32_11df_b8b3_aa10916debe6";
String xDimension = "field5";
String yDimension = "field6";
String groupDimension = "field1";
String speciesColumn = "field3";
String filter1 = "Brown seaweeds";
String filter2 = "River eels";
StatisticsGenerator stg = new StatisticsGenerator();
stg.init("./cfg/");
GraphGroups gg = stg.generateGraphs(3, table, xDimension, yDimension, groupDimension, speciesColumn, filter1, filter2);
RadarGraph radar = new RadarGraph("");
radar.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,50 @@
package org.gcube.contentmanagement.graphtools.examples;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.plotting.graphs.HistogramGraph;
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class ExampleStringGraphData {
public static void main(String[] args) throws Exception{
String table = "ts_161efa00_2c32_11df_b8b3_aa10916debe6";
String xDimension = "field5";
String yDimension = "field6";
String groupDimension = "field1";
String speciesColumn = "field3";
String filter1 = "Brown seaweeds";
String filter2 = "River eels";
StatisticsGenerator stg = new StatisticsGenerator();
LexicalEngineConfiguration conf = new LexicalEngineConfiguration();
//database Parameters
conf.setDatabaseUserName("root");
// conf.setDatabasePassword("password");
conf.setDatabaseDriver("com.mysql.jdbc.Driver");
conf.setDatabaseURL("jdbc:mysql://localhost/timeseries");
conf.setDatabaseDialect("org.hibernate.dialect.MySQLDialect");
conf.setDatabaseAutomaticTestTable("connectiontesttable");
conf.setDatabaseIdleConnectionTestPeriod("3600");
stg.init("./cfg/",conf);
//String generation
String ggs = stg.generateStringGraphs(100, table, xDimension, yDimension, groupDimension, speciesColumn, filter1, filter2);
System.out.println(ggs);
//String rebuilding
XStream xStream = new XStream(new DomDriver());
GraphGroups gg = (GraphGroups) xStream.fromXML(ggs);
//graph plot
HistogramGraph series = new HistogramGraph("");
series.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,54 @@
package org.gcube.contentmanagement.graphtools.examples.graphsTypes;
import java.util.ArrayList;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.data.GraphSamplesTable;
import org.gcube.contentmanagement.graphtools.data.conversions.GraphConverter2D;
import org.gcube.contentmanagement.graphtools.plotting.graphs.TransectLineGraph;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphData;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
public class ExampleDataBuiltGraph {
public static void main(String[] args) throws Exception{
ArrayList<String> l = new ArrayList<String>();
ArrayList<Double> v = new ArrayList<Double>();
ArrayList<String> stationaryLables = new ArrayList<String>();
for (int i=0;i<10;i++){
double d= 10*Math.random();
v.add(d);
l.add("value "+i);
if (d>5){
stationaryLables.add("statpoint"+i);
}
}
GraphSamplesTable gts = new GraphSamplesTable("Random Series",l,v,true);
GraphData grd = new GraphData(gts.getGraph(), false);
GraphGroups graphgroups = new GraphGroups();
graphgroups.addGraph("Distribution", grd);
GraphConverter2D.anotateStationaryPoints(graphgroups,stationaryLables);
GraphConverter2D.getStationaryPoints(grd);
TransectLineGraph series = new TransectLineGraph("");
series.renderGraphGroup(graphgroups);
// series.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,65 @@
package org.gcube.contentmanagement.graphtools.examples.graphsTypes;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.data.conversions.GraphConverter2D;
import org.gcube.contentmanagement.graphtools.plotting.graphs.TransectLineGraph;
import org.gcube.contentmanagement.graphtools.utils.MathFunctions;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphData;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
public class ExampleDerivative {
public static void main(String[] args) throws Exception{
String table = "ts_a904da30_b4fc_11df_800d_bcef80d51986";
String xDimension = "field1";
String yDimension = "field4";
String groupDimension = "field2";
String speciesColumn = "field3";
String filter1 = "Toluene";
// String filter2 = "Osteichthyes";
StatisticsGenerator stg = new StatisticsGenerator();
stg.init("./cfg/");
GraphGroups gg = stg.generateGraphs(200, table, xDimension, yDimension, speciesColumn, speciesColumn, filter1);
/*
for (String key:gg.getGraphs().keySet()){
GraphData graph = gg.getGraphs().get(key);
//for each series
int trends = graph.getData().size();
int yvalues = graph.getData().get(0).getEntries().size();
System.out.println("yvalues "+yvalues);
System.out.println("trends "+trends);
for (int i=0;i<trends;i++){
double [] points = MathFunctions.points2Double(graph.getData(), i, yvalues);
double [] derivative = MathFunctions.derivative(points);
boolean [] spikes = MathFunctions.findSpikes(derivative);
for (int k=0;k<yvalues;k++){
if (spikes[k]){
String label = graph.getData().get(i).getEntries().get(k).getLabel();
String newLabel = label+";spike";
graph.getData().get(i).getEntries().get(k).setLabel(newLabel);
}
}
}
}
*/
GraphConverter2D.anotateStationaryPoints(gg);
TransectLineGraph series = new TransectLineGraph("");
series.renderGraphGroup(gg);
// series.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,29 @@
package org.gcube.contentmanagement.graphtools.examples.graphsTypes;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.plotting.graphs.HistogramGraph;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
public class ExampleHistogram {
public static void main(String[] args) throws Exception{
String table = "ts_161efa00_2c32_11df_b8b3_aa10916debe6";
String xDimension = "field5";
String yDimension = "field6";
String groupDimension = "field1";
String speciesColumn = "field3";
String filter2 = "Brown seaweeds";
String filter1 = "River eels";
// String filter2 = "Osteichthyes";
StatisticsGenerator stg = new StatisticsGenerator();
stg.init("./cfg/");
GraphGroups gg = stg.generateGraphs(200, table, xDimension, yDimension, groupDimension, speciesColumn, filter1, filter2);
HistogramGraph series = new HistogramGraph("");
series.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,113 @@
package org.gcube.contentmanagement.graphtools.examples.graphsTypes;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;
import javax.imageio.ImageIO;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.data.conversions.ImageTools;
import org.gcube.contentmanagement.graphtools.plotting.graphs.GaussianDistributionGraph;
import org.gcube.contentmanagement.graphtools.plotting.graphs.ScatterGraphGeneric;
import org.gcube.contentmanagement.graphtools.plotting.graphs.ScatterGraphNumeric;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class ExampleImage extends Applet {
public void paint1(Graphics g) {
BufferedImage bgimg = loadImage();
g.drawImage(bgimg, 0, 0, this);
// g.fillRect(0, 0, 10, 10);
}
static int width = 320;
static int height = 280;
private BufferedImage loadImage() {
String imgFileName = "C:/Users/coro/Desktop/WorkFolder/Workspace/StatisticsExtractor/weather-cloud.png";
BufferedImage img = null;
try {
img = ImageIO.read(new File(imgFileName));
} catch (Exception e) {
e.printStackTrace();
}
return img;
}
public void init() {
setBackground( Color.LIGHT_GRAY );
setSize(width, height);
}
// public void paint(Graphics g){
public static void main(String[] args){
try{
String table = "ts_161efa00_2c32_11df_b8b3_aa10916debe6";
String xDimension = "field5";
String yDimension = "field6";
String groupDimension = "field1";
String speciesColumn = "field3";
String filter1 = "Brown seaweeds";
StatisticsGenerator stg = new StatisticsGenerator();
stg.init("C:/Users/coro/Desktop/WorkFolder/Workspace/StatisticsExtractor/cfg/");
GraphGroups gg = stg.generateGraphs(200, table, xDimension, yDimension, groupDimension, speciesColumn, filter1);
// ScatterGraphNumeric series = new ScatterGraphNumeric("");
// GaussianDistributionGraph series = new GaussianDistributionGraph("");
ScatterGraphGeneric series = new ScatterGraphGeneric("");
series.renderImages("./saved",width,height,gg);
AnalysisLogger.getLogger().debug("finished");
System.exit(0);
/* OLD CODE
List<Image> image = series.renderGraphGroupImage(width,height,gg);
Image singleimage = image.get(1);
BufferedImage bimage = ImageTools.toBufferedImage(singleimage);
XStream xStream = new XStream(new DomDriver());
String xmlimage = xStream.toXML(singleimage);
System.out.println(xmlimage);
File outputfile = new File("saved.png");
ImageIO.write(bimage, "png", outputfile);
*/
}catch(Exception e){
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,33 @@
package org.gcube.contentmanagement.graphtools.examples.graphsTypes;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.plotting.graphs.LineGraph;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
public class ExampleLines {
public static void main(String[] args) throws Exception{
String table = "ts_161efa00_2c32_11df_b8b3_aa10916debe6";
String xDimension = "field5";
String yDimension = "field6";
String groupDimension = "field1";
String speciesColumn = "field3";
String filter1 = "Brown seaweeds";
String filter2 = "River eels";
// String filter2 = "Osteichthyes";
StatisticsGenerator stg = new StatisticsGenerator();
stg.init("./cfg/");
GraphGroups gg = stg.generateGraphs(200, table, xDimension, yDimension, groupDimension, speciesColumn, filter1, filter2);
LineGraph series = new LineGraph("");
series.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,32 @@
package org.gcube.contentmanagement.graphtools.examples.graphsTypes;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.plotting.graphs.GaussianDistributionGraph;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
public class ExampleNormalDistribution {
public static void main(String[] args) throws Exception{
String table = "ts_161efa00_2c32_11df_b8b3_aa10916debe6";
String xDimension = "field5";
String yDimension = "field6";
String groupDimension = "field1";
String speciesColumn = "field3";
String filter2 = "Brown seaweeds";
String filter1 = "River eels";
// String filter2 = "Osteichthyes";
StatisticsGenerator stg = new StatisticsGenerator();
stg.init("./cfg/");
GraphGroups gg = stg.generateGraphs(200, table, xDimension, yDimension, groupDimension, speciesColumn, filter1);
GaussianDistributionGraph pie = new GaussianDistributionGraph("");
pie.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,32 @@
package org.gcube.contentmanagement.graphtools.examples.graphsTypes;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.plotting.graphs.NumericSeriesGraph;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
public class ExampleNumericSeries {
public static void main(String[] args) throws Exception{
String table = "ts_161efa00_2c32_11df_b8b3_aa10916debe6";
String xDimension = "field5";
String yDimension = "field6";
String groupDimension = "field1";
String speciesColumn = "field3";
String filter1 = "Brown seaweeds";
String filter2 = "River eels";
// String filter2 = "Osteichthyes";
StatisticsGenerator stg = new StatisticsGenerator();
stg.init("./cfg/");
GraphGroups gg = stg.generateGraphs(200, table, xDimension, yDimension, groupDimension, speciesColumn, filter1, filter2);
NumericSeriesGraph series = new NumericSeriesGraph("");
series.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,32 @@
package org.gcube.contentmanagement.graphtools.examples.graphsTypes;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.plotting.graphs.PieGraph;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
public class ExamplePie {
public static void main(String[] args) throws Exception{
String table = "ts_161efa00_2c32_11df_b8b3_aa10916debe6";
String xDimension = "field5";
String yDimension = "field6";
String groupDimension = "field1";
String speciesColumn = "field3";
String filter1 = "Brown seaweeds";
String filter2 = "River eels";
// String filter2 = "Osteichthyes";
StatisticsGenerator stg = new StatisticsGenerator();
stg.init("./cfg/");
GraphGroups gg = stg.generateGraphs(200, table, xDimension, yDimension, groupDimension, speciesColumn, filter1, filter2);
PieGraph pie = new PieGraph("");
pie.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,32 @@
package org.gcube.contentmanagement.graphtools.examples.graphsTypes;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.plotting.graphs.RadarGraph;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
public class ExampleRadar {
public static void main(String[] args) throws Exception{
String table = "ts_161efa00_2c32_11df_b8b3_aa10916debe6";
String xDimension = "field5";
String yDimension = "field6";
String groupDimension = "field1";
String speciesColumn = "field3";
String filter1 = "Brown seaweeds";
String filter2 = "River eels";
// String filter2 = "Osteichthyes";
StatisticsGenerator stg = new StatisticsGenerator();
stg.init("./cfg/");
GraphGroups gg = stg.generateGraphs(200, table, xDimension, yDimension, groupDimension, speciesColumn, filter2);
RadarGraph radar = new RadarGraph("");
radar.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,30 @@
package org.gcube.contentmanagement.graphtools.examples.graphsTypes;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.plotting.graphs.ScatterGraphNumeric;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
public class ExampleScatter {
public static void main(String[] args) throws Exception{
String table = "ts_161efa00_2c32_11df_b8b3_aa10916debe6";
String xDimension = "field5";
String yDimension = "field6";
String groupDimension = "field1";
String speciesColumn = "field3";
String filter1 = "Brown seaweeds";
String filter2 = "River eels";
// String filter2 = "Osteichthyes";
StatisticsGenerator stg = new StatisticsGenerator();
stg.init("./cfg/");
GraphGroups gg = stg.generateGraphs(200, table, xDimension, yDimension, groupDimension, speciesColumn, filter1);
// ScatterGraphGeneric series = new ScatterGraphGeneric("");
ScatterGraphNumeric series = new ScatterGraphNumeric("");
series.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,32 @@
package org.gcube.contentmanagement.graphtools.examples.graphsTypes;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.plotting.graphs.TimeSeriesGraph;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
public class ExampleTimeSeries {
public static void main(String[] args) throws Exception{
String table = "ts_161efa00_2c32_11df_b8b3_aa10916debe6";
String xDimension = "field5";
String yDimension = "field6";
String groupDimension = "field1";
String speciesColumn = "field3";
String filter1 = "Brown seaweeds";
String filter2 = "River eels";
// String filter2 = "Osteichthyes";
StatisticsGenerator stg = new StatisticsGenerator();
stg.init("./cfg/");
GraphGroups gg = stg.generateGraphs(200, table, xDimension, yDimension, groupDimension, speciesColumn, filter1);
TimeSeriesGraph series = new TimeSeriesGraph("");
series.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,32 @@
package org.gcube.contentmanagement.graphtools.examples.graphsTypes;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.plotting.graphs.TimeSeriesGraph;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
public class ExampleTimeSeries2 {
public static void main(String[] args) throws Exception{
String table = "ts_a904da30_b4fc_11df_800d_bcef80d51986";
String xDimension = "field1";
String yDimension = "field4";
String groupDimension = "field2";
String speciesColumn = "field3";
String filter1 = "Toluene";
String filter2 = "River eels";
// String filter2 = "Osteichthyes";
StatisticsGenerator stg = new StatisticsGenerator();
stg.init("./cfg/");
GraphGroups gg = stg.generateGraphs(200, table, xDimension, yDimension, groupDimension, speciesColumn, filter1);
TimeSeriesGraph series = new TimeSeriesGraph("");
series.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,143 @@
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------------
* PieGraph.java
* ------------------
* (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): ;
*
* Changes
* -------
* 09-Mar-2005 : Version 1, copied from the demo collection that ships with
* the JFreeChart Developer Guide (DG);
*
*/
package org.gcube.contentmanagement.graphtools.plotting.demo;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
* A simple demonstration application showing how to create a pie chart using
* data from a {@link DefaultPieDataset}.
*/
public class PieChartDemo1 extends ApplicationFrame {
/**
* Default constructor.
*
* @param title the frame title.
*/
public PieChartDemo1(String title) {
super(title);
setContentPane(createDemoPanel());
}
/**
* Creates a sample dataset.
*
* @return A sample dataset.
*/
private static PieDataset createDataset() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("One", new Double(43.2));
dataset.setValue("Two", new Double(10.0));
dataset.setValue("Three", new Double(27.5));
dataset.setValue("Four", new Double(17.5));
dataset.setValue("Five", new Double(11.0));
dataset.setValue("Six", new Double(19.4));
return dataset;
}
/**
* Creates a chart.
*
* @param dataset the dataset.
*
* @return A chart.
*/
private static JFreeChart createChart(PieDataset dataset) {
JFreeChart chart = ChartFactory.createPieChart(
"Pie Chart Demo 1", // chart title
dataset, // data
true, // include legend
true,
false
);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionOutlinesVisible(false);
plot.setNoDataMessage("No data available");
return chart;
}
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
return new ChartPanel(chart);
}
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
// ******************************************************************
// More than 150 demo applications are included with the JFreeChart
// Developer Guide...for more information, see:
//
// > http://www.object-refinery.com/jfreechart/guide.html
//
// ******************************************************************
PieChartDemo1 demo = new PieChartDemo1("Pie Chart Demo 1");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}

View File

@ -0,0 +1,143 @@
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------------
* PieGraph.java
* ------------------
* (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): ;
*
* Changes
* -------
* 09-Mar-2005 : Version 1, copied from the demo collection that ships with
* the JFreeChart Developer Guide (DG);
*
*/
package org.gcube.contentmanagement.graphtools.plotting.demo;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.SpiderWebPlot;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.Dataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
* A simple demonstration application showing how to create a pie chart using
* data from a {@link DefaultPieDataset}.
*/
public class SpiderWebDemo1 extends ApplicationFrame {
/**
* Default constructor.
*
* @param title the frame title.
*/
public SpiderWebDemo1(String title) {
super(title);
setContentPane(createDemoPanel());
}
/**
* Creates a sample dataset.
*
* @return A sample dataset.
*/
private static Dataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(35.0, "S1", "C1");
dataset.addValue(45.0, "S1", "C2");
dataset.addValue(55.0, "S1", "C3");
dataset.addValue(15.0, "S1", "C4");
dataset.addValue(25.0, "S1", "C5");
dataset.addValue(39.0, "S2", "C1");
dataset.addValue(20.0, "S2", "C2");
dataset.addValue(34.0, "S2", "C3");
dataset.addValue(30.0, "S2", "C4");
dataset.addValue(13.0, "S2", "C5");
return dataset;
}
/**
* Creates a chart.
*
* @param dataset the dataset.
*
* @return A chart.
*/
private static JFreeChart createChart(Dataset dataset) {
SpiderWebPlot plot = new SpiderWebPlot((DefaultCategoryDataset)dataset);
JFreeChart chart = new JFreeChart(plot);
return chart;
}
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
JPanel jp = new ChartPanel(chart);
return jp;
}
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
// ******************************************************************
// More than 150 demo applications are included with the JFreeChart
// Developer Guide...for more information, see:
//
// > http://www.object-refinery.com/jfreechart/guide.html
//
// ******************************************************************
SpiderWebDemo1 demo = new SpiderWebDemo1("Web Chart Demo 1");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,372 @@
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* -----------------------
* SpiderWebPlotTests.java
* -----------------------
* (C) Copyright 2005-2008, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* Changes
* -------
* 10-Jun-2005 : Version 1 (DG);
* 01-Jun-2006 : Added testDrawWithNullInfo() method (DG);
* 05-Feb-2007 : Added more checks to testCloning (DG);
*
*/
package org.gcube.portlets.user.timeseriesportlets.rapidminer.plotting;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.text.DecimalFormat;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.SpiderWebPlot;
import org.jfree.chart.urls.StandardCategoryURLGenerator;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.util.Rotation;
import org.jfree.util.TableOrder;
/**
* Tests for the {@link SpiderWebPlot} class.
*/
public class SpiderWebPlotTests extends TestCase {
/**
* Returns the tests as a test suite.
*
* @return The test suite.
*/
public static Test suite() {
return new TestSuite(SpiderWebPlotTests.class);
}
/**
* Constructs a new set of tests.
*
* @param name the name of the tests.
*/
public SpiderWebPlotTests(String name) {
super(name);
}
/**
* Some checks for the equals() method.
*/
public void testEquals() {
SpiderWebPlot p1 = new SpiderWebPlot(new DefaultCategoryDataset());
SpiderWebPlot p2 = new SpiderWebPlot(new DefaultCategoryDataset());
assertTrue(p1.equals(p2));
assertTrue(p2.equals(p1));
// dataExtractOrder
p1.setDataExtractOrder(TableOrder.BY_COLUMN);
assertFalse(p1.equals(p2));
p2.setDataExtractOrder(TableOrder.BY_COLUMN);
assertTrue(p1.equals(p2));
// headPercent
p1.setHeadPercent(0.321);
assertFalse(p1.equals(p2));
p2.setHeadPercent(0.321);
assertTrue(p1.equals(p2));
// interiorGap
p1.setInteriorGap(0.123);
assertFalse(p1.equals(p2));
p2.setInteriorGap(0.123);
assertTrue(p1.equals(p2));
// startAngle
p1.setStartAngle(0.456);
assertFalse(p1.equals(p2));
p2.setStartAngle(0.456);
assertTrue(p1.equals(p2));
// direction
p1.setDirection(Rotation.ANTICLOCKWISE);
assertFalse(p1.equals(p2));
p2.setDirection(Rotation.ANTICLOCKWISE);
assertTrue(p1.equals(p2));
// maxValue
p1.setMaxValue(123.4);
assertFalse(p1.equals(p2));
p2.setMaxValue(123.4);
assertTrue(p1.equals(p2));
// legendItemShape
p1.setLegendItemShape(new Rectangle(1, 2, 3, 4));
assertFalse(p1.equals(p2));
p2.setLegendItemShape(new Rectangle(1, 2, 3, 4));
assertTrue(p1.equals(p2));
// seriesPaint
p1.setSeriesPaint(new GradientPaint(1.0f, 2.0f, Color.red,
3.0f, 4.0f, Color.white));
assertFalse(p1.equals(p2));
p2.setSeriesPaint(new GradientPaint(1.0f, 2.0f, Color.red,
3.0f, 4.0f, Color.white));
assertTrue(p1.equals(p2));
// seriesPaintList
p1.setSeriesPaint(1, new GradientPaint(1.0f, 2.0f, Color.yellow,
3.0f, 4.0f, Color.white));
assertFalse(p1.equals(p2));
p2.setSeriesPaint(1, new GradientPaint(1.0f, 2.0f, Color.yellow,
3.0f, 4.0f, Color.white));
assertTrue(p1.equals(p2));
// baseSeriesPaint
p1.setBaseSeriesPaint(new GradientPaint(1.0f, 2.0f, Color.red,
3.0f, 4.0f, Color.black));
assertFalse(p1.equals(p2));
p2.setBaseSeriesPaint(new GradientPaint(1.0f, 2.0f, Color.red,
3.0f, 4.0f, Color.black));
assertTrue(p1.equals(p2));
// seriesOutlinePaint
p1.setSeriesOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.blue,
3.0f, 4.0f, Color.black));
assertFalse(p1.equals(p2));
p2.setSeriesOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.blue,
3.0f, 4.0f, Color.black));
assertTrue(p1.equals(p2));
// seriesOutlinePaintList
p1.setSeriesOutlinePaint(1, new GradientPaint(1.0f, 2.0f, Color.blue,
3.0f, 4.0f, Color.green));
assertFalse(p1.equals(p2));
p2.setSeriesOutlinePaint(1, new GradientPaint(1.0f, 2.0f, Color.blue,
3.0f, 4.0f, Color.green));
assertTrue(p1.equals(p2));
// baseSeriesOutlinePaint
p1.setBaseSeriesOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.cyan,
3.0f, 4.0f, Color.green));
assertFalse(p1.equals(p2));
p2.setBaseSeriesOutlinePaint(new GradientPaint(1.0f, 2.0f, Color.cyan,
3.0f, 4.0f, Color.green));
assertTrue(p1.equals(p2));
// seriesOutlineStroke
BasicStroke s = new BasicStroke(1.23f);
p1.setSeriesOutlineStroke(s);
assertFalse(p1.equals(p2));
p2.setSeriesOutlineStroke(s);
assertTrue(p1.equals(p2));
// seriesOutlineStrokeList
p1.setSeriesOutlineStroke(1, s);
assertFalse(p1.equals(p2));
p2.setSeriesOutlineStroke(1, s);
assertTrue(p1.equals(p2));
// baseSeriesOutlineStroke
p1.setBaseSeriesOutlineStroke(s);
assertFalse(p1.equals(p2));
p2.setBaseSeriesOutlineStroke(s);
assertTrue(p1.equals(p2));
// webFilled
p1.setWebFilled(false);
assertFalse(p1.equals(p2));
p2.setWebFilled(false);
assertTrue(p1.equals(p2));
// axisLabelGap
p1.setAxisLabelGap(0.11);
assertFalse(p1.equals(p2));
p2.setAxisLabelGap(0.11);
assertTrue(p1.equals(p2));
// labelFont
p1.setLabelFont(new Font("Serif", Font.PLAIN, 9));
assertFalse(p1.equals(p2));
p2.setLabelFont(new Font("Serif", Font.PLAIN, 9));
assertTrue(p1.equals(p2));
// labelPaint
p1.setLabelPaint(new GradientPaint(1.0f, 2.0f, Color.red,
3.0f, 4.0f, Color.blue));
assertFalse(p1.equals(p2));
p2.setLabelPaint(new GradientPaint(1.0f, 2.0f, Color.red,
3.0f, 4.0f, Color.blue));
assertTrue(p1.equals(p2));
// labelGenerator
p1.setLabelGenerator(new StandardCategoryItemLabelGenerator("XYZ: {0}",
new DecimalFormat("0.000")));
assertFalse(p1.equals(p2));
p2.setLabelGenerator(new StandardCategoryItemLabelGenerator("XYZ: {0}",
new DecimalFormat("0.000")));
assertTrue(p1.equals(p2));
// toolTipGenerator
p1.setToolTipGenerator(new StandardCategoryToolTipGenerator());
assertFalse(p1.equals(p2));
p2.setToolTipGenerator(new StandardCategoryToolTipGenerator());
assertTrue(p1.equals(p2));
// urlGenerator
p1.setURLGenerator(new StandardCategoryURLGenerator());
assertFalse(p1.equals(p2));
p2.setURLGenerator(new StandardCategoryURLGenerator());
assertTrue(p1.equals(p2));
// axisLinePaint
p1.setAxisLinePaint(Color.red);
assertFalse(p1.equals(p2));
p2.setAxisLinePaint(Color.red);
assertTrue(p1.equals(p2));
// axisLineStroke
p1.setAxisLineStroke(new BasicStroke(1.1f));
assertFalse(p1.equals(p2));
p2.setAxisLineStroke(new BasicStroke(1.1f));
assertTrue(p1.equals(p2));
}
/**
* Confirm that cloning works.
*/
public void testCloning() {
SpiderWebPlot p1 = new SpiderWebPlot(new DefaultCategoryDataset());
Rectangle2D legendShape = new Rectangle2D.Double(1.0, 2.0, 3.0, 4.0);
p1.setLegendItemShape(legendShape);
SpiderWebPlot p2 = null;
try {
p2 = (SpiderWebPlot) p1.clone();
}
catch (CloneNotSupportedException e) {
e.printStackTrace();
}
assertTrue(p1 != p2);
assertTrue(p1.getClass() == p2.getClass());
assertTrue(p1.equals(p2));
// change the legendItemShape
legendShape.setRect(4.0, 3.0, 2.0, 1.0);
assertFalse(p1.equals(p2));
p2.setLegendItemShape(legendShape);
assertTrue(p1.equals(p2));
// change a series paint
p1.setSeriesPaint(1, Color.black);
assertFalse(p1.equals(p2));
p2.setSeriesPaint(1, Color.black);
assertTrue(p1.equals(p2));
// change a series outline paint
p1.setSeriesOutlinePaint(0, Color.red);
assertFalse(p1.equals(p2));
p2.setSeriesOutlinePaint(0, Color.red);
assertTrue(p1.equals(p2));
// change a series outline stroke
p1.setSeriesOutlineStroke(0, new BasicStroke(1.1f));
assertFalse(p1.equals(p2));
p2.setSeriesOutlineStroke(0, new BasicStroke(1.1f));
assertTrue(p1.equals(p2));
}
/**
* Serialize an instance, restore it, and check for equality.
*/
public void testSerialization() {
SpiderWebPlot p1 = new SpiderWebPlot(new DefaultCategoryDataset());
SpiderWebPlot p2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(buffer);
out.writeObject(p1);
out.close();
ObjectInput in = new ObjectInputStream(
new ByteArrayInputStream(buffer.toByteArray()));
p2 = (SpiderWebPlot) in.readObject();
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
assertEquals(p1, p2);
}
/**
* Draws the chart with a null info object to make sure that no exceptions
* are thrown.
*/
public void testDrawWithNullInfo() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(35.0, "S1", "C1");
dataset.addValue(45.0, "S1", "C2");
dataset.addValue(55.0, "S1", "C3");
dataset.addValue(15.0, "S1", "C4");
dataset.addValue(25.0, "S1", "C5");
SpiderWebPlot plot = new SpiderWebPlot(dataset);
JFreeChart chart = new JFreeChart(plot);
boolean success = false;
try {
BufferedImage image = new BufferedImage(200 , 100,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
// g2.dispose();
success = true;
}
catch (Exception e) {
success = false;
}
assertTrue(success);
}
}

View File

@ -0,0 +1,43 @@
package org.gcube.contentmanagement.graphtools.plotting.demo;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.SpiderWebPlot;
import org.jfree.data.category.DefaultCategoryDataset;
public class TestWeb {
/**
* @param args
*/
public static void main(String[] args) {
testDrawWithNullInfo();
}
public static void testDrawWithNullInfo() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(35.0, "S1", "C1");
dataset.addValue(45.0, "S1", "C2");
dataset.addValue(55.0, "S1", "C3");
dataset.addValue(15.0, "S1", "C4");
dataset.addValue(25.0, "S1", "C5");
SpiderWebPlot plot = new SpiderWebPlot(dataset);
JFreeChart chart = new JFreeChart(plot);
boolean success = false;
try {
BufferedImage image = new BufferedImage(200 , 100,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
chart.draw(g2, new Rectangle2D.Double(0, 0, 200, 100), null, null);
// g2.dispose();
success = true;
}
catch (Exception e) {
success = false;
}
}
}

View File

@ -0,0 +1,219 @@
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2009, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* -------------------------
* TimeSeriesChartDemo1.java
* -------------------------
* (C) Copyright 2003-2009, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): ;
*
* Changes
* -------
* 09-Mar-2005 : Version 1, copied from the demo collection that ships with
* the JFreeChart Developer Guide (DG);
*
*/
package org.gcube.contentmanagement.graphtools.plotting.demo;
import java.awt.Color;
import java.text.SimpleDateFormat;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;
/**
* An example of a time series chart. For the most part, default settings are
* used, except that the renderer is modified to show filled shapes (as well as
* lines) at each data point.
*/
public class TimeSeriesChartDemo1 extends ApplicationFrame {
/**
* A demonstration application showing how to create a simple time series
* chart. This example uses monthly data.
*
* @param title the frame title.
*/
public TimeSeriesChartDemo1(String title) {
super(title);
ChartPanel chartPanel = (ChartPanel) createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(chartPanel);
}
/**
* Creates a chart.
*
* @param dataset a dataset.
*
* @return A chart.
*/
private static JFreeChart createChart(XYDataset dataset) {
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Legal & General Unit Trust Prices", // title
"Date", // x-axis label
"Price Per Unit", // y-axis label
dataset, // data
true, // create legend?
true, // generate tooltips?
false // generate URLs?
);
chart.setBackgroundPaint(Color.white);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
XYItemRenderer r = plot.getRenderer();
if (r instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
renderer.setDrawSeriesLineAsPath(true);
}
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
return chart;
}
/**
* Creates a dataset, consisting of two series of monthly data.
*
* @return The dataset.
*/
private static XYDataset createDataset() {
TimeSeries s1 = new TimeSeries("L&G European Index Trust");
s1.add(new Month(2, 2001), 181.8);
s1.add(new Month(3, 2001), 167.3);
s1.add(new Month(4, 2001), 153.8);
s1.add(new Month(5, 2001), 167.6);
s1.add(new Month(6, 2001), 158.8);
s1.add(new Month(7, 2001), 148.3);
s1.add(new Month(8, 2001), 153.9);
s1.add(new Month(9, 2001), 142.7);
s1.add(new Month(10, 2001), 123.2);
s1.add(new Month(11, 2001), 131.8);
s1.add(new Month(12, 2001), 139.6);
s1.add(new Month(1, 2002), 142.9);
s1.add(new Month(2, 2002), 138.7);
s1.add(new Month(3, 2002), 137.3);
s1.add(new Month(4, 2002), 143.9);
s1.add(new Month(5, 2002), 139.8);
s1.add(new Month(6, 2002), 137.0);
s1.add(new Month(7, 2002), 132.8);
TimeSeries s2 = new TimeSeries("L&G UK Index Trust");
s2.add(new Month(2, 2001), 129.6);
s2.add(new Month(3, 2001), 123.2);
s2.add(new Month(4, 2001), 117.2);
s2.add(new Month(5, 2001), 124.1);
s2.add(new Month(6, 2001), 122.6);
s2.add(new Month(7, 2001), 119.2);
s2.add(new Month(8, 2001), 116.5);
s2.add(new Month(9, 2001), 112.7);
s2.add(new Month(10, 2001), 101.5);
s2.add(new Month(11, 2001), 106.1);
s2.add(new Month(12, 2001), 110.3);
s2.add(new Month(1, 2002), 111.7);
s2.add(new Month(2, 2002), 111.0);
s2.add(new Month(3, 2002), 109.6);
s2.add(new Month(4, 2002), 113.2);
s2.add(new Month(5, 2002), 111.6);
s2.add(new Month(6, 2002), 108.8);
s2.add(new Month(7, 2002), 101.6);
// ******************************************************************
// More than 150 demo applications are included with the JFreeChart
// Developer Guide...for more information, see:
//
// > http://www.object-refinery.com/jfreechart/guide.html
//
// ******************************************************************
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(s1);
dataset.addSeries(s2);
return dataset;
}
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
ChartPanel panel = new ChartPanel(chart);
panel.setFillZoomRectangle(true);
panel.setMouseWheelEnabled(true);
return panel;
}
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
TimeSeriesChartDemo1 demo = new TimeSeriesChartDemo1(
"Time Series Chart Demo 1");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}

View File

@ -0,0 +1,208 @@
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------------
* PieGraph.java
* ------------------
* (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): ;
*
* Changes
* -------
* 09-Mar-2005 : Version 1, copied from the demo collection that ships with
* the JFreeChart Developer Guide (DG);
*
*/
package org.gcube.contentmanagement.graphtools.plotting.graphs;
import java.awt.BasicStroke;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.gcube.contentmanagement.graphtools.abstracts.GenericStandaloneGraph;
import org.gcube.contentmanagement.graphtools.utils.MathFunctions;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphData;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
import org.gcube.portlets.user.timeseries.charts.support.types.Point;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.function.NormalDistributionFunction2D;
import org.jfree.data.general.Dataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.XYSeriesCollection;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
public class GaussianDistributionGraph extends GenericStandaloneGraph {
private static final long serialVersionUID = 1L;
public GaussianDistributionGraph(String title) {
super(title);
}
protected Dataset generateDataset() {
XYSeriesCollection xyseriescollection = new XYSeriesCollection();
NormalDistributionFunction2D normaldistributionfunction2d = new NormalDistributionFunction2D(0.0D, 1.0D);
org.jfree.data.xy.XYSeries xyseries = DatasetUtilities.sampleFunction2DToSeries(normaldistributionfunction2d, -5.0999999999999996D, 5.0999999999999996D, 121, "N1");
xyseriescollection.addSeries(xyseries);
NormalDistributionFunction2D normaldistributionfunction2d1 = new NormalDistributionFunction2D(0.0D, Math.sqrt(0.20000000000000001D));
org.jfree.data.xy.XYSeries xyseries1 = DatasetUtilities.sampleFunction2DToSeries(normaldistributionfunction2d1, -5.0999999999999996D, 5.0999999999999996D, 121, "N2");
xyseriescollection.addSeries(xyseries1);
NormalDistributionFunction2D normaldistributionfunction2d2 = new NormalDistributionFunction2D(0.0D, Math.sqrt(5D));
org.jfree.data.xy.XYSeries xyseries2 = DatasetUtilities.sampleFunction2DToSeries(normaldistributionfunction2d2, -5.0999999999999996D, 5.0999999999999996D, 121, "N3");
xyseriescollection.addSeries(xyseries2);
NormalDistributionFunction2D normaldistributionfunction2d3 = new NormalDistributionFunction2D(-2D, Math.sqrt(0.5D));
org.jfree.data.xy.XYSeries xyseries3 = DatasetUtilities.sampleFunction2DToSeries(normaldistributionfunction2d3, -5.0999999999999996D, 5.0999999999999996D, 121, "N4");
xyseriescollection.addSeries(xyseries3);
return xyseriescollection;
}
protected JFreeChart createChart(Dataset dataset) {
String label = "mean:"+mean+" variance:"+variance;
if (label.length()>30)
label = label.substring(0,30)+"...";
JFreeChart jfreechart = ChartFactory.createXYLineChart("", label, "", (XYSeriesCollection)dataset, PlotOrientation.VERTICAL, true, true, false);
XYPlot xyplot = (XYPlot)jfreechart.getPlot();
xyplot.setDomainZeroBaselineVisible(true);
xyplot.setRangeZeroBaselineVisible(true);
xyplot.setDomainPannable(true);
xyplot.setRangePannable(true);
ValueAxis valueaxis = xyplot.getDomainAxis();
valueaxis.setLowerMargin(0.0D);
valueaxis.setUpperMargin(0.0D);
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)xyplot.getRenderer();
xylineandshaperenderer.setDrawSeriesLineAsPath(true);
xylineandshaperenderer.setSeriesStroke(0, new BasicStroke(1.5F));
return jfreechart;
}
@Override
protected GenericStandaloneGraph getInstance(String title) {
return new GaussianDistributionGraph(title);
}
double mean;
double variance;
@Override
protected Dataset convert2Dataset(GraphData st) {
List<Point<? extends Number, ? extends Number>> pointslist = st.getData();
// NOTE: after the graph generation graphs are inverted in x and y
int numbOfSeries = pointslist.size();
XYSeriesCollection xyseriescollection = new XYSeriesCollection();
if (numbOfSeries > 0) {
int numbOfPoints = pointslist.get(0).getEntries().size();
//for each series
for (int s = 0; s < numbOfSeries; s++) {
//get label
String serieslabel = pointslist.get(s).getLabel();
double maxRange = st.getMaxY().doubleValue();
double minRange = st.getMinY().doubleValue();
//get doubles vector for performing mean and variance calculation
double [] points = MathFunctions.points2Double(pointslist,s,numbOfPoints);
mean = MathFunctions.mean(points);
variance = com.rapidminer.tools.math.MathFunctions.variance(points, Double.NEGATIVE_INFINITY);
mean = Math.round(mean);
variance = Math.round(variance);
if (variance==0)
variance = 0.1;
AnalysisLogger.getLogger().debug("mean "+mean+" variance "+variance);
//build up normal distribution and add to the series
NormalDistributionFunction2D normaldistributionfunction2d = new NormalDistributionFunction2D(mean, variance);
//make the representation a bit longer
maxRange = maxRange*2;
org.jfree.data.xy.XYSeries xyseries = DatasetUtilities.sampleFunction2DToSeries(normaldistributionfunction2d, minRange, maxRange, 121, serieslabel);
xyseriescollection.addSeries(xyseries);
}
}
return xyseriescollection;
}
public static Map<String,List<NormalDistributionFunction2D>> graphs2Normals(GraphGroups gg){
Map<String,List<NormalDistributionFunction2D>> normalsMap = new HashMap<String, List<NormalDistributionFunction2D>>();
for (String key:gg.getGraphs().keySet())
{
GraphData st = gg.getGraphs().get(key);
List<Point<? extends Number, ? extends Number>> pointslist = st.getData();
List<NormalDistributionFunction2D> normalsList = new ArrayList<NormalDistributionFunction2D>();
// NOTE: after the graph generation graphs are inverted in x and y
int numbOfSeries = pointslist.size();
if (numbOfSeries > 0) {
int numbOfPoints = pointslist.get(0).getEntries().size();
//for each series
for (int s = 0; s < numbOfSeries; s++) {
//get doubles vector for performing mean and variance calculation
double [] points = MathFunctions.points2Double(pointslist,s,numbOfPoints);
double mean = MathFunctions.mean(points);
double variance = com.rapidminer.tools.math.MathFunctions.variance(points, Double.NEGATIVE_INFINITY);
if (variance==0)
variance = 0.1;
AnalysisLogger.getLogger().debug("mean "+mean+" variance "+variance);
//build up normal distribution and add to the series
NormalDistributionFunction2D normaldistributionfunction2d = new NormalDistributionFunction2D(mean, variance);
normalsList.add(normaldistributionfunction2d);
}
}
normalsMap.put(key, normalsList);
}
return normalsMap;
}
}

View File

@ -0,0 +1,131 @@
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------------
* PieGraph.java
* ------------------
* (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): ;
*
* Changes
* -------
* 09-Mar-2005 : Version 1, copied from the demo collection that ships with
* the JFreeChart Developer Guide (DG);
*
*/
package org.gcube.contentmanagement.graphtools.plotting.graphs;
import java.awt.Color;
import org.gcube.contentmanagement.graphtools.abstracts.GenericStandaloneGraph;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.Dataset;
import org.jfree.ui.RectangleInsets;
public class HistogramGraph extends GenericStandaloneGraph {
private static final long serialVersionUID = 1L;
public HistogramGraph(String title) {
super(title);
}
protected Dataset generateDataset() {
String s = "S1";
String s1 = "S2";
String s2 = "S3";
String s3 = "Category 1";
String s4 = "Category 2";
String s5 = "Category 3";
String s6 = "Category 4";
String s7 = "Category 5";
String s8 = "Category 6";
String s9 = "Category 7";
String s10 = "Category 8";
DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
defaultcategorydataset.addValue(1.0D, s, s3);
defaultcategorydataset.addValue(4D, s, s4);
defaultcategorydataset.addValue(3D, s, s5);
defaultcategorydataset.addValue(5D, s, s6);
defaultcategorydataset.addValue(5D, s, s7);
defaultcategorydataset.addValue(7D, s, s8);
defaultcategorydataset.addValue(7D, s, s9);
defaultcategorydataset.addValue(8D, s, s10);
defaultcategorydataset.addValue(5D, s1, s3);
defaultcategorydataset.addValue(7D, s1, s4);
defaultcategorydataset.addValue(6D, s1, s5);
defaultcategorydataset.addValue(8D, s1, s6);
defaultcategorydataset.addValue(4D, s1, s7);
defaultcategorydataset.addValue(4D, s1, s8);
defaultcategorydataset.addValue(2D, s1, s9);
defaultcategorydataset.addValue(1.0D, s1, s10);
defaultcategorydataset.addValue(4D, s2, s3);
defaultcategorydataset.addValue(3D, s2, s4);
defaultcategorydataset.addValue(2D, s2, s5);
defaultcategorydataset.addValue(3D, s2, s6);
defaultcategorydataset.addValue(6D, s2, s7);
defaultcategorydataset.addValue(3D, s2, s8);
defaultcategorydataset.addValue(4D, s2, s9);
defaultcategorydataset.addValue(3D, s2, s10);
return defaultcategorydataset;
}
protected JFreeChart createChart(Dataset dataset) {
JFreeChart chart = ChartFactory.createBarChart("Histogram Chart", "", "", (DefaultCategoryDataset) dataset, PlotOrientation.VERTICAL, true, true, false);
chart.setBackgroundPaint(Color.white);
CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
categoryplot.setBackgroundPaint(new Color(238, 238, 255));
categoryplot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
CategoryAxis categoryaxis = categoryplot.getDomainAxis();
categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
LegendTitle legendtitle = new LegendTitle(categoryplot.getRenderer(0));
legendtitle.setMargin(new RectangleInsets(2D, 2D, 2D, 2D));
return chart;
}
@Override
protected GenericStandaloneGraph getInstance(String title) {
return new HistogramGraph(title);
}
}

View File

@ -0,0 +1,153 @@
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------------
* PieGraph.java
* ------------------
* (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): ;
*
* Changes
* -------
* 09-Mar-2005 : Version 1, copied from the demo collection that ships with
* the JFreeChart Developer Guide (DG);
*
*/
package org.gcube.contentmanagement.graphtools.plotting.graphs;
import java.awt.Color;
import org.gcube.contentmanagement.graphtools.abstracts.GenericStandaloneGraph;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.Dataset;
public class LineGraph extends GenericStandaloneGraph {
private static final long serialVersionUID = 1L;
public LineGraph(String title) {
super(title);
}
protected Dataset generateDataset() {
// row keys...
String series1 = "First";
String series2 = "Second";
String series3 = "Third";
// column keys...
String type1 = "Type 1";
String type2 = "Type 2";
String type3 = "Type 3";
String type4 = "Type 4";
String type5 = "Type 5";
String type6 = "Type 6";
String type7 = "Type 7";
String type8 = "Type 8";
// create the dataset...
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, series1, type1);
dataset.addValue(4.0, series1, type2);
dataset.addValue(3.0, series1, type3);
dataset.addValue(5.0, series1, type4);
dataset.addValue(5.0, series1, type5);
dataset.addValue(7.0, series1, type6);
dataset.addValue(7.0, series1, type7);
dataset.addValue(8.0, series1, type8);
dataset.addValue(5.0, series2, type1);
dataset.addValue(7.0, series2, type2);
dataset.addValue(6.0, series2, type3);
dataset.addValue(8.0, series2, type4);
dataset.addValue(4.0, series2, type5);
dataset.addValue(4.0, series2, type6);
dataset.addValue(2.0, series2, type7);
dataset.addValue(1.0, series2, type8);
dataset.addValue(4.0, series3, type1);
dataset.addValue(3.0, series3, type2);
dataset.addValue(2.0, series3, type3);
dataset.addValue(3.0, series3, type4);
dataset.addValue(6.0, series3, type5);
dataset.addValue(3.0, series3, type6);
dataset.addValue(4.0, series3, type7);
dataset.addValue(3.0, series3, type8);
return dataset;
}
protected JFreeChart createChart(Dataset dataset) {
// create the chart...
JFreeChart chart = ChartFactory.createLineChart(
this.getTitle(), // chart title
"", // domain axis label
"", // range axis label
(DefaultCategoryDataset)dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = chart.getCategoryPlot();
// plot.setBackgroundPaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setDomainCrosshairVisible(true);
plot.setDomainGridlinesVisible(true);
plot.setRangeCrosshairVisible(true);
plot.setRenderer(new LineAndShapeRenderer(true,true));
//deprecated
/*
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
renderer.setShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true);
renderer.setFillPaint(Color.white);
*/
return chart;
}
@Override
protected GenericStandaloneGraph getInstance(String title) {
return new LineGraph(title);
}
}

View File

@ -0,0 +1,149 @@
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------------
* PieGraph.java
* ------------------
* (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): ;
*
* Changes
* -------
* 09-Mar-2005 : Version 1, copied from the demo collection that ships with
* the JFreeChart Developer Guide (DG);
*
*/
package org.gcube.contentmanagement.graphtools.plotting.graphs;
import java.awt.Color;
import java.util.List;
import org.gcube.contentmanagement.graphtools.abstracts.GenericStandaloneGraph;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphData;
import org.gcube.portlets.user.timeseries.charts.support.types.Point;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.general.Dataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleInsets;
public class NumericSeriesGraph extends GenericStandaloneGraph {
private static final long serialVersionUID = 1L;
public NumericSeriesGraph(String title) {
super(title);
}
protected Dataset generateDataset() {
XYSeries xyseries = new XYSeries("Series 1");
xyseries.add(2D, 56.270000000000003D);
xyseries.add(3D, 41.32D);
xyseries.add(4D, 31.449999999999999D);
xyseries.add(5D, 30.050000000000001D);
xyseries.add(6D, 24.690000000000001D);
xyseries.add(7D, 19.780000000000001D);
xyseries.add(8D, 20.940000000000001D);
xyseries.add(9D, 16.73D);
xyseries.add(10D, 14.210000000000001D);
xyseries.add(11D, 12.44D);
XYSeriesCollection xyseriescollection = new XYSeriesCollection(xyseries);
XYSeries xyseries1 = new XYSeries("Series 2");
xyseries1.add(11D, 56.270000000000003D);
xyseries1.add(10D, 41.32D);
xyseries1.add(9D, 31.449999999999999D);
xyseries1.add(8D, 30.050000000000001D);
xyseries1.add(7D, 24.690000000000001D);
xyseries1.add(6D, 19.780000000000001D);
xyseries1.add(5D, 20.940000000000001D);
xyseries1.add(4D, 16.73D);
xyseries1.add(3D, 14.210000000000001D);
xyseries1.add(2D, 12.44D);
xyseriescollection.addSeries(xyseries1);
return xyseriescollection;
}
protected JFreeChart createChart(Dataset dataset) {
NumberAxis numberaxis = new NumberAxis("X");
numberaxis.setAutoRangeIncludesZero(true);
NumberAxis numberaxis1 = new NumberAxis("Y");
numberaxis1.setAutoRangeIncludesZero(true);
XYSplineRenderer xysplinerenderer = new XYSplineRenderer();
XYPlot xyplot = new XYPlot((XYDataset) dataset, numberaxis, numberaxis1, xysplinerenderer);
xyplot.setBackgroundPaint(Color.lightGray);
xyplot.setDomainGridlinePaint(Color.white);
xyplot.setRangeGridlinePaint(Color.white);
xyplot.setAxisOffset(new RectangleInsets(4D, 4D, 4D, 4D));
JFreeChart chart = new JFreeChart("Numeric Series", JFreeChart.DEFAULT_TITLE_FONT, xyplot, true);
return chart;
}
@Override
protected Dataset convert2Dataset(GraphData st) {
List<Point<? extends Number, ? extends Number>> pointslist = st.getData();
// NOTE: after the graph generation graphs are inverted in x and y
int numbOfRows = pointslist.size();
XYSeriesCollection xyseriescollection = new XYSeriesCollection();
if (numbOfRows > 0) {
int numbOfCols = pointslist.get(0).getEntries().size();
// calclulation will be made only for the first series
for (int x = 0; x < numbOfRows; x++) {
String serieslabel = pointslist.get(x).getLabel();
XYSeries xyseries = new XYSeries(serieslabel);
for (int y = 0; y < numbOfCols; y++) {
// String xlabel = pointslist.get(x).getEntries().get(y).getLabel();
double value = pointslist.get(x).getEntries().get(y).getValue().doubleValue();
xyseries.add(y + 1, value);
}
xyseriescollection.addSeries(xyseries);
}
}
return xyseriescollection;
}
@Override
protected GenericStandaloneGraph getInstance(String title) {
return new NumericSeriesGraph(title);
}
}

View File

@ -0,0 +1,149 @@
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------------
* PieGraph.java
* ------------------
* (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): ;
*
* Changes
* -------
* 09-Mar-2005 : Version 1, copied from the demo collection that ships with
* the JFreeChart Developer Guide (DG);
*
*/
package org.gcube.contentmanagement.graphtools.plotting.graphs;
import java.util.List;
import org.gcube.contentmanagement.graphtools.abstracts.GenericStandaloneGraph;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphData;
import org.gcube.portlets.user.timeseries.charts.support.types.Point;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.Dataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.TableOrder;
public class PieGraph extends GenericStandaloneGraph {
private static final long serialVersionUID = 1L;
public PieGraph(String title) {
super(title);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
protected Dataset generateDataset() {
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue("One", Double.valueOf(43.2));
dataset.setValue("Two", Double.valueOf(10.0));
dataset.setValue("Three", Double.valueOf(27.5));
dataset.setValue("Four", Double.valueOf(17.5));
dataset.setValue("Five", Double.valueOf(11.0));
dataset.setValue("Six", Double.valueOf(19.4));
return dataset;
}
/*
protected JFreeChart createChart(Dataset dataset) {
PiePlot plot = new PiePlot((DefaultPieDataset) dataset);
JFreeChart chart = new JFreeChart(plot);
return chart;
}
*/
protected JFreeChart createChart(Dataset dataset) {
JFreeChart chart = ChartFactory.createMultiplePieChart(
"Multiple Pie Chart", // chart title
(DefaultCategoryDataset)dataset, // dataset
TableOrder.BY_ROW,
true, // include legend
true,
false
);
return chart;
}
protected Dataset convert2DatasetOld(GraphData st) {
DefaultPieDataset dataset = new DefaultPieDataset();
List<Point<? extends Number, ? extends Number>> pointslist = st.getData();
// NOTE: after the graph generation graphs are inverted in x and y
int numbOfRows = pointslist.size();
if (numbOfRows > 0) {
int numbOfCols = pointslist.get(0).getEntries().size();
// calclulation will be made only for the first series
int x = 0;
String xlabel = pointslist.get(x).getLabel();
//calculate maximum
double max = 0;
for (int y = 0; y < numbOfCols; y++) {
double value = pointslist.get(x).getEntries().get(y).getValue().doubleValue();
if (value>max){
max = value;
}
}
for (int y = 0; y < numbOfCols; y++) {
double value = pointslist.get(x).getEntries().get(y).getValue().doubleValue();
value = (value/max)*100;
String ylabel = pointslist.get(x).getEntries().get(y).getLabel();
AnalysisLogger.getLogger().info(xlabel + ":" + ylabel +"->" + value);
dataset.setValue(xlabel + ":" + ylabel, value);
}
}
return dataset;
}
@Override
protected GenericStandaloneGraph getInstance(String title) {
return new PieGraph(title);
}
}

View File

@ -0,0 +1,65 @@
package org.gcube.contentmanagement.graphtools.plotting.graphs;
import org.gcube.contentmanagement.graphtools.abstracts.GenericStandaloneGraph;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.SpiderWebPlot;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.Dataset;
public class RadarGraph extends GenericStandaloneGraph{
/**
*
*/
private static final long serialVersionUID = 1L;
public RadarGraph(String title) {
super(title);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
@Override
protected Dataset generateDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(35.0, "S1", "C1");
dataset.addValue(45.0, "S1", "C2");
dataset.addValue(55.0, "S1", "C3");
dataset.addValue(15.0, "S1", "C4");
dataset.addValue(25.0, "S1", "C5");
dataset.addValue(39.0, "S2", "C1");
dataset.addValue(20.0, "S2", "C2");
dataset.addValue(34.0, "S2", "C3");
dataset.addValue(30.0, "S2", "C4");
dataset.addValue(13.0, "S2", "C5");
return dataset;
}
@Override
protected JFreeChart createChart(Dataset dataset) {
SpiderWebPlot plot = new SpiderWebPlot((DefaultCategoryDataset)dataset);
JFreeChart chart = new JFreeChart(plot);
return chart;
}
@Override
protected GenericStandaloneGraph getInstance(String title) {
return new RadarGraph(title);
}
}

View File

@ -0,0 +1,131 @@
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------------
* PieGraph.java
* ------------------
* (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): ;
*
* Changes
* -------
* 09-Mar-2005 : Version 1, copied from the demo collection that ships with
* the JFreeChart Developer Guide (DG);
*
*/
package org.gcube.contentmanagement.graphtools.plotting.graphs;
import java.awt.BasicStroke;
import java.awt.Color;
import java.util.List;
import org.gcube.contentmanagement.graphtools.abstracts.GenericStandaloneGraph;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphData;
import org.gcube.portlets.user.timeseries.charts.support.types.Point;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.Dataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class ScatterGraphGeneric extends GenericStandaloneGraph {
private static final long serialVersionUID = 1L;
public ScatterGraphGeneric(String title) {
super(title);
}
protected Dataset generateDataset() {
return null;
}
protected JFreeChart createChart(Dataset dataset) {
// create the chart...
JFreeChart chart = ChartFactory.createLineChart(
"", // chart title
"", // domain axis label
"", // range axis label
(DefaultCategoryDataset)dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = chart.getCategoryPlot();
/*
plot.setDomainGridlineStroke(new BasicStroke(0.0F));
plot.setDomainGridlinePaint(Color.blue);
plot.setRangeGridlineStroke(new BasicStroke(0.0F));
plot.setRangeMinorGridlineStroke(new BasicStroke(0.0F));
plot.setRangeGridlinePaint(Color.blue);
plot.setRangeMinorGridlinesVisible(true);
plot.setNoDataMessage("NO DATA");
plot.setRangePannable(true);
plot.setRangeZeroBaselineVisible(true);
*/
plot.setBackgroundPaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setDomainCrosshairVisible(true);
plot.setDomainGridlinesVisible(false);
plot.setRangeCrosshairVisible(true);
plot.setRenderer(new LineAndShapeRenderer(false,true));
//deprecated
/*
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
renderer.setShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true);
renderer.setFillPaint(Color.white);
*/
return chart;
}
@Override
protected GenericStandaloneGraph getInstance(String title) {
return new ScatterGraphGeneric(title);
}
}

View File

@ -0,0 +1,144 @@
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------------
* PieGraph.java
* ------------------
* (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): ;
*
* Changes
* -------
* 09-Mar-2005 : Version 1, copied from the demo collection that ships with
* the JFreeChart Developer Guide (DG);
*
*/
package org.gcube.contentmanagement.graphtools.plotting.graphs;
import java.awt.BasicStroke;
import java.awt.Color;
import java.util.List;
import org.gcube.contentmanagement.graphtools.abstracts.GenericStandaloneGraph;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphData;
import org.gcube.portlets.user.timeseries.charts.support.types.Point;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.Dataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class ScatterGraphNumeric extends GenericStandaloneGraph {
private static final long serialVersionUID = 1L;
public ScatterGraphNumeric(String title) {
super(title);
}
protected Dataset generateDataset() {
return null;
}
protected JFreeChart createChart(Dataset dataset) {
JFreeChart jfreechart = ChartFactory.createScatterPlot("", "", "", (XYDataset)dataset, PlotOrientation.VERTICAL, true, true, false);
XYPlot xyplot = (XYPlot)jfreechart.getPlot();
xyplot.setNoDataMessage("NO DATA");
xyplot.setDomainPannable(true);
xyplot.setRangePannable(true);
xyplot.setDomainZeroBaselineVisible(true);
xyplot.setRangeZeroBaselineVisible(true);
xyplot.setDomainGridlineStroke(new BasicStroke(0.0F));
xyplot.setDomainMinorGridlineStroke(new BasicStroke(0.0F));
xyplot.setDomainGridlinePaint(Color.blue);
xyplot.setRangeGridlineStroke(new BasicStroke(0.0F));
xyplot.setRangeMinorGridlineStroke(new BasicStroke(0.0F));
xyplot.setRangeGridlinePaint(Color.blue);
xyplot.setDomainMinorGridlinesVisible(true);
xyplot.setRangeMinorGridlinesVisible(true);
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)xyplot.getRenderer();
xylineandshaperenderer.setSeriesOutlinePaint(0, Color.black);
xylineandshaperenderer.setUseOutlinePaint(true);
NumberAxis numberaxis = (NumberAxis)xyplot.getDomainAxis();
numberaxis.setAutoRangeIncludesZero(false);
numberaxis.setTickMarkInsideLength(2.0F);
numberaxis.setTickMarkOutsideLength(2.0F);
numberaxis.setMinorTickCount(2);
numberaxis.setMinorTickMarksVisible(true);
NumberAxis numberaxis1 = (NumberAxis)xyplot.getRangeAxis();
numberaxis1.setTickMarkInsideLength(2.0F);
numberaxis1.setTickMarkOutsideLength(2.0F);
numberaxis1.setMinorTickCount(2);
numberaxis1.setMinorTickMarksVisible(true);
return jfreechart;
}
@Override
protected Dataset convert2Dataset(GraphData st) {
List<Point<? extends Number, ? extends Number>> pointslist = st.getData();
// NOTE: after the graph generation graphs are inverted in x and y
int numbOfRows = pointslist.size();
XYSeriesCollection xyseriescollection = new XYSeriesCollection();
if (numbOfRows > 0) {
int numbOfCols = pointslist.get(0).getEntries().size();
// calculation will be made only for the first series
for (int x = 0; x < numbOfRows; x++) {
String serieslabel = pointslist.get(x).getLabel();
XYSeries xyseries = new XYSeries(serieslabel);
for (int y = 0; y < numbOfCols; y++) {
// String xlabel = pointslist.get(x).getEntries().get(y).getLabel();
double value = pointslist.get(x).getEntries().get(y).getValue().doubleValue();
xyseries.add(y + 1, value);
}
xyseriescollection.addSeries(xyseries);
}
}
return xyseriescollection;
}
@Override
protected GenericStandaloneGraph getInstance(String title) {
return new ScatterGraphNumeric(title);
}
}

View File

@ -0,0 +1,186 @@
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------------
* PieGraph.java
* ------------------
* (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): ;
*
* Changes
* -------
* 09-Mar-2005 : Version 1, copied from the demo collection that ships with
* the JFreeChart Developer Guide (DG);
*
*/
package org.gcube.contentmanagement.graphtools.plotting.graphs;
import java.awt.Color;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import org.gcube.contentmanagement.graphtools.abstracts.GenericStandaloneGraph;
import org.gcube.contentmanagement.graphtools.utils.DateGuesser;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphData;
import org.gcube.portlets.user.timeseries.charts.support.types.Point;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.general.Dataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.Minute;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RectangleInsets;
public class TimeSeriesGraph extends GenericStandaloneGraph {
private static final long serialVersionUID = 1L;
private static final String formatYear= "yyyy";
private static final String formatMonth= "MM-yyyy";
private static final String formatDay= "MM-dd-yyyy";
private String timeseriesformat;
public TimeSeriesGraph(String title) {
super(title);
}
protected Dataset generateDataset() {
TimeSeriesCollection dataset = new TimeSeriesCollection();
final TimeSeries s1 = new TimeSeries("Series 1");
s1.add(new Minute(0, 0, 7, 12, 2003), 1.2);
s1.add(new Minute(30, 12, 7, 12, 2003), 3.0);
s1.add(new Minute(15, 14, 7, 12, 2003), 8.0);
final TimeSeries s2 = new TimeSeries("Series 2");
s2.add(new Minute(0, 3, 7, 12, 2003), 0.0);
s2.add(new Minute(30, 9, 7, 12, 2003), 0.0);
s2.add(new Minute(15, 10, 7, 12, 2003), 0.0);
dataset.addSeries(s1);
dataset.addSeries(s2);
return dataset;
}
protected JFreeChart createChart(Dataset dataset) {
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Time Series", // title
"", // x-axis label
"", // y-axis label
(XYDataset)dataset, // data
true, // create legend?
true, // generate tooltips?
false // generate URLs?
);
chart.setBackgroundPaint(Color.white);
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
XYItemRenderer r = plot.getRenderer();
if (r instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
renderer.setDrawSeriesLineAsPath(true);
}
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat(timeseriesformat));
return chart;
}
@Override
protected Dataset convert2Dataset(GraphData st) {
List<Point<? extends Number, ? extends Number>> pointslist = st.getData();
// NOTE: after the graph generation graphs are inverted in x and y
int numbOfRows = pointslist.size();
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
if (numbOfRows > 0) {
int numbOfCols = pointslist.get(0).getEntries().size();
// calclulation will be made only for the first series
for (int x = 0; x < numbOfRows; x++) {
String serieslabel = pointslist.get(x).getLabel();
TimeSeries xyseries = new TimeSeries(serieslabel);
for (int y = 0; y < numbOfCols; y++) {
String xlabel = pointslist.get(x).getEntries().get(y).getLabel();
double value = pointslist.get(x).getEntries().get(y).getValue().doubleValue();
Calendar cal = DateGuesser.convertDate(xlabel);
String granularity = DateGuesser.granularity(xlabel);
if (granularity.equals(DateGuesser.YEAR)) timeseriesformat = formatYear;
else if (granularity.equals(DateGuesser.MONTH)) timeseriesformat = formatMonth;
else if (granularity.equals(DateGuesser.DAY)) timeseriesformat = formatDay;
AnalysisLogger.getLogger().debug("TimeSeriesGraph-> granularity "+granularity+" format "+timeseriesformat);
xyseries.add(new Day(new Date(cal.getTimeInMillis())),value);
}
timeseriescollection.addSeries(xyseries);
}
}
return timeseriescollection;
}
@Override
protected GenericStandaloneGraph getInstance(String title) {
TimeSeriesGraph tsg = new TimeSeriesGraph(title);
tsg.timeseriesformat = timeseriesformat;
return tsg;
}
}

View File

@ -0,0 +1,293 @@
/* ===========================================================
* JFreeChart : a free chart library for the Java(tm) platform
* ===========================================================
*
* (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jfreechart/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------------
* PieGraph.java
* ------------------
* (C) Copyright 2003-2007, by Object Refinery Limited and Contributors.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): ;
*
* Changes
* -------
* 09-Mar-2005 : Version 1, copied from the demo collection that ships with
* the JFreeChart Developer Guide (DG);
*
*/
package org.gcube.contentmanagement.graphtools.plotting.graphs;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import org.gcube.contentmanagement.graphtools.abstracts.GenericStandaloneGraph;
import org.gcube.contentmanagement.graphtools.utils.MathFunctions;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.AxisState;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.CategoryTick;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.Dataset;
import org.jfree.text.TextBlock;
import org.jfree.text.TextBlockAnchor;
import org.jfree.text.TextLine;
import org.jfree.ui.RectangleEdge;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.TextAnchor;
public class TransectLineGraph extends GenericStandaloneGraph {
private static final long serialVersionUID = 1L;
public TransectLineGraph(String title) {
super(title);
}
protected Dataset generateDataset() {
// row keys...
String series1 = "First";
String series2 = "Second";
String series3 = "Third";
// column keys...
String type1 = "Type 1";
String type2 = "Type 2";
String type3 = "Type 3";
String type4 = "Type 4";
String type5 = "Type 5";
String type6 = "Type 6";
String type7 = "Type 7";
String type8 = "Type 8";
// create the dataset...
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, series1, type1);
dataset.addValue(4.0, series1, type2);
dataset.addValue(3.0, series1, type3);
dataset.addValue(5.0, series1, type4);
dataset.addValue(5.0, series1, type5);
dataset.addValue(7.0, series1, type6);
dataset.addValue(7.0, series1, type7);
dataset.addValue(8.0, series1, type8);
dataset.addValue(5.0, series2, type1);
dataset.addValue(7.0, series2, type2);
dataset.addValue(6.0, series2, type3);
dataset.addValue(8.0, series2, type4);
dataset.addValue(4.0, series2, type5);
dataset.addValue(4.0, series2, type6);
dataset.addValue(2.0, series2, type7);
dataset.addValue(1.0, series2, type8);
dataset.addValue(4.0, series3, type1);
dataset.addValue(3.0, series3, type2);
dataset.addValue(2.0, series3, type3);
dataset.addValue(3.0, series3, type4);
dataset.addValue(6.0, series3, type5);
dataset.addValue(3.0, series3, type6);
dataset.addValue(4.0, series3, type7);
dataset.addValue(3.0, series3, type8);
return dataset;
}
protected JFreeChart createChart(Dataset dataset) {
DefaultCategoryDataset dataset1 = new DefaultCategoryDataset();
DefaultCategoryDataset dataset2 = new DefaultCategoryDataset();
DefaultCategoryDataset dataset0 = (DefaultCategoryDataset)dataset;
ArrayList<Integer> relevantindexes = new ArrayList<Integer>();
for (Object row:dataset0.getRowKeys()){
int spikescounter=0;
int pointscounter=0;
int allcounter=0;
int mincolumns = 0;
int maxcolumns = dataset0.getColumnCount()-1;
int medcolumns = (maxcolumns)/2;
for (Object column:dataset0.getColumnKeys()){
// System.out.println("row "+row+" column "+column );
double value = dataset0.getValue((String)row, (String)column).doubleValue();
String xlab = (String) column;
String annotation ="";
String x1lab = xlab;
int commaindex = xlab.indexOf(";");
if (commaindex>0){
annotation = xlab.substring(commaindex+1);
x1lab = xlab.substring(0,commaindex);
dataset2.addValue(value, (String)row, ""+(allcounter+1)+": "+annotation);
spikescounter++;
relevantindexes.add(allcounter);
}
else{
if ((allcounter==mincolumns)||(allcounter==maxcolumns)||(allcounter==medcolumns))
relevantindexes.add(allcounter);
dataset2.addValue(value, (String)row, ""+(allcounter+1)+"");
pointscounter++;
}
allcounter++;
dataset1.addValue(value, (String)row, x1lab);
}
}
// create the chart...
JFreeChart chart = ChartFactory.createLineChart(
" ", // chart title
"", // domain axis label
"", // range axis label
(DefaultCategoryDataset)dataset1, // data
PlotOrientation.VERTICAL, // orientation
false, // include legend
true, // tooltips
false // urls
);
chart.setTitle(new TextTitle(" ", new Font("sansserif", Font.BOLD, 60)));
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = chart.getCategoryPlot();
plot.setBackgroundPaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setDomainCrosshairVisible(true);
plot.setDomainGridlinesVisible(true);
plot.setRangeCrosshairVisible(true);
// plot.setRenderer(new LineAndShapeRenderer(true,true));
plot.setRenderer(new LineAndShapeRenderer(true,false));
plot.setAxisOffset(new RectangleInsets(1D, 1D, 1D, 1D));
plot.setDomainAxis(0,new CustomXAxis("",dataset1,relevantindexes));
CategoryAxis categoryaxis1 = plot.getDomainAxis(0);
categoryaxis1.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
plot.mapDatasetToDomainAxis(0, 0);
plot.setDataset(1, (DefaultCategoryDataset)dataset2);
plot.setDomainAxis(1,new CustomXAxis("",dataset2,relevantindexes));
CategoryAxis categoryaxis2 = plot.getDomainAxis(1);
categoryaxis2.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
plot.mapDatasetToDomainAxis(1, 1);
plot.setDomainAxisLocation(1, AxisLocation.TOP_OR_LEFT);
// categoryaxis2.setLabelInsets(new RectangleInsets(100, 100, 100, 100));
// categoryaxis2.setLowerMargin(0.05D);
// categoryaxis2.setUpperMargin(1D);
// plot.mapDatasetToRangeAxis(1, 1);
//deprecated
/*
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
renderer.setShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true);
renderer.setFillPaint(Color.white);
*/
// rangeAxis.setStandardTickUnits(ValueAxis);
// rangeAxis.setAutoRangeIncludesZero(false);
// rangeAxis.setUpperMargin(0.12);
chart.setPadding(new RectangleInsets(30, 30, 90, 90));
big=true;
chart.getPlot().setBackgroundPaint(Color.white);
return chart;
}
@Override
protected GenericStandaloneGraph getInstance(String title) {
return new TransectLineGraph(title);
}
static class CustomXAxis extends CategoryAxis
{
DefaultCategoryDataset dataset;
List<Integer> samplingindexes;
public java.util.List refreshTicks(Graphics2D graphics2d, AxisState axisstate, Rectangle2D rectangle2d, RectangleEdge rectangleedge)
{
ArrayList arraylist = new ArrayList();
int size = dataset.getColumnCount();
for (int i=0;i<size;i++){
TextBlock tb = new TextBlock();
if (MathFunctions.isIn(samplingindexes, i)){
String xlab = (String)dataset.getColumnKeys().get(i);
// xlab = xlab.substring(xlab.indexOf(":")+1);
tb.addLine(new TextLine(xlab,new Font("sansserif", Font.BOLD, 8)));
}
else{
tb.addLine(new TextLine(""));
}
arraylist.add(new CategoryTick("p"+i, tb , TextBlockAnchor.CENTER_RIGHT,TextAnchor.CENTER_RIGHT, 0));
}
return arraylist;
}
public CustomXAxis(String s,DefaultCategoryDataset d,List<Integer> indexes)
{
super(s);
dataset = d;
samplingindexes = indexes;
}
}
}

View File

@ -0,0 +1,64 @@
package org.gcube.contentmanagement.graphtools.tests;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.plotting.graphs.GaussianDistributionGraph;
import org.gcube.contentmanagement.graphtools.plotting.graphs.HistogramGraph;
import org.gcube.contentmanagement.graphtools.plotting.graphs.LineGraph;
import org.gcube.contentmanagement.graphtools.plotting.graphs.PieGraph;
import org.gcube.contentmanagement.graphtools.plotting.graphs.RadarGraph;
import org.gcube.contentmanagement.graphtools.plotting.graphs.ScatterGraphGeneric;
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
public class RegressionTestAllGraphs {
public static void main(String[] args) throws Exception{
String table = "rdm7d053300d89e11e087918065b36ddd05";
String xDimension = "field3";
String yDimension = "field5";
String groupDimension = "field2";
String linesColumn = "field4";
String filter1 = "Perciformes";
String filter2 = "Osteichthyes";
StatisticsGenerator stg = new StatisticsGenerator();
LexicalEngineConfiguration conf = new LexicalEngineConfiguration();
//database Parameters
conf.setDatabaseURL("jdbc:postgresql://localhost/testdb");
conf.setDatabaseUserName("gcube");
conf.setDatabasePassword("d4science2");
conf.setDatabaseDriver("org.postgresql.Driver");
conf.setDatabaseDialect("org.hibernate.dialect.PostgreSQLDialect");
stg.init("./cfg/",conf);
// stg.addColumnFilter("field4", "F","=");
//String generation
GraphGroups gg = stg.generateGraphs(100, table, xDimension, yDimension, groupDimension, linesColumn, filter1, filter2);
//graph plot
RadarGraph series = new RadarGraph("");
series.renderGraphGroup(gg);
HistogramGraph series2 = new HistogramGraph("");
series2.renderGraphGroup(gg);
LineGraph series3 = new LineGraph("");
series3.renderGraphGroup(gg);
PieGraph series4 = new PieGraph("");
series4.renderGraphGroup(gg);
GaussianDistributionGraph series5 = new GaussianDistributionGraph("");
series5.renderGraphGroup(gg);
ScatterGraphGeneric series6 = new ScatterGraphGeneric("");
series6.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,40 @@
package org.gcube.contentmanagement.graphtools.tests.old;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.data.BigSamplesTable;
import org.gcube.contentmanagement.graphtools.plotting.graphs.TimeSeriesGraph;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
import com.rapidminer.RapidMiner;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.operator.preprocessing.sampling.AbsoluteSampling;
import com.rapidminer.tools.OperatorService;
import com.rapidminer.tools.math.MathFunctions;
public class ExampleDerivative {
public static void main(String[] args) throws Exception{
String table = "ts_a904da30_b4fc_11df_800d_bcef80d51986";
String xDimension = "field1";
String yDimension = "field4";
String groupDimension = "field2";
String speciesColumn = "field3";
String filter1 = "Toluene";
// String filter2 = "Osteichthyes";
StatisticsGenerator stg = new StatisticsGenerator();
stg.init("./cfg/");
GraphGroups gg = stg.generateGraphs(200, table, xDimension, yDimension, groupDimension, speciesColumn, filter1);
TimeSeriesGraph series = new TimeSeriesGraph("");
series.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,79 @@
package org.gcube.contentmanagement.graphtools.tests.old;
import org.gcube.contentmanagement.graphtools.data.BigSamplesTable;
import com.rapidminer.RapidMiner;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.operator.preprocessing.sampling.AbsoluteSampling;
import com.rapidminer.tools.OperatorService;
import com.rapidminer.tools.math.MathFunctions;
public class ExampleMathFunctions {
public static void main(String[] args) throws Exception{
double variance = MathFunctions.variance(new double[] { 0.1, 0.1, 0.0, -0.1 }, Double.NEGATIVE_INFINITY);
double covariance = MathFunctions.correlation(new double[] { 0.1, 0.2, -0.3, 0.0 }, new double[] { 0.0, 0.1, 0.1, -0.1 });
double meanc = mean(new double[] { 0.1, 0.1, 0.0, -0.1 });
// String pluginDirString = new File("C:/Users/coro/Desktop/WorkFolder/Workspace/RapidMinerPlugins/TextProcessing/Vega/lib/").getAbsolutePath();
// System.setProperty(RapidMiner.PROPERTY_RAPIDMINER_INIT_PLUGINS_LOCATION, pluginDirString);
System.setProperty("rapidminer.init.operators","C:/Users/coro/Desktop/WorkFolder/Workspace/RapidMiner_Wasat/resources/operators.xml");
RapidMiner.init ();
BigSamplesTable bst = new BigSamplesTable();
bst.addSampleRow("prova 1", 10, 12,13,14,15);
bst.addSampleRow("prova 2", 20, 15,14,15);
bst.addSampleRow("prova 3", 30, 11,110,150);
System.out.println(bst.toString());
// OperatorService.createOperator(ExampleSource.class);
// as.apply(bst.generateExampleSet());
// OperatorChain wvtoolOperator = (OperatorChain) OperatorService.createOperator(SingleTextInput.class);
// Learner learner = (Learner)OperatorService .createOperator("J48");
AbsoluteSampling as = (AbsoluteSampling)OperatorService .createOperator("AbsoluteSampling");
// SimpleSampling ss = (SimpleSampling)OperatorService .createOperator("Sampling");
// ss.setParameter("sample_ratio", "0.8");
as.setParameter("sample_size", "2");
as.setParameter("local_random_seed", "-1");
ExampleSet exampleSet = bst.generateExampleSet();
System.out.println(exampleSet.toResultString());
// ExampleSet exampleSetOut = ss.apply(exampleSet);
ExampleSet exampleSetOut = as.apply(exampleSet);
System.out.println(exampleSetOut.toResultString());
BigSamplesTable bstOut = new BigSamplesTable();
bstOut.generateSampleTable(exampleSetOut);
System.out.println(bstOut.toString());
// as.apply(es);
System.out.println("variance "+variance+" covariance "+covariance+" mean "+meanc);
}
//================================================= mean
public static double mean(double[] p) {
double sum = 0; // sum of all the elements
for (int i=0; i<p.length; i++) {
sum += p[i];
}
return sum / p.length;
}//end method mean
}

View File

@ -0,0 +1,62 @@
package org.gcube.contentmanagement.graphtools.tests.old;
import java.io.IOException;
import com.rapidminer.RapidMiner;
import com.rapidminer.example.Attribute;
import com.rapidminer.example.ExampleReader;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.operator.IOContainer;
import com.rapidminer.operator.Model;
import com.rapidminer.operator.ModelApplier;
import com.rapidminer.operator.Operator;
import com.rapidminer.operator.OperatorCreationException;
import com.rapidminer.operator.OperatorException;
import com.rapidminer.operator.io.ExampleSource;
import com.rapidminer.operator.learner.Learner;
import com.rapidminer.tools.OperatorService;
public class ExampleOperatorsInvoking {
public static void main(String [] args) {
try {
System.setProperty("rapidminer.init.operators", "./cfg/operators.xml");
RapidMiner. init ();
// learn
Operator exampleSource = OperatorService .createOperator(ExampleSource.class);
exampleSource.setParameter(" attributes ",
"/path/to/your/training data .xml");
IOContainer container = exampleSource.apply(new IOContainer());
ExampleSet exampleSet = container.get(ExampleSet.class);
// here the string based creation must be used since the J48 operator
// do not have an own class ( derived from the Weka library ).
Learner learner = (Learner)OperatorService .createOperator("J48");
Model model = learner. learn (exampleSet);
// loading the test set (plus adding the model to result container )
Operator testSource = OperatorService .createOperator(ExampleSource.class);
testSource .setParameter(" attributes ", "/path/to/your/test data.xml");
container = testSource.apply(new IOContainer());
container = container.append(model);
// applying the model
Operator modelApp =
OperatorService .createOperator(ModelApplier.class );
container = modelApp.apply(container);
// print results
ExampleSet resultSet = container.get(ExampleSet.class);
// Attribute predictedLabel = resultSet.getPredictedLabel ();
// ExampleReader reader = resultSet.getExampleReader();
// while (reader .hasNext()) {
// System.out. println (reader .next (). getValueAsString( predictedLabel ));
// }
} catch (OperatorCreationException e) {
System.err . println ("Cannot create operator:" + e.getMessage());
} catch (OperatorException e) {
System.err . println ("Cannot create model: " + e.getMessage());
}
}
}

View File

@ -0,0 +1,73 @@
package org.gcube.contentmanagement.graphtools.tests.old;
import com.rapidminer.RapidMiner;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.operator.IOContainer;
import com.rapidminer.operator.Operator;
import com.rapidminer.operator.OperatorException;
import com.rapidminer.operator.generator.ExampleSetGenerator;
import com.rapidminer.tools.OperatorService;
public class ExampleRMUsage {
/**
* @param args
*/
public static void main(String[] args) {
// create process
com.rapidminer.Process process = createProcess();
// print process setup
System.out. println (process .getRootOperator().createProcessTree (0));
try {
// perform process
process.run ();
// to run the process with input created by your application use
// process .run(new IOContainer(new IOObject[] f ... your objects ... g);
}catch (OperatorException e) { e. printStackTrace (); }
}
public static com.rapidminer.Process createProcess() {
System.out.println("INIT R-I");
// invoke init before using the OperatorService
RapidMiner.init();
System.out.println("INIT FINISHED");
// create process
com.rapidminer.Process process = new com.rapidminer.Process();
try {
// create operator
Operator inputOperator = OperatorService.createOperator(ExampleSetGenerator.class);
// set parameters
inputOperator.setParameter("target function", "sum classification");
// add operator to process
process.getRootOperator().addOperator(inputOperator,0);
// add other operators and set parameters
// [...]
System.out.println("INIT PROCESS FINISHED");
} catch (Exception e) {
e.printStackTrace();
}
return process;
}
public static IOContainer createInput () {
// create a wrapper that implements the ExampleSet interface and
// encapsulates your data
// ...
return new IOContainer();
}
}

View File

@ -0,0 +1,58 @@
package org.gcube.contentmanagement.graphtools.tests.old;
import java.util.LinkedList;
import java.util.List;
import com.rapidminer.example.Attribute;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.example.table.AttributeFactory;
import com.rapidminer.example.table.DoubleArrayDataRow;
import com.rapidminer.example.table.MemoryExampleTable;
import com.rapidminer.tools.Ontology;
public class ExampleUserSampleSet {
public static void main(String[] argv) {
// create attribute list
List<Attribute> attributes = new LinkedList<Attribute>();
for (int a = 0; a < getMyNumOfAttributes(); a++) {
attributes.add(AttributeFactory.createAttribute("att" + a, Ontology.REAL));
}
Attribute label = AttributeFactory.createAttribute(" label ", Ontology.NOMINAL);
attributes.add(label);
// create table
MemoryExampleTable table = new MemoryExampleTable(attributes);
// ll table (here : only real values )
for (int d = 0; d < getMyNumOfDataRows(); d++) {
double[] data = new double[attributes.size()];
for (int a = 0; a < getMyNumOfAttributes(); a++) {
// all with proper data here
data[a] = getMyValue(d, a);
}
// maps the nominal classi cation to a double value
data[data.length - 1] = label.getMapping().mapString(getMyClassification(d));
// add data row
table.addDataRow(new DoubleArrayDataRow(data));
}
// create example set
ExampleSet exampleSet = table.createExampleSet(label);
}
//to be defined in future applications
private static int getMyNumOfAttributes() {
return 3;
}
private static int getMyNumOfDataRows() {
return 10;
}
private static double getMyValue(int d, int a) {
return 10;
}
private static String getMyClassification(int d) {
return "ciao";
}
}

View File

@ -0,0 +1,70 @@
package org.gcube.contentmanagement.graphtools.tests.old;
import java.util.List;
import org.gcube.contentmanagement.graphtools.data.BigSamplesTable;
import org.gcube.contentmanagement.graphtools.data.conversions.GraphConverter2D;
import org.gcube.portlets.user.timeseries.charts.support.types.Point;
public class TestConverters {
public static void main1(String[] args) throws Exception{
BigSamplesTable bst = new BigSamplesTable();
bst.addSampleRow("prova 1", 10, 15);
bst.addSampleRow("prova 2", 10, 12);
bst.addSampleRow("prova 3", 30, 11);
bst.addSampleRow("prova 1", 10, 15);
bst.addSampleRow("prova 2", 10, 12);
bst.addSampleRow("prova 3", 30, 11);
bst.addSampleRow("prova 1", 10, 15);
bst.addSampleRow("prova 2", 10, 12);
bst.addSampleRow("prova 3", 30, 11);
bst.addSampleRow("prova 1", 10, 15);
bst.addSampleRow("prova 2", 10, 12);
bst.addSampleRow("prova 3", 30, 11);
bst.addSampleRow("prova 1", 10, 15);
bst.addSampleRow("prova 2", 10, 12);
bst.addSampleRow("prova 3", 30, 11);
bst.addSampleRow("prova 1", 10, 15);
bst.addSampleRow("prova 2", 10, 12);
bst.addSampleRow("prova 3", 30, 11);
bst.addSampleRow("prova 1", 10, 15);
bst.addSampleRow("prova 2", 10, 12);
bst.addSampleRow("prova 3", 30, 11);
System.out.println(bst.toString());
List<Point<? extends Number, ? extends Number>> graphicus = GraphConverter2D.convert(bst);
graphicus = GraphConverter2D.reduceDimension(graphicus);
System.out.println("finished!");
}
public static void main(String[] args) throws Exception{
BigSamplesTable bst = new BigSamplesTable();
bst.addSampleRow("1;prova y1", 0, 0);
bst.addSampleRow("1;prova y2", 0, 0);
bst.addSampleRow("1;prova y3", 0, 0);
bst.addSampleRow("prova 1;prova y1", 10, 15);
bst.addSampleRow("prova 2;prova y2", 10, 12);
bst.addSampleRow("prova 3;prova y2", 30, 11);
bst.addSampleRow("prova 3;prova y1", 30, 10);
System.out.println(bst.toString());
List<Point<? extends Number, ? extends Number>> graphicus = GraphConverter2D.convert(bst);
graphicus = GraphConverter2D.reduceDimension(graphicus);
System.out.println("finished!");
}
}

View File

@ -0,0 +1,55 @@
package org.gcube.contentmanagement.graphtools.tests.old;
import java.util.List;
import java.util.Map;
import org.gcube.contentmanagement.graphtools.abstracts.SamplesTable;
import org.gcube.contentmanagement.graphtools.data.conversions.GraphConverter2D;
import org.gcube.contentmanagement.graphtools.data.databases.CommonDBExtractor;
import org.gcube.contentmanagement.lexicalmatcher.utils.DatabaseFactory;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphData;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
import org.gcube.portlets.user.timeseries.charts.support.types.Point;
import org.hibernate.SessionFactory;
public class TestDBExtractor {
public static void main(String[] args) throws Exception{
SessionFactory referenceDBSession = DatabaseFactory.initDBConnection("./hibernate.cfg.xml");
CommonDBExtractor extractor = new CommonDBExtractor(referenceDBSession);
String table = "ts_161efa00_2c32_11df_b8b3_aa10916debe6";
String xDimension = "field5";
String groupDimension = "field1";
String yValue = "field6";
String speciesColumn = "field3";
String filter1 = "Brown seaweeds";
String filter2 = "River eels";
Map<String, SamplesTable> samplesMap = extractor.getMultiDimTemporalTables(table, xDimension, groupDimension, yValue, speciesColumn, filter1, filter2);
System.out.println("MAP EXTRACTED : \n"+samplesMap.toString());
GraphGroups graphgroups = new GraphGroups();
for (String key:samplesMap.keySet()){
SamplesTable stable = samplesMap.get(key);
List<Point<? extends Number, ? extends Number>> singlegraph = GraphConverter2D.transformTable(stable);
GraphData grd = new GraphData(singlegraph,true);
graphgroups.addGraph("Distribution for "+key, grd);
}
System.out.println("finished!");
}
}

View File

@ -0,0 +1,40 @@
package org.gcube.contentmanagement.graphtools.tests.old;
import java.io.File;
import com.rapidminer.RapidMiner;
import com.rapidminer.operator.IOContainer;
import com.rapidminer.operator.Model;
import com.rapidminer.operator.ModelApplier;
import com.rapidminer.operator.Operator;
import com.rapidminer.operator.OperatorChain;
import com.rapidminer.operator.io.ModelLoader;
import com.rapidminer.tools.OperatorService;
public class TextTest {
public static void main(String[] argv) throws Exception {
String pluginDirString = new File("C:\\Dokumente und Einstellungen\\Mierswa\\Eigene Dateien\\workspace\\RMTextTest\\lib").getAbsolutePath();
System.setProperty(RapidMiner.PROPERTY_RAPIDMINER_INIT_PLUGINS_LOCATION, pluginDirString);
File wordListFile = new File(".");
File modelFile = new File(".");
RapidMiner.init();
/*
OperatorChain wvtoolOperator = (OperatorChain) OperatorService.createOperator(SingleTextInput.class);
wvtoolOperator.setParameter("input_word_list", wordListFile.getAbsolutePath());
wvtoolOperator.addOperator(OperatorService.createOperator(SimpleTokenizer.class));
wvtoolOperator.addOperator(OperatorService.createOperator(PorterStemmer.class));
Operator modelApplier = OperatorService.createOperator(ModelApplier.class);
Operator modelLoader = OperatorService.createOperator(ModelLoader.class);
modelLoader.setParameter(ModelLoader.PARAMETER_MODEL_FILE, modelFile.getAbsolutePath());
IOContainer container = modelLoader.apply(new IOContainer());
Model model = container.get(Model.class);
*/
}
}

View File

@ -0,0 +1,61 @@
package org.gcube.contentmanagement.graphtools.tests.show;
import org.gcube.contentmanagement.graphtools.core.StatisticsGenerator;
import org.gcube.contentmanagement.graphtools.plotting.graphs.GaussianDistributionGraph;
import org.gcube.contentmanagement.graphtools.plotting.graphs.HistogramGraph;
import org.gcube.contentmanagement.graphtools.plotting.graphs.LineGraph;
import org.gcube.contentmanagement.graphtools.plotting.graphs.PieGraph;
import org.gcube.contentmanagement.graphtools.plotting.graphs.RadarGraph;
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
import org.gcube.portlets.user.timeseries.charts.support.types.GraphGroups;
public class ExampleStringGraphData {
public static void main(String[] args) throws Exception{
String table = "ts_3637f670_430c_11df_a0a2_909e7d074592";
String xDimension = "field5";
String yDimension = "field6";
String groupDimension = "field1";
String speciesColumn = "field3";
String filter1 = "Crabs, sea-spiders";
String filter2 = "Marine fishes not identified";
StatisticsGenerator stg = new StatisticsGenerator();
LexicalEngineConfiguration conf = new LexicalEngineConfiguration();
//database Parameters
conf.setDatabaseUserName("root");
// conf.setDatabasePassword("password");
conf.setDatabaseDriver("com.mysql.jdbc.Driver");
conf.setDatabaseURL("jdbc:mysql://localhost/timeseries");
conf.setDatabaseDialect("org.hibernate.dialect.MySQLDialect");
conf.setDatabaseAutomaticTestTable("connectiontesttable");
conf.setDatabaseIdleConnectionTestPeriod("3600");
stg.init("./cfg/",conf);
stg.addColumnFilter("field4", "F","=");
//String generation
GraphGroups gg = stg.generateGraphs(100, table, xDimension, yDimension, groupDimension, speciesColumn, filter1, filter2);
//graph plot
RadarGraph series = new RadarGraph("");
series.renderGraphGroup(gg);
HistogramGraph series2 = new HistogramGraph("");
series2.renderGraphGroup(gg);
LineGraph series3 = new LineGraph("");
series3.renderGraphGroup(gg);
PieGraph series4 = new PieGraph("");
series4.renderGraphGroup(gg);
GaussianDistributionGraph series5 = new GaussianDistributionGraph("");
series5.renderGraphGroup(gg);
}
}

View File

@ -0,0 +1,44 @@
package org.gcube.contentmanagement.graphtools.tests;
import java.util.List;
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.contentmanagement.lexicalmatcher.utils.DatabaseFactory;
import org.hibernate.SessionFactory;
public class testConnections {
public static void main(String args[]) throws Exception{
LexicalEngineConfiguration conf = new LexicalEngineConfiguration();
conf.setDatabaseURL("jdbc:postgresql://localhost/testdb");
conf.setDatabaseUserName("gcube");
conf.setDatabasePassword("d4science2");
conf.setDatabaseDriver("org.postgresql.Driver");
conf.setDatabaseDialect("org.hibernate.dialect.PostgreSQLDialect");
String hibernateDefaultFile = "hibernate.cfg.xml";
String loggerDefaultFile = "ALog.properties";
String configPath = "./cfg/";
AnalysisLogger.setLogger(configPath+loggerDefaultFile);
SessionFactory session = DatabaseFactory.initDBConnection(configPath+hibernateDefaultFile,conf);
List<Object> resultSet = DatabaseFactory.executeSQLQuery("select * from hcaf_s limit 10", session);
for (Object result:resultSet){
Object [] row = (Object[]) result;
for (int i=0;i<row.length;i++){
System.out.print(""+row[i]+" ");
}
System.out.println();
}
// DatabaseFactory.executeSQLUpdate("set ... ", session);
session.close();
}
}

View File

@ -0,0 +1,151 @@
package org.gcube.contentmanagement.graphtools.utils;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class DateGuesser {
public static void main(String[] args) {
Calendar c = convertDate("20/2/2010 0:49:00");
System.out.println("giorno " + c.get(Calendar.DAY_OF_MONTH) + " mese " + (c.get(Calendar.MONTH) + 1) + " anno " + c.get(Calendar.YEAR));
System.out.println("pattern "+getPattern("20/2/2010 0:49:00") );
System.out.println("pattern "+getPattern("2009-05-12 15:42:10") );
System.out.println("pattern "+getPattern("1970") );
System.out.println("pattern "+getPattern("2009-05-12") );
System.out.println("pattern "+getPattern("prova") );
System.out.println("pattern "+getPattern("9/26/2010 1:49:00"));
c = convertDate("2009-05-12");
System.out.println("giorno " + c.get(Calendar.DAY_OF_MONTH) + " mese " + (c.get(Calendar.MONTH) + 1) + " anno " + c.get(Calendar.YEAR));
// Properties prop = new Properties(System.getProperties());
// prop.list(System.out);
}
public static Calendar convertDate(String data) {
return convertDate(data,null);
}
public static String getPattern(String data) {
return getPattern(data,null);
}
// private static final String[] formats = { "MM\\dd\\yyyy", "MM\\dd\\yy", "MM/dd/yy", "MM/dd/yyyy", "dd/MM/yy", "dd/MM/yyyy", "dd/MM/yyyy HH:mm:ss", "dd/MM/yy HH:mm:ss", "dd/MM/yyyy HH:mm:ss","MM/yy","MM/yyyy", "yyyy.MM.dd G 'at' HH:mm:ss z", "EEE, MMM d, ''yy", "h:mm a", "hh 'o''clock' a, zzzz", "K:mm a, z", "MM-dd-yy","MM-dd-yyyy", "dd-MMM-yy", "yyyy.MM.dd.HH.mm.ss", "E, dd MMM yyyy HH:mm:ss Z", "yyyyy.MMMMM.dd GGG hh:mm aaa", "EEE, d MMM yyyy HH:mm:ss Z", "yyMMddHHmmssZ", "yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd HH:mm","yyyy-MM-dd","yyyy-MM-dd HH:mm:ss", "h:mm a", "yyyy"};
private static final String[] formats = { "MM\\dd\\yyyy", "MM\\dd\\yy", "MM/dd/yy", "MM/dd/yyyy", "MM/yy","MM/yyyy", "yyyy.MM.dd G 'at' HH:mm:ss z", "MM/dd/yyyy HH:mm:ss","MM/dd/yyyy HH:mm:ss aaa","dd/MM/yyyy HH:mm:ss","EEE, MMM d, ''yy", "h:mm a", "hh 'o''clock' a, zzzz", "K:mm a, z", "MM-dd-yy","MM-dd-yyyy", "dd-MMM-yy", "yyyy.MM.dd.HH.mm.ss", "E, dd MMM yyyy HH:mm:ss Z", "yyyyy.MMMMM.dd GGG hh:mm aaa", "EEE, d MMM yyyy HH:mm:ss Z", "yyMMddHHmmssZ", "yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd HH:mm","yyyy-MM-dd","yyyy-MM-dd HH:mm:ss", "h:mm a", "yyyy"};
private static final String[] formatiITA = { "dd\\MM\\yyyy", "dd\\MM\\yy", "dd/MM/yy", "dd/MM/yyyy", "dd/MM/yy", "dd/MM/yyyy","dd/MM/yyyy HH:mm:ss", "dd/MM/yy HH:mm:ss", "dd/MM/yyyy HH:mm:ss", "MM/yy","MM/yyyy","dd.MM.yyyy G 'alle' HH:mm:ss z", "EEE, MMM d, ''yy", "h:mm a", "hh a, zzzz", "K:mm a, z", "dd-MMM-yy", "dd.MM.yyyy.HH.mm.ss", "E, dd MMM yyyy HH:mm:ss Z", "yyyyy.MMMMM.dd GGG hh:mm aaa", "EEE, d MMM yyyy HH:mm:ss Z", "yyMMddHHmmssZ", "yyyy-MM-dd'T'HH:mm:ss.SSSZ", "dd-MMM-yyyy HH:mm", "h:mm a", "yyyy"};
public static String getPattern(String data, String language) {
//String language = System.getProperty("user.language");
Locale l = Locale.UK;
if (language!=null && language.equalsIgnoreCase("it"))
l = Locale.ITALY;
Date dat = null;
DateFormat formatter = null;
String[] formati = formats;
if (l.equals(Locale.ITALY))
formati = formatiITA;
int index = -1;
for (int i = 0; i < formati.length; i++) {
try {
formatter = new SimpleDateFormat(formati[i],l);
dat = (Date) formatter.parse(data);
if (index == -1)
index = i;
else if (formati[index].length()<=formati[i].length())
index = i;
// System.out.println(formati[i]);
} catch (Exception e) {
// e.printStackTrace();
}
}
if (dat!=null){
return formati[index];
}
else
return null;
}
public static Calendar convertDate(String data, String language) {
//String language = System.getProperty("user.language");
Locale l = Locale.UK;
if (language!=null && language.equalsIgnoreCase("it"))
l = Locale.ITALY;
Date dat = null;
DateFormat formatter = null;
String[] formati = formats;
if (l.equals(Locale.ITALY))
formati = formatiITA;
int index = -1;
Date bestDate = null;
for (int i = 0; i < formati.length; i++) {
try {
formatter = new SimpleDateFormat(formati[i],l);
dat = (Date) formatter.parse(data);
if (index==-1){
bestDate = dat;
index = i;
}
else if (formati[index].length()<=formati[i].length()){
bestDate = dat;
index = i;
}
// break;
} catch (Exception e) {
// e.printStackTrace();
}
}
if (bestDate!=null){
Calendar c = Calendar.getInstance();
c.setTime(bestDate);
// System.out.println("data "+data+" giorno " + c.get(Calendar.DAY_OF_MONTH) + " mese " + (c.get(Calendar.MONTH) + 1) + " anno " + c.get(Calendar.YEAR));
return c;
}
else
return null;
}
public static final String YEAR = "YEAR";
public static final String MONTH = "MONTH";
public static final String DAY = "DAY";
public static String granularity(String pattern) {
SimpleDateFormat formatter = null;
try{
formatter = new SimpleDateFormat("MM/dd/yyyy");
formatter.parse(pattern);
return DAY;
}catch(Exception e){}
try{
formatter = new SimpleDateFormat("MM/yyyy");
formatter.parse(pattern);
return MONTH;
}catch(Exception e){}
try{
formatter = new SimpleDateFormat("MM/yy");
formatter.parse(pattern);
return MONTH;
}catch(Exception e){}
return YEAR;
}
}

View File

@ -0,0 +1,282 @@
package org.gcube.contentmanagement.graphtools.utils;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Type;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import com.google.gson.Gson;
public class HttpRequest {
/**
* Sends an HTTP GET request to a url
*
* @param endpoint
* - The URL of the server. (Example: " http://www.yahoo.com/search")
* @param requestParameters
* - all the request parameters (Example: "param1=val1&param2=val2"). Note: This method will add the question mark (?) to the request - DO NOT add it yourself
* @return - The response from the end point
*/
public static String sendGetRequest(String endpoint, String requestParameters) {
String result = null;
if (endpoint.startsWith("http://")) {
// Send a GET request to the servlet
try {
// Send data
String urlStr = endpoint;
if (requestParameters != null && requestParameters.length() > 0) {
urlStr += "?" + requestParameters;
}
URL url = new URL(urlStr);
URLConnection conn = url.openConnection();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
result = sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* Reads data from the data reader and posts it to a server via POST request. data - The data you want to send endpoint - The server's address output - writes the server's response to output
*
* @throws Exception
*/
public static void postData(Reader data, URL endpoint, Writer output) throws Exception {
HttpURLConnection urlc = null;
try {
urlc = (HttpURLConnection) endpoint.openConnection();
try {
urlc.setRequestMethod("POST");
} catch (ProtocolException e) {
throw new Exception("Shouldn't happen: HttpURLConnection doesn't support POST??", e);
}
urlc.setDoOutput(true);
urlc.setDoInput(true);
urlc.setUseCaches(false);
urlc.setAllowUserInteraction(false);
urlc.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");
OutputStream out = urlc.getOutputStream();
try {
Writer writer = new OutputStreamWriter(out, "UTF-8");
pipe(data, writer);
writer.close();
} catch (IOException e) {
throw new Exception("IOException while posting data", e);
} finally {
if (out != null)
out.close();
}
InputStream in = urlc.getInputStream();
try {
Reader reader = new InputStreamReader(in);
pipe(reader, output);
reader.close();
} catch (IOException e) {
throw new Exception("IOException while reading response", e);
} finally {
if (in != null)
in.close();
}
} catch (IOException e) {
throw new Exception("Connection error (is server running at " + endpoint + " ?): " + e);
} finally {
if (urlc != null)
urlc.disconnect();
}
}
// performs a simple Get from a remote url
public static Object getJSonData(String endpoint, String requestParameters, Type outputClass) throws Exception {
String output = sendGetRequest(endpoint, requestParameters);
Gson gson = new Gson();
// AnalysisLogger.getLogger().debug("HttpRequest-> OUTPUT JSON:\n"+output.toString());
// Output the response
Object rebuiltJson = gson.fromJson(output.toString(), outputClass);
return rebuiltJson;
}
// performs a simple transformation to a json object
public static String toJSon(Object obj) {
Gson gson = new Gson();
String jsonString = gson.toJson(obj);
return jsonString;
}
public static Object postJSonData(String endpoint, Object obj, Type outputClass) throws Exception {
HttpURLConnection urlc = null;
try {
// Send the request
URL url = new URL(endpoint);
urlc = (HttpURLConnection) url.openConnection();
try {
urlc.setRequestMethod("POST");
} catch (ProtocolException e) {
throw new Exception("Error in HttpURLConnection", e);
}
urlc.setDoOutput(true);
urlc.setDoInput(true);
urlc.setUseCaches(false);
urlc.setAllowUserInteraction(false);
urlc.setRequestProperty("Content-type", "application/json; charset=" + "UTF-8");
OutputStreamWriter writer = null;
Gson gson = new Gson();
if (obj != null) {
OutputStream out = urlc.getOutputStream();
writer = new OutputStreamWriter(out);
// write parameters
String jsonString = gson.toJson(obj);
AnalysisLogger.getLogger().trace("INPUT JSON:\n" + jsonString);
writer.write(jsonString);
writer.flush();
}
// Get the response
StringBuffer answer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(urlc.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
answer.append(line);
}
if (obj != null)
writer.close();
reader.close();
// AnalysisLogger.getLogger().debug("OUTPUT JSON:\n"+answer.toString());
// Output the response
Object rebuiltJson = gson.fromJson(answer.toString(), outputClass);
return rebuiltJson;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
/**
* Pipes everything from the reader to the writer via a buffer
*/
private static void pipe(Reader reader, Writer writer) throws IOException {
char[] buf = new char[1024];
int read = 0;
while ((read = reader.read(buf)) >= 0) {
writer.write(buf, 0, read);
}
writer.flush();
}
public static String sendPostRequest(String endpoint, String requestParameters) {
// Build parameter string
String data = requestParameters;
try {
// Send the request
URL url = new URL(endpoint);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
// write parameters
writer.write(data);
writer.flush();
// Get the response
StringBuffer answer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
answer.append(line);
}
writer.close();
reader.close();
// Output the response
return answer.toString();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
public static String ManageCDATA(String phrase) {
return phrase.replace("<![CDATA[", "").replace("]]>", "");
}
public static String AddCDATA(String phrase) {
return "<![CDATA[" + phrase + "]]>";
}
public static int checkUrl(String url, final String username, final String password) {
int checkConn = -1;
try {
if ((username != null) && (password != null)) {
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
});
}
URL checkurl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) checkurl.openConnection();
checkConn = conn.getResponseCode();
conn.disconnect();
} catch (Exception e) {
System.out.println("ERROR in URL " + e.getMessage());
}
return checkConn;
}
public static void main(String[] args) {
String url = "http://geoserver-dev.d4science-ii.research-infrastructures.eu/geoserver/rest/layergroups/group4402c0cff-27e3-4606-a2f1-993ad37c3dfb.json";
int d = checkUrl(url, "admin", "gcube@geo2010");
System.out.println(d);
}
}

View File

@ -0,0 +1,253 @@
package org.gcube.contentmanagement.graphtools.utils;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import org.gcube.portlets.user.timeseries.charts.support.types.Point;
public class MathFunctions {
/**
* @param args
*/
public static void main(String[] args) {
/*
double[] a = logSubdivision(1,4874,5);
for (int i =0;i<a.length;i++){
System.out.print(a[i]+" ");
}
*/
System.out.println(" "+roundDecimal(300.23454,2));
}
//rounds to the xth decimal position
public static double roundDecimal(double number,int decimalposition){
double n = (double)Math.round(number * Math.pow(10.00,decimalposition))/Math.pow(10.00,decimalposition);
return n;
}
// increments a percentage o mean calculation when a lot of elements are present
public static float incrementPerc(float perc, float quantity, int N) {
if (N == 0)
return quantity;
float out = 0;
int N_plus_1 = N + 1;
out = (float) ((perc + ((double) quantity / (double) N)) * ((double) N / (double) N_plus_1));
return out;
}
public static ArrayList<Integer> generateRandoms(int numberOfRandoms, int min, int max) {
ArrayList<Integer> randomsSet = new ArrayList<Integer>();
// if number of randoms is equal to -1 generate all numbers
if (numberOfRandoms == -1) {
for (int i = min; i < max; i++) {
randomsSet.add(i);
}
} else {
int numofrandstogenerate = 0;
if (numberOfRandoms <= max) {
numofrandstogenerate = numberOfRandoms;
} else {
numofrandstogenerate = max;
}
if (numofrandstogenerate == 0) {
randomsSet.add(0);
} else {
for (int i = 0; i < numofrandstogenerate; i++) {
int RNum = -1;
RNum = (int) ((max) * Math.random()) + min;
// generate random number
while (randomsSet.contains(RNum)) {
RNum = (int) ((max) * Math.random()) + min;
// AnalysisLogger.getLogger().debug("generated " + RNum);
}
// AnalysisLogger.getLogger().debug("generated " + RNum);
if (RNum >= 0)
randomsSet.add(RNum);
}
}
}
return randomsSet;
}
public static int[] generateSequence(int elements) {
int[] sequence = new int[elements];
for (int i = 0; i < elements; i++) {
sequence[i] = i;
}
return sequence;
}
public static BigInteger chunk2Index(int chunkIndex, int chunkSize) {
return BigInteger.valueOf(chunkIndex).multiply(BigInteger.valueOf(chunkSize));
}
// calculates mean
public static double mean(double[] p) {
double sum = 0; // sum of all the elements
for (int i = 0; i < p.length; i++) {
sum += p[i];
}
return sum / p.length;
}// end method mean
//calculates normalized derivative
public static double[] derivative(double[] a) {
double[] d = new double[a.length];
double max = 1;
if (a.length > 0) {
for (int i = 0; i < a.length; i++) {
double current = a[i];
double previous = current;
if (i > 0) {
previous = a[i - 1];
}
d[i] = current - previous;
if (Math.abs(d[i])>max)
max = Math.abs(d[i]);
// System.out.println("point "+a[i]+" derivative "+d[i]);
}
//normalize
for (int i = 0; i < a.length; i++) {
d[i] = d[i]/max;
}
}
return d;
}
// returns a list of spikes indexes
public static boolean[] findSpikes(double[] derivative,double threshold) {
boolean[] d = new boolean[derivative.length];
if (d.length > 0) {
d[0] = false;
for (int i = 1; i < derivative.length - 1; i++) {
if (derivative[i] / derivative[i + 1] < 0){
// double ratio = Math.abs((double) derivative[i]/ (double) derivative[i+1]);
// System.out.println("RATIO "+i+" "+Math.abs(derivative[i]));
// if ((threshold>0)&&(ratio<threshold))
if ((threshold>0)&&(Math.abs(derivative[i])>threshold))
d[i] = true;
}
else
d[i] = false;
}
d[derivative.length - 1] = false;
}
return d;
}
// returns a list of spikes indexes
public static boolean[] findSpikes(double[] derivative) {
return findSpikes(derivative,-1);
}
// transforms a list of points for a series in a double vector of y values
// it applies ONLY to transposed graphs not to extracted list of points (see GraphSamplesTable)
public static double[] points2Double(List<Point<? extends Number, ? extends Number>> pointslist, int seriesIndex, int numbOfPoints) {
double[] points = new double[numbOfPoints];
// System.out.print("points: ");
for (int y = 0; y < numbOfPoints; y++) {
double value = pointslist.get(seriesIndex).getEntries().get(y).getValue().doubleValue();
points[y] = value;
// System.out.print(value+" ");
}
return points;
}
// searches for an index into an array
public static boolean isIn(List<Integer> indexarray, int index) {
int size = indexarray.size();
for (int i = 0; i < size; i++) {
if (index == indexarray.get(i).intValue())
return true;
}
return false;
}
// finds the indexes of zero points
public static List<Integer> findZeros(double[] points) {
int size = points.length;
ArrayList<Integer> zeros = new ArrayList<Integer>();
for (int i = 0; i < size; i++) {
if (points[i]==0){
int start = i;
int end = i;
for (int j=i+1;j<size;j++)
{
if (points[j]!=0){
end = j-1;
break;
}
}
int center = start+((end-start)/2);
zeros.add(center);
i = end;
}
}
return zeros;
}
public static double[] logSubdivision(double start,double end,int numberOfParts){
if (end<=start)
return null;
if (start == 0){
start = 0.01;
}
double logStart = Math.log(start);
double logEnd = Math.log(end);
double step =0 ;
if (numberOfParts >0){
double difference = logEnd-logStart;
step = (difference/(double)numberOfParts);
}
// double [] points = new double[numberOfParts+1];
double[] linearpoints = new double[numberOfParts+1];
for (int i=0;i<numberOfParts+1;i++){
// points[i] = logStart+(i*step);
linearpoints[i]= Math.exp(logStart+(i*step));
if (linearpoints[i]<0.011)
linearpoints[i] = 0;
}
return linearpoints;
}
}

View File

@ -10,6 +10,8 @@ import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.dataanalysis.ecoengine.utils.DatabaseFactory;
import org.hibernate.SessionFactory;
import com.rapidminer.RapidMiner;
public class AlgorithmConfiguration extends LexicalEngineConfiguration{
public static Properties getProperties(String absoluteFilePath) {
@ -37,6 +39,8 @@ public class AlgorithmConfiguration extends LexicalEngineConfiguration{
public static String modelsFile = "models.properties";
public static String modelersFile = "modelers.properties";
public static String evaluatorsFile = "evaluators.properties";
public static String RapidMinerOperatorsFile = "operators.xml";
public static int chunkSize = 100000;
public static int refreshResourcesTime = 10;
@ -138,10 +142,13 @@ public class AlgorithmConfiguration extends LexicalEngineConfiguration{
this.persistencePath = persistencePath;
}
//the agent is the processor running an algorithm, or a modeler or an evaluator of performances
//it has been distinguished from the Model variable in order to separate the meta-processor from the underlying processor
public String getAgent() {
return generator;
}
//the agent is the processor running an algorithm, or a modeler or an evaluator of performances
//it has been distinguished from the Model variable in order to separate the meta-processor from the underlying processor
public void setAgent(String generator) {
this.generator = generator;
}
@ -164,4 +171,11 @@ public class AlgorithmConfiguration extends LexicalEngineConfiguration{
return connection;
}
public void initRapidMiner(){
System.setProperty("rapidminer.init.operators", configPath+ AlgorithmConfiguration.RapidMinerOperatorsFile);
RapidMiner.init();
AnalysisLogger.getLogger().info("Rapid Miner initialized");
}
}

View File

@ -9,14 +9,25 @@ import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.dataanalysis.ecoengine.configuration.AlgorithmConfiguration;
import org.gcube.dataanalysis.ecoengine.interfaces.DataAnalysis;
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.VARTYPE;
import org.gcube.dataanalysis.ecoengine.utils.VarCouple;
import org.hibernate.SessionFactory;
public class DiscrepancyAnalysis extends DataAnalysis {
static String discrepancyQuery = "select distinct a.%1$s as csquareone,b.%2$s as csquaretwo,a.%3$s as firstprob,b.%4$s as secondprob from %5$s as a inner join %6$s as b on a.%1$s=b.%2$s and (a.%3$s<>b.%4$s)";
// static String discrepancyQuery = "select distinct a.%1$s as csquareone,b.%2$s as csquaretwo,a.%3$s as firstprob,b.%4$s as secondprob from %5$s as a inner join %6$s as b on a.%1$s=b.%2$s and (a.%3$s<>b.%4$s)";
// static String discrepancyQuery = "select distinct a.%1$s as csquareone,b.%2$s as csquaretwo,a.%3$s as firstprob,b.%4$s as secondprob from (select * from %5$s order by %1$s limit %7$s) as a inner join (select * from %6$s order by %2$s limit %7$s) as b on a.%1$s=b.%2$s and (a.%3$s<>b.%4$s)";
static String discrepancyQuery = "select distinct a.%1$s as csquareone,b.%2$s as csquaretwo,a.%3$s as firstprob,b.%4$s as secondprob from " +
"(select csquarecode,sum(%3$s) as %3$s from (select * from %5$s order by %1$s limit %7$s) as aa group by %1$s) as a " +
"inner join " +
"(select csquarecode,sum(%4$s) as %4$s from (select * from %6$s order by %2$s limit %7$s) as aa group by %2$s) as b " +
"on a.%1$s=b.%2$s and (a.%3$s<>b.%4$s)";
static String getNumberOfElementsQuery = "select count(*) from %1$s";
private static int minElements = 100;
private static int maxElements = 30000;
float threshold = 0.1f;
String configPath = "./cfg/";
@ -41,6 +52,7 @@ public class DiscrepancyAnalysis extends DataAnalysis {
parameters.put("FirstTableProbabilityColumn", new VarCouple(VARTYPE.STRING, "csquare"));
parameters.put("SecondTableProbabilityColumn", new VarCouple(VARTYPE.STRING, "csquarecode"));
parameters.put("ComparisonThreshold", new VarCouple(VARTYPE.STRING, "0.1"));
parameters.put("MaxSamples", new VarCouple(VARTYPE.STRING, "10000"));
parameters.put("DatabaseUserName", new VarCouple(VARTYPE.DATABASEUSERNAME, ""));
parameters.put("DatabasePassword", new VarCouple(VARTYPE.DATABASEPASSWORD, ""));
parameters.put("DatabaseURL", new VarCouple(VARTYPE.DATABASEURL, ""));
@ -82,9 +94,22 @@ public class DiscrepancyAnalysis extends DataAnalysis {
String SecondTableProbabilityColumn = config.getParam("SecondTableProbabilityColumn");
String FirstTable = config.getParam("FirstTable");
String SecondTable = config.getParam("SecondTable");
String query = String.format(discrepancyQuery, FirstTableCsquareColumn, SecondTableCsquareColumn, FirstTableProbabilityColumn, SecondTableProbabilityColumn, FirstTable, SecondTable);
String maxSamples = config.getParam("MaxSamples");
int maxCompElements = maxElements;
if (maxSamples!=null && maxSamples.length()>0){
int maxx = Integer.parseInt(maxSamples);
maxCompElements = maxx!=0?maxx:Integer.MAX_VALUE;
}
// String query = String.format(discrepancyQuery, FirstTableCsquareColumn, SecondTableCsquareColumn, FirstTableProbabilityColumn, SecondTableProbabilityColumn, FirstTable, SecondTable);
List<Object> nelementsQ = DatabaseFactory.executeSQLQuery(DatabaseUtils.countElementsStatement(FirstTable),connection);
int nelements = Integer.parseInt(""+nelementsQ.get(0));
nelements = Math.min(Operations.calcNumOfRepresentativeElements(nelements, minElements),maxCompElements);
AnalysisLogger.getLogger().trace("Number Of Elements to take: "+nelements);
String query = String.format(discrepancyQuery, FirstTableCsquareColumn, SecondTableCsquareColumn, FirstTableProbabilityColumn, SecondTableProbabilityColumn, FirstTable, SecondTable,""+nelements);
List<Object> takeNPoints = DatabaseFactory.executeSQLQuery(String.format(getNumberOfElementsQuery, FirstTable), connection);
List<Object> takeMPoints = DatabaseFactory.executeSQLQuery(String.format(getNumberOfElementsQuery, SecondTable), connection);
int nPoints = Integer.parseInt(""+takeNPoints.get(0));

View File

@ -0,0 +1,223 @@
package org.gcube.dataanalysis.ecoengine.evaluation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.dataanalysis.ecoengine.configuration.AlgorithmConfiguration;
import org.gcube.dataanalysis.ecoengine.interfaces.DataAnalysis;
import org.gcube.dataanalysis.ecoengine.models.cores.pca.PrincipalComponentAnalysis;
import org.gcube.dataanalysis.ecoengine.utils.DatabaseFactory;
import org.gcube.dataanalysis.ecoengine.utils.Operations;
import org.gcube.dataanalysis.ecoengine.utils.Transformations;
import org.gcube.dataanalysis.ecoengine.utils.VARTYPE;
import org.gcube.dataanalysis.ecoengine.utils.VarCouple;
import org.hibernate.SessionFactory;
public class HabitatRepresentativeness extends DataAnalysis {
static String getNumberOfElementsQuery = "select count(*) from %1$s %OPTIONAL%";
static String getRandomVectors = "select %1$s from %2$s %OPTIONAL% order by RANDOM() limit %3$s";
String configPath = "./cfg/";
private HashMap<String, String> output;
private static int minimumNumberToTake = 100;
public HashMap<String, VarCouple> getInputParameters() {
HashMap<String, VarCouple> parameters = new HashMap<String, VarCouple>();
parameters.put("ProjectingAreaTable", new VarCouple(VARTYPE.STRING, ""));
parameters.put("ProjectingAreaFeaturesOptionalCondition", new VarCouple(VARTYPE.STRING, "oceanarea>0"));
parameters.put("PositiveCasesTable", new VarCouple(VARTYPE.STRING, ""));
parameters.put("NegativeCasesTable", new VarCouple(VARTYPE.STRING, ""));
parameters.put("FeaturesColumns", new VarCouple(VARTYPE.STRING, ""));
parameters.put("PositiveFeaturesColumns", new VarCouple(VARTYPE.STRING, ""));
parameters.put("DatabaseUserName", new VarCouple(VARTYPE.DATABASEUSERNAME, ""));
parameters.put("DatabasePassword", new VarCouple(VARTYPE.DATABASEPASSWORD, ""));
parameters.put("DatabaseURL", new VarCouple(VARTYPE.DATABASEURL, ""));
parameters.put("DatabaseDriver", new VarCouple(VARTYPE.DATABASEDRIVER, ""));
return parameters;
}
public List<String> getOutputParameters() {
List<String> outputs = new ArrayList<String>();
outputs.add("HRS_VECTOR");
outputs.add("HRS");
return outputs;
}
private int calculateNumberOfPoints(String table, String option) {
String numberOfPositiveCasesQuery = String.format(getNumberOfElementsQuery, table);
numberOfPositiveCasesQuery = numberOfPositiveCasesQuery.replace("%OPTIONAL%", (option!=null)? option:"");
List<Object> totalPoints = DatabaseFactory.executeSQLQuery(numberOfPositiveCasesQuery, connection);
int points = Integer.parseInt("" + totalPoints.get(0));
return points;
}
private double[][] getPoints(String table, String option, String features, int numberOfElemsToTake) {
String query = String.format(getRandomVectors, features, table, "" + numberOfElemsToTake);
query = query.replace("%OPTIONAL%", (option!=null)? option:"");
AnalysisLogger.getLogger().trace("Compare - Query to perform for points:" + query);
List<Object> caughtpoints = DatabaseFactory.executeSQLQuery(query, connection);
int size = 0;
if (caughtpoints != null)
size = caughtpoints.size();
double[][] points = null;
if (size > 0) {
points = new double[size][((Object[]) caughtpoints.get(0)).length];
for (int i = 0; i < size; i++) {
if (caughtpoints.get(i) != null) {
Object[] arrayFeatures = (Object[]) caughtpoints.get(i);
for (int j = 0; j < arrayFeatures.length; j++) {
points[i][j] = Double.parseDouble("" + arrayFeatures[j]);
}
}
}
}
return points;
}
public HashMap<String, String> analyze(AlgorithmConfiguration config) throws Exception {
String projectingAreaTable = config.getParam("ProjectingAreaTable");
String projectingAreaFeaturesOptionalCondition = config.getParam("ProjectingAreaFeaturesOptionalCondition");
String FeaturesColumns = config.getParam("FeaturesColumns");
String positiveCasesTable = config.getParam("PositiveCasesTable");
String negativeCasesTable = config.getParam("NegativeCasesTable");
int numberOfElements = calculateNumberOfPoints(projectingAreaTable, projectingAreaFeaturesOptionalCondition);
int numberOfElementsToTake = Operations.calcNumOfRepresentativeElements(numberOfElements, minimumNumberToTake);
//1 - take the right number of points
double[][] areaPoints = getPoints(projectingAreaTable, projectingAreaFeaturesOptionalCondition, FeaturesColumns, numberOfElementsToTake);
Operations operations = new Operations();
//2 - standardize the matrix
areaPoints = operations.standardize(areaPoints);
//3 - calculate PCA
PrincipalComponentAnalysis pca = new PrincipalComponentAnalysis();
pca.calcPCA(areaPoints);
//4 - get the pca components for all the vector
double[][] pcaComponents = pca.getComponentsMatrix(areaPoints);
//5 - calculate the frequency distributions for all the pca: each row will be a frequency distribution for a pca component associated to uniform divisions of the range
calcFrequenciesDistributionsForComponents(pcaComponents);
//6 - take positive points and negative points - eventually merge them
double[][] positivePoints = getPoints(positiveCasesTable, "" , FeaturesColumns, numberOfElementsToTake);
double[][] negativePoints = getPoints(negativeCasesTable, "" , FeaturesColumns, numberOfElementsToTake);
double [][] habitatPoints = Transformations.mergeMatrixes(positivePoints, negativePoints);
//7 - Standardize the points respect to previous means and variances
habitatPoints = operations.standardize(habitatPoints, operations.means, operations.variances);
//8 - calculate the pca components for habitat
double[][] habitatPcaComponents = pca.getComponentsMatrix(habitatPoints);
//9 - calculate frequencies distributions for each component, respect to previous intervals
int components = habitatPcaComponents[0].length;
//10 - calculate absolute differences and sum -> obtain a hrs for each PCA component = for each feature
double [] hrs = new double[components];
for (int i=0;i<components;i++){
double [] habitatPcaPoints = Transformations.getColumn(i, habitatPcaComponents);
//calculate frequency distributions respect to previous intervals
double [] habitatPcafrequencies = Operations.calcFrequencies(intervals.get(i), habitatPcaPoints);
double [] absdifference = Operations.vectorialAbsoluteDifference(habitatPcafrequencies, frequencyDistrib.get(i));
hrs[i] = Operations.sumVector(absdifference);
}
//11 - obtain hrsScore by weighted sum of hrs respect to inverse eigenvalues
double hrsScore = Operations.scalarProduct(hrs, pca.getInverseEigenvalues());
output = new HashMap<String, String>();
output.put("HRS_VECTOR", "" + Transformations.vector2String(hrs));
output.put("HRS", "" + hrsScore);
return output;
}
List<double[]> frequencyDistrib;
List<double[]> intervals; //uniform subdivision of the numeric ranges
//calculate a frequency distribution for eache component
private void calcFrequenciesDistributionsForComponents(double[][] pcaComponents) {
frequencyDistrib = null;
if (pcaComponents.length > 0){
int sizeDistrib = pcaComponents[0].length;
frequencyDistrib = new ArrayList<double[]>();
intervals = new ArrayList<double[]>();
for (int i=0;i<sizeDistrib;i++){
double [] pcaPoints = Transformations.getColumn(i, pcaComponents);
double [] interval = Operations.uniformDivide(Operations.getMax(pcaPoints), Operations.getMax(pcaPoints), pcaPoints);
double[] frequencies = Operations.calcFrequencies(interval, pcaPoints);
intervals.add(interval);
frequencyDistrib.add(frequencies);
}
}
}
public static void visualizeResults(HashMap<String, String> results) {
for (String key : results.keySet()) {
System.out.println(key + ":" + results.get(key));
}
}
SessionFactory connection;
public void init(AlgorithmConfiguration config) throws Exception {
AnalysisLogger.setLogger(config.getConfigPath() + AlgorithmConfiguration.defaultLoggerFile);
// init db connection
connection = AlgorithmConfiguration.getConnectionFromConfig(config);
}
public void end() {
try {
connection.close();
} catch (Exception e) {
}
}
public static void main(String[] args) {
AnalysisLogger.setLogger("./cfg/" + AlgorithmConfiguration.defaultLoggerFile);
int n = 100;
double[] posRandom = new double[n];
double[] negRandom = new double[n];
for (int i = 0; i < n; i++) {
posRandom[i] = Math.random();
negRandom[i] = Math.random();
}
HabitatRepresentativeness quality = new HabitatRepresentativeness();
}
@Override
public String getContentType() {
return HashMap.class.getName();
}
@Override
public Object getContent() {
return output;
}
}

View File

@ -0,0 +1,334 @@
package org.gcube.dataanalysis.ecoengine.evaluation.bioclimate;
import java.awt.Image;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.gcube.contentmanagement.graphtools.utils.MathFunctions;
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
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.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.hibernate.SessionFactory;
import org.jfree.data.category.DefaultCategoryDataset;
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 meanVal = "select avg(%2$s) from %1$s where oceanarea>0";
// private static String meanVal = "select %2$s from %1$s where csquarecode = '1311:478:4'";
private String configPath;
private String temporaryDirectory;
protected SessionFactory referencedbConnection;
private String[] csquareTable;
private String[] finalDistributionTable;
//hspec
private int[] highProbabilityCells;
private double[] discrepancies;
//hcaf
private double[] avgIce;
private double[] avgSST;
private double[] avgSalinity;
private Evaluator eval;
private float status;
private boolean liveRender;
boolean doHcafAn;
boolean doHspecAn;
private LexicalEngineConfiguration config;
private static enum FIELD {
iceconann, sstanmean, salinitymean
};
public static void main(String[] args) throws Exception {
String configPath = "./cfg/";
String databaseUrl = "jdbc:postgresql://localhost/testdb";
String databaseUser = "gcube";
String databasePassword = "d4science2";
BioClimateAnalysis bioClimate = new BioClimateAnalysis(configPath, configPath, databaseUrl, databaseUser, databasePassword,true);
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" };
public void produceGraphs2D() throws Exception {
DefaultCategoryDataset testpoints = new DefaultCategoryDataset();
// double[] points = Operations.parabolicInterpolation(-200d, 200d, 100);
double[] points = Operations.parabolicInterpolation(0.1926, 0.1727, 20);
// double[] points = Operations.inverseParabolicInterpolation(-200d, 300d, 50);
// double[] points = Operations.inverseExponentialInterpolation(29d, 30d, 10);
for (int i = 0; i < points.length; i++) {
// System.out.println(points[i]);
testpoints.addValue(points[i], "Points", "" + i);
}
BioClimateGraph lineg9 = new BioClimateGraph("parabols", Operations.getMax(points), Operations.getMin(points));
lineg9.render(testpoints);
}
private List<Image> producedImages;
public List<Image> getProducedImages() {
return producedImages;
}
private void produceGraphs() throws Exception {
producedImages = new ArrayList<Image>();
int numberOfTrends = highProbabilityCells.length;
// create the datasets...
DefaultCategoryDataset probabilityTrend = new DefaultCategoryDataset();
DefaultCategoryDataset discrepanciesTrend = new DefaultCategoryDataset();
DefaultCategoryDataset avgIceD = new DefaultCategoryDataset();
DefaultCategoryDataset avgSSTD = new DefaultCategoryDataset();
DefaultCategoryDataset avgSalinityD = new DefaultCategoryDataset();
for (int i = 0; i < numberOfTrends; i++) {
if (doHcafAn){
avgIceD.addValue(avgIce[i], "Ice Conc.", csquareTable[i]);
avgSSTD.addValue(avgSST[i], "SST", csquareTable[i]);
avgSalinityD.addValue(avgSalinity[i], "Salinity", csquareTable[i]);
}
if (doHspecAn){
probabilityTrend.addValue(highProbabilityCells[i], "Number Of Cells", finalDistributionTable[i]);
if (i > 0) {
discrepanciesTrend.addValue(discrepancies[i], "Mean Discrepancy Respect to Prev. Distrib.", finalDistributionTable[i]);
}
}
}
int width = 680;
int height = 420;
if (doHspecAn){
BioClimateGraph lineg1 = new BioClimateGraph(SERIES[0], Operations.getMax(highProbabilityCells), Operations.getMin(highProbabilityCells));
double min = Operations.getMin(discrepancies);
discrepancies[0] = min;
BioClimateGraph lineg4 = new BioClimateGraph(SERIES[3], Operations.getMax(discrepancies), min);
if (liveRender){
lineg4.render(discrepanciesTrend);
lineg1.render(probabilityTrend);
}
producedImages.add(lineg1.renderImgObject(width, height, probabilityTrend));
producedImages.add(lineg4.renderImgObject(width, height, discrepanciesTrend));
}
if (doHcafAn){
BioClimateGraph lineg6 = new BioClimateGraph(SERIES[5], Operations.getMax(avgIce), Operations.getMin(avgIce));
BioClimateGraph lineg7 = new BioClimateGraph(SERIES[6], Operations.getMax(avgSST), Operations.getMin(avgSST));
BioClimateGraph lineg8 = new BioClimateGraph(SERIES[7], Operations.getMax(avgSalinity), Operations.getMin(avgSalinity));
if (liveRender){
lineg6.render(avgIceD);
lineg7.render(avgSSTD);
lineg8.render(avgSalinityD);
}
producedImages.add(lineg6.renderImgObject(width, height, avgIceD));
producedImages.add(lineg7.renderImgObject(width, height, avgSSTD));
producedImages.add(lineg8.renderImgObject(width, height, avgSalinityD));
}
AnalysisLogger.getLogger().trace("Produced All Images");
}
public void hcafEvolutionAnalysis(String[] hcafTable) throws Exception{
evolutionAnalysis(hcafTable, null, null, null);
}
public void hspecEvolutionAnalysis(String[] hspecTables, String probabilityColumn, String csquareColumn) throws Exception{
evolutionAnalysis(null, hspecTables, probabilityColumn, csquareColumn);
}
public void evolutionAnalysis(String[] hcafTable, String[] hspecTables, String probabilityColumn, String csquareColumn) throws Exception {
try {
referencedbConnection = DatabaseFactory.initDBConnection(configPath + AlgorithmConfiguration.defaultConnectionFile, config);
AnalysisLogger.getLogger().debug("ReferenceDB initialized");
doHcafAn = true;
doHspecAn = true;
if (hcafTable == null)
doHcafAn = false;
if (hspecTables == null)
doHspecAn = false;
status = 0f;
this.csquareTable = hcafTable;
this.finalDistributionTable = hspecTables;
int numbOfPoints = (hcafTable!=null)? hcafTable.length:hspecTables.length;
highProbabilityCells = new int[numbOfPoints];
discrepancies = new double[numbOfPoints];
avgIce = new double[numbOfPoints];
avgSST = new double[numbOfPoints];
avgSalinity = new double[numbOfPoints];
float statusstep = 80f / (float) numbOfPoints;
for (int i = 0; i < numbOfPoints; i++) {
if (doHspecAn)
highProbabilityCells[i] = calcHighProbabilityCells(hspecTables[i], 0.8);
if (doHcafAn){
avgIce[i] = avgValue(hcafTable[i], FIELD.iceconann.name());
avgSST[i] = avgValue(hcafTable[i], FIELD.sstanmean.name());
avgSalinity[i] = avgValue(hcafTable[i], FIELD.salinitymean.name());
AnalysisLogger.getLogger().trace("(" + hcafTable[i] +"): " + " ICE " + avgIce[i] + " SST " + avgSST[i] + " SAL " + avgSalinity[i]);
}
if (doHspecAn) {
if (i == 0) {
discrepancies[i] = 1.0;
} else {
// OLD CALCULATION discrepancies[i] = MathFunctions.roundDecimal(calcDiscrepancy(configPath, temporaryDirectory, hspecTables[i], hspecTables[i - 1], probabilityColumn, csquareColumn, 0.1f), 5);
discrepancies[i] = MathFunctions.roundDecimal(calcOverDiscrepancy(configPath, temporaryDirectory, hspecTables[i], hspecTables[i - 1], probabilityColumn, csquareColumn, 0.1f), 5);
}
AnalysisLogger.getLogger().trace("(" + hspecTables[i] +"): DISCREPANCY " + discrepancies[i] +" HIGH PROB CELLS " + highProbabilityCells[i]);
}
// AnalysisLogger.getLogger().trace("(" + hcafTable[i] + "," + hspecTables[i] + "): HIGH PROB CELLS " + highProbabilityCells[i] + " DISCREPANCY " + discrepancies[i] + " ICE " + avgIce[i] + " SST " + avgSST[i] + " SAL " + avgSalinity[i]);
status = status + statusstep;
}
status = 80f;
produceGraphs();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
status = 100f;
referencedbConnection.close();
}
}
// init connections
public BioClimateAnalysis(String configPath, String persistencePath, String databaseURL, String databaseUserName, String databasePassword,boolean liveRender) throws Exception {
this.configPath = configPath;
this.temporaryDirectory = persistencePath;
if (!configPath.endsWith("/"))
configPath += "/";
if (!persistencePath.endsWith("/"))
this.temporaryDirectory += "/";
AnalysisLogger.setLogger(configPath + AlgorithmConfiguration.defaultLoggerFile);
config = new LexicalEngineConfiguration();
config.setDatabaseURL(databaseURL);
config.setDatabaseUserName(databaseUserName);
config.setDatabasePassword(databasePassword);
this.liveRender = liveRender;
}
public int calcHighProbabilityCells(String hspec, double probabilty) throws Exception {
AnalysisLogger.getLogger().trace("Calculating High Prob Cells");
List<Object> countage = DatabaseFactory.executeSQLQuery(String.format(countHighProbabilityCells, hspec, probabilty), referencedbConnection);
int count = Integer.parseInt("" + countage.get(0));
AnalysisLogger.getLogger().trace("Calc High Prob Cells: "+count);
return count;
}
public double avgValue(String hcaf1, String field) throws Exception {
List<Object> countage = DatabaseFactory.executeSQLQuery(String.format(meanVal, hcaf1, field), referencedbConnection);
double count = 0;
if (countage != null && countage.size() > 0)
count = Double.parseDouble("" + countage.get(0));
return count;
}
public int countIceLeakageCells(String hcaf1, String hcaf2) throws Exception {
List<Object> countage = DatabaseFactory.executeSQLQuery(String.format(iceLeakage, hcaf1, hcaf2), referencedbConnection);
int count = Integer.parseInt("" + countage.get(0));
return count;
}
public int countSeaCells(String hcaf1, String hcaf2, double threshold) throws Exception {
// System.out.println(String.format(countSeaCells, hcaf1, hcaf2,threshold));
List<Object> countage = DatabaseFactory.executeSQLQuery(String.format(countSeaCells, hcaf1, hcaf2, threshold), referencedbConnection);
int count = Integer.parseInt("" + countage.get(0));
return count;
}
public float getStatus() {
return status;
}
public double calcOverDiscrepancy(String configPath, String persistencePath, String firstTable, String secondTable, String probabilityColumnName, String csquareColumn, float comparisonThreshold) throws Exception {
List<Object> nelementsQ1 = DatabaseFactory.executeSQLQuery(DatabaseUtils.countElementsStatement(firstTable),referencedbConnection);
int nelements = Integer.parseInt(""+nelementsQ1.get(0));
AnalysisLogger.getLogger().trace("Number Of elements1: "+nelementsQ1);
List<Object> nelementsQ2 = DatabaseFactory.executeSQLQuery(DatabaseUtils.countElementsStatement(secondTable),referencedbConnection);
int nelements2 = Integer.parseInt(""+nelementsQ2.get(0));
AnalysisLogger.getLogger().trace("Number Of elements2: "+nelementsQ1);
List<Object> sumFirst = DatabaseFactory.executeSQLQuery(DatabaseUtils.sumElementsStatement(firstTable,probabilityColumnName),referencedbConnection);
double sum1 = Double.parseDouble(""+sumFirst.get(0));
AnalysisLogger.getLogger().trace("Sum1: "+sum1);
List<Object> sumSecond = DatabaseFactory.executeSQLQuery(DatabaseUtils.sumElementsStatement(secondTable,probabilityColumnName),referencedbConnection);
double sum2 = Double.parseDouble(""+sumSecond.get(0));
AnalysisLogger.getLogger().trace("Sum2: "+sum1);
double d = (double)(sum2-sum1)/(double)(nelements+nelements2);
return d;
}
public double calcDiscrepancy(String configPath, String persistencePath, String firstTable, String secondTable, String probabilityColumnName, String csquareColumn, float comparisonThreshold) throws Exception {
AlgorithmConfiguration config = new AlgorithmConfiguration();
config.setConfigPath(configPath);
config.setPersistencePath(persistencePath);
config.setNumberOfResources(1);
config.setAgent("DISCREPANCY_ANALYSIS");
config.setParam("FirstTable", firstTable);
config.setParam("SecondTable", secondTable);
config.setParam("FirstTableCsquareColumn", csquareColumn);
config.setParam("SecondTableCsquareColumn", csquareColumn);
config.setParam("FirstTableProbabilityColumn", probabilityColumnName);
config.setParam("SecondTableProbabilityColumn", probabilityColumnName);
config.setParam("ComparisonThreshold", "" + comparisonThreshold);
config.setParam("MaxSamples", ""+30000);
eval = EvaluatorsFactory.getEvaluators(config).get(0);
HashMap<String, String> out = eval.process(config);
Double d = Double.parseDouble(out.get("MEAN"));
return d;
}
}

View File

@ -0,0 +1,136 @@
package org.gcube.dataanalysis.ecoengine.evaluation.bioclimate;
import java.awt.Color;
import org.gcube.contentmanagement.graphtools.abstracts.GenericStandaloneGraph;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.PlotRenderingInfo;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.Dataset;
import org.jfree.ui.RectangleInsets;
public class BioClimateGraph extends GenericStandaloneGraph {
private static final long serialVersionUID = 1L;
double max;
double min;
public BioClimateGraph(String title,double max,double min) {
super(title);
this.max= max;
this.min = min;
}
protected Dataset generateDataset() {
// row keys...
String series1 = "First";
String series2 = "Second";
String series3 = "Third";
// column keys...
String type1 = "Type 1";
String type2 = "Type 2";
String type3 = "Type 3";
String type4 = "Type 4";
String type5 = "Type 5";
String type6 = "Type 6";
String type7 = "Type 7";
String type8 = "Type 8";
// create the dataset...
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(1.0, series1, type1);
dataset.addValue(4.0, series1, type2);
dataset.addValue(3.0, series1, type3);
dataset.addValue(5.0, series1, type4);
dataset.addValue(5.0, series1, type5);
dataset.addValue(7.0, series1, type6);
dataset.addValue(7.0, series1, type7);
dataset.addValue(8.0, series1, type8);
dataset.addValue(5.0, series2, type1);
dataset.addValue(7.0, series2, type2);
dataset.addValue(6.0, series2, type3);
dataset.addValue(8.0, series2, type4);
dataset.addValue(4.0, series2, type5);
dataset.addValue(4.0, series2, type6);
dataset.addValue(2.0, series2, type7);
dataset.addValue(1.0, series2, type8);
dataset.addValue(4.0, series3, type1);
dataset.addValue(3.0, series3, type2);
dataset.addValue(2.0, series3, type3);
dataset.addValue(3.0, series3, type4);
dataset.addValue(6.0, series3, type5);
dataset.addValue(3.0, series3, type6);
dataset.addValue(4.0, series3, type7);
dataset.addValue(3.0, series3, type8);
return dataset;
}
protected JFreeChart createChart(Dataset dataset) {
// create the chart...
JFreeChart chart = ChartFactory.createLineChart(
this.getTitle(), // chart title
"", // domain axis label
"", // range axis label
(DefaultCategoryDataset)dataset, // data
PlotOrientation.VERTICAL, // orientation
true, // include legend
true, // tooltips
false // urls
);
chart.setBackgroundPaint(Color.white);
CategoryPlot plot = chart.getCategoryPlot();
// plot.setBackgroundPaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setDomainCrosshairVisible(true);
plot.setDomainGridlinesVisible(true);
plot.setRangeCrosshairVisible(true);
plot.setRenderer(new LineAndShapeRenderer(true,true));
CategoryAxis categoryaxis1 = plot.getDomainAxis(0);
categoryaxis1.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
plot.mapDatasetToDomainAxis(0, 0);
// plot.zoomRangeAxes(0.1,10d,null,null);
//
if (max!=min){
plot.getRangeAxis().setAutoRange(false);
plot.getRangeAxis().setUpperBound(max);
plot.getRangeAxis().setLowerBound(min);
double avg = min+((max-min)/2d);
plot.getRangeAxis().centerRange(avg);
}
//deprecated
/*
LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
renderer.setShapesVisible(true);
renderer.setDrawOutlines(true);
renderer.setUseFillPaint(true);
renderer.setFillPaint(Color.white);
*/
return chart;
}
@Override
protected GenericStandaloneGraph getInstance(String title) {
return new BioClimateGraph(title,max,min);
}
}

View File

@ -0,0 +1,255 @@
package org.gcube.dataanalysis.ecoengine.evaluation.bioclimate;
import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.gcube.contentmanagement.graphtools.utils.MathFunctions;
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.DataTypeRecognizer;
import org.gcube.contentmanagement.lexicalmatcher.analysis.core.LexicalEngineConfiguration;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.contentmanagement.lexicalmatcher.utils.FileTools;
import org.gcube.dataanalysis.ecoengine.configuration.AlgorithmConfiguration;
import org.gcube.dataanalysis.ecoengine.utils.DatabaseFactory;
import org.gcube.dataanalysis.ecoengine.utils.DatabaseUtils;
import org.gcube.dataanalysis.ecoengine.utils.Operations;
import org.hibernate.SessionFactory;
/**
* checks if two tables are equal checks numbers at the second decimal position
*/
public class InterpolateTables {
// connection setup
protected String temporaryDirectory;
// selection query
public static String selectElementsQuery = "select %1$s from %2$s order by %3$s";
public static String selectDestElementsQuery = "select %1$s from %2$s where %3$s";
public float status;
private String[] interpolatedTables;
private LexicalEngineConfiguration config;
// database connections
protected SessionFactory referencedbConnection;
private String configPath;
private File[] producedfiles;
public static enum INTERPOLATIONFUNCTIONS {
LINEAR, PARABOLIC
};
public String[] getInterpolatedTables(){
return interpolatedTables;
}
// init connections
public InterpolateTables(String configPath, String persistencePath, String databaseURL, String databaseUserName, String databasePassword) throws Exception {
this.configPath = configPath;
this.temporaryDirectory = persistencePath;
if (!configPath.endsWith("/"))
configPath += "/";
if (!persistencePath.endsWith("/"))
this.temporaryDirectory += "/";
AnalysisLogger.setLogger(configPath + AlgorithmConfiguration.defaultLoggerFile);
config = new LexicalEngineConfiguration();
config.setDatabaseURL(databaseURL);
config.setDatabaseUserName(databaseUserName);
config.setDatabasePassword(databasePassword);
}
// tables have to present the same structure
public void interpolate(String table1, String table2, int intervals, INTERPOLATIONFUNCTIONS function,int startYear,int endYear) throws Exception {
try {
referencedbConnection = DatabaseFactory.initDBConnection(configPath + AlgorithmConfiguration.defaultConnectionFile, config);
AnalysisLogger.getLogger().debug("ReferenceDB initialized");
status = 0f;
DatabaseUtils utils = new DatabaseUtils(referencedbConnection);
// analyze table and take information about it
String createTableStatement = utils.buildCreateStatement(table1, "%1$s");
int numberOfColumns = utils.getColumnDecriptions().size();
// initialize the map of columns to write
List<List<StringBuffer>> outputFiles = new ArrayList<List<StringBuffer>>();
for (int g = 0; g < intervals - 2; g++) {
outputFiles.add(new ArrayList<StringBuffer>());
}
float statusstep = 60f/(float)numberOfColumns;
// DatabaseFactory.executeSQLUpdate(creationStatement, referencedbConnection);
// take the columns
for (int j = 0; j < numberOfColumns; j++) {
// take column name
String gotColumn = utils.getColumnName(j);
String gotColumnType = utils.getColumnType(j);
String javatype = DataTypeRecognizer.transformTypeFromDB(gotColumnType);
List<Object> takeFirstColumn = DatabaseFactory.executeSQLQuery(DatabaseUtils.getOrderedElements(table1, utils.getPrimaryKey(), gotColumn), referencedbConnection);
List<Object> takeSecondColumn = DatabaseFactory.executeSQLQuery(DatabaseUtils.getOrderedElements(table2, utils.getPrimaryKey(), gotColumn), referencedbConnection);
// only if data are of numeric type, perform calculation
if (javatype.equals(BigDecimal.class.getName())) {
System.out.println("interpolating -> " + gotColumn);
List<List<Object>> interpolations = interpolateColumns(takeFirstColumn, takeSecondColumn, intervals, gotColumnType, function);
for (int i = 1; i < intervals - 1; i++) {
// create the interpolation table
String tableInterp = table1 + "_" + (i);
// for each column to substitute
List<Object> columnToSub = interpolations.get(i);
if (columnToSub.size() > 0) {
System.out.println("UPDATE TABLE " + tableInterp + " ON COLUMN " + gotColumn);
addColumnToTable(outputFiles.get(i - 1), columnToSub);
} else {
System.out.println("DOESN'T CHANGE TABLE " + tableInterp + " COLUMN " + gotColumn);
addColumnToTable(outputFiles.get(i - 1), takeFirstColumn);
}
}
}
// else update all the tables
else {
for (int i = 0; i < intervals - 2; i++) {
addColumnToTable(outputFiles.get(i), takeFirstColumn);
}
}
status = status +statusstep;
}
status = 60f;
System.out.println("WRITING ALL THE BUFFERS");
writeAllStringBuffersToFiles(table1, outputFiles, function,startYear,endYear);
statusstep = 40f/(float)producedfiles.length;
interpolatedTables = new String[producedfiles.length+1];
interpolatedTables[0] = table1;
for (int i = 0; i < producedfiles.length; i++) {
String filename = producedfiles[i].getName();
filename = filename.substring(0, filename.lastIndexOf("."));
interpolatedTables[i+1] = filename;
String copyFileQuery = DatabaseUtils.copyFileToTableStatement(temporaryDirectory + producedfiles[i].getName(), filename);
// create Table
System.out.println("CREATING TABLE->" + filename);
DatabaseFactory.executeSQLUpdate(String.format(createTableStatement, filename), referencedbConnection);
System.out.println("FULFILLING TABLE->" + filename + ": " + copyFileQuery);
DatabaseFactory.executeSQLUpdate(copyFileQuery, referencedbConnection);
status = Math.min(status+statusstep,99);
}
interpolatedTables[producedfiles.length] = table2;
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
// close connections
referencedbConnection.close();
status = 100f;
}
}
public float getStatus(){
return status;
}
private void addColumnToTable(List<StringBuffer> rows, List<Object> elements) {
int size = elements.size();
for (int i = 0; i < size; i++) {
Object[] couple = (Object[]) elements.get(i);
String value = "" + couple[1];
StringBuffer buffer = null;
if (i >= rows.size()) {
buffer = new StringBuffer();
buffer.append(value);
rows.add(buffer);
} else {
buffer = rows.get(i);
buffer.append(";" + value);
}
}
}
private void writeAllStringBuffersToFiles(String initialFile, List<List<StringBuffer>> outputFiles, INTERPOLATIONFUNCTIONS function,int startYear,int endYear) throws Exception {
int numOfFiles = outputFiles.size();
int yearStep = (int)((float)(endYear-startYear)/(float)numOfFiles);
producedfiles = new File[numOfFiles];
for (int i = 0; i < numOfFiles; i++) {
List<StringBuffer> rows = outputFiles.get(i);
StringBuffer completeFile = new StringBuffer();
int nrows = rows.size();
for (int k = 0; k < nrows; k++) {
completeFile.append(rows.get(k) + "\n");
}
String filename = temporaryDirectory + initialFile + "_" + (startYear+(i+1)*yearStep) + "_" + function.name() + ".csv";
FileTools.saveString(filename, completeFile.toString(), true, "UTF-8");
producedfiles[i] = new File(filename);
}
}
// interpolates parallel columns
private List<List<Object>> interpolateColumns(List<Object> col1, List<Object> col2, int intervals, String type, INTERPOLATIONFUNCTIONS function) {
int elements = col1.size();
ArrayList<List<Object>> columns = new ArrayList<List<Object>>();
for (int i = 0; i < intervals; i++) {
columns.add(new ArrayList<Object>());
}
// produce a column couple for each interval
boolean interping=true;
for (int i = 0; i < elements; i++) {
Object[] row1 = (Object[]) col1.get(i);
Object[] row2 = (Object[]) col2.get(i);
double firstNum = row1[1] != null ? Double.parseDouble("" + row1[1]) : 0d;
double secondNum = row2[1] != null ? Double.parseDouble("" + row2[1]) : 0d;
Object key = row1[0];
double[] interpolation = null;
if (firstNum != secondNum) {
if (interping)
{ System.out.println("Interpolating ... "); interping = false;}
if (function == INTERPOLATIONFUNCTIONS.LINEAR)
interpolation = Operations.linearInterpolation(firstNum, secondNum, intervals);
else if (function == INTERPOLATIONFUNCTIONS.PARABOLIC)
interpolation = Operations.parabolicInterpolation(firstNum, secondNum, intervals);
}
for (int j = 0; j < intervals; j++) {
Object[] couple = new Object[2];
couple[0] = key;
double interp = firstNum;
if (interpolation!=null)
interp = interpolation[j];
if (type.equals("integer"))
couple[1] = Math.round(interp);
else {
interp = MathFunctions.roundDecimal(interp, 2);
couple[1] = interp;
}
columns.get(j).add(couple);
}
}
return columns;
}
public static void main(String[] args) throws Exception {
String configPath = "./cfg/";
String persistencePath = "c:/tmp/";
String databaseUrl = "jdbc:postgresql://localhost/testdb";
String databaseUser = "gcube";
String databasePassword = "d4science2";
InterpolateTables interp = new InterpolateTables(configPath, persistencePath, databaseUrl, databaseUser, databasePassword);
interp.interpolate("hcaf_d", "hcaf_d_2050", 10, INTERPOLATIONFUNCTIONS.PARABOLIC,2012,2050);
}
}

View File

@ -0,0 +1,46 @@
package org.gcube.dataanalysis.ecoengine.evaluation.bioclimate;
import org.gcube.dataanalysis.ecoengine.user.GeneratorT;
import org.gcube.dataanalysis.ecoengine.user.ModelerT;
public class ProduceTestMap {
public static void main(String[] args) throws Exception {
String configPath = "./cfg/";
final String[] csquareTables = { "hcaf_d_2016_PARABOLIC", "hcaf_d_2020_PARABOLIC", "hcaf_d_2024_PARABOLIC", "hcaf_d_2028_PARABOLIC", "hcaf_d_2032_PARABOLIC", "hcaf_d_2036_PARABOLIC", "hcaf_d_2040_PARABOLIC", "hcaf_d_2044_PARABOLIC"};
final String [] envelopeOutputTable = {"hspen_validation_1","hspen_validation_2","hspen_validation_3","hspen_validation_4","hspen_validation_5","hspen_validation_6","hspen_validation_7","hspen_validation_8"};
final String [] finalDistributionTable = { "hspec_v_0", "hspec_v_1", "hspec_v_2", "hspec_v_3", "hspec_v_4", "hspec_v_5", "hspec_v_6", "hspec_v_7", "hspec_v_8"};
/*
String [] csquareTable = {"hcaf_d","hcaf_d_2050"};
String [] envelopeOutputTable = {"hspen_micro_0","hspen_micro_2050"};
String [] finalDistributionTable = {"hspec_0","hspec_2050"};
*/
String preprocessedTable = "maxminlat_hspen";
String envelopeTable = "hspen_validation";
String occurrenceCells = "occurrenceCells";
String commonkeycolumn = "csquarecode";
String probabilitycolumn = "probability";
int numberOfResources = 4;
String speciesCode = "Fis-10199";
String userName = "gianpaolo.coro";
String modelName = "HSPEN";
String generatorName = "AQUAMAPS_SUITABLE";
String qualityOperationName = "QUALITY_ANALYSIS";
String discrepancyOperationName = "DISCREPANCY_ANALYSIS";
String finalDistributionKeyColumn = "csquarecode";
for (int i =0;i<csquareTables.length;i++){
// Train
ModelerT.train(ModelerT.getTrainingConfigHSPEN(modelName, envelopeOutputTable[i], occurrenceCells,envelopeTable,csquareTables[i],configPath));
// Generate
GeneratorT.generate(GeneratorT.getGenerationConfig(numberOfResources, generatorName, envelopeOutputTable[i], preprocessedTable, speciesCode, userName, csquareTables[i], finalDistributionTable[i], configPath));
}
}
}

View File

@ -0,0 +1,179 @@
package org.gcube.dataanalysis.ecoengine.models.cores.pca;
import org.gcube.contentmanagement.graphtools.data.BigSamplesTable;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.dataanalysis.ecoengine.configuration.AlgorithmConfiguration;
import org.gcube.dataanalysis.ecoengine.utils.Operations;
import org.gcube.dataanalysis.ecoengine.utils.Transformations;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.operator.IOContainer;
import com.rapidminer.operator.IOObject;
import com.rapidminer.operator.ModelApplier;
import com.rapidminer.operator.features.transformation.PCA;
import com.rapidminer.operator.features.transformation.PCAModel;
import com.rapidminer.tools.OperatorService;
public class PrincipalComponentAnalysis {
public static void main(String[] args) throws Exception{
AlgorithmConfiguration config = new AlgorithmConfiguration();
config.setConfigPath("./cfg/");
config.setPersistencePath("./cfg/");
config.setNumberOfResources(1);
config.setAgent("QUALITY_ANALYSIS");
AnalysisLogger.setLogger(config.getConfigPath()+AlgorithmConfiguration.defaultLoggerFile);
config.initRapidMiner();
PrincipalComponentAnalysis pca = new PrincipalComponentAnalysis();
/*random example
int m= 20;
int n = 10;
double values[][] = new double[m][n];
for (int i=0;i<m;i++){
for (int j=0;j<n;j++)
if ((i==0)||(i==1))
values[i][j]=1d;
else
values[i][j]=Math.random();
}
*/
int m= 5;
int n = 5;
double values[][] = new double[m][n];
double val1[] = {1.000d,0.451d,0.511d,0.197d,0.162d};
double val2[] = {0.451d,1.000d,0.445d,0.252d,0.238d};
double val3[] = {0.511d,0.445d,1.000d,0.301d,0.227d};
double val4[] = {0.197d,0.252d,0.301d,1.000d,0.620d};
double val5[] = {0.162d,0.238d,0.227d,0.620d,1.000d};
values[0] = val1;
values[1] = val2;
values[2] = val3;
values[3] = val4;
values[4] = val5;
//calculates the PCA
pca.calcPCA(values);
PCAModel model = pca.getModel();
int components = model.getMaximumNumberOfComponents();
for (int i=0;i<components;i++){
AnalysisLogger.getLogger().debug((i+1)+"->"+model.getEigenvalue(i));
double[] eigen = model.getEigenvector(i);
for (int j=0;j<eigen.length;j++)
System.out.print(eigen[j]+" ");
System.out.println();
}
double [][] componentsMatrix = pca.getComponentsMatrix(values);
System.exit(0);
}
public void init(AlgorithmConfiguration config){
config.initRapidMiner();
}
PCAModel innermodel;
int numberOfComponents;
public PCAModel getModel(){
return innermodel;
}
public double[] getEigenvector (int index){
return innermodel.getEigenvector(index);
}
public double getEigenvalue (int index){
return innermodel.getEigenvalue(index);
}
public double [] getEigenvalues (){
double [] values = new double[numberOfComponents];
for (int i=0;i<numberOfComponents;i++){
values[i] = getEigenvalue(i);
}
return values;
}
public double [] getInverseEigenvalues (){
double [] values = new double[numberOfComponents];
for (int i=0;i<numberOfComponents;i++){
values[i] = 1d/getEigenvalue(i);
}
return values;
}
public double[][] getComponentsMatrix(double[][] vectors) throws Exception{
int nsamples=vectors.length;
double[][] components = new double[nsamples][];
for (int i=0;i<nsamples;i++){
components[i] = getComponents(vectors[i]);
}
return components;
}
public double[] getComponents(double[] vector) throws Exception{
double [] components = new double[numberOfComponents];
for (int i=0;i<numberOfComponents;i++){
components[i] = Operations.scalarProduct(vector, getEigenvector(i));
}
return components;
}
protected double[][] getPCA(double[][] sampleVectors) throws Exception{
ExampleSet set = Transformations.matrix2ExampleSet(sampleVectors);
ExampleSet outset = innermodel.apply(set);
return Transformations.exampleSet2Matrix(outset);
}
public void calcPCA(double [][] sampleVectors) throws Exception{
AnalysisLogger.getLogger().debug("STARTING PCA COMPUTATION");
PCA pca = (PCA) OperatorService.createOperator("PCA");
pca.setParameter("variance_threshold", "0.95");
pca.setParameter("dimensionality_reduction", "keep variance");
pca.setParameter("number_of_components", "-1");
ExampleSet set = Transformations.matrix2ExampleSet(sampleVectors);
IOContainer innerInput = new IOContainer(set);
IOContainer output = pca.apply(innerInput);
IOObject[] outputvector = output.getIOObjects();
// ExampleSet setOut = (ExampleSet) outputvector[0];
innermodel = (PCAModel) outputvector[1];
numberOfComponents = innermodel.getMaximumNumberOfComponents();
AnalysisLogger.getLogger().debug("MODEL APPLIED");
}
}

View File

@ -93,4 +93,14 @@ public class ProcessorsFactory {
}
}
public static HashMap<String,List<String>> getAllFeatures(String configPath) throws Exception{
HashMap<String,List<String>> map = new HashMap<String, List<String>>();
map.put("DISTRIBUTIONS", GeneratorsFactory.getProbabilityDistributionAlgorithms(configPath));
map.put("MODELS", ModelersFactory.getModels(configPath));
map.put("EVALUATORS", EvaluatorsFactory.getAllEvaluators(configPath));
return map;
}
}

View File

@ -0,0 +1,54 @@
package org.gcube.dataanalysis.ecoengine.test;
import org.gcube.dataanalysis.ecoengine.user.GeneratorT;
import org.gcube.dataanalysis.ecoengine.user.ModelerT;
public class ProduceTestMap {
public static void main(String[] args) throws Exception {
String configPath = "./cfg/";
String presenceTable = "presence_data_baskingshark";
String absenceTable = "absence_data_baskingshark_random";
String csquareTable = "hcaf_d";
String preprocessedTable = "maxminlat_hspen";
String envelopeTable = "hspen_baskingshark";
String commonkeycolumn = "csquarecode";
String probabilitycolumn = "probability";
String positiveClassificationThreshold = "0.8";
String negativeClassificationThreshold = "0.3";
String comparisonThreshold = "0.1";
int numberOfResources = 4;
String speciesCode = "Fis-22747";
String userName = "gianpaolo.coro";
String modelName = "AQUAMAPSNN";
String generatorName = "AQUAMAPS_SUITABLE_NEURALNETWORK";
String qualityOperationName = "QUALITY_ANALYSIS";
String discrepancyOperationName = "DISCREPANCY_ANALYSIS";
String manualTableOfComparison = "hspec_native_baskingshark";
String manualTableCsquareColumn = "csquare";
String secondTableOfComparison = "hspec_native_baskingshark_aquamaps";
String neuralNetworkLayers = "300";
// String finalDistributionTable = "hspec_nn_baskingshark_close_best";
// String finalDistributionKeyColumn= "csquarecode";
// String finalDistributionTable = "hspec_nn_baskingshark_random_best2";
// String finalDistributionKeyColumn= "csquarecode";
// String finalDistributionTable = "hspec_native_baskingshark";
// String finalDistributionKeyColumn= "csquare";
String finalDistributionTable = "hspec_native_baskingshark_aquamaps";
String finalDistributionKeyColumn = "csquarecode";
// Train
// ModelerT.train(ModelerT.getTrainingConfig(modelName, absenceTable, presenceTable, speciesCode, userName, neuralNetworkLayers, configPath));
// Generate
GeneratorT.generate(GeneratorT.getGenerationConfig(numberOfResources, generatorName, envelopeTable, preprocessedTable, speciesCode, userName, csquareTable, finalDistributionTable, configPath));
}
}

View File

@ -67,6 +67,10 @@ public static void main(String[] args) throws Exception {
List<Evaluator> eval = EvaluatorsFactory.getEvaluators(testConfigEvaluator());
System.out.println("Database Default Values: "+eval);
System.out.println("\n***TEST 12- Get Evaluator Parameters ***");
HashMap<String,List<String>> features = ProcessorsFactory.getAllFeatures("./cfg/");
System.out.println("Database Default Values: "+features);
int cores = Runtime.getRuntime().availableProcessors();
System.out.println("Number of cores: "+cores);
}

View File

@ -110,17 +110,17 @@ public static void main(String[] args) throws Exception {
config.setNumberOfResources(5);
config.setModel("AQUAMAPS_SUITABLE");
config.setParam("DistributionTable","hspec_suitable_test");
config.setParam("DistributionTable","hspec_suitable_test_gp");
config.setParam("CsquarecodesTable","hcaf_d");
config.setParam("EnvelopeTable","hspen_micro");
config.setParam("EnvelopeTable","hspen");
config.setParam("PreprocessedTable", "maxminlat_hspen");
config.setParam("CreateTable","true");
/*
config.setParam("DatabaseUserName","gcube");
config.setParam("DatabasePassword","d4science2");
config.setParam("DatabaseURL","jdbc:postgresql://localhost/testdb");
config.setParam("DatabaseURL","jdbc:postgresql://dbtest.next.research-infrastructures.eu/testdb");
config.setParam("DatabaseDriver","org.postgresql.Driver");
*/
return config;
}

View File

@ -69,5 +69,6 @@ public class GeneratorT implements Runnable{
return config;
}
}

View File

@ -6,13 +6,12 @@ import org.gcube.dataanalysis.ecoengine.configuration.AlgorithmConfiguration;
import org.gcube.dataanalysis.ecoengine.interfaces.Modeler;
import org.gcube.dataanalysis.ecoengine.processing.factories.ModelersFactory;
public class ModelerT implements Runnable{
public class ModelerT implements Runnable {
Modeler dg;
AlgorithmConfiguration config;
public ModelerT(Modeler dg,AlgorithmConfiguration config) {
public ModelerT(Modeler dg, AlgorithmConfiguration config) {
this.dg = dg;
this.config = config;
}
@ -27,10 +26,10 @@ public class ModelerT implements Runnable{
}
public static void train(AlgorithmConfiguration config) throws Exception {
List<Modeler> modelers=ModelersFactory.getModelers(config);
List<Modeler> modelers = ModelersFactory.getModelers(config);
Modeler modeler = modelers.get(0);
if (modeler != null) {
ModelerT tc = new ModelerT(modeler, config);
Thread t = new Thread(tc);
@ -50,24 +49,39 @@ public class ModelerT implements Runnable{
}
public static AlgorithmConfiguration getTrainingConfig(String modelName, String absenceTable,String presenceTable,String speciesCode,String userName,String layersNeurons, String configPath){
public static AlgorithmConfiguration getTrainingConfigHSPEN(String modelName, String outputTable, String occurrenceCells, String hspenOrigin, String hcaf, String configPath) {
AlgorithmConfiguration config = new AlgorithmConfiguration();
config.setConfigPath(configPath);
config.setPersistencePath("./");
config.setNumberOfResources(2);
config.setModel("HSPEN");
config.setParam("OuputEnvelopeTable", outputTable);
config.setParam("OccurrenceCellsTable", occurrenceCells);
config.setParam("EnvelopeTable", hspenOrigin);
config.setParam("CsquarecodesTable", hcaf);
config.setParam("CreateTable", "true");
return config;
}
public static AlgorithmConfiguration getTrainingConfig(String modelName, String absenceTable, String presenceTable, String speciesCode, String userName, String layersNeurons, String configPath) {
AlgorithmConfiguration config = new AlgorithmConfiguration();
config.setConfigPath(configPath);
config.setPersistencePath(configPath);
config.setNumberOfResources(1);
config.setModel(modelName);
config.setParam("AbsenceDataTable",absenceTable);
config.setParam("PresenceDataTable",presenceTable);
config.setParam("SpeciesName",speciesCode);
config.setParam("UserName",userName);
config.setParam("LayersNeurons",layersNeurons);
config.setModel(modelName);
config.setParam("AbsenceDataTable", absenceTable);
config.setParam("PresenceDataTable", presenceTable);
config.setParam("SpeciesName", speciesCode);
config.setParam("UserName", userName);
config.setParam("LayersNeurons", layersNeurons);
return config;
}
}

View File

@ -0,0 +1,174 @@
package org.gcube.dataanalysis.ecoengine.utils;
import java.util.List;
import org.hibernate.SessionFactory;
public class DatabaseUtils {
static String queryDesc = "SELECT column_name,data_type, character_maximum_length, is_nullable FROM information_schema.COLUMNS WHERE table_name ='%1$s'";
static String queryForKeys = "SELECT b.column_name as name, a.constraint_type as type FROM information_schema.table_constraints as a join information_schema.key_column_usage as b on a.table_name ='%1$s' and a.constraint_name = b.constraint_name";
static String genCreationStatement = "CREATE TABLE %1$s ( %2$s %3$s );";
static String updateColValues = "UPDATE %1$s SET %2$s = %3$s.%4$s FROM %3$s WHERE %1$s.%5$s = %3$s.%6$s ;";
SessionFactory referencedbConnection;
public DatabaseUtils(SessionFactory referencedbConnection) {
this.referencedbConnection = referencedbConnection;
}
public static String createUpdateStatement(String tableToUpdate, String fieldToUpdate, String tableFromUpdate, String columnFromUpdate, String tableToUpKey, String tableFromUpKey) {
return String.format(updateColValues, tableToUpdate, fieldToUpdate, tableFromUpdate, columnFromUpdate, tableFromUpdate, tableToUpdate, tableToUpKey, tableFromUpdate, tableFromUpKey);
}
private String primaryK;
private String primaryKColName;
private void getPrimaryKeys(List<Object> keys, String table) {
int keynum = keys.size();
StringBuffer pkeybuffer = new StringBuffer();
primaryKColName = "";
for (int i = 0; i < keynum; i++) {
Object[] valueKey = (Object[]) keys.get(i);
String colname = (String) valueKey[0];
String type = (String) valueKey[1];
if (type.equals("PRIMARY KEY")) {
if (pkeybuffer.length() > 0)
pkeybuffer.append(",");
pkeybuffer.append(colname);
if (primaryKColName.length() > 0)
primaryKColName = ",";
primaryKColName = primaryKColName + colname;
}
}
if (pkeybuffer.length() > 0)
primaryK = ", CONSTRAINT " + table + "_idx PRIMARY KEY (" + pkeybuffer.toString() + ")";
}
public String getPrimaryKey() {
return primaryKColName;
}
private String columnDescrs;
private void getColumnsDesc(List<Object> columnsDescs) {
int num = columnsDescs.size();
StringBuffer colbuffer = new StringBuffer();
for (int i = 0; i < num; i++) {
Object[] descriptions = (Object[]) columnsDescs.get(i);
String colname = "" + descriptions[0];
String type = "" + descriptions[1];
String len = "" + descriptions[2];
String isnullable = "" + descriptions[3];
if (len != null && len.length() > 0 && !len.equals("null"))
type = type + "(" + len + ")";
if (isnullable != null && isnullable.equalsIgnoreCase("NO"))
isnullable = "NOT NULL";
else
isnullable = "";
colbuffer.append(colname + " " + type + " " + isnullable);
if (i < num - 1) {
colbuffer.append(",");
}
}
columnDescrs = colbuffer.toString();
}
List<Object> columns;
public List<Object> getColumnDecriptions() {
return columns;
}
public String getColumnName(int index) {
return "" + ((Object[]) getColumnDecriptions().get(index))[0];
}
public String getColumnType(int index) {
return "" + ((Object[]) getColumnDecriptions().get(index))[1];
}
public static String duplicateTableStatement(String tableFrom, String tableTo) {
return "select * into " + tableTo + " from " + tableFrom;
}
public static String dropTableStatement(String table) {
return "drop table " + table;
}
public static String getOrderedElements(String table, String key, String column) {
return "select " + key + "," + column + " from " + table + " order by " + key;
}
public static String sumElementsStatement(String table, String column) {
return "select sum(" + column + ") from " + table;
}
public static String countElementsStatement(String table) {
return "select count(*) from " + table;
}
public static String update(String table, String valueColumnName, String value, String keyColumnName, String key) {
return "UPDATE " + table + " SET " + valueColumnName + " = '" + value + "'" + " WHERE " + keyColumnName + " = '" + key + "'";
}
public static String insertIntoColumn(String table, String keyColumnName, String valueColumnName, List<Object> couplesColumnAndKeys) {
StringBuffer buffer = new StringBuffer();
int ncols = couplesColumnAndKeys.size();
if (ncols > 0) {
buffer.append("insert into " + table + " (" + keyColumnName + "," + valueColumnName + ") values ");
for (int i = 0; i < ncols; i++) {
Object[] couples = (Object[]) couplesColumnAndKeys.get(i);
String key = "" + couples[0];
String value = "" + couples[1];
buffer.append("(" + key + "," + value + ")");
if (i < ncols - 1)
buffer.append(", ");
}
}
return buffer.toString();
}
public static String copyFileToTableStatement (String file, String table){
return "COPY "+table+" FROM '"+file+"' DELIMITERS ';' WITH NULL AS 'null string'";
}
public static String updateTableColumnFromOther(String tableName, String fieldToUpdate,String otherTable,String otherColumn,String keyColumn,String otherKeyColumn){
return "UPDATE "+tableName+" SET "+fieldToUpdate+" = "+otherTable+"."+otherColumn+" FROM "+otherTable+" WHERE "+tableName+"."+keyColumn +"="+ otherTable+"."+otherKeyColumn;
}
public static String updateColumn(String table, String keyColumnName, String valueColumnName, List<Object> couplesColumnAndKeys) {
StringBuffer buffer = new StringBuffer();
int ncols = couplesColumnAndKeys.size();
for (int i = 0; i < ncols; i++) {
Object[] couples = (Object[]) couplesColumnAndKeys.get(i);
String key = "" + couples[0];
String value = "" + couples[1];
buffer.append("UPDATE " + table + " SET " + valueColumnName + " = '" + value + "'" + " WHERE " + keyColumnName + " = '" + key + "'");
if (i < ncols - 1)
buffer.append(";\n");
}
return buffer.toString();
}
public String buildCreateStatement(String originaltable, String destinationTable) {
// take the structure of table 1
List<Object> keys = DatabaseFactory.executeSQLQuery(String.format(queryForKeys, originaltable), referencedbConnection);
getPrimaryKeys(keys, destinationTable);
columns = DatabaseFactory.executeSQLQuery(String.format(queryDesc, originaltable), referencedbConnection);
getColumnsDesc(columns);
String creationStatement = String.format(genCreationStatement, destinationTable, columnDescrs, primaryK);
return creationStatement;
}
}

View File

@ -0,0 +1,337 @@
package org.gcube.dataanalysis.ecoengine.utils;
import org.gcube.contentmanagement.graphtools.utils.MathFunctions;
public class Operations {
public static double scalarProduct(double[] a, double[] b) {
double sum = 0;
for (int i = 0; i < a.length; i++) {
if (i < b.length)
sum = sum + a[i] * b[i];
}
return sum;
}
public static double sumVector(double[] a) {
double sum = 0;
for (int i = 0; i < a.length; i++) {
sum = sum + a[i];
}
return sum;
}
public static double[] vectorialDifference(double[] a, double[] b) {
double [] diff = new double [a.length];
for (int i = 0; i < a.length; i++) {
if (i < b.length)
diff[i] = a[i] - b[i];
else
diff[i] = a[i];
}
return diff;
}
public static double[] vectorialAbsoluteDifference(double[] a, double[] b) {
double [] diff = new double [a.length];
for (int i = 0; i < a.length; i++) {
if (i < b.length)
diff[i] = Math.abs(a[i] - b[i]);
else
diff[i] = Math.abs(a[i]);
}
return diff;
}
public static double getMax(double[] points) {
double max = -Double.MAX_VALUE;
for (int i = 0; i < points.length; i++) {
if (max < points[i])
max = points[i];
}
return max;
}
public static int getMax(int[] points) {
int max = -Integer.MAX_VALUE;
for (int i = 0; i < points.length; i++) {
if (max < points[i])
max = points[i];
}
return max;
}
public static int getMin(int[] points) {
int min = Integer.MAX_VALUE;
for (int i = 0; i < points.length; i++) {
if (min > points[i])
min = points[i];
}
return min;
}
public static double getMin(double[] points) {
double min = Double.MAX_VALUE;
for (int i = 0; i < points.length; i++) {
if (min > points[i])
min = points[i];
}
return min;
}
// calculates the frequency distribution for a set of points respect to a set of intervals
public static double[] calcFrequencies(double[] interval, double[] points) {
int intervs = interval.length;
int npoints = points.length;
double[] frequencies = new double[intervs];
for (int i = 0; i < intervs; i++) {
for (int j = 0; j < npoints; j++) {
if (((i == 0) && (points[j] < interval[i])) || ((i == intervs - 1) && (points[j] >= interval[i - 1]) && (points[j] <= interval[i])) || ((i > 0) && (points[j] >= interval[i - 1]) && (points[j] < interval[i]))) {
System.out.println("(" + (i == 0 ? "" : interval[i - 1]) + "," + interval[i] + ")" + " - " + points[j]);
frequencies[i] = frequencies[i] + 1;
}
}
}
for (int i = 0; i < intervs; i++) {
frequencies[i] = frequencies[i] / (double) npoints;
}
return frequencies;
}
// checks if an interval contains at least one element from a sequence of points
public static boolean intervalContainsPoints(double min, double max, double[] points) {
// System.out.println(min+"-"+max);
boolean contains = false;
for (int i = 0; i < points.length; i++) {
if ((points[i] >= min) && (points[i] < max)) {
// System.out.println("---->"+points[i]);
contains = true;
break;
}
}
return contains;
}
// finds the best subdivision for a sequence of numbers
public static double[] uniformDivide(double max, double min, double[] points) {
int maxintervals = 10;
int n = maxintervals;
boolean subdivisionOK = false;
double gap = (max - min) / n;
while (!subdivisionOK) {
// System.out.println("*************************");
boolean notcontains = false;
for (int i = 0; i < n; i++) {
double leftmost = 0;
if (i == n - 1)
leftmost = max + 0.01;
else
leftmost = min + gap * (i + 1);
if (!intervalContainsPoints(min + gap * i, leftmost, points)) {
notcontains = true;
break;
}
}
if (notcontains && n > 0) {
n--;
gap = (max - min) / n;
} else if (n == 0) {
n = maxintervals;
subdivisionOK = true;
} else
subdivisionOK = true;
}
double[] intervals = new double[n];
for (int i = 0; i < n; i++) {
if (i<n-1)
intervals[i] = min + gap * (i + 1);
else
intervals[i] = Double.POSITIVE_INFINITY;
}
return intervals;
}
public double[][] standardize(double[][] matrix) {
return standardize(matrix,null,null);
}
public double[] means;
public double[] variances;
// standardizes a matrix: each row represents a vector: outputs columns means and variances
public double[][] standardize(double[][] matrix, double[] meansVec, double[] variancesVec) {
if (matrix.length > 0) {
int ncols = matrix[0].length;
if ((means==null) && (variances==null))
means = new double[ncols];
variances = new double[ncols];
for (int i = 0; i < ncols ; i++) {
double[] icolumn = Transformations.getColumn(i, matrix);
double mean = 0;
if (meansVec == null){
mean = MathFunctions.mean(icolumn);
means[i] = mean;
}
else
mean = meansVec[i];
double variance = 0;
if (variancesVec==null){
variance = com.rapidminer.tools.math.MathFunctions.variance(icolumn, Double.NEGATIVE_INFINITY);
variances[i] = variance;
}
else
variance = variancesVec[i];
for (int j = 0; j < icolumn.length; i++) {
// standardization
icolumn[j] = (icolumn[j] - mean) / variance;
}
Transformations.substColumn(icolumn, i, matrix);
}
}
return matrix;
}
// calculates the number of elements to take from a set with inverse weight respect to the number of elements
public static int calcNumOfRepresentativeElements(int numberOfElements, int minimumNumberToTake) {
return (int)Math.max(minimumNumberToTake,numberOfElements/Math.log10(numberOfElements));
}
public static double[] linearInterpolation(double el1, double el2,int intervals){
double step = (el2-el1)/(double)intervals;
double [] intervalsd = new double[intervals];
intervalsd[0] = el1;
for (int i=1;i<intervals-1;i++){
intervalsd[i] = el1+step*i;
}
intervalsd[intervals-1] = el2;
return intervalsd;
}
private static double parabol(double a, double b,double c,double x,double shift){
return a*(x-shift)*(x-shift)+b*(x-shift)+c;
}
private static double[] inverseParabol(double a, double b,double c,double y){
double []ret = {(-1d*b+Math.sqrt(b*b+4*a*(Math.abs(y)-c)))/(2*a), (-1d*b-Math.sqrt(b*b+4*a*(Math.abs(y)-c)))/(2*a)};
return ret;
}
private static double logaritmicTransformation(double y){
y = Math.abs(y);
if (y ==0) return -Double.MAX_VALUE;
else
return Math.log10(y);
}
//the parabol is centered on the start Point
public static double[] parabolicInterpolation(double startP,double endP,int intervals){
double start = startP;
double end = endP;
double shift = start;
double a = 1000d;
double b = 0d;
double c = 0d;
double parabolStart = parabol(a,b,c,start,shift);
if (start<0)
parabolStart = -1*parabolStart;
double parabolEnd = parabol(a,b,c,end,start);
if (end<0)
parabolEnd = -1*parabolEnd;
double step =0 ;
if (intervals >0){
double difference = Math.abs(parabolEnd-parabolStart);
step = (difference/(double)intervals);
}
double[] linearpoints = new double[intervals];
linearpoints[0] = startP;
// System.out.println("Y0: "+parabolStart);
for (int i=1;i<intervals-1;i++){
double ypoint =0;
if (end>start)
ypoint = parabolStart+(i*step);
else
ypoint = parabolStart-(i*step);
// System.out.println("Y: "+ypoint);
double res[] = inverseParabol(a, b, c, Math.abs(ypoint));
// System.out.println("X: "+linearpoints[i]);
if (ypoint<0)
linearpoints[i]= res[1]+shift;
else
linearpoints[i]= res[0]+shift;
}
linearpoints[intervals-1] = endP;
return linearpoints;
}
public static void main1(String[] args) {
// double [] points = {1,1.2,1.3,2,5};
double[] points = new double[20];
for (int i = 0; i < 20; i++)
points[i] = 10 * Math.random();
double max = getMax(points);
double min = getMin(points);
System.out.println("<" + min + "," + max + ">");
double[] interval = uniformDivide(max, min, points);
double[] frequencies = calcFrequencies(interval, points);
for (int i = 0; i < interval.length; i++) {
System.out.print(interval[i] + " ");
System.out.println("->" + frequencies[i] + " ");
}
}
public static void main(String[] args) {
System.out.println("numbers to take: "+calcNumOfRepresentativeElements(100,100));
double [] interp = linearInterpolation(27.27,28.28,3);
double [] parabinterp = parabolicInterpolation(1,10,9);
System.out.println("");
}
}

View File

@ -0,0 +1,98 @@
package org.gcube.dataanalysis.ecoengine.utils;
import org.gcube.contentmanagement.graphtools.data.BigSamplesTable;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.example.table.DataRow;
import com.rapidminer.example.table.ExampleTable;
public class Transformations {
public static ExampleSet matrix2ExampleSet(double[][] sampleVectors) {
int m = sampleVectors.length;
BigSamplesTable samples = new BigSamplesTable();
for (int k = 0; k < m; k++)
samples.addSampleRow("sample", sampleVectors[k]);
return samples.generateExampleSet();
}
public static double[][] exampleSet2Matrix(ExampleSet set) {
int m = set.size();
ExampleTable table = set.getExampleTable();
int n = table.getAttributeCount();
double[][] matrix = new double[m][n - 1];
for (int i = 0; i < m; i++) {
DataRow row = table.getDataRow(i);
for (int j = 0; j < n - 1; j++) {
if (!table.getAttribute(j).isNominal()) {
double d = row.get(table.getAttribute(j));
matrix[i][j] = d;
}
}
}
return matrix;
}
// gets a column from a matrix
public static double[] getColumn(int index, double[][] matrix) {
int colulen = matrix.length;
double column[] = new double[colulen];
for (int i = 0; i < colulen; i++) {
column[i] = matrix[i][index];
}
return column;
}
// gets a column from a matrix
public static void substColumn(double[] column, int index, double[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
matrix[i][index] = column[i];
}
}
// merge matrixes: puts the rows of a matrix under another matrix
public static double[][] mergeMatrixes(double[][] matrix1, double[][] matrix2) {
if ((matrix1 == null) || (matrix1.length == 0))
return matrix2;
else if ((matrix2 == null) || (matrix2.length == 0))
return matrix1;
else {
int superlen = matrix1.length + matrix2.length;
int len1 = matrix1.length;
int len2 = matrix2.length;
double[][] supermatrix = new double[superlen][];
for (int i = 0; i < len1; i++) {
supermatrix[i] = matrix1[i];
}
for (int i = len1; i < superlen; i++) {
supermatrix[i] = matrix2[i - len1];
}
return supermatrix;
}
}
public static String vector2String(double[] vector){
String out = "(";
for (int i=0;i<vector.length;i++){
if (i>0)
out = out + ","+vector;
else
out = ""+vector;
}
out +=")";
return out;
}
}