1.6. RectiPy.rectipy.utility

Module that contains various functions for weight initialization and model training.

rectipy.utility.add_op_name(op: str, var: str | None, new_var_names: dict) str | None[source]

Adds an operator name to a variable identifier.

Parameters:
  • op – Operator name to be added.

  • var – Current variable identifier.

  • new_var_names – Dictionary that contains the maping between old and updated varaible identifiers.

Returns:

Updated variable name.

Return type:

str

rectipy.utility.circular_connectivity(N: int, p: float, spatial_distribution: rv_discrete, homogeneous_weights: bool = True) ndarray[source]

Generate a coupling matrix between nodes aligned on a circle.

Parameters:
  • N – Number of nodes.

  • p – Connection probability.

  • spatial_distribution – Probability distribution defined over space. Will be used to draw indices of nodes from which each node in the circular network receives inputs.

  • homogeneous_weights – If true, all incoming weights to a node will have the same strength. Since incoming edges are drawn with replacement from the spatial distribution, this means that the actual connection probability is smaller or equal to p. If false, each drawn sample will contribute to the edge weights, such that the resulting edge strengths can be heterogeneous.

Returns:

2D coupling matrix (N x N).

Return type:

np.ndarray

rectipy.utility.input_connections(n: int, m: int, p: float, variance: float = 1.0, zero_mean: bool = True)[source]

Generate random input connections.

Parameters:
  • n – Number of rows.

  • m – Number of columns.

  • p – Coupling probability.

  • variance – Variance of the randomly drawn input weights in each row.

  • zero_mean – If true, input weights in each row will be normalized such that they sum up to 0.

Returns:

2D input weight matrix (n x m)

Return type:

np.ndarray

rectipy.utility.line_connectivity(N: int, p: float, spatial_distribution: rv_discrete, homogeneous_weights: bool = True) ndarray[source]

Generate a coupling matrix between nodes aligned on a circle.

Parameters:
  • N – Number of nodes.

  • p – Connection probability.

  • spatial_distribution – Probability distribution defined over space. Will be used to draw indices of nodes from which each node in the circular network receives inputs.

  • homogeneous_weights

Returns:

2D coupling matrix (N x N).

Return type:

np.ndarray

rectipy.utility.normalize(x: ndarray, mode: str = 'minmax', row_wise: bool = False) ndarray[source]

Normalization function for matrices.

Parameters:
  • x – N x m matrix.

  • mode – Normalization mode. Can be one of the following options: - ‘minmax’: Normalize such that the minimum of the data is 0 and the maximum is 1. - ‘zscore’: Normalize data such that the mean is 0 and the standard deviation is 1. - ‘sum’: Normalize such that the sum over the data equals 1.

  • row_wise – If true, normalization will be applied independently for each row of x.

Returns:

N x m matrix, normalized.

Return type:

np.ndarray

rectipy.utility.random_connectivity(n: int, m: int, p: float, normalize: bool = True) ndarray[source]

Generate a random coupling matrix.

Parameters:
  • n – Number of rows

  • m – Number of columns

  • p – Coupling probability.

  • normalize – If true, all rows will be normalized such that they sum up to 1.

Returns:

2D couping matrix (n x m).

Return type:

np.ndarray

rectipy.utility.retrieve_from_dict(keys: list, data: dict) dict[source]

Remove dictionary entries and collect them in a new dictionary.

Parameters:
  • keys – Entries in data that are to be removed from data and collected in a new dict.

  • data – Original dictionary.

Returns:

New dictionary that contains the key-value pairs that were originally stored in data under keys.

Return type:

dict

rectipy.utility.to_device(x: Tensor, device: str) Tensor[source]
rectipy.utility.wta_score(x: ndarray, y: ndarray) float[source]

Calculates the winner-takes-all score.

Parameters:
  • x – 2D array, where rows are samples and columns are features.

  • y – 2D array, where rows are samples and columns are features.

Returns:

WTA score.

Return type:

float