Skip to content

Commit

Permalink
Merge pull request #12 from YosefLab/dist-patch
Browse files Browse the repository at this point in the history
fixed distance with dataframes
  • Loading branch information
colganwi authored Sep 27, 2024
2 parents 4cc120e + fa435fc commit 19eebb2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/pycea/tl/distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ def distance(
connect_key = _format_keys(connect_key, "connectivities")
single_obs = False
X = get_keyed_obsm_data(tdata, key)
if isinstance(X, pd.DataFrame):
X = X.values
if update:
_check_previous_params(
tdata, {"metric": metric, "metric_kwds": metric_kwds}, key_added, ["neighbors", "distances"]
Expand Down
Binary file modified tests/data/tdata.h5ad
Binary file not shown.
9 changes: 8 additions & 1 deletion tests/test_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
def tdata():
tdata = td.TreeData(
obs=pd.DataFrame({"group": ["1", "1", "2"]}, index=["A", "B", "C"]),
obsm={"spatial": np.array([[0, 0], [1, 1], [2, 2]]), "characters": np.array([[0, 0], [1, 1], [0, 1]])},
obsm={
"spatial": np.array([[0, 0], [1, 1], [2, 2]]),
"characters": pd.DataFrame(np.array([[0, 0], [1, 1], [0, 1]]), index=["A", "B", "C"], columns=["c1", "c2"]),
},
obsp={"connectivities": sp.sparse.csr_matrix(([1, 1], ([0, 0], [1, 2])), shape=(3, 3))},
)
yield tdata
Expand Down Expand Up @@ -63,6 +66,10 @@ def test_sampled_distance(tdata):
assert tdata.obsp["spatial_distances"].data.tolist() == [2, 0]
assert tdata.obsp["spatial_connectivities"].shape == (3, 3)
assert len(tdata.obsp["spatial_connectivities"].data) == 2
distance(tdata, "characters", sample_n=2, metric="cityblock", random_state=3)
assert tdata.obsp["characters_distances"].shape == (3, 3)
assert len(tdata.obsp["characters_distances"].data) == 2
assert tdata.obsp["characters_distances"].data.tolist() == [1, 1]


def test_connected_distance(tdata):
Expand Down

0 comments on commit 19eebb2

Please sign in to comment.