Skip to content

constraints

Reporting functions for PROCESS summary plots.

plot_equality_constraint_equations(axis, m_file_data, scan)

Plot the equality constraints for a solution and their normalised residuals

Parameters:

Name Type Description Default
axis Axes
required
m_file_data MFile
required
scan int
required
Source code in process/core/io/plot/summary/reporting/constraints.py
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def plot_equality_constraint_equations(axis: plt.Axes, m_file_data: MFile, scan: int):
    """Plot the equality constraints for a solution and their normalised residuals

    Parameters
    ----------
    axis: plt.Axes :

    m_file_data: MFile :

    scan: int :

    """
    y_labels = []
    y_pos = []

    # Build a mapping from itvar index to its name (description)
    con_names = {}
    con_numbers = {}
    for var in m_file_data.data:
        if var.startswith("eq_con"):
            idx = int(var[6:])  # e.g. "itvar001" -> 1
            con_names[idx] = m_file_data.data[var].var_description
            con_numbers[idx] = idx

    for n_plot, n in enumerate(con_numbers.values()):
        # Constraint value needed
        con_value = m_file_data.data[f"val_eq_con{n:03d}"].get_scan(scan)

        # Use the variable name if available, else fallback to "eq_conXXX"
        var_label = con_names.get(n, f"eq_con{n:03d}")

        # Normalized residual of the constraint
        con_norm_residual = m_file_data.data[f"eq_con{n:03d}"].get_scan(scan)

        # Unit type of the constraint
        con_units_raw = m_file_data.data[f"eq_units_con{n:03d}"].get_scan(scan)
        con_units = str(con_units_raw).strip("'`")

        # Remove '_normalised_residue' from the label if present
        if isinstance(var_label, str) and var_label.endswith("_normalised_residue"):
            var_label = var_label.replace("_normalised_residue", "")

            # Remove trailing underscores and replace underscores between words with
            # spaces
            var_label = var_label.rstrip("_").replace("_", " ")

        # Plot the normalised residual as a bar
        axis.barh(
            n_plot,
            con_norm_residual,
            height=0.6,
            color="blue",
            label="Normalized Residual" if n_plot == 0 else "",
            align="center",
        )

        # Add the value as a number to the right of the bar
        axis.text(
            con_norm_residual + 0.52,
            n_plot,
            f"{con_norm_residual:.8g}",
            va="center",
            ha="left",
            fontsize=8,
            color="blue",
        )

        # Add the constraint value as text to the left of the y-axis
        axis.text(
            0.45,
            n_plot,
            f"{con_value:.8g} {con_units}",
            va="center",
            ha="right",
            fontsize=8,
            color="black",
        )

        y_labels.append(var_label)
        y_pos.append(n_plot)

    axis.axvline(0.5, color="red", linewidth=2, zorder=0)
    axis.set_yticks(y_pos)
    axis.set_yticklabels(y_labels)
    axis.set_facecolor("#f5f5f5")
    axis.set_xlim(-0.4, 1.2)  # Normalised bounds
    axis.set_title("Equality Constraint Equations")
    axis.set_xticks([])
    axis.legend()

plot_inequality_constraint_equations(axis, m_file, scan)

Plot the inequality constraints for a solution and where they lay within their bounds

Parameters:

Name Type Description Default
axis Axes
required
m_file MFile
required
scan int
required
Source code in process/core/io/plot/summary/reporting/constraints.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
def plot_inequality_constraint_equations(axis: plt.Axes, m_file: MFile, scan: int):
    """Plot the inequality constraints for a solution and where they lay within their
    bounds

    Parameters
    ----------
    axis: plt.Axes :

    m_file: MFile :

    scan: int :

    """
    y_labels = []
    y_pos = []

    # Build a mapping from itvar index to its name (description)
    con_names = {}
    con_numbers = {}
    for var in m_file.data:
        if var.startswith("ineq_con"):
            idx = int(var[8:])  # e.g. "ineq_con001" -> 1
            con_names[idx] = m_file.data[var].var_description
            con_numbers[idx] = idx

    for n_plot, n in enumerate(con_numbers.values()):
        # Constraint value/bound
        con_bound = m_file.data[f"ineq_bound_con{n:03d}"].get_scan(scan)

        # Value of constraint variable
        con_value = m_file.data[f"ineq_value_con{n:03d}"].get_scan(scan)

        # Constraint symbol can be `<=` for an upper limit or `>=` for a lower limit
        con_symbol = m_file.data[f"ineq_symbol_con{n:03d}"].get_scan(scan)

        # Use the variable name if available, else fallback to "ineq_conXXX"
        var_label = con_names.get(n, f"ineq_con{n:03d}")

        # Normalized residual of the constraint
        con_residual_norm = m_file.data[f"ineq_con{n:03d}"].get_scan(scan)

        # Unit type of the constraint
        con_units = m_file.data[f"ineq_units_con{n:03d}"].get_scan(scan).strip("'`")

        # Add a vertical line at the normalised constraint bounds of 0 and 1
        axis.axvline(
            0.0,
            color="red",
            linestyle="--",
            linewidth=1.5,
            zorder=0,
        )

        axis.axvline(
            1.0,
            color="red",
            linestyle="--",
            linewidth=1.5,
            zorder=0,
        )

        # Remove '_normalised_residue' from the label if present
        if isinstance(var_label, str) and var_label.endswith("_normalised_residue"):
            var_label = var_label.replace("_normalised_residue", "")
            var_label = var_label.rstrip("_").replace("_", " ")

        # Calculate the normalised constraint threshold depending if the constraint is an
        # upper
        # or lower limit
        if con_symbol == "'<='":
            normalised_value = 1 - con_residual_norm
            bar_left = normalised_value
            bar_width = 1 - normalised_value
        else:
            # For a lower limit, the normalised value is the residual itself
            normalised_value = con_residual_norm
            bar_left = 0
            # Set the bar width to be 1/10 times the normalised value,
            # but cap it at 1.0 to avoid overly long bars
            bar_width = min(normalised_value * 0.1, 1.0)

        # If the constraint value is very close to the bound then plot a square marker at
        # the bound
        if np.isclose(normalised_value, 1.0, atol=1e-3):
            axis.plot(
                1,
                n_plot,
                "s",
                color="black",
                markersize=8,
                zorder=5,
            )
        elif np.isclose(normalised_value, 0.0, atol=1e-3):
            axis.plot(
                0,
                n_plot,
                "s",
                color="black",
                markersize=8,
                zorder=5,
            )

        else:
            # If constraint value is not very close to bound then plot bar as normal
            axis.barh(
                n_plot,
                bar_width,
                left=bar_left,
                color="blue",
                edgecolor="black",
                linewidth=1.5,
                height=1.0,
                alpha=0.7,
                label="Constraint Value" if n_plot == 0 else "",
            )

        # Plot the value as a number at x = 0.5
        axis.text(
            0.5,
            n_plot,
            f"{con_value:,.8g} {con_units}",
            va="center",
            ha="center",
            fontsize=8,
            color=(
                "orange"
                if np.isclose(normalised_value, 1.0, atol=1e-3)
                or np.isclose(normalised_value, 0.0, atol=1e-3)
                else "green"
            ),
            bbox={
                "boxstyle": "round",
                "facecolor": "white",
                "alpha": 0.8,
                "edgecolor": "white",
                "linewidth": 1,
            },
        )
        # Annoate the bound value depending if it is an upper or lower limit
        if con_symbol == "'<='":
            # Add the constraint symbol and bound as text
            axis.text(
                1.02,  # Position text slightly to the right of the normalised bound
                n_plot,
                f"$\\leq$ {con_bound:,.8g} {con_units}",
                va="center",
                ha="left",
                fontsize=8,
                color="black",
            )
        else:  # con_symbol == ">="
            axis.text(
                -0.025,  # Position text slightly to the left of the normalised bound
                n_plot,
                f"$\\geq$ {con_bound:,.8g} {con_units}",
                va="center",
                ha="right",
                fontsize=8,
                color="black",
            )

        y_labels.append(var_label)
        y_pos.append(n_plot)

    axis.set_yticks(y_pos)
    axis.set_yticklabels(y_labels)
    axis.set_title("Inequality Constraint Equations")
    axis.set_xlim(-0.3, 1.275)
    axis.set_xticks([])
    axis.set_facecolor("#f5f5f5")
    axis.set_xticks(np.arange(0, 1.0, 0.1))
    axis.grid(True, axis="x", linestyle="--", alpha=0.3)
    axis.set_xticklabels([])