Skip to content

Commit

Permalink
fix data type
Browse files Browse the repository at this point in the history
  • Loading branch information
YQ-Wang committed Nov 20, 2023
1 parent bff86b2 commit 69ec97f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
19 changes: 10 additions & 9 deletions bsp2/bsp2.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
"""


from typing import Dict, Tuple, Union
from typing import Dict, List, Tuple, Union

import numpy as np
import pandas as pd
from scipy.sparse import csr_matrix, diags, identity, isspmatrix_csr # type: ignore
import pandas as pd # type: ignore
from scipy.sparse import (csr_matrix, diags, identity, # type: ignore
isspmatrix_csr)
from scipy.spatial import KDTree # type: ignore
from scipy.stats import gmean, lognorm # type: ignore
from sklearn.preprocessing import minmax_scale # type: ignore
Expand Down Expand Up @@ -63,15 +64,15 @@ def _binary_distance_matrix_threshold(
)


def _spvars(input_csr_mat: csr_matrix, axis: int) -> np.ndarray:
def _spvars(input_csr_mat: csr_matrix, axis: int) -> List[float]:
input_csr_mat_squared = input_csr_mat.copy()
input_csr_mat_squared.data **= 2
return input_csr_mat_squared.mean(axis) - np.square(input_csr_mat.mean(axis))


def _test_scores(
input_sp_mat: np.ndarray, input_exp_mat_raw: csr_matrix, d1: float, d2: float
) -> np.ndarray:
) -> List[float]:
input_exp_mat_norm = _scale_sparse_minmax(input_exp_mat_raw).transpose()
input_exp_mat_raw = input_exp_mat_raw.transpose()
inverted_diag_matrix_cache: Dict[Tuple, csr_matrix] = {}
Expand All @@ -86,7 +87,7 @@ def _get_inverted_diag_matrix(sum_axis_0: np.ndarray) -> csr_matrix:
)
return inverted_diag_matrix_cache[cache_key]

def _var_local_means(d_val: float) -> np.ndarray:
def _var_local_means(d_val: float) -> list:
patches_cells = _binary_distance_matrix_threshold(input_sp_mat, d_val)
patches_cells_centroid = diags(
(patches_cells.sum(axis=1) > 1).astype(float).A.ravel(),
Expand All @@ -103,15 +104,15 @@ def _var_local_means(d_val: float) -> np.ndarray:
var_x_0_add = _spvars(input_exp_mat_raw, axis=1).A.ravel() # type: ignore
var_x_0_add /= max(var_x_0_add)
t_matrix = (var_x[:, 1] / var_x[:, 0]) * var_x_0_add
return t_matrix
return t_matrix.tolist()


def granp(
input_sp_mat: np.ndarray,
input_exp_mat_raw: Union[np.ndarray, pd.DataFrame, csr_matrix],
d1: float = 1.0,
d2: float = 3.0,
) -> np.ndarray:
) -> List[float]:
# Normalize patch size
# Using gmean for geometric mean to scale d1 and d2 accordingly
scale_factor = (
Expand All @@ -134,7 +135,7 @@ def granp(

# Calculate p-values
t_matrix_sum_upper90 = np.quantile(t_matrix_sum, 0.90)
t_matrix_sum_mid = t_matrix_sum[t_matrix_sum < t_matrix_sum_upper90]
t_matrix_sum_mid = [val for val in t_matrix_sum if val < t_matrix_sum_upper90]
log_t_matrix_sum_mid = np.log(t_matrix_sum_mid)
log_norm_params = (log_t_matrix_sum_mid.mean(), log_t_matrix_sum_mid.std(ddof=1))

Expand Down
4 changes: 2 additions & 2 deletions test/test_bsp2.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ def test_non_empty_matrices(self):
result = _test_scores(input_sp_mat, input_exp_mat_raw, d1, d2)

# Check if the result is a numpy.ndarray
self.assertIsInstance(result, np.ndarray)
self.assertIsInstance(result, list)
# Check the shape of the result
self.assertEqual(result.shape, (2,)) # Shape depends on your function's logic
self.assertEqual(len(result), 2) # Shape depends on your function's logic


class TestGranp(unittest.TestCase):
Expand Down

0 comments on commit 69ec97f

Please sign in to comment.