helper

snafu.helper.censored(walk, td=None, seed=None)[source]

Apply censoring rules to a random walk to simulate participant data.

Filters repeated items from a walk according to emission and censoring faults.

Parameters:
  • walk (list of tuple) – List of edges representing the walk.

  • td (object, optional) – Object with attributes emission_fault and censor_fault (probabilities).

  • seed (int, optional) – Seed for random number generator for reproducibility.

Returns:

List of nodes after applying censoring.

Return type:

list

class snafu.helper.dotdict(*args, **kwargs)[source]

Bases: dict

snafu.helper.edges_from_nodes(path)[source]

Convert a sequence of nodes into a sequence of edges.

Creates a list of consecutive (source, target) tuples from an ordered list of nodes representing a walk through a graph.

Parameters:

path (list) – List of nodes in the order they were visited.

Returns:

List of edges representing transitions between consecutive nodes.

Return type:

list of tuple

snafu.helper.find_ngrams(input_list, n)[source]

Generate a list of n-grams from an input list.

Parameters:
  • input_list (list) – The input list from which to generate n-grams.

  • n (int) – The number of elements in each n-gram.

Returns:

A list of n-gram tuples.

Return type:

list of tuple

snafu.helper.firstHits(walk)[source]

Compute first hitting times for each node in a censored walk.

For each unique node in a censored walk, finds the index of its first occurrence in the original walk’s edge list.

Parameters:
  • walk (list of int)

  • walk. (List of nodes visited in a)

Returns:

  • list of tuple

  • List of (node, index) pairs representing the first time each node is visited.

snafu.helper.flatten_list(l, numtimes=1)[source]

Flatten a nested list or numpy array by one level, optionally multiple times.

Parameters:
  • l (list) – The list to flatten.

  • numtimes (int, optional) – Number of times to flatten the list, by default 1.

Returns:

The flattened list.

Return type:

list

snafu.helper.groupToIndividual(Xs, group_dict)[source]

Map group-level node labels to individual-level indices.

Parameters:
  • Xs (list of list of int) – Participant responses in group space.

  • group_dict (dict) – Mapping of group node indices to labels.

Returns:

  • Translated data with local indices.

  • Dictionary mapping local indices to labels.

Return type:

tuple

snafu.helper.logTrick(loglist)[source]

Numerically stable log-sum-exp trick for a list of log-likelihoods.

Parameters:

loglist (list of float) – A list of log-likelihood values.

Returns:

The log of the summed exponentiated values.

Return type:

float

snafu.helper.maxn(items, n)[source]

Return the top n maximum elements from a list.

Parameters:
  • items (list) – Input list of numeric values.

  • n (int) – Number of maximum values to retrieve.

Returns:

A sorted list of the top n maximum values.

Return type:

list

snafu.helper.mexgauss(rts)[source]

Estimate parameters for the ex-Gaussian distribution from response times.

This function estimates the parameters of an ex-Gaussian distribution (mu, sigma, lambda) using the method of moments. It is ported from the mexgauss function in R’s retimes package.

Parameters:

rts (array-like) – A list or array of response times.

Returns:

  • tuple of float

  • A tuple containing

    • mufloat

      Mean of the normal component.

    • sigmafloat

      Standard deviation of the normal component.

    • lambdafloat

      Rate parameter of the exponential component (1/tau).

snafu.helper.no_persev(x)[source]

This function is copied from scipy to avoid shipping that whole library with snafu unlike scipy version, this one doesn’t return p-value (requires C code from scipy)

snafu.helper.nodes_from_edges(walk)[source]

Convert a sequence of edges into a sequence of nodes.

Assumes the input is a list of (source, target) tuples representing a walk through a graph. Reconstructs the sequence of visited nodes by taking the source of each edge and appending the target of the last edge.

Parameters:

walk (list of tuple) – List of edges (as tuples of nodes) representing a walk.

Returns:

List of nodes visited in the walk.

Return type:

list

snafu.helper.nogc(fun)[source]

Decorator to disable garbage collection during function execution.

Temporarily disables garbage collection to potentially speed up functions that involve frequent memory allocations and deallocations.

Parameters:

fun (callable) – The function to wrap.

Returns:

The wrapped function with garbage collection disabled during execution.

Return type:

callable

snafu.helper.numToItemLabel(data, items)[source]

Convert numerical indices in nested lists to corresponding item labels.

Parameters:
  • data (list of list of int) – Lists containing indices of items.

  • items (dict) – Dictionary mapping indices to labels.

Returns:

Nested lists with item labels instead of indices.

Return type:

list of list of str

snafu.helper.numToLabel(Xs, items)[source]

Convert numerical node IDs to corresponding labels in-place.

Parameters:
  • Xs (list of list of int) – Lists containing node indices.

  • items (dict) – Dictionary mapping node indices to labels.

Returns:

Lists with node labels.

Return type:

list of list of str

snafu.helper.pearsonr(x, y)[source]

Compute the Pearson correlation coefficient between two arrays.

Parameters:
  • x (array-like) – First input array.

  • y (array-like) – Second input array.

Returns:

Pearson correlation coefficient.

Return type:

float

snafu.helper.rand_exg(irt, sigma, lambd)[source]

Generate a random sample from an ex-Gaussian distribution.

Parameters:
  • irt (float) – Mean of the Gaussian component.

  • sigma (float) – Standard deviation of the Gaussian component.

  • lambd (float) – Rate parameter (1/tau) of the exponential component.

Returns:

A sample drawn from the ex-Gaussian distribution.

Return type:

float

snafu.helper.reverseDict(items)[source]

Reverse keys and values in a dictionary.

Parameters:

items (dict) – Dictionary to reverse.

Returns:

Dictionary with keys and values swapped.

Return type:

dict

snafu.helper.stationary(t, method='unweighted')[source]

Compute the stationary distribution of a transition matrix.

Parameters:
  • t (ndarray) – Transition matrix.

  • method (str, optional) – Method for computing the stationary distribution. Options: - “unweighted”: Returns the proportion of non-zero entries (only works for unweighted matrices). - otherwise: Computes the dominant eigenvector (may be buggy).

Returns:

Stationary distribution as a vector (if using eigen method), or a scalar proportion (if unweighted).

Return type:

ndarray or float

snafu.helper.timer(fun)[source]

Decorator that prints the elapsed time of a function call.

Parameters:

fun (callable) – The function to time.

Returns:

The wrapped function that prints execution time.

Return type:

callable