diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 17aa5dd1..8417abfb 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -61,4 +61,4 @@ repos: hooks: - id: pydocstyle args: # google style + __init__, see http://www.pydocstyle.org/en/stable/error_codes.html - - --ignore=D100,D101,D102,D103,D106,D107,D203,D204,D213,D215,D400,D401,D403,D404,D406,D407,D408,D409,D413 + - --ignore=D100,D101,D102,D103,D106,D107,D203,D204,D213,D215,D400,D401,D403,D404,D406,D407,D408,D409,D413,D418 diff --git a/lnschema_core/_core.py b/lnschema_core/_core.py index 1ecb0857..60951e2c 100644 --- a/lnschema_core/_core.py +++ b/lnschema_core/_core.py @@ -1,7 +1,9 @@ from datetime import datetime as datetime from pathlib import Path -from typing import List, Optional, Union +from typing import Any, List, Optional, Union, overload # noqa +import anndata as ad +import pandas as pd import sqlalchemy as sa from cloudpathlib import CloudPath from pydantic.fields import PrivateAttr @@ -182,6 +184,81 @@ def path(self) -> Union[Path, CloudPath]: """Path on storage.""" return filepath_from_dobject(self) + def load(self, stream: bool = False): + """Load data object. + + Returns object associated with the stored `dobject`. + + Populates `RunIn` when called from a notebook. + + Guide: https://lamin.ai/docs/db/guide/select-load + """ + from lamindb._load import load as lnload + + return lnload(dobject=self, stream=stream) + + @overload + def __init__( + self, + data: Union[Path, str, pd.DataFrame, ad.AnnData] = None, + *, + name: Optional[str] = None, + features_ref: Any = None, + source: Optional["Run"] = None, + id: Optional[str] = None, + format: Optional[str] = None, + ): + """Create a DObject record from data.""" + ... + + @overload + def __init__( + self, + id: Optional[str] = None, + name: Optional[str] = None, + source: Optional["Run"] = None, + suffix: Optional[str] = None, + hash: Optional[str] = None, + run_id: Optional[str] = None, + storage_id: Optional[str] = None, + features: List["Features"] = [], + targets: List["Run"] = [], + ): + """Create a DObject record from fields.""" + ... + + def __init__( # type: ignore + self, + data: Union[Path, str, pd.DataFrame, ad.AnnData] = None, + *, + features_ref: Any = None, + source: Optional["Run"] = None, + format: Optional[str] = None, + id: Optional[str] = None, + name: Optional[str] = None, + suffix: Optional[str] = None, + hash: Optional[str] = None, + run_id: Optional[str] = None, + storage_id: Optional[str] = None, + features: List["Features"] = [], + targets: List["Run"] = [], + ): + kwargs = locals() + if data is not None: + from lamindb._record import create_dobject_from_data + + record = create_dobject_from_data( + data=data, + name=name, + features_ref=features_ref, + source=source, + id=id, + format=format, + ) + kwargs = record.dict() + + super().__init__(**kwargs) + class Run(SQLModel, table=True): # type: ignore """Code runs that transform data. diff --git a/pyproject.toml b/pyproject.toml index 977f7124..85116bfa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,6 +10,7 @@ dynamic = ["version", "description"] dependencies = [ "nbproject", "sqlmodel", + "anndata" ] [project.urls]