/// <summary> /// Creates a new instance of the <see cref="PeakDetector"/> class. /// </summary> public PeakDetector(IPeakFunctionComputor peakFunctionComputor, IMathzComputor mathzComputor, IDiluter dilluter, DetectionSettings detectionSettings) { _peakFunctionComputor = peakFunctionComputor; _detectionSettings = detectionSettings; _mathzComputor = mathzComputor; _dilluter = dilluter; }
/// <summary> /// Creates a new instance of the <see cref="PeakDetector"/> class. /// </summary> public PeakDetector(IPeakFunctionComputor peakFunctionComputor) { _peakFunctionComputor = peakFunctionComputor; _detectionSettings = new DetectionSettings(); _mathzComputor = new DefaultMathzComputor(); _dilluter = new DefaultDiluter(); }
/// <summary> /// Creates a new instance of the <see cref="PeakDetector"/> class. /// </summary> public PeakDetector(DetectionSettings detectionSettings) { _detectionSettings = detectionSettings; _peakFunctionComputor = new DefaultPeakFunctionComputor(); _mathzComputor = new DefaultMathzComputor(); _dilluter = new DefaultDiluter(); }
/// <summary> /// Computes the values checking points in specific window. Gets the min and max value in that window and result is average of that. /// </summary> public PeakDetectionData Compute(IEnumerable <IPoint> points, DetectionSettings detectionSettings) { PeakDetectionData peakDetectionData = new PeakDetectionData(); // initialize min/max values peakDetectionData.MinValue = peakDetectionData.MaxValue = 0; // Compute peak function values double[] S = new double[points.Count()]; double maxLeft, maxRight; for (int i = detectionSettings.WindowSize; i < S.Length - detectionSettings.WindowSize; i++) { IPoint currentChartPoint = points.ElementAt(i); IPoint leftChartPoint = points.ElementAt(i - 1); IPoint rightChartPoint = points.ElementAt(i + 1); // used to remember min and max value if (currentChartPoint.Y > peakDetectionData.MaxValue) { peakDetectionData.MaxValue = currentChartPoint.Y; } if (currentChartPoint.Y < peakDetectionData.MinValue || peakDetectionData.MinValue == 0) { peakDetectionData.MinValue = currentChartPoint.Y; } // get difference between point and its left neighbor maxLeft = currentChartPoint.Y - leftChartPoint.Y; maxRight = currentChartPoint.Y - rightChartPoint.Y; // get the max difference for left and right neighbors for (int j = 2; j <= detectionSettings.WindowSize; j++) { leftChartPoint = points.ElementAt(i - j); rightChartPoint = points.ElementAt(i + j); if (currentChartPoint.Y - leftChartPoint.Y > maxLeft) { maxLeft = currentChartPoint.Y - leftChartPoint.Y; } if (currentChartPoint.Y - rightChartPoint.Y > maxRight) { maxRight = currentChartPoint.Y - rightChartPoint.Y; } } S[i] = 0.5f * (maxRight + maxLeft); } // set the peak function values and return peakDetectionData.PeakFunctionValues = S; return(peakDetectionData); }
/// <summary> /// Calculates range between the first and last date. Then gets the minimal distance between two peaks as percentage of range between min/max dates. /// Removes all peaks which are closes as that minimal distance. /// </summary> public List <IPoint> Dillute(IEnumerable <IPoint> points, DetectionSettings detectionSettings) { if (points.Count() == 0) { return(new List <IPoint>()); } // get peaks ordered by their X value List <IPoint> orderedPeaks = new List <IPoint>(points.Where((point) => { return(point.IsPeak); }).OrderBy((point) => { return(point.X); })); int length = orderedPeaks.Count; long distanceBetweenPeaks = points.Last().X.Ticks - points.First().X.Ticks; distanceBetweenPeaks = Convert.ToInt64(distanceBetweenPeaks * detectionSettings.PeakDensityPercentage); for (int index = 0; index < length; index++) { if (index + 1 >= length) { continue; } if (Math.Abs(orderedPeaks[index + 1].X.Ticks - orderedPeaks[index].X.Ticks) <= distanceBetweenPeaks) { if (orderedPeaks[index + 1].Y > orderedPeaks[index].Y) { orderedPeaks[index].IsPeak = false; orderedPeaks.Remove(orderedPeaks[index]); } else { orderedPeaks[index + 1].IsPeak = false; orderedPeaks.Remove(orderedPeaks[index + 1]); } length--; index--; } } return(orderedPeaks); }
/// <summary> /// Checks for peak conditions /// </summary> public bool IsPeak(double peakValue, PeakDetectionData peakDetectionData, DetectionSettings detectionSettings) { // if peak function value minus mean is greater than Stringency * StandardDeviance then point is a peak return(peakValue > 0 && (peakValue - peakDetectionData.Mean) > detectionSettings.Stringency * peakDetectionData.StandardDeviance); }