1#!/usr/bin/env python
2from vtk import *
3import os.path
4
5# Set database parameters
6data_dir = "../../../../VTKData/Data/Infovis/SQLite/"
7if not os.path.exists( data_dir):
8  data_dir = "../../../../../VTKData/Data/Infovis/SQLite/"
9if not os.path.exists( data_dir):
10  data_dir = "../../../../../../VTKData/Data/Infovis/SQLite/"
11sqlite_file = data_dir + "temperatures.db"
12databaseToTable = vtkSQLDatabaseTableSource()
13databaseToTable.SetURL("sqlite://" + sqlite_file)
14
15# Pull the first data set from the database
16databaseToTable.SetQuery("select * from main_tbl where CompId==2")
17
18# Calculate primary descriptive statistics for first batch
19print "# Calculate primary model of descriptive statistics for first data set:"
20ds1 = vtkDescriptiveStatistics()
21ds1.AddInputConnection( databaseToTable.GetOutputPort() )
22ds1.AddColumn("Temp1")
23ds1.AddColumn("Temp2")
24ds1.SetLearnOption( 1 )
25ds1.SetDeriveOption( 0 )
26ds1.SetAssessOption( 0 )
27ds1.SetTestOption( 0 )
28ds1.Update()
29
30# Show primary descriptive statistics for first batch
31dStats1 = ds1.GetOutputDataObject( 1 )
32dPrimary1 = dStats1.GetBlock( 0 )
33dPrimary1.Dump( 15 )
34print
35
36# Pull the second data set from the database
37databaseToTable.SetQuery("select * from main_tbl where CompId==3")
38
39# Calculate primary descriptive statistics for second batch
40print "# Calculate primary model of descriptive statistics for second data set:"
41ds2 = vtkDescriptiveStatistics()
42ds2.AddInputConnection( databaseToTable.GetOutputPort() )
43ds2.AddColumn("Temp1")
44ds2.AddColumn("Temp2")
45ds2.SetLearnOption( 1 )
46ds2.SetDeriveOption( 0 )
47ds2.SetAssessOption( 0 )
48ds2.SetTestOption( 0 )
49ds2.Update()
50
51# Show primary descriptive statistics for second batch
52dStats2 = ds2.GetOutputDataObject( 1 )
53dPrimary2 = dStats2.GetBlock( 0 )
54dPrimary2.Dump( 15 )
55print
56
57# Finally aggregate both models to get a new primary model for the whole ensemble
58print "# Aggregate  both primary models:"
59collection = vtkDataObjectCollection()
60collection.AddItem( dStats1 )
61collection.AddItem( dStats2 )
62ds = vtkDescriptiveStatistics()
63aggregated = vtkMultiBlockDataSet()
64ds.Aggregate( collection, aggregated )
65dPrimary = aggregated.GetBlock( 0 )
66dPrimary.Dump( 15 )
67print
68
69# Calculate derived model for whole ensemble
70print "# Now calculating derived statistics for whole ensemble:"
71ds.SetInputData( 2, aggregated )
72ds.SetLearnOption( 0 )
73ds.SetDeriveOption( 1 )
74ds.SetAssessOption( 0 )
75ds.SetTestOption( 0 )
76ds.Update()
77dStats = ds.GetOutputDataObject( 1 )
78dDerived = dStats.GetBlock( 1 )
79dDerived.Dump( 15 )
80print
81
82# Pull entire data set from the database
83databaseToTable.SetQuery("select * from main_tbl")
84
85# Verify with calculation for whole ensemble at once
86print "# Finally verifying by directly calculating statistics for whole ensemble:"
87ds0 = vtkDescriptiveStatistics()
88ds0.AddInputConnection( databaseToTable.GetOutputPort() )
89ds0.AddColumn("Temp1")
90ds0.AddColumn("Temp2")
91ds0.SetLearnOption( 1 )
92ds0.SetDeriveOption( 1 )
93ds0.SetAssessOption( 0 )
94ds0.SetTestOption( 0 )
95ds0.Update()
96
97# Show all descriptive statistics for whole ensemble
98dStats0 = ds0.GetOutputDataObject( 1 )
99dPrimary0 = dStats0.GetBlock( 0 )
100dPrimary0.Dump( 15 )
101dDerived0 = dStats0.GetBlock( 1 )
102dDerived0.Dump( 15 )
103print
104