Metrics for Machine Learning
pip install avilabs-ml-metrics==1.1.0
A simple metric library with a built-in implementation that logs metrics to SQLite databases. It is possible to implement other backing datastores as well.
pip install avilabs-ml-metrics
db = './metrics.db'
metric = SqliteMetric(db, name='fuel_gauge', labels={'model', 'trip'})
This will create a table called fuel_gauge
in metrics.db
if it doesn't exist. If it does that will be used. The table will have the following fields:
model
which is a text fieldtrip
which is a text fieldtimestamp
which is a real fieldvalue
which is a real fieldmetric.log(model='toyota', trip='short', value=1.2)
Will add a row in the fuel_gauge
table. The timestamp will be added automatically. It is just datetime.now().timestamp()
.
logs = metric.logs(model='toyota')
for row in logs:
for fld in row.keys():
print(fld, row[fld])
It is possible that a bunch of logs represent the value of the metric at a single instant in time. Ideally all these logs will have the same timestamp. Howeverm, writing them one by one will result in each log having a different timestamp. To prevent this from happening and ensuring that all subsequent logs should have the same timestamp, use snapshots
.
metric.start_snapshot()
for _ in range(10):
metric.log(model='toyota', trip='short', value=val)
metric.end_snapshot()
It is possible to query snapshots instead of individual logs.
snapshots = metric.snapshots(start=1550554038.80172, end=1550554038.80265)
for snapshot in snapshots:
for row in snapshot:
for fld in row.keys():
print(fld, row[fld])
The snapshots
method will return an iterator of snapshots, each snapshot is a sequence of logs with the same timestamp.
See the examples directory for more usage.