use of org.jfree.data.time.TimeSeriesCollection in project Gemma by PavlidisLab.
the class ExpressionExperimentQCController method writeDetailedFactorAnalysis.
private boolean writeDetailedFactorAnalysis(ExpressionExperiment ee, OutputStream os) throws Exception {
SVDValueObject svdo = svdService.getSvdFactorAnalysis(ee.getId());
if (svdo == null)
return false;
if (svdo.getFactors().isEmpty() && svdo.getDates().isEmpty()) {
return false;
}
Map<Integer, Map<Long, Double>> factorCorrelations = svdo.getFactorCorrelations();
// Map<Integer, Map<Long, Double>> factorPvalues = svdo.getFactorPvalues();
Map<Integer, Double> dateCorrelations = svdo.getDateCorrelations();
assert ee.getId().equals(svdo.getId());
// need the experimental design
ee = expressionExperimentService.thawLite(ee);
int maxWidth = 30;
Map<Long, String> efs = this.getFactorNames(ee, maxWidth);
Map<Long, ExperimentalFactor> efIdMap = EntityUtils.getIdMap(ee.getExperimentalDesign().getExperimentalFactors());
Collection<Long> continuousFactors = new HashSet<>();
for (ExperimentalFactor ef : ee.getExperimentalDesign().getExperimentalFactors()) {
boolean isContinous = ExperimentalDesignUtils.isContinuous(ef);
if (isContinous) {
continuousFactors.add(ef.getId());
}
}
/*
* Make plots of the dates vs. PCs, factors vs. PCs.
*/
int MAX_COMP = 3;
Map<Long, List<JFreeChart>> charts = new LinkedHashMap<>();
ChartFactory.setChartTheme(StandardChartTheme.createLegacyTheme());
/*
* FACTORS
*/
String componentShorthand = "PC";
for (Integer component : factorCorrelations.keySet()) {
if (component >= MAX_COMP)
break;
String xaxisLabel = componentShorthand + (component + 1);
for (Long efId : factorCorrelations.get(component).keySet()) {
/*
* Should not happen.
*/
if (!efs.containsKey(efId)) {
log.warn("No experimental factor with id " + efId);
continue;
}
if (!svdo.getFactors().containsKey(efId)) {
// this should not happen.
continue;
}
boolean isCategorical = !continuousFactors.contains(efId);
Map<Long, String> categories = new HashMap<>();
if (isCategorical) {
this.getCategories(efIdMap, efId, categories);
}
if (!charts.containsKey(efId)) {
charts.put(efId, new ArrayList<JFreeChart>());
}
Double a = factorCorrelations.get(component).get(efId);
// unique?
String plotname = (efs.get(efId) == null ? "?" : efs.get(efId)) + " " + xaxisLabel;
if (a != null && !Double.isNaN(a)) {
String title = plotname + " " + String.format("%.2f", a);
List<Double> values = svdo.getFactors().get(efId);
Double[] eigenGene = this.getEigenGene(svdo, component);
assert values.size() == eigenGene.length;
/*
* Plot eigengene vs values, add correlation to the plot
*/
JFreeChart chart;
if (isCategorical) {
/*
* Categorical factor
*/
// use the absolute value of the correlation, since direction is arbitrary.
title = plotname + " " + String.format("r=%.2f", Math.abs(a));
DefaultMultiValueCategoryDataset dataset = new DefaultMultiValueCategoryDataset();
/*
* What this code does is organize the factor values by the groups.
*/
Map<String, List<Double>> groupedValues = new TreeMap<>();
for (int i = 0; i < values.size(); i++) {
Long fvId = values.get(i).longValue();
String fvValue = categories.get(fvId);
if (fvValue == null) {
// is this all we need to do?
continue;
}
if (!groupedValues.containsKey(fvValue)) {
groupedValues.put(fvValue, new ArrayList<Double>());
}
groupedValues.get(fvValue).add(eigenGene[i]);
if (log.isDebugEnabled())
log.debug(fvValue + " " + values.get(i));
}
for (String key : groupedValues.keySet()) {
dataset.add(groupedValues.get(key), plotname, key);
}
// don't show the name of the X axis: it's redundant with the title.
NumberAxis rangeAxis = new NumberAxis(xaxisLabel);
rangeAxis.setAutoRangeIncludesZero(false);
// rangeAxis.setAutoRange( false );
rangeAxis.setAutoRangeMinimumSize(4.0);
// rangeAxis.setRange( new Range( -2, 2 ) );
CategoryPlot plot = new CategoryPlot(dataset, new CategoryAxis(null), rangeAxis, new ScatterRenderer());
plot.setRangeGridlinesVisible(false);
plot.setDomainGridlinesVisible(false);
chart = new JFreeChart(title, new Font("SansSerif", Font.BOLD, 12), plot, false);
ScatterRenderer renderer = (ScatterRenderer) plot.getRenderer();
float saturationDrop = (float) Math.min(1.0, component * 0.8f / MAX_COMP);
renderer.setSeriesFillPaint(0, Color.getHSBColor(0.0f, 1.0f - saturationDrop, 0.7f));
renderer.setSeriesShape(0, new Ellipse2D.Double(0, 0, 3, 3));
renderer.setUseOutlinePaint(false);
renderer.setUseFillPaint(true);
renderer.setDefaultFillPaint(Color.white);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
} else {
/*
* Continuous value factor
*/
DefaultXYDataset series = new DefaultXYDataset();
series.addSeries(plotname, new double[][] { ArrayUtils.toPrimitive(values.toArray(new Double[] {})), ArrayUtils.toPrimitive(eigenGene) });
// don't show x-axis label, which would otherwise be efs.get( efId )
chart = ChartFactory.createScatterPlot(title, null, xaxisLabel, series, PlotOrientation.VERTICAL, false, false, false);
XYPlot plot = chart.getXYPlot();
plot.setRangeGridlinesVisible(false);
plot.setDomainGridlinesVisible(false);
XYItemRenderer renderer = plot.getRenderer();
renderer.setDefaultPaint(Color.white);
renderer.setSeriesShape(0, new Ellipse2D.Double(0, 0, 3, 3));
float saturationDrop = (float) Math.min(1.0, component * 0.8f / MAX_COMP);
renderer.setSeriesPaint(0, Color.getHSBColor(0.0f, 1.0f - saturationDrop, 0.7f));
plot.setRenderer(renderer);
}
chart.getTitle().setFont(new Font("SansSerif", Font.BOLD, 12));
charts.get(efId).add(chart);
}
}
}
/*
* DATES
*/
charts.put(-1L, new ArrayList<JFreeChart>());
for (Integer component : dateCorrelations.keySet()) {
String xaxisLabel = componentShorthand + (component + 1);
List<Date> dates = svdo.getDates();
if (dates.isEmpty())
break;
long secspan = ubic.basecode.util.DateUtil.numberOfSecondsBetweenDates(dates);
if (component >= MAX_COMP)
break;
Double a = dateCorrelations.get(component);
if (a != null && !Double.isNaN(a)) {
Double[] eigenGene = svdo.getvMatrix().getColObj(component);
/*
* Plot eigengene vs values, add correlation to the plot
*/
TimeSeries series = new TimeSeries("Dates vs. eigen" + (component + 1));
int i = 0;
for (Date d : dates) {
// if span is less than an hour, retain the minute.
if (secspan < 60 * 60) {
series.addOrUpdate(new Minute(d), eigenGene[i++]);
} else {
series.addOrUpdate(new Hour(d), eigenGene[i++]);
}
}
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(series);
JFreeChart chart = ChartFactory.createTimeSeriesChart("Dates: " + xaxisLabel + " " + String.format("r=%.2f", a), null, xaxisLabel, dataset, false, false, false);
XYPlot xyPlot = chart.getXYPlot();
chart.getTitle().setFont(new Font("SansSerif", Font.BOLD, 12));
// standard renderer makes lines.
XYDotRenderer renderer = new XYDotRenderer();
renderer.setDefaultFillPaint(Color.white);
renderer.setDotHeight(3);
renderer.setDotWidth(3);
// has no effect, need dotheight.
renderer.setSeriesShape(0, new Ellipse2D.Double(0, 0, 3, 3));
float saturationDrop = (float) Math.min(1.0, component * 0.8f / MAX_COMP);
renderer.setSeriesPaint(0, Color.getHSBColor(0.0f, 1.0f - saturationDrop, 0.7f));
ValueAxis domainAxis = xyPlot.getDomainAxis();
domainAxis.setVerticalTickLabels(true);
xyPlot.setRenderer(renderer);
xyPlot.setRangeGridlinesVisible(false);
xyPlot.setDomainGridlinesVisible(false);
charts.get(-1L).add(chart);
}
}
/*
* Plot in a grid, with each factor as a column. FIXME What if we have too many factors to fit on the screen?
*/
int columns = (int) Math.ceil(charts.size());
int perChartSize = ExpressionExperimentQCController.DEFAULT_QC_IMAGE_SIZE_PX;
BufferedImage image = new BufferedImage(columns * perChartSize, MAX_COMP * perChartSize, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = image.createGraphics();
int currentX = 0;
int currentY = 0;
for (Long id : charts.keySet()) {
for (JFreeChart chart : charts.get(id)) {
this.addChartToGraphics(chart, g2, currentX, currentY, perChartSize, perChartSize);
if (currentY + perChartSize < MAX_COMP * perChartSize) {
currentY += perChartSize;
} else {
currentY = 0;
currentX += perChartSize;
}
}
}
os.write(ChartUtils.encodeAsPNG(image));
return true;
}
use of org.jfree.data.time.TimeSeriesCollection in project nimbus by nimbus-org.
the class TimeSeriesCollectionFactoryService method createDataset.
/**
* データセットを生成する。<p>
*
* @param dsConditions データセット条件配列
* @return データセット
* @exception DatasetCreateException
*/
public Dataset createDataset(DatasetCondition[] dsConditions) throws DatasetCreateException {
DatasetConnection connection = createConnection(dsConditions);
TimeSeriesCollection dataset = new TimeSeriesCollection();
try {
List cursors = connection.getSeriesCursorList();
if (cursors == null) {
return dataset;
}
Calendar workCal = Calendar.getInstance();
Holder inOut = new Holder();
Record record = new Record();
DoubleList sameDateValues = null;
OHLCList ohlcList = null;
for (int i = 0, imax = cursors.size(); i < imax; i++) {
TimeSeriesCursor cursor = (TimeSeriesCursor) cursors.get(i);
String series = cursor.getSeriesName();
Class timePeriodClass = (Class) timePeriodClassMap.get(series);
int periodType = 0;
TimeSeries timeSeries = null;
if (collateDataType != 0) {
if (timePeriodClass == null) {
timePeriodClass = Millisecond.class;
}
timeSeries = new TimeSeries(series, timePeriodClass);
} else {
if (timePeriodClass != null) {
timeSeries = new TimeSeries(series, timePeriodClass);
} else {
timeSeries = new TimeSeries(series);
timePeriodClass = timeSeries.getTimePeriodClass();
if (timePeriodClass == null) {
timePeriodClass = Millisecond.class;
}
}
}
periodType = convertPeriodType(timePeriodClass);
double value = 0d;
if (sameDateValues != null) {
sameDateValues.clear();
}
if (ohlcList != null) {
ohlcList.clear();
}
inOut.clear();
record.clear();
Date date = null;
boolean hasNext = cursor.next();
while (hasNext) {
// 同値の最後のデータを追加する際に使う日付
if (inOut.date == null || inOut.preDate == null) {
inOut.preDate = inOut.date;
} else {
inOut.preDate.setTime(inOut.date.getTime());
}
date = cursor.getDate();
if (date == null) {
throw new DatasetCreateException("date is null.");
}
value = cursor.getValue();
boolean wasNull = cursor.wasNull();
if (!wasNull) {
inOut.date = date;
if (isAutoTimesharing) {
// 自動時分割を行う
if (inOut.preDate != null && inOut.preDate.equals(date)) {
// 同じ時間の値を溜め込む
record.setDate(date);
record.add(value);
hasNext = cursor.next();
if (hasNext) {
continue;
}
} else {
record.setPeriodMillis(getPeriodMillis(workCal, inOut.lastDate, inputDataField, inputDataPeriod));
// 溜め込んだ同じ時間の値をTimeSeriesに追加
inOut.date = inOut.preDate;
double tmpValue = Double.NaN;
while (record.hasNext()) {
if (inOut.date == null || inOut.preDate == null) {
inOut.preDate = (Date) inOut.date.clone();
} else {
inOut.preDate.setTime(inOut.date.getTime());
}
inOut.date = record.nextDate();
tmpValue = record.nextValue();
addTimeSeries(date, tmpValue, workCal, timeSeries, periodType, false, inOut);
}
record.clear();
inOut.date = date;
}
}
}
if (hasNext) {
hasNext = cursor.next();
}
if (!hasNext) {
// collateDataTypeが設定されていない(0)時はすでにすべての値が追加されているので処理する必要なし
if (collateDataType != 0) {
if (isAutoTimesharing && record.size() != 0) {
record.setPeriodMillis(getPeriodMillis(workCal, inOut.lastDate, inputDataField, inputDataPeriod));
// 溜め込んだ同じ時間の値をTimeSeriesに追加
inOut.date = inOut.preDate;
double tmpValue = Double.NaN;
while (record.hasNext()) {
if (inOut.date == null || inOut.preDate == null) {
inOut.preDate = (Date) inOut.date.clone();
} else {
inOut.preDate.setTime(inOut.date.getTime());
}
inOut.date = record.nextDate();
tmpValue = record.nextValue();
addTimeSeries(date, tmpValue, workCal, timeSeries, periodType, wasNull && !record.hasNext(), inOut);
}
record.clear();
inOut.date = date;
} else {
// 現在の値を追加
addTimeSeries(date, value, workCal, timeSeries, periodType, false, inOut);
}
}
if (!wasNull) {
// 最後の期間の値を追加
addTimeSeries(date, value, workCal, timeSeries, periodType, true, inOut);
}
} else if (!wasNull) {
// 現在の値を追加
addTimeSeries(date, value, workCal, timeSeries, periodType, false, inOut);
}
}
dataset.addSeries(timeSeries);
timePeriodClass = null;
cursor.close();
}
} finally {
connection.close();
}
return dataset;
}
use of org.jfree.data.time.TimeSeriesCollection in project nimbus by nimbus-org.
the class MovingAverageFactoryService method createDataset.
public Dataset createDataset(DatasetCondition[] dsConditions) throws DatasetCreateException {
if (dsConditions == null || dsConditions.length == 0) {
return null;
}
Dataset dataset = null;
try {
dataset = datasetFactory.createDataset(dsConditions);
} catch (DatasetCreateException e) {
throw new MovingAverageCreateException(e);
}
Dataset datasetMV = dataset;
if (dataset instanceof TimeSeriesCollection) {
final List seriesList = ((TimeSeriesCollection) dataset).getSeries();
final int seriesLength = seriesList.size();
final TimeSeriesCollection newCollection = new TimeSeriesCollection();
for (int i = 0; i < periodCounts.length; i++) {
for (int j = 0; j < seriesLength; j++) {
TimeSeries series = (TimeSeries) seriesList.get(j);
series = MovingAverage.createMovingAverage(series, series.getKey() + (suffixs == null ? "" : suffixs[i]), (int) periodCounts[i], (int) (skips == null ? 0 : skips[i]));
newCollection.addSeries(series);
}
}
datasetMV = newCollection;
} else if (dataset instanceof XYDataset) {
XYSeriesCollection newCollection = new XYSeriesCollection();
XYDataset inDataset = (XYDataset) dataset;
for (int i = 0; i < periodCounts.length; i++) {
for (int j = 0; j < inDataset.getSeriesCount(); j++) {
XYSeries series = MovingAverage.createMovingAverage(inDataset, j, inDataset.getSeriesKey(j) + (suffixs == null ? "" : suffixs[i]), periodCounts[i], skips == null ? 0 : skips[i]);
newCollection.addSeries(series);
}
}
datasetMV = newCollection;
}
return datasetMV;
}
use of org.jfree.data.time.TimeSeriesCollection in project ta4j by ta4j.
the class CandlestickChartWithChopIndicator method createChopDataset.
private static TimeSeriesCollection createChopDataset(BarSeries series) {
ChopIndicator indicator = new ChopIndicator(series, CHOP_INDICATOR_TIMEFRAME, CHOP_SCALE_VALUE);
TimeSeriesCollection dataset = new TimeSeriesCollection();
org.jfree.data.time.TimeSeries chartTimeSeries = new org.jfree.data.time.TimeSeries("CHOP_14");
for (int i = 0; i < series.getBarCount(); i++) {
Bar bar = series.getBar(i);
if (i < CHOP_INDICATOR_TIMEFRAME)
continue;
chartTimeSeries.add(new Second(new Date(bar.getEndTime().toEpochSecond() * 1000)), indicator.getValue(i).doubleValue());
}
dataset.addSeries(chartTimeSeries);
return dataset;
}
use of org.jfree.data.time.TimeSeriesCollection in project ta4j by ta4j.
the class CandlestickChartWithChopIndicator method main.
public static void main(String[] args) {
series = CsvTradesLoader.loadBitstampSeries();
/*
* Create the OHLC dataset from the data series
*/
OHLCDataset ohlcDataset = createOHLCDataset(series);
/*
* Create volume dataset
*/
TimeSeriesCollection xyDataset = createAdditionalDataset(series);
/*
* add the CHOP Indicator
*/
TimeSeriesCollection chopSeries = createChopDataset(series);
/*
* Display the chart
*/
displayChart(ohlcDataset, xyDataset, chopSeries);
}
Aggregations