Skip to content

Commit

Permalink
added healthy properties to flowsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
elphick committed Oct 27, 2024
1 parent 0738ec6 commit 20a2960
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
15 changes: 9 additions & 6 deletions docs/source/sg_execution_times.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Computation times
=================
**00:14.610** total execution time for 17 files **from all galleries**:
**00:17.549** total execution time for 18 files **from all galleries**:

.. container::

Expand All @@ -32,8 +32,11 @@ Computation times
* - Example
- Time
- Mem (MB)
* - :ref:`sphx_glr_auto_examples_examples_02_interval_sample_04_partition_models.py` (``..\..\examples\02_interval_sample\04_partition_models.py``)
- 00:14.610
* - :ref:`sphx_glr_auto_examples_examples_02_interval_sample_05_resampling_interval_data.py` (``..\..\examples\02_interval_sample\05_resampling_interval_data.py``)
- 00:12.469
- 0.0
* - :ref:`sphx_glr_auto_examples_examples_03_flowsheet_02_flowsheet_from_file.py` (``..\..\examples\03_flowsheet\02_flowsheet_from_file.py``)
- 00:05.080
- 0.0
* - :ref:`sphx_glr_auto_examples_examples_01_getting_started_01_create_sample.py` (``..\..\examples\01_getting_started\01_create_sample.py``)
- 00:00.000
Expand All @@ -56,13 +59,13 @@ Computation times
* - :ref:`sphx_glr_auto_examples_examples_02_interval_sample_03_incremental_separation.py` (``..\..\examples\02_interval_sample\03_incremental_separation.py``)
- 00:00.000
- 0.0
* - :ref:`sphx_glr_auto_examples_examples_02_interval_sample_05_resampling_interval_data.py` (``..\..\examples\02_interval_sample\05_resampling_interval_data.py``)
* - :ref:`sphx_glr_auto_examples_examples_02_interval_sample_04_partition_models.py` (``..\..\examples\02_interval_sample\04_partition_models.py``)
- 00:00.000
- 0.0
* - :ref:`sphx_glr_auto_examples_examples_03_flowsheet_01_flowsheet_basics.py` (``..\..\examples\03_flowsheet\01_flowsheet_basics.py``)
* - :ref:`sphx_glr_auto_examples_examples_02_interval_sample_06_resampling_interval_data_2d.py` (``..\..\examples\02_interval_sample\06_resampling_interval_data_2d.py``)
- 00:00.000
- 0.0
* - :ref:`sphx_glr_auto_examples_examples_03_flowsheet_02_flowsheet_from_file.py` (``..\..\examples\03_flowsheet\02_flowsheet_from_file.py``)
* - :ref:`sphx_glr_auto_examples_examples_03_flowsheet_01_flowsheet_basics.py` (``..\..\examples\03_flowsheet\01_flowsheet_basics.py``)
- 00:00.000
- 0.0
* - :ref:`sphx_glr_auto_examples_examples_03_flowsheet_03_filtering_flowsheet.py` (``..\..\examples\03_flowsheet\03_filtering_flowsheet.py``)
Expand Down
28 changes: 22 additions & 6 deletions elphick/geomet/flowsheet/flowsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
from pathlib import Path
from typing import Dict, List, Optional, Tuple, Union, TypeVar, TYPE_CHECKING
import re

import matplotlib
import matplotlib.cm as cm
Expand Down Expand Up @@ -39,11 +40,23 @@ def __init__(self, name: str = 'Flowsheet'):
self._logger: logging.Logger = logging.getLogger(__class__.__name__)

@property
def balanced(self) -> bool:
def healthy(self) -> bool:
return self.all_nodes_healthy and self.all_streams_healthy

@property
def all_nodes_healthy(self) -> bool:
bal_vals: List = [self.graph.nodes[n]['mc'].is_balanced for n in self.graph.nodes]
bal_vals = [bv for bv in bal_vals if bv is not None]
return all(bal_vals)

@property
def all_streams_healthy(self) -> bool:
"""Check if all streams are healthy"""
# account for the fact that some edges may not have an mc object
if not all([d['mc'] for u, v, d in self.graph.edges(data=True)]):
return False
return all([self.graph.edges[u, v]['mc'].status.ok for u, v in self.graph.edges])

@classmethod
def from_objects(cls, objects: list[MC],
name: Optional[str] = 'Flowsheet') -> FS:
Expand Down Expand Up @@ -368,14 +381,18 @@ def plot(self, orientation: str = 'horizontal') -> plt.Figure:
return hf

def _plot_title(self, html: bool = True, compact: bool = False):
title = self.name
# title = f"{self.name}<br><br><sup>Balanced: {self.balanced}<br>Edge Status OK: {self.edge_status[0]}</sup>"
# title = self.name
title = (f"{self.name}<br><sup>Nodes Healthy: "
f"<span style='color: {'red' if not self.all_nodes_healthy else 'black'}'>{self.all_nodes_healthy}</span>, "
f"Streams Healthy: "
f"<span style='color: {'red' if not self.all_streams_healthy else 'black'}'>{self.all_streams_healthy}</span></sup>")
# if compact:
# title = title.replace("<br><br>", "<br>").replace("<br>Edge", ", Edge")
# if not self.edge_status[0]:
# title = title.replace("</sup>", "") + f", {self.edge_status[1]}</sup>"
# if not html:
# title = title.replace('<br><br>', '\n').replace('<br>', '\n').replace('<sup>', '').replace('</sup>', '')
if not html:
title = title.replace('<br><br>', '\n').replace('<br>', '\n').replace('<sup>', '').replace('</sup>', '')
title = re.sub(r'<span style=.*?>(.*?)</span>', r'\1', title)
return title

def report(self, apply_formats: bool = False) -> pd.DataFrame:
Expand Down Expand Up @@ -1037,4 +1054,3 @@ def reset_stream_nodes(self, stream: Optional[str] = None):
mc: MC = self.get_edge_by_name(stream)
mc.set_nodes([random_int(), random_int()])
self._update_graph(mc)

2 changes: 1 addition & 1 deletion elphick/geomet/utils/estimates.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def coerce_estimates(estimate_stream: Stream, input_stream: Stream,
fig = fs2.table_plot(plot_type='network')
fig.update_layout(title=f"{fs2.name}: Coerced Estimates").show()

if fs2.balanced is False:
if fs2.all_nodes_healthy is False:
raise ValueError('Flowsheet is not balanced after adjustment')

return estimate_stream
2 changes: 1 addition & 1 deletion examples/03_flowsheet/01_flowsheet_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
# NOTE: presently this only includes node balance status
# edge balance status will assure the mass-moisture balance is satisfied

print(fs.balanced)
print(fs.all_nodes_healthy)

# %%
# Plot the network.
Expand Down

0 comments on commit 20a2960

Please sign in to comment.