Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove use of dask.array.ma in PCA in favour of array API compliant functions #1272

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions sgkit/stats/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def fit(
Alternate allele counts with missing values encoded as either nan
or negative numbers.
"""
X = da.ma.masked_array(X, mask=da.isnan(X) | (X < 0))
self.mean_ = da.ma.filled(da.mean(X, axis=0), fill_value=np.nan)
X = _replace_missing_with_nan(X)
self.mean_ = da.nanmean(X, axis=0)
p = self.mean_ / self.ploidy
self.scale_ = da.sqrt(p * (1 - p))
self.n_features_in_ = X.shape[1]
Expand All @@ -90,10 +90,10 @@ def transform(
Alternate allele counts with missing values encoded as either nan
or negative numbers.
"""
X = da.ma.masked_array(X, mask=da.isnan(X) | (X < 0))
X = _replace_missing_with_nan(X)
X -= self.mean_
X /= self.scale_
return da.ma.filled(X, fill_value=np.nan)
return X

def inverse_transform(self, X: ArrayLike, copy: Optional[bool] = None) -> ArrayLike:
"""Invert transform
Expand All @@ -109,6 +109,14 @@ def inverse_transform(self, X: ArrayLike, copy: Optional[bool] = None) -> ArrayL
return X


def _replace_missing_with_nan(X):
if np.issubdtype(X.dtype, np.floating):
nanarray = da.asarray(np.nan, dtype=X.dtype)
else:
nanarray = da.asarray(np.nan)
return da.where(X < 0, nanarray, X)


def filter_partial_calls(
ds: Dataset,
*,
Expand Down
Loading