lamindb 1.2a2__py3-none-any.whl → 1.3.1__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.
@@ -1,25 +1,20 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  from pathlib import PurePosixPath
4
- from typing import TYPE_CHECKING, TypeAlias, TypeVar
4
+ from typing import TYPE_CHECKING, TypeAlias
5
5
 
6
6
  from anndata import AnnData
7
7
  from pandas import DataFrame
8
8
 
9
+ from lamindb.core._compat import (
10
+ with_package_obj,
11
+ )
12
+ from lamindb.core.types import ScverseDataStructures
13
+
9
14
  if TYPE_CHECKING:
10
15
  from lamindb_setup.core.types import UPathStr
11
16
 
12
- SpatialData = TypeVar("SpatialData")
13
- MuData = TypeVar("MuData")
14
-
15
- SupportedDataTypes: TypeAlias = AnnData | DataFrame | MuData | SpatialData
16
-
17
-
18
- def is_package_installed(package_name):
19
- import importlib.util
20
-
21
- spec = importlib.util.find_spec(package_name)
22
- return spec is not None
17
+ SupportedDataTypes: TypeAlias = DataFrame | ScverseDataStructures
23
18
 
24
19
 
25
20
  def infer_suffix(dmem: SupportedDataTypes, format: str | None = None):
@@ -38,25 +33,34 @@ def infer_suffix(dmem: SupportedDataTypes, format: str | None = None):
38
33
  if isinstance(dmem, DataFrame):
39
34
  return ".parquet"
40
35
 
41
- if is_package_installed("mudata"):
42
- from mudata import MuData
43
-
44
- if isinstance(dmem, MuData):
45
- return ".h5mu"
46
-
47
- if is_package_installed("spatialdata"):
48
- from spatialdata import SpatialData
49
-
50
- if isinstance(dmem, SpatialData):
51
- if format is not None:
52
- if format not in {"spatialdata.zarr"}:
53
- raise ValueError(
54
- "Error when specifying SpatialData storage format, it should be"
55
- f" 'zarr', 'spatialdata.zarr', not '{format}'. Check 'format'"
56
- " or the suffix of 'key'."
57
- )
58
- return "." + format
59
- return ".zarr"
36
+ if with_package_obj(
37
+ dmem,
38
+ "MuData",
39
+ "mudata",
40
+ lambda obj: True, # Just checking type, not calling any method
41
+ )[0]:
42
+ return ".h5mu"
43
+
44
+ has_spatialdata, spatialdata_suffix = with_package_obj(
45
+ dmem,
46
+ "SpatialData",
47
+ "spatialdata",
48
+ lambda obj: (
49
+ format
50
+ if format is not None and format in {"spatialdata.zarr", "zarr"}
51
+ else ".zarr"
52
+ if format is None
53
+ else (_ for _ in ()).throw(
54
+ ValueError(
55
+ "Error when specifying SpatialData storage format, it should be"
56
+ f" 'zarr', 'spatialdata.zarr', not '{format}'. Check 'format'"
57
+ " or the suffix of 'key'."
58
+ )
59
+ )
60
+ ),
61
+ )
62
+ if has_spatialdata:
63
+ return spatialdata_suffix
60
64
  else:
61
65
  raise NotImplementedError
62
66
 
@@ -78,18 +82,15 @@ def write_to_disk(dmem: SupportedDataTypes, filepath: UPathStr) -> None:
78
82
  dmem.to_parquet(filepath)
79
83
  return
80
84
 
81
- if is_package_installed("mudata"):
82
- from mudata import MuData
83
-
84
- if isinstance(dmem, MuData):
85
- dmem.write(filepath)
86
- return
85
+ if with_package_obj(dmem, "MuData", "mudata", lambda obj: obj.write(filepath))[0]:
86
+ return
87
87
 
88
- if is_package_installed("spatialdata"):
89
- from spatialdata import SpatialData
88
+ if with_package_obj(
89
+ dmem,
90
+ "SpatialData",
91
+ "spatialdata",
92
+ lambda obj: obj.write(filepath, overwrite=True),
93
+ )[0]:
94
+ return
90
95
 
91
- if isinstance(dmem, SpatialData):
92
- dmem.write(filepath, overwrite=True)
93
- return
94
- else:
95
- raise NotImplementedError
96
+ raise NotImplementedError
lamindb/core/types.py CHANGED
@@ -1,9 +1,19 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING, TypeVar
4
+
5
+ from anndata import AnnData
1
6
  from lamindb_setup.core.types import UPathStr
2
7
 
3
8
  from lamindb.base.types import (
4
- FeatureDtype,
9
+ Dtype,
5
10
  FieldAttr,
6
11
  ListLike,
7
12
  StrField,
8
13
  TransformType,
9
14
  )
15
+
16
+ MuData = TypeVar("MuData")
17
+ SpatialData = TypeVar("SpatialData")
18
+
19
+ ScverseDataStructures = AnnData | MuData | SpatialData