String openUrlAsString(String address, int maxLines) { StringBuffer sb; try { URL url = new URL(address); InputStream in = url.openStream(); BufferedReader br = new BufferedReader(new InputStreamReader(in)); sb = new StringBuffer(); int count = 0; String line; while ((line = br.readLine()) != null && count++ < maxLines) sb.append(line + "\n"); in.close(); } catch (IOException e) { sb = null; } return sb != null ? new String(sb) : null; }
public boolean get_errors(double[] params, int[] fixes) { GenericDialog gd = new GenericDialog("Error Options"); String[] methods = {"Support Plane", "Monte Carlo"}; gd.addChoice("Method", methods, methods[0]); float conf = 0.67f; gd.addNumericField("SP_Confidence Limit (%)", (int) (conf * 100.0f), 5, 10, null); String[] labels = {"P1", "P2", "P3", "P4", "P5", "P6", "P7", "P8", "P9", "P10"}; gd.addChoice("SP_Parameter", labels, labels[0]); double spacing = 0.01; gd.addNumericField("SP_Chi^2_plot_spacing (% of value)?", spacing * 100.0, 2, 10, null); int ntrials = 100; gd.addNumericField("MC_#_Trials", ntrials, 0); gd.showDialog(); if (gd.wasCanceled()) { return false; } int methodindex = gd.getNextChoiceIndex(); conf = 0.01f * (float) gd.getNextNumber(); int paramindex = gd.getNextChoiceIndex(); spacing = 0.01 * gd.getNextNumber(); ntrials = (int) gd.getNextNumber(); if (methodindex == 0) { support_plane_errors_v2 erclass = new support_plane_errors_v2(this, 0.0001, 50, false, 0.1); int errindex = paramindex; int nfit = 0; for (int i = 0; i < labels.length; i++) { if (fixes[i] == 0) { nfit++; } } int npts = tempdata.length; int dofnum = npts - (nfit - 1) - 1; int dofden = npts - nfit - 1; double flim = (new jdist()).FLimit(dofnum, dofden, (double) conf); IJ.log("FLimit = " + (float) flim); if (flim == Double.NaN && flim < 1.0) { IJ.showMessage("Invalid Limiting F Value"); return false; } double truespacing = Math.abs(params[errindex] * spacing); double[][] c2plot = erclass.geterrors( params, fixes, constraints, tempdata, weights, flim, truespacing, errindex); IJ.log("upper limit = " + c2plot[1][0] + " lower limit = " + c2plot[0][0]); IJ.log( "upper error = " + (c2plot[1][0] - params[errindex]) + " lower error = " + (params[errindex] - c2plot[0][0])); int templength = c2plot[0].length; float[][] c2plotf = new float[2][templength - 1]; for (int i = 0; i < (templength - 1); i++) { c2plotf[0][i] = (float) c2plot[0][i + 1]; c2plotf[1][i] = (float) c2plot[1][i + 1]; } new PlotWindow4("c2 plot", labels[errindex], "Chi^2", c2plotf[0], c2plotf[1]).draw(); } else { StringBuffer sb = new StringBuffer(); sb.append("Trial\t"); for (int i = 0; i < labels.length; i++) { if (fixes[i] == 0) sb.append(labels[i] + "\t"); } sb.append("chi^2"); tw = new TextWindow("Monte Carlo Results", sb.toString(), "", 400, 400); redirect = true; monte_carlo_errors_v2 erclass = new monte_carlo_errors_v2(this, 0.0001, 50, false, 0.1); double[][] errors = erclass.geterrors(params, fixes, constraints, tempdata, weights, ntrials); sb = new StringBuffer(); sb.append("StDev\t"); for (int i = 0; i < errors.length; i++) { float[] ferr = new float[errors[0].length]; for (int j = 0; j < ferr.length; j++) ferr[j] = (float) errors[i][j]; float stdev = jstatistics.getstatistic("StDev", ferr, null); sb.append("" + stdev); if (i < (errors.length - 1)) sb.append("\t"); } tw.append(sb.toString()); redirect = false; } return true; }
private String fixLinuxString(String s) { StringBuffer sb = new StringBuffer(200); for (int i = 0; i < s.length(); i += 2) sb.append(s.charAt(i)); return new String(sb); }