Util module

Collection of different utility functions

@author: jdiedrichsen

util.G_to_dist(G)

Transforms a second moment matrix into a squared Euclidean matrix (mostly for visualization)

Parameters:

G (ndarray) – 2d or 3d array of second moment matrices

util.check_grad(fcn, theta0, delta)

Checks the gradient of a function around a value for theta

Parameters:
  • fcn (function) – needs to return criterion and derivative

  • theta0 (ndarray) – Vector of parameters

util.classical_mds(G, contrast=None, align=None, thres=0)

Calculates a low-dimensional projection of a G-matrix That preserves the relationship of different conditions Equivalent to classical MDS. If contrast is given, the method becomes equivalent to dPCA, as it finds the representation that maximizes the variance acording to this contrast. Developement: If align is given, it performs Procrustes alignment of the result to a given V within the found dimension

Parameters:
  • G (ndarray) – KxK second moment matrix

  • contrast (ndarray) – Contrast matrix to optimize for. Defaults to None.

  • align (ndarry) – A different loading matrix to which to align

  • thres (float) – Cut off eigenvalues under a certain value

Returns:
  • W (ndarray) – Loading of the K different conditions on main axis

  • Glam (ndarray) – Variance explained by each axis

util.est_G_crossval(Y, Z, part_vec, X=None, S=None)

Obtains a crossvalidated estimate of G Y = Z @ U + X @ B + E, where var(U) = G

Parameters:
  • Y (numpy.ndarray) – Activity data

  • Z (numpy.ndarray) – 2-d: Design matrix for conditions / features U 1-d: condition vector

  • part_vec (numpy.ndarray) – Vector indicating the partition number

  • X (numpy.ndarray) – Fixed effects to be removed

  • S (numpy.ndarray) –

Returns:
  • G_hat (numpy.ndarray) – n_cond x n_cond matrix

  • Sig (numpy.ndarray) – n_cond x n_cond noise estimate per block

util.make_pd(G, thresh=1e-10)

Enforces that G is semi-positive definite by setting small eigenvalues to minimal value

Parameters:
  • G (square 2d-np.array) – estimated KxK second momement matrix

  • thresh (float) – threshold for increasing small eigenvalues

Returns:

Gpd (square 2d-np.array) – semi-positive definite version of G