@@ -78,16 +78,16 @@ def CheckedArray(axes: AxisCollection, dtype: np.dtype = float) -> Type[Array]:
78
78
# to get autocompletion from PyCharm
79
79
from larray .core .checked import CheckedArrayImpl
80
80
"""
81
- Represents a constrained array.
81
+ Represents a checked array.
82
82
Its axes are assumed to be "frozen", meaning they are constant all along the execution of the program.
83
83
A constraint on the dtype of the data can be also specified.
84
84
85
85
Parameters
86
86
----------
87
87
axes: AxisCollection
88
- Axes of the constrained array.
88
+ Axes of the checked array.
89
89
dtype: data-type, optional
90
- Data-type for the constrained array. Defaults to float.
90
+ Data-type for the checked array. Defaults to float.
91
91
92
92
Returns
93
93
-------
@@ -195,12 +195,11 @@ class CheckedSession(Session, AbstractCheckedSession, metaclass=ModelMetaclass):
195
195
This class is intended to be inherited by user defined classes in which the variables of a model are declared.
196
196
Each declared variable is constrained by a type defined explicitly or deduced from the given default value
197
197
(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.
200
199
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.
204
203
205
204
By declaring variables, users will speed up the development of their models using the auto-completion
206
205
(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):
254
253
... births: Array
255
254
... # --- declare variables with a default value ---
256
255
... # 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'
259
258
... 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
262
261
... # constant all along the execution of the program.
263
262
... 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).
266
265
... deaths: CheckedArray((AGE, GENDER, TIME), dtype=int) = 0
267
266
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.
271
270
>>> m = ModelVariables(
272
271
... variant_name = variant_name,
273
272
... birth_rate = zeros((AGE, GENDER)),
@@ -276,8 +275,17 @@ class CheckedSession(Session, AbstractCheckedSession, metaclass=ModelMetaclass):
276
275
... )
277
276
278
277
>>> # ==== 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.
281
289
>>> # example 1: Let's say we want to calculate the new births for the year 2025 and we assume that
282
290
>>> # the birth rate only differ by gender.
283
291
>>> # 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):
335
343
dtype: int32
336
344
memory used: 968 bytes
337
345
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
339
347
>>> # but this must be done with caution.
340
348
>>> m.undeclared_var = 'undeclared_var'
341
349
@@ -345,9 +353,9 @@ class CheckedSession(Session, AbstractCheckedSession, metaclass=ModelMetaclass):
345
353
dumping variant_name ... done
346
354
dumping birth_rate ... done
347
355
dumping births ... done
348
- dumping target_age ... done
349
356
dumping mortality_rate ... done
350
357
dumping deaths ... done
358
+ dumping target_age ... done
351
359
dumping population ... done
352
360
dumping undeclared_var ... done
353
361
"""
0 commit comments