Skip to content

Commit

Permalink
Adding explicit inventory root metrics and metrics (#100)
Browse files Browse the repository at this point in the history
* adding inv tables to root metrics list

* Added a few inventory metrics.  There are issues with compact form due to dict - i.e. cannot work with the compact inventory table using pandas without expanding it to the non-compact version.
  • Loading branch information
opotowsky authored and rwcarlsen committed May 3, 2016
1 parent 45ea2c1 commit 881db9d
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 4 deletions.
55 changes: 55 additions & 0 deletions cymetric/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,61 @@ def transaction_quantity(series):
del _transdeps, _transschema


# Explicit Inventory By Agent
_invdeps = [
('ExplicitInventory', ('SimId', 'AgentId', 'Time', 'InventoryName', 'NucId'),
'Quantity')
]

_invschema = [
('SimId', ts.UUID), ('AgentId', ts.INT),
('Time', ts.INT), ('InventoryName', ts.STRING),
('NucId', ts.INT), ('Quantity', ts.DOUBLE)
]

@metric(name='ExplicitInventoryByAgent', depends=_invdeps, schema=_invschema)
def explicit_inventory_by_agent(series):
"""The Inventory By Agent metric groups the inventories by Agent
(keeping all nuc information)
"""
inv_index = ['SimId', 'AgentId', 'Time', 'InventoryName', 'NucId']
inv = series[0]
inv = inv.groupby(level=inv_index).sum()
inv.name = 'Quantity'
rtn = inv.reset_index()
return rtn

del _invdeps, _invschema


# Explicit Inventory By Nuc
_invdeps = [
('ExplicitInventory', ('SimId', 'Time', 'InventoryName', 'NucId'),
'Quantity')
]

_invschema = [
('SimId', ts.UUID), ('Time', ts.INT),
('InventoryName', ts.STRING), ('NucId', ts.INT),
('Quantity', ts.DOUBLE)
]

@metric(name='ExplicitInventoryByNuc', depends=_invdeps, schema=_invschema)
def explicit_inventory_by_nuc(series):
"""The Inventory By Nuc metric groups the inventories by nuclide
and discards the agent information it is attached to (providing fuel
cycle-wide nuclide inventories)
"""
inv_index = ['SimId', 'Time', 'InventoryName', 'NucId']
inv = series[0]
inv = inv.groupby(level=inv_index).sum()
inv.name = 'Quantity'
rtn = inv.reset_index()
return rtn

del _invdeps, _invschema


# Electricity Generated [MWe-y]
_egdeps = [('TimeSeriesPower', ('SimId', 'AgentId', 'Time'), 'Value'),]

Expand Down
2 changes: 2 additions & 0 deletions cymetric/root_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def root_metric(obj=None, name=None, schema=None, *args, **kwargs):
snapshots = root_metric(name='Snapshots')
debug_requests = root_metric(name='DebugRequests')
debug_bids = root_metric(name='DebugBids')
explicit_inventory = root_metric(name='ExplicitInventory')
explicit_inventory_compact = root_metric(name='ExplicitInventoryCompact')

#where do these tables come from?
commod_priority = root_metric(name='CommodPriority')
Expand Down
2 changes: 2 additions & 0 deletions tests/test-input.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<duration>5</duration>
<startmonth>1</startmonth>
<startyear>2000</startyear>
<explicit_inventory>true</explicit_inventory>
<explicit_inventory_compact>true</explicit_inventory_compact>
</control>

<archetypes>
Expand Down
62 changes: 58 additions & 4 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,70 @@ def test_transaction_quantity():
assert_frame_equal(exp, obs)


def test_explicit_inventory_by_agent():
exp = pd.DataFrame(np.array([
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 1, 'core', 922350000, 1.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 2, 'core', 922350000, 4.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 1, 'inventory', 922350000, 1.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 1, 'inventory', 922380000, 2.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 2, 'core', 922350000, 2.0)
], dtype=ensure_dt_bytes([
('SimId', 'O'), ('AgentId', '<i8'), ('Time', '<i8'),
('InventoryName', 'O'), ('NucId', '<i8'), ('Quantity', '<f8')]))
)
inv = pd.DataFrame(np.array([
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 1, 'core', 922350000, 1.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 2, 'core', 922350000, 2.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 2, 'core', 922350000, 2.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 1, 'inventory', 922350000, 1.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 1, 'inventory', 922380000, 2.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 2, 'core', 922350000, 2.0),
], dtype=ensure_dt_bytes([
('SimId', 'O'), ('AgentId', '<i8'), ('Time', '<i8'),
('InventoryName', 'O'), ('NucId', '<i8'), ('Quantity', '<f8')]))
)
series = [inv.set_index(['SimId', 'AgentId', 'Time', 'InventoryName', 'NucId'])['Quantity']]
obs = metrics.explicit_inventory_by_agent.func(series)
assert_frame_equal(exp, obs)


def test_explicit_inventory_by_nuc():
exp = pd.DataFrame(np.array([
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 'core', 922350000, 1.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 'inventory', 922350000, 1.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 'inventory', 922380000, 2.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 'core', 922350000, 4.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 'core', 922380000, 2.0)
], dtype=ensure_dt_bytes([
('SimId', 'O'), ('Time', '<i8'), ('InventoryName', 'O'),
('NucId', '<i8'), ('Quantity', '<f8')]))
)
inv = pd.DataFrame(np.array([
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 1, 'core', 922350000, 1.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 2, 'core', 922350000, 2.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 2, 'core', 922380000, 2.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 1, 'inventory', 922350000, 1.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 1, 'inventory', 922380000, 2.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 2, 'core', 922350000, 2.0),
], dtype=ensure_dt_bytes([
('SimId', 'O'), ('AgentId', '<i8'), ('Time', '<i8'),
('InventoryName', 'O'), ('NucId', '<i8'), ('Quantity', '<f8')]))
)
series = [inv.set_index(['SimId', 'Time', 'InventoryName', 'NucId'])['Quantity']]
obs = metrics.explicit_inventory_by_nuc.func(series)
assert_frame_equal(exp, obs)


def test_annual_electricity_generated_by_agent():
exp = pd.DataFrame(np.array([
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 0, 100),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 1, 100),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 1, 100),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 0, 200),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 1, 200),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 3, 1, 400)
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 1, 200),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 3, 1, 400)
], dtype=ensure_dt_bytes([
('SimId', 'O'), ('AgentId', '<i8'), ('Year', '<i8'),
('Energy', '<f8')]))
('Energy', '<f8')]))
)
tsp = pd.DataFrame(np.array([
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 3, 1200),
Expand Down
16 changes: 16 additions & 0 deletions tests/test_root_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ def test_snapshots(db, fname, backend):
assert_equal('Snapshots', r.name)


@dbtest
def test_inventories(db, fname, backend):
r = root_metrics.explicit_inventory(db=db)
obs = r()
assert_less(0, len(obs))
assert_equal('ExplicitInventory', r.name)


@dbtest
def test_inventories_compact(db, fname, backend):
r = root_metrics.explicit_inventory_compact(db=db)
obs = r()
assert_less(0, len(obs))
assert_equal('ExplicitInventoryCompact', r.name)


@dbtest
def test_resources_non_existent_filter(db, fname, backend):
r = root_metrics.resources(db=db)
Expand Down

0 comments on commit 881db9d

Please sign in to comment.