-
Notifications
You must be signed in to change notification settings - Fork 12
Write Annotations to AnnData
#303
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
Open
srivarra
wants to merge
15
commits into
main
Choose a base branch
from
annotations/first-pass
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1ff5f4f
write embeddings data as anndata
srivarra 6c28c2d
Update viscy/representation/evaluation/__init__.py
srivarra b93d32d
Merge branch 'main' into annotations/first-pass
srivarra c3de548
added converter and example
srivarra 52bbdf1
ruff
srivarra 5c63dc5
.gitignore add blank line
srivarra 445449c
unlowerbound umap-learn
srivarra 03d6ad5
comment out scanpy example for ruff
srivarra c405f68
ruff format
srivarra 08b8d4e
added tests, simplified xarray to anndata conversion
srivarra fdc9677
rough format, ruff format
srivarra 8a155b2
fixed test, use reindex + dropna instead of .loc so it returns NaNs i…
srivarra 5a9a551
updated example script
srivarra b38b3dc
replaced pca with phate for example script
srivarra 42845ee
added cli
srivarra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,283 @@ | ||
from __future__ import annotations | ||
|
||
from pathlib import Path | ||
|
||
import anndata as ad | ||
import numpy as np | ||
import pandas as pd | ||
import pytest | ||
import xarray as xr | ||
from iohub import open_ome_zarr | ||
from pytest import TempPathFactory | ||
|
||
from viscy.representation.evaluation import ( | ||
convert_xarray_annotation_to_anndata, | ||
load_annotation_anndata, | ||
) | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def xr_embeddings_dataset( | ||
tracks_hcs_dataset: Path, tmp_path_factory: TempPathFactory | ||
) -> Path: | ||
""" | ||
Provides a mock xarray embeddings dataset with tracking information from tracks_hcs_dataset. | ||
|
||
Parameters | ||
---------- | ||
tracks_hcs_dataset : Path | ||
Path to the HCS dataset with tracking CSV files. | ||
|
||
Returns | ||
------- | ||
Path | ||
Path to the zarr store containing the embeddings dataset. | ||
""" | ||
dataset_path = tmp_path_factory.mktemp("xr_embeddings.zarr") | ||
|
||
all_tracks = [] | ||
|
||
dataset = open_ome_zarr(tracks_hcs_dataset) | ||
|
||
for fov_name, _ in dataset.positions(): | ||
tracks_csv_path = tracks_hcs_dataset / fov_name / "tracks.csv" | ||
tracks_df = pd.read_csv(tracks_csv_path) | ||
tracks_df["fov_name"] = fov_name | ||
all_tracks.append(tracks_df) | ||
|
||
# Combine all tracks | ||
tracks_df = pd.concat(all_tracks, ignore_index=True) | ||
|
||
n_samples = len(tracks_df) | ||
n_features = 32 | ||
|
||
rng = np.random.default_rng(42) | ||
|
||
# Generate synthetic features (embeddings) | ||
features = rng.normal(size=(n_samples, n_features)).astype(np.float32) | ||
|
||
# Create coordinates (PCA, UMAP, PHATE, projections) | ||
pca_coords = rng.normal(size=(n_samples, 2)).astype(np.float32) | ||
umap_coords = rng.normal(size=(n_samples, 2)).astype(np.float32) | ||
phate_coords = rng.normal(size=(n_samples, 2)).astype(np.float32) | ||
projections = rng.normal(size=(n_samples, 2)).astype( | ||
np.float32 | ||
) # 2 projection dims | ||
|
||
# Create the xarray dataset | ||
ds = xr.Dataset( | ||
data_vars={ | ||
"features": (["sample", "feature"], features), | ||
}, | ||
coords={ | ||
"fov_name": ("sample", tracks_df["fov_name"]), | ||
"track_id": ("sample", tracks_df["track_id"]), | ||
"t": ("sample", tracks_df["t"]), | ||
"id": ("sample", tracks_df["id"]), | ||
"parent_track_id": ("sample", tracks_df["parent_track_id"]), | ||
"parent_id": ("sample", tracks_df["parent_id"]), | ||
"y": ("sample", tracks_df["y"]), | ||
"x": ("sample", tracks_df["x"]), | ||
"PCA_0": ("sample", pca_coords[:, 0]), | ||
"PCA_1": ("sample", pca_coords[:, 1]), | ||
"UMAP_0": ("sample", umap_coords[:, 0]), | ||
"UMAP_1": ("sample", umap_coords[:, 1]), | ||
"PHATE_0": ("sample", phate_coords[:, 0]), | ||
"PHATE_1": ("sample", phate_coords[:, 1]), | ||
"projections": (["sample", "projection"], projections), | ||
"sample": np.arange(n_samples), | ||
"feature": np.arange(n_features), | ||
"projection": np.arange(projections.shape[1]), | ||
}, | ||
) | ||
|
||
# Save to zarr | ||
ds.to_zarr(dataset_path) | ||
|
||
return dataset_path | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def anndata_embeddings( | ||
xr_embeddings_dataset: Path, tmp_path_factory: TempPathFactory | ||
) -> Path: | ||
""" | ||
Provides an AnnData zarr store created from xarray embeddings dataset. | ||
|
||
Parameters | ||
---------- | ||
xr_embeddings_dataset : Path | ||
Path to the xarray embeddings dataset. | ||
|
||
Returns | ||
------- | ||
Path | ||
Path to the AnnData zarr store. | ||
""" | ||
rng = np.random.default_rng(42) | ||
|
||
# Create output path for AnnData | ||
adata_path = tmp_path_factory.mktemp("anndata_embeddings.zarr") | ||
|
||
# Load the xarray dataset | ||
embeddings_ds = xr.open_zarr(xr_embeddings_dataset) | ||
|
||
# Extract features as X matrix | ||
n_samples = len(embeddings_ds.coords["sample"]) | ||
|
||
X = rng.normal(size=(n_samples, 32)).astype(np.float32) | ||
|
||
obs_data = { | ||
"id": embeddings_ds.coords["id"].values, | ||
"fov_name": embeddings_ds.coords["fov_name"].values.astype(str), | ||
"track_id": embeddings_ds.coords["track_id"].values, | ||
"parent_track_id": embeddings_ds.coords["parent_track_id"].values, | ||
"parent_id": embeddings_ds.coords["parent_id"].values, | ||
"t": embeddings_ds.coords["t"].values, | ||
"y": embeddings_ds.coords["y"].values, | ||
"x": embeddings_ds.coords["x"].values, | ||
} | ||
obs_df = pd.DataFrame(obs_data) | ||
|
||
# Get the number of samples from the dataset | ||
|
||
adata = ad.AnnData( | ||
X=X, | ||
obs=obs_df, | ||
obsm={ | ||
"X_projections": rng.normal(size=(n_samples, 3)).astype(np.float32), | ||
"X_pca": rng.normal(size=(n_samples, 3)).astype(np.float32), | ||
"X_umap": rng.uniform(-10, 10, size=(n_samples, 3)).astype(np.float32), | ||
"X_phate": rng.normal(scale=0.5, size=(n_samples, 3)).astype(np.float32), | ||
}, | ||
) | ||
|
||
# Write to zarr | ||
adata.write_zarr(adata_path) | ||
|
||
return adata_path | ||
|
||
|
||
def test_convert_xarray_annotation_to_anndata(xr_embeddings_dataset, tmp_path): | ||
"""Test that convert_xarray_annotation_to_anndata correctly converts xarray to AnnData.""" | ||
# Load the xarray dataset | ||
embeddings_ds = xr.open_zarr(xr_embeddings_dataset) | ||
|
||
# Define output path | ||
output_path = tmp_path / "test_converted.zarr" | ||
|
||
# Convert to AnnData using the function we're testing | ||
adata_result = convert_xarray_annotation_to_anndata( | ||
embeddings_ds=embeddings_ds, | ||
output_path=output_path, | ||
overwrite=True, | ||
return_anndata=True, | ||
) | ||
# Second conversion with overwrite=False should raise FileExistsError | ||
with pytest.raises( | ||
FileExistsError, match=f"Output path {output_path} already exists" | ||
): | ||
convert_xarray_annotation_to_anndata( | ||
embeddings_ds=embeddings_ds, | ||
output_path=output_path, | ||
overwrite=False, | ||
return_anndata=False, | ||
) | ||
|
||
assert isinstance(adata_result, ad.AnnData) | ||
|
||
assert output_path.exists() | ||
|
||
adata_loaded = ad.read_zarr(output_path) | ||
|
||
np.testing.assert_allclose(adata_loaded.X, embeddings_ds["features"].values) | ||
|
||
# Verify obs columns | ||
expected_obs_columns = [ | ||
"id", | ||
"fov_name", | ||
"track_id", | ||
"parent_track_id", | ||
"parent_id", | ||
"t", | ||
"y", | ||
"x", | ||
] | ||
for col in expected_obs_columns: | ||
assert col in adata_loaded.obs.columns | ||
if col == "fov_name": | ||
assert list(adata_loaded.obs[col]) == list( | ||
embeddings_ds.coords[col].values.astype(str) | ||
) | ||
else: | ||
np.testing.assert_allclose( | ||
adata_loaded.obs[col].values, embeddings_ds.coords[col].values | ||
) | ||
|
||
# Verify obsm (embeddings) | ||
assert all( | ||
embedding_key in adata_loaded.obsm | ||
for embedding_key in ["X_pca", "X_umap", "X_phate", "X_projections"] | ||
) | ||
|
||
# Check projections | ||
np.testing.assert_allclose( | ||
adata_loaded.obsm["X_projections"], embeddings_ds.coords["projections"].values | ||
) | ||
|
||
# Check PCA | ||
np.testing.assert_allclose( | ||
adata_loaded.obsm["X_pca"][:, 0], embeddings_ds.coords["PCA_0"].values | ||
) | ||
np.testing.assert_allclose( | ||
adata_loaded.obsm["X_pca"][:, 1], embeddings_ds.coords["PCA_1"].values | ||
) | ||
|
||
# Check UMAP | ||
np.testing.assert_allclose( | ||
adata_loaded.obsm["X_umap"][:, 0], embeddings_ds.coords["UMAP_0"].values | ||
) | ||
np.testing.assert_allclose( | ||
adata_loaded.obsm["X_umap"][:, 1], embeddings_ds.coords["UMAP_1"].values | ||
) | ||
|
||
# Check PHATE | ||
np.testing.assert_allclose( | ||
adata_loaded.obsm["X_phate"][:, 0], embeddings_ds.coords["PHATE_0"].values | ||
) | ||
np.testing.assert_allclose( | ||
adata_loaded.obsm["X_phate"][:, 1], embeddings_ds.coords["PHATE_1"].values | ||
) | ||
|
||
|
||
def test_load_annotation_anndata(tracks_hcs_dataset, anndata_embeddings, tmp_path): | ||
"""Test that load_annotation_anndata correctly loads annotations from an AnnData object.""" | ||
# Load the AnnData object | ||
adata = ad.read_zarr(anndata_embeddings) | ||
|
||
A11_annotations_path = tracks_hcs_dataset / "A" / "1" / "1" / "tracks.csv" | ||
|
||
A11_annotations_df = pd.read_csv(A11_annotations_path) | ||
|
||
rng = np.random.default_rng(42) | ||
A11_annotations_df["fov_name"] = "A/1/1" | ||
A11_annotations_df["infection_state"] = rng.choice( | ||
[-1, 0, 1], size=len(A11_annotations_df) | ||
) | ||
|
||
# Save the modified annotations to a new CSV file | ||
annotations_path = tmp_path / "test_annotations.csv" | ||
A11_annotations_df.to_csv(annotations_path, index=False) | ||
|
||
# Test the function with the new CSV file | ||
result = load_annotation_anndata(adata, str(annotations_path), "infection_state") | ||
|
||
assert len(result) == 2 # Only 2 observations from A/1/1 have annotations | ||
|
||
expected_values = A11_annotations_df["infection_state"].values | ||
actual_values = result.values | ||
np.testing.assert_array_equal(actual_values, expected_values) | ||
|
||
# Verify the index structure | ||
assert result.index.names == ["fov_name", "id"] | ||
assert all(result.index.get_level_values("fov_name") == "A/1/1") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.