Skip to content

Commit a846c9b

Browse files
f0uriestdpaniciYigitElma
authored
Add pass through kwargs from plotting to compute (#1915)
- Only adds `compute_kwargs` option to "generic" plotting functions `plot_{1d,2d,3d,section,fsa}`, the other ones generally already accept kwargs relevant to their function (eg `plot_boozer_modes`) - No tests yet, as I can't think of any that would be meaningful until #1899 is resolved. Resolves #1352 --------- Co-authored-by: Dario Panici <37969854+dpanici@users.noreply.github.com> Co-authored-by: Yigit Gunsur Elmacioglu <102380275+YigitElma@users.noreply.github.com>
1 parent d65aabf commit a846c9b

1 file changed

Lines changed: 118 additions & 15 deletions

File tree

desc/plotting.py

Lines changed: 118 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,14 @@ def _get_plot_axes(grid):
266266
return tuple(plot_axes)
267267

268268

269-
def _compute(eq, name, grid, component=None, reshape=True):
269+
def _compute(
270+
eq,
271+
name,
272+
grid,
273+
component=None,
274+
reshape=True,
275+
compute_kwargs=None,
276+
):
270277
"""Compute quantity specified by name on grid for Equilibrium eq.
271278
272279
Parameters
@@ -279,6 +286,8 @@ def _compute(eq, name, grid, component=None, reshape=True):
279286
Grid of coordinates to evaluate at.
280287
component : str, optional
281288
For vector variables, which element to plot. Default is the norm of the vector.
289+
compute_kwargs : dict, optional
290+
Additional keyword arguments to pass to ``eq.compute``
282291
283292
Returns
284293
-------
@@ -302,7 +311,9 @@ def _compute(eq, name, grid, component=None, reshape=True):
302311

303312
label = data_index[parameterization][name]["label"]
304313

305-
data = eq.compute(name, grid=grid)[name]
314+
compute_kwargs = setdefault(compute_kwargs, {})
315+
316+
data = eq.compute(name, grid=grid, **compute_kwargs)[name]
306317

307318
if data_index[parameterization][name]["dim"] > 1:
308319
if component is None:
@@ -505,7 +516,15 @@ def plot_coefficients(eq, L=True, M=True, N=True, ax=None, **kwargs):
505516

506517

507518
def plot_1d( # noqa : C901
508-
eq, name, grid=None, log=False, normalize=None, ax=None, return_data=False, **kwargs
519+
eq,
520+
name,
521+
grid=None,
522+
log=False,
523+
normalize=None,
524+
ax=None,
525+
return_data=False,
526+
compute_kwargs=None,
527+
**kwargs,
509528
):
510529
"""Plot 1D profiles.
511530
@@ -525,6 +544,8 @@ def plot_1d( # noqa : C901
525544
Axis to plot on.
526545
return_data : bool
527546
If True, return the data plotted as well as fig,ax
547+
compute_kwargs : dict, optional
548+
Additional keyword arguments to pass to ``eq.compute``.
528549
**kwargs : dict, optional
529550
Specify properties of the figure, axis, and plot appearance e.g.::
530551
@@ -587,6 +608,7 @@ def plot_1d( # noqa : C901
587608
ax=ax,
588609
return_data=return_data,
589610
grid=grid,
611+
compute_kwargs=compute_kwargs,
590612
**kwargs,
591613
)
592614
rho = grid.nodes[:, 0]
@@ -601,6 +623,7 @@ def plot_1d( # noqa : C901
601623
ax=ax,
602624
return_data=return_data,
603625
grid=grid,
626+
compute_kwargs=compute_kwargs,
604627
**kwargs,
605628
)
606629

@@ -614,11 +637,18 @@ def plot_1d( # noqa : C901
614637
plot_axes = _get_plot_axes(grid)
615638

616639
data, ylabel = _compute(
617-
eq, name, grid, kwargs.pop("component", None), reshape=False
640+
eq,
641+
name,
642+
grid,
643+
kwargs.pop("component", None),
644+
reshape=False,
645+
compute_kwargs=compute_kwargs,
618646
)
619647

620648
if normalize:
621-
norm_data, _ = _compute(eq, normalize, grid, reshape=False)
649+
norm_data, _ = _compute(
650+
eq, normalize, grid, compute_kwargs=compute_kwargs, reshape=False
651+
)
622652
data = data / np.nanmean(np.abs(norm_data)) # normalize
623653

624654
# reshape data to 1D
@@ -688,7 +718,15 @@ def plot_1d( # noqa : C901
688718

689719

690720
def plot_2d( # noqa : C901
691-
eq, name, grid=None, log=False, normalize=None, ax=None, return_data=False, **kwargs
721+
eq,
722+
name,
723+
grid=None,
724+
log=False,
725+
normalize=None,
726+
ax=None,
727+
return_data=False,
728+
compute_kwargs=None,
729+
**kwargs,
692730
):
693731
"""Plot 2D cross-sections.
694732
@@ -708,6 +746,8 @@ def plot_2d( # noqa : C901
708746
Axis to plot on.
709747
return_data : bool
710748
If True, return the data plotted as well as fig,ax
749+
compute_kwargs : dict, optional
750+
Additional keyword arguments to pass to ``eq.compute``.
711751
**kwargs : dict, optional
712752
Specify properties of the figure, axis, and plot appearance e.g.::
713753
@@ -787,6 +827,7 @@ def plot_2d( # noqa : C901
787827
name,
788828
grid,
789829
component=component,
830+
compute_kwargs=compute_kwargs,
790831
)
791832
else:
792833
data, label = _compute_Bn(
@@ -802,7 +843,13 @@ def plot_2d( # noqa : C901
802843
divider = make_axes_locatable(ax)
803844

804845
if normalize:
805-
norm_data, _ = _compute(eq, normalize, grid, reshape=False)
846+
norm_data, _ = _compute(
847+
eq,
848+
normalize,
849+
grid,
850+
reshape=False,
851+
compute_kwargs=compute_kwargs,
852+
)
806853
data = data / np.nanmean(np.abs(norm_data)) # normalize
807854

808855
# reshape data to 2D
@@ -954,6 +1001,7 @@ def plot_3d( # noqa : C901
9541001
normalize=None,
9551002
fig=None,
9561003
return_data=False,
1004+
compute_kwargs=None,
9571005
**kwargs,
9581006
):
9591007
"""Plot 3D surfaces.
@@ -974,6 +1022,8 @@ def plot_3d( # noqa : C901
9741022
Figure to plot on.
9751023
return_data : bool
9761024
If True, return the data plotted as well as fig,ax
1025+
compute_kwargs : dict, optional
1026+
Additional keyword arguments to pass to ``eq.compute``.
9771027
**kwargs : dict, optional
9781028
Specify properties of the figure, axis, and plot appearance e.g.::
9791029
@@ -1061,6 +1111,7 @@ def plot_3d( # noqa : C901
10611111
name,
10621112
grid,
10631113
component=component,
1114+
compute_kwargs=compute_kwargs,
10641115
)
10651116
else:
10661117
data, label = _compute_Bn(
@@ -1224,6 +1275,7 @@ def plot_fsa( # noqa: C901
12241275
ax=None,
12251276
return_data=False,
12261277
grid=None,
1278+
compute_kwargs=None,
12271279
**kwargs,
12281280
):
12291281
"""Plot flux surface averages of quantities.
@@ -1262,6 +1314,8 @@ def plot_fsa( # noqa: C901
12621314
grid : _Grid
12631315
Grid to compute name on. If provided, the parameters
12641316
``rho``, ``M``, and ``N`` are ignored.
1317+
compute_kwargs : dict, optional
1318+
Additional keyword arguments to pass to ``eq.compute``.
12651319
**kwargs : dict, optional
12661320
Specify properties of the figure, axis, and plot appearance e.g.::
12671321
@@ -1354,7 +1408,12 @@ def plot_fsa( # noqa: C901
13541408
# desired surface average.
13551409
name = "<" + name + ">"
13561410
values, ylabel = _compute(
1357-
eq, name, grid, kwargs.pop("component", None), reshape=False
1411+
eq,
1412+
name,
1413+
grid,
1414+
kwargs.pop("component", None),
1415+
reshape=False,
1416+
compute_kwargs=compute_kwargs,
13581417
)
13591418
ylabel = ylabel.split("~")
13601419
if (
@@ -1371,7 +1430,13 @@ def plot_fsa( # noqa: C901
13711430
else:
13721431
compute_surface_averages = surface_averages_map(grid, expand_out=False)
13731432
if with_sqrt_g: # flux surface average
1374-
sqrt_g = _compute(eq, "sqrt(g)", grid, reshape=False)[0]
1433+
sqrt_g = _compute(
1434+
eq,
1435+
"sqrt(g)",
1436+
grid,
1437+
reshape=False,
1438+
compute_kwargs=compute_kwargs,
1439+
)[0]
13751440
# Attempt to compute the magnetic axis limit.
13761441
# Compute derivative depending on various naming schemes.
13771442
# e.g. B -> B_r, V(r) -> V_r(r), S_r(r) -> S_rr(r)
@@ -1384,7 +1449,13 @@ def plot_fsa( # noqa: C901
13841449
)
13851450
values_r = next(
13861451
(
1387-
_compute(eq, x, grid, reshape=False)[0]
1452+
_compute(
1453+
eq,
1454+
x,
1455+
grid,
1456+
reshape=False,
1457+
compute_kwargs=compute_kwargs,
1458+
)[0]
13881459
for x in schemes
13891460
if x in data_index[p]
13901461
),
@@ -1393,7 +1464,10 @@ def plot_fsa( # noqa: C901
13931464
if (np.isfinite(values) & np.isfinite(values_r))[grid.axis].all():
13941465
# Otherwise cannot compute axis limit in this agnostic manner.
13951466
sqrt_g = grid.replace_at_axis(
1396-
sqrt_g, _compute(eq, "sqrt(g)_r", grid, reshape=False)[0], copy=True
1467+
sqrt_g,
1468+
_compute(eq, "sqrt(g)_r", grid, reshape=False)[0],
1469+
copy=True,
1470+
compute_kwargs=compute_kwargs,
13971471
)
13981472
averages = compute_surface_averages(values, sqrt_g=sqrt_g)
13991473
ylabel = r"$\langle " + ylabel[0][1:] + r" \rangle~" + "~".join(ylabel[1:])
@@ -1413,7 +1487,13 @@ def plot_fsa( # noqa: C901
14131487
plot_data_ylabel_key = f"<{name}>_fsa"
14141488

14151489
if normalize:
1416-
norm_data = _compute(eq, normalize, grid, reshape=False)[0]
1490+
norm_data = _compute(
1491+
eq,
1492+
normalize,
1493+
grid,
1494+
reshape=False,
1495+
compute_kwargs=compute_kwargs,
1496+
)[0]
14171497
values = values / np.nanmean(np.abs(norm_data)) # normalize
14181498
if log:
14191499
values = np.abs(values) # ensure data is positive for log plot
@@ -1455,7 +1535,15 @@ def plot_fsa( # noqa: C901
14551535

14561536

14571537
def plot_section(
1458-
eq, name, grid=None, log=False, normalize=None, ax=None, return_data=False, **kwargs
1538+
eq,
1539+
name,
1540+
grid=None,
1541+
log=False,
1542+
normalize=None,
1543+
ax=None,
1544+
return_data=False,
1545+
compute_kwargs=None,
1546+
**kwargs,
14591547
):
14601548
"""Plot Poincare sections.
14611549
@@ -1475,6 +1563,8 @@ def plot_section(
14751563
Axis to plot on.
14761564
return_data : bool
14771565
If True, return the data plotted as well as fig,ax
1566+
compute_kwargs : dict, optional
1567+
Additional keyword arguments to pass to ``eq.compute``.
14781568
**kwargs : dict, optional
14791569
Specify properties of the figure, axis, and plot appearance e.g.::
14801570
@@ -1584,9 +1674,22 @@ def plot_section(
15841674
rows = np.floor(np.sqrt(nphi)).astype(int)
15851675
cols = np.ceil(nphi / rows).astype(int)
15861676

1587-
data, _ = _compute(eq, name, grid, kwargs.pop("component", None), reshape=False)
1677+
data, _ = _compute(
1678+
eq,
1679+
name,
1680+
grid,
1681+
kwargs.pop("component", None),
1682+
reshape=False,
1683+
compute_kwargs=compute_kwargs,
1684+
)
15881685
if normalize:
1589-
norm_data, _ = _compute(eq, normalize, grid, reshape=False)
1686+
norm_data, _ = _compute(
1687+
eq,
1688+
normalize,
1689+
grid,
1690+
reshape=False,
1691+
compute_kwargs=compute_kwargs,
1692+
)
15901693
data = data / np.nanmean(np.abs(norm_data)) # normalize
15911694

15921695
figw = 5 * cols

0 commit comments

Comments
 (0)