.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/06_clustering/plot_22_cell_formation.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 or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_06_clustering_plot_22_cell_formation.py: Forming manufacturing cells from the hypergraph =============================================== Following Nagy et al. (2022), communities of the activities (hyperedges) and of the resources (vertices) suggest how the production could be divided into cells. Louvain on the line graph groups the activities; hypergraph Louvain and Louvain on the clique expansion group the resources. The drawing colors the resources by their community. .. GENERATED FROM PYTHON SOURCE LINES 13-31 .. code-block:: Python import matplotlib.pyplot as plt import hatvis as hv H = hv.datasets.load_wire_harness() # [snippet:cells] act = hv.louvain(H, target="edges", random_state=0) res = hv.hypergraph_louvain(H, random_state=0) res_clique = hv.louvain(H, random_state=0) # [/snippet:cells] for c in sorted(act.unique()): steps = H.edge_data.loc[act.index[act == c], "step"] print(f"activity community {c}: {len(steps)} activities, e.g. {sorted(set(steps))[:3]}") for c, members in enumerate(hv.community.communities(res)): print(f"resource community {c}: {members}") print("modularity: hypergraph Louvain", round(hv.modularity(H, res), 3), " clique Louvain", round(hv.modularity(H, res_clique), 3)) .. rst-class:: sphx-glr-script-out .. code-block:: none activity community 0: 24 activities, e.g. ['Assembly1 - Buffer2', 'Assembly2 - Buffer4', 'Assembly3 - Buffer6'] activity community 1: 24 activities, e.g. ['Assembly1', 'Assembly2', 'Assembly3'] activity community 2: 22 activities, e.g. ['Assembly1', 'Assembly2', 'Assembly3'] resource community 0: ['O1', 'C8'] resource community 1: ['O2', 'O5', 'R2', 'R5', 'M1', 'M2', 'M3', 'M4', 'D1', 'D2', 'D3', 'C6', 'C7', 'S1', 'S3'] resource community 2: ['O3', 'O4', 'O6', 'O7', 'R1', 'R3', 'R4', 'R6', 'AGV', 'E1', 'E6', 'C1', 'C3', 'S2'] modularity: hypergraph Louvain 0.419 clique Louvain 0.394 .. GENERATED FROM PYTHON SOURCE LINES 32-34 The incidence matrix ordered by the two partitions shows the cells as blocks; the clique expansion is colored by the resource communities. .. GENERATED FROM PYTHON SOURCE LINES 34-46 .. code-block:: Python rows = list(res.sort_values(kind="stable").index) cols = list(act.sort_values(kind="stable").index) fig, axes = plt.subplots(1, 2, figsize=(15, 6.5), gridspec_kw={"width_ratios": [1.6, 1]}) hv.plotting.plot_incidence(H, order=(rows, cols), ax=axes[0], node_groups="type", label_size=6, title="resources (rows) and activities (columns) ordered by community") axes[0].set_xticks([]) for y in res.value_counts().sort_index().cumsum().to_numpy()[:-1]: axes[0].axhline(y - 0.5, color=hv.plotting.PALETTE[1], lw=1) for x in act.value_counts().sort_index().cumsum().to_numpy()[:-1]: axes[0].axvline(x - 0.5, color=hv.plotting.PALETTE[1], lw=1) hv.plotting.draw_clique(H, ax=axes[1], layout="force", node_color=res, title="clique expansion, resource communities") plt.tight_layout() .. image-sg:: /auto_examples/06_clustering/images/sphx_glr_plot_22_cell_formation_001.png :alt: resources (rows) and activities (columns) ordered by community, clique expansion, resource communities :srcset: /auto_examples/06_clustering/images/sphx_glr_plot_22_cell_formation_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.646 seconds) .. _sphx_glr_download_auto_examples_06_clustering_plot_22_cell_formation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/hatvis/main?urlpath=lab/tree/notebooks/auto_examples/06_clustering/plot_22_cell_formation.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_22_cell_formation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_22_cell_formation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_22_cell_formation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_