Skip to content

Commit

Permalink
commod dict can find nested commodities (aka streams)
Browse files Browse the repository at this point in the history
  • Loading branch information
nuclearkatie committed Apr 12, 2021
1 parent de1c6df commit 5ce4554
Showing 1 changed file with 70 additions and 4 deletions.
74 changes: 70 additions & 4 deletions trailmap/commodity_dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_commod_names(metadata, uitype, agent):
'''Return all archetypes and their aliases for a given uitype.
inputs:
- metadata
- metadata: Cyclus metadata
- uitype: a string. Example, "incommodity"
- agent: a string. Example, ":cycamore:Enrichment"
Expand All @@ -40,14 +40,78 @@ def get_commod_names(metadata, uitype, agent):

for var in agent_data:
for param in agent_data[var]:
if param == "uitype" and uitype in agent_data[var][param]:
aliases.append(var)
if param == "uitype":
if uitype in agent_data[var][param]: #typical for archetypes
aliases.append(var)
#if streams are present
path = search_var_recursive(agent_data[var][param], uitype)
if path is not None:
aliases.append(find_alias(agent_data[var]["alias"], path))

#drop the "val" (signifing an interleave) and "streams_" aliases
try:
while True:
aliases.remove("val")
except:
pass

try:
while True:
aliases.remove('streams_')
except:
pass

return aliases


def search_var_recursive(var, uitype):
'''Finds the path within streams_ or similar tree that matches desired
uitype. Searches recursively
inputs:
- var: a nested list from an archetype's metadata
- uitype: a string. Example, "incommodity"
outputs:
- path: the path within var that locates uitype
'''
for index,item in enumerate(var):
if item == uitype:
return [index]
if isinstance(item, list):
path = search_var_recursive(item, uitype)
if path:
return [index] + path


def find_alias(var, path):
'''Given path to locate uitype, searches archetype aliases to locate
matching alias
inputs:
- var: a nested list of aliases
- path: the path to search for the desired alias
outputs:
- alias: a string
'''
if len(path) == 1:
return var[path[0]]
else:
return find_alias(var[path[0]], path[1:])


def build_facility_dictionary(metadata, archetypes):
'''Identify commodities for each available archetype'''
'''Identify commodities for each available archetype
inputs:
- metadata: Cyclus metadata
- archetypes: a list of archetypes to use
outputs:
- archetype_commods: a dictionary with the Cyclus archetypes available
and the names of their incommodities and outcommodities
'''
archetype_commods = {}

for archetype in archetypes:
Expand All @@ -56,6 +120,8 @@ def build_facility_dictionary(metadata, archetypes):
outcommods = get_commod_names(metadata["annotations"], "outcommodity",
archetype)



archetype_commods.update({archetype: (incommods,
outcommods)})

Expand Down

0 comments on commit 5ce4554

Please sign in to comment.