Page MenuHomeHEPForge

ReactionPlot.java
No OneTemporary

ReactionPlot.java

package cedar.hepdata.webapp.pages;
import cedar.hepdata.model.*;
import cedar.hepdata.xml.*;
import cedar.hepdata.db.*;
import org.apache.tapestry.annotations.*;
import org.apache.tapestry.services.*;
import org.hibernate.*;
//import org.hibernate.criterion.*;
import java.util.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.List;
import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.Link;
import org.apache.tapestry.MarkupWriter;
import org.apache.tapestry.StreamResponse;
import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.annotations.Parameter;
import org.apache.tapestry.ioc.services.TypeCoercer;
import org.apache.tapestry.dom.Element;
import org.apache.tapestry.services.Response;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.*;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.axis.LogarithmicAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYErrorRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYIntervalSeries;
import org.jfree.data.xy.XYIntervalSeriesCollection;
import org.jfree.data.Range;
import org.jfree.ui.HorizontalAlignment;
public class ReactionPlot{
@Inject
private RequestGlobals _reqGlobals;
@Inject
private org.hibernate.Session _session;
@Persist
List <String> _saved = new ArrayList();
// private YAxis _yaxis;
public String getQueryParam(String param){
return _reqGlobals.getRequest().getParameter(param);
}
public String getHepId(){
return _reqGlobals.getRequest().getParameter("hepid");
}
public String getDsId(){
return _reqGlobals.getRequest().getParameter("dsid");
}
public String getXId(){
return _reqGlobals.getRequest().getParameter("xid");
}
public String getYId(){
return _reqGlobals.getRequest().getParameter("yid");
}
public Long getSpiresId(){
return _paper.getSpiresId();
}
public String getQueryString(){
return _reqGlobals.getRequest().getParameter("string");
}
public String getXscale(){
return _reqGlobals.getRequest().getParameter("xscale");
}
public String getNotXscale(){
if(_reqGlobals.getRequest().getParameter("xscale").equals("lin")){ return "log";}
else { return "lin";}
}
public String getYscale(){
return _reqGlobals.getRequest().getParameter("yscale");
}
public String getNotYscale(){
if(_reqGlobals.getRequest().getParameter("yscale").equals("lin")){ return "log";}
else { return "lin";}
}
public String getSys(){
return _reqGlobals.getRequest().getParameter("sys");
}
public String getNotSys(){
if(_reqGlobals.getRequest().getParameter("sys").equals("yes")){ return "no";}
else { return "yes";}
}
private String _select;
public String getSelect() { return _select; }
public void setSelect(String select) { _select = select; }
public List <String> getSelects(){
List <String> _selected= new ArrayList();
List <String> _names = _reqGlobals.getRequest().getParameterNames();
int size= _names.size();
for (int i=0; i<size; i++){
if(_names.get(i).startsWith("plot")){
_selected.add(_names.get(i));
}
}
return _selected;
}
private String _save;
public String getSave() { return _save; }
public void setSave(String save) { _save = save; }
public List <String> getSaved(){
List <String> _names = _reqGlobals.getRequest().getParameterNames();
int size= _names.size();
for (int i=0; i<size; i++){
if(_names.get(i).startsWith("save")){
_saved.add(_names.get(i));
}
}
return _saved;
}
public String writePlots(){
List <String> _names = _reqGlobals.getRequest().getParameterNames();
int size= _names.size();
boolean save = false;
for (int i=0; i<size; i++){
if(_names.get(i).startsWith("save")){save=true;}
}
for (int i=0; i<size; i++){
if(_names.get(i).startsWith("plot")){
int i1 = _names.get(i).indexOf("d");
int i2 = _names.get(i).indexOf("y");
String ds = _names.get(i).substring(i1+2,i2);
String ya = _names.get(i).substring(i2+2);
// add to save file /tmp/hepdataplot for the moment
System.out.println("attempting to write to a file");
System.out.println("saved size is " + _saved.size() );
try{
BufferedWriter out = new BufferedWriter(new FileWriter("/tmp/hepdataplot",true));
out.write(_paper.getDataset(Integer.parseInt(ds)).getYAxis(Integer.parseInt(ya)).getAxisId() + "\n");
out.close();
} catch(IOException e) {
//
}
//
}
}
return "";
}
public List <YAxis> getSelectedYAxes(){
List <YAxis> _selected = new ArrayList();
List <String> _names = _reqGlobals.getRequest().getParameterNames();
System.out.println(_names);
int size= _names.size();
for (int i=0; i<size; i++){
if(_names.get(i).startsWith("plot")){
int i1 = _names.get(i).indexOf("d");
int i2 = _names.get(i).indexOf("y");
String ds = _names.get(i).substring(i1+2,i2);
String ya = _names.get(i).substring(i2+2);
_selected.add(_paper.getDataset(Integer.parseInt(ds)).getYAxis(Integer.parseInt(ya)));
}
}
return _selected;
}
public String getFirstAuthorName(){
int n = 0;
String _firstauthor = "";
for (String author : _paper.getAuthors()){
n += 1;
if(n == 1) {_firstauthor = author;}
}
String _refdate = "xx";
for (Reference reference : _paper.getReferences()){
_refdate = reference.getDate();
}
int _lref = _refdate.length();
_firstauthor = _firstauthor + " " + _refdate.substring(_lref-2,_lref);
return _firstauthor;
}
public String getFirstPublished(){
int n = 0;
String _firstpublished = "Not Published";
for (Reference ref : _paper.getReferences()){
String refdesc = ref.getDescription();
if(refdesc.startsWith("PL") ||
refdesc.startsWith("PR") ||
refdesc.startsWith("ZP") ||
refdesc.startsWith("EPJ") ||
refdesc.startsWith("NC") ||
refdesc.startsWith("JPG") ||
refdesc.startsWith("JHEP") ){
n += 1;
if(n == 1) { _firstpublished = refdesc;}
}
}
return _firstpublished;
}
public String getFirstPreprint(){
int n = 0;
String _firstpreprint= "Not Known";
for (Reference reference : _paper.getReferences()){
String ref = reference.getDescription();
if(ref.startsWith("PL") ||
ref.startsWith("PR") ||
ref.startsWith("ZP") ||
ref.startsWith("EPJ") ||
ref.startsWith("NC") ||
ref.startsWith("JPG") ||
ref.startsWith("JHEP") ){
}
else{
n += 1;
if(n == 1) { _firstpreprint = ref;}
}
}
return _firstpreprint;
}
public String getFirstExperimentName(){
int n = 0;
String _firstexperiment = "";
for (Experiment experiment : _paper.getExperiments()){
n += 1;
if(n == 1) { _firstexperiment = experiment.getName();}
}
return _firstexperiment;
}
public String getFirstInformalName(){
int n = 0;
String _firstinformal = "";
for (Experiment experiment : _paper.getExperiments()){
n += 1;
if(n == 1) { _firstinformal = experiment.getInformalName();}
}
return _firstinformal;
}
public String getTitle(){
return _paper.getTitle();
}
public String getFirstDatasetComment(){
int n = 0;
String _firstcomment = "";
for ( String comment : _paper.getDataset(Integer.parseInt(getDsId())).getComments() ){
n += 1;
if(n == 1) { _firstcomment = comment;}
}
return _firstcomment;
}
//YAxis
private static YAxis _yaxis;
public YAxis getYAxis() { return _yaxis; }
public void setYAxis(YAxis yaxis) { _yaxis = yaxis; }
//YAxes
private Paper _paper=null;
public List <YAxis> getYAxes(){
List <YAxis> _ylist = new ArrayList();
Query q=null;
StringBuffer b = new StringBuffer();
// b.append("select distinct p from Paper p where p._hepdataId = ");
b.append("select distinct p from Paper p where p._id = ");
b.append(getHepId());
q = _session.createQuery(b.toString());
_paper = (Paper) q.uniqueResult();
String yid=getYId();
if(yid.equals("0")){yid = "1";}
_ylist.add(_paper.getDataset(Integer.parseInt(getDsId())).getYAxis(Integer.parseInt(yid)));
return _ylist;
}
// now for the plotting stuff
@Inject
private ComponentResources _resources;
public String setTitleStringBlank(){
titlestring="";
// return "zeroed titlrestring";
return "";
}
static private int numberRun = 0;
static private HashMap seriesMap = new HashMap();
static private HashMap xLabelMap = new HashMap();
static private HashMap yLabelMap = new HashMap();
static private HashMap commentMap = new HashMap();
static private String titlestring = "";
static private String caption = "";
//-----------------------------------------getChart3()
public Link getChart3(){
if (numberRun > 10000) {
numberRun=0;
}
int numberRunStart = numberRun;
XYIntervalSeries series1 = null;
if(!getSelectedYAxes().isEmpty()){
System.out.println("there are selected plots");
for (YAxis ya : getSelectedYAxes()){
numberRun +=1;
series1 = getDataSeries(ya,Integer.parseInt(getXId()),getXscale(),getYscale(),true);
seriesMap.put(numberRun,series1);
xLabelMap.put(numberRun,ya.getDataset().getXAxis(1).getHeader());
yLabelMap.put(numberRun,ya.getHeader());
}
}
else if(getYId().equals("0")){
for (YAxis ya : _yaxis.getDataset().getYAxes()){
numberRun +=1;
if(getSys().equals("yes")){
series1 = getDataSeries(ya,Integer.parseInt(getXId()),getXscale(),getYscale(),true);
}
else{
series1 = getDataSeries(ya,Integer.parseInt(getXId()),getXscale(),getYscale(),false);
}
seriesMap.put(numberRun,series1);
xLabelMap.put(numberRun,ya.getDataset().getXAxis(1).getHeader());
yLabelMap.put(numberRun,ya.getHeader());
}
}
else{
numberRun +=1;
if(getSys().equals("yes")){
series1 = getDataSeries(_yaxis,Integer.parseInt(getXId()),getXscale(),getYscale(),true);
}
else{
series1 = getDataSeries(_yaxis,Integer.parseInt(getXId()),getXscale(),getYscale(),false);
}
seriesMap.put(numberRun,series1);
xLabelMap.put(numberRun,_yaxis.getDataset().getXAxis(1).getHeader());
yLabelMap.put(numberRun,_yaxis.getHeader());
}
int number = numberRun-numberRunStart;
String xsize = "500";
String ysize = "500";
// String ysize = Integer.toString(400+number*100);
return _resources.createActionLink("chart", false, new Object[]{xsize,ysize,numberRunStart,number,getXscale(),getYscale()});
}
//-----------------------------------------getDataSeries(YAxis yaxis)
public XYIntervalSeries getDataSeries(YAxis yaxis,int xid, String xscale,String yscale,Boolean systematics){
XAxis _xaxis = yaxis.getDataset().getXAxis(xid);
titlestring=titlestring.concat("\n" + yaxis.getDataset().getId().toString() + "/" + yaxis.getId().toString());
for (String comment : yaxis.getDataset().getComments()){
if(comment.startsWith("Location:")){
titlestring=titlestring.concat(" - " + comment);
}
else{
caption=comment;
}
}
XYIntervalSeries series = new XYIntervalSeries(" ");
// if(!systematics){series.setKey(caption);}
// series.setKey(caption);
series.setKey(yaxis.getDataset().getId().toString() + "/" + yaxis.getId().toString());
for (int ip=1; ip<=yaxis.getPoints().size(); ip++){
try{
double y = yaxis.getPoint(ip).getValue();
double eplus = 0.0;
double eminus = 0.0;
for (Uncertainty e : yaxis.getPoint(ip).getErrors()){
Boolean use = true;
if(e.getSourceType().toString().equals("systematic") && !systematics) { use = false; }
if(use) {
if(e.getNormType() == ErrorNorm.PCT){
eplus = eplus + (y*e.getPlus()/100.0)*(y*e.getPlus()/100.0);
eminus = eminus + (y*e.getMinus()/100.0)*(y*e.getMinus()/100.0);
}
else{
eplus = eplus + e.getPlus()*e.getPlus();
eminus = eminus - e.getMinus()*e.getMinus();
}
}
}
eplus = Math.sqrt(eplus);
eminus = Math.sqrt(eminus);
double yhigh = y + eplus;
double ylow = y - eplus;
double x;
double xhigh;
double xlow;
if(_xaxis.getBin(ip).getLowValue() != null && _xaxis.getBin(ip).getHighValue() != null){
x = (_xaxis.getBin(ip).getLowValue()+_xaxis.getBin(ip).getHighValue())/2;
xlow = _xaxis.getBin(ip).getLowValue();
xhigh = _xaxis.getBin(ip).getHighValue();
}
else{
x = _xaxis.getBin(ip).getFocus();
xhigh = _xaxis.getBin(ip).getFocus();
xlow = _xaxis.getBin(ip).getFocus();
}
if(xscale.equals("lin") && yscale.equals("lin") ) {series.add(x,xlow,xhigh,y,ylow,yhigh);}
if(xscale.equals("log") && yscale.equals("lin") && xlow > 0.0 ) {series.add(x,xlow,xhigh,y,ylow,yhigh);}
if(xscale.equals("lin") && yscale.equals("log") && ylow > 0.0 ) {series.add(x,xlow,xhigh,y,ylow,yhigh);}
if(xscale.equals("log") && yscale.equals("log") && ylow > 0.0 && xlow > 0.0) {series.add(x,xlow,xhigh,y,ylow,yhigh);}
} catch(Exception e){}
}
return series;
}
//-----------------------------------------onChart(..................)
public StreamResponse onChart(final int width, final int height, final int numberRunStart, int number, String _xscale, String _yscale){
final XYIntervalSeriesCollection collection = new XYIntervalSeriesCollection();
double xlow = 1000000.0;
double xhigh = -1000000.0;
double ylow = 1000000.0;
double yhigh = -1000000.0;
for (int n=1; n<=number; n++){
XYIntervalSeries series = (XYIntervalSeries) seriesMap.get(numberRunStart+n);
int nitems = series.getItemCount();
for (int nn=0; nn<nitems; nn++){
if(series.getXLowValue(nn) < xlow){ xlow = series.getXLowValue(nn);};
if(series.getXHighValue(nn) > xhigh){ xhigh = series.getXHighValue(nn);};
if(series.getYLowValue(nn) < ylow){ ylow = series.getYLowValue(nn);};
if(series.getYHighValue(nn) > yhigh){ yhigh = series.getYHighValue(nn);};
}
seriesMap.remove(numberRunStart+n);
collection.addSeries(series);
}
xlow = xlow - 0.05*(xhigh-xlow);
xhigh = xhigh + 0.05*(xhigh-xlow);
ylow = ylow - 0.05*(yhigh-ylow);
yhigh = yhigh + 0.05*(yhigh-ylow);
if(xlow == xhigh){
xlow = 0.90*xlow;
xhigh= 1.10*xhigh;
}
IntervalXYDataset data1 = collection;
// final XYItemRenderer renderer1 = new XYErrorRenderer();
final XYErrorRenderer renderer1 = new XYErrorRenderer();
renderer1.setCapLength(3);
final Range rangex = new Range(xlow,xhigh);
final Range rangey = new Range(ylow,yhigh);
NumberAxis rangeAxisX = null;
if(_xscale.equals("lin")){
rangeAxisX = new NumberAxis();
rangeAxisX.setRange(rangex);
} else{
LogarithmicAxis logaxistemp = new LogarithmicAxis("header");
rangeAxisX = logaxistemp;
}
try {
String xlabel = (String) xLabelMap.get(numberRunStart+1);
rangeAxisX.setLabel(xlabel);
}
catch(Exception e){
rangeAxisX.setLabel("xaxis label ?");
}
// rangeAxisX.setAutoRangeIncludesZero(false);
NumberAxis rangeAxisY = null;
if(_yscale.equals("lin")) {
rangeAxisY = new NumberAxis();
rangeAxisY.setRange(rangey);
} else{
LogarithmicAxis logaxistemp = new LogarithmicAxis(_yaxis.getHeader());
// LogarithmicAxis logaxistemp = new LogarithmicAxis("abc");
// logaxistemp.setAllowNegativesFlag(true);
// logaxistemp.setStrictValuesFlag(false);
// logaxistemp.setAutoRangeNextLogFlag(true);
rangeAxisY = logaxistemp;
}
try{
String ylabel = (String) yLabelMap.get(numberRunStart+1);
rangeAxisY.setLabel(ylabel);
}
catch (Exception e){
rangeAxisY.setLabel("yaxis label ?");
}
final XYPlot subplot1 = new XYPlot(data1, rangeAxisX, rangeAxisY, renderer1);
final TextTitle title = new TextTitle(titlestring);
title.setTextAlignment(HorizontalAlignment.LEFT);
final JFreeChart chart = new JFreeChart(subplot1);
chart.addSubtitle(title);
return new StreamResponse(){
public String getContentType(){
return "image/png";
}
public InputStream getStream() throws IOException {
BufferedImage image = chart.createBufferedImage(width, height);
ByteArrayOutputStream byteArray = new ByteArrayOutputStream() ;
ChartUtilities.writeBufferedImageAsJPEG(byteArray, image) ;
return new ByteArrayInputStream(byteArray.toByteArray());
}
public void prepareResponse(Response response){}
};
}
//---------------------------------------end of onChart(..................)
}

File Metadata

Mime Type
text/plain
Expires
Sat, Dec 21, 4:19 PM (21 h, 55 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4023421
Default Alt Text
ReactionPlot.java (18 KB)

Event Timeline