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

Address a few frequent matrix dtype conversion errors #281

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions pegasus/tools/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,8 @@ def select_features(
psum = np.multiply(X, X).sum(axis=0)
std = ((psum - X.shape[0] * (m1 ** 2)) / (X.shape[0] - 1.0)) ** 0.5
std[std == 0] = 1
X -= m1
X /= std
X = X - m1
X = X / std
data.uns["stdzn_mean"] = m1
data.uns["stdzn_std"] = std
data.uns.pop(batch_ls_key, None)
Expand Down Expand Up @@ -723,8 +723,8 @@ def pc_transform(
# standardization
if "stdzn_mean" in data.uns:
assert "stdzn_std" in data.uns
X -= data.uns["stdzn_mean"]
X /= data.uns["stdzn_std"]
X = X - data.uns["stdzn_mean"]
X = X - data.uns["stdzn_std"]
if "stdzn_max_value" in data.uns:
max_value = data.uns["stdzn_max_value"]
X[X > max_value] = max_value
Expand Down
3 changes: 3 additions & 0 deletions pegasus/tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ def calc_stat_per_batch(X: Union[csr_matrix, np.ndarray], batch: Union[pd.Catego
codes = np.array(batch, dtype = np.int32)
nbatch = codes.max() + 1 # assume cluster label starts from 0

# Try to fix frequent dtype errors in this function by ensuring that the matrix is float32
X = X.astype(np.float32)

if issparse(X):
from pegasus.cylib.fast_utils import calc_stat_per_batch_sparse
return calc_stat_per_batch_sparse(X.shape[0], X.shape[1], X.data, X.indices, X.indptr, nbatch, codes)
Expand Down