From 3835c016dec03bc535d05d5c07fe14ea1b8aaf8b Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Wed, 18 Sep 2024 11:15:00 -0400 Subject: [PATCH 1/2] Prevent import errors if glue-jupyter is not installed. --- glue_ar/common/scatter.py | 2 ++ glue_ar/common/scatter_gltf.py | 11 ++++++++++- glue_ar/common/scatter_usd.py | 13 +++++++++++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/glue_ar/common/scatter.py b/glue_ar/common/scatter.py index ded9baf..1b48996 100644 --- a/glue_ar/common/scatter.py +++ b/glue_ar/common/scatter.py @@ -1,5 +1,7 @@ from functools import partial from numpy import clip, isfinite, isnan, ndarray, ones, sqrt + +# Backwards compatibility for Python < 3.10 try: from types import NoneType except ImportError: diff --git a/glue_ar/common/scatter_gltf.py b/glue_ar/common/scatter_gltf.py index 333bef6..649e0e4 100644 --- a/glue_ar/common/scatter_gltf.py +++ b/glue_ar/common/scatter_gltf.py @@ -1,12 +1,21 @@ from gltflib import AccessorType, BufferTarget, ComponentType, PrimitiveMode -from glue_jupyter.common.state3d import ViewerState3D from glue_vispy_viewers.scatter.layer_state import ScatterLayerState from glue_vispy_viewers.volume.viewer_state import Vispy3DViewerState from numpy import array, isfinite, ndarray from numpy.linalg import norm +# Backwards compatibility for Python < 3.10 +try: + from types import NoneType +except ImportError: + NoneType = type(None) from typing import List, Literal, Optional, Tuple +try: + from glue_jupyter.common.state3d import ViewerState3D +except ImportError: + ViewerState3D = NoneType + from glue_ar.common.export_options import ar_layer_export from glue_ar.common.scatter_export_options import ARIpyvolumeScatterExportOptions, ARVispyScatterExportOptions diff --git a/glue_ar/common/scatter_usd.py b/glue_ar/common/scatter_usd.py index abcc581..ecacd11 100644 --- a/glue_ar/common/scatter_usd.py +++ b/glue_ar/common/scatter_usd.py @@ -1,7 +1,16 @@ +# Backwards compatibility for Python < 3.10 +try: + from types import NoneType +except ImportError: + NoneType = type(None) from typing import List, Optional, Tuple -from glue_jupyter.common.state3d import ViewerState3D -from glue_jupyter.ipyvolume.scatter import Scatter3DLayerState +try: + from glue_jupyter.common.state3d import ViewerState3D + from glue_jupyter.ipyvolume.scatter import Scatter3DLayerState +except ImportError: + ViewerState3D = NoneType + Scatter3DLayerState = NoneType from glue_vispy_viewers.scatter.layer_state import ScatterLayerState from glue_vispy_viewers.scatter.viewer_state import Vispy3DViewerState from numpy import array, ndarray From 2cd220b9283a220adda01f031da40a95e862890e Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Wed, 18 Sep 2024 11:27:14 -0400 Subject: [PATCH 2/2] Refactor NoneType check into package utilities. --- glue_ar/common/scatter.py | 16 +++++----------- glue_ar/common/scatter_gltf.py | 17 ++++++----------- glue_ar/common/scatter_usd.py | 20 ++++++++------------ glue_ar/utils.py | 5 +++++ 4 files changed, 24 insertions(+), 34 deletions(-) diff --git a/glue_ar/common/scatter.py b/glue_ar/common/scatter.py index 1b48996..5c330a7 100644 --- a/glue_ar/common/scatter.py +++ b/glue_ar/common/scatter.py @@ -1,25 +1,19 @@ from functools import partial from numpy import clip, isfinite, isnan, ndarray, ones, sqrt - -# Backwards compatibility for Python < 3.10 -try: - from types import NoneType -except ImportError: - NoneType = type(None) from typing import Callable, Dict, List, Optional, Tuple, Union - from glue.utils import ensure_numerical from glue_vispy_viewers.scatter.layer_state import ScatterLayerState + +from glue_ar.common.shapes import rectangular_prism_points, rectangular_prism_triangulation, \ + sphere_points, sphere_triangles +from glue_ar.utils import Bounds, NoneType, Viewer3DState, mask_for_bounds + try: from glue_jupyter.ipyvolume.scatter import Scatter3DLayerState except ImportError: Scatter3DLayerState = NoneType -from glue_ar.common.shapes import rectangular_prism_points, rectangular_prism_triangulation, \ - sphere_points, sphere_triangles -from glue_ar.utils import Bounds, Viewer3DState, mask_for_bounds - ScatterLayerState3D = Union[ScatterLayerState, Scatter3DLayerState] Point = Tuple[float, float, float] diff --git a/glue_ar/common/scatter_gltf.py b/glue_ar/common/scatter_gltf.py index 649e0e4..7206598 100644 --- a/glue_ar/common/scatter_gltf.py +++ b/glue_ar/common/scatter_gltf.py @@ -4,18 +4,8 @@ from numpy import array, isfinite, ndarray from numpy.linalg import norm -# Backwards compatibility for Python < 3.10 -try: - from types import NoneType -except ImportError: - NoneType = type(None) from typing import List, Literal, Optional, Tuple -try: - from glue_jupyter.common.state3d import ViewerState3D -except ImportError: - ViewerState3D = NoneType - from glue_ar.common.export_options import ar_layer_export from glue_ar.common.scatter_export_options import ARIpyvolumeScatterExportOptions, ARVispyScatterExportOptions @@ -28,7 +18,12 @@ from glue_ar.common.scatter import Scatter3DLayerState, ScatterLayerState3D, \ PointsGetter, box_points_getter, IPYVOLUME_POINTS_GETTERS, \ IPYVOLUME_TRIANGLE_GETTERS, VECTOR_OFFSETS, radius_for_scatter_layer, \ - scatter_layer_mask, sizes_for_scatter_layer, sphere_points_getter + scatter_layer_mask, sizes_for_scatter_layer, sphere_points_getter, NoneType + +try: + from glue_jupyter.common.state3d import ViewerState3D +except ImportError: + ViewerState3D = NoneType def add_vectors_gltf(builder: GLTFBuilder, diff --git a/glue_ar/common/scatter_usd.py b/glue_ar/common/scatter_usd.py index ecacd11..307488f 100644 --- a/glue_ar/common/scatter_usd.py +++ b/glue_ar/common/scatter_usd.py @@ -1,16 +1,5 @@ -# Backwards compatibility for Python < 3.10 -try: - from types import NoneType -except ImportError: - NoneType = type(None) from typing import List, Optional, Tuple -try: - from glue_jupyter.common.state3d import ViewerState3D - from glue_jupyter.ipyvolume.scatter import Scatter3DLayerState -except ImportError: - ViewerState3D = NoneType - Scatter3DLayerState = NoneType from glue_vispy_viewers.scatter.layer_state import ScatterLayerState from glue_vispy_viewers.scatter.viewer_state import Vispy3DViewerState from numpy import array, ndarray @@ -25,9 +14,16 @@ from glue_ar.common.shapes import cone_triangles, cone_points, cylinder_points, cylinder_triangles, \ normalize, rectangular_prism_triangulation, sphere_triangles from glue_ar.utils import Viewer3DState, export_label_for_layer, iterable_has_nan, hex_to_components, \ - layer_color, xyz_for_layer, Bounds + layer_color, xyz_for_layer, Bounds, NoneType from glue_ar.usd_utils import material_for_color +try: + from glue_jupyter.common.state3d import ViewerState3D + from glue_jupyter.ipyvolume.scatter import Scatter3DLayerState +except ImportError: + ViewerState3D = NoneType + Scatter3DLayerState = NoneType + def add_vectors_usd(builder: USDBuilder, viewer_state: Viewer3DState, diff --git a/glue_ar/utils.py b/glue_ar/utils.py index 8174cc3..6c21c36 100644 --- a/glue_ar/utils.py +++ b/glue_ar/utils.py @@ -19,6 +19,11 @@ except ImportError: ViewerState3D = Vispy3DViewerState +# Backwards compatibility for Python < 3.10 +try: + from types import NoneType # noqa +except ImportError: + NoneType = type(None) PACKAGE_DIR = dirname(abspath(__file__)) AR_ICON = abspath(join(dirname(__file__), "ar.png"))