Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cellpy/readers/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ def pickle_protocol(level):
class BaseDbReader(metaclass=abc.ABCMeta):
"""Base class for database readers."""

@abc.abstractmethod
def from_batch(
self, batch_name: str|None=None,
include_key: bool=False,
include_individual_arguments: bool=False,
**kwargs: Any
) -> dict:
"""Get a dictionary with the data from a batch for the journal.

Args:
batch: name of the batch.
include_key: include the key (the cell ids).
include_individual_arguments: include the individual arguments.

Returns:
dict: dictionary with the data.
"""
pass



class BaseSimpleDbReader(metaclass=abc.ABCMeta):
"""Base class for database readers."""

@abc.abstractmethod
def select_batch(self, batch: str) -> List[int]:
pass
Expand Down
2 changes: 1 addition & 1 deletion cellpy/readers/dbreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __repr__(self):
return f"<DbCols: {self.__dict__}>"


class Reader(core.BaseDbReader):
class Reader(core.BaseSimpleDbReader):
def __init__(
self,
db_file=None,
Expand Down
42 changes: 42 additions & 0 deletions cellpy/readers/json_dbreader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from typing import Any
from cellpy.readers.core import BaseDbReader
import json


class BaseJSONReader(BaseDbReader):
def __init__(self, json_file: str):
self.json_file = json_file
self.data = self.load_data()

def _load_raw_data(self):
with open(self.json_file, "r") as f:
return json.load(f)

def load_data(self):
return pd.read_json(self.json_file)


class BattBaseJSONReader(BaseJSONReader):
_version = "1.0.0"

def from_batch(
self,
batch_name: str | None = None,
project_name: str | None = None,
include_key: bool = False,
include_individual_arguments: bool = False,
) -> dict:
raise NotImplementedError("This method is not implemented for this reader")


if __name__ == "__main__":
from pathlib import Path
import pandas as pd

pd.set_option("display.max_columns", None)

local_dir = Path(__file__).parent.parent.parent / "local"
json_file = local_dir / "cellpy_journal_table.json"
print(json_file.exists())
reader = BattBaseJSONReader(json_file)
print(reader.from_batch())
4 changes: 2 additions & 2 deletions cellpy/readers/sql_dbreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
BATCH_ATTRS_TO_IMPORT_FROM_EXCEL_SQLITE,
get_headers_journal,
)
from cellpy.readers.core import BaseDbReader
from cellpy.readers.core import BaseSimpleDbReader

# ----------------- USED WHEN CONVERTING FROM EXCEL -----------------
DB_FILE_EXCEL = prms.Paths.db_filename
Expand Down Expand Up @@ -158,7 +158,7 @@ def __str__(self) -> str:
return f"batch: '{self.name}' (#{self.pk})"


class SQLReader(BaseDbReader):
class SQLReader(BaseSimpleDbReader):
"""A custom database reader for the batch utility in cellpy."""

def __init__(self, db_connection: str = None, batch: str = None, **kwargs) -> None:
Expand Down
10 changes: 10 additions & 0 deletions cellpy/utils/batch_tools/engines.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pandas as pd

from cellpy import dbreader
from cellpy.readers import json_dbreader
from cellpy.parameters.internal_settings import get_headers_journal, get_headers_summary
from cellpy.utils.batch_tools import batch_helpers as helper

Expand Down Expand Up @@ -191,6 +192,15 @@ def simple_db_engine(
reader = dbreader.Reader()
logging.debug("No reader provided. Creating one myself.")

if isinstance(reader, str):
match reader:
case "simple_excel_reader":
reader = dbreader.Reader()
case "batbase_json_reader":
reader = json_dbreader.BatbaseJSONReader()
case _:
raise ValueError(f"Invalid reader: {reader}")

if cell_ids is None:
logging.debug("cell_ids is None")
pages_dict = reader.from_batch(
Expand Down
Loading