Pattern Parent Class

Every pattern described herin inherits from the parent Pattern class and implements its methods.

class paragami.base_patterns.Pattern(flat_length, free_flat_length, free_default=None)

A abstract class for a parameter pattern.

See derived classes for examples.

__init__(flat_length, free_flat_length, free_default=None)
Parameters:
flat_length : int

The length of a non-free flattened vector.

free_flat_length : int

The length of a free flattened vector.

as_dict()

Return a dictionary of attributes describing the pattern.

The dictionary should completely describe the pattern in the sense that if the contents of two patterns’ dictionaries are identical the patterns should be considered identical.

If the keys of the returned dictionary match the arguments to __init__, then the default methods for to_json and from_json will work with no additional modification.

empty(valid)

Return an empty parameter in its folded shape.

Parameters:
valid : bool

Whether or folded shape should be filled with valid values.

Returns:
folded_val : Folded value

A parameter value in its original folded shape.

empty_bool(value)

Return folded shape containing booleans.

Parameters:
value : bool

The value with which to fill the folded shape.

Returns:
folded_bool : Folded value

A boolean value in its original folded shape.

flat_indices(folded_bool, free=None)

Get which flattened indices correspond to which folded values.

Parameters:
folded_bool : Folded booleans

A variable in the folded shape but containing booleans. The elements that are True are the ones for which we will return the flat indices.

free : bool

Whether or not the flattened value is to be in a free parameterization. If not specified, the attribute free_default is used.

Returns:
indices : numpy.ndarray (N,)

A list of indices into the flattened value corresponding to the True members of folded_bool.

flat_length(free=None)

Return the length of the pattern’s flattened value.

Parameters:
free : bool, optional

Whether or not the flattened value is to be in a free parameterization. If not specified, free_default is used.

Returns:
length : int

The length of the pattern’s flattened value.

flatten(folded_val, free=None, validate_value=None)

Flatten a folded value into a flat vector.

Parameters:
folded_val : Folded value

The parameter in its original folded shape.

free : bool, optional

Whether or not the flattened value is to be in a free parameterization. If not specified, the attribute free_default is used.

validate_value : bool

Whether to check that the folded value is valid. If None, the pattern will employ a default behavior.

Returns:
flat_val : numpy.ndarray, (N, )

The flattened value.

fold(flat_val, free=None, validate_value=None)

Fold a flat value into a parameter.

Parameters:
flat_val : numpy.ndarray, (N, )

The flattened value.

free : bool, optional.

Whether or not the flattened value is a free parameterization. If not specified, the attribute free_default is used.

validate_value : bool, optional.

Whether to check that the folded value is valid. If None, the pattern will employ a default behavior.

Returns:
folded_val : Folded value

The parameter value in its original folded shape.

freeing_jacobian(folded_val, sparse=True)

The Jacobian of the map from a flat free value to a flat value.

If the folded value of the parameter is val, val_flat = flatten(val, free=False), and val_freeflat = flatten(val, free=True), then this calculates the Jacobian matrix d val_free / d val_freeflat. For entries with no dependence between them, the Jacobian is taken to be zero.

Parameters:
folded_val : Folded value

The folded value at which the Jacobian is to be evaluated.

sparse : bool, optional

Whether to return a sparse or a dense matrix.

Returns:
``numpy.ndarray``, (N, M)

The Jacobian matrix d val_free / d val_freeflat. Consistent with standard Jacobian notation, the elements of val_free correspond to the rows of the Jacobian matrix and the elements of val_freeflat correspond to the columns.

classmethod from_json(json_string)

Return a pattern from json_string created by to_json.

See also

Pattern.to_json

random()

Return an random, valid parameter in its folded shape.

Note

There is no reason this provides a meaningful distribution over folded values. This function is intended to be used as a convenience for testing.

Returns:
folded_val : Folded value

A random parameter value in its original folded shape.

to_json()

Return a JSON representation of the pattern.

unfreeing_jacobian(folded_val, sparse=True)

The Jacobian of the map from a flat value to a flat free value.

If the folded value of the parameter is val, val_flat = flatten(val, free=False), and val_freeflat = flatten(val, free=True), then this calculates the Jacobian matrix d val_freeflat / d val_free. For entries with no dependence between them, the Jacobian is taken to be zero.

Parameters:
folded_val : Folded value

The folded value at which the Jacobian is to be evaluated.

sparse : bool, optional

If True, return a sparse matrix. Otherwise, return a dense numpy 2d array.

Returns:
``numpy.ndarray``, (N, N)

The Jacobian matrix d val_freeflat / d val_free. Consistent with standard Jacobian notation, the elements of val_freeflat correspond to the rows of the Jacobian matrix and the elements of val_free correspond to the columns.

validate_folded(folded_val, validate_value=None)

Check whether a folded value is valid.

Parameters:
folded_val : Folded value

A parameter value in its original folded shape.

validate_value : bool

Whether to validate the value in addition to the shape. The shape is always validated.

Returns:
is_valid : bool

Whether folded_val is an allowable shape and value.

err_msg : str