2.4. distoptica.LeastSquaresAlgParams
- class LeastSquaresAlgParams(max_num_iterations=10, initial_damping=0.001, factor_for_decreasing_damping=9, factor_for_increasing_damping=11, improvement_tol=0.1, rel_err_tol=0.01, plateau_tol=0.001, plateau_patience=2, skip_validation_and_conversion=False)[source]
Bases:
PreSerializableAndUpdatable
The parameters of the least-squares algorithm to be used to calculate the mapping of fractional Cartesian coordinates of distorted images to those of the corresponding undistorted images.
Users are encouraged to read the summary documentation of the class
distoptica.DistortionModel
before reading the documentation for the current class as it provides essential context to what is discussed below.As discussed in the summary documentation of the class
distoptica.DistortionModel
, optical distortions introduced in an imaging experiment can be described by a coordinate transformation, comprising of two components: \(T_{⌑;x}\left(u_{x},u_{y}\right)\) and \(T_{⌑;y}\left(u_{x},u_{y}\right)\). Moreover, we assume that there exists a right inverse to the coordinate transformation, i.e. that there are functions \(T_{\square;x}\left(q_{x},q_{y}\right)\) and \(T_{\square;y}\left(q_{x},q_{y}\right)\) satisfying Eqs. (2.3.1) and (2.3.2).In order to undistort images, one needs to calculate \(T_{\square;x}\left(q_{x},q_{y}\right)\) and \(T_{\square;y}\left(q_{x},q_{y}\right)\) for multiple values of both \(q_{x}\) and \(q_{y}\), where \(q_{x}\) and \(q_{y}\) are fractional horizontal and vertical coordinates respectively of a distorted image. Let \(q_{x;w_{1},w_{2}}\) and \(q_{y;w_{1},w_{2}}\) be matrices of the same dimensions storing the values of \(q_{x}\) and \(q_{y}\), respectively, for which to calculate \(T_{\square;x}\left(q_{x},q_{y}\right)\) and \(T_{\square;y}\left(q_{x},q_{y}\right)\), where \(w_{1}\) and \(w_{2}\) are row and column indices respectively. In
distoptica
, \(T_{\square;x}\left(q_{x},q_{y}\right)\) and \(T_{\square;y}\left(q_{x},q_{y}\right)\) are calculated using the Levenberg-Marquardt (LM) algorithm. Specifically, we apply the LM algorithm to try to find a solution iteratively to the problem:(2.4.1)\[0=q_{x;w_{1},w_{2}}- T_{⌑;x}\left(u_{x}=u_{x;w_{1},w_{2}},u_{y}=u_{y;w_{1},w_{2}}\right),\](2.4.2)\[0=q_{y;w_{1},w_{2}}- T_{⌑;y}\left(u_{x}=u_{x;w_{1},w_{2}},u_{y}=u_{y;w_{1},w_{2}}\right),\]where \(q_{x;w_{1},w_{2}}\), \(q_{y;w_{1},w_{2}}\), \(T_{⌑;x}\left(u_{x},u_{y}\right)\), and \(T_{⌑;y}\left(u_{x},u_{y}\right)\) are given, and \(u_{x;w_{1},w_{2}}\) and \(u_{y;w_{1},w_{2}}\) are unknowns to be determined, if possible.
Before describing the LM algorithm, we introduce a few quantities and notation. First, let \(\lambda_{0}\) be a given positive real number, which we refer to as the initial damping of the LM algorithm. Next, let \(\lambda_{\uparrow}\) and \(\lambda_{\downarrow}\) be given positive real numbers, which we refer to as the factors for increasing and decreasing the damping respectively. Next, let \(\epsilon_{\rho}\), \(\epsilon_{\chi}\), and \(\epsilon_{-}\) be given positive real numbers, which we refer to as the improvement tolerance, the error tolerance, and the plateau tolerance. Next, let \(N_{-;\max}\) and \(N_{\nu}\) be given positive integers, which we refer to as the plateau patience and max number of iterations respectively. Next, let \(N_{w;x}\) and \(N_{w;y}\) be the number of columns and rows of the matrix \(q_{x;w_{1},w_{2}}\). Next, let \(\nu\) be the iteration index. Next, we introduce the following boldface notation:
(2.4.3)\[\begin{split}\mathbf{O}_{\nu;w_{1},w_{2}}= \begin{pmatrix}O_{\nu;w_{1},w_{2};0,0} & O_{\nu;w_{1},w_{2};0,1}\\ O_{\nu;w_{1},w_{2};1,0} & O_{\nu;w_{1},w_{2};1,1} \end{pmatrix},\end{split}\](2.4.4)\[\begin{split}\mathbf{o}_{\nu;w_{1},w_{2}}=\begin{pmatrix}o_{\nu;w_{1},w_{2};0}\\ o_{\nu;w_{1},w_{2};1} \end{pmatrix},\end{split}\]where the letters \(\mathbf{O}\) and \(\mathbf{o}\) are placeholders for any boldface uppercase and lowercase symbols respectively.
With the quantities and notation that we have introduced in the previous paragraph, we describe the LM algorithm below:
\(\nu\leftarrow 0\).
\(N_{-}\leftarrow 0\).
\(\Delta_{\text{best}}\leftarrow\infty\).
\(\lambda_{\nu;w_{1},w_{2}}\leftarrow\lambda_{0}\).
5. \(\mathbf{p}_{\nu;w_{1},w_{2}}\leftarrow\begin{pmatrix}q_{x;w_{1},w_{2}}\\ q_{y;w_{1},w_{2}} \end{pmatrix}\).
6. \(\hat{\mathbf{q}}_{\nu;w_{1},w_{2}}\leftarrow \begin{pmatrix}T_{⌑;x}\left(u_{x}=p_{\nu;w_{1},w_{2};0}, u_{y}=p_{\nu;w_{1},w_{2};1}\right)\\ T_{⌑;y}\left(u_{x}=p_{\nu;w_{1},w_{2};0},u_{y}=p_{\nu;w_{1},w_{2};1}\right) \end{pmatrix}\).
7. \(\boldsymbol{\chi}_{\nu;w_{1},w_{2}}\leftarrow \begin{pmatrix}q_{x;w_{1},w_{2}}-\hat{q}_{\nu;w_{1},w_{2};0}\\ q_{y;w_{1},w_{2}}-\hat{q}_{\nu;w_{1},w_{2};1} \end{pmatrix}\).
8. \(\mathbf{J}_{\nu;w_{1},w_{2}}\leftarrow \mathbf{J}_{⌑}\left(u_{x}=p_{\nu;w_{1},w_{2};0}, u_{y}=p_{\nu;w_{1},w_{2};1}\right)\).
9. \(\mathbf{H}_{\nu;w_{1},w_{2}}\leftarrow \mathbf{J}_{\nu;w_{1},w_{2}}\mathbf{J}_{\nu;w_{1},w_{2}}\).
10. \(\mathbf{D}_{\nu;w_{1},w_{2}}\leftarrow \lambda_{\nu;w_{1},w_{2}}\begin{pmatrix}H_{\nu;w_{1},w_{2};0,0} & 0\\ 0 & H_{\nu;w_{1},w_{2};1,1} \end{pmatrix}\).
11. \(\mathbf{A}_{\nu;w_{1},w_{2}}\leftarrow \mathbf{H}_{\nu;w_{1},w_{2}}+\mathbf{D}_{\nu;w_{1},w_{2}}\).
12. \(\mathbf{g}_{\nu;w_{1},w_{2}}\leftarrow \mathbf{J}_{\nu;w_{1},w_{2}}\boldsymbol{\chi}_{\nu;w_{1},w_{2}}\).
13. Solve \(\mathbf{A}_{\nu;w_{1},w_{2}} \mathbf{h}_{\nu;w_{1},w_{2}}=\mathbf{g}_{\nu;w_{1},w_{2}}\), for \(\mathbf{h}_{\nu;w_{1},w_{2}}\) via singular value decomposition.
14. \(\mathbf{p}_{\nu+1;w_{1},w_{2}}\leftarrow \mathbf{p}_{\nu;w_{1},w_{2}}+\mathbf{h}_{\nu;w_{1},w_{2}}\).
15. \(\hat{\mathbf{q}}_{\nu+1;w_{1},w_{2}}\leftarrow \begin{pmatrix}T_{⌑;x}\left(u_{x}=p_{\nu+1;w_{1},w_{2};0}, u_{y}=p_{\nu+1;w_{1},w_{2};1}\right)\\ T_{⌑;y}\left(u_{x}=p_{\nu+1;w_{1},w_{2};0}, u_{y}=p_{\nu+1;w_{1},w_{2};1}\right) \end{pmatrix}\).
16. \(\boldsymbol{\chi}_{\nu+1;w_{1},w_{2}}\leftarrow \begin{pmatrix}q_{x;w_{1},w_{2}}-\hat{q}_{\nu+1;w_{1},w_{2};0}\\ q_{y;w_{1},w_{2}}-\hat{q}_{\nu+1;w_{1},w_{2};1} \end{pmatrix}\).
17. \(\delta_{\nu+1;w_{1},w_{2}}\leftarrow \max\left(N_{w;x},N_{w;y}\right) \left|\boldsymbol{\chi}_{\nu+1;w_{1},w_{2}}\right|\).
18. \(\Delta_{\nu+1}\leftarrow \sum_{w_{1},w_{2}}\delta_{\nu+1;w_{1},w_{2}}\).
19. \(\boldsymbol{\rho}_{\nu+1;w_{1},w_{2}}\leftarrow \frac{\left\Vert \boldsymbol{\chi}_{\nu;w_{1},w_{2}}\right\Vert ^{2} -\left\Vert \boldsymbol{\chi}_{\nu+1;w_{1}, w_{2}}\right\Vert ^{2}}{\left|\mathbf{h}_{\nu;w_{1}, w_{2}}^{\text{T}}\left(\mathbf{D}_{\nu;w_{1}, w_{2}}\mathbf{h}_{\nu;w_{1},w_{2}}+\mathbf{g}_{\nu;w_{1}, w_{2}}\right)\right|}\).
20. \(\lambda_{\nu+1;w_{1},w_{2}}\leftarrow\begin{cases} \max\left(\frac{1}{\lambda_{\downarrow}}\lambda_{\nu+1;w_{1},w_{2}}, 10^{-7}\right), & \text{if }\boldsymbol{\rho}_{\nu+1;w_{1},w_{2}}>\epsilon_{\rho},\\ \min\left(\lambda_{\uparrow}\lambda_{\nu+1;w_{1},w_{2}},10^{7}\right), & \text{otherwise}. \end{cases}\)
21. \(N_{-}\leftarrow\begin{cases} 0, & \text{if }\Delta_{\nu+1}<\left(1-\epsilon_{-}\right)\Delta_{\text{best}},\\ N_{-}+1, & \text{otherwise}. \end{cases}\)
22. If \(N_{-}\ge N_{-;\max}\), then go to step 23. Otherwise, go to step 24.
23. If \(\delta_{\nu+1;w_{1},w_{2}}<\epsilon_{\chi}\), for all \(w_{1}\) and \(w_{2}\) in which \(\mathbf{p}_{\nu+1;w_{1},w_{2}}\in[0,1]\times\left[0,1\right]\), then go to step 29. Otherwise, go to step 25.
24. If \(\delta_{\nu+1;w_{1},w_{2}}<\epsilon_{\chi}\), for all \(w_{1}\) and \(w_{2}\), then go to step 29. Otherwise, go to step 25.
If \(\nu=N_{\nu}-1\), then go to step 32. Otherwise, go to step 26.
26. \(\Delta_{\text{best}}\leftarrow \max\left(\Delta_{\nu+1},\Delta_{\text{best}}\right)\).
\(\nu\leftarrow \nu+1\).
Go to step 8.
\(u_{x;w_{1},w_{2}}\leftarrow p_{\nu+1;w_{1},w_{2};0}\).
\(u_{y;w_{1},w_{2}}\leftarrow p_{\nu+1;w_{1},w_{2};1}\).
Stop algorithm without raising an exception.
Stop algorithm with an exception raised.
- Parameters:
- max_num_iterationsint, optional
The max number of iterations, \(N_{\nu}\), introduced in the summary documentation above.
- initial_dampingfloat, optional
The initial damping, \(\lambda_{0}\), introduced in the summary documentation above.
- factor_for_decreasing_dampingfloat, optional
The factor for decreasing damping, \(\lambda_{\downarrow}\), introduced in the summary documentation above.
- factor_for_increasing_dampingfloat, optional
The factor for increase damping, \(\lambda_{\uparrow}\), introduced in the summary documentation above.
- improvement_tolfloat, optional
The improvement tolerance, \(\epsilon_{\rho}\), introduced in the summary documentation above.
- rel_err_tolfloat, optional
The error tolerance, \(\epsilon_{\chi}\), introduced in the summary documentation above.
- plateau_tolfloat, optional
The plateau tolerance, \(\epsilon_{-}\), introduced in the summary documentation above.
- plateau_patienceint, optional
The plateau patience, \(N_{-;\max}\), introduced in the summary documentation above.
- skip_validation_and_conversionbool, optional
Let
validation_and_conversion_funcs
andcore_attrs
denote the attributesvalidation_and_conversion_funcs
andcore_attrs
respectively, both of which being dict objects.Let
params_to_be_mapped_to_core_attrs
denote the dict representation of the constructor parameters excluding the parameterskip_validation_and_conversion
, where each dict keykey
is a different constructor parameter name, excluding the name"skip_validation_and_conversion"
, andparams_to_be_mapped_to_core_attrs[key]
would yield the value of the constructor parameter with the name given bykey
.If
skip_validation_and_conversion
is set toFalse
, then for each keykey
inparams_to_be_mapped_to_core_attrs
,core_attrs[key]
is set tovalidation_and_conversion_funcs[key] (params_to_be_mapped_to_core_attrs)
.Otherwise, if
skip_validation_and_conversion
is set toTrue
, thencore_attrs
is set toparams_to_be_mapped_to_core_attrs.copy()
. This option is desired primarily when the user wants to avoid potentially expensive deep copies and/or conversions of the dict values ofparams_to_be_mapped_to_core_attrs
, as it is guaranteed that no copies or conversions are made in this case.
- Attributes:
core_attrs
dict: The “core attributes”.
de_pre_serialization_funcs
dict: The de-pre-serialization functions.
pre_serialization_funcs
dict: The pre-serialization functions.
validation_and_conversion_funcs
dict: The validation and conversion functions.
Methods
de_pre_serialize
([serializable_rep, ...])Construct an instance from a serializable representation.
dump
([filename, overwrite])Serialize instance and save the result in a JSON file.
dumps
()Serialize instance.
get_core_attrs
([deep_copy])Return the core attributes.
Return the de-pre-serialization functions.
Return the pre-serialization functions.
Return the validation and conversion functions.
load
([filename, skip_validation_and_conversion])Construct an instance from a serialized representation that is stored in a JSON file.
loads
([serialized_rep, ...])Construct an instance from a serialized representation.
Pre-serialize instance.
update
([new_core_attr_subset_candidate, ...])Update a subset of the core attributes.
Methods
Construct an instance from a serializable representation.
Serialize instance and save the result in a JSON file.
Serialize instance.
Return the core attributes.
Return the de-pre-serialization functions.
Return the pre-serialization functions.
Return the validation and conversion functions.
Construct an instance from a serialized representation that is stored in a JSON file.
Construct an instance from a serialized representation.
Pre-serialize instance.
Update a subset of the core attributes.
Attributes
dict: The "core attributes".
dict: The de-pre-serialization functions.
dict: The pre-serialization functions.
dict: The validation and conversion functions.
- property core_attrs
dict: The “core attributes”.
The keys of
core_attrs
are the same as the attributevalidation_and_conversion_funcs
, which is also a dict object.Note that
core_attrs
should be considered read-only.
- property de_pre_serialization_funcs
dict: The de-pre-serialization functions.
de_pre_serialization_funcs
has the same keys as the attributevalidation_and_conversion_funcs
, which is also a dict object.Let
validation_and_conversion_funcs
andpre_serialization_funcs
denote the attributesvalidation_and_conversion_funcs
pre_serialization_funcs
respectively, the last of which being a dict object as well.Let
core_attrs_candidate_1
be any dict object that has the same keys asvalidation_and_conversion_funcs
, where for each dict keykey
incore_attrs_candidate_1
,validation_and_conversion_funcs[key](core_attrs_candidate_1)
does not raise an exception.Let
serializable_rep
be a dict object that has the same keys ascore_attrs_candidate_1
, where for each dict keykey
incore_attrs_candidate_1
,serializable_rep[key]
is set topre_serialization_funcs[key](core_attrs_candidate_1[key])
.The items of
de_pre_serialization_funcs
are expected to be set to callable objects that would lead tode_pre_serialization_funcs[key](serializable_rep[key])
not raising an exception for each dict keykey
inserializable_rep
.Let
core_attrs_candidate_2
be a dict object that has the same keys asserializable_rep
, where for each dict keykey
invalidation_and_conversion_funcs
,core_attrs_candidate_2[key]
is set tode_pre_serialization_funcs[key](serializable_rep[key])
.The items of
de_pre_serialization_funcs
are also expected to be set to callable objects that would lead tovalidation_and_conversion_funcs[key](core_attrs_candidate_2)
not raising an exception for each dict keykey
incore_attrs_candidate_2
.Note that
de_pre_serialization_funcs
should be considered read-only.
- classmethod de_pre_serialize(serializable_rep={}, skip_validation_and_conversion=False)
Construct an instance from a serializable representation.
- Parameters:
- serializable_repdict, optional
A dict object that has the same keys as the attribute
validation_and_conversion_funcs
, which is also a dict object.Let
validation_and_conversion_funcs
andde_pre_serialization_funcs
denote the attributesvalidation_and_conversion_funcs
de_pre_serialization_funcs
respectively, the last of which being a dict object as well.The items of
serializable_rep
are expected to be objects that would lead tode_pre_serialization_funcs[key](serializable_rep[key])
not raising an exception for each dict keykey
inserializable_rep
.Let
core_attrs_candidate
be a dict object that has the same keys asserializable_rep
, where for each dict keykey
inserializable_rep
,core_attrs_candidate[key]
is set to de_pre_serialization_funcs[key](serializable_rep[key])``.The items of
serializable_rep
are also expected to be set to objects that would lead tovalidation_and_conversion_funcs[key](core_attrs_candidate)
not raising an exception for each dict keykey
inserializable_rep
.- skip_validation_and_conversionbool, optional
Let
core_attrs
denote the attributecore_attrs
, which is a dict object.If
skip_validation_and_conversion
is set toFalse
, then for each keykey
inserializable_rep
,core_attrs[key]
is set tovalidation_and_conversion_funcs[key] (core_attrs_candidate)
, withvalidation_and_conversion_funcs
andcore_attrs_candidate_1
being introduced in the above description ofserializable_rep
.Otherwise, if
skip_validation_and_conversion
is set toTrue
, thencore_attrs
is set tocore_attrs_candidate.copy()
. This option is desired primarily when the user wants to avoid potentially expensive deep copies and/or conversions of the dict values ofcore_attrs_candidate
, as it is guaranteed that no copies or conversions are made in this case.
- Returns:
- instance_of_current_clsCurrent class
An instance constructed from the serializable representation
serializable_rep
.
- dump(filename='serialized_rep_of_fancytype.json', overwrite=False)
Serialize instance and save the result in a JSON file.
- Parameters:
- filenamestr, optional
The relative or absolute path to the JSON file in which to store the serialized representation of an instance.
- overwritebool, optional
If
overwrite
is set toFalse
and a file exists at the pathfilename
, then the serialized instance is not written to that file and an exception is raised. Otherwise, the serialized instance will be written to that file barring no other issues occur.
- Returns:
- dumps()
Serialize instance.
- Returns:
- serialized_repdict
A serialized representation of an instance.
- get_core_attrs(deep_copy=True)
Return the core attributes.
- Parameters:
- deep_copybool, optional
Let
core_attrs
denote the attributecore_attrs
, which is a dict object.If
deep_copy
is set toTrue
, then a deep copy ofcore_attrs
is returned. Otherwise, a shallow copy ofcore_attrs
is returned.
- Returns:
- core_attrsdict
The attribute
core_attrs
.
- classmethod get_de_pre_serialization_funcs()[source]
Return the de-pre-serialization functions.
- Returns:
- de_pre_serialization_funcsdict
The attribute
de_pre_serialization_funcs
.
- classmethod get_pre_serialization_funcs()[source]
Return the pre-serialization functions.
- Returns:
- pre_serialization_funcsdict
The attribute
pre_serialization_funcs
.
- classmethod get_validation_and_conversion_funcs()[source]
Return the validation and conversion functions.
- Returns:
- validation_and_conversion_funcsdict
The attribute
validation_and_conversion_funcs
.
- classmethod load(filename='serialized_rep_of_fancytype.json', skip_validation_and_conversion=False)
Construct an instance from a serialized representation that is stored in a JSON file.
Users can save serialized representations to JSON files using the method
fancytypes.PreSerializable.dump()
.- Parameters:
- filenamestr, optional
The relative or absolute path to the JSON file that is storing the serialized representation of an instance.
filename
is expected to be such thatjson.load(open(filename, "r"))
does not raise an exception.Let
serializable_rep=json.load(open(filename, "r"))
.Let
validation_and_conversion_funcs
andde_pre_serialization_funcs
denote the attributesvalidation_and_conversion_funcs
de_pre_serialization_funcs
respectively, both of which being dict objects as well.filename
is also expected to be such thatde_pre_serialization_funcs[key](serializable_rep[key])
does not raise an exception for each dict keykey
inde_pre_serialization_funcs
.Let
core_attrs_candidate
be a dict object that has the same keys asde_pre_serialization_funcs
, where for each dict keykey
inserializable_rep
,core_attrs_candidate[key]
is set to de_pre_serialization_funcs[key](serializable_rep[key])``.filename
is also expected to be such thatvalidation_and_conversion_funcs[key](core_attrs_candidate)
does not raise an exception for each dict keykey
inserializable_rep
.- skip_validation_and_conversionbool, optional
Let
core_attrs
denote the attributecore_attrs
, which is a dict object.Let
core_attrs_candidate
be as defined in the above description offilename
.If
skip_validation_and_conversion
is set toFalse
, then for each keykey
incore_attrs_candidate
,core_attrs[key]
is set tovalidation_and_conversion_funcs[key] (core_attrs_candidate)
, , withvalidation_and_conversion_funcs
andcore_attrs_candidate
being introduced in the above description offilename
.Otherwise, if
skip_validation_and_conversion
is set toTrue
, thencore_attrs
is set tocore_attrs_candidate.copy()
. This option is desired primarily when the user wants to avoid potentially expensive deep copies and/or conversions of the dict values ofcore_attrs_candidate
, as it is guaranteed that no copies or conversions are made in this case.
- Returns:
- instance_of_current_clsCurrent class
An instance constructed from the serialized representation stored in the JSON file.
- classmethod loads(serialized_rep='{}', skip_validation_and_conversion=False)
Construct an instance from a serialized representation.
Users can generate serialized representations using the method
dumps()
.- Parameters:
- serialized_repstr | bytes | bytearray, optional
The serialized representation.
serialized_rep
is expected to be such thatjson.loads(serialized_rep)
does not raise an exception.Let
serializable_rep=json.loads(serialized_rep)
.Let
validation_and_conversion_funcs
andde_pre_serialization_funcs
denote the attributesvalidation_and_conversion_funcs
de_pre_serialization_funcs
respectively, both of which being dict objects as well.serialized_rep
is also expected to be such thatde_pre_serialization_funcs[key](serializable_rep[key])
does not raise an exception for each dict keykey
inde_pre_serialization_funcs
.Let
core_attrs_candidate
be a dict object that has the same keys asserializable_rep
, where for each dict keykey
inde_pre_serialization_funcs
,core_attrs_candidate[key]
is set to de_pre_serialization_funcs[key](serializable_rep[key])``.serialized_rep
is also expected to be such thatvalidation_and_conversion_funcs[key](core_attrs_candidate)
does not raise an exception for each dict keykey
inserializable_rep
.- skip_validation_and_conversionbool, optional
Let
core_attrs
denote the attributecore_attrs
, which is a dict object.If
skip_validation_and_conversion
is set toFalse
, then for each keykey
incore_attrs_candidate
,core_attrs[key]
is set tovalidation_and_conversion_funcs[key] (core_attrs_candidate)
, withvalidation_and_conversion_funcs
andcore_attrs_candidate_1
being introduced in the above description ofserialized_rep
.Otherwise, if
skip_validation_and_conversion
is set toTrue
, thencore_attrs
is set tocore_attrs_candidate.copy()
. This option is desired primarily when the user wants to avoid potentially expensive deep copies and/or conversions of the dict values ofcore_attrs_candidate
, as it is guaranteed that no copies or conversions are made in this case.
- Returns:
- instance_of_current_clsCurrent class
An instance constructed from the serialized representation.
- property pre_serialization_funcs
dict: The pre-serialization functions.
pre_serialization_funcs
has the same keys as the attributevalidation_and_conversion_funcs
, which is also a dict object.Let
validation_and_conversion_funcs
andcore_attrs
denote the attributesvalidation_and_conversion_funcs
andcore_attrs
respectively, the last of which being a dict object as well.For each dict key
key
incore_attrs
,pre_serialization_funcs[key](core_attrs[key])
is expected to yield a serializable object, i.e. it should yield an object that can be passed into the functionjson.dumps
without raising an exception.Note that
pre_serialization_funcs
should be considered read-only.
- pre_serialize()
Pre-serialize instance.
- Returns:
- serializable_repdict
A serializable representation of an instance.
- update(new_core_attr_subset_candidate={}, skip_validation_and_conversion=False)
Update a subset of the core attributes.
- Parameters:
- new_core_attr_subset_candidatedict, optional
A dict object.
- skip_validation_and_conversionbool, optional
Let
validation_and_conversion_funcs
andcore_attrs
denote the attributesvalidation_and_conversion_funcs
andcore_attrs
respectively, both of which being dict objects.If
skip_validation_and_conversion
is set toFalse
, then for each keykey
incore_attrs
that is also innew_core_attr_subset_candidate
,core_attrs[key]
is set tovalidation_and_conversion_funcs[key] (new_core_attr_subset_candidate)
.Otherwise, if
skip_validation_and_conversion
is set toTrue
, then for each keykey
incore_attrs
that is also innew_core_attr_subset_candidate
,core_attrs[key]
is set tonew_core_attr_subset_candidate[key]
. This option is desired primarily when the user wants to avoid potentially expensive deep copies and/or conversions of the dict values ofnew_core_attr_subset_candidate
, as it is guaranteed that no copies or conversions are made in this case.
- property validation_and_conversion_funcs
dict: The validation and conversion functions.
The keys of
validation_and_conversion_funcs
are the names of the constructor parameters, excludingskip_validation_and_conversion
if it exists as a construction parameter.Let
core_attrs
denote the attributecore_attrs
, which is also a dict object.For each dict key
key
incore_attrs
,validation_and_conversion_funcs[key](core_attrs)
is expected to not raise an exception.Note that
validation_and_conversion_funcs
should be considered read-only.