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

Simplify get_latents function #203

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions vamb/aamb_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from collections.abc import Sequence
from typing import Optional, IO, Union
from numpy.typing import NDArray


############################################################################# MODEL ###########################################################
Expand Down Expand Up @@ -425,8 +426,8 @@ def trainmodel(

########### funciton that retrieves the clusters from Y latents
def get_latents(
self, contignames: Sequence[str], data_loader, last_epoch: bool = True
):
self, contignames: Sequence[str], data_loader
) -> tuple[dict[str, set[str]], NDArray[np.float32]]:
"""Retrieve the categorical latent representation (y) and the contiouous latents (l) of the inputs

Inputs:
Expand All @@ -447,7 +448,7 @@ def get_latents(
latent = np.empty((length, self.ld), dtype=np.float32)
index_contigname = 0
row = 0
clust_y_dict = dict()
clust_y_dict: dict[str, set[str]] = dict()
Tensor = torch.cuda.FloatTensor if self.usecuda else torch.FloatTensor
with torch.no_grad():
for depths_in, tnfs_in, _ in new_data_loader:
Expand All @@ -473,23 +474,18 @@ def get_latents(
depths_in = depths_in.cuda()
tnfs_in = tnfs_in.cuda()

if last_epoch:
mu, _, _, _, y_sample = self(depths_in, tnfs_in)[0:5]
else:
y_sample = self(depths_in, tnfs_in)[4]
mu, _, _, _, y_sample = self(depths_in, tnfs_in)[0:5]

if self.usecuda:
Ys = y_sample.cpu().detach().numpy()
if last_epoch:
mu = mu.cpu().detach().numpy()
latent[row : row + len(mu)] = mu
row += len(mu)
mu = mu.cpu().detach().numpy()
latent[row : row + len(mu)] = mu
row += len(mu)
else:
Ys = y_sample.detach().numpy()
if last_epoch:
mu = mu.detach().numpy()
latent[row : row + len(mu)] = mu
row += len(mu)
mu = mu.detach().numpy()
latent[row : row + len(mu)] = mu
row += len(mu)
del y_sample

for _y in Ys:
Expand All @@ -505,7 +501,4 @@ def get_latents(
index_contigname += 1
del Ys

if last_epoch:
return clust_y_dict, latent
else:
return clust_y_dict
return clust_y_dict, latent