Skip to content

Commit fcf8138

Browse files
committed
updated documentation and doctests of CheckedArray and CheckedSession classes
1 parent b19b363 commit fcf8138

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

larray/core/checked.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,16 @@ def CheckedArray(axes: AxisCollection, dtype: np.dtype = float) -> Type[Array]:
7878
# to get autocompletion from PyCharm
7979
from larray.core.checked import CheckedArrayImpl
8080
"""
81-
Represents a constrained array.
81+
Represents a checked array.
8282
Its axes are assumed to be "frozen", meaning they are constant all along the execution of the program.
8383
A constraint on the dtype of the data can be also specified.
8484
8585
Parameters
8686
----------
8787
axes: AxisCollection
88-
Axes of the constrained array.
88+
Axes of the checked array.
8989
dtype: data-type, optional
90-
Data-type for the constrained array. Defaults to float.
90+
Data-type for the checked array. Defaults to float.
9191
9292
Returns
9393
-------
@@ -195,12 +195,11 @@ class CheckedSession(Session, AbstractCheckedSession, metaclass=ModelMetaclass):
195195
This class is intended to be inherited by user defined classes in which the variables of a model are declared.
196196
Each declared variable is constrained by a type defined explicitly or deduced from the given default value
197197
(see examples below).
198-
All classes inheriting from `CheckedSession` will have access to all methods of the
199-
:py:class:`Session` class.
198+
All classes inheriting from `CheckedSession` will have access to all methods of the :py:class:`Session` class.
200199
201-
The special :py:funct:`ConstantAxesArray` type represents an Array object with constant axes.
202-
This prevents users from modifying the dimensions and/or labels of an array by mistake and make sure that
203-
the definition of an array remains always valid in the model.
200+
The special :py:funct:`CheckedArray` type represents an Array object with fixed axes and/or dtype.
201+
This prevents users from modifying the dimensions (and labels) and/or the dtype of an array by mistake
202+
and make sure that the definition of an array remains always valid in the model.
204203
205204
By declaring variables, users will speed up the development of their models using the auto-completion
206205
(the feature in which development tools like PyCharm try to predict the variable or function a user intends
@@ -254,20 +253,20 @@ class CheckedSession(Session, AbstractCheckedSession, metaclass=ModelMetaclass):
254253
... births: Array
255254
... # --- declare variables with a default value ---
256255
... # The default value will be used to set the variable if no value is passed at instantiation (see below).
257-
... # Such variable will be constrained by the type deduced from its default value.
258-
... target_age: Group = AGE[:2] >> '0-2'
256+
... # Their type is deduced from their default value and cannot be changed at runtime.
257+
... target_age = AGE[:2] >> '0-2'
259258
... population = zeros((AGE, GENDER, TIME), dtype=int)
260-
... # --- declare constrained arrays ---
261-
... # the constrained arrays have axes assumed to be "frozen", meaning they are
259+
... # --- declare checked arrays ---
260+
... # The checked arrays have axes assumed to be "frozen", meaning they are
262261
... # constant all along the execution of the program.
263262
... mortality_rate: CheckedArray((AGE, GENDER))
264-
... # for constrained arrays, the default value can be given as a scalar.
265-
... # A dtype can be also optionally specified (defaults to float).
263+
... # For checked arrays, the default value can be given as a scalar.
264+
... # Optionally, a dtype can be also specified (defaults to float).
266265
... deaths: CheckedArray((AGE, GENDER, TIME), dtype=int) = 0
267266
268-
>>> variant_name = 'variant_1'
269-
>>> # instantiation --> create an instance of the ModelVariables class
270-
>>> # all variables declared without a default value must be set
267+
>>> variant_name = "baseline"
268+
>>> # Instantiation --> create an instance of the ModelVariables class.
269+
>>> # Warning: All variables declared without a default value must be set.
271270
>>> m = ModelVariables(
272271
... variant_name = variant_name,
273272
... birth_rate = zeros((AGE, GENDER)),
@@ -276,8 +275,17 @@ class CheckedSession(Session, AbstractCheckedSession, metaclass=ModelMetaclass):
276275
... )
277276
278277
>>> # ==== model ====
279-
>>> # axes and dtype of arrays 'birth_rate', 'births' and 'population' are not protected,
280-
>>> # leading to potentially unexpected behavior of the model.
278+
>>> # In the definition of ModelVariables, the 'birth_rate' variable, has been declared as an Array object.
279+
>>> # This means that the 'birth_rate' variable will always remain of type Array.
280+
>>> # Any attempt to assign a non-Array value to 'birth_rate' will make the program to crash.
281+
>>> m.birth_rate = Array([0.045, 0.055], GENDER) # OK
282+
>>> m.birth_rate = [0.045, 0.055] # Fails
283+
Traceback (most recent call last):
284+
...
285+
pydantic.errors.ArbitraryTypeError: instance of Array expected
286+
>>> # However, the arrays 'birth_rate', 'births' and 'population' have not been declared as 'CheckedArray'.
287+
>>> # Thus, axes and dtype of these arrays are not protected, leading to potentially unexpected behavior
288+
>>> # of the model.
281289
>>> # example 1: Let's say we want to calculate the new births for the year 2025 and we assume that
282290
>>> # the birth rate only differ by gender.
283291
>>> # In the line below, we add an additional TIME axis to 'birth_rate' while it was initialized
@@ -335,7 +343,7 @@ class CheckedSession(Session, AbstractCheckedSession, metaclass=ModelMetaclass):
335343
dtype: int32
336344
memory used: 968 bytes
337345
338-
>>> # note that it still possible to add undeclared variables to a constrained session
346+
>>> # note that it still possible to add undeclared variables to a checked session
339347
>>> # but this must be done with caution.
340348
>>> m.undeclared_var = 'undeclared_var'
341349
@@ -345,9 +353,9 @@ class CheckedSession(Session, AbstractCheckedSession, metaclass=ModelMetaclass):
345353
dumping variant_name ... done
346354
dumping birth_rate ... done
347355
dumping births ... done
348-
dumping target_age ... done
349356
dumping mortality_rate ... done
350357
dumping deaths ... done
358+
dumping target_age ... done
351359
dumping population ... done
352360
dumping undeclared_var ... done
353361
"""

0 commit comments

Comments
 (0)