List of usage examples for org.apache.commons.math3.analysis.interpolation SplineInterpolator SplineInterpolator
SplineInterpolator
From source file:uk.ac.diamond.scisoft.ncd.calibration.NCDAbsoluteCalibration.java
public void setAbsoluteData(List<Amount<ScatteringVector>> lstAbsQ, Dataset absI, Unit<ScatteringVector> unit) { absQ = new DoubleDataset(lstAbsQ.size()); for (int idx = 0; idx < lstAbsQ.size(); idx++) { Amount<ScatteringVector> vec = lstAbsQ.get(idx); absQ.set(vec.doubleValue(unit), idx); }// w w w . j ava 2s . c om this.absI = absI.clone(); UnivariateInterpolator interpolator = new SplineInterpolator(); absInterpolate = interpolator.interpolate((double[]) absQ.getBuffer(), (double[]) absI.getBuffer()); }
From source file:uk.ac.diamond.scisoft.ncd.core.DegreeOfOrientation.java
public Object[] process(Serializable buffer, Serializable axis, final int[] dimensions) { double[] parentaxis = (double[]) ConvertUtils.convert(axis, double[].class); float[] parentdata = (float[]) ConvertUtils.convert(buffer, float[].class); int size = dimensions[dimensions.length - 1]; double[] myaxis = new double[size]; double[] mydata = new double[size]; double[] cos2data = new double[size]; double[] sin2data = new double[size]; double[] sincosdata = new double[size]; for (int i = 0; i < parentaxis.length; i++) { myaxis[i] = Math.toRadians(parentaxis[i]); mydata[i] = parentdata[i];/*w w w. ja v a2 s. co m*/ float cos2alpha = (float) Math.cos(2.0 * myaxis[i]); float sin2alpha = (float) Math.sin(2.0 * myaxis[i]); cos2data[i] = (1.0f + cos2alpha) * parentdata[i] / 2.0; sin2data[i] = (1.0f - cos2alpha) * parentdata[i] / 2.0; sincosdata[i] = sin2alpha * parentdata[i] / 2.0; } UnivariateInterpolator interpolator = new SplineInterpolator(); UnivariateFunction function = interpolator.interpolate(myaxis, mydata); UnivariateFunction cos2Function = interpolator.interpolate(myaxis, cos2data); UnivariateFunction sin2Function = interpolator.interpolate(myaxis, sin2data); UnivariateFunction sincosFunction = interpolator.interpolate(myaxis, sincosdata); UnivariateIntegrator integrator = new IterativeLegendreGaussIntegrator(15, BaseAbstractUnivariateIntegrator.DEFAULT_RELATIVE_ACCURACY, BaseAbstractUnivariateIntegrator.DEFAULT_ABSOLUTE_ACCURACY); try { float cos2mean = (float) integrator.integrate(INTEGRATION_POINTS, cos2Function, myaxis[0], myaxis[myaxis.length - 1]); float sin2mean = (float) integrator.integrate(INTEGRATION_POINTS, sin2Function, myaxis[0], myaxis[myaxis.length - 1]); float sincosmean = (float) integrator.integrate(INTEGRATION_POINTS, sincosFunction, myaxis[0], myaxis[myaxis.length - 1]); float norm = (float) integrator.integrate(INTEGRATION_POINTS, function, myaxis[0], myaxis[myaxis.length - 1]); cos2mean /= norm; sin2mean /= norm; sincosmean /= norm; float result = (float) Math.sqrt(Math.pow(cos2mean - sin2mean, 2) - 4.0 * sincosmean * sincosmean); double angle = MathUtils.normalizeAngle(Math.atan2(2.0 * sincosmean, cos2mean - sin2mean) / 2.0, Math.PI); Object[] output = new Object[] { new float[] { result }, new float[] { (float) Math.toDegrees(angle) }, new float[] { (float) (result * Math.cos(angle)), (float) (result * Math.sin(angle)) }, }; return output; } catch (TooManyEvaluationsException e) { return new Object[] { new float[] { Float.NaN }, new double[] { Double.NaN } }; } catch (MaxCountExceededException e) { return new Object[] { new float[] { Float.NaN }, new double[] { Double.NaN } }; } }
From source file:uk.ac.diamond.scisoft.ncd.core.SaxsInvariant.java
public Object[] process(Serializable buffer, Serializable errors, Serializable axis, final int[] dimensions) { double[] parentaxis = (double[]) ConvertUtils.convert(axis, double[].class); float[] parentdata = (float[]) ConvertUtils.convert(buffer, float[].class); double[] parenterrors = (double[]) ConvertUtils.convert(errors, double[].class); int shift = (parentaxis[0] > 0 ? 1 : 0); int size = dimensions[dimensions.length - 1] + shift; double[] myaxis = new double[size]; double[] mydata = new double[size]; double[] myerrors = new double[size]; if (shift > 0) { myaxis[0] = 0.0;//from w ww . j a v a2 s . c om mydata[0] = 0.0; myerrors[0] = 0.0; } for (int i = 0; i < parentaxis.length; i++) { myaxis[i + shift] = parentaxis[i]; mydata[i + shift] = parentdata[i] * parentaxis[i] * parentaxis[i]; myerrors[i + shift] = parenterrors[i] * Math.pow(parentaxis[i], 4); } UnivariateInterpolator interpolator = new SplineInterpolator(); UnivariateFunction function = interpolator.interpolate(myaxis, mydata); UnivariateIntegrator integrator = new IterativeLegendreGaussIntegrator(15, BaseAbstractUnivariateIntegrator.DEFAULT_RELATIVE_ACCURACY, BaseAbstractUnivariateIntegrator.DEFAULT_ABSOLUTE_ACCURACY); try { float result = (float) integrator.integrate(INTEGRATION_POINTS, function, 0.0, myaxis[myaxis.length - 1]); IDataset data = new FloatDataset(parentdata, dimensions); IDataset qaxis = new DoubleDataset(parentaxis, dimensions); PorodPlotData porodPlotData = (PorodPlotData) SaxsAnalysisPlotType.POROD_PLOT.getSaxsPlotDataObject(); SimpleRegression regression = porodPlotData.getPorodPlotParameters(data.squeeze(), qaxis.squeeze()); Amount<Dimensionless> c4 = porodPlotData.getC4(regression); result += (float) (c4.getEstimatedValue() / myaxis[myaxis.length - 1]); double error = 0.0; for (int i = 0; i < myaxis.length; i++) { int idx1 = Math.max(0, i - 1); int idx2 = Math.min(myaxis.length - 1, i + 1); error += Math.pow((myaxis[idx2] - myaxis[idx1]), 2) * myerrors[i] / 4.0; } error += Math.pow(c4.getAbsoluteError() / myaxis[myaxis.length - 1], 2); return new Object[] { new float[] { result }, new double[] { error } }; } catch (TooManyEvaluationsException e) { return new Object[] { new float[] { Float.NaN }, new double[] { Double.NaN } }; } catch (MaxCountExceededException e) { return new Object[] { new float[] { Float.NaN }, new double[] { Double.NaN } }; } }
From source file:uk.ac.ed.bio.SynthSys.SBMLDataTools.SBMLAddTimeCourseData.java
/** * Main command line call.//from www. j a va2 s .c o m * * @param args command line arguments * * @throws IOException if an unexpected IO error occurs. Most common errors are reported nicer * than throwing an exception. */ public static void main(String[] args) throws IOException { Options options = getCommandLineOptions(); try { CommandLineParser parser = new DefaultParser(); CommandLine commandLine = parser.parse(options, args); // Handle help option if (commandLine.hasOption(OPTION_HELP)) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(PROGRAM_NAME, options); return; } // Everything should be in the options so if there are any any left then we have an // error if (commandLine.getArgs().length != 0) { String error = "Usage error: unexpected arguments:"; for (String s : commandLine.getArgList()) { error = error + " " + s; } throw new ParseException(error); } // One of sbmlOut or csvOut is required if (!commandLine.hasOption(OPTION_SBML_OUT) && !commandLine.hasOption(OPTION_CSV_OUT)) { throw new ParseException("One of sbmlOut or csvOut arguments is required."); } // Get the CSV in file Reader csvInReader; if (commandLine.hasOption(OPTION_CSV_IN)) { String fileName = commandLine.getOptionValue(OPTION_CSV_IN); csvInReader = new BufferedReader(new FileReader(fileName)); } else { // Read from stdin csvInReader = new BufferedReader(new InputStreamReader(System.in)); } // Get SBML in reader SBMLDocument doc; if (commandLine.hasOption(OPTION_SBML_IN)) { File file = new File(commandLine.getOptionValue(OPTION_SBML_IN)); doc = SBMLReader.read(file); } else { // Create an empty SBML model int level = getIntegerOption(commandLine, OPTION_SBML_LEVEL, DEFAULT_SBML_LEVEL); int version = getIntegerOption(commandLine, OPTION_SBML_VERSION, DEFAULT_SBML_VERSION); doc = new SBMLDocument(level, version); doc.createModel("model"); } // Get SBML out file File sbmlOutFile = null; if (commandLine.hasOption(OPTION_SBML_OUT)) { sbmlOutFile = new File(commandLine.getOptionValue(OPTION_SBML_OUT)); } // CSV file out BufferedWriter csvOutWriter = null; if (commandLine.hasOption(OPTION_CSV_OUT)) { File csvFileOut = new File(commandLine.getOptionValue(OPTION_CSV_OUT)); csvOutWriter = new BufferedWriter(new FileWriter(csvFileOut)); } // Interpolator String interpolatorName = DEFAULT_INTERPOLATOR; Interpolator interpolator = null; if (commandLine.hasOption(OPTION_INTERPOLATOR)) { interpolatorName = commandLine.getOptionValue(OPTION_INTERPOLATOR); } // Map interpolator to appropriate class instance if (interpolatorName.equalsIgnoreCase("cubic")) { interpolator = new PolynomialInterpolator(new SplineInterpolator()); } else if (interpolatorName.equalsIgnoreCase("linear")) { interpolator = new PolynomialInterpolator(new LinearInterpolator()); } else { throw new ParseException("Unknown interpolator: " + interpolatorName); } // Do the work process(csvInReader, doc.getModel(), csvOutWriter, getSeparator(commandLine), interpolator); csvInReader.close(); if (csvOutWriter != null) csvOutWriter.close(); // Write the SBML file out if (commandLine.hasOption(OPTION_SBML_OUT)) { SBMLWriter.write(doc, sbmlOutFile, "SBMLAddTimeCourseData", "1.0"); } } catch (ParseException e) { System.err.println("Error: " + e.getLocalizedMessage()); HelpFormatter formatter = new HelpFormatter(); formatter.printHelp(PROGRAM_NAME, options); } catch (FileNotFoundException e) { System.err.println("Error: File not found: " + e.getLocalizedMessage()); } catch (WstxUnexpectedCharException e) { System.err.println("Error reading SBML file: " + e.getLocalizedMessage()); } catch (XMLStreamException e) { System.err.println("Error reading SBML file: " + e.getLocalizedMessage()); } catch (IllegalArgumentException e) { System.err.println("Error: " + e.getLocalizedMessage()); } }
From source file:uk.ac.ed.bio.SynthSys.SBMLDataTools.SBMLTimeCourseDataHelperTest.java
/** * Tests that the correct exception is produced when passing a NULL model. *//*w w w. j av a 2s. c om*/ @Test public void addParameterArrayVersionNullModel() { boolean caughtException = false; setSinData(); try { SBMLTimeCourseDataHelper.addParameter(null, "myParam", _times, _values, new PolynomialInterpolator(new SplineInterpolator())); } catch (IllegalArgumentException iae) { caughtException = true; assertEquals("sbmlModel parameter cannot be null", iae.getMessage()); } if (!caughtException) fail("Expected IllegalArgumentException"); }
From source file:uk.ac.ed.bio.SynthSys.SBMLDataTools.SBMLTimeCourseDataHelperTest.java
/** * Tests that the correct exception is produced when passing a NULL parameter name. *//*from w w w . j a v a 2 s.c o m*/ @Test public void addParameterArrayVersionNullParameterName() { boolean caughtException = false; setSinData(); SBMLDocument doc = new SBMLDocument(3, 1); Model model = doc.createModel("test_model"); try { SBMLTimeCourseDataHelper.addParameter(model, null, _times, _values, new PolynomialInterpolator(new SplineInterpolator())); } catch (IllegalArgumentException iae) { caughtException = true; assertEquals("parameterName parameter cannot be null", iae.getMessage()); } if (!caughtException) fail("Expected IllegalArgumentException"); }
From source file:uk.ac.ed.bio.SynthSys.SBMLDataTools.SBMLTimeCourseDataHelperTest.java
/** * Tests that the correct exception is produced when passing NULL times. *//*from w w w . j av a 2s. co m*/ @Test public void addParameterArrayVersionNullTimes() { boolean caughtException = false; setSinData(); SBMLDocument doc = new SBMLDocument(3, 1); Model model = doc.createModel("test_model"); try { SBMLTimeCourseDataHelper.addParameter(model, "myParam", null, _values, new PolynomialInterpolator(new SplineInterpolator())); } catch (IllegalArgumentException iae) { caughtException = true; assertEquals("times parameter cannot be null", iae.getMessage()); } if (!caughtException) fail("Expected IllegalArgumentException"); }
From source file:uk.ac.ed.bio.SynthSys.SBMLDataTools.SBMLTimeCourseDataHelperTest.java
/** * Tests that the correct exception is produced when passing NULL values. *///from w ww .ja v a 2s . com @Test public void addParameterArrayVersionNullValues() { boolean caughtException = false; setSinData(); SBMLDocument doc = new SBMLDocument(3, 1); Model model = doc.createModel("test_model"); try { SBMLTimeCourseDataHelper.addParameter(model, "myParam", _times, null, new PolynomialInterpolator(new SplineInterpolator())); } catch (IllegalArgumentException iae) { caughtException = true; assertEquals("values parameter cannot be null", iae.getMessage()); } if (!caughtException) fail("Expected IllegalArgumentException"); }
From source file:uk.ac.ed.bio.SynthSys.SBMLDataTools.SBMLTimeCourseDataHelperTest.java
/** * Tests that the correct exception is produced when passing times and values arrays of * different lengths.// w ww . j ava2s . co m */ @Test public void addParameterArrayVersionDifferentLengthArrays() { boolean caughtException = false; setSinData(); SBMLDocument doc = new SBMLDocument(3, 1); Model model = doc.createModel("test_model"); // Make longer values array double[] values = new double[_values.length + 1]; System.arraycopy(_values, 0, values, 0, _values.length); values[values.length - 1] = 0.0; try { SBMLTimeCourseDataHelper.addParameter(model, "myParam", _times, values, new PolynomialInterpolator(new SplineInterpolator())); } catch (IllegalArgumentException iae) { caughtException = true; assertEquals("Number of data points in values parameter differs from times parameter", iae.getMessage()); } if (!caughtException) fail("Expected IllegalArgumentException"); }
From source file:uk.ac.ed.bio.SynthSys.SBMLDataTools.SBMLTimeCourseDataHelperTest.java
/** * Tests that the correct exception is produced when passing too few data points. *//*ww w . j a v a 2 s . c o m*/ @Test public void tooFewDataPoints() { boolean caughtException = false; _times = new double[] { -3.0, -2.0 }; _values = new double[_times.length]; for (int i = 0; i < _times.length; ++i) { _values[i] = Math.sin(_times[i]); } SBMLDocument doc = new SBMLDocument(3, 1); Model model = doc.createModel("test_model"); try { SBMLTimeCourseDataHelper.addParameter(model, "myParam", _times, _values, new PolynomialInterpolator(new SplineInterpolator())); } catch (IllegalArgumentException iae) { caughtException = true; assertEquals("Data in the times and values parameters must contain at least 3 data points", iae.getMessage()); } if (!caughtException) fail("Expected IllegalArgumentException"); }