.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/11_applications/plot_01_polymer_grades.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_11_applications_plot_01_polymer_grades.py: Grade transitions of a polymerization reactor ============================================= A polymer plant produces several grades -- here three target molecular weights of poly(methyl methacrylate) -- and switches between them. The historical record of such a plant is the raw material of three questions studied by Dobos, Bankó and Abonyi: where are the steady grades and the transitions, which grade is the reactor in, and which periods carry information on the kinetic parameters. The isothermal MMA reactor of Doyle, Ogunnaike and Pearson (1995) produces the data. .. GENERATED FROM PYTHON SOURCE LINES 13-22 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import mvtsa as mv from mvtsa import segmentation as sg from mvtsa.datasets import MMA_PARAMETERS, simulate_mma_cstr from mvtsa.explain import transition_graph .. GENERATED FROM PYTHON SOURCE LINES 23-27 A campaign of grade changes --------------------------- Initiator flow profile: three grades, ramped transitions, small random moves of the operators; sampling time 0.05 h. .. GENERATED FROM PYTHON SOURCE LINES 27-40 .. code-block:: Python rng = np.random.default_rng(2) grades = {"A": 0.012, "B": 0.0168, "C": 0.026} plan = ["A", "B", "C", "B", "A", "C", "A"] FI = [] for g in plan: level = grades[g] if FI: FI.extend(np.linspace(FI[-1], level, 20)) # ramped transition FI.extend(level * (1 + 0.03 * np.repeat(rng.normal(size=8), 10))) FI = np.asarray(FI) Mn = simulate_mma_cstr(FI) * (1 + 0.003 * rng.normal(size=len(FI))) X = np.column_stack([FI * 1000, Mn / 1000]) # L/h and kg/mol .. GENERATED FROM PYTHON SOURCE LINES 41-45 Steady grades and transitions ----------------------------- Segment-and-cluster: a penalized bottom-up segmentation followed by the clustering of the Gaussian segment models into three states. .. GENERATED FROM PYTHON SOURCE LINES 45-54 .. code-block:: Python seg = sg.StateSegmenter( segmenter=sg.BottomUp("normal", penalty=40, initial_size=10), n_states=3 ).segment(X) print("segments:", seg.n_segments, "| grade sequence:", seg.labels.tolist()) G = transition_graph(seg, sampling_period=0.05) print( {v: round(G.nodes[v]["total_duration"], 1) for v in G.nodes}, "hours per state", ) .. rst-class:: sphx-glr-script-out .. code-block:: none segments: 9 | grade sequence: [1, 2, 0, 2, 1, 2, 0, 2, 1] {1: 13.5, 2: 11.5, 0: 9.0} hours per state .. GENERATED FROM PYTHON SOURCE LINES 55-58 The plan was A, B, C, B, A, C, A. The two direct transitions between A and C pass through the molecular weight of grade B, and are labelled B: the state sequence reads A, B, C, B, A, (B), C, (B), A. .. GENERATED FROM PYTHON SOURCE LINES 58-66 .. code-block:: Python fig, axes = plt.subplots( 2, 1, figsize=(7, 4.6), sharex=True, gridspec_kw={"height_ratios": [3, 1]} ) mv.viz.plot_segmentation( X, seg, names=["$F_I$ (L/h)", "$M_n$ (kg/mol)"], ax=axes[0] ) mv.viz.plot_states(seg, ax=axes[1]) .. image-sg:: /auto_examples/11_applications/images/sphx_glr_plot_01_polymer_grades_001.png :alt: plot 01 polymer grades :srcset: /auto_examples/11_applications/images/sphx_glr_plot_01_polymer_grades_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 67-69 Which periods identify the kinetics? ------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 69-85 .. code-block:: Python names = ["kp", "kI"] p0 = np.array([MMA_PARAMETERS[k] for k in names]) S = sg.finite_difference_sensitivities( lambda p: simulate_mma_cstr(FI, params=dict(zip(names, p, strict=True))), p0, rel_step=1e-4, ) fs = sg.FisherSegmenter( n_segments=seg.n_segments, criterion="logD", window=20, initial_size=10 ).fit(S) print( fs.ranking_[["start", "stop", "logD"]] .head(5) .round(2) .to_string(index=False) ) .. rst-class:: sphx-glr-script-out .. code-block:: none start stop logD 580 610 32.96 480 510 32.80 280 480 32.39 180 210 31.42 0 110 31.31 .. GENERATED FROM PYTHON SOURCE LINES 86-89 The transitions are the informative periods; long steady operation adds little, in line with the segmentation-based experiment design of Dobos et al. (2009). .. GENERATED FROM PYTHON SOURCE LINES 89-96 .. code-block:: Python fig, ax = plt.subplots(figsize=(7, 2.4)) ax.plot(fs.criterion_, color="#009E73") ax.set_ylabel("log det $F$ (window 20)") ax.set_xlabel("sample") for c in seg.change_points: ax.axvline(c, color="gray", ls=":", lw=0.8) plt.show() .. image-sg:: /auto_examples/11_applications/images/sphx_glr_plot_01_polymer_grades_002.png :alt: plot 01 polymer grades :srcset: /auto_examples/11_applications/images/sphx_glr_plot_01_polymer_grades_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.540 seconds) .. _sphx_glr_download_auto_examples_11_applications_plot_01_polymer_grades.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/mvtsa/main?urlpath=lab/tree/notebooks/auto_examples/11_applications/plot_01_polymer_grades.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_01_polymer_grades.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_01_polymer_grades.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_01_polymer_grades.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_