12
12
check_assays_are_equal ,
13
13
merge_assays ,
14
14
merge_se_colnames ,
15
- relaxed_merge_assays
15
+ relaxed_merge_assays ,
16
16
)
17
17
from summarizedexperiment ._frameutils import _sanitize_frame
18
18
from summarizedexperiment .RangedSummarizedExperiment import GRangesOrGRangesList
19
19
from singlecellexperiment import SingleCellExperiment
20
- from singlecellexperiment ._combineutils import (
21
- merge_generic ,
22
- relaxed_merge_generic ,
23
- relaxed_merge_numpy_generic
24
- )
20
+ from singlecellexperiment ._combineutils import merge_generic , relaxed_merge_generic , relaxed_merge_numpy_generic
25
21
26
22
from ._imgutils import get_img_idx
27
23
from ._validators import (
@@ -202,9 +198,7 @@ def __init__(
202
198
column_data = _sanitize_frame (column_data , num_rows = self .shape [1 ])
203
199
204
200
if not column_data .has_column ("sample_id" ):
205
- column_data ["sample_id" ] = ["sample01" ] * self .shape [
206
- 1
207
- ] # hard code default sample_id as "sample01"
201
+ column_data ["sample_id" ] = ["sample01" ] * self .shape [1 ] # hard code default sample_id as "sample01"
208
202
209
203
spatial_coords = _sanitize_frame (spatial_coords , num_rows = self .shape [1 ])
210
204
img_data = _sanitize_frame (img_data , num_rows = 0 )
@@ -217,9 +211,7 @@ def __init__(
217
211
_validate_column_data (column_data = column_data )
218
212
_validate_img_data (img_data = img_data )
219
213
_validate_sample_ids (column_data = column_data , img_data = img_data )
220
- _validate_spatial_coords (
221
- spatial_coords = spatial_coords , column_data = column_data
222
- )
214
+ _validate_spatial_coords (spatial_coords = spatial_coords , column_data = column_data )
223
215
224
216
#########################
225
217
######>> Copying <<######
@@ -323,14 +315,10 @@ def __repr__(self) -> str:
323
315
output += ", row_ranges=" + self ._row_ranges .__repr__ ()
324
316
325
317
if self ._alternative_experiments is not None :
326
- output += ", alternative_experiments=" + ut .print_truncated_list (
327
- self .alternative_experiment_names
328
- )
318
+ output += ", alternative_experiments=" + ut .print_truncated_list (self .alternative_experiment_names )
329
319
330
320
if self ._reduced_dims is not None :
331
- output += ", reduced_dims=" + ut .print_truncated_list (
332
- self .reduced_dim_names
333
- )
321
+ output += ", reduced_dims=" + ut .print_truncated_list (self .reduced_dim_names )
334
322
335
323
if self ._main_experiment_name is not None :
336
324
output += ", main_experiment_name=" + self ._main_experiment_name
@@ -358,10 +346,14 @@ def __str__(self) -> str:
358
346
359
347
output += f"assays({ len (self .assay_names )} ): { ut .print_truncated_list (self .assay_names )} \n "
360
348
361
- output += f"row_data columns({ len (self ._rows .column_names )} ): { ut .print_truncated_list (self ._rows .column_names )} \n "
349
+ output += (
350
+ f"row_data columns({ len (self ._rows .column_names )} ): { ut .print_truncated_list (self ._rows .column_names )} \n "
351
+ )
362
352
output += f"row_names({ 0 if self ._row_names is None else len (self ._row_names )} ): { ' ' if self ._row_names is None else ut .print_truncated_list (self ._row_names )} \n "
363
353
364
- output += f"column_data columns({ len (self ._cols .column_names )} ): { ut .print_truncated_list (self ._cols .column_names )} \n "
354
+ output += (
355
+ f"column_data columns({ len (self ._cols .column_names )} ): { ut .print_truncated_list (self ._cols .column_names )} \n "
356
+ )
365
357
output += f"column_names({ 0 if self ._column_names is None else len (self ._column_names )} ): { ' ' if self ._column_names is None else ut .print_truncated_list (self ._column_names )} \n "
366
358
367
359
output += f"main_experiment_name: { ' ' if self ._main_experiment_name is None else self ._main_experiment_name } \n "
@@ -434,9 +426,7 @@ def set_spatial_coords(
434
426
in_place : bool = False ,
435
427
) -> "SpatialExperiment" :
436
428
"""Alias for :py:meth:`~set_spatial_coordinates`."""
437
- return self .set_spatial_coordinates (
438
- spatial_coords = spatial_coords , in_place = in_place
439
- )
429
+ return self .set_spatial_coordinates (spatial_coords = spatial_coords , in_place = in_place )
440
430
441
431
@property
442
432
def spatial_coords (self ) -> BiocFrame :
@@ -458,9 +448,7 @@ def spatial_coordinates(self) -> BiocFrame:
458
448
return self .get_spatial_coordinates ()
459
449
460
450
@spatial_coordinates .setter
461
- def spatial_coordinates (
462
- self , spatial_coords : Optional [Union [BiocFrame , np .ndarray ]]
463
- ):
451
+ def spatial_coordinates (self , spatial_coords : Optional [Union [BiocFrame , np .ndarray ]]):
464
452
"""Alias for :py:meth:`~set_spatial_coordinates`."""
465
453
warn (
466
454
"Setting property 'spatial_coords' is an in-place operation, use 'set_spatial_coordinates' instead." ,
@@ -510,21 +498,15 @@ def set_spatial_coordinates_names(
510
498
new_spatial_coords = self ._spatial_coords
511
499
else :
512
500
_validate_spatial_coords_names (spatial_coords_names , self ._spatial_coords )
513
- new_spatial_coords = self ._spatial_coords .set_column_names (
514
- spatial_coords_names
515
- )
501
+ new_spatial_coords = self ._spatial_coords .set_column_names (spatial_coords_names )
516
502
517
503
output = self ._define_output (in_place )
518
504
output ._spatial_coords = new_spatial_coords
519
505
return output
520
506
521
- def set_spatial_coords_names (
522
- self , spatial_coords_names : List [str ], in_place : bool = False
523
- ) -> "SpatialExperiment" :
507
+ def set_spatial_coords_names (self , spatial_coords_names : List [str ], in_place : bool = False ) -> "SpatialExperiment" :
524
508
"""Alias for :py:meth:`~set_spatial_coordinates_names`."""
525
- return self .set_spatial_coordinates_names (
526
- spatial_coords_names = spatial_coords_names , in_place = in_place
527
- )
509
+ return self .set_spatial_coordinates_names (spatial_coords_names = spatial_coords_names , in_place = in_place )
528
510
529
511
@property
530
512
def spatial_coords_names (self ) -> List [str ]:
@@ -538,9 +520,7 @@ def spatial_coords_names(self, spatial_coords_names: List[str]):
538
520
"Setting property 'spatial_coords_names' is an in-place operation, use 'set_spatial_coordinates_names' instead." ,
539
521
UserWarning ,
540
522
)
541
- self .set_spatial_coordinates_names (
542
- spatial_coords_names = spatial_coords_names , in_place = True
543
- )
523
+ self .set_spatial_coordinates_names (spatial_coords_names = spatial_coords_names , in_place = True )
544
524
545
525
@property
546
526
def spatial_coordinates_names (self ) -> List [str ]:
@@ -554,9 +534,7 @@ def spatial_coordinates_names(self, spatial_coords_names: List[str]):
554
534
"Setting property 'spatial_coords_names' is an in-place operation, use 'set_spatial_coordinates_names' instead." ,
555
535
UserWarning ,
556
536
)
557
- self .set_spatial_coordinates_names (
558
- spatial_coords_names = spatial_coords_names , in_place = True
559
- )
537
+ self .set_spatial_coordinates_names (spatial_coords_names = spatial_coords_names , in_place = True )
560
538
561
539
##############################
562
540
########>> img_data <<########
@@ -574,9 +552,7 @@ def get_img_data(self) -> BiocFrame:
574
552
"""Alias for :py:meth:`~get_image_data`."""
575
553
return self .get_image_data ()
576
554
577
- def set_image_data (
578
- self , img_data : Optional [BiocFrame ], in_place : bool = False
579
- ) -> "SpatialExperiment" :
555
+ def set_image_data (self , img_data : Optional [BiocFrame ], in_place : bool = False ) -> "SpatialExperiment" :
580
556
"""Set new image data.
581
557
582
558
Args:
@@ -605,9 +581,7 @@ def set_image_data(
605
581
output ._img_data = img_data
606
582
return output
607
583
608
- def set_img_data (
609
- self , img_data : BiocFrame , in_place : bool = False
610
- ) -> "SpatialExperiment" :
584
+ def set_img_data (self , img_data : BiocFrame , in_place : bool = False ) -> "SpatialExperiment" :
611
585
"""Alias for :py:meth:`~set_image_data`."""
612
586
return self .set_image_data (img_data = img_data , in_place = in_place )
613
587
@@ -669,9 +643,7 @@ def get_scale_factors(
669
643
_validate_id (sample_id )
670
644
_validate_id (image_id )
671
645
672
- idxs = get_img_idx (
673
- img_data = self .img_data , sample_id = sample_id , image_id = image_id
674
- )
646
+ idxs = get_img_idx (img_data = self .img_data , sample_id = sample_id , image_id = image_id )
675
647
676
648
return self .img_data [idxs ,]["scale_factor" ]
677
649
@@ -734,19 +706,15 @@ def get_slice(
734
706
spe = super ().get_slice (rows = rows , columns = columns )
735
707
736
708
slicer = self ._generic_slice (rows = rows , columns = columns )
737
- do_slice_cols = not (
738
- isinstance (slicer .col_indices , slice ) and slicer .col_indices == slice (None )
739
- )
709
+ do_slice_cols = not (isinstance (slicer .col_indices , slice ) and slicer .col_indices == slice (None ))
740
710
741
711
new_spatial_coords = None
742
712
743
713
if do_slice_cols :
744
714
new_spatial_coords = self .spatial_coords [slicer .col_indices , :]
745
715
746
716
column_sample_ids = set (spe .column_data ["sample_id" ])
747
- mask = [
748
- sample_id in column_sample_ids for sample_id in self .img_data ["sample_id" ]
749
- ]
717
+ mask = [sample_id in column_sample_ids for sample_id in self .img_data ["sample_id" ]]
750
718
751
719
new_img_data = self .img_data [mask ,]
752
720
@@ -822,11 +790,9 @@ def get_img(
822
790
if not self .img_data :
823
791
return None
824
792
825
- idxs = get_img_idx (
826
- img_data = self .img_data , sample_id = sample_id , image_id = image_id
827
- )
793
+ indices = get_img_idx (img_data = self .img_data , sample_id = sample_id , image_id = image_id )
828
794
829
- images = self .img_data [idxs ,]["data" ]
795
+ images = self .img_data [indices ,]["data" ]
830
796
return images [0 ] if len (images ) == 1 else images
831
797
832
798
def add_img (
@@ -869,9 +835,7 @@ def add_img(
869
835
Raises:
870
836
ValueError: If the sample_id and image_id pair already exists.
871
837
"""
872
- _validate_sample_image_ids (
873
- img_data = self ._img_data , new_sample_id = sample_id , new_image_id = image_id
874
- )
838
+ _validate_sample_image_ids (img_data = self ._img_data , new_sample_id = sample_id , new_image_id = image_id )
875
839
876
840
if isinstance (image_source , (str , Path )):
877
841
is_url = urlparse (str (image_source )).scheme in ("http" , "https" , "ftp" )
@@ -897,12 +861,8 @@ def add_img(
897
861
output ._img_data = new_img_data
898
862
return output
899
863
900
- # TODO: implement rmv_img()
901
- def rmv_img (
902
- self ,
903
- sample_id : Union [str , bool , None ] = None ,
904
- image_id : Union [str , bool , None ] = None ,
905
- in_place : bool = False
864
+ def remove_img (
865
+ self , sample_id : Union [str , bool , None ] = None , image_id : Union [str , bool , None ] = None , in_place : bool = False
906
866
) -> "SpatialExperiment" :
907
867
"""Remove an image entry.
908
868
@@ -921,17 +881,24 @@ def rmv_img(
921
881
Whether to modify the ``SpatialExperiment`` in place.
922
882
Defaults to False.
923
883
"""
924
- raise NotImplementedError ()
884
+ _validate_id (sample_id )
885
+ _validate_id (image_id )
886
+
887
+ indices = get_img_idx (img_data = self .img_data , sample_id = sample_id , image_id = image_id )
888
+
889
+ new_img_data = self ._img_data .remove_rows (indices )
890
+
891
+ output = self ._define_output (in_place = in_place )
892
+ output ._img_data = new_img_data
893
+ return output
925
894
926
895
def img_source (
927
896
self ,
928
897
sample_id : Union [str , bool , None ] = None ,
929
898
image_id : Union [str , bool , None ] = None ,
930
899
path = False ,
931
900
):
932
- raise NotImplementedError (
933
- "This function is irrelevant because it is for `RemoteSpatialImages`"
934
- )
901
+ raise NotImplementedError ("This function is irrelevant because it is for `RemoteSpatialImages`" )
935
902
936
903
def img_raster (self , sample_id = None , image_id = None ):
937
904
# NOTE: this function seems redundant, might be an artifact of the different subclasses of SpatialImage in the R implementation? just call `get_img()` for now
0 commit comments