|
[HepData-svn] r1929 - trunk/hepdata-webapp/src/main/java/cedar/hepdata/formatsblackhole at projects.hepforge.org blackhole at projects.hepforge.orgWed Feb 17 16:38:52 GMT 2016
Author: watt Date: Wed Feb 17 16:38:52 2016 New Revision: 1929 Log: YAML formatter: fix location of image files, reformat correlation/covariance matrices Modified: trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/YamlFormatter.java Modified: trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/YamlFormatter.java ============================================================================== --- trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/YamlFormatter.java Fri Feb 12 18:08:19 2016 (r1928) +++ trunk/hepdata-webapp/src/main/java/cedar/hepdata/formats/YamlFormatter.java Wed Feb 17 16:38:52 2016 (r1929) @@ -66,7 +66,7 @@ if(p.getRedId()!=null) { s.append(sp+"{type: red, id: "+p.getRedId()+"}\n"); } if(p.getPublicationYear()!=null) { s.append("publicationyear: "+p.getPublicationYear()+"\n"); } if(p.getPreprintYear()!=null) { s.append("preprintyear: "+p.getPreprintYear()+"\n"); } - s.append("dateupdated: '"+p.getDateUpdated()+"'\n"); + s.append("dateupdated: '"+p.getDateUpdated()+"'\n"); if(p.getModifications().size()>0){ s.append("modifications: # what, by whom and when encoded or changed\n"); int mcount=0; @@ -188,6 +188,7 @@ s.append("name: 'Table "+ds.getId()+"'\n"); for (String ct : ds.getComments()){ if(ct.startsWith("Location:")) { + location = ct.replaceFirst("Location:","").replaceAll(" ",""); s.append("label: '"+ct.replaceFirst("Location:","Data from").replaceAll("'","''")+"'\n"); } } @@ -320,10 +321,12 @@ } return s.toString(); + } - public static String _dataYAML(Dataset ds){ + public static String _dataYAML(Dataset ds){ + String s4=" "; String sp=" - "; StringBuffer s = new StringBuffer(); @@ -331,255 +334,135 @@ int id=idbase+ds.getId(); String location = ""; Paper p = ds.getPaper(); - int npoints=ds.getNumPoints(); + boolean isMatrix = false; + // Check if dscomment contains "covariance", "correlation", "matrix". + for (String ct : ds.getComments()) { + if (ct.toLowerCase().contains("covariance") || + ct.toLowerCase().contains("correlation") || + ct.toLowerCase().contains("matrix")) { + isMatrix = true; + } + } + if (isMatrix) { + // Check if 1 x-axis, and npoints (>= 2) equals number of y-axes. + int nxax = ds.getXAxes().size(); + int nyax = ds.getYAxes().size(); + if (nxax != 1 || nyax != npoints || npoints < 2) + { isMatrix = false; } + } - //s.append("name: 'Table "+ds.getId()+"'\n"); s.append("independent_variables:\n"); - //if (npoints > 0) { for (XAxis xax : ds.getXAxes()){ - String name=xax.getHeader(); - String unit=""; - if(xax.getHeader().contains(" IN ")){ - name=xax.getHeader().substring(0,xax.getHeader().indexOf(" IN ")).trim(); - unit=xax.getHeader().substring(xax.getHeader().indexOf(" IN ")+4).trim(); - } else if (xax.getHeader().contains(" in ")) { - name=xax.getHeader().substring(0,xax.getHeader().indexOf(" in ")).trim(); - unit=xax.getHeader().substring(xax.getHeader().indexOf(" in ")+4).trim(); - } - s.append(sp+"header: {name: '"+name.replaceAll("'","''")+"'"); - if(!unit.equals("")){ s.append(", units: '"+unit+"'"); } - s.append("}\n"); + s.append(_xheader(xax)); if (npoints > 0) { s.append(s4+"values:\n"); } else { s.append(s4+"values: []\n"); } - for (int ip=1; ip<npoints+1; ip++){ + for (int ip=1; ip<npoints+1; ip++) { Bin bin = xax.getBin(ip); - if(bin == null) { - s.append(s4+sp+"{value: '-'}\n"); - } - else { - boolean first=true; - s.append(s4+sp+"{"); - // if(bin.getId() != null ) { - // s.append("id: "+bin.getId()); - // first=false; - // } - - boolean asymmfocus = false; - if (bin.getLowValue() != null && bin.getHighValue() != null && bin.getFocus() != null) { - double diff = bin.getFocus() - (bin.getLowValue() + bin.getHighValue()) / 2.0; - if (Math.abs(diff/bin.getFocus()) > 1E-6) { asymmfocus = true; } - } - boolean haswidth = false; - if (bin.getLowValue() != null && bin.getHighValue() != null) { - double diff = bin.getHighValue() - bin.getLowValue(); - double mean = (bin.getLowValue() + bin.getHighValue()) / 2.0; - if((Math.abs(diff/mean) > 1E-6) || (mean == 0.0 && Math.abs(diff) > 1E-6)){ haswidth = true;} - } - - if(bin.getRelation() != null && bin.getRelation() != Relation.EQUALS ) { - if(!first) { s.append(", "); } - s.append("value: '"+bin.getRelation()+" "); - if (bin.getLowValue() != null) { - s.append(bin.getLowValue()); - } else if (bin.getHighValue() != null) { - s.append(bin.getHighValue()); - } - s.append("'"); - first=false; - } - else if(bin.getFocus() != null) { - if(asymmfocus || !haswidth) { - if(!first) {s.append(", "); } - if(bin.getFocusLength()!=null) { - s.append("value: "+forms(bin.getFocus(),bin.getFocusLength())); - } else { - s.append("value: "+bin.getFocus()); - } - first=false; - } - } - if(bin.getLowValue() != null && haswidth) { - if(!first) { s.append(", "); } - if(bin.getLowValueLength()!=null) { - s.append("low: "+forms(bin.getLowValue(),bin.getLowValueLength())); - } else { - s.append("low: "+bin.getLowValue()); - } - first=false; - } - if(bin.getHighValue() != null && haswidth) { - if(!first) {s.append(", "); } - if(bin.getHighValueLength()!=null) { - s.append("high: "+forms(bin.getHighValue(),bin.getHighValueLength())); - } else { - s.append("high: "+bin.getHighValue()); - } - first=false; - } - if(bin.getDescription() != null ) { - if(!first) { s.append(", "); } - s.append("value: '"+bin.getDescription().replaceAll("'","''")+"'"); - first=false; - } - if (bin.getDescription() == null && bin.getFocus() == null && bin.getLowValue() == null && bin.getHighValue() == null) { - s.append("value: '-'"); + s.append(_xvalue(xax, bin)); + } + if (isMatrix) { + // Repeat for each of the other y-axes. + for (int jp=2; jp<npoints+1; jp++) { + for (int ip=1; ip<npoints+1; ip++) { + Bin bin = xax.getBin(ip); + s.append(_xvalue(xax, bin)); } - if(bin.getId() < xax.getBins().size()) { - s.append("}"); + } + // Second independent variable duplicates the first. + s.append(_xheader(xax)); + s.append(s4+"values:\n"); + for (int ip=1; ip<npoints+1; ip++) { + Bin bin = xax.getBin(ip); + for (int jp=1; jp<npoints+1; jp++) { + s.append(_xvalue(xax, bin)); } - else{s.append("}");} - s.append("\n"); } } } - //} s.append("dependent_variables:\n"); - - //if (npoints > 0) { + boolean firstyax = true; for (YAxis yax : ds.getYAxes()){ - String name=yax.getHeader(); - String unit=""; - if(yax.getHeader().contains(" IN ")){ - name=yax.getHeader().substring(0,yax.getHeader().indexOf(" IN ")).trim(); - unit=yax.getHeader().substring(yax.getHeader().indexOf(" IN ")+4).trim(); - } else if (yax.getHeader().contains(" in ")) { - name=yax.getHeader().substring(0,yax.getHeader().indexOf(" in ")).trim(); - unit=yax.getHeader().substring(yax.getHeader().indexOf(" in ")+4).trim(); - } - s.append(sp+"header: {name: '"+name.replaceAll("'","''")+"'"); - if(!unit.equals("")){ s.append(", units: '"+unit+"'"); } - s.append("}\n"); - boolean first=true; - List <String> storelist = new ArrayList(); - for (Property prop : yax.getProperties()){ - StringBuffer stemp = new StringBuffer(); - if(first) { s.append(s4+"qualifiers:\n"); } - first=false; - - stemp.append(s4+sp+"{name: '"+prop.getName().replaceAll("'","''").replaceFirst("\\.","zzzz")+"'"); - - if(prop.getFocus()!=null || prop.getLowValue()!=null || prop.getHighValue()!=null){ - stemp.append(", value: '"); + if (!isMatrix || firstyax) { + firstyax = false; + String name=yax.getHeader(); + String unit=""; + if(yax.getHeader().contains(" IN ")){ + name=yax.getHeader().substring(0,yax.getHeader().indexOf(" IN ")).trim(); + unit=yax.getHeader().substring(yax.getHeader().indexOf(" IN ")+4).trim(); + } else if (yax.getHeader().contains(" in ")) { + name=yax.getHeader().substring(0,yax.getHeader().indexOf(" in ")).trim(); + unit=yax.getHeader().substring(yax.getHeader().indexOf(" in ")+4).trim(); + } + if (isMatrix) { + // Suppress header if different for second y-axis. + if (!ds.getYAxis(1).getHeader().equals(ds.getYAxis(2).getHeader())) { name = ""; unit = ""; } + } + s.append(sp+"header: {name: '"+name.replaceAll("'","''")+"'"); + if(!unit.equals("")){ s.append(", units: '"+unit+"'"); } + s.append("}\n"); + List <String> storelist = new ArrayList(); + for (Property prop : yax.getProperties()) { + storelist.add(_yprop(prop)); + } + for (String comment : yax.getComments()) { + storelist.add(_ycomment(comment)); + } + Collections.sort(storelist); + List <String> storelist2 = new ArrayList(); + if (isMatrix) { + // Store qualifiers for second y-axis. + for (Property prop : ds.getYAxis(2).getProperties()) { + storelist2.add(_yprop(prop)); + } + for (String comment : ds.getYAxis(2).getComments()) { + storelist2.add(_ycomment(comment)); + } + } + Collections.sort(storelist2); + boolean first=true; + for (int i=0; i<storelist.size(); i++){ + if (!isMatrix || (storelist.get(i).equals(storelist2.get(i)))) { + // If matrix, write qualifier only if same for second y-axis. + if(first) { s.append(s4+"qualifiers:\n"); } + first=false; + s.append(storelist.get(i).replaceFirst("zzzz","")); + } } - if (prop.getFocus() != null){ - stemp.append(prop.getFocus()); + for(Uncertainty error : ds.getErrors()){ + if(error.getPlus()==0.0){ + if(first) { s.append(s4+"qualifiers:\n"); } + first=false; + s.append(s4+sp+"{name: 'SYS', value: '"+error.getComment().replaceAll("'","''")+"'}\n"); + } } - - if (prop.getLowValue() != null && prop.getHighValue() != null && - prop.getLowValue().equals(prop.getHighValue())) { - stemp.append(prop.getLowValue()); - } else { - if(prop.getFocus() != null) { stemp.append(" ("); } - stemp.append(prop.getLowValue()); - stemp.append("-"); - stemp.append(prop.getHighValue()); - if(prop.getFocus() != null) { stemp.append(")"); } - } - if(prop.getFocus()!=null || prop.getLowValue()!=null || prop.getHighValue()!=null){ - stemp.append("'"); - } - - if (prop.getUnit().toString().equals("") || !prop.getUnit().isDimensionless()) { - stemp.append(", units: '"); - stemp.append(prop.getUnit().toString()+"'"); - } - - stemp.append("}\n"); - storelist.add(stemp.toString()); - } - for (String comment : yax.getComments()){ - StringBuffer stemp = new StringBuffer(); - if(first) { s.append(s4+"qualifiers:\n"); } - first=false; - unit=""; - String type=""; - String value=""; - if (comment.contains(":")) { - type = comment.substring(0,comment.indexOf(":")-1).trim(); - value = comment.substring(comment.indexOf(":")+1).trim(); - } else { - type = "."; - value = comment; + for(Uncertainty error : yax.getErrors()){ + if(error.getPlus()==0.0){ + if(first) { s.append(s4+"qualifiers:\n"); } + first=false; + s.append(s4+sp+"{name: 'SYS', value: '"+error.getComment().replaceAll("'","''")+"'}\n"); + } } - // String type="SYS"; - // String value=comment; - // if(comment.indexOf(":")>-1) { - // type=comment.substring(0,comment.indexOf(":")-1).trim(); - // value=comment.substring(comment.indexOf(":")+1).trim(); - // } - if(type.contains(" IN ")){ - unit=type.substring(type.indexOf(" IN ")+4).trim(); - type=type.substring(0,type.indexOf(" IN ")).trim(); - } else if (type.contains(" in ")) { - unit=type.substring(type.indexOf(" in ")+4).trim(); - type=type.substring(0,type.indexOf(" in ")).trim(); - } - stemp.append(s4+sp+"{name: '"+type.replaceAll("'","''").replaceFirst("\\.","zzzz")+"'"); - stemp.append(", value: '"+ value.replaceAll("'","''")+"'"); - // if(type.equals("SYS") && value.indexOf("//")>-1){ - // String comments[] = value.replaceAll("////","//").split("//"); - // for( int i=0; i<comments.length; i++) { - // s.append(s4+sp+"{name: '"+type.replaceAll("'","''")+"'"); - // s.append(", value: '"+ comments[i].replaceAll("'","''")+"'"); - // if(i!=comments.length-1) { s.append("}\n"); } - // } - // } - // else { - // s.append(s4+sp+"{name: '"+type.replaceAll("'","''")+"'"); - // s.append(", value: '"+ value.replaceAll("'","''")+"'"); - // } - if(!unit.equals("")){ - stemp.append(", units: '"+ unit+"'"); - } - stemp.append("}\n"); - storelist.add(stemp.toString()); - } - Collections.sort(storelist); - for (int i=0; i<storelist.size(); i++){ - s.append(storelist.get(i).replaceFirst("zzzz","")); - } - - for(Uncertainty error : ds.getErrors()){ - if(error.getPlus()==0.0){ - if(first) { s.append(s4+"qualifiers:\n"); } - first=false; - s.append(s4+sp+"{name: 'SYS', value: '"+error.getComment().replaceAll("'","''")+"'}\n"); - } - } - for(Uncertainty error : yax.getErrors()){ - if(error.getPlus()==0.0){ - if(first) { s.append(s4+"qualifiers:\n"); } - first=false; - s.append(s4+sp+"{name: 'SYS', value: '"+error.getComment().replaceAll("'","''")+"'}\n"); + if (npoints > 0) { + s.append(s4+"values:\n"); + } else { + s.append(s4+"values: []\n"); } } - - if (npoints > 0) { - s.append(s4+"values:\n"); - } else { - s.append(s4+"values: []\n"); - } - for (int ip=1; ip<npoints+1; ip++){ Point point = yax.getPoint(ip); if(point==null){ - // s.append(s4+sp+"id: "+ip+"\n"); - // s.append(s4+s4+"value: '-'\n"); s.append(s4+sp+"value: '-'\n"); } else{ - // s.append(s4+sp+"id: "+point.getId()+"\n"); - // s.append(s4+s4+"value: "); s.append(s4+sp+"value: "); if(point.getRelation() != Relation.EQUALS){ s.append("'"+point.getRelation()); } - // s.append(point.getValue()+" "+point.getValueLength()+"\n"); if (point.getDescription() != null) { if(point.getRelation() == Relation.EQUALS){ s.append("'"); @@ -593,11 +476,6 @@ } s.append("\n"); boolean firstError=true; - // if (point.getErrors().size() > 0 - // || yax.getErrors().size() > 0 - // || ds.getErrors().size() > 0) { - // s.append(s4+s4+"errors:\n"); - // } if(point.getErrors().size()>0){ for(Uncertainty error : point.getErrors()){ if(firstError) { s.append(s4+s4+"errors:\n"); firstError=false; } @@ -619,21 +497,22 @@ } } } - //} - return s.toString(); + return s.toString(); + } public static String _formatError(Uncertainty error, String which) { - String s4=" "; - String sp=" - "; - StringBuffer s = new StringBuffer(); - Double test = error.getMinus(); - if(which.equals("point")) test=-test; - if((error.getPlus()>=0.0 && (error.getPlus().equals(test))) || error.hasZeroSize()){ - s.append(s4+s4+sp+"{symerror: "); - s.append(error.getPlus()); - if(error.getNormType()==ErrorNorm.PCT){ s.append("%"); } + + String s4=" "; + String sp=" - "; + StringBuffer s = new StringBuffer(); + Double test = error.getMinus(); + if(which.equals("point")) test=-test; + if((error.getPlus()>=0.0 && (error.getPlus().equals(test))) || error.hasZeroSize()){ + s.append(s4+s4+sp+"{symerror: "); + s.append(error.getPlus()); + if(error.getNormType()==ErrorNorm.PCT){ s.append("%"); } } else{ s.append(s4+s4+sp+"{asymerror: "); @@ -652,6 +531,7 @@ } s.append("}\n"); return s.toString(); + } @@ -668,6 +548,177 @@ } if(sval.endsWith(".")) { sval=sval.substring(0,sval.length()-1); } return sval; + + } + + + public static String _xheader(XAxis xax) { + + String sp=" - "; + StringBuffer s = new StringBuffer(); + String name=xax.getHeader(); + String unit=""; + if(xax.getHeader().contains(" IN ")){ + name=xax.getHeader().substring(0,xax.getHeader().indexOf(" IN ")).trim(); + unit=xax.getHeader().substring(xax.getHeader().indexOf(" IN ")+4).trim(); + } else if (xax.getHeader().contains(" in ")) { + name=xax.getHeader().substring(0,xax.getHeader().indexOf(" in ")).trim(); + unit=xax.getHeader().substring(xax.getHeader().indexOf(" in ")+4).trim(); + } + s.append(sp+"header: {name: '"+name.replaceAll("'","''")+"'"); + if(!unit.equals("")){ s.append(", units: '"+unit+"'"); } + s.append("}\n"); + return s.toString(); + + } + + + public static String _xvalue(XAxis xax, Bin bin) { + + String s4=" "; + String sp=" - "; + StringBuffer s = new StringBuffer(); + if(bin == null) { + s.append(s4+sp+"{value: '-'}\n"); + } + else { + boolean first=true; + s.append(s4+sp+"{"); + boolean asymmfocus = false; + if (bin.getLowValue() != null && bin.getHighValue() != null && bin.getFocus() != null) { + double diff = bin.getFocus() - (bin.getLowValue() + bin.getHighValue()) / 2.0; + if (Math.abs(diff/bin.getFocus()) > 1E-6) { asymmfocus = true; } + } + boolean haswidth = false; + if (bin.getLowValue() != null && bin.getHighValue() != null) { + double diff = bin.getHighValue() - bin.getLowValue(); + double mean = (bin.getLowValue() + bin.getHighValue()) / 2.0; + if((Math.abs(diff/mean) > 1E-6) || (mean == 0.0 && Math.abs(diff) > 1E-6)){ haswidth = true;} + } + if(bin.getRelation() != null && bin.getRelation() != Relation.EQUALS ) { + if(!first) { s.append(", "); } + s.append("value: '"+bin.getRelation()+" "); + if (bin.getLowValue() != null) { + s.append(bin.getLowValue()); + } else if (bin.getHighValue() != null) { + s.append(bin.getHighValue()); + } + s.append("'"); + first=false; + } + else if(bin.getFocus() != null) { + if(asymmfocus || !haswidth) { + if(!first) {s.append(", "); } + if(bin.getFocusLength()!=null) { + s.append("value: "+forms(bin.getFocus(),bin.getFocusLength())); + } else { + s.append("value: "+bin.getFocus()); + } + first=false; + } + } + if(bin.getLowValue() != null && haswidth) { + if(!first) { s.append(", "); } + if(bin.getLowValueLength()!=null) { + s.append("low: "+forms(bin.getLowValue(),bin.getLowValueLength())); + } else { + s.append("low: "+bin.getLowValue()); + } + first=false; + } + if(bin.getHighValue() != null && haswidth) { + if(!first) {s.append(", "); } + if(bin.getHighValueLength()!=null) { + s.append("high: "+forms(bin.getHighValue(),bin.getHighValueLength())); + } else { + s.append("high: "+bin.getHighValue()); + } + first=false; + } + if(bin.getDescription() != null ) { + if(!first) { s.append(", "); } + s.append("value: '"+bin.getDescription().replaceAll("'","''")+"'"); + first=false; + } + if (bin.getDescription() == null && bin.getFocus() == null && bin.getLowValue() == null && bin.getHighValue() == null) { + s.append("value: '-'"); + } + if(bin.getId() < xax.getBins().size()) { + s.append("}"); + } + else{s.append("}");} + s.append("\n"); + } + return s.toString(); + + } + + + public static String _yprop(Property prop) { + + String s4=" "; + String sp=" - "; + StringBuffer stemp = new StringBuffer(); + stemp.append(s4+sp+"{name: '"+prop.getName().replaceAll("'","''").replaceFirst("\\.","zzzz")+"'"); + if(prop.getFocus()!=null || prop.getLowValue()!=null || prop.getHighValue()!=null){ + stemp.append(", value: '"); + } + if (prop.getFocus() != null){ + stemp.append(prop.getFocus()); + } + if (prop.getLowValue() != null && prop.getHighValue() != null && + prop.getLowValue().equals(prop.getHighValue())) { + stemp.append(prop.getLowValue()); + } else { + if(prop.getFocus() != null) { stemp.append(" ("); } + stemp.append(prop.getLowValue()); + stemp.append("-"); + stemp.append(prop.getHighValue()); + if(prop.getFocus() != null) { stemp.append(")"); } + } + if(prop.getFocus()!=null || prop.getLowValue()!=null || prop.getHighValue()!=null){ + stemp.append("'"); + } + if (prop.getUnit().toString().equals("") || !prop.getUnit().isDimensionless()) { + stemp.append(", units: '"); + stemp.append(prop.getUnit().toString()+"'"); + } + stemp.append("}\n"); + return stemp.toString(); + + } + + + public static String _ycomment(String comment) { + + String s4=" "; + String sp=" - "; + StringBuffer stemp = new StringBuffer(); + String unit=""; + String type=""; + String value=""; + if (comment.contains(":")) { + type = comment.substring(0,comment.indexOf(":")-1).trim(); + value = comment.substring(comment.indexOf(":")+1).trim(); + } else { + type = "."; + value = comment; + } + if(type.contains(" IN ")){ + unit=type.substring(type.indexOf(" IN ")+4).trim(); + type=type.substring(0,type.indexOf(" IN ")).trim(); + } else if (type.contains(" in ")) { + unit=type.substring(type.indexOf(" in ")+4).trim(); + type=type.substring(0,type.indexOf(" in ")).trim(); + } + stemp.append(s4+sp+"{name: '"+type.replaceAll("'","''").replaceFirst("\\.","zzzz")+"'"); + stemp.append(", value: '"+ value.replaceAll("'","''")+"'"); + if(!unit.equals("")){ + stemp.append(", units: '"+ unit+"'"); + } + stemp.append("}\n"); + return stemp.toString(); + }
More information about the HepData-svn mailing list |