[HepData-svn] r1359 - in trunk/hepdata-webapp/src/main/java/cedar/hepdata: formats webapp/pages

blackhole at projects.hepforge.org blackhole at projects.hepforge.org
Mon Jun 7 22:12:23 BST 2010


Author: buckley
Date: Mon Jun  7 22:12:22 2010
New Revision: 1359

Log:
Fixing the ROOT formatter, and adding the (probably dysfunctional) Matplotlib formatter

Added:
   trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/MatplotlibFormatter.java
Modified:
   trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/RootFormatter.java
   trunk/hepdata-webapp/src/main/java/cedar/hepdata/webapp/pages/View.java

Added: trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/MatplotlibFormatter.java
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/MatplotlibFormatter.java	Mon Jun  7 22:12:22 2010	(r1359)
@@ -0,0 +1,122 @@
+package cedar.hepdata.formats;
+
+import cedar.hepdata.model.*;
+import cedar.hepdata.util.*;
+import cedar.hepdata.xml.*;
+import cedar.hepdata.db.*;
+
+import java.util.*;
+
+import org.antlr.stringtemplate.*;
+
+
+public class MatplotlibFormatter {
+
+
+    public static String format(Paper p) {
+        if (p == null) return null;
+        return format(p.getDatasets());
+    }
+
+
+    public static String format(Collection<Dataset> ds) {
+        StringBuffer s = new StringBuffer();
+        s.append(_header());
+        for (Dataset d : ds) {
+            s.append(MatplotlibFormatter._formatDataset(d));
+        }
+        return s.toString();
+    }
+
+
+    public static String format(Dataset d) {
+        String out = null;
+        if (d != null) {
+            StringBuffer s = new StringBuffer();
+            s.append(_header());
+            s.append(MatplotlibFormatter._formatDataset(d));
+            out = s.toString();
+        }
+        return out;
+    }
+
+
+    public static String _header() {
+        StringBuffer s = new StringBuffer();
+        s.append("import matplotlib.pyplot as plt\n");
+        return s.toString();
+    }
+
+
+    public static String _formatDataset(Dataset d) {
+        String out = null;
+        if (d != null) {
+            StringBuffer s = new StringBuffer();
+            for (XAxis x : d.getXAxes()) {
+                for (YAxis y : d.getYAxes()) {
+
+                    // Build Python point array strings
+                    StringBuffer xv = new StringBuffer("xval = [");
+                    StringBuffer xem = new StringBuffer("xerrminus = [");
+                    StringBuffer xep = new StringBuffer("xerrplus = [");
+                    StringBuffer yv = new StringBuffer("yval = [");
+                    StringBuffer yem = new StringBuffer("yerrminus = [");
+                    StringBuffer yep = new StringBuffer("yerrplus = [");
+                    String sep = "";
+                    for (int r = 1; r <= d.getMaxPointId(); ++r) {
+                        Bin b = x.getBin(r);
+                        Point p = y.getPoint(r);
+                        if (b != null && p != null) {
+                            Double xval = b.getFocus();
+                            xv.append(sep + xval.toString());
+                            Double dxminus = b.getDMinus();
+                            xem.append(sep + dxminus.toString());
+                            Double dxplus = b.getDPlus();
+                            xep.append(sep + dxplus.toString());
+                            Double yval = p.getValue();
+                            yv.append(sep + yval.toString());
+                            Double dyminus = p.getQuadCombinedMinusError();
+                            yem.append(sep + dyminus.toString());
+                            Double dyplus = p.getQuadCombinedPlusError();
+                            yep.append(sep + dyplus.toString());
+                            // Once we've seen a valid point, init the comma separator
+                            sep = ", ";
+                        }
+                    }
+
+                    // Append point array strings to output
+                    s.append(xv + "]\n");
+                    s.append(xem + "]\n");
+                    s.append(xep + "]\n");
+                    s.append(yv + "]\n");
+                    s.append(yem + "]\n");
+                    s.append(yep + "]\n");
+
+                    // Make name and path
+                    StringTemplate name = new StringTemplate("d$did$x$xid$y$yid$");
+                    name.setAttribute("did", d.getId());
+                    name.setAttribute("xid", x.getId());
+                    name.setAttribute("yid", y.getId());
+                    StringTemplate fullname = new StringTemplate("p$paperid$_$name$");
+                    fullname.setAttribute("paperid", d.getPaper().getId());
+                    fullname.setAttribute("name", name.toString());
+                    StringTemplate path = new StringTemplate("/HepData/$paperid$/$name$");
+                    path.setAttribute("paperid", d.getPaper().getId());
+                    path.setAttribute("name", name.toString());
+
+                    // Append code to make the graph object
+                    String varname = fullname.toString();
+                    s.append("numpoints = " + d.getNumPoints() + "\n");
+                    s.append(varname + " = plt.plot(numpoints, xval, yval, xerrminus, xerrplus, yerrminus, yerrplus)\n");
+                    s.append(varname + ".SetName(\"" + path.toString() + "\")\n");
+                    s.append(varname + ".SetTitle(\"" + path.toString() + "\")\n");
+                    s.append("\n");
+
+                }
+            }
+            out = s.toString();
+        }
+        return out;
+    }
+
+}

Modified: trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/RootFormatter.java
==============================================================================
--- trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/RootFormatter.java	Mon Jun  7 17:44:03 2010	(r1358)
+++ trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/RootFormatter.java	Mon Jun  7 22:12:22 2010	(r1359)
@@ -21,48 +21,107 @@
 
     public static String format(Collection<Dataset> ds) {
         StringBuffer s = new StringBuffer();
+        s.append(_header());
         for (Dataset d : ds) {
-            s.append(YodaFormatter.format(d));
+            s.append(RootFormatter._formatDataset(d));
         }
+        s.append(_footer());
         return s.toString();
     }
 
 
     public static String format(Dataset d) {
-        String asYODA = null;
+        String out = null;
+        if (d != null) {
+            StringBuffer s = new StringBuffer();
+            s.append(_header());
+            s.append(RootFormatter._formatDataset(d));
+            s.append(_footer());
+            out = s.toString();
+        }
+        return out;
+    }
+
+
+    public static String _header() {
+        return "{\n\n";
+    }
+
+
+    public static String _footer() {
+        return "\n}\n";
+    }
+
+
+    public static String _formatDataset(Dataset d) {
+        String out = null;
         if (d != null) {
             StringBuffer s = new StringBuffer();
             for (XAxis x : d.getXAxes()) {
                 for (YAxis y : d.getYAxes()) {
-                    StringTemplate path = new StringTemplate("/HepData/$paperid$/d$did$-x$xid$-y$yid$");
-                    path.setAttribute("paperid", d.getPaper().getId());
-                    path.setAttribute("did", d.getId());
-                    path.setAttribute("xid", x.getId());
-                    path.setAttribute("yid", y.getId());
-                    s.append("BEGIN SCATTER "  + path.toString());
-                    for (int r = 1; r < d.getMaxPointId(); ++r) {
-                        StringTemplate row = new StringTemplate("$xlow$\t$xhigh$\t$yval$\t$yerr$");
+
+                    // Build Python point array strings
+                    StringBuffer xv = new StringBuffer("xval = {");
+                    StringBuffer xem = new StringBuffer("xerrminus = [");
+                    StringBuffer xep = new StringBuffer("xerrplus = [");
+                    StringBuffer yv = new StringBuffer("yval = [");
+                    StringBuffer yem = new StringBuffer("yerrminus = [");
+                    StringBuffer yep = new StringBuffer("yerrplus = [");
+                    String sep = "";
+                    for (int r = 1; r <= d.getMaxPointId(); ++r) {
                         Bin b = x.getBin(r);
-                        if (b != null) {
-                            Double xlow = b.getLowValue();
-                            Double xhigh = b.getHighValue();
-                            row.setAttribute("xlow", xlow);
-                            row.setAttribute("xhigh", xhigh);
-                        }
                         Point p = y.getPoint(r);
-                        if (p != null) {
-                            row.setAttribute("yval", p.getValue());
-                            double yerr = p.getQuadCombinedPlusMinusError();
-                            row.setAttribute("yerr", yerr);
+                        if (b != null && p != null) {
+                            Double xval = b.getFocus();
+                            xv.append(sep + xval.toString());
+                            Double dxminus = b.getDMinus();
+                            xem.append(sep + dxminus.toString());
+                            Double dxplus = b.getDPlus();
+                            xep.append(sep + dxplus.toString());
+                            Double yval = p.getValue();
+                            yv.append(sep + yval.toString());
+                            Double dyminus = p.getQuadCombinedMinusError();
+                            yem.append(sep + dyminus.toString());
+                            Double dyplus = p.getQuadCombinedPlusError();
+                            yep.append(sep + dyplus.toString());
+                            // Once we've seen a valid point, init the comma separator
+                            sep = ", ";
                         }
-                        s.append(row.toString() + "\n");
                     }
-                    s.append("END SCATTER " + "\n\n\n");
+
+                    // Append point array strings to output
+                    s.append(xv + "]\n");
+                    s.append(xem + "]\n");
+                    s.append(xep + "]\n");
+                    s.append(yv + "]\n");
+                    s.append(yem + "]\n");
+                    s.append(yep + "]\n");
+
+                    // Make name and path
+                    StringTemplate name = new StringTemplate("d$did$x$xid$y$yid$");
+                    name.setAttribute("did", d.getId());
+                    name.setAttribute("xid", x.getId());
+                    name.setAttribute("yid", y.getId());
+                    StringTemplate fullname = new StringTemplate("p$paperid$_$name$");
+                    fullname.setAttribute("paperid", d.getPaper().getId());
+                    fullname.setAttribute("name", name.toString());
+                    StringTemplate path = new StringTemplate("/HepData/$paperid$/$name$");
+                    path.setAttribute("paperid", d.getPaper().getId());
+                    path.setAttribute("name", name.toString());
+
+                    // Append code to make the graph object
+                    String varname = fullname.toString();
+                    s.append("numpoints = " + d.getNumPoints() + "\n");
+                    s.append(varname + " = TGraphAsymmErrors(numpoints, xval, yval, xerrminus, xerrplus, yerrminus, yerrplus)\n");
+                    s.append(varname + ".SetName(\"" + path.toString() + "\")\n");
+                    s.append(varname + ".SetTitle(\"" + path.toString() + "\")\n");
+                    s.append("\n");
+
                 }
             }
-            asYODA = s.toString();
+            out = s.toString();
         }
-        return asYODA;
+        return out;
     }
 
 }

Modified: trunk/hepdata-webapp/src/main/java/cedar/hepdata/webapp/pages/View.java
==============================================================================
--- trunk/hepdata-webapp/src/main/java/cedar/hepdata/webapp/pages/View.java	Mon Jun  7 17:44:03 2010	(r1358)
+++ trunk/hepdata-webapp/src/main/java/cedar/hepdata/webapp/pages/View.java	Mon Jun  7 22:12:22 2010	(r1359)
@@ -57,7 +57,7 @@
             if (fmt.equals("pyroot")) return asPyROOT();
             if (fmt.equals("yoda")) return asYODA();
             if (fmt.equals("root")) return asROOT();
-            //if (fmt.equals("mpl")) return asPlain();
+            if (fmt.equals("mpl")) return asMatplotlib();
             //if (fmt.equals("gnuplot")) return asPlain();
         }
 
@@ -137,8 +137,27 @@
         return formatContext("root");
     }
     public StreamResponse asROOT() {
-        return new TextStreamResponse("text/plain",
-                                      "This will be a ROOT macro once I get round to it...");
+        /// @todo This should really be called "asCINT"
+        Set<Dataset> ds = getDatasets();
+        String asRoot = RootFormatter.format(ds);
+        if (asRoot == null) {
+            asRoot = "No valid paper and dataset specified";
+        }
+        return new TextStreamResponse("text/plain", asRoot);
+    }
+
+
+    public Object getMatplotlibContext() {
+        return formatContext("mpl");
+    }
+    public StreamResponse asMatplotlib() {
+        /// @todo This should really be called "asCINT"
+        Set<Dataset> ds = getDatasets();
+        String asMpl = MatplotlibFormatter.format(ds);
+        if (asMpl == null) {
+            asMpl = "No valid paper and dataset specified";
+        }
+        return new TextStreamResponse("text/plain", asMpl);
     }
 
 
@@ -182,7 +201,7 @@
     }
 
     public boolean getHaveSys() {
-	    String filename = "/home/whalley/systematics/files/" + getPaper().getSpiresId() + ".sys";
+        String filename = "/home/whalley/systematics/files/" + getPaper().getSpiresId() + ".sys";
         File testfile = new File(filename);
         return testfile.exists();
     }


More information about the HepData-svn mailing list