Skip to content

Commit 1b1b6b4

Browse files
koubaaMohamed Koubaapyansys-ci-botpre-commit-ci[bot]germa89
authored
refactor: do not import ansys.tools.visualizer by default when importing ansys.mapdl.core (#3887)
* change how the install check works * chore: adding changelog file 3887.miscellaneous.md [dependabot-skip] * chore: adding changelog file 3887.added.md [dependabot-skip] * don't expect requests to be loaded on import * only set the default theme once * use default symbol * Don't import plotter from visualizer by default Don't import plotter from post module * ci: auto fixes from pre-commit.com hooks. for more information, see https://pre-commit.ci * remove global import of Plotter * chore: adding changelog file 3887.added.md [dependabot-skip] * cicd: empty commit to retrigger CICD * fix ci * update * fix autodoc * hold the bc settings per plotter instance (#3897) * hold the bc settings per plotter instance * ci: auto fixes from pre-commit.com hooks. for more information, see https://pre-commit.ci * chore: adding changelog file 3897.miscellaneous.md [dependabot-skip] --------- Co-authored-by: Mohamed Koubaa <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: pyansys-ci-bot <[email protected]> * add docstring * type hint --------- Co-authored-by: Mohamed Koubaa <[email protected]> Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: German <[email protected]>
1 parent ab5fd48 commit 1b1b6b4

File tree

9 files changed

+47
-32
lines changed

9 files changed

+47
-32
lines changed

doc/changelog.d/3887.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
refactor: do not import ansys.tools.visualizer by default when importing ansys.mapdl.core
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hold the bc settings per plotter instance

doc/source/api/plotting.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Various PyMAPDL specific plotting commands.
99
.. autosummary::
1010
:toctree: _autosummary
1111

12-
plotting.MapdlPlotter
12+
plotting.visualizer.MapdlPlotter
1313
plotting.MapdlTheme
1414
Mapdl.aplot
1515
Mapdl.eplot

src/ansys/mapdl/core/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#
2727
import logging
2828
import os
29-
import sys
3029
from typing import Dict, List, Tuple
3130
from warnings import warn
3231

@@ -135,6 +134,3 @@
135134
get_mapdl_path,
136135
save_ansys_path,
137136
)
138-
139-
if _HAS_VISUALIZER:
140-
from ansys.tools.visualization_interface import Plotter

src/ansys/mapdl/core/helpers.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"""Module for helper functions"""
2424

2525
from functools import namedtuple
26-
import importlib
26+
import importlib.util
2727
import os
2828
import sys
2929
from warnings import warn
@@ -34,14 +34,15 @@
3434
def is_installed(package_name: str) -> bool:
3535
"""Check if a package is installed"""
3636
package_name = package_name.replace("-", ".")
37-
3837
try:
39-
importlib.import_module(package_name)
40-
41-
return True
42-
except ModuleNotFoundError: # pragma: no cover
38+
package_spec = importlib.util.find_spec(package_name)
39+
if package_spec is None: # pragma: no cover
40+
LOG.debug(f"The module '{package_name}' is not installed.")
41+
return False
42+
except ModuleNotFoundError:
4343
LOG.debug(f"The module '{package_name}' is not installed.")
4444
return False
45+
return True
4546

4647

4748
def get_python_version() -> namedtuple:
@@ -94,13 +95,7 @@ def run_first_time() -> None:
9495

9596
def run_every_import() -> None:
9697
# Run every time we import PyMAPDL
97-
from ansys.mapdl.core import _HAS_VISUALIZER, RUNNING_TESTS
98-
99-
# Apply custom theme
100-
if _HAS_VISUALIZER:
101-
from ansys.mapdl.core.plotting.theme import _apply_default_theme
102-
103-
_apply_default_theme()
98+
from ansys.mapdl.core import RUNNING_TESTS
10499

105100
# In case we want to do something specific for testing.
106101
if RUNNING_TESTS: # pragma: no cover

src/ansys/mapdl/core/plotting/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,3 @@ class GraphicsBackend(Enum):
4646

4747
if _HAS_VISUALIZER:
4848
from ansys.mapdl.core.plotting.theme import MapdlTheme # noqa: F401
49-
from ansys.mapdl.core.plotting.visualizer import MapdlPlotter # noqa: F401

src/ansys/mapdl/core/plotting/visualizer.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
"""Module for the MapdlPlotter class."""
2424
from collections import OrderedDict
25-
from typing import Any, Dict, Iterable, Optional, Union
25+
from typing import Any, Callable, Dict, Iterable, Optional, Union
2626

2727
from ansys.tools.visualization_interface import Plotter
2828
from ansys.tools.visualization_interface.backends.pyvista import PyVistaBackendInterface
@@ -43,12 +43,22 @@
4343
)
4444
from ansys.mapdl.core.plotting.theme import MapdlTheme
4545

46+
_FIRST_USE_RUN = False
47+
4648
if _HAS_VISUALIZER:
4749
import pyvista as pv
4850

49-
from ansys.mapdl.core.plotting.plotting_defaults import DefaultSymbol
5051

51-
BC_plot_settings = DefaultSymbol()
52+
def _first_use():
53+
# Run first time we use the visualizer
54+
global _FIRST_USE_RUN
55+
if _FIRST_USE_RUN is True:
56+
return
57+
if _HAS_VISUALIZER:
58+
from ansys.mapdl.core.plotting.theme import _apply_default_theme
59+
60+
_apply_default_theme()
61+
_FIRST_USE_RUN = True
5262

5363

5464
class MapdlPlotterBackend(PyVistaBackendInterface):
@@ -115,6 +125,7 @@ def __init__(
115125
self, use_trame: bool = False, theme: pv.Plotter.theme = None, **plotter_kwargs
116126
):
117127
"""Initialize the ``MapdlPlotter`` class."""
128+
_first_use()
118129
self._backend = MapdlPlotterBackend(use_trame=use_trame, **plotter_kwargs)
119130
super().__init__(backend=self._backend)
120131
self._theme = theme
@@ -124,6 +135,19 @@ def __init__(
124135
self._notebook = None
125136
self._savefig = None
126137
self._title = None
138+
self._bc_settings = None
139+
140+
@property
141+
def bc_settings(self) -> Callable:
142+
"""Get the boundary condition settings object."""
143+
if self._bc_settings is None:
144+
self._make_bc_settings()
145+
return self._bc_settings
146+
147+
def _make_bc_settings(self) -> None:
148+
from ansys.mapdl.core.plotting.plotting_defaults import DefaultSymbol
149+
150+
self._bc_settings = DefaultSymbol()
127151

128152
def _bc_labels_checker(self, bc_labels):
129153
"""Make sure we have allowed parameters and data types for ``bc_labels``"""
@@ -587,13 +611,13 @@ def bc_nodes_plot(
587611
orient=False,
588612
scale="scale",
589613
# tolerance=0.05,
590-
geom=BC_plot_settings(each_label)["glyph"],
614+
geom=self.bc_settings(each_label)["glyph"],
591615
)
592616
name_ = f"{each_label}"
593617
self.scene.add_mesh(
594618
glyphs,
595619
# name_filter=None,
596-
color=BC_plot_settings(each_label)["color"],
620+
color=self.bc_settings(each_label)["color"],
597621
style="surface",
598622
# style='wireframe',
599623
# line_width=3,
@@ -644,11 +668,11 @@ def bc_nodes_plot(
644668
# something it can be seen properly in the legend
645669
label_ = value[1]
646670
if "U" in label_:
647-
value = [BC_plot_settings("UY")["glyph"], label_, value[2]]
671+
value = [self.bc_settings("UY")["glyph"], label_, value[2]]
648672
elif "F" in label_:
649-
value = [BC_plot_settings("FX")["glyph"], label_, value[2]]
673+
value = [self.bc_settings("FX")["glyph"], label_, value[2]]
650674
else:
651-
value = [BC_plot_settings(label_)["glyph"], label_, value[2]]
675+
value = [self.bc_settings(label_)["glyph"], label_, value[2]]
652676

653677
if symbol == value[1]:
654678
sorted_dict[key] = value

src/ansys/mapdl/core/post.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,9 @@
2525

2626
import numpy as np
2727

28-
from ansys.mapdl.core import _HAS_VISUALIZER
2928
from ansys.mapdl.core.errors import MapdlRuntimeError
3029
from ansys.mapdl.core.misc import requires_package, supress_logging
3130

32-
if _HAS_VISUALIZER:
33-
from ansys.mapdl.core.plotting.visualizer import MapdlPlotter
34-
3531
COMPONENT_STRESS_TYPE = ["X", "Y", "Z", "XY", "YZ", "XZ"]
3632
PRINCIPAL_TYPE = ["1", "2", "3"]
3733
STRESS_TYPES = ["X", "Y", "Z", "XY", "YZ", "XZ", "1", "2", "3", "INT", "EQV"]
@@ -633,6 +629,8 @@ def _plot_point_scalars(self, scalars, show_node_numbering=False, **kwargs):
633629
"exist within the result file."
634630
)
635631

632+
from ansys.mapdl.core.plotting.visualizer import MapdlPlotter
633+
636634
with self._mapdl.save_selection:
637635
mask = self.selected_nodes
638636
nodes_ids = self._mapdl.get_array("NODE", item1="NLIST")
@@ -679,6 +677,8 @@ def _plot_cell_scalars(self, scalars, show_elem_numbering=False, **kwargs):
679677
"exist within the result file."
680678
)
681679

680+
from ansys.mapdl.core.plotting.visualizer import MapdlPlotter
681+
682682
with self._mapdl.save_selection:
683683
# Select nodes to avoid segfault
684684
self._mapdl.nsle("s", "all")

tests/test_examples.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ def test_detach_examples_submodule():
179179
180180
assert 'ansys.mapdl.core' in sys.modules, 'PyMAPDL is not loaded!'
181181
assert 'ansys.mapdl.core.examples' not in sys.modules, 'Examples is loaded!'
182-
assert 'requests' in sys.modules, 'Requests is loaded!'
183182
184183
from ansys.mapdl.core.examples import vmfiles
185184

0 commit comments

Comments
 (0)