DM_attributes_statistic.py
1 ## @package python.demo.DM_attributes_statistic
2 #
3 # Small example script showing how to access the attribute statistics
4 # of a datamanager
5 # (script works in python 2 and 3)
6 #
7 
8 from __future__ import print_function # print function syntax as in python 3
9 import random, sys
10 
11 from opals import pyDM
12 
13 
14 def DM_attributes_statistic(filename):
15  # pyDM.Datamanager.load parameter: filename(string), readOnly(bool) threadSafety(bool)
16  dm = pyDM.Datamanager.load(filename, True, False)
17  if not dm:
18  print("Unable to open ODM '" + filename + "'")
19  sys.exit(1)
20 
21  # get point index object
22  pi = dm.getPointIndex()
23 
24  print("ODM contains ", dm.sizePoint(), "points")
25  print("Estimated point density %.3f" % pi.estimatePointDensity(), "[points/m^2]")
26 
27  # get attribute statistic object
28  stats = pi.getAddInfoStatistics()
29 
30  print("Attribute statistics:")
31  print("Name\t type\t count\t min\t max\t mean\t sigma")
32  for i in range(0, stats.columns()):
33  colname = stats.min().name(i)
34  coltype = stats.min().type(i)
35  # output colname and coltype
36  print(colname + "\t" + str(coltype) + "\t", end="")
37  # output statistics
38  if stats.count(i) > 0:
39  print("%5d" % stats.count(i), "%.3f" % stats.min().get(i), "%.3f" % stats.max().get(i),
40  "%.3f" % stats.mean(i), "%.3f" % stats.sigma(i))
41  else:
42  print("%5d" % stats.count(i), "---", "---", "---", "---")
43 
44 
45 if len(sys.argv) == 1:
46  print("ODM parameter missing")
47  sys.exit(-1)
48 
49 DM_attributes_statistic(sys.argv[1])