lamindb 0.76.0__py3-none-any.whl → 0.76.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.
lamindb/__init__.py CHANGED
@@ -21,9 +21,8 @@ Key functionality:
21
21
  .. autosummary::
22
22
  :toctree: .
23
23
 
24
+ context
24
25
  connect
25
- track
26
- finish
27
26
  Curate
28
27
  view
29
28
  save
@@ -42,7 +41,7 @@ Modules & settings:
42
41
  """
43
42
 
44
43
  # denote a release candidate for 0.1.0 with 0.1rc1, 0.1a1, 0.1b1, etc.
45
- __version__ = "0.76.0"
44
+ __version__ = "0.76.1"
46
45
 
47
46
  import os as _os
48
47
 
@@ -93,10 +92,9 @@ if _check_instance_setup(from_lamindb=True):
93
92
  integrations,
94
93
  )
95
94
  from ._curate import Curate
96
- from ._finish import finish
97
95
  from ._save import save
98
96
  from ._view import view
99
- from .core._run_context import run_context as _run_context
97
+ from .core._context import context
100
98
  from .core._settings import settings
101
99
 
102
100
  # schema modules
@@ -107,8 +105,8 @@ if _check_instance_setup(from_lamindb=True):
107
105
 
108
106
  _reload_schema_modules(_lamindb_setup.settings.instance)
109
107
 
110
- track = _run_context._track
108
+ track = context.track # backward compat
109
+ finish = context.finish # backward compat
111
110
  settings.__doc__ = """Global :class:`~lamindb.core.Settings`."""
111
+ context.__doc__ = """Global :class:`~lamindb.core.Context`."""
112
112
  from django.db.models import Q
113
-
114
- Annotate = Curate # backward compat
lamindb/_artifact.py CHANGED
@@ -107,7 +107,7 @@ def process_pathlike(
107
107
  new_root = list(filepath.parents)[-1]
108
108
  # do not register remote storage locations on hub if the current instance
109
109
  # is not managed on the hub
110
- storage_settings = init_storage(
110
+ storage_settings, _ = init_storage(
111
111
  new_root, prevent_register_hub=not setup_settings.instance.is_on_hub
112
112
  )
113
113
  storage_record = register_storage_in_instance(storage_settings)
lamindb/_curate.py CHANGED
@@ -313,11 +313,11 @@ class DataFrameCurator:
313
313
 
314
314
  def clean_up_failed_runs(self):
315
315
  """Clean up previous failed runs that don't save any outputs."""
316
- from lamindb.core._run_context import run_context
316
+ from lamindb.core._context import context
317
317
 
318
- if run_context.transform is not None:
319
- Run.filter(transform=run_context.transform, output_artifacts=None).exclude(
320
- uid=run_context.run.uid
318
+ if context.run is not None:
319
+ Run.filter(transform=context.run.transform, output_artifacts=None).exclude(
320
+ uid=context.run.uid
321
321
  ).delete()
322
322
 
323
323
 
lamindb/_finish.py CHANGED
@@ -9,9 +9,6 @@ from typing import TYPE_CHECKING
9
9
  import lamindb_setup as ln_setup
10
10
  from lamin_utils import logger
11
11
  from lamindb_setup.core.hashing import hash_file
12
- from lnschema_core.types import TransformType
13
-
14
- from .core._run_context import is_run_from_ipython, run_context
15
12
 
16
13
  if TYPE_CHECKING:
17
14
  from pathlib import Path
@@ -21,51 +18,7 @@ if TYPE_CHECKING:
21
18
  from ._query_set import QuerySet
22
19
 
23
20
 
24
- class TrackNotCalled(SystemExit):
25
- pass
26
-
27
-
28
- class NotebookNotSaved(SystemExit):
29
- pass
30
-
31
-
32
- def get_seconds_since_modified(filepath) -> float:
33
- return datetime.now().timestamp() - filepath.stat().st_mtime
34
-
35
-
36
- def finish() -> None:
37
- """Mark a tracked run as finished.
38
-
39
- Saves source code and, for notebooks, a run report to your default storage location.
40
- """
41
- if run_context.run is None:
42
- raise TrackNotCalled("Please run `ln.track()` before `ln.finish()`")
43
- if run_context.path is None:
44
- if run_context.transform.type in {"script", "notebook"}:
45
- raise ValueError(
46
- f"Transform type is not allowed to be 'script' or 'notebook' but is {run_context.transform.type}."
47
- )
48
- run_context.run.finished_at = datetime.now(timezone.utc)
49
- run_context.run.save()
50
- # nothing else to do
51
- return None
52
- if is_run_from_ipython: # notebooks
53
- if (
54
- get_seconds_since_modified(run_context.path) > 3
55
- and os.getenv("LAMIN_TESTING") is None
56
- ):
57
- raise NotebookNotSaved(
58
- "Please save the notebook in your editor right before running `ln.finish()`"
59
- )
60
- save_run_context_core(
61
- run=run_context.run,
62
- transform=run_context.transform,
63
- filepath=run_context.path,
64
- finished_at=True,
65
- )
66
-
67
-
68
- def save_run_context_core(
21
+ def save_context_core(
69
22
  *,
70
23
  run: Run,
71
24
  transform: Transform,
@@ -76,6 +29,8 @@ def save_run_context_core(
76
29
  ) -> str | None:
77
30
  import lamindb as ln
78
31
 
32
+ from .core._context import context, is_run_from_ipython
33
+
79
34
  ln.settings.verbosity = "success"
80
35
 
81
36
  # for scripts, things are easy
@@ -182,7 +137,9 @@ def save_run_context_core(
182
137
  f"replaced transform._source_code_artifact: {transform._source_code_artifact}"
183
138
  )
184
139
  else:
185
- logger.warning("Please re-run `ln.track()` to make a new version")
140
+ logger.warning(
141
+ "Please re-run `ln.context.track()` to make a new version"
142
+ )
186
143
  return "rerun-the-notebook"
187
144
  else:
188
145
  logger.important("source code is already saved")
@@ -282,7 +239,7 @@ def save_run_context_core(
282
239
  logger.important(
283
240
  f"if you want to update your {thing} without re-running it, use `lamin save {name}`"
284
241
  )
285
- # because run & transform changed, update the global run_context
286
- run_context.run = run
287
- run_context.transform = transform
242
+ # because run & transform changed, update the global context
243
+ context._run = run
244
+ context._transform = transform
288
245
  return None
lamindb/_query_manager.py CHANGED
@@ -40,11 +40,11 @@ class QueryManager(models.Manager):
40
40
  self.source_field_name == "collection"
41
41
  and self.target_field_name == "artifact"
42
42
  ):
43
+ from lamindb.core._context import context
43
44
  from lamindb.core._data import WARNING_RUN_TRANSFORM, _track_run_input
44
- from lamindb.core._run_context import run_context
45
45
 
46
46
  if (
47
- run_context.run is None
47
+ context.run is None
48
48
  and not settings.creation.artifact_silence_missing_run_warning
49
49
  ):
50
50
  logger.warning(WARNING_RUN_TRANSFORM)
lamindb/_query_set.py CHANGED
@@ -17,14 +17,12 @@ from lnschema_core.models import (
17
17
  Transform,
18
18
  )
19
19
 
20
+ from lamindb.core.exceptions import DoesNotExist
21
+
20
22
  if TYPE_CHECKING:
21
23
  from lnschema_core.types import ListLike, StrField
22
24
 
23
25
 
24
- class NoResultFound(Exception):
25
- pass
26
-
27
-
28
26
  class MultipleResultsFound(Exception):
29
27
  pass
30
28
 
@@ -59,7 +57,7 @@ def get_keys_from_df(data: list, registry: Record) -> list[str]:
59
57
 
60
58
  def one_helper(self):
61
59
  if len(self) == 0:
62
- raise NoResultFound
60
+ raise DoesNotExist
63
61
  elif len(self) > 1:
64
62
  raise MultipleResultsFound(self)
65
63
  else:
lamindb/_record.py CHANGED
@@ -123,13 +123,17 @@ def filter(cls, **expressions) -> QuerySet:
123
123
 
124
124
  @classmethod # type:ignore
125
125
  @doc_args(Record.get.__doc__)
126
- def get(cls, idlike: int | str) -> Record:
126
+ def get(
127
+ cls,
128
+ idlike: int | str | None = None,
129
+ **expressions,
130
+ ) -> Record:
127
131
  """{}""" # noqa: D415
128
132
  from lamindb._filter import filter
129
133
 
130
134
  if isinstance(idlike, int):
131
135
  return filter(cls, id=idlike).one()
132
- else:
136
+ elif isinstance(idlike, str):
133
137
  qs = filter(cls, uid__startswith=idlike)
134
138
  if issubclass(cls, IsVersioned):
135
139
  if len(idlike) <= cls._len_stem_uid:
@@ -138,12 +142,19 @@ def get(cls, idlike: int | str) -> Record:
138
142
  return qs.one()
139
143
  else:
140
144
  return qs.one()
145
+ else:
146
+ assert idlike is None # noqa: S101
147
+ # below behaves exactly like `.one()`
148
+ return cls.objects.get(**expressions)
141
149
 
142
150
 
143
151
  @classmethod # type:ignore
144
152
  @doc_args(Record.df.__doc__)
145
153
  def df(
146
- cls, include: str | list[str] | None = None, join: str = "inner"
154
+ cls,
155
+ include: str | list[str] | None = None,
156
+ join: str = "inner",
157
+ limit: int = 100,
147
158
  ) -> pd.DataFrame:
148
159
  """{}""" # noqa: D415
149
160
  from lamindb._filter import filter
@@ -151,7 +162,7 @@ def df(
151
162
  query_set = filter(cls)
152
163
  if hasattr(cls, "updated_at"):
153
164
  query_set = query_set.order_by("-updated_at")
154
- return query_set.df(include=include, join=join)
165
+ return query_set[:limit].df(include=include, join=join)
155
166
 
156
167
 
157
168
  # from_values doesn't apply for QuerySet or Manager
@@ -473,8 +484,8 @@ def transfer_to_default_db(
473
484
  return record_on_default
474
485
  if not mute:
475
486
  logger.hint(f"saving from instance {db} to default instance: {record}")
487
+ from lamindb.core._context import context
476
488
  from lamindb.core._data import WARNING_RUN_TRANSFORM
477
- from lamindb.core._run_context import run_context
478
489
 
479
490
  if hasattr(record, "created_by_id"):
480
491
  # this line is needed to point created_by to default db
@@ -482,16 +493,16 @@ def transfer_to_default_db(
482
493
  record.created_by_id = ln_setup.settings.user.id
483
494
  if hasattr(record, "run_id"):
484
495
  record.run = None
485
- if run_context.run is not None:
486
- record.run_id = run_context.run.id
496
+ if context.run is not None:
497
+ record.run_id = context.run.id
487
498
  else:
488
499
  if not settings.creation.artifact_silence_missing_run_warning:
489
500
  logger.warning(WARNING_RUN_TRANSFORM)
490
501
  record.run_id = None
491
502
  if hasattr(record, "transform_id") and record._meta.model_name != "run":
492
503
  record.transform = None
493
- if run_context.transform is not None:
494
- record.transform_id = run_context.transform.id
504
+ if context.run is not None:
505
+ record.transform_id = context.run.transform_id
495
506
  else:
496
507
  record.transform_id = None
497
508
  # transfer other foreign key fields
lamindb/core/__init__.py CHANGED
@@ -35,14 +35,20 @@ Curators:
35
35
  MuDataCurator
36
36
  CurateLookup
37
37
 
38
- Other:
38
+ Settings & context:
39
39
 
40
40
  .. autosummary::
41
41
  :toctree: .
42
42
 
43
43
  Settings
44
+ Context
45
+
46
+ Data loaders:
47
+
48
+ .. autosummary::
49
+ :toctree: .
50
+
44
51
  MappedCollection
45
- run_context
46
52
 
47
53
  Modules:
48
54
 
@@ -84,6 +90,6 @@ from lamindb.core._feature_manager import FeatureManager, ParamManager
84
90
  from lamindb.core._label_manager import LabelManager
85
91
 
86
92
  from . import _data, datasets, exceptions, fields, subsettings, types
93
+ from ._context import Context
87
94
  from ._mapped_collection import MappedCollection
88
- from ._run_context import run_context
89
95
  from ._settings import Settings