|
| 1 | +import pytest |
| 2 | + |
| 3 | +from spatialdata import SpatialData, concatenate, match_sdata_to_table |
| 4 | +from spatialdata.datasets import blobs_annotating_element |
| 5 | + |
| 6 | + |
| 7 | +def _make_test_data() -> SpatialData: |
| 8 | + sdata1 = blobs_annotating_element("blobs_polygons") |
| 9 | + sdata2 = blobs_annotating_element("blobs_polygons") |
| 10 | + sdata = concatenate({"sdata1": sdata1, "sdata2": sdata2}, concatenate_tables=True) |
| 11 | + sdata["table"].obs["value"] = list(range(sdata["table"].obs.shape[0])) |
| 12 | + return sdata |
| 13 | + |
| 14 | + |
| 15 | +# constructing the example data; let's use a global variable as we can reuse the same object on most tests |
| 16 | +# without having to recreate it |
| 17 | +sdata = _make_test_data() |
| 18 | + |
| 19 | + |
| 20 | +def test_match_sdata_to_table_filter_specific_instances(): |
| 21 | + """ |
| 22 | + Filter to keep only specific instances. Note that it works even when the table annotates multiple elements. |
| 23 | + """ |
| 24 | + matched = match_sdata_to_table( |
| 25 | + sdata, |
| 26 | + table=sdata["table"][sdata["table"].obs.instance_id.isin([1, 2])], |
| 27 | + table_name="table", |
| 28 | + ) |
| 29 | + assert len(matched["table"]) == 4 |
| 30 | + assert "blobs_polygons-sdata1" in matched |
| 31 | + assert "blobs_polygons-sdata2" in matched |
| 32 | + |
| 33 | + |
| 34 | +def test_match_sdata_to_table_filter_specific_instances_element(): |
| 35 | + """ |
| 36 | + Filter to keep only specific instances, in a specific element. |
| 37 | + """ |
| 38 | + matched = match_sdata_to_table( |
| 39 | + sdata, |
| 40 | + table=sdata["table"][ |
| 41 | + sdata["table"].obs.instance_id.isin([1, 2]) & (sdata["table"].obs.region == "blobs_polygons-sdata1") |
| 42 | + ], |
| 43 | + table_name="table", |
| 44 | + ) |
| 45 | + assert len(matched["table"]) == 2 |
| 46 | + assert "blobs_polygons-sdata1" in matched |
| 47 | + assert "blobs_polygons-sdata2" not in matched |
| 48 | + |
| 49 | + |
| 50 | +def test_match_sdata_to_table_filter_by_threshold(): |
| 51 | + """ |
| 52 | + Filter by a threshold on a value column, in a specific element. |
| 53 | + """ |
| 54 | + matched = match_sdata_to_table( |
| 55 | + sdata, |
| 56 | + table=sdata["table"][sdata["table"].obs.query('value < 5 and region == "blobs_polygons-sdata1"').index], |
| 57 | + table_name="table", |
| 58 | + ) |
| 59 | + assert len(matched["table"]) == 5 |
| 60 | + assert "blobs_polygons-sdata1" in matched |
| 61 | + assert "blobs_polygons-sdata2" not in matched |
| 62 | + |
| 63 | + |
| 64 | +def test_match_sdata_to_table_subset_certain_obs(): |
| 65 | + """ |
| 66 | + Subset to certain obs (we could also subset to certain var or layer). |
| 67 | + """ |
| 68 | + matched = match_sdata_to_table( |
| 69 | + sdata, |
| 70 | + table=sdata["table"][[0, 1, 2, 3]], |
| 71 | + table_name="table", |
| 72 | + ) |
| 73 | + assert len(matched["table"]) == 4 |
| 74 | + assert "blobs_polygons-sdata1" in matched |
| 75 | + assert "blobs_polygons-sdata2" not in matched |
| 76 | + |
| 77 | + |
| 78 | +def test_match_sdata_to_table_shapes_and_points(): |
| 79 | + """ |
| 80 | + The function works both for shapes (examples above) and points. |
| 81 | + Changes the target of the table to labels. |
| 82 | + """ |
| 83 | + sdata = _make_test_data() |
| 84 | + sdata["table"].obs["region"] = sdata["table"].obs["region"].apply(lambda x: x.replace("polygons", "points")) |
| 85 | + sdata["table"].obs["region"] = sdata["table"].obs["region"].astype("category") |
| 86 | + sdata.set_table_annotates_spatialelement( |
| 87 | + table_name="table", |
| 88 | + region=["blobs_points-sdata1", "blobs_points-sdata2"], |
| 89 | + region_key="region", |
| 90 | + instance_key="instance_id", |
| 91 | + ) |
| 92 | + |
| 93 | + matched = match_sdata_to_table( |
| 94 | + sdata, |
| 95 | + table=sdata["table"], |
| 96 | + table_name="table", |
| 97 | + ) |
| 98 | + |
| 99 | + assert len(matched["table"]) == 10 |
| 100 | + assert "blobs_points-sdata1" in matched |
| 101 | + assert "blobs_points-sdata2" in matched |
| 102 | + assert "blobs_polygons-sdata1" not in matched |
| 103 | + |
| 104 | + |
| 105 | +def test_match_sdata_to_table_match_labels_error(): |
| 106 | + """ |
| 107 | + match_sdata_to_table() uses the join operations; so when trying to match labels, the error will be raised by the |
| 108 | + join. |
| 109 | + """ |
| 110 | + sdata = _make_test_data() |
| 111 | + sdata["table"].obs["region"] = sdata["table"].obs["region"].apply(lambda x: x.replace("polygons", "labels")) |
| 112 | + sdata["table"].obs["region"] = sdata["table"].obs["region"].astype("category") |
| 113 | + sdata.set_table_annotates_spatialelement( |
| 114 | + table_name="table", |
| 115 | + region=["blobs_labels-sdata1", "blobs_labels-sdata2"], |
| 116 | + region_key="region", |
| 117 | + instance_key="instance_id", |
| 118 | + ) |
| 119 | + |
| 120 | + with pytest.warns( |
| 121 | + UserWarning, |
| 122 | + match="Element type `labels` not supported for 'right' join. Skipping ", |
| 123 | + ): |
| 124 | + matched = match_sdata_to_table( |
| 125 | + sdata, |
| 126 | + table=sdata["table"], |
| 127 | + table_name="table", |
| 128 | + ) |
| 129 | + |
| 130 | + assert len(matched["table"]) == 10 |
| 131 | + assert "blobs_labels-sdata1" in matched |
| 132 | + assert "blobs_labels-sdata2" in matched |
| 133 | + assert "blobs_points-sdata1" not in matched |
| 134 | + |
| 135 | + |
| 136 | +def test_match_sdata_to_table_no_table_argument(): |
| 137 | + """ |
| 138 | + If no table argument is passed, the table_name argument will be used to match the table. |
| 139 | + """ |
| 140 | + matched = match_sdata_to_table(sdata=sdata, table_name="table") |
| 141 | + |
| 142 | + assert len(matched["table"]) == 10 |
| 143 | + assert "blobs_polygons-sdata1" in matched |
| 144 | + assert "blobs_polygons-sdata2" in matched |
0 commit comments