Skip to content

Commit

Permalink
Fix requirements conflict and improve plt.show() patching (#481)
Browse files Browse the repository at this point in the history
* Properly closing open figures

* Prevent matplolib x numpy conflict

* Monkeypatching plt.show() calls

* Delete import
  • Loading branch information
HGSilveri authored Mar 22, 2023
1 parent bb62296 commit c8c12b2
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 138 deletions.
3 changes: 2 additions & 1 deletion pulser-core/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
jsonschema
matplotlib
numpy >= 1.20
# Numpy 1.20 introduces type hints, 1.24.0 breaks matplotlib < 3.6.1
numpy >= 1.20, != 1.24.0
scipy
backports.cached-property; python_version == '3.7'
typing-extensions; python_version == '3.7'
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import matplotlib.pyplot as plt
import numpy as np
import pytest

Expand Down Expand Up @@ -72,3 +73,11 @@ def mod_device() -> Device:
),
),
)


@pytest.fixture()
def patch_plt_show(monkeypatch):
# Close residual figures
plt.close("all")
# Closes a figure instead of showing it
monkeypatch.setattr(plt, "show", plt.close)
12 changes: 5 additions & 7 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from unittest.mock import patch

import numpy as np
import pytest
Expand Down Expand Up @@ -131,7 +130,7 @@ def test_mappable_register():
assert not new_mapped_seq.is_register_mappable()


def test_rare_cases():
def test_rare_cases(patch_plt_show):
reg = Register.square(4)
seq = Sequence(reg, Chadoq2)
var = seq.declare_variable("var")
Expand All @@ -158,11 +157,10 @@ def test_rare_cases():
assert wf_.build() == BlackmanWaveform(100, 10)
with pytest.warns(UserWarning, match="Serialization of 'getattr'"):
draw_func = wf_.draw
with patch("matplotlib.pyplot.show"):
with pytest.warns(
UserWarning, match="Calls to methods of parametrized objects"
):
draw_func().build()
with pytest.warns(
UserWarning, match="Calls to methods of parametrized objects"
):
draw_func().build()

rotated_reg = parametrize(Register.rotate)(reg, var)
with pytest.raises(NotImplementedError):
Expand Down
8 changes: 2 additions & 6 deletions tests/test_pulse.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from unittest.mock import patch

import numpy as np
import pytest

Expand Down Expand Up @@ -74,10 +71,9 @@ def test_repr():
assert pls_.__repr__() == msg


def test_draw():
def test_draw(patch_plt_show):
pls_ = Pulse.ConstantDetuning(bwf, -10, 1, post_phase_shift=-np.pi)
with patch("matplotlib.pyplot.show"):
pls_.draw()
pls_.draw()


def test_fall_time():
Expand Down
86 changes: 37 additions & 49 deletions tests/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import numpy as np
import pytest
from matplotlib import pyplot as plt

from pulser import Register, Register3D
from pulser.devices import Chadoq2, MockDevice
Expand Down Expand Up @@ -289,31 +288,27 @@ def test_rotation():


@pytest.mark.parametrize("draw_params", draw_params)
def test_drawing(draw_params):
def test_drawing(draw_params, patch_plt_show):
with pytest.raises(ValueError, match="Blockade radius"):
reg = Register.from_coordinates([(1, 0), (0, 1)])
reg.draw(blockade_radius=0.0, draw_half_radius=True, **draw_params)

reg = Register.from_coordinates([(1, 0), (0, 1)])
with patch("matplotlib.pyplot.show"):
reg.draw(blockade_radius=0.1, draw_graph=True, **draw_params)
reg.draw(blockade_radius=0.1, draw_graph=True, **draw_params)

reg = Register.triangular_lattice(3, 8)
with patch("matplotlib.pyplot.show"):
reg.draw(**draw_params)
reg.draw(**draw_params)

with patch("matplotlib.pyplot.show"):
with patch("matplotlib.pyplot.savefig"):
reg.draw(fig_name="my_register.pdf", **draw_params)
with patch("matplotlib.pyplot.savefig"):
reg.draw(fig_name="my_register.pdf")

reg = Register.rectangle(1, 8)
with patch("matplotlib.pyplot.show"):
reg.draw(
blockade_radius=5,
draw_half_radius=True,
draw_graph=True,
**draw_params,
)
reg.draw(
blockade_radius=5,
draw_half_radius=True,
draw_graph=True,
**draw_params,
)

with pytest.raises(ValueError, match="'blockade_radius' to draw."):
reg.draw(draw_half_radius=True, **draw_params)
Expand All @@ -322,8 +317,6 @@ def test_drawing(draw_params):
with pytest.raises(NotImplementedError, match="Needs more than one atom"):
reg.draw(blockade_radius=5, draw_half_radius=True, **draw_params)

plt.close("all")


def test_orthorombic():
# Check rows
Expand Down Expand Up @@ -354,54 +347,49 @@ def test_cubic():


@pytest.mark.parametrize("draw_params", draw_params)
def test_drawing3D(draw_params):
def test_drawing3D(draw_params, patch_plt_show):
with pytest.raises(ValueError, match="Blockade radius"):
reg = Register3D.from_coordinates([(1, 0, 0), (0, 0, 1)])
reg.draw(blockade_radius=0.0, **draw_params)

reg = Register3D.cubic(3, 8)
with patch("matplotlib.pyplot.show"):
with patch("matplotlib.pyplot.savefig"):
reg.draw(fig_name="my_register.pdf", **draw_params)

with patch("matplotlib.pyplot.savefig"):
reg.draw(fig_name="my_register.pdf", **draw_params)

reg = Register3D.cuboid(1, 8, 2)
with patch("matplotlib.pyplot.show"):
reg.draw(
blockade_radius=5,
draw_half_radius=True,
draw_graph=True,
**draw_params,
)
reg.draw(
blockade_radius=5,
draw_half_radius=True,
draw_graph=True,
**draw_params,
)

with pytest.raises(ValueError, match="'blockade_radius' to draw."):
reg.draw(draw_half_radius=True, **draw_params)

reg = Register3D.cuboid(2, 2, 2)
with patch("matplotlib.pyplot.show"):
reg.draw(
blockade_radius=5,
draw_half_radius=True,
draw_graph=True,
projection=False,
with_labels=True,
**draw_params,
)
with patch("matplotlib.pyplot.show"):
reg.draw(
blockade_radius=5,
draw_half_radius=True,
draw_graph=False,
projection=True,
with_labels=True,
**draw_params,
)
reg.draw(
blockade_radius=5,
draw_half_radius=True,
draw_graph=True,
projection=False,
with_labels=True,
**draw_params,
)
reg.draw(
blockade_radius=5,
draw_half_radius=True,
draw_graph=False,
projection=True,
with_labels=True,
**draw_params,
)

reg = Register3D.cubic(1)
with pytest.raises(NotImplementedError, match="Needs more than one atom"):
reg.draw(blockade_radius=5, draw_half_radius=True, **draw_params)

plt.close("all")


def test_to_2D():
reg = Register3D.cuboid(2, 2, 2)
Expand Down
21 changes: 8 additions & 13 deletions tests/test_register_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,18 @@ def test_register_definition(layout, layout3d):
reg2d.rotate(30)


def test_draw(layout, layout3d):
with patch("matplotlib.pyplot.show"):
layout.draw()
def test_draw(layout, layout3d, patch_plt_show):
layout.draw()

with patch("matplotlib.pyplot.show"):
with patch("matplotlib.pyplot.savefig"):
layout.draw(fig_name="my_registerlayout.pdf")
with patch("matplotlib.pyplot.savefig"):
layout.draw(fig_name="my_registerlayout.pdf")

with patch("matplotlib.pyplot.show"):
layout3d.draw()
layout3d.draw()

with patch("matplotlib.pyplot.show"):
layout3d.draw(projection=False)
layout3d.draw(projection=False)

with patch("matplotlib.pyplot.show"):
with patch("matplotlib.pyplot.savefig"):
layout3d.draw(fig_name="my_registerlayout.pdf")
with patch("matplotlib.pyplot.savefig"):
layout3d.draw(fig_name="my_registerlayout.pdf")


def test_repr(layout):
Expand Down
Loading

0 comments on commit c8c12b2

Please sign in to comment.