IDS peak ICV v1.1.0
Loading...
Searching...
No Matches
module_base.py
Go to the documentation of this file.
1from abc import ABC
2
4from ids_peak_common.serialization import IArchive
5from ids_peak_icv.exceptions import NotSupportedException, CorruptedException
6
7
8class ModuleBase(IModule, ABC):
9 """!
10 @brief
11 Base class for all pipeline modules.
12
13 @ingroup ids_peak_icv_python_pipeline_modules
14 @since ids_peak_icv 1.0
15 """
16
17 def __init__(self) -> None:
18 """!
19 @brief
20 Creates an instance of class ModuleBase.
21
22 @since ids_peak_icv 1.0
23 """
24 self._enabled: bool = True
25
26 # -------------------------------------------------------------------------
27 @property
28 def version(self) -> int:
29 """!
30 @brief
31 Return the module version.
32
33 @return Module version.
34
35 @since ids_peak_icv 1.0
36 """
37 return 1
38
39 @property
40 def enabled(self) -> bool:
41 """!
42 @brief
43 Get whether this module is enabled.
44
45 When disabled, `process` returns the input image unchanged.
46
47 @return True if the module is enabled, False otherwise.
48
49 @since ids_peak_icv 1.0
50 """
51 return self._enabled
52
53 @enabled.setter
54 def enabled(self, value: bool) -> None:
55 """!
56 @brief
57 Enable or disable the module.
58
59 When disabled, `process` returns the input image unchanged.
60
61 @param value: True to enable, False to disable.
62
63 @since ids_peak_icv 1.0
64 """
65 self._enabled = value
66
67 # -------------------------------------------------------------------------
68
69 def serialize(self, archive: IArchive) -> None:
70 """!
71 @brief
72 Serializes the object's internal state into the provided archive.
73
74 This function populates the given archive with all parameters required to fully represent the current state of the object.
75 It ensures that the object can be reconstructed or transmitted accurately by saving all relevant data members
76 in a consistent and structured format.
77
78 @return The serialized module's internal state.
79
80 @since ids_peak_icv 1.0
81 """
82 archive.set("Enabled", self.enabledenabledenabledenabledenabled)
83 archive.set("Version", self.versionversion)
84
85 def deserialize(self, archive: IArchive) -> None:
86 """!
87 @brief
88 Restores the object's state from the provided archive.
89
90 This function reads and applies all necessary parameters from the given archive to reconstruct the internal state of the object.
91 It ensures that the object is restored to a valid and consistent state.
92
93 @param archive: The source archive containing the serialized parameters.
94
95 @exception CorruptedException: If Archive is malformed, misses keys or the values are invalid
96 @exception NotSupportedException: If the 'Version' entry indicates an unsupported version.
97
98 @note This function requires that the archive contains all expected fields as produced by a corresponding serialize() call.
99
100 @since ids_peak_icv 1.0
101 """
102 self._validate_version(archive)
103 self.enabledenabledenabledenabledenabled = archive.get("Enabled", bool)
104
105 def _validate_version(self, archive: IArchive) -> None:
106 archive_version = archive.get("Version", int)
107 if archive_version > self.versionversion or archive_version < 1:
108 self._raise_version_is_not_supported(str(archive_version), str(self.versionversion))
109
110 # -------------------------------------------------------------------------
111
112 def _raise_corrupted_archive_key_missing(self, missing_archive_key: str) -> None:
113 raise CorruptedException(f"The given archive is corrupted. The key '{missing_archive_key}' is missing.")
114
115 def _raise_version_is_not_supported(self, actual_version: str, expected_version: str) -> None:
117 "The given version in the settings exceeds the module version. "
118 f"Given settings version: {actual_version}, Module version: {self.version}. "
119 "The module version has to be >= the given settings version.")
Interface for a module used within a pipeline.
Definition imodule.py:9
None enabled(self, bool value)
Sets the enabled state of the module.
Definition imodule.py:35
bool enabled(self)
Gets whether the module is currently enabled.
Definition imodule.py:22
Thrown when input data or a file is malformed, unreadable, or otherwise corrupted.
Thrown when a requested operation is not implemented or supported.
Definition exceptions.py:71
Base class for all pipeline modules.
Definition module_base.py:8
int version(self)
Return the module version.
None deserialize(self, IArchive archive)
Restores the object's state from the provided archive.
None enabled(self, bool value)
Enable or disable the module.
None __init__(self)
Creates an instance of class ModuleBase.
bool enabled(self)
Get whether this module is enabled.
None _raise_corrupted_archive_key_missing(self, str missing_archive_key)
None _raise_version_is_not_supported(self, str actual_version, str expected_version)
None serialize(self, IArchive archive)
Serializes the object's internal state into the provided archive.