Package: on-the-fly-stats
On-the-fly StatisticsThis is a python module that provides a way to run standard deviation on the fly i.e. without storing data.Sample codesee sample.py for a full examplefromon_the_fly_statsimportOTFStatsstats=OTFStats()# create an instance of the std deviationstats.create_stddev('some_tag')# add some values to itstats.update_stddev('some_tag',1)stats.update_stddev('some_tag',2)stats.update_stddev('some_tag',3)# have other instances of itstats.update_stddev('other_tag',0.1)stats.update_stddev('other_tag',0.2)stats.update_stddev('other_tag',0.3)# show the current data for a specific instanceprint(stats.stddev['some_tag'].stddev)print(stats.stddev['some_tag'].variance)print(stats.stddev['some_tag'].mean)print(stats.stddev['some_tag'].num_elements)# show the current data for a specific instanceprint(stats.stddev['other_tag'].stddev)# some a summary reportstats.set_report_writer(print)stats.report_minimal()# skip the headers for even less datastats.report_minimal(headers=False)Min/MaxHolds the minimum and maximum of the values provided so farimportrandomfromon_the_fly_statsimportOTFStatsstats=OTFStats()stats.create_min_max('minmax1')for_inrange(10):stats.update_min_max('minmax1',random.randint(0,10))print(stats.min_max['minmax1'].minimum)print(stats.min_max['minmax1'].maximum)print(stats.min_max['minmax1'].num_elements)AverageHolds the average of the values provided so farimportrandomfromon_the_fly_statsimportOTFStatsstats=OTFStats()stats.create_average('avg1')stats.update_average('avg1',random.uniform(0,10))stats.update_average('avg1',random.uniform(0,10))print(stats.average['avg1'].average)print(stats.average['avg1'].anum_elementss)Standard DeviationHolds the standard deviation, mean and variance of the values provided so farimportrandomfromon_the_fly_statsimportOTFStatsstats=OTFStats()stats.create_stddev('sd1')stats.update_stddev('sd1',random.random())stats.update_stddev('sd1',random.random())stats.update_stddev('sd1',random.random())print(stats.stddev['sd1'].stddev)print(stats.stddev['sd1'].mean)print(stats.stddev['sd1'].variance)print(stats.stddev['sd1'].num_elementss)CountersHolds counters of the values provided so farfromon_the_fly_statsimportOTFStatsstats=OTFStats()# automatically create a counterstats.inc_counter('counter1')stats.inc_counter('counter1')# should be 2stats.dec_counter('counter2')# should be -1stats.inc_counter('counter3')stats.dec_counter('counter3')# should be 0print(stats.counters['counter1'].count)print(stats.counters['counter2'].count)print(stats.counters['counter3'].count)ReportsThere are two builtin reports:a minimal reporta full reportTo use the builtin reports a function that writes a single line must be provided.
The simplest is to use print().stats.set_report_writer(print)stats.report_minimal()stats.report()Minimal report outputThe minimal report shows each stat kept and minimal data with minimal formattingMin/Max:
              0               7 minmax1
       0.019647        0.825751 minmax2
Average:
            4.495221 avg1
Stddev
     111.109474 stddev1
       0.300395 stddev2
Counters:
              2 counter1
             -1 counter2
              0 counter3Full report outputThe full report has more formatting and layout---- Stats:

                 Min             Max statistic
     --------------- --------------- ------------------------------------------------------------------
                   0               7 minmax1
            0.019647        0.825751 minmax2
     >>> end of min/max

             Average statistic
     --------------- ------------------------------------------------------------------
            4.495221 avg1
     >>> end of Averages

              StdDev statistic
     --------------- ------------------------------------------------------------------
          111.109474 stddev1
            0.300395 stddev2
     >>> end of StdDev

               Total statistic
     --------------- ------------------------------------------------------------------
                   2 counter1
                  -1 counter2
                   0 counter3
     >>> end of counters