Ejemplo n.º 1
0
        public void CompressionAsyncTest()
        {
            AutoResetEvent          waitHandle = new AutoResetEvent(false);
            CustomCancellationToken token      = CustomCancellationTokenSource.GetToken();

            compressor = new GZipCompressor(waitHandle);
            string decompressedFileName = @"Decompressed_" + startFileName;

            GZipCompressionDelegate del = compressor.Compress;

            del.BeginInvoke(startFileName, endFileName, token, OnCompressionComplete, del);
            waitHandle.WaitOne();
            string startFileHash        = "";
            string decompressedFileHash = "";

            MD5 md5hash = MD5.Create();

            del = compressor.Decompress;
            del.BeginInvoke(endFileName, decompressedFileName, token, OnCompressionComplete, del);
            waitHandle.WaitOne();

            using (Stream startFileStream = File.OpenRead(startFileName))
                using (Stream decompressedFIleStream = File.OpenRead(decompressedFileName))
                {
                    startFileHash        = Encoding.Default.GetString(md5hash.ComputeHash(startFileStream));
                    decompressedFileHash = Encoding.Default.GetString(md5hash.ComputeHash(decompressedFIleStream));
                }
            Assert.AreEqual(startFileHash, decompressedFileHash);
        }
Ejemplo n.º 2
0
        public override void Train(double[] xArr, double[] yArr, CustomCancellationToken token)
        {
            Array.Sort(xArr, yArr);
            _xArr = xArr;
            double[] xNormal;
            double[] yNormal;

            GetResolutionNormalizedPoints(xArr, yArr, out xNormal, out yNormal,
                                          out _minX, out _maxX, out _minY, out _maxY);

            var indStdev = xNormal.StandardDeviation();
            var depStdev = yNormal.StandardDeviation();

            var bandWidth = Math.Pow(xArr.Length, -1f / 6f) * (indStdev + depStdev) / 2.0f;

            var stdev = (float)Math.Min(_resolution / 40f, bandWidth / 2.3548);

            float[,] stamp = GetCosineGaussianStamp(new CosineGaussian(stdev));

            float[,] histogram = new float[_resolution, _resolution];

            StampOutHistogram(histogram, stamp, xNormal, yNormal, token);

            int    bestXi = -1;
            int    bestYi = -1;
            double best   = double.MinValue;

            for (int x = 0; x < _resolution; x++)
            {
                for (int y = 0; y < _resolution; y++)
                {
                    float val = histogram[x, y];
                    if (val > best)
                    {
                        best   = val;
                        bestXi = x;
                        bestYi = y;
                    }
                }
            }
            var points = new LinkedList <Tuple <int, int> >();

            points.AddFirst(new Tuple <int, int>(bestXi, bestYi));
            TraceNorthEast(histogram, bestXi, bestYi, points);
            TraceSouthWest(histogram, bestXi, bestYi, points);
            GetConsolodatedArrays(points, out _consolodatedXY, out _consolodatedYX);
            _rmsd      = 0;
            _smoothedY = new double[xArr.Length];
            for (int i = 0; i < xArr.Length; i++)
            {
                var x    = xArr[i];
                var y    = yArr[i];
                var pred = GetValue(x);
                _smoothedY[i] = pred;
                var diff = y - pred;
                _rmsd += diff * diff / xArr.Length;
            }
            _rmsd = Math.Sqrt(_rmsd);
        }
Ejemplo n.º 3
0
 public LoessRegression(double[] x, double[] y, bool irtIndependent = false, CustomCancellationToken token = null)
 {
     _linearFit = new RegressionLine(x, y);
     _xMin      = x.Min();
     _xMax      = x.Max();
     _loess     = new LoessAligner(0.4);
     _token     = token;
     _loess.Train(x, y, _token ?? CustomCancellationToken.NONE);
     XValues = x;
     YValues = y;
 }
Ejemplo n.º 4
0
        public void CompressionWithCancellationTest()
        {
            AutoResetEvent          waitHandle = new AutoResetEvent(false);
            CustomCancellationToken token      = CustomCancellationTokenSource.GetToken();

            compressor = new GZipCompressor(waitHandle);
            //startFileName = @"video.avi";
            GZipCompressionDelegate del = compressor.Decompress;

            compressor.OnCompressionComplete += compressor_OnCompressionCompleteWithCancellation;
            del.BeginInvoke(startFileName, endFileName, token, null, del);
            Thread.Sleep(6000);
            token.IsCancelled = true;
            waitHandle.WaitOne();
        }
Ejemplo n.º 5
0
        public override void Train(double[] xArr, double[] yArr, CustomCancellationToken token)
        {
            var statX = new Statistics(xArr);
            var statY = new Statistics(yArr);

            _regressionLine        = new RegressionLine(statX.Slope(statY), statX.Intercept(statY));
            _reverseRegressionLine = new RegressionLine(statY.Slope(statX), statY.Intercept(statX));
            _rmsd = 0;
            for (int i = 0; i < xArr.Length; i++)
            {
                var pred = GetValue(xArr[i]);
                var diff = pred - yArr[i];
                _rmsd += diff * diff / xArr.Length;
            }
            _rmsd = Math.Sqrt(_rmsd);
        }
Ejemplo n.º 6
0
        public override void Train(double[] xArr, double[] yArr, CustomCancellationToken token)
        {
            //Calculate lowess
            Array.Sort(xArr, yArr);
            double[] lowessArr;
            if (xArr.Length > 2)
            {
                LoessInterpolator interpolator = new LoessInterpolator(Math.Max(_bandwidth, 2.0 / xArr.Length), _robustIters);
                lowessArr = interpolator.Smooth(xArr, yArr, token);
            }
            else
            {
                lowessArr = yArr;
            }

            _minX = xArr[0];
            _maxX = xArr[xArr.Length - 1];

            _minY = lowessArr.Min();
            _maxY = lowessArr.Max();

            _xArr         = xArr;
            _yArrOrig     = yArr;
            _yArrSmoothed = lowessArr;

            var sum = 0.0;

            for (int i = 0; i < _yArrOrig.Length; i++)
            {
                var e = _yArrOrig[i] - lowessArr[i];
                sum += (e * e) / _yArrOrig.Length;
            }
            _rmsd = Math.Sqrt(sum);

            if (CanCalculateReverseRegression)
            {
                //We must copy arrays and sort twice since
                //X and Y arrays are not necessarily monotonically increasing
                _reverseXArr = new double[_xArr.Length];
                _reverseYArr = new double[_yArrSmoothed.Length];

                Array.Copy(_xArr, _reverseYArr, _xArr.Length);
                Array.Copy(_yArrSmoothed, _reverseXArr, _yArrSmoothed.Length);

                Array.Sort(_reverseXArr, _reverseYArr);
            }
        }
Ejemplo n.º 7
0
 public abstract void Train(double[] xArr, double[] yArr, CustomCancellationToken token);
Ejemplo n.º 8
0
        /// <summary>
        /// Align retention times with a target.
        /// For the MS2 Id's that are found in both the target and the timesToAlign, the MS2 id's
        /// are plotted against each other, and a linear regression is performed.
        /// In cases where there is more than one MS2 id in either file, only the earliest MS2 id from
        /// each file is used.
        /// </summary>
        public static AlignedRetentionTimes AlignLibraryRetentionTimes(IDictionary <Target, double> target, IDictionary <Target, double> originalTimes, double refinementThreshhold, RegressionMethodRT regressionMethod,
                                                                       CustomCancellationToken token)
        {
            var calculator      = new DictionaryRetentionScoreCalculator(@"alignment", originalTimes);
            var targetTimesList = new List <MeasuredRetentionTime>();

            foreach (var entry in calculator.RetentionTimes)
            {
                double targetTime;
                if (!target.TryGetValue(entry.Key, out targetTime))
                {
                    continue;
                }
                MeasuredRetentionTime measuredRetentionTime;
                try
                {
                    measuredRetentionTime = new MeasuredRetentionTime(entry.Key, targetTime);
                }
                catch
                {
                    continue;
                }
                targetTimesList.Add(measuredRetentionTime);
            }

            RetentionTimeStatistics regressionStatistics;
            var regression = RetentionTimeRegression.CalcSingleRegression(XmlNamedElement.NAME_INTERNAL, calculator,
                                                                          targetTimesList, null, false, regressionMethod, out regressionStatistics, out _, token);

            if (regression == null)
            {
                return(null);
            }
            RetentionTimeRegression regressionRefined;
            RetentionTimeStatistics regressionRefinedStatistics = regressionStatistics;
            HashSet <int>           outIndexes = new HashSet <int>();

            if (regressionStatistics.R >= refinementThreshhold)
            {
                regressionRefined = regression;
            }
            else
            {
                var cache = new RetentionTimeScoreCache(new[] { calculator }, new MeasuredRetentionTime[0], null);
                regressionRefined = regression.FindThreshold(refinementThreshhold, null, 0,
                                                             targetTimesList.Count, new MeasuredRetentionTime[0], targetTimesList, null, regressionStatistics,
                                                             calculator, regressionMethod, cache, token, ref regressionRefinedStatistics,
                                                             ref outIndexes);
            }

            return(new AlignedRetentionTimes
            {
                TargetTimes = target,
                OriginalTimes = originalTimes,
                Regression = regression,
                RegressionStatistics = regressionStatistics,
                RegressionRefined = regressionRefined,
                RegressionRefinedStatistics = regressionRefinedStatistics,
                OutlierIndexes = outIndexes,
                Calculator = calculator
            });
        }
Ejemplo n.º 9
0
 private void StampOutHistogram(float[,] histogram, float[,] stamp, double[] xArr, double[] yArr, CustomCancellationToken token)
 {
     for (int p = 0; p < xArr.Length; p++)
     {
         ThreadingHelper.CheckCanceled(token);
         int x = (int)xArr[p];
         int y = (int)yArr[p];
         Stamp(histogram, stamp, x, y);
     }
 }