Skip to content

Commit bb862ad

Browse files
committed
chi2 lazy import
1 parent c59c33a commit bb862ad

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

spatialmath/base/graphics.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import warnings
44
import numpy as np
55
import scipy as sp
6-
from scipy.stats.distributions import chi2
76

87
from spatialmath import base
98

9+
# Only import chi2 from scipy.stats.distributions when used
10+
_chi2 = None
11+
12+
1013
try:
1114
import matplotlib.pyplot as plt
1215
from matplotlib.patches import Circle
@@ -15,6 +18,7 @@
1518
Line3DCollection,
1619
pathpatch_2d_to_3d,
1720
)
21+
1822
_matplotlib_exists = True
1923
except ImportError: # pragma: no cover
2024
_matplotlib_exists = False
@@ -74,7 +78,9 @@ def plot_text(pos, text=None, ax=None, color=None, **kwargs):
7478
return [handle]
7579

7680

77-
def plot_point(pos, marker="bs", label=None, text=None, ax=None, textargs=None, **kwargs):
81+
def plot_point(
82+
pos, marker="bs", label=None, text=None, ax=None, textargs=None, **kwargs
83+
):
7884
"""
7985
Plot a point using matplotlib
8086
@@ -130,7 +136,7 @@ def plot_point(pos, marker="bs", label=None, text=None, ax=None, textargs=None,
130136
"""
131137

132138
if text is not None:
133-
raise DeprecationWarning('use label not text')
139+
raise DeprecationWarning("use label not text")
134140

135141
if isinstance(pos, np.ndarray):
136142
if pos.ndim == 1:
@@ -399,7 +405,8 @@ def plot_box(
399405

400406
return [r]
401407

402-
def plot_poly(vertices, *fmt, close=False,**kwargs):
408+
409+
def plot_poly(vertices, *fmt, close=False, **kwargs):
403410

404411
if close:
405412
vertices = np.hstack((vertices, vertices[:, [0]]))
@@ -523,6 +530,10 @@ def ellipse(E, centre=(0, 0), scale=1, confidence=None, resolution=40, inverted=
523530
raise ValueError("ellipse is defined by a 2x2 matrix")
524531

525532
if confidence:
533+
# Import chi2 if first time used
534+
if _chi2 is None:
535+
from scipy.stats.distributions import chi2
536+
526537
# process the probability
527538
s = math.sqrt(chi2.ppf(confidence, df=2)) * scale
528539
else:
@@ -601,6 +612,7 @@ def plot_ellipse(
601612

602613
# =========================== 3D shapes =================================== #
603614

615+
604616
def sphere(radius=1, centre=(0, 0, 0), resolution=50):
605617
"""
606618
Points on a sphere
@@ -850,6 +862,7 @@ def plot_cylinder(
850862

851863
return handles
852864

865+
853866
def plot_cone(
854867
radius,
855868
height,
@@ -895,7 +908,7 @@ def plot_cone(
895908
:seealso: :func:`~matplotlib.pyplot.plot_surface`, :func:`~matplotlib.pyplot.plot_wireframe`
896909
"""
897910
ax = axes_logic(ax, 3)
898-
911+
899912
# https://stackoverflow.com/questions/26874791/disconnected-surfaces-when-plotting-cones
900913
# Set up the grid in polar coords
901914
theta = np.linspace(0, 2 * np.pi, resolution)
@@ -905,7 +918,7 @@ def plot_cone(
905918
# Then calculate X, Y, and Z
906919
X = R * np.cos(T) + centre[0]
907920
Y = R * np.sin(T) + centre[1]
908-
Z = np.sqrt(X**2 + Y**2) / radius * height + centre[2]
921+
Z = np.sqrt(X ** 2 + Y ** 2) / radius * height + centre[2]
909922
if flip:
910923
Z = height - Z
911924

@@ -924,6 +937,7 @@ def plot_cone(
924937

925938
return handles
926939

940+
927941
def plot_cuboid(
928942
sides=[1, 1, 1], centre=(0, 0, 0), pose=None, ax=None, filled=False, **kwargs
929943
):
@@ -1040,13 +1054,16 @@ def _axes_dimensions(ax):
10401054
elif classname in ("AxesSubplot", "Animate2"):
10411055
return 2
10421056

1057+
10431058
def axes_get_limits(ax):
10441059
return np.r_[ax.get_xlim(), ax.get_ylim()]
10451060

1061+
10461062
def axes_get_scale(ax):
10471063
limits = axes_get_limits(ax)
10481064
return max(abs(limits[1] - limits[0]), abs(limits[3] - limits[2]))
10491065

1066+
10501067
def axes_logic(ax, dimensions, projection="ortho", autoscale=True):
10511068
"""
10521069
Axis creation logic
@@ -1093,7 +1110,7 @@ def axes_logic(ax, dimensions, projection="ortho", autoscale=True):
10931110
# axis was given
10941111

10951112
if _axes_dimensions(ax) == dimensions:
1096-
#print("use existing axes")
1113+
# print("use existing axes")
10971114
return ax
10981115
# mismatch in dimensions, create new axes
10991116
# print('create new axes')
@@ -1128,9 +1145,9 @@ def plotvol2(dim, ax=None, equal=True, grid=False, labels=True):
11281145
================== ====== ======
11291146
input xrange yrange
11301147
================== ====== ======
1131-
A (scalar) -A:A -A:A
1132-
[A, B] A:B A:B
1133-
[A, B, C, D, E, F] A:B C:D
1148+
A (scalar) -A:A -A:A
1149+
[A, B] A:B A:B
1150+
[A, B, C, D, E, F] A:B C:D
11341151
================== ====== ======
11351152
11361153
:seealso: :func:`plotvol3`, :func:`expand_dims`
@@ -1153,7 +1170,9 @@ def plotvol2(dim, ax=None, equal=True, grid=False, labels=True):
11531170
return ax
11541171

11551172

1156-
def plotvol3(dim=None, ax=None, equal=True, grid=False, labels=True, projection="ortho"):
1173+
def plotvol3(
1174+
dim=None, ax=None, equal=True, grid=False, labels=True, projection="ortho"
1175+
):
11571176
"""
11581177
Create 3D plot volume
11591178
@@ -1287,4 +1306,3 @@ def isnotebook():
12871306
/ "test_graphics.py"
12881307
).read()
12891308
) # pylint: disable=exec-used
1290-

0 commit comments

Comments
 (0)