private static void InterpolateDimension(string name, ISpectralAnalysisWriter writer, List <double> xvalues, List <double> yvalues, List <SpectralAnchorPointMatch> anchorPoints, Func <double, double, double> func) { var interpolator = new LoessInterpolator(); var pre = new List <double>(); var post = new List <double>(); var fit = interpolator.Smooth(xvalues, yvalues, FitFunctionFactory.Create(FitFunctionTypes.TriCubic)); writer.WriteLine(""); WriteMatches(name, writer, xvalues, yvalues, fit, interpolator, anchorPoints, pre, post, func); WriteErrorHistogram(name, pre, post, writer); }
public void TestLoess(FitFunctionTypes type, FitFunctionTypes generatingFunction, double dt, double attenuation) { var interpolator = new LoessInterpolator(.25, 0); var xValues = new List <double>(); var yValues = new List <double>(); double cv = 0; var fitFunction = FitFunctionFactory.Create(type); var genFunction = FitFunctionFactory.Create(generatingFunction); var random = new Random(); // Take one period of the sine wave... while (cv < Math.PI * 3) { var value = genFunction(cv) + random.NextDouble() * attenuation; xValues.Add(cv); yValues.Add(value); cv += dt; } var newYValues = interpolator.Smooth(xValues, yValues, fitFunction); for (var i = 0; i < xValues.Count; i++) { Console.WriteLine(@"{0} {1} {2} {3}", i, xValues[i], yValues[i], newYValues[i]); } dt /= 2; cv = 0; Console.WriteLine(); // Take one period of the sine wave... while (cv < Math.PI * 3) { var predicted = interpolator.Predict(cv); Console.WriteLine(@"{0} {1}", cv, predicted); cv += dt; } }
public void TestAlignmentfunction( string path, string outputPath, double bandwidth, int robustnessIterators ) { var data = File.ReadAllLines(path); var x = new List <double>(); var y = new List <double>(); for (var i = 1; i < data.Length; i++) { var columns = data[i].Split('\t'); if (columns.Count() < 4) { continue; } x.Add(Convert.ToDouble(columns[0])); y.Add(Convert.ToDouble(columns[2])); } using (var writer = File.CreateText(outputPath)) { var loess = new LoessInterpolator(bandwidth, robustnessIterators); loess.MaxDistance = bandwidth; loess.Smooth(x, y, FitFunctionFactory.Create(FitFunctionTypes.Cubic)); writer.WriteLine("NET\tNETY\tAligned\tNET\tERRORY\tERROR-Aligned"); for (var i = 0; i < y.Count; i++) { var value = loess.Predict(y[i]); writer.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", x[i], y[i], value, x[i], y[i] - x[i], value - x[i]); } } }
public void CreateAlignmentFunctions(IEnumerable <SpectralAnchorPointMatch> matches) { var netXvalues = new List <double>(); var netYvalues = new List <double>(); var massXvalues = new List <double>(); var massYvalues = new List <double>(); matches = matches.ToList().OrderBy(x => x.AnchorPointX.Net); // 1. Find the best matches // 2. Find only matches that have been made once. var bestMatches = new Dictionary <int, SpectralAnchorPointMatch>(); foreach (var match in matches) { var scan = match.AnchorPointX.Scan; if (bestMatches.ContainsKey(scan)) { if (bestMatches[scan].SimilarityScore < match.SimilarityScore) { bestMatches[scan] = match; } } else { bestMatches.Add(scan, match); } } // 2. Find only those matched once var all = new Dictionary <int, SpectralAnchorPointMatch>(); foreach (var match in bestMatches.Values) { var scan = match.AnchorPointY.Scan; if (all.ContainsKey(scan)) { if (all[scan].SimilarityScore < match.SimilarityScore) { all[scan] = match; } } else { all.Add(scan, match); } } // Then generate the NET Alignment using R1 var anchorPoints = all.Values.OrderBy(x => x.AnchorPointY.Net).ToList(); matches = anchorPoints.Where( x => FeatureLight.ComputeMassPPMDifference(x.AnchorPointX.Mz, x.AnchorPointY.Mz) < 20 && x.AnchorPointX.Spectrum.ParentFeature.ChargeState == x.AnchorPointY.Spectrum.ParentFeature.ChargeState ).ToList(); foreach (var match in matches) { netXvalues.Add(match.AnchorPointX.Net); netYvalues.Add(match.AnchorPointY.Net); } var netInterpolator = new LoessInterpolator(Bandwidth, 5); netInterpolator.Smooth(netYvalues, netXvalues, FitFunctionFactory.Create(FitFunctionTypes.TriCubic)); // Then generate the Mass Alignment using R1 // We also have to resort the matches based on mass now too anchorPoints = all.Values.OrderBy(x => x.AnchorPointY.Mz).ToList(); foreach (var match in anchorPoints) { massXvalues.Add(match.AnchorPointX.Mz); massYvalues.Add(match.AnchorPointY.Mz); } var massInterpolator = new LoessInterpolator(); massInterpolator.Smooth(massYvalues, massXvalues, FitFunctionFactory.Create(FitFunctionTypes.TriCubic)); m_netInterpolator = netInterpolator; m_massInterpolator = massInterpolator; foreach (var match in anchorPoints) { match.AnchorPointY.NetAligned = netInterpolator.Predict(match.AnchorPointY.Net); match.AnchorPointY.MzAligned = massInterpolator.Predict(match.AnchorPointY.Mz); } }