Beispiel #1
0
        /// <summary>Creates and filled a shrinked or 'crude' instance of Patterns class from the range of the inputArray array
        /// and calculate their averages and standart deviation values.
        /// </summary>
        /// <param name="worker">The BackgroundWorker in wich Processing is run.</param>
        /// <param name="startIndex">The index in the input sequence at which scanning begins.</param>
        /// <param name="length">The length of scanning range.</param>
        /// <param name="shakeCnt">The amount of shakes.</param>
        /// <param name="avrgs">Null in case of Individual mode; the array of single Average otherwise.</param>
        /// <param name="maxFreqNumber">The number of Pattern with max Frequence: used in 'Global' only.</param>
        /// <param name="stepCntOnShake">The number of progress bar steps generated by method within one shake; 0 if without progress bar steps.</param>
        /// <param name="isDrawPlot">True if a plot should be drawing; otherwise, false.</param>
        /// <returns>Sorted by Frequence and shrinked collection in case of isSortByFreq=true; otherwise sorted by Number and unshrinked.</returns>
        public static Patterns GetTreatedPatterns(
            BackgroundWorker worker,
            int startIndex, int length, short shakeCnt,
            ref Average[] avrgs, ref int maxFreqNumber, float stepCntOnShake,
            bool isDrawPlot)
        {
            int strictLength = length - Words.WordLength;

            int[] strictFreqs  = new int[Words.Count];
            int   loopsPerStep = stepCntOnShake > 0 ? (int)(Words.Count / stepCntOnShake) : -1;

            Patterns mainPtns = new Patterns();

            mainPtns.Init(
                worker, Sequence.Original, startIndex,
                strictLength, strictFreqs, loopsPerStep, ref maxFreqNumber, true, isDrawPlot
                );
            if (shakeCnt > 0)           // calculate mean & SD
            {
                int      i;
                int      maxFreqNumberTmp = 0;
                Patterns shakedPtns       = new Patterns();
                // corretcing ProgressBar steps for for shake and scanning
                int shakeSteps = 0;
                //if (loopsPerStep > 3)       shakeSteps = 3;
                //else if (loopsPerStep > 2)  shakeSteps = 2;
                //else if (loopsPerStep > 1)  shakeSteps = 1;
                loopsPerStep -= shakeSteps;

                if (avrgs != null)  //*** Max Average Choice mode set
                {
                    Average.SumCounter sumCounter = new Average.SumCounter();
                    // search patterns with max Frequance for each shake and add this value to the sumCounter
                    for (i = 0; i < shakeCnt; i++)
                    {
                        // we need only max Frequance from the shaked Patterns
                        sumCounter += shakedPtns.Init(worker,
                                                      Sequence.Shake(startIndex, length, worker, shakeSteps),
                                                      0, strictLength, strictFreqs, loopsPerStep, ref maxFreqNumberTmp, false, isDrawPlot
                                                      );
                    }
                    avrgs[0] = new Average(sumCounter); // set total Average on the first place;
                }
                else                                    //*** Individual Average Choice mode set
                {
                    // this case is possible only in Local task, so mainPtns is always sorted and shrinked
                    // create and fill the array of sum counters of frequencies from shaked Patterns;
                    // it is 'sinchronised' with shakedPtns, or the same order as a shakedPtns
                    Average.SumCounter[] SCounters = new Average.SumCounter[Words.Count];
                    maxFreqNumberTmp = -1;      // disable max frequency return
                    for (int j = 0; j < shakeCnt; j++)
                    {
                        shakedPtns.Init(worker,
                                        Sequence.Shake(startIndex, length, worker, shakeSteps),
                                        0, strictLength, strictFreqs, loopsPerStep, ref maxFreqNumberTmp, false, isDrawPlot
                                        );
                        for (i = 0; i < Words.Count; i++)
                        {
                            //if (shakedPtns[i].Freq > 0)
                            //    SCounters[i] += shakedPtns[i].Freq;
                            if (shakedPtns[i] > 0)
                            {
                                SCounters[i] += shakedPtns[i];
                            }
                        }
                    }
                    // set the average of frequency and SD for each Pattern in the initial collection
                    avrgs = new Average[mainPtns.Count];
                    for (i = 0; i < mainPtns.Count; i++)   // each initial pattern with unzero Frequency...
                    {
                        avrgs[i] = new Average(SCounters[i]);
                    }
                }
            }
            return(mainPtns);
        }
Beispiel #2
0
        /// <summary>Creates and calculate an instance of ScanWindows class.</summary>
        /// <param name="worker">BackgroundWorker in wich Processing is run.</param>
        /// <param name="prgBar">ProgressBar to modify its Maximum.</param>
        /// <param name="shakeCnt">Number of shakes.</param>
        /// <param name="winStartLenght">Initial length of current scanned window.</param>
        /// <param name="winStopLenght">Final length of of current scanned window.</param>
        /// <param name="winIncr">Incriment of length of current scanned window.</param>
        /// <param name="winShift">Shift of current scanned window.</param>
        /// <param name="scanCnt">Nnumber of scanned window.</param>
        /// <returns>The sorted ScanWindow's collection.</returns>
        public static ScanWindows GetTreatedWindows(
            System.ComponentModel.BackgroundWorker worker,
            ProgressBar prgBar,
            short shakeCnt,
            short winStartLenght, short winStopLenght, short winIncr, short winShift,
            int scanCnt)
        {
            int j, length, startInd = 0, maxFreqInd, maxFreqNumber = 0;
            int winCnt       = (winStopLenght - winStartLenght) / winIncr + 1; // the amount of windows from start to stop lengths
            int loopsPerStep = scanCnt * winCnt / prgBar.Maximum;              // ProgressBar.DefaultMaximum;

            if (loopsPerStep < 10)                                             // modify Maximum in case of appreciable cumulative error
            {
                prgBar.Maximum = scanCnt * winCnt / loopsPerStep;
            }
            float      F, maxF;
            Patterns   ptns;                                       // threated patterns within inner loop
            ScanWindow sWin;                                       // scanWindow with max Frequency

            int[]       maxFreqNumbers = new int[winCnt];          // array for keeping maxFreqNumber within inner loop
            Patterns[]  ptnsArray      = new Patterns[winCnt];     // array for keeping Patterns within inner loop
            Average[]   avrgs          = new Average[1];           // single Average from threated patterns
            ScanWindows sWins          = new ScanWindows(winCnt);  // ScanWindows within inner loop
            ScanWindows sWinsRes       = new ScanWindows(scanCnt); // result ScanWindows

            OnPrgBarReseat();
            for (int i = 0; i < scanCnt; i++)
            {
                length = winStartLenght;
                // choose patterns with max F within inner loop (among increasing subsequenses)
                for (maxF = 0, maxFreqInd = 0, j = 0; j < winCnt; length += winIncr, j++)
                {
                    ptnsArray[j] = ptns = Patterns.GetTreatedPatterns(
                        worker, startInd, length, shakeCnt,
                        ref avrgs,
                        ref maxFreqNumber, 0, false);
                    // ptns is unsorted
                    maxFreqNumbers[j] = maxFreqNumber;
                    sWins[j]          = new ScanWindow(
                        startInd, length, maxFreqNumber,
                        F = avrgs[0].F(ptns[maxFreqNumber]),
                        0);
                    // keep index of maxF element to avoid sorting by F at the end
                    if (F > maxF)
                    {
                        maxF = F; maxFreqInd = j;
                    }
                    // increase progress bar
                    OnPrgBarIncreased(worker, loopsPerStep);
                }
                // calculate CV for sWin with max F
                maxFreqNumber = maxFreqNumbers[maxFreqInd];
                sWin          = sWins[maxFreqInd];
                sWin.CV       = ptnsArray[maxFreqInd].
                                GetSimilars(maxFreqNumber).
                                GetCV(sWin.StartIndex, sWin.Length, maxFreqNumber);

                // save sWin with max F
                sWinsRes[i] = sWin;
                startInd   += winShift;

                // corrects startInd on the last step
                if (startInd + winStopLenght > Sequence.Length)
                {
                    startInd = Sequence.Length - winStopLenght;
                }
            }
            sWinsRes.Sort();
            return(sWinsRes);
        }