Example #1
0
 def getRnSData(self, fileName, SGwd_length, SGpolyOrd):
     t = []
     v = []
     pltData = []
     data, times = fT.readCSV(fileName)
     for i in range(len(data)):
         t.append(i)
         v.append(float(data[i]))
     sgf = savgol_filter(data, SGwd_length, SGpolyOrd)
     R2_RnS = round(mthT.coeff_of_determination(np.array(data), sgf), 3)
     #print(times)
     pltData += [
         go.Scatter(
             x=times,  # assign x as the dataframe column 'x'
             y=v,
             name='Raw',
             mode='lines',
             marker=dict(size=5, color='rgba(0,0,0,0.8)'),
             line=dict(width=8, )
             # =============================================================================
             #                         ),
             #             go.Scatter(
             #                 x=times, # assign x as the dataframe column 'x'
             #                 y=sgf,
             #                 name='S-Gf: R2:'+str(R2_RnS),
             #                 mode='line',
             #                 marker=dict(
             #                         size=5,
             #                         color='rgba(0,0,0,0.0)'),
             #                         line=dict(
             #                                 width=3,
             #                                 color='rgba(0,0,0,0.0)')
             # =============================================================================
         )
     ]
     return sgf, pltData
Example #2
0
    # =============================================================================
    #     orgrawData,times=fT.readCSV(mypath+fileName[testList[testTime-1]])
    #
    #     print('Compressing Data And Dcompressing Data to Compared')
    #     coef_ary1,decompData1=CA.CompressAlg().Seg2poly(mypath+fileName[testList[testTime-1]])
    # =============================================================================
    #print(decompData1)
    n = 0
    testTime = 0
    coeffAry = []
    deCompData = []
    aYearCoeffAry = []
    aYeardeCompData = []
    for testTime in range(30):
        orgrawData, times = fT.readCSV(mypath + fileName[testList[testTime]])
        coef_ary1, decompData1 = CA.CompressAlg().Seg2poly(
            mypath + fileName[testList[testTime]])
        coeffAry.extend(coef_ary1)
        deCompData.extend(decompData1)
    print(len(deCompData))
    for m in range(12):
        aYearCoeffAry.extend(coeffAry)
        aYeardeCompData.extend(deCompData)
        print(len(aYeardeCompData))
    print(len(aYeardeCompData))

    for testTime in range(5):
        orgrawData, times = fT.readCSV(mypath + fileName[testList[testTime]])
        coef_ary1, decompData1 = CA.CompressAlg().Seg2poly(
            mypath + fileName[testList[testTime]])
Example #3
0
        ltMean=[]
# =============================================================================
#         spList=rSp.Sampling(len(fileName),spNum,OldSplist)##簡單隨機抽樣樣品 list
#         OldSplist.append(spList)
# ============================================================================= 
        ## 讀取RawData Ver 的1000個樣本
        ## 改動了readOldList 中的檔案名稱
        #spList=rdList.readOldList(count-1)
        
        #print(len(fileName))
#=========== for start ===============
        for i in test:#range(len(fileName))  全部數據
            global pltData
            sensorType=fileName[i][6:]  
            if(sensorType=='T1' or sensorType=='T2' or sensorType=='H1' or sensorType=='H2' ):
                data,times=fT.readCSV(mypath,fileName[i])
                plotTime=[]
                for ts in times:
                    plotTime.append(datetime.datetime.fromtimestamp(time.mktime(ts.timetuple())))
                t=[]
                v=[]
                pltData=[]
                tot_segNum=0
                stop=False        
                ssrType=sensorType[0]
                for x in range(len(data)):
                    t.append(x)
                    v.append(float(data[x]))

                #print(times)
                pltData += [
Example #4
0
    def Seg2poly(self,
                 fileName,
                 SGwd_length=11,
                 SGpolyOrd=5,
                 min_r2=0.99,
                 polyIndex=5,
                 window_size_percent=0.05,
                 min_interval_percent=0.0,
                 isplot=False):
        startPt = 0
        endPt = 0
        delta = 0
        timer = 0
        t = []
        v = []

        sgf, pltData = self.getRnSData(fileName, SGwd_length, SGpolyOrd)
        data, times = fT.readCSV(fileName)
        for i in range(len(data)):
            t.append(i)
            v.append(float(data[i]))
        tStart = time.time()  #計時開始
        #plt.plot(t,v,'-')
        #plt.plot(t,sgf,'-')
        if (len(data) > 100):
            pass
        else:
            window_size_percent = 1
        #print('wdSize:',window_size_percent)
        interval = int(len(t) * window_size_percent) - 1
        start = []
        end = []
        #intervalAry=[]
        coef_ary = []
        coeff_5 = []
        coeff_4 = []
        coeff_3 = []
        coeff_2 = []
        coeff_1 = []
        coeff_0 = []

        decompData = []
        #first_inl=0
        R2_PnS = []
        R2_PnR = []
        Dlta = []
        Maxr2SGF = 0.0
        Maxr2RW = 0.0
        MaxEnd = 0
        MaxCoeff = []
        MaxYs = []
        rsqSGF = 0
        rsqRW = 0
        ys_line = []
        DeltaCnt = 0
        #===condition====
        #max_slope=2 #  反應差
        min_time_interval = int(len(t) * min_interval_percent)
        c = 1

        while (startPt < (len(t))):
            #MaxStart=0

            islimit = False
            # =============================================================================
            #
            #             if ((interval-startPt)<=min_time_interval) :
            #                 interval=interval+int((len(t)/10))
            #
            # =============================================================================
            for i in range(interval, startPt - 1, -1):

                if ((i - startPt) > min_time_interval):
                    endPt = i + 1
                    delta = endPt - startPt
                    coeff, ys_line, rsqSGF, rsqRW = mthT.polyLine(
                        startPt, endPt, polyIndex, t, sgf, data)
                    if (rsqRW >= Maxr2SGF):
                        Maxr2SGF = rsqRW
                        MaxYs = ys_line
                        Maxr2RW = rsqRW
                        #MaxStart=startPt
                        MaxEnd = endPt
                        MaxCoeff = coeff
                    break
                elif ((i - startPt) <= min_time_interval + 1):
                    if (Maxr2SGF == 0):
                        endPt = len(t)
                        delta = endPt - startPt
                        coeff, ys_line, rsqSGF, rsqRW = mthT.polyLine(
                            startPt, endPt, polyIndex, t, sgf, data)
                        islimit = True
                    else:
                        rsqRW = Maxr2SGF
                        endPt = MaxEnd
                        coeff = MaxCoeff
                        delta = endPt - startPt
                        rsqRW = Maxr2RW
                        ys_line = MaxYs
                        delta = endPt - startPt
                        islimit = True
                    break
            if (rsqRW >= min_r2) or islimit:
                #====coeff[0] = A  coeff[1] = B coeff[2] = C; Ax^2+Bx+C
                #start.append(time[startPt])
                #end.append(time[endPt])
                # =============================================================================
                #             if(rsqTT<min_r2):
                #                 print(c)
                #                 print(Maxr2SGF)
                #                 print(islimit)
                #                 print(i)
                #                 print(startPt)
                # =============================================================================
                start.append(times[startPt])
                if (endPt != len(times)):
                    end.append(times[endPt])
                    #print(len(times),':',endPt)
                else:
                    end.append(times[endPt - 1])
                    #print(len(times),':',endPt)
                #coef_ary.append(coeff)
                #print(np.poly1d(coeff))

                coeff_5.append(float(coeff[0]))
                coeff_4.append(float(coeff[1]))
                coeff_3.append(float(coeff[2]))
                coeff_2.append(float(coeff[3]))
                coeff_1.append(float(coeff[4]))
                coeff_0.append(float(coeff[5]))
                coef_ary.extend(coeff)
                decompData.extend(ys_line)
                #print()
                Dlta.append(delta)

                DeltaCnt = DeltaCnt + endPt - startPt
                # print(len(decompData),'Delta=',DeltaCnt)

                pltData += [
                    go.Scatter(
                        x=times[startPt:
                                endPt],  # assign x as the dataframe column 'x'
                        y=ys_line,
                        mode='lines',
                        name='',
                        #name=str(c)+'. R: '+str(rsqRW),
                        marker=dict(size=5, color='rgba(255,0,0,1)'),
                        line=dict(width=3, ))
                ]
                c += 1
                #print(startPt,"-",endPt)
                R2_PnR.append(round(rsqRW, 3))
                R2_PnS.append(round(rsqSGF, 3))
                startPt = endPt
                interval = startPt + int((len(t) * window_size_percent)) - 1

                # =============================================================================
                if (interval >= len(t)):
                    interval = len(t) - 1

                #intervalAry.append(interval)
                MaxCoeff = []
                MaxYs = []
                Maxr2SGF = 0.0
                MaxEnd = interval


# =============================================================================
#                 break
# =============================================================================
            else:
                interval -= 1

        tEnd = time.time()  #計時結束
        timer = tEnd - tStart
        #=================每一天壓縮後數據==================================
        #==each day polynorimal data
        dtPolyData = {
            'start': start,
            'end': end,
            'Delta': Dlta,
            'coeff_5': coeff_5,
            'coeff_4': coeff_4,
            'coeff_3': coeff_3,
            'coeff_2': coeff_2,
            'coeff_1': coeff_1,
            'coeff_0': coeff_0,
            'R2_Raw': R2_PnR,
        }
        headerPD = [
            'start', 'end', 'Delta', 'coeff_5', 'coeff_4', 'coeff_3',
            'coeff_2', 'coeff_1', 'coeff_0'
        ]
        #輸出沒有了r2 值
        dfpolyDtlData = pd.DataFrame(dtPolyData, columns=headerPD, index=None)
        SnrCsvfileName = fileName + "_DetailData.csv"
        dfpolyDtlData.to_csv(SnrCsvfileName, mode='w', index=None)
        print('總結:times=', len(times), 'deCompDaya=', len(decompData))
        print('Compressing Time:', timer)
        pltData += [
            go.Scatter(
                x=times,  # assign x as the dataframe column 'x'
                y=decompData,
                mode='lines',
                name='CHEB',
                marker=dict(size=5, color='rgba(255,0,0,0.0)'))
        ]
        pltT.PlotLy(t, window_size_percent, min_interval_percent, polyIndex,
                    fileName, pltData, isplot)
        return coef_ary, decompData