-
Notifications
You must be signed in to change notification settings - Fork 20
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
Adding region and institution hierarchy to graphs/subgraphs #197
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
"""A plot generator for Cymetric. | ||
""" | ||
|
||
import warnings | ||
|
||
|
||
try: | ||
from graphviz import Digraph | ||
|
||
HAVE_GRAPHVIZ = True | ||
except ImportError: | ||
HAVE_GRAPHVIZ = False | ||
|
@@ -13,8 +15,16 @@ | |
from cymetric.filters import transactions_nuc | ||
|
||
|
||
def flow_graph(evaler, senders=(), receivers=(), commodities=(), nucs=(), | ||
label='', start=None, stop=None): | ||
def flow_graph( | ||
evaler, | ||
senders=(), | ||
receivers=(), | ||
commodities=(), | ||
nucs=(), | ||
label="", | ||
start=None, | ||
stop=None, | ||
): | ||
""" | ||
Generate the dot graph of the transation between facilitiese. Applying times | ||
nuclides selection when required. | ||
|
@@ -31,35 +41,74 @@ def flow_graph(evaler, senders=(), receivers=(), commodities=(), nucs=(), | |
start : first timestep to consider, start included | ||
stop : last timestep to consider, stop included | ||
""" | ||
tools.raise_no_graphviz('Unable to generate flow graph!', HAVE_GRAPHVIZ) | ||
tools.raise_no_graphviz("Unable to generate flow graph!", HAVE_GRAPHVIZ) | ||
|
||
df = transactions_nuc( | ||
evaler, senders, receivers, commodities, nucs) | ||
df = transactions_nuc(evaler, senders, receivers, commodities, nucs) | ||
|
||
if start is not None: | ||
df = df.loc[(df['Time'] >= start)] | ||
df = df.loc[(df["Time"] >= start)] | ||
if stop is not None: | ||
df = df.loc[(df['Time'] <= stop)] | ||
df = df.loc[(df["Time"] <= stop)] | ||
|
||
group_end = ['ReceiverPrototype', 'SenderPrototype', 'Commodity'] | ||
group_start = group_end + ['Mass'] | ||
group_end = ["ReceiverPrototype", "SenderPrototype", "Commodity"] | ||
group_start = group_end + ["Mass"] | ||
df = df[group_start].groupby(group_end).sum() | ||
df.reset_index(inplace=True) | ||
|
||
agents_ = evaler.eval('AgentEntry')['Prototype'].tolist() | ||
agents_ = evaler.eval("AgentEntry") | ||
|
||
dot = Digraph('G') | ||
dot = Digraph("G", strict=True) | ||
|
||
for agent in agents_: | ||
dot.node(agent) | ||
# start by constructing region subgraphs | ||
regions = agents_[agents_["Kind"] == "Region"] | ||
for i, row in regions.iterrows(): | ||
region_id = row["AgentId"] | ||
region_prototype = row["Prototype"] | ||
institutions = agents_[ | ||
(agents_["ParentId"] == region_id) & (agents_["Kind"] == "Inst") | ||
] | ||
# graphviz requires subgraphs start with the prefix cluster_ | ||
with dot.subgraph(name=f"cluster_{region_id}") as c: | ||
c.attr( | ||
style="dotted", | ||
label=region_prototype, | ||
color="black", | ||
) | ||
# then construct institution subgraphs | ||
for j, institution in institutions.iterrows(): | ||
institution_id = institution["AgentId"] | ||
institution_prototype = institution["Prototype"] | ||
# graphviz requires subgraphs start with the prefix cluster_ | ||
with c.subgraph(name=f"cluster_{institution_id}") as b: | ||
b.attr( | ||
style="filled", | ||
label=institution_prototype, | ||
color="lightgray", | ||
) | ||
# facilities are nodes in the (sub)graph(s) | ||
facilities = agents_[ | ||
(agents_["ParentId"] == institution_id) | ||
& (agents_["Kind"] == "Facility") | ||
] | ||
Comment on lines
+89
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise for facilities. |
||
for k, facility in facilities.iterrows(): | ||
facility_id = facility["AgentId"] | ||
facility_prototype = facility["Prototype"] | ||
b.node( | ||
name=str(facility_id), | ||
label=str(facility_prototype), | ||
) | ||
|
||
# use transactions to determine edges | ||
for index, row in df.iterrows(): | ||
lbl = '' | ||
if 'com' in label: | ||
lbl += str(row['Commodity']) + ' ' | ||
if 'mass' in label: | ||
lbl += str('{:.2e}'.format(row['Mass'])) + ' ' | ||
dot.edge(row['SenderPrototype'], row['ReceiverPrototype'], | ||
label=lbl) | ||
lbl = "" | ||
if "com" in label: | ||
lbl += str(row["Commodity"]) + " " | ||
if "mass" in label: | ||
lbl += str("{:.2e}".format(row["Mass"])) + " " | ||
dot.edge( | ||
str(row["SenderId"]), | ||
str(row["ReceiverId"]), | ||
label=lbl, | ||
) | ||
|
||
return dot |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In line with using
Kind
to filter forRegion
, we can also useInst
to ensure that we are filtering the appropriate agents.