File size: 4,468 Bytes
07423df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from typing import Optional

import pandas as pd
from pandas.core.frame import DataFrame
from sqlalchemy import Integer, String, create_engine
from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column


class Base(DeclarativeBase):
    pass


class Dataset(Base):
    """Dataset table"""

    __tablename__ = "datasets"

    id: Mapped[int] = mapped_column("id", Integer, autoincrement=True, primary_key=True)
    name: Mapped[str] = mapped_column("name", String, unique=True)
    path: Mapped[str] = mapped_column("path", String)
    config_file: Mapped[str] = mapped_column("config_file", String)
    train_rows: Mapped[int] = mapped_column("train_rows", Integer)
    validation_rows: Mapped[Optional[int]] = mapped_column(
        "validation_rows", Integer, nullable=True
    )


class Experiment(Base):
    """Experiment table"""

    __tablename__ = "experiments"

    id: Mapped[int] = mapped_column("id", Integer, primary_key=True)
    name: Mapped[str] = mapped_column("name", String)
    mode: Mapped[str] = mapped_column("mode", String)
    dataset: Mapped[str] = mapped_column("dataset", String)
    config_file: Mapped[str] = mapped_column("config_file", String)
    path: Mapped[str] = mapped_column("path", String)
    seed: Mapped[int] = mapped_column("seed", Integer)
    process_id: Mapped[int] = mapped_column("process_id", Integer)
    gpu_list: Mapped[str] = mapped_column("gpu_list", String)


class Database:
    """Class for managing database."""

    def __init__(self, path_db: str) -> None:
        """Initialize database

        Args:
            path_db: path to sqlite database file
        """

        self.__engine__ = create_engine(f"sqlite:///{path_db}")
        Base.metadata.create_all(self.__engine__)
        self._session = Session(self.__engine__)

    def add_dataset(self, dataset: Dataset) -> None:
        """Add a dataset to the table

        Args:
            dataset: dataset to add
        """
        self._session.add(dataset)
        self._session.commit()

    def delete_dataset(self, id: int) -> None:
        """Delete a dataset from the table

        Args:
            id: dataset id to delete
        """

        dataset = self._session.query(Dataset).get(int(id))
        self._session.delete(dataset)
        self._session.commit()

    def get_dataset(self, id: int) -> Dataset:
        """Return dataset given an id

        Args:
            id: dataset id to return

        Returns:
            Dataset with given id
        """

        return self._session.query(Dataset).get(int(id))

    def get_datasets_df(self) -> DataFrame:
        """Return dataframe containing all datasets

        Returns:
            All datasets
        """

        datasets = pd.read_sql(self._session.query(Dataset).statement, self.__engine__)
        return datasets.sort_values("id", ascending=False)

    def add_experiment(self, experiment: Experiment) -> None:
        """Add an experiment to the table

        Args:
            experiment: experiment to add
        """

        self._session.add(experiment)
        self._session.commit()

    def delete_experiment(self, id: int) -> None:
        """Delete an experiment from the table

        Args:
            id: experiment id to delete
        """

        experiment = self._session.query(Experiment).get(int(id))
        self._session.delete(experiment)
        self._session.commit()

    def get_experiment(self, id: int) -> Experiment:
        """Return experiment given an id

        Args:
            id: experiment id to return

        Returns:
            Experiment with given id
        """

        return self._session.query(Experiment).get(int(id))

    def get_experiments_df(self) -> DataFrame:
        """Return dataframe containing all experiments

        Returns:
            All experiments
        """

        experiments = pd.read_sql(
            self._session.query(Experiment).statement, self.__engine__
        )
        return experiments.sort_values("id", ascending=False)

    def rename_experiment(self, id: int, new_name: str, new_path: str) -> None:
        """Rename experiment given id and new name

        Args:
            id: experiment id
            new_name: new name
        """

        experiment = self.get_experiment(id)
        experiment.name = new_name
        experiment.path = new_path
        self._session.commit()

    def update(self) -> None:
        self._session.commit()