Skip to content

feat: add screenshot in pyvista related methods #521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
eed9b7a
tests: add test on plot method
SMoraisAnsys Mar 27, 2025
3245ebc
chore: adding changelog file 521.test.md [dependabot-skip]
pyansys-ci-bot Mar 27, 2025
de7baf2
test: avoid patch and use xvfb
SMoraisAnsys Mar 27, 2025
34fad26
ci: auto fixes from pre-commit.com hooks.
pre-commit-ci[bot] Mar 27, 2025
1354d33
ci: split install in two steps
SMoraisAnsys Mar 27, 2025
ab9020f
Merge branch 'tests/show-methods' of github.com:ansys-internal/pyspeo…
SMoraisAnsys Mar 27, 2025
55ba348
ci: update libs to match ubuntu 24.04
SMoraisAnsys Mar 27, 2025
3e044e3
feature: allow saving image in tests dir
SMoraisAnsys Mar 27, 2025
c924eb7
ci: auto fixes from pre-commit.com hooks.
pre-commit-ci[bot] Mar 27, 2025
a7adb7b
test: add off_screen=true by default
SMoraisAnsys Mar 27, 2025
30de62a
ci: auto fixes from pre-commit.com hooks.
pre-commit-ci[bot] Mar 27, 2025
948bb63
test: extend test with asserts
SMoraisAnsys Mar 27, 2025
3d445e3
ci: auto fixes from pre-commit.com hooks.
pre-commit-ci[bot] Mar 27, 2025
e6a2e44
Merge branch 'main' into tests/show-methods
SMoraisAnsys Mar 27, 2025
a42dced
chore: adding changelog file 521.added.md [dependabot-skip]
pyansys-ci-bot Mar 27, 2025
5961e87
fix bug and improve tests
StefanThoene Mar 28, 2025
bcd03bc
Merge branch 'main' into tests/show-methods
pluAtAnsys Mar 28, 2025
675724e
Apply suggestions from code review
StefanThoene Mar 31, 2025
0fe20f1
Merge branch 'main' into tests/show-methods
StefanThoene Mar 31, 2025
89e3eac
add notes to highlight correct way to close pyvista
StefanThoene Mar 31, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ jobs:
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Start container
shell: bash
env:
ANSYSLMD_LICENSE_FILE: 1055@${{ secrets.LICENSE_SERVER }}
run: |
Expand All @@ -99,6 +100,7 @@ jobs:
dependencies: "pandoc"
needs-quarto: true
- name: Stop container
shell: bash
run: |
docker kill speos-rpc
docker rm speos-rpc
Expand All @@ -112,18 +114,26 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Installing OS packages
shell: bash
run: |
sudo apt-get update
sudo apt install -y libgl1 libglx-mesa0 xvfb libgomp1

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.MAIN_PYTHON_VERSION }}

- name: Create Python virtual environment
shell: bash
run: |
python -m venv .venv
source .venv/bin/activate
python -c "import sys; print(sys.executable)"

- name: Install packages for testing
shell: bash
run: |
. .venv/bin/activate
python -m pip install --upgrade pip
Expand All @@ -140,13 +150,15 @@ jobs:
- name: Start container
env:
ANSYSLMD_LICENSE_FILE: 1055@${{ secrets.LICENSE_SERVER }}
shell: bash
run: |
docker run --detach --name speos-rpc -p 50098:50098 -e SPEOS_LOG_LEVEL=2 -e ANSYSLMD_LICENSE_FILE=${{ env.ANSYSLMD_LICENSE_FILE }} -v "${{ github.workspace }}/tests/assets:/app/assets" --entrypoint /app/SpeosRPC_Server.x ghcr.io/ansys/speos-rpc:dev

- name: Run pytest
shell: bash
run: |
. .venv/bin/activate
pytest -xs
xvfb-run pytest -xs

- name: Upload Coverage Results
if: always()
Expand All @@ -164,6 +176,7 @@ jobs:
files: .cov/xml

- name: Stop container
shell: bash
run: |
docker kill speos-rpc
docker rm speos-rpc
Expand Down
1 change: 1 addition & 0 deletions doc/changelog.d/521.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add screenshot in pyvista related methods
20 changes: 15 additions & 5 deletions src/ansys/speos/core/lxp.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
from __future__ import annotations

import os
from typing import Union
from pathlib import Path
from typing import Optional, Union

import pyvista as pv

Expand Down Expand Up @@ -431,7 +432,7 @@ def __add_ray_to_pv(plotter: pv.Plotter, ray: RayPath, max_ray_length: float):
max_ray_length : float
Length of the last ray.
"""
temp = ray.impacts
temp = ray.impacts.copy()
if not 7 <= ray.intersection_type[-1] <= 15:
temp.append(
[
Expand All @@ -452,6 +453,7 @@ def preview(
max_ray_length: float = 50.0,
ray_filter: bool = False,
project: Project = None,
screenshot: Optional[Union[str, Path]] = None,
) -> LightPathFinder:
"""Preview LPF file with pyvista.

Expand All @@ -465,11 +467,19 @@ def preview(
Boolean to decide if filtered rays or all rays should be shown.
project : ansys.speos.core.project.Project
Speos Project/Geometry to be added to pyvista visualisation.
screenshot : str or Path or ``None``
Path to save a screenshot of the plotter.

Returns
-------
ansys.speos.core.lxp.LightPathFinder
LightPathFinder Instance.

Notes
-----
Please use the ``q``-key to close the plotter as some
operating systems (namely Windows) will experience issues
saving a screenshot if the exit button in the GUI is pressed.
"""
if ray_filter:
if len(self._filtered_rays) > 0:
Expand All @@ -495,10 +505,10 @@ def preview(
else:
for i in range(nb_ray):
self.__add_ray_to_pv(plotter, temp_rays[i], max_ray_length)
if os.environ.get("DOCUMENTATION_BUILDING", "true") == "true":
plotter.show(jupyter_backend="html")
if os.environ.get("DOCUMENTATION_BUILDING", "false") == "true":
plotter.show(screenshot=screenshot, jupyter_backend="html")
else:
plotter.show()
plotter.show(screenshot=screenshot)
return self


Expand Down
20 changes: 16 additions & 4 deletions src/ansys/speos/core/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from __future__ import annotations

import os
from pathlib import Path
import re
from typing import List, Mapping, Optional, Union
import uuid
Expand Down Expand Up @@ -924,7 +925,11 @@ def _create_preview(self, viz_args=None) -> pv.Plotter:
p.add_mesh(_preview_mesh, show_edges=True, **viz_args)
return p

def preview(self, viz_args=None) -> None:
def preview(
self,
viz_args=None,
screenshot: Optional[Union[str, Path]] = None,
) -> None:
"""Preview cad bodies inside the project's scene.

Parameters
Expand All @@ -934,12 +939,19 @@ def preview(self, viz_args=None) -> None:
e.g.
- {'style': 'wireframe'},
- {'style': 'surface', 'color':'white'},
- {'opacity': 0.7, 'color':'white', 'show_edges': False},
- {'opacity': 0.7, 'color':'white', 'show_edges': False}.

screenshot : str or Path or ``None``
Path to save a screenshot of the plotter.

"""
if viz_args is None:
viz_args = {"opacity": 1}
if screenshot is not None:
screenshot = Path(screenshot)

p = self._create_preview(viz_args=viz_args)
if os.environ.get("DOCUMENTATION_BUILDING", "true") == "true":
p.show(jupyter_backend="html")
p.show(screenshot=screenshot, jupyter_backend="html")
else:
p.show()
p.show(screenshot=screenshot)
10 changes: 10 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
from ansys.speos.core import LOG
from ansys.speos.core.speos import Speos

try:
import pyvista as pv

pv.OFF_SCREEN = True
pv.global_theme.window_size = [500, 500]
except ImportError:
pass

IMAGE_RESULTS_DIR = Path(Path(__file__).parent, "image_results")


@pytest.fixture(scope="session")
def speos():
Expand Down
34 changes: 33 additions & 1 deletion tests/core/test_lxp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@
from pathlib import Path

import ansys.speos.core.lxp as lxp
from ansys.speos.core.project import Project
from ansys.speos.core.speos import Speos
from tests.conftest import test_path
from tests.conftest import IMAGE_RESULTS_DIR, test_path


def test_light_path_finder_direct(speos: Speos):
"""Test for direct simulation lpf."""
path = str(Path(test_path) / "basic_DirectSimu.lpf")
lpf = lxp.LightPathFinder(speos=speos, path=path)
screenshot = Path(IMAGE_RESULTS_DIR, "test_light_path_finder_preview.png")
lpf.preview(screenshot=screenshot)
expected_ray = {
"nb_impacts": 4,
"impacts": [
Expand Down Expand Up @@ -123,3 +126,32 @@ def test_light_path_finder_inverse(speos: Speos):
lpf.filter_error_rays()
assert len(lpf.filtered_rays) == 0
assert lpf.rays[50].get() == expected_ray


def test_lpf_preview_with_project(speos: Speos):
"""Test for visualizing lpf data."""
path = str(Path(test_path) / "basic_DirectSimu.lpf")
screenshot = Path(IMAGE_RESULTS_DIR, "test_light_path_finder_direct.png")
p = Project(
speos=speos,
path=str(
Path(test_path) / "LG_50M_Colorimetric_short.sv5" / "LG_50M_Colorimetric_short.sv5"
),
)
lpf = lxp.LightPathFinder(speos=speos, path=path)
lpf.preview(project=p, screenshot=screenshot)
assert screenshot.exists()
assert screenshot.stat().st_size > 0


def test_lpf_preview_without_project(speos: Speos):
"""Test for visualizing lpf data."""
path = str(Path(test_path) / "basic_DirectSimu.lpf")
screenshot = Path(IMAGE_RESULTS_DIR, "test_lpf_preview_without_project.png")
lpf1 = lxp.LightPathFinder(speos=speos, path=path)
lpf1.filter_by_body_ids([3601101451])
lpf1.filter_by_face_ids([3866239813], new=False)
lpf1.remove_error_rays()
lpf1.preview(ray_filter=True, screenshot=screenshot)
assert screenshot.exists()
assert screenshot.stat().st_size > 0
2 changes: 2 additions & 0 deletions tests/image_results/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
Loading