List of usage examples for org.apache.commons.math3.analysis.interpolation LinearInterpolator LinearInterpolator
LinearInterpolator
From source file:uk.ac.diamond.scisoft.analysis.fitting.functions.SmoothGoldEdgeFunction.java
@Override public void setData(IDataset x, IDataset data) { this.xds = DatasetUtils.convertToDataset(x); this.yds = DatasetUtils.convertToDataset(data); try {//from w ww . ja va 2s . c o m // Smooth the data by the real ammount for the smoothed section of the process this.smoothed = ApachePolynomial.getPolynomialSmoothed(xds, yds, (int) Math.round(getParameterValue(0)), 3); // Fit a polyline to this to allow for easy interpolation IDataset arg2 = DatasetUtils.cast(smoothed, Dataset.FLOAT64); this.polySplineFunction = new LinearInterpolator().interpolate(new DoubleDataset(xds).getData(), ((DoubleDataset) arg2).getData()); lowerFit = new SimpleRegression(); double lowerProp = xds.getShape()[0] * getParameterValue(1); for (int i = 0; i < lowerProp; i++) { lowerFit.addData(xds.getDouble(i), yds.getDouble(i)); } lowerFit.regress(); upperFit = new SimpleRegression(); double upperProp = xds.getShape()[0] * (1.0 - getParameterValue(1)); for (int i = xds.getShape()[0] - 1; i > upperProp; i--) { upperFit.addData(xds.getDouble(i), yds.getDouble(i)); } upperFit.regress(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:uk.ac.ed.bio.SynthSys.SBMLDataTools.SBMLAddTimeCourseData.java
/** * Main command line call./* w ww.j a v a 2 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()); } }