diff --git a/cymetric/metrics.py b/cymetric/metrics.py index 36692ba6..18d5b034 100644 --- a/cymetric/metrics.py +++ b/cymetric/metrics.py @@ -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 + + +# Quantity per GigaWattElectric in TransactionQuantity [kg/GWe] +_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] + diff --git a/tests/test_metrics.py b/tests/test_metrics.py index 48712010..458e3e9b 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -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', '