Skip to content

Commit b154c0d

Browse files
authored
Merge pull request #245 from PlasmaControl/plot_kwargs
Documents kwargs for plotting functions and adds sensible kwargs to those that were lacking them (such as figsize) - Document kwargs, according to [matplotlib-style documentation](https://stackoverflow.com/questions/62511086/how-to-document-kwargs-according-to-numpy-style-docstring) - add kwargs to plotting functions missing sensible/useful kwargs - add check for unused kwargs to most plotting functions - add plot comparisons to artifact uploaded to github - bump `pytest-mpl` version to fix [bug](matplotlib/pytest-mpl#171) where if `unittest.TestCase` is used, it did not actually run those tests correctly resolves #178 , #244
2 parents 4f2c9b4 + 09fcbc3 commit b154c0d

16 files changed

+432
-102
lines changed

.github/workflows/pytest.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ jobs:
3939
run: |
4040
pwd
4141
lscpu
42-
python -m pytest --durations=0 --cov-report xml:cov.xml --cov-config=setup.cfg --cov=desc/ --mpl
43-
- name: save coverage file
42+
python -m pytest --durations=0 --cov-report xml:cov.xml --cov-config=setup.cfg --cov=desc/ --mpl --mpl-results-path=mpl_results.html --mpl-generate-summary=html
43+
- name: save coverage file and plot comparison results
4444
uses: actions/upload-artifact@v2
4545
with:
4646
name: my-artifact
47-
path: ./cov.xml
47+
path: |
48+
./cov.xml
49+
./mpl_results.html
4850
- name: Upload coverage
4951
uses: codecov/codecov-action@v2
5052
with:

desc/plotting.py

Lines changed: 399 additions & 78 deletions
Large diffs are not rendered by default.

devtools/dev-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pylint >= 2.12.2
1818
pytest >= 5.0.0
1919
pytest-cov >= 2.6.0
2020
codecov
21-
pytest-mpl == 0.16.0
21+
pytest-mpl == 0.16.1
2222
pytest-benchmark
2323
markupsafe==2.0.1
2424
nbmake

docs/theory_general.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ The parameter vector :math:`\mathbf{c}` contains the spectral coefficients of th
116116
.. math::
117117
\mathbf{c} = [R^b_{mn} \\ Z^b_{mn} \\ p_l \\ \iota_l \\ \psi_a]^T
118118
119-
The equations :math:`\mathbf{f}` are the force error residuals at a series of collocation points, as well as additional equations to enforce the boundary condition:
119+
The equations :math:`\mathbf{f}` are the force error residuals at a series of collocation points:
120120

121121
.. math::
122-
\mathbf{f} = [f_\rho \\ f_\beta \\ BC]^T
122+
\mathbf{f} = [f_\rho \\ f_\beta]^T
123123
124-
DESC allows flexibility in the choice of optimization algorithm used to solve this system of equations; popular approaches include Newton-Raphson methods and least-squares minimization.
124+
DESC allows flexibility in the choice of optimization algorithm used to solve this system of equations; popular approaches include Newton-Raphson methods and least-squares minimization (as the collocation grids are often oversampled, which has been found to improve convergence and robustness).

tests/baseline/test_2d_logF.png

680 Bytes
Loading
Loading
62.2 KB
Loading
Loading
21.2 KB
Loading
22.3 KB
Loading
22 KB
Loading
22.2 KB
Loading
23 KB
Loading

tests/baseline/test_plot_grid_ocs.png

22 KB
Loading
46.9 KB
Loading

tests/test_plotting.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,43 @@
2727
DoubleFourierSeries,
2828
FourierZernikeBasis,
2929
)
30-
from desc.equilibrium import EquilibriaFamily
30+
from desc.equilibrium import EquilibriaFamily, Equilibrium
3131
from desc.coils import FourierXYZCoil, CoilSet
3232

3333

34+
def test_kwarg_warning(DummyStellarator):
35+
eq = Equilibrium.load(load_from=str(DummyStellarator["output_path"]))
36+
with pytest.raises(AssertionError):
37+
fig, ax = plot_1d(eq, "p", not_a_kwarg=True)
38+
return None
39+
40+
3441
@pytest.mark.mpl_image_compare(tolerance=50)
3542
def test_1d_p(SOLOVEV):
3643
eq = EquilibriaFamily.load(load_from=str(SOLOVEV["desc_h5_path"]))[-1]
37-
fig, ax = plot_1d(eq, "p")
44+
fig, ax = plot_1d(eq, "p", figsize=(4, 4))
3845
return fig
3946

4047

4148
@pytest.mark.mpl_image_compare(tolerance=50)
4249
def test_1d_dpdr(SOLOVEV):
4350
eq = EquilibriaFamily.load(load_from=str(SOLOVEV["desc_h5_path"]))[-1]
44-
fig, ax = plot_1d(eq, "p_r")
51+
fig, ax = plot_1d(eq, "p_r", figsize=(4, 4))
4552
return fig
4653

4754

4855
@pytest.mark.mpl_image_compare(tolerance=50)
4956
def test_1d_iota(SOLOVEV):
5057
eq = EquilibriaFamily.load(load_from=str(SOLOVEV["desc_h5_path"]))[-1]
5158
grid = LinearGrid(rho=0.5, theta=100, zeta=0.0)
52-
fig, ax = plot_1d(eq, "iota", grid=grid)
59+
fig, ax = plot_1d(eq, "iota", grid=grid, figsize=(4, 4))
5360
return fig
5461

5562

5663
@pytest.mark.mpl_image_compare(tolerance=50)
5764
def test_1d_logpsi(SOLOVEV):
5865
eq = EquilibriaFamily.load(load_from=str(SOLOVEV["desc_h5_path"]))[-1]
59-
fig, ax = plot_1d(eq, "psi", log=True)
66+
fig, ax = plot_1d(eq, "psi", log=True, figsize=(4, 4))
6067
return fig
6168

6269

@@ -65,30 +72,30 @@ def test_2d_logF(SOLOVEV):
6572
# plot test is inconsistent
6673
eq = EquilibriaFamily.load(load_from=str(SOLOVEV["desc_h5_path"]))[-1]
6774
grid = LinearGrid(rho=100, theta=100, zeta=0.0)
68-
fig, ax = plot_2d(eq, "|F|", log=True, grid=grid)
75+
fig, ax = plot_2d(eq, "|F|", log=True, grid=grid, figsize=(4, 4))
6976
return fig
7077

7178

7279
@pytest.mark.mpl_image_compare(tolerance=50)
7380
def test_2d_g_tz(SOLOVEV):
7481
eq = EquilibriaFamily.load(load_from=str(SOLOVEV["desc_h5_path"]))[-1]
7582
grid = LinearGrid(rho=0.5, theta=100, zeta=100)
76-
fig, ax = plot_2d(eq, "sqrt(g)", grid=grid)
83+
fig, ax = plot_2d(eq, "sqrt(g)", grid=grid, figsize=(4, 4))
7784
return fig
7885

7986

8087
@pytest.mark.mpl_image_compare(tolerance=50)
8188
def test_2d_g_rz(SOLOVEV):
8289
eq = EquilibriaFamily.load(load_from=str(SOLOVEV["desc_h5_path"]))[-1]
8390
grid = LinearGrid(rho=100, theta=0.0, zeta=100)
84-
fig, ax = plot_2d(eq, "sqrt(g)", grid=grid)
91+
fig, ax = plot_2d(eq, "sqrt(g)", grid=grid, figsize=(4, 4))
8592
return fig
8693

8794

8895
@pytest.mark.mpl_image_compare(tolerance=50)
8996
def test_2d_lambda(SOLOVEV):
9097
eq = EquilibriaFamily.load(load_from=str(SOLOVEV["desc_h5_path"]))[-1]
91-
fig, ax = plot_2d(eq, "lambda")
98+
fig, ax = plot_2d(eq, "lambda", figsize=(4, 4))
9299
return fig
93100

94101

@@ -215,42 +222,42 @@ def test_plot_comparison_no_theta(DSHAPE):
215222
@pytest.mark.mpl_image_compare(tolerance=50)
216223
def test_plot_con_basis(SOLOVEV):
217224
eq = EquilibriaFamily.load(load_from=str(SOLOVEV["desc_h5_path"]))[-1]
218-
fig, ax = plot_2d(eq, "e^rho", component="R")
225+
fig, ax = plot_2d(eq, "e^rho", component="R", figsize=(4, 4))
219226
return fig
220227

221228

222229
@pytest.mark.mpl_image_compare(tolerance=50)
223230
def test_plot_cov_basis(SOLOVEV):
224231
eq = EquilibriaFamily.load(load_from=str(SOLOVEV["desc_h5_path"]))[-1]
225-
fig, ax = plot_2d(eq, "e_rho")
232+
fig, ax = plot_2d(eq, "e_rho", figsize=(4, 4))
226233
return fig
227234

228235

229236
@pytest.mark.mpl_image_compare(tolerance=50)
230237
def test_plot_magnetic_tension(SOLOVEV):
231238
eq = EquilibriaFamily.load(load_from=str(SOLOVEV["desc_h5_path"]))[-1]
232-
fig, ax = plot_2d(eq, "|(B*grad)B|")
239+
fig, ax = plot_2d(eq, "|(B*grad)B|", figsize=(4, 4))
233240
return fig
234241

235242

236243
@pytest.mark.mpl_image_compare(tolerance=50)
237244
def test_plot_magnetic_pressure(SOLOVEV):
238245
eq = EquilibriaFamily.load(load_from=str(SOLOVEV["desc_h5_path"]))[-1]
239-
fig, ax = plot_2d(eq, "|grad(|B|^2)|/2mu0")
246+
fig, ax = plot_2d(eq, "|grad(|B|^2)|/2mu0", figsize=(4, 4))
240247
return fig
241248

242249

243250
@pytest.mark.mpl_image_compare(tolerance=50)
244251
def test_plot_gradpsi(SOLOVEV):
245252
eq = EquilibriaFamily.load(load_from=str(SOLOVEV["desc_h5_path"]))[-1]
246-
fig, ax = plot_2d(eq, "|grad(rho)|")
253+
fig, ax = plot_2d(eq, "|grad(rho)|", figsize=(4, 4))
247254
return fig
248255

249256

250257
@pytest.mark.mpl_image_compare(tolerance=55)
251258
def test_plot_normF_2d(SOLOVEV):
252259
eq = EquilibriaFamily.load(load_from=str(SOLOVEV["desc_h5_path"]))[-1]
253-
fig, ax = plot_2d(eq, "|F|", norm_F=True)
260+
fig, ax = plot_2d(eq, "|F|", norm_F=True, figsize=(4, 4))
254261
return fig
255262

256263

@@ -284,7 +291,7 @@ def test_plot_grid_linear(self):
284291
@pytest.mark.mpl_image_compare(tolerance=50)
285292
def test_plot_grid_quad(self):
286293
grid = QuadratureGrid(L=10, M=10, N=1)
287-
fig, ax = plot_grid(grid)
294+
fig, ax = plot_grid(grid, figsize=(6, 6))
288295
return fig
289296

290297
@pytest.mark.mpl_image_compare(tolerance=50)
@@ -381,7 +388,7 @@ def test_plot_boozer_modes(SOLOVEV):
381388
@pytest.mark.mpl_image_compare(tolerance=50)
382389
def test_plot_boozer_surface(SOLOVEV):
383390
eq = EquilibriaFamily.load(load_from=str(SOLOVEV["desc_h5_path"]))[-1]
384-
fig, ax = plot_boozer_surface(eq)
391+
fig, ax = plot_boozer_surface(eq, figsize=(4, 4))
385392
return fig
386393

387394

0 commit comments

Comments
 (0)