Skip to content

Base¤

The Base class is the foundational object of Zodiax and is what allows for a path-based pytree interface. Classes that inherit from Base will gain methods that allow for operations and functions to be applied to leaves specified by their paths. Here is a summary of the methods:

Getter Methods

value = pytree.get(paths)

Setter Methods

pytree = pytree.set(paths, values)

Arithmetic Methods

pytree = pytree.add(paths, values)
pytree = pytree.multiply(paths, values)
pytree = pytree.divide(paths, values)
pytree = pytree.power(paths, values)
pytree = pytree.min(paths, values)
pytree = pytree.max(paths, values)

Full API

Bases: Module

Extend the Equninox.Module class to give a user-friendly 'param based' API for working with pytrees by adding a series of methods used to interface with the leaves of the pytree using parameters.

Source code in zodiax/base.py
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
class Base(Module):
    """
    Extend the Equninox.Module class to give a user-friendly 'param based' API
    for working with pytrees by adding a series of methods used to interface
    with the leaves of the pytree using parameters.
    """

    def get(self: Base, parameters: Params) -> Any:
        """
        Get the leaf specified by param.

        Parameters
        ----------
        parameters : Params
            A list/tuple of nested parameters to unwrap.

        Returns
        -------
        leaf, leaves : Any, list
            The leaf or list of leaves specified by parameters.
        """
        new_parameters = _format(parameters)
        values = _get_leaves(self, new_parameters)
        return values[0] if len(new_parameters) == 1 else values

    def set(self: Base, parameters: Params, values: Union[List[Any], Any]) -> Base:
        """
        Set the leaves specified by parameters with values.

        Parameters
        ----------
        parameters : Params
            A param or list of parameters or list of nested parameters.
        values : Union[List[Any], Any]
            The list of values to set at the leaves specified by parameters.

        Returns
        -------
        pytree : Base
            The pytree with leaves specified by parameters updated with values.
        """
        # Allow None inputs
        if values is None:
            values = [None]
            if isinstance(parameters, str):
                parameters = [parameters]
        new_parameters, new_values = _format(parameters, values)

        # Define 'where' function and update pytree
        def leaves_fn(pytree):
            return _get_leaves(pytree, new_parameters)

        return tree_at(leaves_fn, self, new_values, is_leaf=lambda leaf: leaf is None)

    def update(self: Base, dict: dict) -> Base:
        """
        Calls the set method to update the leaves specified by the keys
        of the dictionary with the values of the dictionary.

        Parameters
        ----------
        dict : dict
            The dictionary of parameters and values to update the leaves with.

        Returns
        -------
        pytree : Base
            The pytree with updated paramaters.
        """

        # Grabbing the parameters and values from the dictionary
        parameters, values = list(dict.keys()), list(dict.values())

        # Calling the set method
        return self.set(parameters, values)

    def add(self: Base, parameters: Params, values: Union[List[Any], Any]) -> Base:
        """
        Add to the the leaves specified by parameters with values.

        Parameters
        ----------
        parameters : Params
            A param or list of parameters or list of nested parameters.
        values : Union[List[Any], Any]
            The list of values to add to the leaves specified by parameters.

        Returns
        -------
        pytree : Base
            The pytree with values added to leaves specified by parameters.
        """
        new_parameters, new_values = _format(parameters, values)
        new_values = [
            leaf + value
            for value, leaf in zip(new_values, _get_leaves(self, new_parameters))
        ]

        # Define 'where' function and update pytree
        def leaves_fn(pytree):
            return _get_leaves(pytree, new_parameters)

        return tree_at(leaves_fn, self, new_values, is_leaf=lambda leaf: leaf is None)

    def multiply(self: Base, parameters: Params, values: Union[List[Any], Any]) -> Base:
        """
        Multiplies the the leaves specified by parameters with values.

        Parameters
        ----------
        parameters : Params
            A param or list of parameters or list of nested parameters.
        values : Union[List[Any], Any]
            The list of values to multiply the leaves specified by parameters.

        Returns
        -------
        pytree : Base
            The pytree with values multiplied by leaves specified by parameters.
        """
        new_parameters, new_values = _format(parameters, values)
        new_values = [
            leaf * value
            for value, leaf in zip(new_values, _get_leaves(self, new_parameters))
        ]

        # Define 'where' function and update pytree
        def leaves_fn(pytree):
            return _get_leaves(pytree, new_parameters)

        return tree_at(leaves_fn, self, new_values, is_leaf=lambda leaf: leaf is None)

    def divide(self: Base, parameters: Params, values: Union[List[Any], Any]) -> Base:
        """
        Divides the the leaves specified by parameters with values.

        Parameters
        ----------
        parameters : Params
            A param or list of parameters or list of nested parameters.
        values : Union[List[Any], Any]
            The list of values to divide the leaves specified by parameters.

        Returns
        -------
        pytree : Base
            The pytree with values divided by leaves specified by parameters.
        """
        new_parameters, new_values = _format(parameters, values)
        new_values = [
            leaf / value
            for value, leaf in zip(new_values, _get_leaves(self, new_parameters))
        ]

        # Define 'where' function and update pytree
        def leaves_fn(pytree):
            return _get_leaves(pytree, new_parameters)

        return tree_at(leaves_fn, self, new_values, is_leaf=lambda leaf: leaf is None)

    def power(self: Base, parameters: Params, values: Union[List[Any], Any]) -> Base:
        """
        Raises th leaves specified by parameters to the power of values.

        Parameters
        ----------
        parameters : Params
            A param or list of parameters or list of nested parameters.
        values : Union[List[Any], Any]
            The list of values to take the leaves specified by parameters to the
            power of.

        Returns
        -------
        pytree : Base
            The pytree with the leaves specified by parameters raised to the power
            of values.
        """
        new_parameters, new_values = _format(parameters, values)
        new_values = [
            leaf**value
            for value, leaf in zip(new_values, _get_leaves(self, new_parameters))
        ]

        # Define 'where' function and update pytree
        def leaves_fn(pytree):
            return _get_leaves(pytree, new_parameters)

        return tree_at(leaves_fn, self, new_values, is_leaf=lambda leaf: leaf is None)

    def min(self: Base, parameters: Params, values: Union[List[Any], Any]) -> Base:
        """
        Updates the leaves specified by parameters with the minimum value of the
        leaves and values.

        Parameters
        ----------
        parameters : Params
            A param or list of parameters or list of nested parameters.
        values : Union[List[Any], Any]
            The list of values to take the minimum of and the leaf.

        Returns
        -------
        pytree : Base
            The pytree with the leaves specified by parameters updated with the
            minimum value of the leaf and values.
        """
        new_parameters, new_values = _format(parameters, values)
        new_values = [
            np.minimum(leaf, value)
            for value, leaf in zip(new_values, _get_leaves(self, new_parameters))
        ]

        # Define 'where' function and update pytree
        def leaves_fn(pytree):
            return _get_leaves(pytree, new_parameters)

        return tree_at(leaves_fn, self, new_values, is_leaf=lambda leaf: leaf is None)

    def max(self: Base, parameters: Params, values: Union[List[Any], Any]) -> Base:
        """
        Updates the leaves specified by parameters with the maximum value of the
        leaves and values.


        Parameters
        ----------
        parameters : Params
            A param or list of parameters or list of nested parameters.
        values : Union[List[Any], Any]
            The list of values to take the maximum of and the leaf.

        Returns
        -------
        pytree : Base
            The pytree with the leaves specified by parameters updated with the
            maximum value of the leaf and values.
        """
        new_parameters, new_values = _format(parameters, values)
        new_values = [
            np.maximum(leaf, value)
            for value, leaf in zip(new_values, _get_leaves(self, new_parameters))
        ]

        # Define 'where' function and update pytree
        def leaves_fn(pytree):
            return _get_leaves(pytree, new_parameters)

        return tree_at(leaves_fn, self, new_values, is_leaf=lambda leaf: leaf is None)

add(parameters, values) ¤

Add to the the leaves specified by parameters with values.

Parameters:

Name Type Description Default
parameters Params

A param or list of parameters or list of nested parameters.

required
values Union[List[Any], Any]

The list of values to add to the leaves specified by parameters.

required

Returns:

Name Type Description
pytree Base

The pytree with values added to leaves specified by parameters.

Source code in zodiax/base.py
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
def add(self: Base, parameters: Params, values: Union[List[Any], Any]) -> Base:
    """
    Add to the the leaves specified by parameters with values.

    Parameters
    ----------
    parameters : Params
        A param or list of parameters or list of nested parameters.
    values : Union[List[Any], Any]
        The list of values to add to the leaves specified by parameters.

    Returns
    -------
    pytree : Base
        The pytree with values added to leaves specified by parameters.
    """
    new_parameters, new_values = _format(parameters, values)
    new_values = [
        leaf + value
        for value, leaf in zip(new_values, _get_leaves(self, new_parameters))
    ]

    # Define 'where' function and update pytree
    def leaves_fn(pytree):
        return _get_leaves(pytree, new_parameters)

    return tree_at(leaves_fn, self, new_values, is_leaf=lambda leaf: leaf is None)

divide(parameters, values) ¤

Divides the the leaves specified by parameters with values.

Parameters:

Name Type Description Default
parameters Params

A param or list of parameters or list of nested parameters.

required
values Union[List[Any], Any]

The list of values to divide the leaves specified by parameters.

required

Returns:

Name Type Description
pytree Base

The pytree with values divided by leaves specified by parameters.

Source code in zodiax/base.py
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
def divide(self: Base, parameters: Params, values: Union[List[Any], Any]) -> Base:
    """
    Divides the the leaves specified by parameters with values.

    Parameters
    ----------
    parameters : Params
        A param or list of parameters or list of nested parameters.
    values : Union[List[Any], Any]
        The list of values to divide the leaves specified by parameters.

    Returns
    -------
    pytree : Base
        The pytree with values divided by leaves specified by parameters.
    """
    new_parameters, new_values = _format(parameters, values)
    new_values = [
        leaf / value
        for value, leaf in zip(new_values, _get_leaves(self, new_parameters))
    ]

    # Define 'where' function and update pytree
    def leaves_fn(pytree):
        return _get_leaves(pytree, new_parameters)

    return tree_at(leaves_fn, self, new_values, is_leaf=lambda leaf: leaf is None)

get(parameters) ¤

Get the leaf specified by param.

Parameters:

Name Type Description Default
parameters Params

A list/tuple of nested parameters to unwrap.

required

Returns:

Type Description
leaf, leaves : Any, list

The leaf or list of leaves specified by parameters.

Source code in zodiax/base.py
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
def get(self: Base, parameters: Params) -> Any:
    """
    Get the leaf specified by param.

    Parameters
    ----------
    parameters : Params
        A list/tuple of nested parameters to unwrap.

    Returns
    -------
    leaf, leaves : Any, list
        The leaf or list of leaves specified by parameters.
    """
    new_parameters = _format(parameters)
    values = _get_leaves(self, new_parameters)
    return values[0] if len(new_parameters) == 1 else values

max(parameters, values) ¤

Updates the leaves specified by parameters with the maximum value of the leaves and values.

Parameters:

Name Type Description Default
parameters Params

A param or list of parameters or list of nested parameters.

required
values Union[List[Any], Any]

The list of values to take the maximum of and the leaf.

required

Returns:

Name Type Description
pytree Base

The pytree with the leaves specified by parameters updated with the maximum value of the leaf and values.

Source code in zodiax/base.py
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
def max(self: Base, parameters: Params, values: Union[List[Any], Any]) -> Base:
    """
    Updates the leaves specified by parameters with the maximum value of the
    leaves and values.


    Parameters
    ----------
    parameters : Params
        A param or list of parameters or list of nested parameters.
    values : Union[List[Any], Any]
        The list of values to take the maximum of and the leaf.

    Returns
    -------
    pytree : Base
        The pytree with the leaves specified by parameters updated with the
        maximum value of the leaf and values.
    """
    new_parameters, new_values = _format(parameters, values)
    new_values = [
        np.maximum(leaf, value)
        for value, leaf in zip(new_values, _get_leaves(self, new_parameters))
    ]

    # Define 'where' function and update pytree
    def leaves_fn(pytree):
        return _get_leaves(pytree, new_parameters)

    return tree_at(leaves_fn, self, new_values, is_leaf=lambda leaf: leaf is None)

min(parameters, values) ¤

Updates the leaves specified by parameters with the minimum value of the leaves and values.

Parameters:

Name Type Description Default
parameters Params

A param or list of parameters or list of nested parameters.

required
values Union[List[Any], Any]

The list of values to take the minimum of and the leaf.

required

Returns:

Name Type Description
pytree Base

The pytree with the leaves specified by parameters updated with the minimum value of the leaf and values.

Source code in zodiax/base.py
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
def min(self: Base, parameters: Params, values: Union[List[Any], Any]) -> Base:
    """
    Updates the leaves specified by parameters with the minimum value of the
    leaves and values.

    Parameters
    ----------
    parameters : Params
        A param or list of parameters or list of nested parameters.
    values : Union[List[Any], Any]
        The list of values to take the minimum of and the leaf.

    Returns
    -------
    pytree : Base
        The pytree with the leaves specified by parameters updated with the
        minimum value of the leaf and values.
    """
    new_parameters, new_values = _format(parameters, values)
    new_values = [
        np.minimum(leaf, value)
        for value, leaf in zip(new_values, _get_leaves(self, new_parameters))
    ]

    # Define 'where' function and update pytree
    def leaves_fn(pytree):
        return _get_leaves(pytree, new_parameters)

    return tree_at(leaves_fn, self, new_values, is_leaf=lambda leaf: leaf is None)

multiply(parameters, values) ¤

Multiplies the the leaves specified by parameters with values.

Parameters:

Name Type Description Default
parameters Params

A param or list of parameters or list of nested parameters.

required
values Union[List[Any], Any]

The list of values to multiply the leaves specified by parameters.

required

Returns:

Name Type Description
pytree Base

The pytree with values multiplied by leaves specified by parameters.

Source code in zodiax/base.py
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
def multiply(self: Base, parameters: Params, values: Union[List[Any], Any]) -> Base:
    """
    Multiplies the the leaves specified by parameters with values.

    Parameters
    ----------
    parameters : Params
        A param or list of parameters or list of nested parameters.
    values : Union[List[Any], Any]
        The list of values to multiply the leaves specified by parameters.

    Returns
    -------
    pytree : Base
        The pytree with values multiplied by leaves specified by parameters.
    """
    new_parameters, new_values = _format(parameters, values)
    new_values = [
        leaf * value
        for value, leaf in zip(new_values, _get_leaves(self, new_parameters))
    ]

    # Define 'where' function and update pytree
    def leaves_fn(pytree):
        return _get_leaves(pytree, new_parameters)

    return tree_at(leaves_fn, self, new_values, is_leaf=lambda leaf: leaf is None)

power(parameters, values) ¤

Raises th leaves specified by parameters to the power of values.

Parameters:

Name Type Description Default
parameters Params

A param or list of parameters or list of nested parameters.

required
values Union[List[Any], Any]

The list of values to take the leaves specified by parameters to the power of.

required

Returns:

Name Type Description
pytree Base

The pytree with the leaves specified by parameters raised to the power of values.

Source code in zodiax/base.py
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
def power(self: Base, parameters: Params, values: Union[List[Any], Any]) -> Base:
    """
    Raises th leaves specified by parameters to the power of values.

    Parameters
    ----------
    parameters : Params
        A param or list of parameters or list of nested parameters.
    values : Union[List[Any], Any]
        The list of values to take the leaves specified by parameters to the
        power of.

    Returns
    -------
    pytree : Base
        The pytree with the leaves specified by parameters raised to the power
        of values.
    """
    new_parameters, new_values = _format(parameters, values)
    new_values = [
        leaf**value
        for value, leaf in zip(new_values, _get_leaves(self, new_parameters))
    ]

    # Define 'where' function and update pytree
    def leaves_fn(pytree):
        return _get_leaves(pytree, new_parameters)

    return tree_at(leaves_fn, self, new_values, is_leaf=lambda leaf: leaf is None)

set(parameters, values) ¤

Set the leaves specified by parameters with values.

Parameters:

Name Type Description Default
parameters Params

A param or list of parameters or list of nested parameters.

required
values Union[List[Any], Any]

The list of values to set at the leaves specified by parameters.

required

Returns:

Name Type Description
pytree Base

The pytree with leaves specified by parameters updated with values.

Source code in zodiax/base.py
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
def set(self: Base, parameters: Params, values: Union[List[Any], Any]) -> Base:
    """
    Set the leaves specified by parameters with values.

    Parameters
    ----------
    parameters : Params
        A param or list of parameters or list of nested parameters.
    values : Union[List[Any], Any]
        The list of values to set at the leaves specified by parameters.

    Returns
    -------
    pytree : Base
        The pytree with leaves specified by parameters updated with values.
    """
    # Allow None inputs
    if values is None:
        values = [None]
        if isinstance(parameters, str):
            parameters = [parameters]
    new_parameters, new_values = _format(parameters, values)

    # Define 'where' function and update pytree
    def leaves_fn(pytree):
        return _get_leaves(pytree, new_parameters)

    return tree_at(leaves_fn, self, new_values, is_leaf=lambda leaf: leaf is None)

update(dict) ¤

Calls the set method to update the leaves specified by the keys of the dictionary with the values of the dictionary.

Parameters:

Name Type Description Default
dict dict

The dictionary of parameters and values to update the leaves with.

required

Returns:

Name Type Description
pytree Base

The pytree with updated paramaters.

Source code in zodiax/base.py
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
def update(self: Base, dict: dict) -> Base:
    """
    Calls the set method to update the leaves specified by the keys
    of the dictionary with the values of the dictionary.

    Parameters
    ----------
    dict : dict
        The dictionary of parameters and values to update the leaves with.

    Returns
    -------
    pytree : Base
        The pytree with updated paramaters.
    """

    # Grabbing the parameters and values from the dictionary
    parameters, values = list(dict.keys()), list(dict.values())

    # Calling the set method
    return self.set(parameters, values)