Skip to content

BaseLoader

Bases: BaseConfig, ABC

Abstract base class for loading and saving processed data.

This class provides a structure for loading and saving datasets and defines abstract methods that need to be implemented by subclasses. It includes methods for specifying data paths and filenames for loading and saving operations.

Inherits
  • BaseConfig: Provides configuration settings for data processing.
  • ABC: Specifies abstract methods for subclasses to implement.
Abstract Methods
  • load_data: Load processed data from the specified path and file.
  • save_data: Save processed data to the specified path and file.
Example
loader = SomeConcreteLoader()
data = loader.load_data(path=Path("/data"), name="processed_data.csv")
loader.save_data(data=data, path=Path("/data"), name="saved_data.csv")
Source code in periomod/data/_basedata.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class BaseLoader(BaseConfig, ABC):
    """Abstract base class for loading and saving processed data.

    This class provides a structure for loading and saving datasets and defines
    abstract methods that need to be implemented by subclasses. It includes methods
    for specifying data paths and filenames for loading and saving operations.

    Inherits:
        - `BaseConfig`: Provides configuration settings for data processing.
        - `ABC`: Specifies abstract methods for subclasses to implement.

    Abstract Methods:
        - `load_data`: Load processed data from the specified path and file.
        - `save_data`: Save processed data to the specified path and file.

    Example:
        ```
        loader = SomeConcreteLoader()
        data = loader.load_data(path=Path("/data"), name="processed_data.csv")
        loader.save_data(data=data, path=Path("/data"), name="saved_data.csv")
        ```
    """

    def __init__(self) -> None:
        """Initializes the BaseLoader, defining the `load_data` abstract method."""
        super().__init__()

    @abstractmethod
    def load_data(self, path: Path):
        """Loads the processed data from the specified path.

        Args:
            path (Path): Directory path for the processed data.
        """

    @abstractmethod
    def save_data(self, data: pd.DataFrame, path: Union[str, Path]) -> None:
        """Saves the processed data to the specified path as a CSV file.

        Args:
            data (pd.DataFrame): The processed DataFrame.
            path (Union[str, Path]): Path (including filename) where data will be saved.

        Raises:
            ValueError: If the DataFrame is empty.
        """
        path = Path(path)
        if not path.is_absolute():
            path = Path.cwd() / path
        if data.empty:
            raise ValueError("Data must be processed before saving.")
        if path.suffix != ".csv":
            path = path.with_suffix(".csv")
        os.makedirs(path.parent, exist_ok=True)
        data.to_csv(path, index=False)
        print(f"Data saved to {path}")

__init__()

Initializes the BaseLoader, defining the load_data abstract method.

Source code in periomod/data/_basedata.py
35
36
37
def __init__(self) -> None:
    """Initializes the BaseLoader, defining the `load_data` abstract method."""
    super().__init__()

load_data(path) abstractmethod

Loads the processed data from the specified path.

Parameters:

Name Type Description Default
path Path

Directory path for the processed data.

required
Source code in periomod/data/_basedata.py
39
40
41
42
43
44
45
@abstractmethod
def load_data(self, path: Path):
    """Loads the processed data from the specified path.

    Args:
        path (Path): Directory path for the processed data.
    """

save_data(data, path) abstractmethod

Saves the processed data to the specified path as a CSV file.

Parameters:

Name Type Description Default
data DataFrame

The processed DataFrame.

required
path Union[str, Path]

Path (including filename) where data will be saved.

required

Raises:

Type Description
ValueError

If the DataFrame is empty.

Source code in periomod/data/_basedata.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
@abstractmethod
def save_data(self, data: pd.DataFrame, path: Union[str, Path]) -> None:
    """Saves the processed data to the specified path as a CSV file.

    Args:
        data (pd.DataFrame): The processed DataFrame.
        path (Union[str, Path]): Path (including filename) where data will be saved.

    Raises:
        ValueError: If the DataFrame is empty.
    """
    path = Path(path)
    if not path.is_absolute():
        path = Path.cwd() / path
    if data.empty:
        raise ValueError("Data must be processed before saving.")
    if path.suffix != ".csv":
        path = path.with_suffix(".csv")
    os.makedirs(path.parent, exist_ok=True)
    data.to_csv(path, index=False)
    print(f"Data saved to {path}")