def TestDiscretize():
    data = mat2csv()
    sc = SymbolicClustering(segmentLength=10,
                            paaSize=2,
                            alphabetSize=10,
                            upperBound=3000,
                            lowerBound=0)
    for i in range(0, 10):
        segments = sc.discretize(data[i][0][0:90, 6])
        sc.printSegments()
def TestGrammarInduction():
    data = mat2csv()
    sc = SymbolicClustering(segmentLength=10,
                            paaSize=2,
                            alphabetSize=10,
                            upperBound=3000,
                            lowerBound=0)
    for i in range(0, 10):
        segments = sc.discretize(data[i][0][0:90, 6])
        #sc.printSegments()
        sc.grammar_induction(segments)
        print(sc.grammar.print_grammar())
        print('--------------------------------------')
def TestClustersGeneration():
    data = mat2csv()
    sc = SymbolicClustering(segmentLength=10,
                            paaSize=2,
                            alphabetSize=10,
                            upperBound=3000,
                            lowerBound=0)
    for i in range(0, 10):
        segments = sc.discretize(data[i][0][0:90, 6])
        sc.grammar_induction(segments)
    frequencyMatrix = sc.get_frequency_matrix()
    windows = sc.cut_window(frequencyMatrix)
    sc.generateInitialClusters(0, windows)
    for window in windows:
        window.printClusters()
def TestCutWindow():
    data = mat2csv()
    sc = SymbolicClustering(segmentLength=10,
                            paaSize=2,
                            alphabetSize=10,
                            upperBound=3000,
                            lowerBound=0)
    for i in range(0, 10):
        segments = sc.discretize(data[i][0][0:90, 6])
        sc.grammar_induction(segments)
        frequencyMatrix = sc.get_frequency_matrix()
        windows = sc.cut_window(frequencyMatrix)
        print('--------------------------------------')
        for window in windows:
            print(str(window.startIndex) + ',' + str(window.endIndex))
def TestGetFrequencyMatrix():
    data = mat2csv()
    sc = SymbolicClustering(segmentLength=10,
                            paaSize=2,
                            alphabetSize=10,
                            upperBound=3000,
                            lowerBound=0)
    for i in range(0, 10):
        segments = sc.discretize(data[i][0][0:90, 6])
        sc.grammar_induction(segments)
        print(sc.grammar.print_grammar())
        frequencyMatrix = sc.get_frequency_matrix()
        print(frequencyMatrix)
        print('--------------------------------------')
    def patternGenerate(self):
        """
        @description  :
        generate patterns from the timeseries, where we will use the symbolic clustering method 
        to generate windows in each dimension. The pattern is defined as the center of clusters 
        from each window.
        ---------
        @param  :
        -------
        @Returns  : listPatterns
        -------
        """

        print('正在生成patterns...')
        numDim = len(self.dataSet)
        listPatterns = []
        for i in range(0, numDim):
            print(i / numDim)
            timeSeries = self.dataSet[i]
            seriesNum = len(timeSeries)
            if self.pStep > seriesNum:
                raise PStepIsNotValid()
            sc = SymbolicClustering(segmentLength=self.segmentLength,
                                    paaSize=self.paaSize,
                                    alphabetSize=self.alphabetSize,
                                    upperBound=self.listBounds[i][1],
                                    lowerBound=self.listBounds[i][0])
            for j in range(0, self.pStep):
                # print(timeSeries[j])
                segments = sc.discretize(timeSeries[j])
                sc.grammar_induction(segments)
            frequencyMatrix = sc.get_frequency_matrix()
            windows = sc.cut_window(frequencyMatrix)
            windows = sc.generateInitialClusters(0, windows)
            # for window in windows:
            #     window.printClustersInDetail()
            dynamicStep = self.pStep
            sc = SymbolicClustering(segmentLength=self.segmentLength,
                                    paaSize=self.paaSize,
                                    alphabetSize=self.alphabetSize,
                                    upperBound=self.listBounds[i][1],
                                    lowerBound=self.listBounds[i][0])
            while dynamicStep < seriesNum:
                if len(timeSeries[dynamicStep]) < self.seriesLen:
                    # for window in windows:
                    #     window.printClustersInDetail()
                    timeSeries[dynamicStep] = timeSeries[dynamicStep].tolist()
                    for j in range(len(timeSeries[dynamicStep]),
                                   self.seriesLen):
                        timeSeries[dynamicStep].append(0.0)
                    print(timeSeries[dynamicStep])

                DynamicClustering.dynamicClustering(timeSeries[dynamicStep],
                                                    windows, dynamicStep,
                                                    self.segmentLength)
                segments = sc.discretize(timeSeries[dynamicStep])
                sc.grammar_induction(segments)
                if (dynamicStep + 1) % self.pStep == 0:
                    frequencyMatrix = sc.get_frequency_matrix()
                    scwindows = sc.cut_window(frequencyMatrix)
                    # scwindows = sc.generateInitialClusters(dynamicStep + 1 - self.pStep, scwindows)
                    windows = ParametersSelfTuning.windowSplitting(
                        windows, scwindows)
                    windows = ParametersSelfTuning.windowCombination(windows)
                    sc = SymbolicClustering(segmentLength=self.segmentLength,
                                            paaSize=self.paaSize,
                                            alphabetSize=self.alphabetSize,
                                            upperBound=self.listBounds[i][1],
                                            lowerBound=self.listBounds[i][0])
                dynamicStep += 1

            listPatterns.append(windows)
        return listPatterns
def TestWindowCombination():
    data = mat2csv()
    sc = SymbolicClustering(segmentLength=10,
                            paaSize=2,
                            alphabetSize=10,
                            upperBound=3000,
                            lowerBound=0)
    for i in range(0, 10):
        segments = sc.discretize(data[i][0][0:90, 6])
        sc.grammar_induction(segments)
    frequencyMatrix = sc.get_frequency_matrix()
    windows = sc.cut_window(frequencyMatrix)
    windows = sc.generateInitialClusters(0, windows)
    print('--------0-10 by now:-------------')
    for window in windows:
        window.printClusters()
    sc = SymbolicClustering(segmentLength=10,
                            paaSize=2,
                            alphabetSize=10,
                            upperBound=3000,
                            lowerBound=0)
    for i in range(10, 20):
        segments = sc.discretize(data[i][0][0:90, 6])
        sc.grammar_induction(segments)
        DynamicClustering.dynamicClustering(data[i][0][0:90, 6], windows, i,
                                            10)
    frequencyMatrix = sc.get_frequency_matrix()
    scwindows = sc.cut_window(frequencyMatrix)
    scwindows = sc.generateInitialClusters(10, scwindows)
    print('--------10-20 by sc:-------------')
    for window in scwindows:
        window.printClusters()
    print('--------10-20 by now:-------------')
    for window in windows:
        window.printClusters()
    windows = ParametersSelfTuning.windowSplitting(windows, scwindows)
    print('--------10-20 by now split:-------------')
    for window in windows:
        window.printClusters()
    windows = ParametersSelfTuning.windowCombination(windows)
    print('--------10-20 by now combination:-------------')
    for window in windows:
        window.printClustersInDetail()
    print('--------0-20 original:-------------')
    for i in range(0, 20):
        print(data[i][0][0:90, 6])