Skip to content

Commit

Permalink
Run ruff fix again
Browse files Browse the repository at this point in the history
  • Loading branch information
const-ae committed Apr 16, 2024
1 parent 791adda commit 31bf7f7
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/pylemur/tl/_design_matrix_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Mapping

import numpy as np
import pandas as pd

Expand Down
7 changes: 5 additions & 2 deletions src/pylemur/tl/_grassmann_lm.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np

from pylemur.tl._design_matrix_utils import row_groups
from pylemur.tl._grassmann import grassmann_log, grassmann_map
from pylemur.tl._lin_alg_wrappers import fit_pca, ridge_regression
Expand All @@ -18,7 +19,7 @@ def grassmann_geodesic_regression(coord_systems, design, base_point, weights=Non
weights : array-like, shape (n_groups,)
Returns
----------
-------
beta: array-like, shape (n_emb, n_features, n_coef)
"""
n_obs = design.shape[0]
Expand All @@ -33,7 +34,7 @@ def grassmann_geodesic_regression(coord_systems, design, base_point, weights=Non
weights = np.ones(n_obs)

tangent_vecs = [
grassmann_log(base_point.T, coord_systems[i].T).T.reshape((n_emb * n_features)) for i in range(n_obs)
grassmann_log(base_point.T, coord_systems[i].T).T.reshape(n_emb * n_features) for i in range(n_obs)
]
tangent_vecs = np.vstack(tangent_vecs)
if tangent_vecs.shape[0] == 0:
Expand All @@ -48,6 +49,7 @@ def grassmann_geodesic_regression(coord_systems, design, base_point, weights=Non
def grassmann_lm(Y, design_matrix, base_point):
"""
Solve Sum_i||Y_i: - Y_i: Proj(Exp_p(Sum_k V_k:: * X_ik))||^2 for V.
Parameters
----------
Y : array-like, shape (n_samples, n_features)
Expand All @@ -56,6 +58,7 @@ def grassmann_lm(Y, design_matrix, base_point):
The design matrix.
base_point : array-like, shape (n_emb, n_features)
The base point.
Returns
-------
beta: array-like, shape (n_emb, n_features, n_coef)
Expand Down
7 changes: 6 additions & 1 deletion src/pylemur/tl/_lin_alg_wrappers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sklearn.decomposition as skd
from typing import NamedTuple

import numpy as np
import sklearn.decomposition as skd


class PCA(NamedTuple):
Expand All @@ -12,6 +13,7 @@ class PCA(NamedTuple):
def fit_pca(Y, n, center=True):
"""
Calculate the PCA of a given data matrix Y.
Parameters
----------
Y : array-like, shape (n_samples, n_features)
Expand All @@ -20,6 +22,7 @@ def fit_pca(Y, n, center=True):
The number of principal components to return.
center : bool, default=True
If True, the data will be centered before computing the covariance matrix.
Returns
-------
pca : sklearn.decomposition.PCA
Expand All @@ -41,6 +44,7 @@ def fit_pca(Y, n, center=True):
def ridge_regression(Y, X, ridge_penalty=0, weights=None):
"""
Calculate the ridge regression of a given data matrix Y.
Parameters
----------
Y : array-like, shape (n_samples, n_features)
Expand All @@ -51,6 +55,7 @@ def ridge_regression(Y, X, ridge_penalty=0, weights=None):
The ridge penalty.
weights : array-like, shape (n_features,)
The weights to apply to each feature.
Returns
-------
ridge: array-like, shape (n_coef, n_features)
Expand Down
7 changes: 1 addition & 6 deletions src/pylemur/tl/alignment.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
from typing import Union
import harmonypy
import numpy as np
import pandas as pd

from pylemur.tl._design_matrix_utils import row_groups
from pylemur.tl._lin_alg_wrappers import multiply_along_axis, ridge_regression



from pylemur.tl._lin_alg_wrappers import ridge_regression


def _align_impl(
Expand Down
29 changes: 16 additions & 13 deletions src/pylemur/tl/lemur.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import re
from typing import Any, Literal, Union
from collections.abc import Iterable, Mapping
import warnings
from collections.abc import Iterable, Mapping
from typing import Any, Literal, Union

import anndata as ad
import formulaic
import numpy as np
import anndata as ad
from sklearn.exceptions import NotFittedError

from pylemur.tl._design_matrix_utils import *
from pylemur.tl._grassmann_lm import grassmann_lm, project_data_on_diffemb
from pylemur.tl._grassmann import grassmann_map
from pylemur.tl._grassmann_lm import grassmann_lm, project_data_on_diffemb
from pylemur.tl._lin_alg_wrappers import *
from pylemur.tl.alignment import _align_impl, _apply_linear_transformation, _init_harmony, _reverse_linear_transformation

from pylemur.tl.alignment import (
_align_impl,
_apply_linear_transformation,
_init_harmony,
_reverse_linear_transformation,
)


class LEMUR:
Expand Down Expand Up @@ -122,7 +127,6 @@ def fit(self, verbose: bool = True):
`self`
The fitted LEMUR model.
"""

Y = self.data_matrix
design_matrix = self.design_matrix
n_embedding = self.n_embedding
Expand Down Expand Up @@ -154,7 +158,7 @@ def fit(self, verbose: bool = True):
self.linear_coefficients = lin_coef

return self

def align_with_harmony(
self, ridge_penalty: Union[float, list[float], np.ndarray] = 0.01, max_iter: int = 10, verbose: bool = True
):
Expand Down Expand Up @@ -287,7 +291,7 @@ def transform(self, adata: ad.AnnData, layer: Union[str, None] = None,
design_matrix, _ = handle_design_parameter(self.formula, adata.obs)
dm = design_matrix.to_numpy()
Y_clean = Y - dm @ self.linear_coefficients
embedding = project_data_on_diffemb(Y_clean, design_matrix = dm, coefficients = self.coefficients,
embedding = project_data_on_diffemb(Y_clean, design_matrix = dm, coefficients = self.coefficients,
base_point = self.base_point)
embedding = _apply_linear_transformation(embedding, self.alignment_coefficients, dm)
if return_type == "embedding":
Expand All @@ -298,7 +302,7 @@ def transform(self, adata: ad.AnnData, layer: Union[str, None] = None,
fit.design_matrix = design_matrix
fit.embedding = embedding
return fit


def predict(self,
embedding: Union[np.ndarray, None] = None,
Expand Down Expand Up @@ -334,7 +338,6 @@ def predict(self,
array-like, shape (n_cells, n_genes)
The predicted expression of the cells in the new condition.
"""

if embedding is None:
if self.embedding is None:
raise NotFittedError("The model has not been fitted yet.")
Expand Down Expand Up @@ -443,13 +446,13 @@ def _get_var_from_colname(colname):
df = pd.DataFrame([kwargs])

return design_matrix.model_spec.get_model_matrix(df)

def __str__(self):
if self.embedding is None:
return f"LEMUR model (not fitted yet) with {self.n_embedding} dimensions"
else:
return f"LEMUR model with {self.n_embedding} dimensions"



def order_axis_by_variance(embedding, coefficients, base_point):
Expand Down
1 change: 1 addition & 0 deletions tests/test_grassmann.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np

from pylemur.tl._grassmann import *


Expand Down
1 change: 1 addition & 0 deletions tests/test_lin_alg_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
from sklearn.linear_model import LinearRegression, Ridge

from pylemur.tl._lin_alg_wrappers import *


Expand Down

0 comments on commit 31bf7f7

Please sign in to comment.