From 4d7a7387b422ee5d9bf2958fab10eb10eaf3fe26 Mon Sep 17 00:00:00 2001 From: Louis Hartono Date: Wed, 19 Feb 2020 15:10:34 -0500 Subject: [PATCH 1/9] Create TransactionQuantityPerGWe metrics and tests for transaction_quantity_per_gwe function --- cymetric/metrics.py | 46 +++++++++++++++++++++++++++++++++++++++++++ tests/test_metrics.py | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/cymetric/metrics.py b/cymetric/metrics.py index 4d1e0761..f1ec2373 100644 --- a/cymetric/metrics.py +++ b/cymetric/metrics.py @@ -502,4 +502,50 @@ def inventory_quantity_per_gwe(expinv,power): 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): + """Returns quantity per GWe in the inventory table + """ + 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() + tranacts = pd.DataFrame(data={'SimId': tranacts.SimId, + 'TransactionId': tranacts.TransactionId, + 'ResourceId': tranacts.ResourceId, + 'ObjId': tranacts.ObjId, + 'Time': tranacts.TimeCreated, + 'SenderId': tranacts.SenderId, + 'ReceiverId': tranacts.ReceiverId, + 'Commodity': tranacts.Commodity, + 'Units': tranacts.Units, + 'Quantity': tranacts.Quantity}, + columns=['SimId','TransactionId','ResourceId','ObjId','Time','SenderId','ReceiverId','Commodity','Units','Quantity']) + tranacts['Units'] = "kg/GWe" + tranacts=pd.merge(tranacts,df1, on=['SimId','Time'],how='left') + tranacts.Quantity = tranacts.Quantity/tranacts.Value + tranacts=tranacts.drop(['Value'],axis=1) + return tranacts diff --git a/tests/test_metrics.py b/tests/test_metrics.py index 28459eb8..f39c3537 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -396,6 +396,40 @@ def test_inventory_quantity_per_gwe(): obs = metrics.inventory_quantity_per_gwe.func(inv, tsp) assert_frame_equal(exp, obs) +def test_transaction_quantity_per_gwe(): + #exp is the expected output metrics + exp = pd.DataFrame(np.array([ + (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 7, 3, 3, 10, 20, 'LWR Fuel', 'kg/GWe', 0.82), + (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 8, 4, 3, 20, 30, 'FR Fuel', 'kg/GWe', 0.61), + (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 3, 9, 5, 12, 30, 40, 'Spent Fuel', 'kg/GWe', 0.09), + ], dtype=ensure_dt_bytes([ + ('SimId', 'O'), ('TransactionId', ' Date: Wed, 19 Feb 2020 15:27:50 -0500 Subject: [PATCH 2/9] Removed inventory_quantity_per_gwe in metrics.py and test_metrics.py --- cymetric/metrics.py | 37 ------------------------------------- tests/test_metrics.py | 31 ------------------------------- 2 files changed, 68 deletions(-) diff --git a/cymetric/metrics.py b/cymetric/metrics.py index f1ec2373..c8b59667 100644 --- a/cymetric/metrics.py +++ b/cymetric/metrics.py @@ -467,43 +467,6 @@ 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): - """Returns quantity per GWe in the inventory table - """ - 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'] diff --git a/tests/test_metrics.py b/tests/test_metrics.py index f39c3537..9892f3c2 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -365,37 +365,6 @@ def test_timelist(): assert_frame_equal(exp, obs) -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', ' Date: Wed, 26 Feb 2020 13:27:22 -0500 Subject: [PATCH 3/9] Change Time to TimeCreated on test_transaction_quantity_per_gwe --- tests/test_metrics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_metrics.py b/tests/test_metrics.py index 9892f3c2..802425f5 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -393,7 +393,7 @@ def test_transaction_quantity_per_gwe(): (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 3, 9, 5, 12, 30, 40, 'Spent Fuel', 'kg', 9), ], dtype=ensure_dt_bytes([ ('SimId', 'O'), ('TransactionId', ' Date: Wed, 4 Mar 2020 13:17:56 -0500 Subject: [PATCH 4/9] Clarify the purpose of the metric --- cymetric/metrics.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cymetric/metrics.py b/cymetric/metrics.py index c8b59667..080fa90c 100644 --- a/cymetric/metrics.py +++ b/cymetric/metrics.py @@ -485,7 +485,9 @@ def timelist(info): @metric(name='TransactionQuantityPerGWe', depends=_tranactsdeps, schema=_tranactsschema) def transaction_quantity_per_gwe(tranacts,power): - """Returns quantity per GWe in the inventory table + """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 = pd.DataFrame(data={'SimId': power.SimId, 'AgentId': power.AgentId, From 3dcb82b034722a704d13119f233d0eac89fbcc64 Mon Sep 17 00:00:00 2001 From: Louis Hartono Date: Fri, 6 Mar 2020 15:21:30 -0500 Subject: [PATCH 5/9] Created a new InventoryQuantityPerTotalGWe metric --- cymetric/metrics.py | 62 +++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/cymetric/metrics.py b/cymetric/metrics.py index 080fa90c..2b484885 100644 --- a/cymetric/metrics.py +++ b/cymetric/metrics.py @@ -5,6 +5,7 @@ import numpy as np import pandas as pd +import scipy.integrate as integrate try: from pyne import data @@ -467,27 +468,22 @@ def timelist(info): del _tldeps, _tlschema -# Quantity per GigaWattElectric in TransactionQuantity [kg/GWe] -_tranactsdeps = ['TransactionQuantity','TimeSeriesPower'] +# Quantity per GigaWattElectric (integration of total power) in Inventory [kg/GWe] +_invdeps = ['ExplicitInventory','TimeSeriesPower'] -_tranactsschema = [ +_invschema = [ ('SimId', ts.UUID), - ('TransactionId', ts.INT), - ('ResourceId', ts.INT), - ('ObjId', ts.INT), + ('AgentId', ts.INT), ('Time', ts.INT), - ('SenderId', ts.INT), - ('ReceiverId', ts.INT), - ('Commodity', ts.STRING), - ('Units', ts.STRING), + ('InventoryName', ts.STRING), + ('NucId', ts.INT), ('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. +@metric(name='InventoryQuantityPerTotalGWe', 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 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, @@ -497,20 +493,20 @@ def transaction_quantity_per_gwe(tranacts,power): power_index = ['SimId','Time'] power = power.groupby(power_index).sum() df1 = power.reset_index() - tranacts = pd.DataFrame(data={'SimId': tranacts.SimId, - 'TransactionId': tranacts.TransactionId, - 'ResourceId': tranacts.ResourceId, - 'ObjId': tranacts.ObjId, - 'Time': tranacts.TimeCreated, - 'SenderId': tranacts.SenderId, - 'ReceiverId': tranacts.ReceiverId, - 'Commodity': tranacts.Commodity, - 'Units': tranacts.Units, - 'Quantity': tranacts.Quantity}, - columns=['SimId','TransactionId','ResourceId','ObjId','Time','SenderId','ReceiverId','Commodity','Units','Quantity']) - tranacts['Units'] = "kg/GWe" - tranacts=pd.merge(tranacts,df1, on=['SimId','Time'],how='left') - tranacts.Quantity = tranacts.Quantity/tranacts.Value - tranacts=tranacts.drop(['Value'],axis=1) - return tranacts - + total_power = 0 + for t in range(len(df1)): + realpower = total_power + df1.Value[t] + total_power += realpower + 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, + '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 From 9c2477939230083070f82eaf600a49c903adf9d8 Mon Sep 17 00:00:00 2001 From: Louis Hartono Date: Fri, 6 Mar 2020 15:38:57 -0500 Subject: [PATCH 6/9] Added test for InventoryQuantityPerTotalGWe --- cymetric/metrics.py | 2 +- tests/test_metrics.py | 41 ++++++++++++++++++++--------------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/cymetric/metrics.py b/cymetric/metrics.py index 2b484885..4b4b3a1b 100644 --- a/cymetric/metrics.py +++ b/cymetric/metrics.py @@ -480,7 +480,7 @@ def timelist(info): ('Quantity', ts.DOUBLE) ] @metric(name='InventoryQuantityPerTotalGWe', depends=_invdeps, schema=_invschema) -def inventory_quantity_per_gwe(expinv,power): +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. diff --git a/tests/test_metrics.py b/tests/test_metrics.py index 802425f5..688e0a31 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -365,39 +365,38 @@ def test_timelist(): assert_frame_equal(exp, obs) -def test_transaction_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, 7, 3, 3, 10, 20, 'LWR Fuel', 'kg/GWe', 0.82), - (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 8, 4, 3, 20, 30, 'FR Fuel', 'kg/GWe', 0.61), - (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 3, 9, 5, 12, 30, 40, 'Spent Fuel', 'kg/GWe', 0.09), + (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'), ('TransactionId', ' Date: Wed, 11 Mar 2020 13:02:07 -0400 Subject: [PATCH 7/9] Make some changes to add the power from time 0 to time t and modify the test --- cymetric/metrics.py | 5 ++--- tests/test_metrics.py | 12 ++++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/cymetric/metrics.py b/cymetric/metrics.py index 4b4b3a1b..dc02e52c 100644 --- a/cymetric/metrics.py +++ b/cymetric/metrics.py @@ -496,9 +496,8 @@ def inventory_quantity_per_total_gwe(expinv,power): total_power = 0 for t in range(len(df1)): realpower = total_power + df1.Value[t] - total_power += realpower - integral = integrate.quad(lambda t:realpower*(t**0),0,df1.Time[t]) - df1.Value[t] = integral[0] + df1.Value[t] = realpower + total_power = realpower inv = pd.DataFrame(data={'SimId': expinv.SimId, 'AgentId': expinv.AgentId, 'Time': expinv.Time, diff --git a/tests/test_metrics.py b/tests/test_metrics.py index 688e0a31..50c03659 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -368,10 +368,10 @@ def test_timelist(): 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, 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) + (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 2, 'core', 922350000, 1.0), + (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 2, 'usedfuel', 922350000, 2.0), + (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 3, 'core', 922350000, 1.5), + (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 3, 'usedfuel', 922350000, 0.5) ], dtype=ensure_dt_bytes([ ('SimId', 'O'), ('AgentId', ' Date: Wed, 11 Mar 2020 14:24:57 -0400 Subject: [PATCH 8/9] Change schema to CumulativeTimeSeriesPower instead of TimeSeriesPower and modify the test --- cymetric/metrics.py | 47 ++++++++++++++++++++++++++----------------- tests/test_metrics.py | 39 ++++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 28 deletions(-) diff --git a/cymetric/metrics.py b/cymetric/metrics.py index dc02e52c..32d53242 100644 --- a/cymetric/metrics.py +++ b/cymetric/metrics.py @@ -5,7 +5,6 @@ import numpy as np import pandas as pd -import scipy.integrate as integrate try: from pyne import data @@ -468,22 +467,19 @@ def timelist(info): del _tldeps, _tlschema -# Quantity per GigaWattElectric (integration of total power) in Inventory [kg/GWe] -_invdeps = ['ExplicitInventory','TimeSeriesPower'] +# Cumulative power from time 0 to time t in TimeSeriesPower metric +_cumpdeps = ['TimeSeriesPower'] -_invschema = [ +_cumpschema = [ ('SimId', ts.UUID), ('AgentId', ts.INT), ('Time', ts.INT), - ('InventoryName', ts.STRING), - ('NucId', ts.INT), - ('Quantity', ts.DOUBLE) + ('Value', ts.DOUBLE) ] -@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. +@metric(name='CumulativeTimeSeriesPower', depends=_cumpdeps, schema=_cumpschema) +def cumulative_timeseriespower(power): + """Cumulative Timeseriespower metric returns the TimeSeriesPower metric with a cumulative sum + of the power generated by each SimId from time 0 to time t. """ power = pd.DataFrame(data={'SimId': power.SimId, 'AgentId': power.AgentId, @@ -493,11 +489,26 @@ def inventory_quantity_per_total_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] - df1.Value[t] = realpower - total_power = realpower + df1['Value'] = df1['Value'].cumsum() + return df1 + +# Quantity per GigaWattElectric Per Cumulative Power in Inventory [kg/GWe] +_invdeps = ['ExplicitInventory','CumulativeTimeSeriesPower'] + +_invschema = [ + ('SimId', ts.UUID), + ('AgentId', ts.INT), + ('Time', ts.INT), + ('InventoryName', ts.STRING), + ('NucId', ts.INT), + ('Quantity', ts.DOUBLE) + ] +@metric(name='InventoryQuantityPerCumulativePower', depends=_invdeps, schema=_invschema) +def inventory_quantity_per_cumulative_power(expinv,cumpower): + """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 cumulative sum of + the electricity generated in TimeSeriesPower metric from time 0 + """ inv = pd.DataFrame(data={'SimId': expinv.SimId, 'AgentId': expinv.AgentId, 'Time': expinv.Time, @@ -505,7 +516,7 @@ def inventory_quantity_per_total_gwe(expinv,power): 'NucId': expinv.NucId, 'Quantity': expinv.Quantity}, columns=['SimId','AgentId','Time','InventoryName','NucId','Quantity']) - inv=pd.merge(inv,df1, on=['SimId','Time'],how='left') + inv=pd.merge(inv,cumpower, on=['SimId','Time'],how='left') inv.Quantity = inv.Quantity/inv.Value inv=inv.drop(['Value'],axis=1) return inv diff --git a/tests/test_metrics.py b/tests/test_metrics.py index 50c03659..4098e734 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -365,27 +365,46 @@ def test_timelist(): assert_frame_equal(exp, obs) -def test_inventory_quantity_per_total_gwe(): +def test_cumulative_timeseriespower(): #exp is the expected output metrics exp = pd.DataFrame(np.array([ - (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 2, 'core', 922350000, 1.0), - (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 1, 2, 'usedfuel', 922350000, 2.0), - (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 3, 'core', 922350000, 1.5), - (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 3, 'usedfuel', 922350000, 0.5) + (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 2, 300), + (UUID('f22f2281-2464-420a-8325-37320fd418f8'), 3, 700) ], dtype=ensure_dt_bytes([ - ('SimId', 'O'), ('AgentId', ' Date: Fri, 8 May 2020 14:02:49 -0500 Subject: [PATCH 9/9] Update test_metrics.py --- tests/test_metrics.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/test_metrics.py b/tests/test_metrics.py index 4098e734..40b22447 100644 --- a/tests/test_metrics.py +++ b/tests/test_metrics.py @@ -365,6 +365,38 @@ def test_timelist(): assert_frame_equal(exp, obs) +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', '