public double[] fitfunc(double[] fitparams) { Bindings b = engine.createBindings(); for (int i = 0; i < 10; i++) b.put("P" + (i + 1), fitparams[i]); /*String script1="P1="+fitparams[0]+"; "+ "P2="+fitparams[1]+"; "+ "P3="+fitparams[2]+"; "+ "P4="+fitparams[3]+"; "+ "P5="+fitparams[4]+"; "+ "P6="+fitparams[5]+"; "+ "P7="+fitparams[6]+"; "+ "P8="+fitparams[7]+"; "+ "P9="+fitparams[8]+"; "+ "P10="+fitparams[9]+"; "+ exdef+"; x="; String script2="; retval="+function+";";*/ try { double[] temp = new double[tempx.length]; for (int i = 0; i < tempx.length; i++) { // temp[i]=((Double)engine.eval(script1+(double)tempx[i]+script2)).doubleValue(); b.put("x", tempx[i]); b.put("y", tempdata[i]); temp[i] = (Double) cs.eval(b); } return temp; } catch (Exception e) { IJ.log(e.getMessage()); return null; } }
boolean init_functions() { GenericDialog gd = new GenericDialog("Fitting Options"); gd.addStringField("Extra Definitions", exdef, 50); gd.addCheckbox("Weight Using Plot Errors", false); gd.addStringField("Weighting Equation (y is for data)", weightfunction, 50); gd.addStringField("Fit_Equation", function, 50); gd.showDialog(); if (gd.wasCanceled()) { return false; } exdef = gd.getNextString(); boolean errweights = gd.getNextBoolean(); weightfunction = gd.getNextString(); function = gd.getNextString(); // first initialize the weights weights = new float[tempdata.length]; if (errweights || weightfunction.equals("") || weightfunction == null || weightfunction == "1.0") { if (errweights) { for (int i = 0; i < tempdata.length; i++) weights[i] = 1.0f / (errs[i] * errs[i]); } else { for (int i = 0; i < tempdata.length; i++) weights[i] = 1.0f; } } else { for (int i = 0; i < tempdata.length; i++) { String script = "y =" + tempdata[i] + "; " + "x =" + tempx[i] + "; " + "retval=" + weightfunction + ";"; Double temp = new Double(0.0); try { temp = (Double) engine.eval(script); } catch (Exception e) { IJ.log(e.getMessage()); } if (!(temp.isInfinite() || temp.isNaN())) { weights[i] = temp.floatValue(); } } } // now compile the function script try { String script1 = exdef + "; retval=" + function + ";"; cs = ce.compile(script1); } catch (Exception e) { IJ.log(e.toString()); return false; } return true; }
/** Reads the pixel data from an image described by a FileInfo object. */ Object readPixels(FileInfo fi) { Object pixels = null; try { InputStream is = createInputStream(fi); if (is == null) return null; ImageReader reader = new ImageReader(fi); pixels = reader.readPixels(is); minValue = reader.min; maxValue = reader.max; is.close(); } catch (Exception e) { if (!Macro.MACRO_CANCELED.equals(e.getMessage())) IJ.handleException(e); } return pixels; }