Esempio n. 1
0
        private void UpdateGraph(SampleCollection col, ObservableDataSource <Point> dataSourceToUpdate)
        {
            var points = col.Select(p => new Point(p.X, p.Y));

            dataSourceToUpdate.Collection.Clear();
            dataSourceToUpdate.AppendMany(points);
        }
Esempio n. 2
0
        /// <summary>
        /// creates a step line by clutering the points.
        /// </summary>
        /// <param name="k">the number of steps we will end up with</param>
        /// <param name="numberOfPointsInNextLineToEndCurrentLine">the number of consecutive points which belong to the next group required in order to climb to the next step</param>
        /// <returns></returns>
        public SampleCollection GetStepLineByKMeans(int k, int numberOfPointsInNextLineToEndCurrentLine = 3, bool requireConsecutivePointsToClimb = true, bool removeRedundantStepLines = true)
        {
            var            yVals          = _points.Select(p => p.Y);
            var            kmeans         = OneDimentinalKMeans(yVals, k);
            List <MyPoint> stepLinePoints = new List <MyPoint>();

            if (removeRedundantStepLines)
            {
                kmeans = RemoveRedundantClusters(kmeans);
            }

            //var groups = _points.GroupBy(p => groupAssignments[p.Y]);
            var listOfPoints = _points.Select(p => new { Point = p, Cluster = kmeans.GroupAssignments[p.Y] }).ToList();


            bool goOverClustersInReversedOrder = listOfPoints.Take(4).Any(p => p.Cluster == kmeans.Means.Count - 1);
            int  increment        = goOverClustersInReversedOrder ? -1 : 1;
            int  startCluster     = goOverClustersInReversedOrder ? kmeans.Means.Count - 1 : 0;
            int  start            = goOverClustersInReversedOrder ? listOfPoints.Count - 1 : 0;
            int  climbStepCounter = 0;
            int  currentCluster   = startCluster;

            stepLinePoints.Add(new MyPoint(listOfPoints.First().Point.X, kmeans.Means[startCluster]));

            //estimate an end points for each line
            for (int i = 0; i < listOfPoints.Count; i++)
            {
                //count next cluster points, towards switch
                if (listOfPoints[i].Cluster == currentCluster + increment)
                {
                    ++climbStepCounter;
                }

                //we said consecutive...
                if (listOfPoints[i].Cluster == currentCluster && requireConsecutivePointsToClimb)
                {
                    climbStepCounter = 0;
                }

                //go to next step
                if (climbStepCounter == numberOfPointsInNextLineToEndCurrentLine)
                {
                    stepLinePoints.Add(new MyPoint(listOfPoints[i - numberOfPointsInNextLineToEndCurrentLine + 1].Point.X, kmeans.Means[currentCluster]));

                    currentCluster += increment;
                    stepLinePoints.Add(new MyPoint(listOfPoints[i - numberOfPointsInNextLineToEndCurrentLine + 1].Point.X + 0.00001, kmeans.Means[currentCluster]));
                    climbStepCounter = 0;
                }
            }
            stepLinePoints.Add(new MyPoint(listOfPoints.Last().Point.X, kmeans.Means[currentCluster]));

            var retVal = new SampleCollection(stepLinePoints);

            return(retVal);
        }
Esempio n. 3
0
        public SampleCollection GetLinearRegression()
        {
            var            regLine = ComputeRegressionLine();
            List <MyPoint> points  = new List <MyPoint>();

            foreach (var point in _points)
            {
                points.Add(new MyPoint(point.X, ComputeRegressionYforX(point.X, regLine)));
            }

            var col = new SampleCollection(points);

            return(col);
        }
Esempio n. 4
0
        public SampleCollection GetBaseline(double alpha, double normalityAmp, int startSectionLength)
        {
            Baseline baseline = Baseline.CreateBaseline(_points.Select(p => (long)p.Y).ToList(), 7, startSectionLength, alpha, normalityAmp);

            var points = new List <MyPoint>();

            for (int i = 0; i < baseline.Averages.Count; ++i)
            {
                points.Add(new MyPoint(_points[i].X, baseline.Averages[i]));
            }

            var col = new SampleCollection(points);

            return(col);
        }
Esempio n. 5
0
        public SampleCollection FilterAbnormalsByBaseline(double alpha, double normalityAmp, int startSectionLength)
        {
            Baseline baseline = Baseline.CreateBaseline(_points.Select(p => (long)p.Y).ToList(), 7, startSectionLength, alpha, normalityAmp);

            var points = new List <MyPoint>();

            for (int i = 0; i < baseline.Averages.Count; ++i)
            {
                if (!baseline.AbnormalPoints.Contains(i))
                {
                    points.Add(new MyPoint(_points[i].X, _points[i].Y));
                }
            }

            var col = new SampleCollection(points);

            col._pointDescByXValue = this._pointDescByXValue;

            return(col);
        }
Esempio n. 6
0
        public SampleCollection FilterAbnormalsByRegression()
        {
            var regLine = ComputeRegressionLine();
            var points  = new List <MyPoint>();
            var std     = StandardDeviation(_points.Select(p => p.X).ToArray()) / (_points.Count);

            for (int i = 0; i < _points.Count; ++i)
            {
                double x          = _points[i].X;
                double y          = _points[i].Y;
                var    regrY      = ComputeRegressionYforX(x, regLine);
                bool   isAbnormal = (Math.Abs(regrY - y) > std * 3);

                if (!isAbnormal)
                {
                    points.Add(new MyPoint(_points[i].X, _points[i].Y));
                }
            }
            var col = new SampleCollection(points);

            return(col);
        }
Esempio n. 7
0
        internal void SyncSubtitles()
        {
            try
            {
                PrepareInputSubs(out _langSub, out _timingSub);
            }
            catch
            {
                return;
            }

            Dictionary <LineInfo, LineInfo> matchedLangLines2timingLines = FindBestMatch(_langSub, _timingSub, MatchLinesToSearchForward, MatchMinimumLettersForMatch, MatchSimilarityThreshold);

            //update the counter.
            CountMatchPoints = matchedLangLines2timingLines.Count() + " of " + _langSub.Lines.Count();

            var orderedMatchPoints = matchedLangLines2timingLines.OrderBy(x => x.Key.TimeStamp.FromTime).ToList();

            var diffPoints = orderedMatchPoints.Select(x => (double)(x.Value.TimeStamp.FromTime - x.Key.TimeStamp.FromTime)).ToList();

            ////correct for line in sub diff
            //for (int i = 0; i < orderedMatchPoints.Count(); ++i)
            //{
            //    var keyval = orderedMatchPoints[i];
            //    var langLine = keyval.Key;
            //    var syncLine = keyval.Value;
            //    int shift = langLine.LineNumberInSub - syncLine.LineNumberInSub;
            //    if (shift != 0)
            //    {
            //        double timeForOneLine = (double)syncLine.TimeStamp.Duration / (double)syncLine.TimeStamp.Lines.Count();
            //        double shiftCorrection = shift * timeForOneLine;
            //        diffPoints[i] -= shiftCorrection;
            //    }
            //}

            var timesForXAxis = orderedMatchPoints.Select(p => p.Key.TimeStamp.FromTime).ToList();
            var mypoints      = diffPoints.Select((p, i) => new MyPoint(timesForXAxis[i], p));

            _lnOriginal = new SampleCollection(mypoints);

            Dictionary <double, string> descsByX = new Dictionary <double, string>();

            //get desc for each x
            var listOfDescs = orderedMatchPoints.Select(m => new { x = m.Key.TimeStamp.FromTime, desc = m.Key.Line + "\n" + m.Value.Line }).ToList();

            listOfDescs.ForEach(p =>
            {
                if (!descsByX.ContainsKey(p.x))
                {
                    descsByX.Add(p.x, p.desc);
                }
            });
            _lnOriginal.PointDescByXValue = descsByX;

            if (RemoveAbnormalPoints)
            {
                _lnOriginal = _lnOriginal.FilterAbnormalsByBaseline(BaselineAlgAlpha, NormalZoneAmplitude, (int)StartSectionLength);
            }

            _lnBaseline   = _lnOriginal.GetBaseline(BaselineAlgAlpha, NormalZoneAmplitude, (int)StartSectionLength);
            _lnRegression = _lnOriginal.GetLinearRegression();

            _lnSteps = _lnOriginal.GetStepLineByKMeans(NumberOfStepLines, NumberOfPointsToJumpStep, StepsAlgRequiresConsecutive, StepsAlgRemoveRedundentLines);

            //update the graphs
            UpdateGraph(_lnBaseline, _baselineData);
            UpdateGraph(_lnRegression, _regressionData);
            UpdateGraph(_lnSteps, _stepsData);
            UpdateGraph(_lnOriginal, _actualData);
        }