Skip to content

Commit

Permalink
adding back cosine
Browse files Browse the repository at this point in the history
  • Loading branch information
edyoshikun committed Jan 10, 2025
1 parent a090eb9 commit e397de1
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions viscy/representation/evaluation/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ def calculate_cosine_similarity_cell(embedding_dataset, fov_name, track_id):
return time_points, cosine_similarities.tolist()


def calculate_euclidian_distance_cell(embedding_dataset, fov_name, track_id):
"""Extract embeddings and calculate euclidean distances for a specific cell"""
filtered_data = embedding_dataset.where(
(embedding_dataset["fov_name"] == fov_name)
& (embedding_dataset["track_id"] == track_id),
drop=True,
)
features = filtered_data["features"].values # (sample, features)
time_points = filtered_data["t"].values # (sample,)
first_time_point_embedding = features[0].reshape(1, -1)
euclidean_distances = np.linalg.norm(first_time_point_embedding - features, axis=1)
return time_points, euclidean_distances.tolist()


def compute_displacement(
embedding_dataset,
distance_metric: Literal["euclidean_squared", "cosine"] = "euclidean_squared",
Expand Down Expand Up @@ -85,8 +99,14 @@ def compute_displacement(
)[0]

if len(matching_indices) == 1:
future_embedding = embeddings[matching_indices[0]].reshape(1, -1)
displacement = np.sum((current_embedding - future_embedding) ** 2)
if distance_metric == "euclidean_squared":
future_embedding = embeddings[matching_indices[0]].reshape(1, -1)
displacement = np.sum((current_embedding - future_embedding) ** 2)
elif distance_metric == "cosine":
future_embedding = embeddings[matching_indices[0]].reshape(1, -1)
displacement = cosine_similarity(
current_embedding, future_embedding
)
displacement_per_delta_t[delta_t].append(displacement)
return dict(displacement_per_delta_t)

Expand Down

0 comments on commit e397de1

Please sign in to comment.