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 Inventory table with quantity (kg/GWe) in terms of cumulative power from time 0 to time of the simulation #163

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
25 changes: 16 additions & 9 deletions cymetric/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import numpy as np
import pandas as pd
import scipy.integrate as integrate

try:
from pyne import data
Expand Down Expand Up @@ -467,21 +468,22 @@ def timelist(info):

del _tldeps, _tlschema

# Quantity per GigaWattElectric in Inventory [kg/GWe]
# Quantity per GigaWattElectric (integration of total power) in Inventory [kg/GWe]
_invdeps = ['ExplicitInventory','TimeSeriesPower']

_invschema = [
('SimId', ts.UUID),
('AgentId', ts.INT),
('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):
"""Returns quantity per GWe in the inventory table
@metric(name='InventoryQuantityPerTotalGWe', depends=_invdeps, schema=_invschema)
def inventory_quantity_per_total_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 integration of total
electricity generated in TimeSeriesPower metric from time 0 to time of the simulation.
"""
power = pd.DataFrame(data={'SimId': power.SimId,
'AgentId': power.AgentId,
Expand All @@ -491,15 +493,20 @@ def inventory_quantity_per_gwe(expinv,power):
power_index = ['SimId','Time']
power = power.groupby(power_index).sum()
df1 = power.reset_index()
total_power = 0
for t in range(len(df1)):
realpower = total_power + df1.Value[t]
total_power += realpower
Copy link
Member

@bam241 bam241 Mar 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
total_power += realpower
total_power += df1.Value[t]

integral = integrate.quad(lambda t:realpower*(t**0),0,df1.Time[t])
df1.Value[t] = integral[0]
inv = pd.DataFrame(data={'SimId': expinv.SimId,
'AgentId': expinv.AgentId,
'Time': expinv.Time,
'Time': expinv.Time,
'InventoryName': expinv.InventoryName,
'NucId': expinv.NucId,
'Quantity': expinv.Quantity},
columns=['SimId','AgentId','Time','InventoryName','NucId','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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe don't drop but just return the required column ?
I recently learned this is possible and I feel like it is clearer about what is actually returned

return inv

24 changes: 13 additions & 11 deletions tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,38 +365,40 @@ def test_timelist():
assert_frame_equal(exp, obs)


def test_inventory_quantity_per_gwe():
def test_inventory_quantity_per_total_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),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 2, 'core', 922350000, 0.5),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 2, 'usedfuel', 922350000, 1.0),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 3, 'core', 922350000, 0.75),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 3, 'usedfuel', 922350000, 1.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),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 2, 100),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 2, 200),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 3, 300),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 3, 100)
], dtype=ensure_dt_bytes([
('SimId', 'O'), ('AgentId', '<i8'), ('Time', '<i8'),
('Value', '<f8')]))
)
#inv is the ExplicitInventory metrics
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#inv is the ExplicitInventory metrics
# 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),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 2, 'core', 922350000, 300),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 2, 'usedfuel', 922350000, 600),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 3, 'core', 922350000, 900),
(UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 3, 'usedfuel', 922350000, 1200)
], dtype=ensure_dt_bytes([
('SimId', 'O'), ('AgentId', '<i8'), ('Time', '<i8'),
('InventoryName', 'O'), ('NucId', '<i8'), ('Quantity', '<f8')]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, maybe put all the pair on a line ?

)
obs = metrics.inventory_quantity_per_gwe.func(inv, tsp)
assert_frame_equal(exp, obs)


if __name__ == "__main__":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PEP8 2 blank lines between methods

nose.runmodule()