Changeset 7015
 Timestamp:
 09/30/10 14:30:00 (9 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

branches/maven/projects/curvefitter/src/main/java/loci/curvefitter/SLIMCurveFitter.java
r6925 r7015 35 35 package loci.curvefitter; 36 36 37 import com.sun.jna.ptr.DoubleByReference; 38 import com.sun.jna.ptr.FloatByReference; 39 import com.sun.jna.Library; 40 import com.sun.jna.Native; 41 import com.sun.jna.Platform; 42 37 43 /** 38 44 * TODO 39 45 * 40 46 * <dl><dt><b>Source code:</b></dt> 41 * <dd><a href="http://dev.loci.wisc.edu/trac/java/browser/trunk/projects/curvefitter/src/main/java/loci/curvefitter/ SLIMCurveFitter.java">Trac</a>,42 * <a href="http://dev.loci.wisc.edu/svn/java/trunk/projects/curvefitter/src/main/java/loci/curvefitter/ SLIMCurveFitter.java">SVN</a></dd></dl>47 * <dd><a href="http://dev.loci.wisc.edu/trac/java/browser/trunk/projects/curvefitter/src/main/java/loci/curvefitter/GrayNRCurveFitter.java">Trac</a>, 48 * <a href="http://dev.loci.wisc.edu/svn/java/trunk/projects/curvefitter/src/main/java/loci/curvefitter/GrayNRCurveFitter.java">SVN</a></dd></dl> 43 49 * 44 50 * @author Aivar Grislis grislis at wisc.edu 45 51 */ 46 public class SLIMCurveFitter { 52 public class SLIMCurveFitter extends AbstractCurveFitter { 53 static CLibrary s_library; 54 int m_algType; 55 56 public interface CLibrary extends Library { 57 58 //TODO I'm omitting noise, s/b Poisson or Gaussian with lots of photons??? 59 //TODO I'm omitting residuals, see below also, same thing... 60 61 public int RLD_fit(double xInc, 62 double y[], 63 int fitStart, 64 int fitEnd, 65 double instr[], 66 int nInstr, 67 double sig[], 68 DoubleByReference z, 69 DoubleByReference a, 70 DoubleByReference tau, 71 double fitted[], 72 DoubleByReference chiSquare, 73 double chiSquareTarget 74 ); 75 76 //TODO I'm omitted noise, see above and restrainType and fitType, for now 77 //TODO also covar, alpha, errAxes and chiSqPercent 78 //TODO I'm omitting residuals[] aren't residuals = y 0 yFitted??? is there some weighting I'm missing that is timeconsuming/impossible to recreate? 79 80 public int LMA_fit(double xInc, 81 double y[], 82 int fitStart, 83 int fitEnd, 84 double instr[], 85 int n_instr, 86 double sig[], 87 double param[], 88 int paramFree[], 89 int nParam, 90 double fitted[], 91 DoubleByReference chiSquare, 92 double chiSquareTarget 93 ); 94 95 96 /* public int nr_GCI_triple_integral_fitting_engine(float xincr, float y[], int fitStart, int fitEnd, 97 float instr[], int nInstr, int noise, float sig[], 98 FloatByReference z, FloatByReference a, FloatByReference tau, 99 float fitted[], float residuals[], 100 FloatByReference chiSq, float chiSqTarget); 101 102 public int nr_GCI_marquardt_fitting_engine(float xincr, float y[], int nData, int fitStart, int fitEnd, 103 float instr[], int nInstr, int noise, float sig[], 104 float param[], int paramFree[], int nParam, 105 int restrainType, int fitType, 106 float fitted[], float residuals[], 107 FloatByReference chiSq);*/ 108 //, 109 // float covar[], float alpha[], float errAxes[], 110 // float chiSqTarget, int chiSqPercent); 111 112 113 /*public int nr_GCI_marquardt_fitting_engine(float xincr, float *trans, int ndata, int fit_start, int fit_end, 114 float prompt[], int nprompt, //TODO ARG is this actually instr[] & ninstr? 115 noise_type noise, float sig[], 116 float param[], int paramfree[], 117 int nparam, restrain_type restrain, 118 fit_type fit, //TODO ARG void (*fitfunc)(float, float [], float *, float [], int), 119 float *fitted, float *residuals, float *chisq, 120 float **covar, float **alpha, float **erraxes, 121 float chisq_target, int chisq_percent) {*/ 122 } 123 124 public SLIMCurveFitter(int algType) { 125 m_algType = algType; 126 } 127 128 public SLIMCurveFitter() { 129 m_algType = 0; 130 } 131 132 133 /** 134 * @inheritDoc 135 */ 136 public int fitData(ICurveFitData[] dataArray, int start, int stop) { 137 int returnValue = 0; 138 if (null == s_library) { 139 try { 140 // load once, ondemand 141 s_library = (CLibrary) Native.loadLibrary("SLIMCurve", CLibrary.class); 142 } 143 catch (UnsatisfiedLinkError e) { 144 System.out.println("unable to load dynamic library " + e.getMessage()); 145 return 0; 146 } 147 } 148 149 //TODO ARG 9/3/10 these issues still need to be addressed: 150 151 //TODO ARG since initial x = fit_start * xincr we have to supply the unused portion of y[] before fit_start. 152 // if this data were already premassaged it might be better to get rid of fit_start & _end, just give the 153 // portion to be fitted and specify an initial x. 154 //TODO ARG August use initial X of 0. 155 156 DoubleByReference chiSquare = new DoubleByReference(); 157 double chiSquareTarget = 1.0; //TODO s/b specified incoming 158 159 double sig[] = new double[stop+1]; 160 for (int i = 0; i < sig.length; ++i) { 161 sig[i] = 1.0; // basically ignoring sig for now 162 } 163 164 if (0 == m_algType) { //TODO crude; use enums 165 // RLD or triple integral fit 166 DoubleByReference z = new DoubleByReference(); 167 DoubleByReference a = new DoubleByReference(); 168 DoubleByReference tau = new DoubleByReference(); 169 170 for (ICurveFitData data: dataArray) { 171 // grab incoming parameters 172 a.setValue(data.getParams()[0]); 173 tau.setValue(1.0 / data.getParams()[1]); // convert lambda to tau 174 z.setValue(data.getParams()[2]); 175 176 returnValue = s_library.RLD_fit( 177 m_xInc, 178 data.getYData(), //TODO data get data??? 179 start, 180 stop, 181 null, // no instr 182 0, // nInstr 183 sig, 184 z, 185 a, 186 tau, 187 data.getYFitted(), 188 chiSquare, 189 chiSquareTarget 190 ); 191 // set outgoing parameters 192 data.getParams()[0] = a.getValue(); 193 data.getParams()[1] = 1.0 / tau.getValue(); 194 data.getParams()[2] = z.getValue(); 195 } 196 } 197 else { 198 // LMA fit 199 for (ICurveFitData data: dataArray) { 200 returnValue = s_library.LMA_fit( 201 m_xInc, 202 data.getYData(), 203 start, 204 stop, 205 null, // no instr 206 0, // nInstr 207 sig, 208 data.getParams(), 209 toIntArray(m_free), 210 data.getParams().length, 211 data.getYFitted(), 212 chiSquare, 213 chiSquareTarget 214 ); 215 } 216 } 217 //TODO error return deserves more thought 218 return returnValue; 219 } 220 221 int[] toIntArray(boolean[] booleanArray) { 222 int intArray[] = new int[booleanArray.length]; 223 for (int i = 0; i < booleanArray.length; ++i) { 224 intArray[i] = (booleanArray[i] ? 1 : 0); 225 } 226 return intArray; 227 } 228 229 double[] floatToDouble(float[] f) { 230 double d[] = new double[f.length]; 231 for (int i = 0; i < f.length; ++i) { 232 d[i] = f[i]; 233 } 234 return d; 235 } 236 237 float[] doubleToFloat(double[] d) { 238 float f[] = new float[d.length]; 239 for (int i = 0; i < d.length; ++i) { 240 f[i] = (float) d[i]; 241 } 242 return f; 243 } 47 244 48 245 }
Note: See TracChangeset
for help on using the changeset viewer.