File: streaming_statistics_pyqt.py

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (127 lines) | stat: -rw-r--r-- 3,612 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
from __future__ import print_function
from vtk import *
import os.path
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from vtk.util.misc import vtkGetDataRoot
VTK_DATA_ROOT = vtkGetDataRoot()


data_dir = VTK_DATA_ROOT + "/Data/Infovis/SQLite/"
if not os.path.exists(data_dir):
  data_dir = VTK_DATA_ROOT + "/Data/Infovis/SQLite/"
if not os.path.exists(data_dir):
  data_dir = VTK_DATA_ROOT + "/Data/Infovis/SQLite/"
sqlite_file = data_dir + "temperatures.db"

# I'm sure there's a better way then these global vars
currentRow = 0;
numberOfRows = 1;
done = False;

psuedoStreamingData = vtkProgrammableFilter()

def streamData():
    global done
    global currentRow

    input = psuedoStreamingData.GetInput()
    output = psuedoStreamingData.GetOutput()

    # Copy just the columns names/types
    output.GetRowData().CopyStructure(input.GetRowData())

    # Loop through all the input data and grab the next bunch of rows
    startRow = currentRow
    endRow = startRow + numberOfRows
    if (endRow >= input.GetNumberOfRows()):
        endRow = input.GetNumberOfRows()
        done = True;
    print("streaming: ", startRow, "-", endRow)

    for i in range(startRow, endRow):
        output.InsertNextRow(input.GetRow(i))

    currentRow = endRow;

psuedoStreamingData.SetExecuteMethod(streamData)

class Timer(QObject):
    def __init__(self, parent=None):
        super(Timer, self).__init__(parent)

        # Setup the data streaming timer
        self.timer = QTimer()
        QObject.connect(self.timer, SIGNAL("timeout()"), self.update)
        self.timer.start(100)

    def update(self):
        if (done):
            quit();
        psuedoStreamingData.Modified() # Is there a way to avoid this?
        psuedoStreamingData.GetExecutive().Push()
        printStats()


def printStats():
    sStats = ss.GetOutputDataObject( 1 )
    sPrimary = sStats.GetBlock( 0 )
    sDerived = sStats.GetBlock( 1 )
    sPrimary.Dump( 15 )
    sDerived.Dump( 15 )


if __name__ == "__main__":
    """ Main entry point of this python script """

    # Set up streaming executive
    streamingExec = vtkThreadedStreamingPipeline()
    vtkAlgorithm.SetDefaultExecutivePrototype(streamingExec)
    streamingExec.FastDelete()
    vtkThreadedStreamingPipeline.SetAutoPropagatePush(True)

    # Pull the table from the database
    databaseToTable = vtkSQLDatabaseTableSource()
    databaseToTable.SetURL("sqlite://" + sqlite_file)
    databaseToTable.SetQuery("select * from main_tbl")

    # Hook up the database to the streaming data filter
    psuedoStreamingData.SetInputConnection(databaseToTable.GetOutputPort())


    # Calculate offline(non-streaming) descriptive statistics
    print("# Calculate offline descriptive statistics:")
    ds = vtkDescriptiveStatistics()
    ds.SetInputConnection(databaseToTable.GetOutputPort())
    ds.AddColumn("Temp1")
    ds.AddColumn("Temp2")
    ds.Update()

    dStats = ds.GetOutputDataObject( 1 )
    dPrimary = dStats.GetBlock( 0 )
    dDerived = dStats.GetBlock( 1 )
    dPrimary.Dump( 15 )
    dDerived.Dump( 15 )


    # Stats filter to place 'into' the streaming filter
    inter = vtkDescriptiveStatistics()
    inter.AddColumn("Temp1")
    inter.AddColumn("Temp2")




    # Calculate online(streaming) descriptive statistics
    print("# Calculate online descriptive statistics:")
    ss = vtkStreamingStatistics()
    ss.SetStatisticsAlgorithm(inter)
    ss.SetInputConnection(psuedoStreamingData.GetOutputPort())

    # Spin up the timer
    app = QApplication(sys.argv)
    stream = Timer()

    sys.exit(app.exec_())