-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from WenjieDu/dev
Add internal and external cluster validation funcs, and enable CRLI and VaDER to return latent for advanced analysis
- Loading branch information
Showing
17 changed files
with
162 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ | |
# Created by Wenjie Du <[email protected]> | ||
# License: GLP-v3 | ||
|
||
from typing import Union, Optional | ||
from typing import Union, Optional, Tuple | ||
|
||
import numpy as np | ||
import torch | ||
|
@@ -50,7 +50,9 @@ def __init__( | |
n_steps, rnn_hidden_size * 2, n_features, decoder_fcn_output_dims, device | ||
) # fully connected network is included in Decoder | ||
self.kmeans = KMeans( | ||
n_clusters=n_clusters | ||
n_clusters=n_clusters, | ||
n_init=10, # FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the | ||
# value of `n_init` explicitly to suppress the warning. | ||
) # TODO: implement KMean with torch for gpu acceleration | ||
|
||
self.n_clusters = n_clusters | ||
|
@@ -417,7 +419,8 @@ def cluster( | |
self, | ||
X: Union[dict, str], | ||
file_type: str = "h5py", | ||
) -> np.ndarray: | ||
return_latent: bool = False, | ||
) -> Union[np.ndarray, Tuple[np.ndarray, dict]]: | ||
self.model.eval() # set the model as eval status to freeze it. | ||
test_set = DatasetForCRLI(X, return_labels=False, file_type=file_type) | ||
test_loader = DataLoader( | ||
|
@@ -426,15 +429,27 @@ def cluster( | |
shuffle=False, | ||
num_workers=self.num_workers, | ||
) | ||
latent_collector = [] | ||
clustering_latent_collector = [] | ||
imputation_collector = [] | ||
|
||
with torch.no_grad(): | ||
for idx, data in enumerate(test_loader): | ||
inputs = self._assemble_input_for_testing(data) | ||
inputs = self.model.forward(inputs, training=False) | ||
latent_collector.append(inputs["fcn_latent"]) | ||
clustering_latent_collector.append(inputs["fcn_latent"]) | ||
imputation_collector.append(inputs["imputation"]) | ||
|
||
latent_collector = torch.cat(latent_collector).cpu().detach().numpy() | ||
clustering = self.model.kmeans.fit_predict(latent_collector) | ||
imputation = torch.cat(imputation_collector).cpu().detach().numpy() | ||
clustering_latent = ( | ||
torch.cat(clustering_latent_collector).cpu().detach().numpy() | ||
) | ||
clustering_results = self.model.kmeans.fit_predict(clustering_latent) | ||
latent_collector = { | ||
"clustering_latent": clustering_latent, | ||
"imputation": imputation, | ||
} | ||
|
||
if return_latent: | ||
return clustering_results, latent_collector | ||
|
||
return clustering | ||
return clustering_results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.