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

Metadata #153

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
99 changes: 99 additions & 0 deletions cymetric/metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import warnings
import pandas as pd
import numpy as np


try:
from pyne import data
import pyne.enrichment as enr
from pyne import nucname
HAVE_PYNE = True
except ImportError:
HAVE_PYNE = False


from cymetric.tools import format_nucs, reduce, merge, add_missing_time_step

def get_usage(evaler):

metadata = evaler.eval("Metadata")
agents_entry = evaler.eval('AgentEntry')
deployment = metadata[ metadata['Type'] == "deployment" ]
decom = metadata[ metadata['Type'] == "decommission" ]
timestep = metadata[ metadata['Type'] == "timestep" ]
throughput_meta = metadata[ metadata['Type'] == "throughput" ]

worklabel = metadata[ metadata['Keyword'] == "WORKLABEL" ]

# Deployement
dep_agent = agents_entry[agents_entry['AgentId'].isin(deployment['AgentId'])]
_tmp = pd.merge(deployment[['SimId', 'AgentId','Keyword', 'Value']], dep_agent, on=['SimId', 'AgentId'])


deployment_use = pd.DataFrame(data={'SimId': _tmp.SimId,
'AgentId': _tmp.AgentId,
'Time': _tmp.EnterTime,
'Keyword': _tmp.Keyword,
'Value':_tmp.Value.astype(float)},
columns=['SimId', 'AgentId', 'Time', 'Keyword', 'Value'])
rtn = deployment_use.copy()

# Decommision
decom_agent = agents_entry[agents_entry['AgentId'].isin(decom['AgentId'])]
decom_agent = decom_agent.reset_index(drop=True)
decom_agent['ExitTime'] = decom_agent['EnterTime'] + decom_agent['Lifetime']
_tmp = pd.merge(decom[['SimId', 'AgentId','Keyword', 'Value']], decom_agent, on=['SimId', 'AgentId'])
decom_use = pd.DataFrame(data={'SimId': _tmp.SimId,
'AgentId': _tmp.AgentId,
'Time': _tmp.ExitTime,
'Keyword': _tmp.Keyword,
'Value':_tmp.Value.astype(float)},
columns=['SimId', 'AgentId', 'Time', 'Keyword', 'Value'])
rtn = pd.concat([rtn, decom_use], ignore_index=True)

# TimeStep
timestep_agent = agents_entry[agents_entry['AgentId'].isin(timestep['AgentId'])]
timestep_agent = timestep_agent.reset_index(drop=True)
timestep_agent['ExitTime'] = timestep_agent['EnterTime'] + timestep_agent['Lifetime']
timestep_tmp = pd.DataFrame(data={'SimId': _tmp.SimId,
'AgentId': _tmp.AgentId,
'EnterTime': _tmp.EnterTime,
'ExitTime': _tmp.ExitTime ,
'Keyword': _tmp.Keyword,
'Value':_tmp.Value.astype(float)},
columns=['SimId', 'AgentId', 'EnterTime', 'ExitTime', 'Keyword', 'Value'])
time_step_data = []
for index, row in timestep_tmp.iterrows():
for i in range(row['EnterTime'], row['ExitTime']):
time_step_data.append( (row['SimId'],
row['AgentId'],
i,
row['Keyword'],
row['Value']))
timestep_use = pd.DataFrame(time_step_data, columns=['SimId', 'AgentId', 'Time', 'Keyword', 'Value'])
rtn = pd.concat([rtn, timestep_use], ignore_index=True)

worklabel.drop_duplicates(inplace=True)
worktimeseries = []
for index, row in worklabel.iterrows():
if (row['AgentId'] in throughput_meta['AgentId']):
work_name = "TimeSeries" + row['Value']
timeseries = evaler.eval(work_name)
if timeseries is not None:
worktimeseries.append(timeseries[timeseries['AgentId'] == row['AgentId'] ])

def get_throughput_timeseries(throughput_df, throughput_meta):
if throughput_df is not None:
_tmp = pd.merge(throughput_meta[['SimId', 'AgentId','Keyword', 'Value']], throughput_df, on=['SimId', 'AgentId'])
_tmp['Value_y'] = _tmp.Value_x.astype(float)*_tmp.Value_y
_tmp.drop(columns=['Value_x'], inplace=True)
_tmp.rename(columns={"Value_y": "Value"}, inplace=True)
return _tmp[['SimId', 'AgentId', 'Time', 'Keyword', 'Value']]
else:
return pd.DataFrame()

for work in worktimeseries:
rtn = pd.concat([rtn, get_throughput_timeseries(work, throughput_meta)], ignore_index=True)


return rtn
2 changes: 1 addition & 1 deletion cymetric/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def annual_electricity_generated_by_agent(elec):
'AgentId': elec.AgentId,
'Year': elec.Time.apply(lambda x: x//12),
'Energy': elec.Value.apply(lambda x: x/12)},
columns=['SimId', 'AgentId', 'Year', 'Energy'])
columns=['SimId', 'AgentId', 'Year', 'Energy'])
el_index = ['SimId', 'AgentId', 'Year']
elec = elec.groupby(el_index).sum()
rtn = elec.reset_index()
Expand Down
3 changes: 3 additions & 0 deletions cymetric/root_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,8 @@ def root_metric(obj=None, name=None, schema=None, *args, **kwargs):

#toolkit-enabled tables
time_series_power = root_metric(name='TimeSeriesPower')
time_series_deployedisnt = root_metric(name='TimeSeriesDeployedInst')
time_series_throughput = root_metric(name='TimeSeriesThroughput')
time_series_enrichmentfeed = root_metric(name='TimeSeriesEnrichmentFeed')
time_series_enrichmentswu = root_metric(name='TimeSeriesEnrichmentSWU')
metadata = root_metric(name='Metadata')
4 changes: 2 additions & 2 deletions cymetric/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ def add_missing_time_step(df, ref_time):

def merge(df, base_col, add_df, add_col):
"""
Merge some additionnal columns fram an additionnal Pandas Data Frame
onother one and then remove the second base column (keeping SimID
Merge some additionnal columns from an additionnal Pandas Data Frame
another one and then remove the second base column (keeping SimID
information).

Parameters
Expand Down