Skip to content

common

Common functions for PROCESS summary plots.

load_plot_image(name)

Load an image bundled with the PROCESS plotting resources.

Source code in process/core/io/plot/summary/common.py
43
44
45
46
47
def load_plot_image(name: str):
    """Load an image bundled with the PROCESS plotting resources."""
    image = resources.files("process.core.io.plot.images").joinpath(name)
    with image.open("rb") as image_file:
        return mpimg.imread(image_file)

place_plot_image(axis, name, bounds, *, zorder=10)

Place a bundled image in an inset axes and hide its frame.

Source code in process/core/io/plot/summary/common.py
50
51
52
53
54
55
def place_plot_image(axis, name: str, bounds, *, zorder: int = 10):
    """Place a bundled image in an inset axes and hide its frame."""
    image_axis = axis.inset_axes(bounds, transform=axis.transAxes, zorder=zorder)
    image_axis.imshow(load_plot_image(name))
    image_axis.axis("off")
    return image_axis

get_pulse_timings(mfile, scan)

Construct pulse timings from an MFILE scan.

Source code in process/core/io/plot/summary/common.py
58
59
60
61
62
63
64
65
66
67
68
def get_pulse_timings(mfile, scan: int) -> PulseTimings:
    """Construct pulse timings from an MFILE scan."""
    keys = (
        "t_plant_pulse_coil_precharge",
        "t_plant_pulse_plasma_current_ramp_up",
        "t_plant_pulse_fusion_ramp",
        "t_plant_pulse_burn",
        "t_plant_pulse_plasma_current_ramp_down",
        "t_plant_pulse_dwell",
    )
    return PulseTimings(**{key: mfile.get(key, scan=scan) for key in keys})

box_style(colour, alpha=1.0, linewidth=2, **kwargs)

Matplotlib boxstyle

Source code in process/core/io/plot/summary/common.py
71
72
73
74
75
76
77
78
79
def box_style(colour: str, alpha: float = 1.0, linewidth: float | None = 2, **kwargs):
    """Matplotlib boxstyle"""
    return {
        "boxstyle": "round",
        "facecolor": colour,
        "alpha": alpha,
        "linewidth": linewidth,
        **kwargs,
    }

text_layout(fig, h_align='left', v_align='bottom')

Matplotlib text layout

Source code in process/core/io/plot/summary/common.py
82
83
84
85
86
87
88
89
def text_layout(fig, h_align="left", v_align="bottom"):
    """Matplotlib text layout"""
    return {
        "fontsize": 9,
        "verticalalignment": v_align,
        "horizontalalignment": h_align,
        "transform": fig.transFigure,
    }

setup_axis(axis, xmin, xmax, ymin, ymax)

Matplotlib axis setup

Source code in process/core/io/plot/summary/common.py
92
93
94
95
96
97
98
def setup_axis(axis, xmin, xmax, ymin, ymax):
    """Matplotlib axis setup"""
    axis.set_ylim(ymin, ymax)
    axis.set_xlim(xmin, xmax)
    axis.set_axis_off()
    axis.set_autoscaley_on(False)
    axis.set_autoscalex_on(False)

add_colourbar(contour_fill, axis, colourbar_axis)

Add colourbar to axis

Source code in process/core/io/plot/summary/common.py
101
102
103
104
105
106
def add_colourbar(contour_fill, axis, colourbar_axis):
    """Add colourbar to axis"""
    # Use a dedicated colorbar axes when provided so the main axes width is unchanged.
    if colourbar_axis is None:
        return axis.figure.colorbar(contour_fill, ax=axis, pad=0.02)
    return axis.figure.colorbar(contour_fill, cax=colourbar_axis)

add_panel(axis, x, top, width, height, text, style, text_x_offset=0.004, fontsize=9, transform=None, text_position=None, linespacing=1.1, text_kwargs=None)

Add one fixed-size box and its unchanged original text.

Source code in process/core/io/plot/summary/common.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
def add_panel(
    axis,
    x: float,
    top: float,
    width: float,
    height: float,
    text: str,
    style: dict,
    text_x_offset: float = 0.004,
    fontsize: float = 9,
    transform=None,
    text_position=None,
    linespacing: float = 1.1,
    text_kwargs=None,
):
    """Add one fixed-size box and its unchanged original text."""
    box_expansion = 0.002
    left = x - box_expansion
    bottom = top - height - box_expansion
    expanded_width = width + 2 * box_expansion
    expanded_height = height + 2 * box_expansion
    fig = axis.get_figure()
    transform = fig.transFigure if transform is None else transform
    patch = FancyBboxPatch(
        (left, bottom),
        expanded_width,
        expanded_height,
        boxstyle="round,pad=0,rounding_size=0.003",
        transform=transform,
        facecolor=style.get("facecolor", "white"),
        edgecolor=style.get("edgecolor", "black"),
        linewidth=style.get("linewidth", 1.1),
        linestyle=style.get("linestyle", "solid"),
        alpha=style.get("alpha", 1.0),
        clip_on=False,
        zorder=20,
    )
    axis.add_patch(patch)

    text_kwargs = text_kwargs or {}
    for reserved_key in ("clip_on", "zorder", "linespacing"):
        text_kwargs.pop(reserved_key, None)
    text_x, text_y = (
        (x + text_x_offset, top - 0.004) if text_position is None else text_position
    )
    return axis.text(
        text_x,
        text_y,
        text,
        clip_on=False,
        zorder=21,
        linespacing=linespacing,
        **{
            **text_layout(fig, v_align="top"),
            "fontsize": fontsize,
            "transform": transform,
            "color": "black",
            **text_kwargs,
        },
    )

add_symbol(axis, panel_name, x, top)

Add a large symbol at its original figure-relative position.

Source code in process/core/io/plot/summary/common.py
171
172
173
174
175
176
177
178
179
180
181
182
183
def add_symbol(axis, panel_name: str, x: float, top: float):
    """Add a large symbol at its original figure-relative position."""
    return axis.text(
        x,
        top,
        panel_name,
        fontsize=23,
        color="black",
        verticalalignment="top",
        transform=axis.get_figure().transFigure,
        clip_on=False,
        zorder=22,
    )

color_key(axis, mfile, scan, colour_scheme)

Function to plot the colour key

Source code in process/core/io/plot/summary/common.py
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
def color_key(axis: Axes, mfile: MFile, scan: int, colour_scheme: Literal[1, 2]):
    """Function to plot the colour key"""
    axis.set_ylim(0, 10)
    axis.set_xlim(0, 10)
    axis.set_axis_off()
    axis.set_autoscaley_on(False)
    axis.set_autoscalex_on(False)

    labels = [
        ("CS coil", SOLENOID_COLOUR[colour_scheme - 1]),
        ("CS comp", CSCOMPRESSION_COLOUR[colour_scheme - 1]),
        (
            "TF coil",
            (
                TFC_COLOUR[colour_scheme - 1]
                if mfile.get("i_tf_sup", scan=scan) != 0
                else "#b87333"
            ),
        ),
        ("Thermal shield", THERMAL_SHIELD_COLOUR[colour_scheme - 1]),
        ("VV & shield", VESSEL_COLOUR[colour_scheme - 1]),
        ("Blanket", BLANKET_COLOUR[colour_scheme - 1]),
        ("First wall", FIRSTWALL_COLOUR[colour_scheme - 1]),
        ("Plasma", PLASMA_COLOUR[colour_scheme - 1]),
        ("PF coils", "none"),
        ("Divertor", "black"),
    ]

    if (mfile.get("i_hcd_primary", scan=scan) in {5, 8}) or (
        mfile.get("i_hcd_secondary", scan=scan) in {5, 8}
    ):
        labels.extend((
            ("NB duct shield", NBSHIELD_COLOUR[colour_scheme - 1]),
            ("Cryostat", CRYOSTAT_COLOUR[colour_scheme - 1]),
        ))
    else:
        labels.append(("Cryostat", CRYOSTAT_COLOUR[colour_scheme - 1]))

    for i, (text, color) in enumerate(labels):
        row = i // 4
        col = i % 4
        y_pos = 9 - row * 1.5
        x_pos = col * 2.5

        axis.text(x_pos, y_pos, text, ha="left", va="top", size="small")
        axis.add_patch(
            patches.Rectangle(
                (x_pos + 1.5, y_pos - 0.35),
                0.5,
                0.4,
                lw=0 if color != "none" else 1,
                facecolor=color if color != "none" else "none",
                edgecolor="black" if color == "none" else "none",
            )
        )