7
7
import numpy as np
8
8
import datetime
9
9
import platform
10
+ import pytest
10
11
11
12
12
13
# Basic class that wraps numpy array and has units
@@ -38,12 +39,8 @@ def __array__(self):
38
39
return np .asarray (self .magnitude )
39
40
40
41
41
- # Tests that the conversion machinery works properly for classes that
42
- # work as a facade over numpy arrays (like pint)
43
- @image_comparison (baseline_images = ['plot_pint' ],
44
- tol = {'aarch64' : 0.02 }.get (platform .machine (), 0.0 ),
45
- extensions = ['png' ], remove_text = False , style = 'mpl20' )
46
- def test_numpy_facade ():
42
+ @pytest .fixture
43
+ def quantity_converter ():
47
44
# Create an instance of the conversion interface and
48
45
# mock so we can check methods called
49
46
qc = munits .ConversionInterface ()
@@ -60,12 +57,29 @@ def convert(value, unit, axis):
60
57
else :
61
58
return Quantity (value , axis .get_units ()).to (unit ).magnitude
62
59
60
+ def default_units (value , axis ):
61
+ if hasattr (value , 'units' ):
62
+ return value .units
63
+ elif np .iterable (value ):
64
+ for v in value :
65
+ if hasattr (v , 'units' ):
66
+ return v .units
67
+ return None
68
+
63
69
qc .convert = MagicMock (side_effect = convert )
64
70
qc .axisinfo = MagicMock (side_effect = lambda u , a : munits .AxisInfo (label = u ))
65
- qc .default_units = MagicMock (side_effect = lambda x , a : x .units )
71
+ qc .default_units = MagicMock (side_effect = default_units )
72
+ return qc
66
73
74
+
75
+ # Tests that the conversion machinery works properly for classes that
76
+ # work as a facade over numpy arrays (like pint)
77
+ @image_comparison (baseline_images = ['plot_pint' ],
78
+ tol = {'aarch64' : 0.02 }.get (platform .machine (), 0.0 ),
79
+ extensions = ['png' ], remove_text = False , style = 'mpl20' )
80
+ def test_numpy_facade (quantity_converter ):
67
81
# Register the class
68
- munits .registry [Quantity ] = qc
82
+ munits .registry [Quantity ] = quantity_converter
69
83
70
84
# Simple test
71
85
y = Quantity (np .linspace (0 , 30 ), 'miles' )
@@ -79,9 +93,9 @@ def convert(value, unit, axis):
79
93
ax .yaxis .set_units ('inches' )
80
94
ax .xaxis .set_units ('seconds' )
81
95
82
- assert qc .convert .called
83
- assert qc .axisinfo .called
84
- assert qc .default_units .called
96
+ assert quantity_converter .convert .called
97
+ assert quantity_converter .axisinfo .called
98
+ assert quantity_converter .default_units .called
85
99
86
100
87
101
# Tests gh-8908
@@ -97,6 +111,15 @@ def test_plot_masked_units():
97
111
ax .plot (data_masked_units )
98
112
99
113
114
+ def test_empty_set_limits_with_units (quantity_converter ):
115
+ # Register the class
116
+ munits .registry [Quantity ] = quantity_converter
117
+
118
+ fig , ax = plt .subplots ()
119
+ ax .set_xlim (Quantity (- 1 , 'meters' ), Quantity (6 , 'meters' ))
120
+ ax .set_ylim (Quantity (- 1 , 'hours' ), Quantity (16 , 'hours' ))
121
+
122
+
100
123
@image_comparison (baseline_images = ['jpl_bar_units' ], extensions = ['png' ],
101
124
savefig_kwarg = {'dpi' : 120 }, style = 'mpl20' )
102
125
def test_jpl_bar_units ():
0 commit comments