Skip to content
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

Systemviewers fixes for older OpticStudio versions #101

Merged
merged 20 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 18 additions & 10 deletions tests/analyses/new/test_systemviewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
from zospy.analyses.new.systemviewers.base import SystemViewerWrapper


def assert_systemviewer_result(result, minimal_version):
"""Makes sure systemviewer results is correctly asserted for different versions of OpticStudio."""
if minimal_version >= "24.1.0":
assert result.data is not None
else: # No result.data as layout exports are not supported
assert result.data is None


class TestBase:
@analysis_settings
class MockSystemViewerSettings:
Expand Down Expand Up @@ -167,49 +175,49 @@ def test_warn_ignored_settings(self, simple_system):


class TestCrossSection:
def test_can_run(self, simple_system):
def test_can_run(self, simple_system, optic_studio_version):
result = CrossSection().run(simple_system)
assert result.data is not None
assert_systemviewer_result(result, optic_studio_version)

def test_to_json(self, simple_system):
result = CrossSection().run(simple_system)
assert result.from_json(result.to_json()).to_json() == result.to_json()


class TestViewer3D:
def test_can_run(self, simple_system):
def test_can_run(self, simple_system, optic_studio_version):
result = Viewer3D().run(simple_system)
assert result.data is not None
assert_systemviewer_result(result, optic_studio_version)

def test_to_json(self, simple_system):
result = Viewer3D().run(simple_system)
assert result.from_json(result.to_json()).to_json() == result.to_json()


class TestShadedModel:
def test_can_run(self, simple_system):
def test_can_run(self, simple_system, optic_studio_version):
result = ShadedModel().run(simple_system)
assert result.data is not None
assert_systemviewer_result(result, optic_studio_version)

def test_to_json(self, simple_system):
result = ShadedModel().run(simple_system)
assert result.from_json(result.to_json()).to_json() == result.to_json()


class TestNSC3DLayout:
def test_can_run(self, nsc_simple_system):
def test_can_run(self, nsc_simple_system, optic_studio_version):
result = NSC3DLayout().run(nsc_simple_system)
assert result.data is not None
assert_systemviewer_result(result, optic_studio_version)

def test_to_json(self, nsc_simple_system):
result = NSC3DLayout().run(nsc_simple_system)
assert result.from_json(result.to_json()).to_json() == result.to_json()


class TestNSCShadedModel:
def test_can_run(self, nsc_simple_system):
def test_can_run(self, nsc_simple_system, optic_studio_version):
result = NSCShadedModel().run(nsc_simple_system)
assert result.data is not None
assert_systemviewer_result(result, optic_studio_version)

def test_to_json(self, nsc_simple_system):
result = NSCShadedModel().run(nsc_simple_system)
Expand Down
8 changes: 7 additions & 1 deletion zospy/analyses/new/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,15 @@ class AnalysisMetadata:


class _TypeInfo(TypedDict):
data_type: Literal["dataframe", "ndarray", "dataclass"]
data_type: Literal["dataframe", "ndarray", "dataclass", "none"]
name: NotRequired[str | None]
module: NotRequired[str | None]


def _serialize_analysis_data_type(data: AnalysisData) -> _TypeInfo:
if data is None:
return {"data_type": "none"}

if isinstance(data, pd.DataFrame):
return {"data_type": "dataframe"}

Expand All @@ -126,6 +129,9 @@ def _deserialize_dataclass(data: dict, typeinfo: _TypeInfo) -> AnalysisData:


def _deserialize_analysis_data(data: dict | list, typeinfo: _TypeInfo) -> AnalysisData:
if typeinfo["data_type"] == "none":
return None

if typeinfo["data_type"] == "dataframe":
return pd.DataFrame.from_dict(data, orient="tight")

Expand Down
6 changes: 4 additions & 2 deletions zospy/analyses/new/parsers/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import logging
from operator import attrgetter
from typing import TYPE_CHECKING, Annotated, Any, Generic, Literal, TypeVar, Union

Expand Down Expand Up @@ -130,8 +131,9 @@ def __init__(self, enum: str):
def _validate_constant(self, value: ZospyConstantType | str) -> ZospyConstantType:
try:
constant = attrgetter(self.enum)(constants)
except AttributeError as e:
raise AttributeError(f"Constant {self.enum} not found in zospy.constants") from e
except AttributeError:
logging.warning(f"Constant {self.enum} not found in zospy.constants")
return None

return constants.process_constant(constant, value)

Expand Down
Loading