Skip to content

Commit

Permalink
closes #160. progress bar optional, plus untested dag bugfix. (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
elphick authored May 16, 2024
1 parent 8a0f49d commit 0d984d7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
Mass_Composition 0.6.1 (2024-05-16)
===================================

Feature
-------

- DAG progress bar now optional.

Bugfix
------

- Attempted fix for DAG.run ValueError('generator already running') (#160)


Mass_Composition 0.6.0 (2024-05-16)
===================================

Expand Down
21 changes: 12 additions & 9 deletions elphick/mass_composition/dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def add_output(self, name: str, stream: str) -> 'DAG':
def _topological_sort(self) -> List[str]:
return list(nx.topological_sort(self.graph))

def run(self, input_streams: dict):
def run(self, input_streams: dict, progress_bar: bool = True):
"""
Executes the Directed Acyclic Graph (DAG).
Expand All @@ -102,6 +102,7 @@ def run(self, input_streams: dict):
Parameters:
input_streams (dict): A dictionary mapping node names to Stream objects. These are the initial Stream objects
for the input nodes of the DAG.
progress_bar (bool): If True, a progress bar is displayed during the execution of the DAG.
Returns:
None
Expand All @@ -114,8 +115,9 @@ def run(self, input_streams: dict):
for node in self.graph.nodes:
self.node_executed[node] = False

# Initialise a progressbar that will count up to the number of nodes in the graph
pbar = tqdm(total=len(self.graph.nodes), desc="Executing nodes", unit="node")
if progress_bar:
# Initialise a progressbar that will count up to the number of nodes in the graph
pbar = tqdm(total=len(self.graph.nodes), desc="Executing nodes", unit="node")

executed_nodes = set() # Keep track of nodes that have been executed

Expand Down Expand Up @@ -153,11 +155,12 @@ def run(self, input_streams: dict):
for edge, strm in updated_edges.items():
logger.debug(f"Updating edge {edge} with stream {strm.name}")
self.graph.edges[edge]['mc'] = strm
# update the progress bar by one step
pbar.set_postfix_str(f"Processed node: {node}")
pbar.update()

pbar.close() # Close the progress bar
if progress_bar:
# update the progress bar by one step
pbar.set_postfix_str(f"Processed node: {node}")
pbar.update()
if progress_bar:
pbar.close() # Close the progress bar
logger.info(f"DAG execution complete for the nodes: {executed_nodes}")

def execute_node(self, node: str, strms: dict):
Expand Down Expand Up @@ -234,7 +237,7 @@ def execute_node(self, node: str, strms: dict):
for successor in self.graph.successors(node):
updated_edges[(node, successor)] = result

return node, result, updated_edges
return node, list(result), updated_edges

def plot(self):
plt.figure(figsize=(8, 6))
Expand Down
13 changes: 9 additions & 4 deletions examples/502_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@
kwargs={'fraction': 0.3, 'name_1': 'lump', 'name_2': 'fines'})
dag.add_step(name='split_2', operation=Stream.split, streams=['lump'],
kwargs={'fraction': 0.3, 'name_1': 'lumpier', 'name_2': 'less_lumpy'})
dag.add_output(name='lumpier', stream='lumpier')
dag.add_output(name='mid', stream='less_lumpy')
dag.add_output(name='fines', stream='fines')
dag.add_step(name='split_3', operation=Stream.split, streams=['fines'],
kwargs={'fraction': 0.3, 'name_1': 'finer', 'name_2': 'less_fine'})
dag.add_step(name='joiner_1', operation=Stream.add, streams=['less_lumpy', 'less_fine'],
kwargs={'name': 'mix_1'})
dag.add_step(name='joiner_2', operation=Stream.add, streams=['lumpier', 'finer'],
kwargs={'name': 'mix_2'})
dag.add_output(name='product_1', stream='mix_1')
dag.add_output(name='product_2', stream='mix_2')


# %%
Expand All @@ -53,7 +58,7 @@

dag.run({'feed_1': mc_sample,
'feed_2': deepcopy(mc_sample).rename('sample_2') # names must be unique
})
}, progress_bar=False)

# %%
# Create a Flowsheet object from the dag, enabling all the usual network plotting and analysis methods.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "mass-composition"
version = "0.6.0"
version = "0.6.1"
description = "For managing multi-dimensional mass-composition datasets, supporting weighted mathematical operations and visualisation."
authors = ["Greg <greg@elphick.com.au>"]
packages = [{ include = "elphick/mass_composition" }]
Expand Down

0 comments on commit 0d984d7

Please sign in to comment.