Skip to content

Commit

Permalink
Nest Settings2D within Settings.color
Browse files Browse the repository at this point in the history
This commit moves the generated Settings2D datamodel frontend from
`settings_2d.py` to `settings2d.py`, as that is the snake case name that
our datamodel frontend generator produces, and we need to import this
file into the generated Settings datamodel frontend.
  • Loading branch information
johningve committed Dec 5, 2024
1 parent 7e2dac4 commit c0b3655
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ show-source = True
### D107: Missing docstring in __init__
per-file-ignores =
settings.py:D101,D102,D106,D107
settings_2d.py:D101,D102,D106,D107
settings2d.py:D101,D102,D106,D107
camera_state.py:D101,D102,D106,D107
camera_info.py:D101,D102,D106,D107
camera_intrinsics.py:D101,D102,D106,D107
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,8 +823,12 @@ def _generate_datamodel_frontend(

def generate_all_datamodels(dest_dir: Path) -> None:
for internal_class, filename, extra_imports in [
(_zivid.Settings, "settings.py", ["datetime", "collections.abc"]),
(_zivid.Settings2D, "settings_2d.py", ["datetime", "collections.abc"]),
(_zivid.Settings2D, "settings2d.py", ["datetime", "collections.abc"]),
(
_zivid.Settings,
"settings.py",
["datetime", "collections.abc", "zivid.settings2d"],
),
(_zivid.CameraInfo, "camera_info.py", []),
(_zivid.CameraState, "camera_state.py", ["collections.abc"]),
(_zivid.FrameInfo, "frame_info.py", ["datetime"]),
Expand Down
2 changes: 1 addition & 1 deletion modules/zivid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from zivid.point_cloud import PointCloud
from zivid.sdk_version import SDKVersion
from zivid.settings import Settings
from zivid.settings_2d import Settings2D
from zivid.settings2d import Settings2D
from zivid.camera_info import CameraInfo
from zivid.camera_intrinsics import CameraIntrinsics
from zivid.matrix4x4 import Matrix4x4
Expand Down
2 changes: 1 addition & 1 deletion modules/zivid/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
_to_network_configuration,
)
from zivid.settings import Settings, _to_internal_settings
from zivid.settings_2d import Settings2D, _to_internal_settings2d
from zivid.settings2d import Settings2D, _to_internal_settings2d


class Camera:
Expand Down
2 changes: 1 addition & 1 deletion modules/zivid/experimental/calibration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from zivid.camera_intrinsics import _to_camera_intrinsics
from zivid.frame import Frame
from zivid.settings import Settings, _to_internal_settings
from zivid.settings_2d import Settings2D, _to_internal_settings2d
from zivid.settings2d import Settings2D, _to_internal_settings2d


def intrinsics(camera, settings=None):
Expand Down
2 changes: 1 addition & 1 deletion modules/zivid/frame_2d.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Contains the Frame class."""

import _zivid
from zivid.settings_2d import _to_settings2d
from zivid.settings2d import _to_settings2d
from zivid.camera_info import _to_camera_info
from zivid.camera_state import _to_camera_state
from zivid.frame_info import _to_frame_info
Expand Down
2 changes: 1 addition & 1 deletion modules/zivid/projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import _zivid
from zivid.frame_2d import Frame2D
from zivid.settings_2d import Settings2D, _to_internal_settings2d
from zivid.settings2d import Settings2D, _to_internal_settings2d


class ProjectedImage:
Expand Down
29 changes: 19 additions & 10 deletions modules/zivid/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# pylint: disable=too-many-lines,protected-access,too-few-public-methods,too-many-arguments,line-too-long,missing-function-docstring,missing-class-docstring,redefined-builtin,too-many-branches,too-many-boolean-expressions
import datetime
import collections.abc
import zivid.settings2d
import _zivid


Expand Down Expand Up @@ -2707,7 +2708,7 @@ def valid_values(cls):
def __init__(
self,
acquisitions=None,
color=_zivid.Settings.Color().value,
color=None,
engine=_zivid.Settings.Engine().value,
diagnostics=None,
processing=None,
Expand All @@ -2733,11 +2734,11 @@ def __init__(
)
)

if isinstance(color, _zivid.Settings2D) or color is None:
self._color = _zivid.Settings.Color(color)
if isinstance(color, zivid.settings2d.Settings2D) or color is None:
self._color = color
else:
raise TypeError(
"Unsupported type, expected: _zivid.Settings2D or None, got {value_type}".format(
"Unsupported type, expected: zivid.settings2d.Settings2D or None, got {value_type}".format(
value_type=type(color)
)
)
Expand Down Expand Up @@ -2785,7 +2786,7 @@ def acquisitions(self):

@property
def color(self):
return self._color.value
return self._color

@property
def engine(self):
Expand Down Expand Up @@ -2827,11 +2828,11 @@ def acquisitions(self, value):

@color.setter
def color(self, value):
if isinstance(value, _zivid.Settings2D) or value is None:
self._color = _zivid.Settings.Color(value)
if isinstance(value, zivid.settings2d.Settings2D) or value is None:
self._color = value
else:
raise TypeError(
"Unsupported type, expected: zivid.Settings or None, got {value_type}".format(
"Unsupported type, expected: zivid.settings2d.Settings2D or None, got {value_type}".format(
value_type=type(value)
)
)
Expand Down Expand Up @@ -3159,7 +3160,11 @@ def _to_settings(internal_settings):
internal_settings.region_of_interest
),
sampling=_to_settings_sampling(internal_settings.sampling),
color=internal_settings.color.value,
color=(
zivid.settings2d._to_settings2d(internal_settings.color.value)
if internal_settings.color.value is not None
else None
),
engine=internal_settings.engine.value,
)

Expand Down Expand Up @@ -3556,7 +3561,11 @@ def _to_internal_settings(settings):
temp_acquisitions.append(_to_internal_settings_acquisition(value))
internal_settings.acquisitions = temp_acquisitions

internal_settings.color = _zivid.Settings.Color(settings.color)
internal_settings.color = _zivid.Settings.Color(
zivid.settings2d._to_internal_settings2d(settings.color)
if settings.color is not None
else None
)
internal_settings.engine = _zivid.Settings.Engine(settings._engine.value)

internal_settings.diagnostics = _to_internal_settings_diagnostics(
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion test/calibration/test_intrinsics.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_intrinsics(shared_file_camera):

def test_intrinsics_with_settings_2d(shared_file_camera):
from zivid.experimental.calibration import intrinsics
from zivid.settings_2d import Settings2D
from zivid.settings2d import Settings2D

camera_intrinsics = intrinsics(
camera=shared_file_camera,
Expand Down
4 changes: 2 additions & 2 deletions test/converters/test_convert_settings_2d.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
def test_to_internal_settings_to_settings_modified():
import datetime
from zivid import Settings2D
from zivid.settings_2d import _to_settings2d, _to_internal_settings2d
from zivid.settings2d import _to_settings2d, _to_internal_settings2d

modified_settings = Settings2D(
acquisitions=[
Expand All @@ -17,7 +17,7 @@ def test_to_internal_settings_to_settings_modified():

def test_to_internal_settings_to_settings_default():
from zivid import Settings2D
from zivid.settings_2d import _to_settings2d, _to_internal_settings2d
from zivid.settings2d import _to_settings2d, _to_internal_settings2d

default_settings = Settings2D()
converted_settings = _to_settings2d(_to_internal_settings2d(default_settings))
Expand Down
29 changes: 29 additions & 0 deletions test/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ def test_default_settings(application):

settings = zivid.Settings()

assert settings.color is None

assert isinstance(settings.acquisitions, list)
assert len(settings.acquisitions) == 0
assert settings.engine is None
Expand Down Expand Up @@ -128,6 +130,33 @@ def test_default_settings(application):
assert settings.region_of_interest.depth.range is None


def test_set_color_settings():
from zivid import Settings, Settings2D

settings = Settings()
assert settings.color is None

settings.color = Settings2D()
assert settings.color is not None
assert isinstance(settings.color, Settings2D)
assert settings.color == Settings2D()

settings = Settings(color=Settings2D())
assert settings.color is not None
assert isinstance(settings.color, Settings2D)
assert settings.color == Settings2D()

settings = Settings(color=Settings2D(acquisitions=(Settings2D.Acquisition(),)))
assert settings.color is not None
assert isinstance(settings.color, Settings2D)
assert len(settings.color.acquisitions) == 1

settings = Settings()
settings.color = Settings2D()
settings.color.acquisitions.append(Settings2D.Acquisition())
assert len(settings.color.acquisitions) == 1


def test_set_acquisition_list():
from zivid import Settings

Expand Down

0 comments on commit c0b3655

Please sign in to comment.