@@ -101,7 +101,7 @@ def _make_multi():
101101
102102 def test_plot_can_color_from_geodataframe (self , sdata_blobs : SpatialData ):
103103 blob = deepcopy (sdata_blobs )
104- blob ["table" ].obs ["region" ] = "blobs_polygons"
104+ blob ["table" ].obs ["region" ] = pd . Categorical ([ "blobs_polygons" ] * blob [ "table" ]. n_obs )
105105 blob ["table" ].uns ["spatialdata_attrs" ]["region" ] = "blobs_polygons"
106106 blob .shapes ["blobs_polygons" ]["value" ] = [1 , 10 , 1 , 20 , 1 ]
107107 blob .pl .render_shapes (
@@ -115,7 +115,7 @@ def test_plot_can_scale_shapes(self, sdata_blobs: SpatialData):
115115 def test_plot_can_filter_with_groups (self , sdata_blobs : SpatialData ):
116116 _ , axs = plt .subplots (nrows = 1 , ncols = 2 , layout = "tight" )
117117
118- sdata_blobs ["table" ].obs ["region" ] = "blobs_polygons"
118+ sdata_blobs ["table" ].obs ["region" ] = pd . Categorical ([ "blobs_polygons" ] * sdata_blobs [ "table" ]. n_obs )
119119 sdata_blobs ["table" ].uns ["spatialdata_attrs" ]["region" ] = "blobs_polygons"
120120 sdata_blobs .shapes ["blobs_polygons" ]["cluster" ] = "c1"
121121 sdata_blobs .shapes ["blobs_polygons" ].iloc [3 :5 , 1 ] = "c2"
@@ -129,7 +129,7 @@ def test_plot_can_filter_with_groups(self, sdata_blobs: SpatialData):
129129 )
130130
131131 def test_plot_coloring_with_palette (self , sdata_blobs : SpatialData ):
132- sdata_blobs ["table" ].obs ["region" ] = "blobs_polygons"
132+ sdata_blobs ["table" ].obs ["region" ] = pd . Categorical ([ "blobs_polygons" ] * sdata_blobs [ "table" ]. n_obs )
133133 sdata_blobs ["table" ].uns ["spatialdata_attrs" ]["region" ] = "blobs_polygons"
134134 sdata_blobs .shapes ["blobs_polygons" ]["cluster" ] = "c1"
135135 sdata_blobs .shapes ["blobs_polygons" ].iloc [3 :5 , 1 ] = "c2"
@@ -142,21 +142,21 @@ def test_plot_coloring_with_palette(self, sdata_blobs: SpatialData):
142142 ).pl .show ()
143143
144144 def test_plot_colorbar_respects_input_limits (self , sdata_blobs : SpatialData ):
145- sdata_blobs ["table" ].obs ["region" ] = "blobs_polygons"
145+ sdata_blobs ["table" ].obs ["region" ] = pd . Categorical ([ "blobs_polygons" ] * sdata_blobs [ "table" ]. n_obs )
146146 sdata_blobs ["table" ].uns ["spatialdata_attrs" ]["region" ] = "blobs_polygons"
147147 sdata_blobs .shapes ["blobs_polygons" ]["cluster" ] = [1 , 2 , 3 , 5 , 20 ]
148148 sdata_blobs .pl .render_shapes ("blobs_polygons" , color = "cluster" ).pl .show ()
149149
150150 def test_plot_colorbar_can_be_normalised (self , sdata_blobs : SpatialData ):
151- sdata_blobs ["table" ].obs ["region" ] = "blobs_polygons"
151+ sdata_blobs ["table" ].obs ["region" ] = pd . Categorical ([ "blobs_polygons" ] * sdata_blobs [ "table" ]. n_obs )
152152 sdata_blobs ["table" ].uns ["spatialdata_attrs" ]["region" ] = "blobs_polygons"
153153 sdata_blobs .shapes ["blobs_polygons" ]["cluster" ] = [1 , 2 , 3 , 5 , 20 ]
154154 norm = Normalize (vmin = 0 , vmax = 5 , clip = True )
155155 sdata_blobs .pl .render_shapes ("blobs_polygons" , color = "cluster" , groups = ["c1" ], norm = norm ).pl .show ()
156156
157157 def test_plot_can_plot_shapes_after_spatial_query (self , sdata_blobs : SpatialData ):
158158 # subset to only shapes, should be unnecessary after rasterizeation of multiscale images is included
159- blob = SpatialData .from_elements_dict (
159+ blob = SpatialData .init_from_elements (
160160 {
161161 "blobs_circles" : sdata_blobs .shapes ["blobs_circles" ],
162162 "blobs_multipolygons" : sdata_blobs .shapes ["blobs_multipolygons" ],
@@ -169,7 +169,7 @@ def test_plot_can_plot_shapes_after_spatial_query(self, sdata_blobs: SpatialData
169169 cropped_blob .pl .render_shapes ().pl .show ()
170170
171171 def test_plot_can_plot_with_annotation_despite_random_shuffling (self , sdata_blobs : SpatialData ):
172- sdata_blobs ["table" ].obs ["region" ] = "blobs_circles"
172+ sdata_blobs ["table" ].obs ["region" ] = pd . Categorical ([ "blobs_circles" ] * sdata_blobs [ "table" ]. n_obs )
173173 new_table = sdata_blobs ["table" ][:5 ]
174174 new_table .uns ["spatialdata_attrs" ]["region" ] = "blobs_circles"
175175 new_table .obs ["instance_id" ] = np .array (range (5 ))
@@ -188,7 +188,7 @@ def test_plot_can_plot_with_annotation_despite_random_shuffling(self, sdata_blob
188188 sdata_blobs .pl .render_shapes ("blobs_circles" , color = "annotation" ).pl .show ()
189189
190190 def test_plot_can_plot_queried_with_annotation_despite_random_shuffling (self , sdata_blobs : SpatialData ):
191- sdata_blobs ["table" ].obs ["region" ] = "blobs_circles"
191+ sdata_blobs ["table" ].obs ["region" ] = pd . Categorical ([ "blobs_circles" ] * sdata_blobs [ "table" ]. n_obs )
192192 new_table = sdata_blobs ["table" ][:5 ].copy ()
193193 new_table .uns ["spatialdata_attrs" ]["region" ] = "blobs_circles"
194194 new_table .obs ["instance_id" ] = np .array (range (5 ))
@@ -216,11 +216,12 @@ def test_plot_can_plot_queried_with_annotation_despite_random_shuffling(self, sd
216216 sdata_cropped .pl .render_shapes ("blobs_circles" , color = "annotation" ).pl .show ()
217217
218218 def test_plot_can_color_two_shapes_elements_by_annotation (self , sdata_blobs : SpatialData ):
219- sdata_blobs ["table" ].obs ["region" ] = "blobs_circles"
219+ sdata_blobs ["table" ].obs ["region" ] = pd . Categorical ([ "blobs_circles" ] * sdata_blobs [ "table" ]. n_obs )
220220 new_table = sdata_blobs ["table" ][:10 ].copy ()
221221 new_table .uns ["spatialdata_attrs" ]["region" ] = ["blobs_circles" , "blobs_polygons" ]
222222 new_table .obs ["instance_id" ] = np .concatenate ((np .array (range (5 )), np .array (range (5 ))))
223223
224+ new_table .obs ["region" ] = new_table .obs ["region" ].cat .add_categories (["blobs_polygons" ])
224225 new_table .obs .loc [5 * [False ] + 5 * [True ], "region" ] = "blobs_polygons"
225226 new_table .obs ["annotation" ] = ["a" , "b" , "c" , "d" , "e" , "v" , "w" , "x" , "y" , "z" ]
226227 new_table .obs ["annotation" ] = new_table .obs ["annotation" ].astype ("category" )
@@ -232,11 +233,12 @@ def test_plot_can_color_two_shapes_elements_by_annotation(self, sdata_blobs: Spa
232233 ).pl .show ()
233234
234235 def test_plot_can_color_two_queried_shapes_elements_by_annotation (self , sdata_blobs : SpatialData ):
235- sdata_blobs ["table" ].obs ["region" ] = "blobs_circles"
236+ sdata_blobs ["table" ].obs ["region" ] = pd . Categorical ([ "blobs_circles" ] * sdata_blobs [ "table" ]. n_obs )
236237 new_table = sdata_blobs ["table" ][:10 ].copy ()
237238 new_table .uns ["spatialdata_attrs" ]["region" ] = ["blobs_circles" , "blobs_polygons" ]
238239 new_table .obs ["instance_id" ] = np .concatenate ((np .array (range (5 )), np .array (range (5 ))))
239240
241+ new_table .obs ["region" ] = new_table .obs ["region" ].cat .add_categories (["blobs_polygons" ])
240242 new_table .obs .loc [5 * [False ] + 5 * [True ], "region" ] = "blobs_polygons"
241243 new_table .obs ["annotation" ] = ["a" , "b" , "c" , "d" , "e" , "v" , "w" , "x" , "y" , "z" ]
242244 new_table .obs ["annotation" ] = new_table .obs ["annotation" ].astype ("category" )
@@ -364,7 +366,7 @@ def test_plot_can_color_by_category_with_cmap(self, sdata_blobs: SpatialData):
364366 sdata_blobs .pl .render_shapes (element = "blobs_polygons" , color = "category" , cmap = "cool" ).pl .show ()
365367
366368 def test_plot_datashader_can_color_by_value (self , sdata_blobs : SpatialData ):
367- sdata_blobs ["table" ].obs ["region" ] = "blobs_polygons"
369+ sdata_blobs ["table" ].obs ["region" ] = pd . Categorical ([ "blobs_polygons" ] * sdata_blobs [ "table" ]. n_obs )
368370 sdata_blobs ["table" ].uns ["spatialdata_attrs" ]["region" ] = "blobs_polygons"
369371 sdata_blobs .shapes ["blobs_polygons" ]["value" ] = [1 , 10 , 1 , 20 , 1 ]
370372 sdata_blobs .pl .render_shapes (element = "blobs_polygons" , color = "value" , method = "datashader" ).pl .show ()
@@ -374,13 +376,13 @@ def test_plot_datashader_can_color_by_identical_value(self, sdata_blobs: Spatial
374376 We test this, because datashader internally scales the values, so when all shapes have the same value,
375377 the scaling would lead to all of them being assigned an alpha of 0, so we wouldn't see anything
376378 """
377- sdata_blobs ["table" ].obs ["region" ] = ["blobs_polygons" ] * sdata_blobs ["table" ].n_obs
379+ sdata_blobs ["table" ].obs ["region" ] = pd . Categorical ( ["blobs_polygons" ] * sdata_blobs ["table" ].n_obs )
378380 sdata_blobs ["table" ].uns ["spatialdata_attrs" ]["region" ] = "blobs_polygons"
379381 sdata_blobs .shapes ["blobs_polygons" ]["value" ] = [1 , 1 , 1 , 1 , 1 ]
380382 sdata_blobs .pl .render_shapes (element = "blobs_polygons" , color = "value" , method = "datashader" ).pl .show ()
381383
382384 def test_plot_datashader_shades_with_linear_cmap (self , sdata_blobs : SpatialData ):
383- sdata_blobs ["table" ].obs ["region" ] = ["blobs_polygons" ] * sdata_blobs ["table" ].n_obs
385+ sdata_blobs ["table" ].obs ["region" ] = pd . Categorical ( ["blobs_polygons" ] * sdata_blobs ["table" ].n_obs )
384386 sdata_blobs ["table" ].uns ["spatialdata_attrs" ]["region" ] = "blobs_polygons"
385387 sdata_blobs .shapes ["blobs_polygons" ]["value" ] = [1 , 2 , 1 , 20 , 1 ]
386388 sdata_blobs .pl .render_shapes (element = "blobs_polygons" , color = "value" , method = "datashader" ).pl .show ()
@@ -414,7 +416,7 @@ def test_plot_datashader_can_render_with_rgba_colored_outline(self, sdata_blobs:
414416 def test_plot_can_set_clims_clip (self , sdata_blobs : SpatialData ):
415417 table_shapes = sdata_blobs ["table" ][:5 ].copy ()
416418 table_shapes .obs .instance_id = list (range (5 ))
417- table_shapes .obs ["region" ] = "blobs_circles"
419+ table_shapes .obs ["region" ] = pd . Categorical ([ "blobs_circles" ] * table_shapes . n_obs )
418420 table_shapes .obs ["dummy_gene_expression" ] = [i * 10 for i in range (5 )]
419421 table_shapes .uns ["spatialdata_attrs" ]["region" ] = "blobs_circles"
420422 sdata_blobs ["new_table" ] = table_shapes
@@ -493,7 +495,7 @@ def test_plot_datashader_can_transform_circles(self, sdata_blobs: SpatialData):
493495 def test_plot_can_do_non_matching_table (self , sdata_blobs : SpatialData ):
494496 table_shapes = sdata_blobs ["table" ][:3 ].copy ()
495497 table_shapes .obs .instance_id = list (range (3 ))
496- table_shapes .obs ["region" ] = "blobs_circles"
498+ table_shapes .obs ["region" ] = pd . Categorical ([ "blobs_circles" ] * table_shapes . n_obs )
497499 table_shapes .uns ["spatialdata_attrs" ]["region" ] = "blobs_circles"
498500 sdata_blobs ["new_table" ] = table_shapes
499501
0 commit comments