Repository: PlasmaPy/PlasmaPy
Pull request #1050 (2021-02-27 20:55:50)

Catch warnings in test_proton_radiography.py
Addresses issue #844 for `proton_radiography.py`

codecov[bot]: # [Codecov](https://codecov.io/gh/PlasmaPy/PlasmaPy/pull/1050?src=pr&el=h1) Report
> Merging [#1050](https://codecov.io/gh/PlasmaPy/PlasmaPy/pull/1050?src=pr&el=desc) (6707281) into [master](https://codecov.io/gh/PlasmaPy/PlasmaPy/commit/af7b9d3b62488a5bf99f7d054a9e6bdfe17f50fd?el=desc) (af7b9d3) will **decrease** coverage by `0.59%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/PlasmaPy/PlasmaPy/pull/1050/graphs/tree.svg?width=650&height=150&src=pr&token=LzZ5mnXVad)](https://codecov.io/gh/PlasmaPy/PlasmaPy/pull/1050?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #1050      +/-   ##
==========================================
- Coverage   96.86%   96.27%   -0.60%     
==========================================
  Files          70       70              
  Lines        6864     6864              
==========================================
- Hits         6649     6608      -41     
- Misses        215      256      +41     
```


| [Impacted Files](https://codecov.io/gh/PlasmaPy/PlasmaPy/pull/1050?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [plasmapy/simulation/particletracker.py](https://codecov.io/gh/PlasmaPy/PlasmaPy/pull/1050/diff?src=pr&el=tree#diff-cGxhc21hcHkvc2ltdWxhdGlvbi9wYXJ0aWNsZXRyYWNrZXIucHk=) | `36.20% <0.00%> (-62.07%)` | :arrow_down: |
| [plasmapy/plasma/sources/plasma3d.py](https://codecov.io/gh/PlasmaPy/PlasmaPy/pull/1050/diff?src=pr&el=tree#diff-cGxhc21hcHkvcGxhc21hL3NvdXJjZXMvcGxhc21hM2QucHk=) | `93.75% <0.00%> (-6.25%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/PlasmaPy/PlasmaPy/pull/1050?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/PlasmaPy/PlasmaPy/pull/1050?src=pr&el=footer). Last update [af7b9d3...a6bc386](https://codecov.io/gh/PlasmaPy/PlasmaPy/pull/1050?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/changelog/1050.trivial.rst b/changelog/1050.trivial.rst
new file mode 100644
index 00000000..e752724e
--- /dev/null
+++ b/changelog/1050.trivial.rst
@@ -0,0 +1,1 @@
+Properly handled warnings in `test_proton_radiography.py`
diff --git a/plasmapy/diagnostics/proton_radiography.py b/plasmapy/diagnostics/proton_radiography.py
index 257b088e..fa8cbccc 100644
--- a/plasmapy/diagnostics/proton_radiography.py
+++ b/plasmapy/diagnostics/proton_radiography.py
@@ -990,8 +990,8 @@ def synthetic_radiograph(
         if percentage < 0.5:
             warnings.warn(
                 f"Only {percentage:.2%} of the particles are shown "
-                " on this synthetic radiograph. Consider increasing "
-                " the size to include more.",
+                "on this synthetic radiograph. Consider increasing "
+                "the size to include more.",
                 RuntimeWarning,
             )
 


Test patch:
diff --git a/plasmapy/diagnostics/tests/test_proton_radiography.py b/plasmapy/diagnostics/tests/test_proton_radiography.py
index 6b7e5ca8..f657c6db 100644
--- a/plasmapy/diagnostics/tests/test_proton_radiography.py
+++ b/plasmapy/diagnostics/tests/test_proton_radiography.py
@@ -123,9 +123,11 @@ def run_1D_example(name):
     source = (0 * u.mm, -10 * u.mm, 0 * u.mm)
     detector = (0 * u.mm, 200 * u.mm, 0 * u.mm)
 
-    # TODO: Catch warnings (test fields aren't well behaved at edges)
-
-    sim = prad.SyntheticProtonRadiograph(grid, source, detector, verbose=False)
+    # Expect warnings because these fields aren't well-behaved at the edges
+    with pytest.warns(
+        RuntimeWarning, match="Fields should go to zero at edges of grid to avoid "
+    ):
+        sim = prad.SyntheticProtonRadiograph(grid, source, detector, verbose=False)
     sim.create_particles(1e4, 3 * u.MeV, max_theta=0.1 * u.deg)
     sim.run()
 
@@ -226,7 +228,7 @@ def test_input_validation():
 
     # Test raises warning when one (or more) of the required fields is missing
     grid_bad = CartesianGrid(-1 * u.mm, 1 * u.mm, num=50)
-    with pytest.warns(RuntimeWarning):
+    with pytest.warns(RuntimeWarning, match="is not specified for the provided grid."):
         sim = prad.SyntheticProtonRadiograph(grid_bad, source, detector, verbose=True)
 
     # ************************************************************************
@@ -254,7 +256,9 @@ def test_input_validation():
 
     # Choose a very small synthetic radiograph size that misses most of the
     # particles
-    with pytest.warns(RuntimeWarning):
+    with pytest.warns(
+        RuntimeWarning, match="of the particles are shown on this synthetic radiograph."
+    ):
         size = np.array([[-1, 1], [-1, 1]]) * 1 * u.mm
         hax, vax, values = sim.synthetic_radiograph(size=size)
 
@@ -346,7 +350,7 @@ def test_run_options():
     # Cartesian
     source = (0 * u.mm, -10 * u.mm, 0 * u.mm)
     detector = (0 * u.mm, 200 * u.mm, 0 * u.mm)
-    sim = prad.SyntheticProtonRadiograph(grid, source, detector, verbose=False)
+    sim = prad.SyntheticProtonRadiograph(grid, source, detector, verbose=True)
 
     # Test that trying to call run() without creating particles
     # raises an exception
@@ -364,7 +368,7 @@ def test_run_options():
 
     # Test way too big of a max_theta
     sim.create_particles(1e4, 3 * u.MeV, max_theta=89 * u.deg)
-    with pytest.warns(RuntimeWarning):
+    with pytest.warns(RuntimeWarning, match="of " "particles entered the field grid"):
         sim.run(field_weighting="nearest neighbor", dt=1e-12 * u.s)
 
     # Test extreme deflections -> warns user
@@ -373,9 +377,17 @@ def test_run_options():
     grid = _test_grid("constant_bz", num=50, B0=250 * u.T)
     source = (0 * u.mm, -10 * u.mm, 0 * u.mm)
     detector = (0 * u.mm, 200 * u.mm, 0 * u.mm)
-    sim = prad.SyntheticProtonRadiograph(grid, source, detector, verbose=False)
+
+    # Expectwarnings because these fields aren't well-behaved at the edges
+    with pytest.warns(
+        RuntimeWarning, match="Fields should go to zero at edges of grid to avoid "
+    ):
+        sim = prad.SyntheticProtonRadiograph(grid, source, detector, verbose=False)
     sim.create_particles(1e4, 3 * u.MeV, max_theta=0.1 * u.deg)
-    with pytest.warns(RuntimeWarning):
+    with pytest.warns(
+        RuntimeWarning,
+        match="particles have been " "deflected away from the detector plane",
+    ):
         sim.run(field_weighting="nearest neighbor", dt=1e-12 * u.s)
     # Calc max deflection: should be between 0 and pi/2
     # Note: that's only true because max_theta is very small
@@ -406,6 +418,7 @@ def test_synthetic_radiograph():
 
 
 if __name__ == "__main__":
+    """
     test_coordinate_systems()
     test_input_validation()
     test_1D_deflections()
@@ -414,4 +427,5 @@ def test_synthetic_radiograph():
     test_load_particles()
     test_run_options()
     test_synthetic_radiograph()
+    """
     pass