harbor run -t openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-0804harbor download openthoughts/tasktrove-exp-flat25-pseudocode-v2-5k-pseudocode-0804MODULE: librosa.sequence
REQUIRED EXCEPTIONS
PUBLIC_API
FUNCTION viterbi(prob, transition, p_init=None, return_logp=False) INPUT - prob: 2D array of shape (n_states, n_observations); non-negative, values ∈ [0, 1] - transition: 2D array of shape (n_states, n_states); each row sums to 1, non-negative - p_init: optional 1D array of length n_states; if None, use uniform distribution - return_logp: boolean; if True, also return log-likelihood of best path OUTPUT - path: 1D integer array of length n_observations (state indices) - (optional) logp: scalar log-likelihood of best path VALIDATION - Shapes: n_states = prob.shape[0] must equal transition.shape[0] and transition.shape[1] - If p_init provided: length == n_states; sum(p_init) ≈ 1; all >= 0 - prob entries: each ∈ [0, 1] - transition: for all rows, sum == 1, all entries >= 0 ALGORITHM - If p_init is None, set p_init := uniform over n_states - Compute in log-domain to avoid underflow L_prob = log(prob) // log emission likelihoods; log(0) treated as -∞ L_trans = log(transition) // log transition probabilities; log(0) treated as -∞ t = number of observations delta[0, s] = log(p_init[s]) + L_prob[s, 0] psi[0, s] = 0 For time k from 1 to t-1: For state s in 0..n_states-1: best_prev = argmax_s' ( delta[k-1, s'] + L_trans[s', s] ) delta[k, s] = delta[k-1, best_prev] + L_trans[best_prev, s] + L_prob[s, k] psi[k, s] = best_prev path[t-1] = argmax_s delta[t-1, s] For k from t-2 down to 0: path[k] = psi[k+1, path[k+1]] logp = max_s delta[t-1, s] NOTES - If any required value is -∞ due to zero probabilities, path is computed accordingly END FUNCTION
FUNCTION viterbi_discriminative(prob_d, transition, p_state=None, p_init=None, return_logp=False) INPUT - prob_d: 2D array of shape (n_states, n_observations); probabilities or proportional values P(state | obs) - transition: 2D array of shape (n_states, n_states) - p_state: optional 1D array length n_states; marginal P(state) - p_init: optional 1D array length n_states; initial state distribution - return_logp: boolean; if True, return (path, logp) OUTPUT - path: 1D integer array of length n_observations - (optional) logp: scalar VALIDATION - prob_d must be 2D; transition must be square with same n_states - If p_state provided: sum ≈ 1, all >= 0 - If p_init provided: sum ≈ 1, all >= 0 - If p_init is None: if p_state provided, set p_init := p_state; else p_init := uniform - prob_d entries must be non-negative ALGORITHM - Compute in log-domain L_pd = log(prob_d) // use -∞ for zeros L_trans = log(transition) Determine p_init as above; if None, use uniform delta[0, s] = log(p_init[s]) + L_pd[s, 0] psi[0, s] = 0 For time t from 1 to T-1: For state s in 0..n_states-1: best_prev = argmax_s' ( delta[t-1, s'] + L_trans[s', s] ) delta[t, s] = delta[t-1, best_prev] + L_trans[best_prev, s] + L_pd[s, t] psi[t, s] = best_prev path[T-1] = argmax_s delta[T-1, s] For t from T-2 down to 0: path[t] = psi[t+1, path[t+1]] - logp computation (if needed): logp = log(p_init[path[0]]) + sum over t=0..T-1 of log(prob_d[path[t], t]) + sum over t=1..T-1 of log(transition[path[t-1], path[t]]) OUTPUT - Return path; if return_logp, return (path, logp) END FUNCTION
FUNCTION viterbi_binary(x, transition, p_state=None, p_init=None, return_logp=False) INPUT - x: 1D array of length m OR 2D array of shape (2, m) - transition: 2D array (n_states, n_states) - p_state: optional 1D array length n_states - p_init: optional 1D array length n_states - return_logp: boolean OUTPUT - path (as in viterbi) - (optional) logp ALGORITHM IF x is 1D THEN m = length(x) prob := 2 x m matrix prob[0, :] = 1 - x prob[1, :] = x REQUIRE transition is 2 x 2; else error CALL viterbi(prob, transition, p_init, return_logp) ELSE IF x is 2D with shape (2, m) THEN CALL viterbi(x, transition, p_state, p_init, return_logp) ELSE RAISE ParameterError END FUNCTION
FUNCTION transition_uniform(n) INPUT - n: integer OUTPUT - M: n x n matrix with all entries equal to 1/n VALIDATION - n is int and n > 0; else raise ParameterError END FUNCTION
FUNCTION transition_loop(n, p) INPUT - n: integer >= 2 - p: scalar in [0, 1] OR 1D array of length n with each entry in [0, 1] OUTPUT - M: n x n stochastic matrix VALIDATION - If n < 2: raise ParameterError - If p is scalar: diag = p off = (1 - p) / (n - 1) row i: M[i,i] = diag; M[i, j≠i] = off - If p is array of length n: for each i: diag[i] = p[i]; off_i = (1 - p[i]) / (n - 1) row i: M[i,i] = diag[i]; M[i, j≠i] = off_i END FUNCTION
FUNCTION transition_cycle(n, p) INPUT - n: integer >= 2 - p: scalar in [0, 1] OR 1D array of length n OUTPUT - M: n x n matrix VALIDATION - For each i: M[i,i] = diag[i] = p[i] (or scalar p) - M[i, (i+1) mod n] = 1 - diag[i] - All other entries = 0 END FUNCTION
FUNCTION transition_local(n, width, window="triangle", wrap=True) INPUT - n: integer >= 2 - width: integer >= 1 OR sequence of length n with entries >= 1 - window: string in {"triangle", "ones"} - wrap: boolean OUTPUT - A: n x n matrix with non-negative entries; rows sum to 1 VALIDATION - If n < 2: raise ParameterError - If window not in {"triangle","ones"}: raise ParameterError - If width is int: scalar case; if sequence: per-row widths width[i] ALGORITHM - Initialize A as zeros(n, n) - For each row i from 0 to n-1: - Determine allowed column indices J_i based on width and wrap per window - If window == "triangle": - Compute weight scheme: w0 = floor((w - 1)/2) + 1 for d = 0..radius: weight_d = w0 - d Build sequence: i, i-1, i+1, i-2, i+2, ... to total w entries Normalize weights to sum to 1 - Else if window == "ones": - Assign equal weight 1/|J_i| to each index in J_i - Set A[i, j] = corresponding weight for j in J_i END FUNCTION
FUNCTION rqa(rec, gap_onset=1, gap_extend=1, knight_moves=False, backtrack=False) INPUT - rec: 2D non-negative array - gap_onset: scalar or np.inf - gap_extend: scalar or np.inf - knight_moves: boolean - backtrack: boolean OUTPUT - scores: 2D array of same shape as rec - IF backtrack THEN path: 2D array of shape (L, 2) with coordinates of maximal path VALIDATION - rec non-negative - gap_onset, gap_extend: if values <= 0 and not np.inf, raise ParameterError ALGORITHM - Perform dynamic-programming over recurrence plot with specified gap rules (define allowed transitions from (i, j) to (i', j') per onset/extend and knight_moves) - Compute maximal score matrix accordingly - If backtrack: - Backtrack to extract a path ending at a high-scoring point - Represent path as array of coordinate pairs [row, col], length L >= 0 - If no path found, return empty path with shape (0, 2) END FUNCTION
DOCUMENTATION AND INTERFACE NOTES
END MODULE