Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New metrics to return Transaction table with quantity in units of kg/GWe #161

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions cymetric/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,80 @@ def timelist(info):


del _tldeps, _tlschema


# Quantity per GigaWattElectric in Inventory [kg/GWe]
_invdeps = ['ExplicitInventory','TimeSeriesPower']

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

@metric(name='InventoryQuantityPerGWe', depends=_invdeps, schema=_invschema)
def inventory_quantity_per_gwe(expinv,power):
"""Inventory Quantity per GWe metric returns the explicit inventory table with quantity
in units of kg/GWe, calculated by dividing the original quantity by the electricity generated
at the corresponding simulation and the specific time in TimeSeriesPower metric.
"""
power = pd.DataFrame(data={'SimId': power.SimId,
'AgentId': power.AgentId,
'Time': power.Time,
'Value': power.Value},
columns=['SimId','AgentID','Time', 'Value'])
power_index = ['SimId','Time']
power = power.groupby(power_index).sum()
df1 = power.reset_index()
inv = pd.DataFrame(data={'SimId': expinv.SimId,
'AgentId': expinv.AgentId,
'Time': expinv.Time,
'InventoryName': expinv.InventoryName,
'NucId': expinv.NucId,
'Quantity': expinv.Quantity},
columns=['SimId','AgentId','Time','InventoryName','NucId','Quantity'])
inv=pd.merge(inv,df1, on=['SimId','Time'],how='left')
inv.Quantity = inv.Quantity/inv.Value
inv=inv.drop(['Value'],axis=1)
return inv
louishartono marked this conversation as resolved.
Show resolved Hide resolved


# Quantity per GigaWattElectric in TransactionQuantity [kg/GWe]
louishartono marked this conversation as resolved.
Show resolved Hide resolved
_tranactsdeps = ['TransactionQuantity','TimeSeriesPower']

_tranactsschema = [
('SimId', ts.UUID),
('TransactionId', ts.INT),
('ResourceId', ts.INT),
('ObjId', ts.INT),
('Time', ts.INT),
('SenderId', ts.INT),
('ReceiverId', ts.INT),
('Commodity', ts.STRING),
('Units', ts.STRING),
('Quantity', ts.DOUBLE)
]


@metric(name='TransactionQuantityPerGWe',
depends=_tranactsdeps, schema=_tranactsschema)
def transaction_quantity_per_gwe(tranacts, power):
"""Transaction Quantity per GWe metric returns the transaction quantity
table with quantity in units of kg/GWe, calculated by dividing the
original quantity by the electricity generated at the corresponding
simulation and the specific time in TimeSeriesPower metric.
"""
power = power.groupby(['SimId', 'Time']).sum()
df1 = power.reset_index()
tranacts_index = ['SimId', 'TransactionId', 'ResourceId',
'ObjId', 'Time','SenderId', 'ReceiverId',
'Commodity', 'Units', 'Quantity']
tranacts = tranacts.rename(columns = {'TimeCreated' : 'Time'})
tranacts['Units'] = tranacts['Units'] + "/MWe"
tranacts=pd.merge(tranacts,df1, on=['SimId', 'Time'],how='left')
tranacts.Quantity = tranacts.Quantity/tranacts.Value
return tranacts[tranacts_index]

122 changes: 85 additions & 37 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,53 +549,101 @@ def test_timelist():
def test_inventory_quantity_per_gwe():
# exp is the expected output metrics
exp = pd.DataFrame(np.array([
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 0, 'core',
922350000, 1.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 0, 'usedfuel',
922350000, 2.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 1, 'core',
922350000, 2.0),
], dtype=ensure_dt_bytes([
('SimId', 'O'),
('AgentId', '<i8'),
('Time', '<i8'),
('InventoryName', 'O'),
('NucId', '<i8'),
('Quantity', '<f8')
]))
)
(UUID('f22f2281-2464-420a-8325-37320fd418f8'),
1, 0, 'core', 922350000, 1.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'),
1, 0, 'usedfuel', 922350000, 2.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'),
2, 1, 'core', 922350000, 2.0),
], dtype=ensure_dt_bytes([
('SimId', 'O'),
('AgentId', '<i8'),
('Time', '<i8'),
('InventoryName', 'O'),
('NucId', '<i8'),
('Quantity', '<f8')]))
)

# tsp is the TimeSeriesPower metrics
tsp = pd.DataFrame(np.array([
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 0, 100),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 0, 200),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 1, 100),
], dtype=ensure_dt_bytes([
('SimId', 'O'),
('AgentId', '<i8'),
('Time', '<i8'),
('Value', '<f8')
]))
)
], dtype=ensure_dt_bytes([
('SimId', 'O'),
('AgentId', '<i8'),
('Time', '<i8'),
('Value', '<f8')]))
)
# inv is the ExplicitInventory metrics
inv = pd.DataFrame(np.array([
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 0, 'core',
922350000, 300),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 0, 'usedfuel',
922350000, 600),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 1, 'core',
922350000, 200),
], dtype=ensure_dt_bytes([
('SimId', 'O'),
('AgentId', '<i8'),
('Time', '<i8'),
('InventoryName', 'O'),
('NucId', '<i8'),
('Quantity', '<f8')
]))
)
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 0, 'core', 922350000, 300),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 0, 'usedfuel', 922350000, 600),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 1, 'core', 922350000, 200),
], dtype=ensure_dt_bytes([
('SimId', 'O'),
('AgentId', '<i8'),
('Time', '<i8'),
('InventoryName', 'O'),
('NucId', '<i8'),
('Quantity', '<f8')]))
)
obs = metrics.inventory_quantity_per_gwe.func(inv, tsp)
assert_frame_equal(exp, obs)
louishartono marked this conversation as resolved.
Show resolved Hide resolved


def test_transaction_quantity_per_gwe():
# exp is the expected output metrics
louishartono marked this conversation as resolved.
Show resolved Hide resolved
exp = pd.DataFrame(np.array([
(UUID('f22f2281-2464-420a-8325-37320fd418f8'),
1, 7, 3, 3, 10, 20, 'LWR Fuel', 'kg/MWe', 0.82),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'),
2, 8, 4, 3, 20, 30, 'FR Fuel', 'kg/MWe', 0.61),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'),
3, 9, 5, 12, 30, 40, 'Spent Fuel', 'kg/MWe', 0.09),
], dtype=ensure_dt_bytes([
('SimId', 'O'),
('TransactionId', '<i8'),
('ResourceId', '<i8'),
('ObjId', '<i8'),
('Time', '<i8'),
('SenderId', '<i8'),
('ReceiverId', '<i8'),
('Commodity', 'O'),
('Units', 'O'),
('Quantity', '<f8')]))
louishartono marked this conversation as resolved.
Show resolved Hide resolved
)
# tsp is the TimeSeriesPower metrics
tsp = pd.DataFrame(np.array([
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 3, 225),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 3, 275),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 12, 100),
], dtype=ensure_dt_bytes([
('SimId', 'O'),
('AgentId', '<i8'),
('Time', '<i8'),
('Value', '<f8')]))
louishartono marked this conversation as resolved.
Show resolved Hide resolved
)
# tranacts is the TransactionQuantity metrics
tranacts = pd.DataFrame(np.array([
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 7, 3, 3, 10, 20, 'LWR Fuel', 'kg', 410),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 8, 4, 3, 20, 30, 'FR Fuel', 'kg', 305),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 3, 9, 5, 12, 30, 40, 'Spent Fuel', 'kg', 9),
], dtype=ensure_dt_bytes([
('SimId', 'O'),
('TransactionId', '<i8'),
('ResourceId', '<i8'),
('ObjId', '<i8'),
('TimeCreated', '<i8'),
('SenderId', '<i8'),
('ReceiverId', '<i8'),
('Commodity', 'O'),
('Units', 'O'),
('Quantity', '<f8')]))
)
obs = metrics.transaction_quantity_per_gwe.func(tranacts, tsp)
assert_frame_equal(exp, obs)


if __name__ == "__main__":
louishartono marked this conversation as resolved.
Show resolved Hide resolved
nose.runmodule()