Skip to content

Commit

Permalink
z_score
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneDefauw committed Dec 13, 2024
1 parent e633e3a commit 59ac0dd
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/flowsom/models/consensus_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ class ConsensusCluster(BaseClusterEstimator):
"""

def __init__(
self, n_clusters, K=None, H=100, resample_proportion=0.9, linkage="average", cluster=AgglomerativeClustering
self,
n_clusters,
K=None,
H=100,
resample_proportion=0.9,
linkage="average",
z_score=False,
z_cap=3, # ignored if z_score is False
cluster=AgglomerativeClustering,
):
super().__init__()
assert 0 <= resample_proportion <= 1, "proportion has to be between 0 and 1"
Expand All @@ -45,6 +53,9 @@ def __init__(
self.resample_proportion = resample_proportion
self.cluster = cluster
self.linkage = linkage
self.z_score = z_score
assert z_cap > 0, f"z_cap should be stricly positive, but got {z_cap}"
self.z_cap = z_cap

def _internal_resample(self, data, proportion):
"""Resamples the data.
Expand All @@ -64,8 +75,8 @@ def fit(self, data):
* data -> (examples,attributes) format
"""
# zscore and clip
data = zscore(data, axis=0)
data = np.clip(data, a_min=-3, a_max=3)
if self.z_score:
data = self._z_score(data)
Mk = np.zeros((data.shape[0], data.shape[0]))
Is = np.zeros((data.shape[0],) * 2)
for _ in range(self.H):
Expand Down Expand Up @@ -93,6 +104,11 @@ def fit(self, data):

def fit_predict(self, data):
"""Predicts on the consensus matrix, for best found cluster number."""
data = zscore(data, axis=0)
data = np.clip(data, a_min=-3, a_max=3)
if self.z_score:
data = self._z_score(data)
return self.cluster(n_clusters=self.n_clusters, linkage=self.linkage).fit_predict(data)

def _z_score(self, data):
data = zscore(data, axis=0)
data = np.clip(data, a_min=-self.z_cap, a_max=self.z_cap)
return data

0 comments on commit 59ac0dd

Please sign in to comment.