API Reference#
- class optimask.OptiMask(n_tries=5, max_steps=16, random_state=None, verbose=False)#
A class to solve the optimal problem of removing NaNs for a 2D array or DataFrame.
- n_tries#
Number of tries to find the optimal solution.
- Type:
int
- max_steps#
Maximum number of steps for the optimization process.
- Type:
int
- random_state#
Seed for the random number generator to ensure reproducibility.
- Type:
int, optional
- verbose#
If True, prints detailed information about the optimization process.
- Type:
bool
- static apply_p_step(p_step, a, b)#
- classmethod apply_permutation(p, x, inplace: bool)#
Applies a permutation to an array.
- Parameters:
p (np.ndarray) – The permutation array.
x (np.ndarray) – The array to be permuted.
inplace (bool) – If True, applies the permutation in place; otherwise, returns a new permuted array.
- Returns:
The permuted array if inplace is False; otherwise, None.
- Return type:
np.ndarray
- classmethod argsort_decreasing(h, n_bins, kind)#
- static compute_to_keep(size, index_with_nan, permutation, split)#
Computes the indices to keep after removing a subset of indices with NaNs.
- Parameters:
size (int) – The total number of indices.
index_with_nan (np.ndarray) – The indices that contain NaNs.
permutation (np.ndarray) – The permutation array.
split (int) – The split point in the permutation array.
- Returns:
The indices to keep after removing the subset with NaNs.
- Return type:
np.ndarray
- static counting_argsort_decreasing(h, n_bins)#
- static groupby_max(a, b, n)#
- numba equivalent to:
size_a = len(a) ret = np.zeros(n, dtype=np.uint32) np.maximum.at(ret, a, b + 1) return ret
- static groupby_max_parallel(a, b, n, n_threads)#
Threaded equivalent of
groupby_max.Each thread writes to a private scratch row, then the rows are reduced into the final result. This avoids races while keeping the hot loop parallel for large NaN coordinate arrays.
- static has_nan_in_subset(X, rows, cols)#
Checks if there are any NaN values in the specified subset of the array.
- Parameters:
X (np.ndarray) – The input 2D array.
rows (np.ndarray) – The row indices of the subset.
cols (np.ndarray) – The column indices of the subset.
- Returns:
True if there are NaN values in the subset, False otherwise.
- Return type:
bool
- static is_decreasing(h)#
- numba equivalent to:
return (np.diff(h)>0).all()
- static numba_apply_permutation(p, x)#
- numba equivalent to:
rank = np.empty_like(p) rank[p] = np.arange(len(p)) # Use the rank array to permute x return rank[x]
- static numba_apply_permutation_inplace(p, x)#
- solve(X: ndarray | DataFrame, return_data: bool = False, check_result=False) Tuple[ndarray, ndarray] | Tuple[Index, Index]#
Solves the optimal problem of removing NaNs for a 2D array or DataFrame.
- Parameters:
X (Union[np.ndarray, pd.DataFrame]) – The input 2D array or DataFrame with NaN values.
return_data (bool) – If True, returns the resulting data; otherwise, returns the indices.
check_result (bool) – If True, checks if the computed submatrix contains NaNs, for tests purposes.
reliable. (Disabled by default as it can slow down the computation and the algorithm has proven to be)
- Returns:
If return_data is True, returns the resulting 2D array or DataFrame; otherwise, returns the indices of rows and columns to retain.
- Return type:
Union[Tuple[np.ndarray, np.ndarray], Tuple[pd.Index, pd.Index]]
- Raises:
InvalidDimensionError – If the input numpy array does not have ndim==2.
EmptyInputError – If the input data is empty.
OptiMaskAlgorithmError – If the OptiMask algorithm encounters an error during optimization.
ValueError – If the input DataFrame’s index contains non-unique entries.