|
1 | 1 | import anndata
|
2 | 2 | import geopandas as gpd
|
3 | 3 | import matplotlib
|
| 4 | +import numpy as np |
4 | 5 | import pandas as pd
|
5 | 6 | import scanpy as sc
|
6 | 7 | import spatialdata_plot # noqa: F401
|
@@ -142,3 +143,114 @@ def test_plot_can_plot_shapes_after_spatial_query(self, sdata_blobs: SpatialData
|
142 | 143 | axes=["x", "y"], min_coordinate=[100, 100], max_coordinate=[300, 300], target_coordinate_system="global"
|
143 | 144 | )
|
144 | 145 | cropped_blob.pl.render_shapes().pl.show()
|
| 146 | + |
| 147 | + def test_plot_can_plot_with_annotation_despite_random_shuffling(self, sdata_blobs: SpatialData): |
| 148 | + new_table = sdata_blobs.table.copy() |
| 149 | + sdata_blobs.table.obs["region"] = "blobs_circles" |
| 150 | + new_table = sdata_blobs.table[:5] |
| 151 | + new_table.uns["spatialdata_attrs"]["region"] = "blobs_circles" |
| 152 | + new_table.obs["instance_id"] = np.array(range(5)) |
| 153 | + |
| 154 | + new_table.obs["annotation"] = ["a", "b", "c", "d", "e"] |
| 155 | + new_table.obs["annotation"] = new_table.obs["annotation"].astype("category") |
| 156 | + |
| 157 | + del sdata_blobs.table |
| 158 | + sdata_blobs.table = new_table |
| 159 | + |
| 160 | + # random permutation of table and shapes |
| 161 | + sdata_blobs.table.obs = sdata_blobs.table.obs.sample(frac=1, random_state=83) |
| 162 | + temp = sdata_blobs["blobs_circles"].sample(frac=1, random_state=47) |
| 163 | + del sdata_blobs.shapes["blobs_circles"] |
| 164 | + sdata_blobs["blobs_circles"] = temp |
| 165 | + |
| 166 | + sdata_blobs.pl.render_shapes("blobs_circles", color="annotation").pl.show() |
| 167 | + |
| 168 | + def test_plot_can_plot_queried_with_annotation_despite_random_shuffling(self, sdata_blobs: SpatialData): |
| 169 | + new_table = sdata_blobs.table.copy() |
| 170 | + sdata_blobs.table.obs["region"] = "blobs_circles" |
| 171 | + new_table = sdata_blobs.table[:5] |
| 172 | + new_table.uns["spatialdata_attrs"]["region"] = "blobs_circles" |
| 173 | + new_table.obs["instance_id"] = np.array(range(5)) |
| 174 | + |
| 175 | + new_table.obs["annotation"] = ["a", "b", "c", "d", "e"] |
| 176 | + new_table.obs["annotation"] = new_table.obs["annotation"].astype("category") |
| 177 | + |
| 178 | + del sdata_blobs.table |
| 179 | + sdata_blobs.table = new_table |
| 180 | + |
| 181 | + # random permutation of table and shapes |
| 182 | + sdata_blobs.table.obs = sdata_blobs.table.obs.sample(frac=1, random_state=83) |
| 183 | + temp = sdata_blobs["blobs_circles"].sample(frac=1, random_state=47) |
| 184 | + del sdata_blobs.shapes["blobs_circles"] |
| 185 | + sdata_blobs["blobs_circles"] = temp |
| 186 | + |
| 187 | + # subsetting the data |
| 188 | + sdata_cropped = sdata_blobs.query.bounding_box( |
| 189 | + axes=("x", "y"), |
| 190 | + min_coordinate=[100, 150], |
| 191 | + max_coordinate=[400, 250], |
| 192 | + target_coordinate_system="global", |
| 193 | + filter_table=True, |
| 194 | + ) |
| 195 | + |
| 196 | + # workaround for bug that should be gone in later versions |
| 197 | + del sdata_cropped.images["blobs_multiscale_image"] |
| 198 | + del sdata_cropped.labels["blobs_labels"] |
| 199 | + del sdata_cropped.labels["blobs_multiscale_labels"] |
| 200 | + |
| 201 | + sdata_cropped.pl.render_shapes("blobs_circles", color="annotation").pl.show() |
| 202 | + |
| 203 | + def test_plot_can_color_two_shapes_elements_by_annotation(self, sdata_blobs: SpatialData): |
| 204 | + new_table = sdata_blobs.table.copy() |
| 205 | + sdata_blobs.table.obs["region"] = "blobs_circles" |
| 206 | + new_table = sdata_blobs.table[:10] |
| 207 | + new_table.uns["spatialdata_attrs"]["region"] = ["blobs_circles", "blobs_polygons"] |
| 208 | + new_table.obs["instance_id"] = np.concatenate((np.array(range(5)), np.array(range(5)))) |
| 209 | + |
| 210 | + new_table.obs.loc[5 * [False] + 5 * [True], "region"] = "blobs_polygons" |
| 211 | + new_table.obs["annotation"] = ["a", "b", "c", "d", "e", "v", "w", "x", "y", "z"] |
| 212 | + new_table.obs["annotation"] = new_table.obs["annotation"].astype("category") |
| 213 | + |
| 214 | + del sdata_blobs.table |
| 215 | + sdata_blobs.table = new_table |
| 216 | + |
| 217 | + sdata_blobs.pl.render_shapes(["blobs_circles", "blobs_polygons"], color="annotation").pl.show() |
| 218 | + |
| 219 | + def test_plot_can_color_two_queried_shapes_elements_by_annotation(self, sdata_blobs: SpatialData): |
| 220 | + new_table = sdata_blobs.table.copy() |
| 221 | + sdata_blobs.table.obs["region"] = "blobs_circles" |
| 222 | + new_table = sdata_blobs.table[:10] |
| 223 | + new_table.uns["spatialdata_attrs"]["region"] = ["blobs_circles", "blobs_polygons"] |
| 224 | + new_table.obs["instance_id"] = np.concatenate((np.array(range(5)), np.array(range(5)))) |
| 225 | + |
| 226 | + new_table.obs.loc[5 * [False] + 5 * [True], "region"] = "blobs_polygons" |
| 227 | + new_table.obs["annotation"] = ["a", "b", "c", "d", "e", "v", "w", "x", "y", "z"] |
| 228 | + new_table.obs["annotation"] = new_table.obs["annotation"].astype("category") |
| 229 | + |
| 230 | + del sdata_blobs.table |
| 231 | + sdata_blobs.table = new_table |
| 232 | + |
| 233 | + # random permutation of table and shapes |
| 234 | + sdata_blobs.table.obs = sdata_blobs.table.obs.sample(frac=1, random_state=83) |
| 235 | + temp = sdata_blobs["blobs_circles"].sample(frac=1, random_state=47) |
| 236 | + del sdata_blobs.shapes["blobs_circles"] |
| 237 | + sdata_blobs["blobs_circles"] = temp |
| 238 | + temp = sdata_blobs["blobs_polygons"].sample(frac=1, random_state=71) |
| 239 | + del sdata_blobs.shapes["blobs_polygons"] |
| 240 | + sdata_blobs["blobs_polygons"] = temp |
| 241 | + |
| 242 | + # subsetting the data |
| 243 | + sdata_cropped = sdata_blobs.query.bounding_box( |
| 244 | + axes=("x", "y"), |
| 245 | + min_coordinate=[100, 150], |
| 246 | + max_coordinate=[350, 300], |
| 247 | + target_coordinate_system="global", |
| 248 | + filter_table=True, |
| 249 | + ) |
| 250 | + |
| 251 | + # workaround for bug that should be gone in later versions |
| 252 | + del sdata_cropped.images["blobs_multiscale_image"] |
| 253 | + del sdata_cropped.labels["blobs_labels"] |
| 254 | + del sdata_cropped.labels["blobs_multiscale_labels"] |
| 255 | + |
| 256 | + sdata_cropped.pl.render_shapes(["blobs_circles", "blobs_polygons"], color="annotation").pl.show() |
0 commit comments