-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[0.1.0] Metadata and Inspector (#67)
- Intro `Metadata`, without implement yet - Intro `Inspector` for generating metadata from `DataLoader` - Implement a `GeneratorConnector` for connecting `ProcessedData` - WIP: More logic in `Synthesizer`
- Loading branch information
Showing
44 changed files
with
738 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
docs/source/api_reference/data_connectors/generator_connector.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
GeneratorConnector | ||
===================================== | ||
|
||
|
||
.. autoclass:: sdgx.data_connectors.generator_connector.GeneratorConnector | ||
:members: | ||
:undoc-members: | ||
:inherited-members: | ||
:show-inheritance: | ||
:private-members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
example/extension/dummymetadatainspector/dummymetadatainspector/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
__version__ = "0.1.0" |
13 changes: 13 additions & 0 deletions
13
example/extension/dummymetadatainspector/dummymetadatainspector/inspector.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from __future__ import annotations | ||
|
||
from sdgx.data_models.inspectors.base import Inspector | ||
from sdgx.data_models.inspectors.extension import hookimpl | ||
|
||
|
||
class MyOwnInspector(Inspector): | ||
... | ||
|
||
|
||
@hookimpl | ||
def register(manager): | ||
manager.register("DummyInspector", MyOwnInspector) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[build-system] | ||
requires = ["hatchling"] | ||
build-backend = "hatchling.build" | ||
|
||
[project] | ||
name = "dummymetadatainspector" | ||
dependencies = ["sdgx"] | ||
dynamic = ["version"] | ||
requires-python = ">=3.8" | ||
classifiers = [ | ||
"Programming Language :: Python :: 3", | ||
'Programming Language :: Python :: 3.8', | ||
'Programming Language :: Python :: 3.9', | ||
'Programming Language :: Python :: 3.10', | ||
'Programming Language :: Python :: 3.11', | ||
] | ||
[project.optional-dependencies] | ||
test = ["pytest"] | ||
|
||
[tool.check-manifest] | ||
ignore = [".*"] | ||
|
||
[tool.hatch.version] | ||
path = "dummymetadatainspector/__init__.py" | ||
|
||
[project.entry-points."sdgx.metadata.inspector"] | ||
dummymetadatainspector = "dummymetadatainspector.inspector" |
16 changes: 16 additions & 0 deletions
16
example/extension/dummymetadatainspector/tests/test_registed_inspector.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import pytest | ||
|
||
from sdgx.data_models.inspectors.manager import InspectorManager | ||
|
||
|
||
@pytest.fixture | ||
def manager(): | ||
yield InspectorManager() | ||
|
||
|
||
def test_registed_cacher(manager: InspectorManager): | ||
assert manager._normalize_name("DummyInspector") in manager.registed_inspectors | ||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main(["-vv", "-s", __file__]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Callable, Generator | ||
|
||
import pandas as pd | ||
|
||
from sdgx.data_connectors.base import DataConnector | ||
|
||
|
||
class GeneratorConnector(DataConnector): | ||
""" | ||
A virtual data connector that wrap `Generator <https://docs.python.org/3/glossary.html#term-generator>`_ | ||
into a DataConnector. | ||
Passing ``offset=0`` to ``read`` will reset the generator. | ||
Warning: | ||
``offset`` and ``limit`` are ignored as ``Generator`` not supporting random access. | ||
Note: | ||
This connector is not been registered by default. So only be used with the library way. | ||
""" | ||
|
||
@property | ||
def identity(self) -> str: | ||
return f"{id(self.generator_caller)}" | ||
|
||
def __init__( | ||
self, | ||
generator_caller: Callable[[], Generator[pd.DataFrame, None, None]], | ||
*args, | ||
**kwargs, | ||
): | ||
super().__init__(*args, **kwargs) | ||
self.generator_caller = generator_caller | ||
self._generator = self.generator_caller() | ||
|
||
def _read(self, offset=0, limit=None) -> pd.DataFrame: | ||
""" | ||
Ingore limit and allow sequential reading. | ||
""" | ||
if offset == 0: | ||
self._generator = self.generator_caller() | ||
|
||
try: | ||
return next(self._generator) | ||
except StopIteration: | ||
return None | ||
|
||
def _columns(self) -> list[str]: | ||
for df in self._iter(): | ||
return list(df.columns) | ||
|
||
def _iter(self, offset=0, chunksize=0) -> Generator[pd.DataFrame, None, None]: | ||
""" | ||
Subclass should implement this for reading data in chunk. | ||
See ``iter`` for more details. | ||
""" | ||
return self.generator_caller() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import pandas as pd | ||
|
||
from sdgx.data_models.inspectors.inspect_meta import InspectMeta | ||
|
||
|
||
class Inspector: | ||
""" | ||
Base Inspector class | ||
Inspector is used to inspect data and generate metadata automatically. | ||
""" | ||
|
||
ready: bool | ||
"""Ready to inspect, maybe all fields are fitted.""" | ||
|
||
def fit(self, raw_data: pd.DataFrame): | ||
"""Fit the inspector. | ||
Args: | ||
raw_data (pd.DataFrame): Raw data | ||
""" | ||
return | ||
|
||
def inspect(self) -> InspectMeta: | ||
"""Inspect raw data and generate metadata.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
import pluggy | ||
|
||
project_name = "sdgx.metadata.inspector" | ||
""" | ||
The entry-point name of this extension. | ||
Should be used in ``pyproject.toml`` as ``[project.entry-points."{project_name}"]`` | ||
""" | ||
hookimpl = pluggy.HookimplMarker(project_name) | ||
""" | ||
Hookimpl marker for this extension, extension module should use this marker | ||
Example: | ||
.. code-block:: python | ||
@hookimpl | ||
def register(manager): | ||
... | ||
""" | ||
|
||
hookspec = pluggy.HookspecMarker(project_name) | ||
|
||
|
||
@hookspec | ||
def register(manager): | ||
""" | ||
For more information about this function, please check the :ref:`manager` | ||
We provided an example package for you in ``{project_root}/example/extension/dummymetadatainspector``. | ||
Example: | ||
.. code-block:: python | ||
class MyOwnInspector(Inspector): | ||
... | ||
from sdgx.data_models.inspectors.extension import hookimpl | ||
@hookimpl | ||
def register(manager): | ||
manager.register("DummyInspector", MyOwnInspector) | ||
Config ``project.entry-points`` so that we can find it | ||
.. code-block:: toml | ||
[project.entry-points."sdgx.metadata.inspector"] | ||
{whatever-name} = "{package}.{path}.{to}.{file-with-hookimpl-function}" | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class InspectMeta: | ||
pass |
Oops, something went wrong.