Skip to content

Commit

Permalink
🎨 Refactor ln.File (#163)
Browse files Browse the repository at this point in the history
* 🎨 Refactor with storage

* 💚 Fix
  • Loading branch information
falexwolf authored Apr 24, 2023
1 parent 3a577ab commit 2df6522
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 40 deletions.
4 changes: 2 additions & 2 deletions docs/guide/1-data-validation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"outputs": [],
"source": [
"from lnschema_core._core import SQLModel\n",
"from lnschema_core.dev.type import usage as event_type\n",
"from lnschema_core.dev.type import Usage as UsageType\n",
"from sqlmodel import Field"
]
},
Expand All @@ -118,7 +118,7 @@
"source": [
"class Usage(SQLModel, table=True): # type: ignore\n",
" id: str = Field(default=None, primary_key=True)\n",
" type: event_type = Field(nullable=False, index=True)"
" type: UsageType = Field(nullable=False, index=True)"
]
},
{
Expand Down
31 changes: 3 additions & 28 deletions lnschema_core/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
import pandas as pd
import sqlalchemy as sa
from lamin_logger import logger
from lndb.dev.upath import UPath
from nbproject._is_run_from_ipython import is_run_from_ipython
from pydantic.fields import PrivateAttr
from sqlmodel import Field, ForeignKeyConstraint, Relationship
from upath import UPath

from . import _name as schema_name
from ._link import FileFeatures, FolderFile, ProjectFolder, RunIn # noqa
from ._timestamps import CreatedAt, UpdatedAt
from ._users import CreatedBy
from .dev import id as idg
from .dev.sqlmodel import schema_sqlmodel
from .dev.type import TransformType
from .types import DataLike, PathLike, TransformType

PathLike = TypeVar("PathLike", str, Path, UPath)
DataLike = TypeVar("DataLike", ad.AnnData, pd.DataFrame)
SQLModel, prefix, schema_arg = schema_sqlmodel(schema_name)


Expand Down Expand Up @@ -480,7 +478,7 @@ def path(self) -> Union[Path, UPath]:
return filepath_from_file_or_folder(self)

# likely needs an arg `key`
def replace(self, data: Union[Path, str, pd.DataFrame, ad.AnnData], run: Optional[Run] = None, format: Optional[str] = None):
def replace(self, data: Union[PathLike, DataLike], run: Optional[Run] = None, format: Optional[str] = None) -> None:
"""Replace data object."""
from lamindb._file import get_file_kwargs_from_data

Expand Down Expand Up @@ -528,29 +526,6 @@ def replace(self, data: Union[Path, str, pd.DataFrame, ad.AnnData], run: Optiona
self._memory_rep = privates["memory_rep"]
self._to_store = not privates["check_path_in_storage"] # no need to upload if new file is already in storage

def stage(self, is_run_input: bool = False):
"""Download from storage if newer than in the cache.
Returns a path to a locally cached on-disk object (say, a
`.jpg` file).
"""
from lamindb._load import stage as lnstage

return lnstage(file=self, is_run_input=is_run_input)

def load(self, stream: bool = False, is_run_input: bool = False):
"""Load from storage (stage on local disk or load to memory).
Returns in-memory representation if configured (say, an `AnnData` object
for an `h5ad` file).
Otherwise, returns a path to a locally cached on-disk object (say, a
`.jpg` file).
"""
from lamindb._load import load as lnload

return lnload(file=self, stream=stream, is_run_input=is_run_input)

@property
def __name__(cls) -> str:
return "File"
Expand Down
11 changes: 10 additions & 1 deletion lnschema_core/dev/_type.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
from enum import Enum
from pathlib import Path
from typing import TypeVar

import anndata as ad
import pandas as pd
from lndb.dev.upath import UPath

class usage(str, Enum):
PathLike = TypeVar("PathLike", str, Path, UPath)
DataLike = TypeVar("DataLike", ad.AnnData, pd.DataFrame)


class Usage(str, Enum):
"""Data access types."""

ingest = "ingest"
Expand Down
11 changes: 2 additions & 9 deletions lnschema_core/dev/type.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
"""Custom types.
.. autosummary::
:toctree: .
usage
TransformType
"""
from ._type import TransformType, usage # noqa
# deprecated, use lnschema_core.types instead
from ._type import TransformType, Usage # noqa
10 changes: 10 additions & 0 deletions lnschema_core/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Types.
.. autosummary::
:toctree: .
PathLike
DataLike
TransformType
"""
from .dev._type import DataLike, PathLike, TransformType # noqa

0 comments on commit 2df6522

Please sign in to comment.