Calculate Statistics Example

Example A.14. Discrete Limited enable/disable

table u'tmp_exact_discrete':
    value
    a
    a
    b
    c
    c
    c
    c
    c
    d
    d
    d
    d
    d
    d

# create pd object
pd = PhysicalData(u'tmp_exact_discrete')
save(u'pd', pd)

# create calculate statistics object
cst=CalculateStatisticsTask()
# set physical data
cst.setPhysicalDataName(u'pd')
# statistics will be calculated for 'value' attribute 
cst.getStatisticsSettings().addAttribute(u'value')
cst.getStatisticsSettings().setMaxValuesCount(3)

save(u'cst', cst)
execute(u'cst')

# load discrete statistics for 'value' attribute 
discVal = load(u'pd').getAttribute(u'value').getAttributeStatistics().getDiscreteStatistics()

# print value and frequency 
print u'With default value of exactDiscreteLimited (off):'
strFormat = u'%-30s%-4s'
print strFormat % (u'Value', u'Frequency')
for item in discVal.getDiscreteValues():
    print strFormat % (item,discVal.getValueCount(item))



# And now set the flag for exact evaluation
cst.getStatisticsSettings().setExactLimitedDiscrete(1)

save(u'cst', cst)
execute(u'cst')

# load discrete statistics for 'value' attribute 
discVal = load(u'pd').getAttribute(u'value').getAttributeStatistics().getDiscreteStatistics()

# print value and frequency 
print u'With exactDiscreteLimited enabled:'
print strFormat % (u'Value', u'Frequency')
for item in discVal.getDiscreteValues():
    print strFormat % (item,discVal.getValueCount(item))

Output:

With default value of exactDiscreteLimited (off):
Value                         Frequency
b                             1   
c                             5   
a                             2   
With exactDiscreteLimited enabled:
Value                         Frequency
d                             6   
c                             5   
a                             2