-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mob next [ci-skip] [ci skip] [skip ci]
lastFile:src/skore/project.py
- Loading branch information
Showing
1 changed file
with
96 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,123 @@ | ||
Path = None | ||
Item = None | ||
"""""" | ||
|
||
import json | ||
from pathlib import Path | ||
from typing import Any, List | ||
|
||
|
||
def get_filepath_from_project_name(project_name) -> Path: | ||
return None | ||
|
||
|
||
class Item: | ||
"""An item in the project.""" | ||
|
||
|
||
class JSONItem(Item): | ||
"""An item that is a JSON serializable Python object.""" | ||
|
||
def __init__(self, value): | ||
self.value = json.dumps(value) | ||
|
||
|
||
class DataFrameItem(Item): | ||
"""An item that is a pandas or polars data frame.""" | ||
|
||
|
||
class NumpyArrayItem(Item): | ||
"""An item that is a Numpy Array.""" | ||
|
||
|
||
class MediaItem(Item): | ||
"""An item that is a media object.""" | ||
|
||
|
||
class ScikitLearnModelItem(Item): | ||
"""An item that is a Scikit-Learn model.""" | ||
|
||
def __init__(self, value): | ||
self.value = value | ||
|
||
@staticmethod | ||
def transform(value): | ||
return html | ||
|
||
|
||
class Transformer: | ||
@staticmethod | ||
def transform(value) -> Item: | ||
... | ||
"""Transform a value into an Item.""" | ||
# Get the class of the value as a string | ||
value_class = type(value).__name__ | ||
|
||
# Map the class name to the corresponding Item subclass | ||
item_class_map = { | ||
"dict": JSONItem, | ||
"list": JSONItem, | ||
"tuple": JSONItem, | ||
"int": JSONItem, | ||
"float": JSONItem, | ||
"str": JSONItem, | ||
"bool": JSONItem, | ||
# TODO: Add | ||
# pandas.DataFrame DataFrameItem | ||
# polars.DataFrame DataFrameItem | ||
# numpy.ndarray NumpyArrayItem | ||
# matplotlib.figure.Figure MediaItem (as SVG) | ||
# PIL.Image.Image MediaItem (as PNG) | ||
# altair.Chart MediaItem (as Vega-Lite JSON) | ||
# bytes + media type MediaItem | ||
# str + media type MediaItem | ||
# Scikit-learn model ScikitLearnModelItem | ||
} | ||
|
||
# Check if the value's class is in our mapping | ||
if value_class in item_class_map: | ||
return item_class_map[value_class](value) | ||
|
||
# For scikit-learn models, we need to check if it's a BaseEstimator | ||
if hasattr(value, "fit") and hasattr(value, "predict"): | ||
return ScikitLearnModelItem(value) | ||
|
||
# If we don't have a specific Item subclass for this type, | ||
# we'll use the base Item class | ||
return Item(value) | ||
|
||
|
||
class Project: | ||
"""A project is a collection of items that are stored in a storage.""" | ||
|
||
def __init__(self, transformer, storage): | ||
self.transformer = transformer | ||
self.storage = storage | ||
|
||
def put(self, key, value): | ||
"""Put a value into the project.""" | ||
self.storage.save( | ||
key, | ||
self.transformer.transform(value), | ||
) | ||
|
||
def put_item(self, key, item): | ||
"""Put an item into the project.""" | ||
self.storage.save( | ||
key, | ||
item, | ||
) | ||
|
||
def get(self, key) -> Any: | ||
"""Get an item from the project.""" | ||
|
||
def get_item(self, key) -> Item: | ||
"""Get an item from the project.""" | ||
|
||
def list_keys(self) -> List[str]: | ||
"""List all keys in the project.""" | ||
|
||
def delete_item(key: str): | ||
"""Delete an item from the project.""" | ||
|
||
|
||
def load(project_name: str) -> Project: | ||
"""Load a project from a file.""" | ||
filepath = get_filepath_from_project_name(project_name) |