.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/examples/03_flowsheet/01_flowsheet_basics.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_examples_03_flowsheet_01_flowsheet_basics.py: Flowsheet Basics ================ Related Sample objects can be managed as a network. In the Process Engineering/Metallurgy disciplines the network will often be called a `flowsheet`. .. GENERATED FROM PYTHON SOURCE LINES 9-20 .. code-block:: Python from copy import deepcopy from typing import Dict import pandas as pd from matplotlib import pyplot as plt from elphick.geomet.flowsheet import Flowsheet from elphick.geomet.flowsheet.operation import Operation from elphick.geomet.flowsheet.stream import Stream from elphick.geomet.utils.data import sample_data .. GENERATED FROM PYTHON SOURCE LINES 21-25 Create some Sample objects -------------------------- Create an object, and split it to create two more objects. .. GENERATED FROM PYTHON SOURCE LINES 26-31 .. code-block:: Python df_data: pd.DataFrame = sample_data() obj_strm: Stream = Stream(df_data, name='Feed') obj_strm_1, obj_strm_2 = obj_strm.split(0.4, name_1='stream 1', name_2='stream 2') .. GENERATED FROM PYTHON SOURCE LINES 32-34 Placeholder random nodes are created for each Sample object. This is done to capture the relationships implicitly defined by any math operations performed on the objects. .. GENERATED FROM PYTHON SOURCE LINES 34-38 .. code-block:: Python for obj in [obj_strm, obj_strm_1, obj_strm_2]: print(obj.name, obj.nodes) .. rst-class:: sphx-glr-script-out .. code-block:: none Feed [UUID('55bdf064-6313-4558-b258-ed8f440eeca6'), UUID('f89baa17-a5ee-4f54-87ad-bac82dad1d3e')] stream 1 [UUID('f89baa17-a5ee-4f54-87ad-bac82dad1d3e'), UUID('97edd248-643f-4e7e-b7b9-d5ab0d8fb96e')] stream 2 [UUID('f89baa17-a5ee-4f54-87ad-bac82dad1d3e'), UUID('8260e941-94bc-4ca6-957f-db7b0a9b4616')] .. GENERATED FROM PYTHON SOURCE LINES 39-43 Create a Flowsheet object ------------------------- This requires passing an Iterable of Sample objects .. GENERATED FROM PYTHON SOURCE LINES 44-47 .. code-block:: Python fs: Flowsheet = Flowsheet.from_objects([obj_strm, obj_strm_1, obj_strm_2]) .. GENERATED FROM PYTHON SOURCE LINES 48-49 Print the node object detail .. GENERATED FROM PYTHON SOURCE LINES 49-53 .. code-block:: Python for node in fs.graph.nodes: print(fs.graph.nodes[node]['mc']) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 54-55 Note that the random node placeholder integers have been renumbered for readability. .. GENERATED FROM PYTHON SOURCE LINES 55-59 .. code-block:: Python for obj in [obj_strm, obj_strm_1, obj_strm_2]: print(obj.name, obj.nodes) .. rst-class:: sphx-glr-script-out .. code-block:: none Feed [0, 1] stream 1 [1, 2] stream 2 [1, 3] .. GENERATED FROM PYTHON SOURCE LINES 60-64 Print the overall network balanced status NOTE: presently this only includes node balance status edge balance status will assure the mass-moisture balance is satisfied .. GENERATED FROM PYTHON SOURCE LINES 64-67 .. code-block:: Python print(fs.all_nodes_healthy) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 68-70 Plot the network. Imbalanced Nodes will appear red. Later, Imbalanced Edges will also appear red. .. GENERATED FROM PYTHON SOURCE LINES 70-74 .. code-block:: Python fs.plot() plt .. image-sg:: /auto_examples/examples/03_flowsheet/images/sphx_glr_01_flowsheet_basics_001.png :alt: Flowsheet Nodes Healthy: True, Streams Healthy: True :srcset: /auto_examples/examples/03_flowsheet/images/sphx_glr_01_flowsheet_basics_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 75-76 Display the weight averages for all edges (streams) in the network (flowsheet) .. GENERATED FROM PYTHON SOURCE LINES 76-80 .. code-block:: Python df_report: pd.DataFrame = fs.report() df_report .. raw:: html
wet_mass mass_dry H2O Fe SiO2 Al2O3 LOI
name
Feed 300.0 260.0 13.333333 59.0 3.515385 1.873077 4.0
stream 1 120.0 104.0 13.333333 59.0 3.515385 1.873077 4.0
stream 2 180.0 156.0 13.333333 59.0 3.515385 1.873077 4.0


.. GENERATED FROM PYTHON SOURCE LINES 81-85 .. code-block:: Python df_report: pd.DataFrame = fs.report(apply_formats=True) df_report .. raw:: html
wet_mass mass_dry H2O Fe SiO2 Al2O3 LOI
name
Feed 300 260 13.3 59.00 3.52 1.87 4.00
stream 1 120 104 13.3 59.00 3.52 1.87 4.00
stream 2 180 156 13.3 59.00 3.52 1.87 4.00


.. GENERATED FROM PYTHON SOURCE LINES 86-87 Plot the interactive network using plotly .. GENERATED FROM PYTHON SOURCE LINES 87-91 .. code-block:: Python fig = fs.plot_network() fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 92-93 Plot the Sankey .. GENERATED FROM PYTHON SOURCE LINES 93-97 .. code-block:: Python fig = fs.plot_sankey() fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 98-99 Demonstrate the table-plot .. GENERATED FROM PYTHON SOURCE LINES 99-103 .. code-block:: Python fig = fs.table_plot(plot_type='sankey', table_pos='top', table_area=0.3).update_layout(height=700) fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 104-108 .. code-block:: Python fig = fs.table_plot(plot_type='network', table_pos='bottom', table_area=0.3).update_layout(height=700) fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 109-112 Expand the Network with Math Operators -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 113-122 .. code-block:: Python obj_strm_3, obj_strm_4 = obj_strm_2.split(0.8, name_1='stream 3', name_2='stream 4') obj_strm_5 = obj_strm_1.add(obj_strm_3, name='stream 5') fs2: Flowsheet = Flowsheet.from_objects([obj_strm, obj_strm_1, obj_strm_2, obj_strm_3, obj_strm_4, obj_strm_5]) fig = fs2.table_plot(plot_type='sankey', table_pos='left') fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 123-125 Setting Node names ------------------ .. GENERATED FROM PYTHON SOURCE LINES 126-130 .. code-block:: Python nodes_before: Dict[int, Operation] = fs.nodes_to_dict() print({n: o.name for n, o in nodes_before.items()}) .. rst-class:: sphx-glr-script-out .. code-block:: none {0: '0', 1: '1', 2: '2', 3: '3'} .. GENERATED FROM PYTHON SOURCE LINES 131-135 .. code-block:: Python fs.set_node_names(node_names={0: 'node_0', 1: 'node_1', 2: 'node_2', 3: 'node_3'}) nodes_after: Dict[int, Operation] = fs.nodes_to_dict() print({n: o.name for n, o in nodes_after.items()}) .. rst-class:: sphx-glr-script-out .. code-block:: none {0: 'node_0', 1: 'node_1', 2: 'node_2', 3: 'node_3'} .. GENERATED FROM PYTHON SOURCE LINES 136-140 Setting Stream data ------------------- First we show how to easily access the stream data as a dictionary .. GENERATED FROM PYTHON SOURCE LINES 141-145 .. code-block:: Python stream_data: Dict[str, Stream] = fs.streams_to_dict() print(stream_data.keys()) .. rst-class:: sphx-glr-script-out .. code-block:: none dict_keys(['Feed', 'stream 1', 'stream 2']) .. GENERATED FROM PYTHON SOURCE LINES 146-147 We will replace stream 2 with the same data as stream 1. .. GENERATED FROM PYTHON SOURCE LINES 147-154 .. code-block:: Python new_stream: Stream = deepcopy(fs.get_stream_by_name('stream 1')) # we need to rename to avoid a creating a duplicate stream name new_stream.name = 'stream 1 copy' fs.set_stream_data({'stream 2': new_stream}) print(fs.streams_to_dict().keys()) .. rst-class:: sphx-glr-script-out .. code-block:: none dict_keys(['Feed', 'stream 1', 'stream 1 copy']) .. GENERATED FROM PYTHON SOURCE LINES 155-156 Of course the network is now unbalanced as highlighted in the Sankey .. GENERATED FROM PYTHON SOURCE LINES 156-160 .. code-block:: Python fig = fs.table_plot() fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 161-168 Methods to modify relationships ------------------------------- Sometimes the network that is automatically created may not be what you are after - for example flow may be in the wrong direction. We'll learn how to modify an existing network, by picking up the network above. Let's break the links for the _stream 1_. .. GENERATED FROM PYTHON SOURCE LINES 169-174 .. code-block:: Python fs.reset_stream_nodes(stream="stream 1") fig = fs.table_plot() fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 175-176 We'll now break all remaining connections (we could have done this from the start). .. GENERATED FROM PYTHON SOURCE LINES 176-181 .. code-block:: Python fs.reset_stream_nodes() fig = fs.table_plot() fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 182-183 Now we'll create some linkages - of course they will be completely rubbish and not balance. .. GENERATED FROM PYTHON SOURCE LINES 183-189 .. code-block:: Python fs.set_stream_parent(stream="stream 1", parent="Feed") fs.set_stream_child(stream="stream 1", child="stream 1 copy") fig = fs.table_plot() fig .. raw:: html


.. GENERATED FROM PYTHON SOURCE LINES 190-191 Perhaps less useful, but possible, we can build relationships by setting nodes directly. .. GENERATED FROM PYTHON SOURCE LINES 191-197 .. code-block:: Python fs.reset_stream_nodes() fs.set_nodes(stream="stream 1", nodes=(1, 2)) fs.set_nodes(stream="stream 1 copy", nodes=(2, 3)) fig = fs.table_plot() fig .. raw:: html


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.861 seconds) .. _sphx_glr_download_auto_examples_examples_03_flowsheet_01_flowsheet_basics.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 01_flowsheet_basics.ipynb <01_flowsheet_basics.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 01_flowsheet_basics.py <01_flowsheet_basics.py>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_