run_markov_chain_ABC module¶
- itrails.run_markov_chain_ABC.compute_matrices_start_end_wrapper(prob_mats, exponential_time, omega_start_masks, omega_end_masks, num_combinations)[source]¶
Parallel wrapper that computes the sliced matrix result for each combination by invoking compute_matrix_start_end on each set of inputs; for every index from 0 to num_combinations-1, it multiplies the corresponding probability matrix (from prob_mats) with the result of slicing the exponential_time matrix using the corresponding omega_start_mask and omega_end_mask, and aggregates all results into a single numpy array; the computations are performed in parallel using joblib.Parallel with ncpu.N_CPU_GLOBAL workers. :type prob_mats: list or np.ndarray. :param prob_mats: List or array of probability matrices where each element is a numpy array. :type prob_mats: list or np.ndarray. :type exponential_time: np.ndarray. :param exponential_time: Numpy array representing the matrix computed by applying the exponential function to the transition matrix multiplied by time. :type exponential_time: np.ndarray. :type omega_start_masks: list or np.ndarray of bool. :param omega_start_masks: List or array of boolean vectors used to mask the rows of the exponential_time matrix for each combination. :type omega_start_masks: list or np.ndarray of bool. :type omega_end_masks: list or np.ndarray of bool. :param omega_end_masks: List or array of boolean vectors used to mask the columns of the exponential_time matrix for each combination. :type omega_end_masks: list or np.ndarray of bool. :type num_combinations: int. :param num_combinations: Total number of combinations (iterations) over which to compute the sliced matrix. :type num_combinations: int. :return: Numpy array containing the resulting matrices computed for each combination. :rtype: np.ndarray.
- itrails.run_markov_chain_ABC.compute_matrix_start_end(prob_mat, exponential_time, omega_start_mask, omega_end_mask)[source]¶
Helper function that computes all matrix multiplications but the first, then slices the matrix to get the rows corresponding to the starting state and columns corresponding to the end state.
- Parameters:
prob_mat (Numpy array) – Matrix of probabilities
exponential_time (Numpy array) – Exponential matrix of transition matrix multiplied by time.
omega_start_mask (Numpy array of booleans.) – Vector of booleans that masks the rows of the matrix.
omega_end_mask (Numpy array of booleans.) – Vector of booleans that masks the columns of the matrix.
- Returns:
Sliced matrix
- Return type:
Numpy array
- itrails.run_markov_chain_ABC.deepest_parallel_inner(deepest_idx, trans_mat_noabs, omega_dict_noabs, deepest_keys_acc_array, deepest_paths_acc_array, deepest_path_lengths_array, acc_prob_mats_noabs)[source]¶
Parallel wrapper that schedules deepest_worker_inner tasks to compute the matrix for the deepest time interval when multiple coalescents occur in the last time interval; it first serializes the omega dictionary (excluding absorbing states), then constructs a list of tasks from the accumulated keys, paths, and path lengths arrays along with the corresponding probability matrices without absorbing states, executes these tasks in parallel using ncpu.N_CPU_GLOBAL workers, and finally aggregates and flattens the results into arrays of updated keys and computed probability matrices along with the total count of valid tasks processed. :type deepest_idx: int. :param deepest_idx: number of indices in the deepest keys accumulator array. :type deepest_idx: int. :type trans_mat_noabs: numpy array. :param trans_mat_noabs: transition matrix without absorbing states used for deepest time interval computations. :type trans_mat_noabs: numpy array. :type omega_dict_noabs: dict. :param omega_dict_noabs: dictionary mapping omega state keys (without absorbing states) to boolean vectors. :type omega_dict_noabs: dict. :type deepest_keys_acc_array: list or numpy array. :param deepest_keys_acc_array: accumulated array of keys for each deepest subpath combination. :type deepest_keys_acc_array: list or numpy array. :type deepest_paths_acc_array: list or numpy array. :param deepest_paths_acc_array: accumulated array of paths for each deepest subpath combination. :type deepest_paths_acc_array: list or numpy array. :type deepest_path_lengths_array: list or numpy array. :param deepest_path_lengths_array: accumulated array of subpath lengths for each deepest subpath combination. :type deepest_path_lengths_array: list or numpy array. :type acc_prob_mats_noabs: list or numpy array. :param acc_prob_mats_noabs: list or array of probability matrices representing the initial state for deepest subpaths without absorbing states. :type acc_prob_mats_noabs: list or numpy array. :return: tuple containing (flattened_keys, flattened_results, total_valid) where flattened_keys is an array of updated keys, flattened_results is an array of computed probability matrices, and total_valid is the total number of valid tasks processed. :rtype: tuple.
- itrails.run_markov_chain_ABC.deepest_worker_inner(idx_i, idx_j, trans_mat_noabs, omega_dict_noabs_serialized, key, paths_array_j, acc_prob_mat_noabs, path_lengths_j)[source]¶
Ray worker function for every combination of subpaths when multiple coalescents happen in the last time interval (Deepest TI).
- Parameters:
idx_i (int) – Marker of the path index within all possible Deepest TI paths.
idx_j (int) – Marker of the subpath index within all possible subpaths.
trans_mat_noabs (Numpy array) – Transition matrix without absorbing states.
omega_dict_noabs_serialized (Serialized dictionary) – Serialized dictionary of omega indices (key) and vector of booleans where each key has the states (value), lacks absorbing states.
paths_array_j (Numpy array) – Array of possible transitions within a subpath.
acc_prob_mat_noabs (Numpy array) – Array of probabilities for each state at the start of the time interval without absorbing states.
path_lengths_j (Numpy array) – Array of lengths of each subpath.
- Returns:
I and J indices, Probability matrix summed over every possible transitions for a subpath, Updated Key.
- Return type:
Tuple(int, int, Numpy array, Numpy array)
- itrails.run_markov_chain_ABC.run_markov_chain_ABC(trans_mat, times, omega_dict, prob_dict, omega_nonrev_counts, inverted_omega_nonrev_counts, n_int_ABC, species, absorbing_state=(7, 7))[source]¶
Function that runs the Discrete Time Markov chain for species A, B, and C.
- Parameters:
trans_mat (Numpy array) – Transition matrix.
times (Numpy array) – Array of cut times, last timepoint is infinity.
omega_dict (Numba typed dictionary) – Dictionary of omega indices (key) and vector of booleans where each key has the states (value).
prob_dict (Numba typed dictionary) – Dictionary of each path (keys) and probabilities for each state at the start of the first time interval (values).
omega_nonrev_counts (Numba typed dictionary) – Dictionary of omega indices (key) and number of non-reversible transitions (value).
inverted_omega_nonrev_counts (Numba typed dictionary) – Dictionary of omega indices (key) and number of non-reversible transitions (value), inverted.
n_int_ABC (int) – Number of intervals for species A, B, and C.
species (int) – Number of species.
absorbing_state (tuple, optional) – State where all coalecents have happened, defaults to (7, 7)
- Returns:
Updated dictionary of each path (keys) and probabilities for each state at the end of the Markov chain (time equals inf)(values).
- Return type:
Numba typed dictionary
- itrails.run_markov_chain_ABC.vanloan_parallel_inner(vl_idx, time, trans_mat, omega_dict, vl_keys_acc_array, vl_paths_acc_array, vl_omega_masks_start, vl_omega_masks_end, vl_prob_mats)[source]¶
Parallel wrapper that schedules vanloan_worker_inner tasks to compute the Van Loan integral for every combination of subpaths when multiple coalescents occur in the same time interval; it first serializes the provided omega dictionary, then builds a list of tasks from the accumulated keys, paths, omega masks, and probability matrices arrays, executes these tasks in parallel using ncpu.N_CPU_GLOBAL workers, and finally aggregates and flattens the results into arrays of updated keys and computed probability matrices along with the total number of valid tasks processed. :type vl_idx: int. :param vl_idx: number of indices in the vanloan keys accumulator array. :type vl_idx: int. :type time: float. :param time: end time of the current time interval used in the Van Loan integral computation. :type time: float. :type trans_mat: numpy array. :param trans_mat: transition matrix used for the Van Loan integral computation. :type trans_mat: numpy array. :type omega_dict: dict. :param omega_dict: dictionary mapping omega state keys to boolean vectors indicating valid states. :type omega_dict: dict. :type vl_keys_acc_array: list or numpy array. :param vl_keys_acc_array: accumulated array of keys for each vanloan subpath combination. :type vl_keys_acc_array: list or numpy array. :type vl_paths_acc_array: list or numpy array. :param vl_paths_acc_array: accumulated array of paths for each vanloan subpath combination. :type vl_paths_acc_array: list or numpy array. :type vl_omega_masks_start: list or numpy array of bool. :param vl_omega_masks_start: list or array of boolean masks applied to the rows of the matrix for each vanloan subpath. :type vl_omega_masks_start: list or numpy array of bool. :type vl_omega_masks_end: list or numpy array of bool. :param vl_omega_masks_end: list or array of boolean masks applied to the columns of the matrix for each vanloan subpath. :type vl_omega_masks_end: list or numpy array of bool. :type vl_prob_mats: list or numpy array. :param vl_prob_mats: list or array of probability matrices representing the initial state for each vanloan subpath. :type vl_prob_mats: list or numpy array. :return: tuple containing (flattened_keys, flattened_results, total_valid) where flattened_keys is an array of updated keys, flattened_results is an array of computed probability matrices, and total_valid is the total number of valid tasks processed. :rtype: tuple.
- itrails.run_markov_chain_ABC.vanloan_worker_inner(idx_i, idx_j, trans_mat, omega_dict_serialized, key, time, paths_array_j, omega_start_mask, omega_end_mask, prob_mat)[source]¶
Ray worker function for every combination of subpaths when multiple coalescents happen in a same time interval (Van Loan).
- Parameters:
idx_i (int) – Marker of the path index within all possible Van Loan paths.
idx_j (int) – Marker of the subpath index within all possible subpaths.
trans_mat (Numpy array) – Transition matrix.
omega_dict_serialized (Serialized dictionary) – Serialized dictionary of omega indices (key) and vector of booleans where each key has the states (value).
key (Numpy array) – Key that represents the current state.
time (float) – End time of the interval.
paths_array_j (Numpy array) – Array of possible transitions within a subpath.
omega_start_mask (Numpy array) – 2D array to mask the matrix based on initial omega, array of booleans.
omega_end_mask (Numpy array) – 2D array to mask the matrix based on final omega, array of booleans.
prob_mat (Numpy array) – Array of probabilities for each state at the start of the time interval.
- Returns:
I and J indices, Probability matrix summed over every possible transitions for a subpath, Updated Key.
- Return type:
Tuple(int, int, Numpy array, Numpy array)