anndata 0.12.9__py3-none-any.whl → 0.12.10__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
anndata/_core/anndata.py CHANGED
@@ -597,8 +597,38 @@ class AnnData(metaclass=utils.DeprecationMixinMeta): # noqa: PLW1641
597
597
  # else:
598
598
  # return X
599
599
 
600
+ def _handle_view_X_cow(self, value: XDataType | None):
601
+ if self._is_view:
602
+ if settings.copy_on_write_X:
603
+ msg = "Setting element `.X` of view, initializing view as actual."
604
+ warnings.warn(msg, ImplicitModificationWarning, stacklevel=2)
605
+ new = self._mutated_copy(X=value)
606
+ self._init_as_actual(new)
607
+ return True
608
+ msg = "Setting element `.X` of view of `AnnData` object will obey copy-on-write semantics in the next minor release. "
609
+ "In other words, this subset of your original `AnnData` will be copied-in-place and initialized with the value passed into this setter. "
610
+ "Set `anndata.settings.copy_on_write_X = True` to begin opting in to this behavior."
611
+ warnings.warn(msg, FutureWarning, stacklevel=2)
612
+ return False
613
+
600
614
  @X.setter
601
615
  def X(self, value: XDataType | None): # noqa: PLR0912
616
+ value = (
617
+ coerce_array(value, name="X", allow_array_like=True)
618
+ if value is not None
619
+ else None
620
+ )
621
+ can_set_direct_if_not_none = value is None or (
622
+ np.isscalar(value)
623
+ or (hasattr(value, "shape") and (self.shape == value.shape))
624
+ or (self.n_vars == 1 and self.n_obs == len(value))
625
+ or (self.n_obs == 1 and self.n_vars == len(value))
626
+ )
627
+ if not can_set_direct_if_not_none:
628
+ msg = f"Data matrix has wrong shape {value.shape}, need to be {self.shape}."
629
+ raise ValueError(msg)
630
+ if self._handle_view_X_cow(value):
631
+ return None
602
632
  if value is None:
603
633
  if self.isbacked:
604
634
  msg = "Cannot currently remove data matrix from backed object."
@@ -607,7 +637,6 @@ class AnnData(metaclass=utils.DeprecationMixinMeta): # noqa: PLW1641
607
637
  self._init_as_actual(self.copy())
608
638
  self._X = None
609
639
  return
610
- value = coerce_array(value, name="X", allow_array_like=True)
611
640
 
612
641
  # If indices are both arrays, we need to modify them
613
642
  # so we don’t set values like coordinates
@@ -620,67 +649,51 @@ class AnnData(metaclass=utils.DeprecationMixinMeta): # noqa: PLW1641
620
649
  oidx, vidx = np.ix_(self._oidx, self._vidx)
621
650
  else:
622
651
  oidx, vidx = self._oidx, self._vidx
623
- if (
624
- np.isscalar(value)
625
- or (hasattr(value, "shape") and (self.shape == value.shape))
626
- or (self.n_vars == 1 and self.n_obs == len(value))
627
- or (self.n_obs == 1 and self.n_vars == len(value))
628
- ):
629
- if not np.isscalar(value):
630
- if self.is_view and any(
631
- isinstance(idx, np.ndarray)
632
- and len(np.unique(idx)) != len(idx.ravel())
633
- for idx in [oidx, vidx]
634
- ):
635
- msg = (
636
- "You are attempting to set `X` to a matrix on a view which has non-unique indices. "
637
- "The resulting `adata.X` will likely not equal the value to which you set it. "
638
- "To avoid this potential issue, please make a copy of the data first. "
639
- "In the future, this operation will throw an error."
640
- )
641
- warnings.warn(msg, FutureWarning, stacklevel=1)
642
- if self.shape != value.shape:
643
- # For assigning vector of values to 2d array or matrix
644
- # Not necessary for row of 2d array
645
- value = value.reshape(self.shape)
646
- if self.isbacked:
647
- if self.is_view:
648
- X = self.file["X"]
649
- if isinstance(X, h5py.Group):
650
- X = sparse_dataset(X)
651
- X[oidx, vidx] = value
652
- else:
653
- self._set_backed("X", value)
654
- elif self.is_view:
655
- if sparse.issparse(self._adata_ref._X) and isinstance(
656
- value, np.ndarray
657
- ):
658
- if isinstance(self._adata_ref.X, CSArray):
659
- memory_class = sparse.coo_array
660
- else:
661
- memory_class = sparse.coo_matrix
662
- value = memory_class(value)
663
- elif sparse.issparse(value) and isinstance(
664
- self._adata_ref._X, np.ndarray
665
- ):
666
- warnings.warn(
667
- "Trying to set a dense array with a sparse array on a view."
668
- "Densifying the sparse array."
669
- "This may incur excessive memory usage",
670
- stacklevel=2,
671
- )
672
- value = value.toarray()
673
- warnings.warn(
674
- "Modifying `X` on a view results in data being overridden",
675
- ImplicitModificationWarning,
676
- stacklevel=2,
652
+ if not np.isscalar(value):
653
+ if self.is_view and any(
654
+ isinstance(idx, np.ndarray) and len(np.unique(idx)) != len(idx.ravel())
655
+ for idx in [oidx, vidx]
656
+ ):
657
+ msg = (
658
+ "You are attempting to set `X` to a matrix on a view which has non-unique indices. "
659
+ "The resulting `adata.X` will likely not equal the value to which you set it. "
660
+ "To avoid this potential issue, please make a copy of the data first. "
661
+ "In the future, this operation will throw an error."
677
662
  )
678
- self._adata_ref._X[oidx, vidx] = value
663
+ warnings.warn(msg, FutureWarning, stacklevel=2)
664
+ if self.shape != value.shape:
665
+ # For assigning vector of values to 2d array or matrix
666
+ # Not necessary for row of 2d array
667
+ value = value.reshape(self.shape)
668
+ if self.isbacked:
669
+ if self.is_view:
670
+ X = self.file["X"]
671
+ if isinstance(X, h5py.Group):
672
+ msg = "Cannot write to views of sparse backed files"
673
+ raise NotImplementedError(msg)
674
+ X[oidx, vidx] = value
679
675
  else:
680
- self._X = value
676
+ self._set_backed("X", value)
677
+ elif self.is_view:
678
+ if sparse.issparse(self._adata_ref._X) and isinstance(value, np.ndarray):
679
+ if isinstance(self._adata_ref.X, CSArray):
680
+ memory_class = sparse.coo_array
681
+ else:
682
+ memory_class = sparse.coo_matrix
683
+ value = memory_class(value)
684
+ elif sparse.issparse(value) and isinstance(self._adata_ref._X, np.ndarray):
685
+ msg = (
686
+ "Trying to set a dense array with a sparse array on a view. "
687
+ "Densifying the sparse array. "
688
+ "This may incur excessive memory usage"
689
+ )
690
+ warnings.warn(msg, UserWarning, stacklevel=2)
691
+ value = value.toarray()
692
+ msg = "Modifying `X` on a view results in data being overridden"
693
+ warnings.warn(msg, ImplicitModificationWarning, stacklevel=2)
694
+ self._adata_ref._X[oidx, vidx] = value
681
695
  else:
682
- msg = f"Data matrix has wrong shape {value.shape}, need to be {self.shape}."
683
- raise ValueError(msg)
696
+ self._X = value
684
697
 
685
698
  @X.deleter
686
699
  def X(self):
@@ -1492,8 +1505,7 @@ class AnnData(metaclass=utils.DeprecationMixinMeta): # noqa: PLW1641
1492
1505
  return self._mutated_copy(
1493
1506
  X=_subset(self._adata_ref.X, (self._oidx, self._vidx)).copy()
1494
1507
  )
1495
- else:
1496
- return self._mutated_copy()
1508
+ return self._mutated_copy()
1497
1509
  else:
1498
1510
  from ..io import read_h5ad, write_h5ad
1499
1511
 
anndata/_settings.py CHANGED
@@ -520,5 +520,18 @@ settings.register(
520
520
  )
521
521
 
522
522
 
523
+ settings.register(
524
+ "copy_on_write_X",
525
+ default_value=False,
526
+ description=(
527
+ "Whether to copy-on-write X. "
528
+ "Currently `my_adata_view[subset].X = value` will write back to the original AnnData object at the `subset` location. "
529
+ "`X` is the only element where this behavior is implemented though."
530
+ ),
531
+ validate=validate_bool,
532
+ get_from_env=check_and_get_bool,
533
+ )
534
+
535
+
523
536
  ##################################################################################
524
537
  ##################################################################################
anndata/_settings.pyi CHANGED
@@ -42,6 +42,7 @@ class _AnnDataSettingsManager(SettingsManager):
42
42
  remove_unused_categories: bool = True
43
43
  check_uniqueness: bool = True
44
44
  allow_write_nullable_strings: bool = False
45
+ copy_on_write_X: bool = False
45
46
  zarr_write_format: Literal[2, 3] = 2
46
47
  use_sparse_array_on_read: bool = False
47
48
  min_rows_for_chunked_h5_copy: int = 1000
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: anndata
3
- Version: 0.12.9
3
+ Version: 0.12.10
4
4
  Summary: Annotated data.
5
5
  Project-URL: Documentation, https://anndata.readthedocs.io/
6
6
  Project-URL: Source, https://github.com/scverse/anndata
@@ -1,6 +1,6 @@
1
1
  anndata/__init__.py,sha256=daAzY8GGouJxCe30Lcr2pl9Jwo2dcGXHPi7WxnHpuOE,1710
2
- anndata/_settings.py,sha256=CXjpjtvgEEp6P5cWH5vLTC1epJiLcVeByUeJC2xLfUc,17889
3
- anndata/_settings.pyi,sha256=8RDrZc1XAzMqtM_Z5oI7hja0-L9edMxY3N8w_xMR6Ms,1707
2
+ anndata/_settings.py,sha256=_lAfCF-oCa27eRdfl9VaHB5zkf7pAYI-tJoANwcRdUI,18294
3
+ anndata/_settings.pyi,sha256=MU2MRCkn8dFb6czbzayKrRKkwMPyeLmEGoWErfPYg7s,1741
4
4
  anndata/_types.py,sha256=RbSN6dc46J2qDTZ9y9JXrzqfwhoCX5zL1ZPH7wTQyrM,5415
5
5
  anndata/_warnings.py,sha256=iFXa9EzPyuPbzRAzoG04oTXAyjnXhQa5zxAMZdsGLwM,702
6
6
  anndata/abc.py,sha256=jG64k59ZZ9Hfn-QWt_btZLuF7eGv_YNYwH91WdbR240,1645
@@ -13,7 +13,7 @@ anndata/_core/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
13
13
  anndata/_core/access.py,sha256=pts7fGUKgGZANSsu_qAA7L10qHM-jT1zIehbl3441OY,873
14
14
  anndata/_core/aligned_df.py,sha256=bM9kkEFURRLeUOUMk90WxVnRC-ZsXGEDx36kDj5gC9I,4278
15
15
  anndata/_core/aligned_mapping.py,sha256=BYU1jslMWIhtFTtUMaXY8ZCyt0J4_ZsJTmj6J2yAXTQ,14257
16
- anndata/_core/anndata.py,sha256=s-ExKqeQXuvin9dk0vKa1GLx6tRvrAX5HlvmqRR9uRw,79455
16
+ anndata/_core/anndata.py,sha256=nE7We_X-vgTDbnFYT2gzDFE7Xgh-27yu4BgDDh-f3zw,80296
17
17
  anndata/_core/extensions.py,sha256=9Rsho6qnr3PJHULrYGiZHCBinBZYJK6zyf3cFsl_gBY,10425
18
18
  anndata/_core/file_backing.py,sha256=6DhBfLQPDFDpoe6wSgnOFtpC4Hnbh-UgOPbqvYDxm8g,5603
19
19
  anndata/_core/index.py,sha256=dz2jhrklxsNIDN-q0WhiXhxwtOreK-T8Iate-MGXpH0,13350
@@ -51,7 +51,7 @@ testing/anndata/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
51
51
  testing/anndata/_doctest.py,sha256=Qew0N0zLLNiPKN1CLunqY5cTinFLaEhY5GagiYfm6KI,344
52
52
  testing/anndata/_pytest.py,sha256=C_R-N2x9NHKZ66YLkvMLWkXQG1WiouOkBnLQpYx_62Q,3994
53
53
  testing/anndata/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
54
- anndata-0.12.9.dist-info/METADATA,sha256=UhIQ5YYycIU5YWdJoIKXWHuXIAevp6lI0oqndfwwgE8,9940
55
- anndata-0.12.9.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
56
- anndata-0.12.9.dist-info/licenses/LICENSE,sha256=VcrXoEVMhtNuvMvKYGP-I5lMT8qZ_6dFf22fsL180qA,1575
57
- anndata-0.12.9.dist-info/RECORD,,
54
+ anndata-0.12.10.dist-info/METADATA,sha256=0darwmDcFICDT542oSN-jQUcaN25TkFVeY0gmS_yiDc,9941
55
+ anndata-0.12.10.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
56
+ anndata-0.12.10.dist-info/licenses/LICENSE,sha256=VcrXoEVMhtNuvMvKYGP-I5lMT8qZ_6dFf22fsL180qA,1575
57
+ anndata-0.12.10.dist-info/RECORD,,