Last active
August 29, 2015 14:16
-
-
Save mattgrogan/30b61f336ffd836fabea to your computer and use it in GitHub Desktop.
Simpy Monitor Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
class Monitor(object): | |
""" Monitors hold data collected during the simulation """ | |
def __init__(self, name): | |
""" Initialize the monitor """ | |
self.name = name | |
self._data = {} | |
def _key(self, index, group=None): | |
""" Keys are stored in tuples for the dictionary """ | |
if group: | |
metric_name = self.name + "_" + group | |
key = (metric_name, index) | |
else: | |
key = (self.name, index) | |
return key | |
def observe(self, index, group, value=None): | |
""" Observe a quantity """ | |
if value is None: | |
# No group is specified, swap the arguments. This makes it easy | |
# to call monitor.observe(1, 10) if there are no sub-groupings | |
group, value = None, group | |
key = self._key(index, group) | |
if key not in self._data: | |
# Only save if we have no observed value for this key | |
self._data[key] = value | |
else: | |
# Otherwise raise an exception | |
msg = "Attempted to ovewrite {} at {} with {}".format( | |
(self.name, key, value)) | |
raise Exception(msg) | |
def increment(self, index, group=None, by=1): | |
""" Increment the key by one (or another amount) | |
Supported calls: | |
(1) m.increment(1) => increment key 1 on self by 1 | |
(2) m.increment(1, "step") => increment step monitor key 1 by 1 | |
(3) m.increment(1, by=10) => increment key 1 on self by 10 | |
(4) m.increment(1, "step", by=10) => increment step key 1 by 10 | |
""" | |
key = self._key(index, group) | |
if key not in self._data: | |
self._data[key] = by | |
else: | |
self._data[key] += by | |
def get_value(self, index, group=None): | |
""" Return the value for a key """ | |
key = self._key(index, group) | |
return self._data[key] | |
def data(self): | |
""" Return a dictionary with the data """ | |
return self._data | |
def df(self): | |
""" Return the data as a pandas dataframe """ | |
values = self._data.values() | |
keys = self._data.keys() | |
index = pd.MultiIndex.from_tuples(keys, names=["metric", "idx"]) | |
df = pd.DataFrame(values, index=index) | |
df.columns = ["value"] | |
df = df.unstack(0) | |
df.columns = df.columns.droplevel(0) | |
return(df) | |
m = Monitor("My_Monitor") | |
m.increment(1, "Step1") # Step 1 happens at time 1 | |
m.increment(1, "Step1") # Step 1 happens at time 1 | |
m.increment(2, "Step1") # Step 1 happens at time 2 | |
m.observe(1, "Queue1", 50) # 50 items in queue1 at time 1 | |
m.observe(2, "Queue1", 48) # 48 items in queue1 at time 2 | |
m.observe(3, "Queue1", 48) # 48 items in queue1 at time 3 | |
m.df() # Obtain the data as a pandas dataframe | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment