CellModules
pythonlib.DatabaseManager.DatabaseManager Class Reference

Public Member Functions

def __init__ (self, str db_filename)
 
def open (self)
 
def close (self)
 
def __enter__ (self)
 
def __exit__ (self, exc_type, exc_val, exc_tb)
 
def add_conditions (self, pd.DataFrame conditions_df, str id_column=None)
 
def add_error (self, pd.DataFrame data_df)
 
def add_parameters (self, list parameters_list)
 
def add_data (self, str table_name, pd.DataFrame data_df)
 
pd.DataFrame get_parameters (self, str filter_conditions=None, bool raw=False)
 
pd.DataFrame get_conditions (self, str filter_conditions=None)
 
pd.DataFrame get (self, str table, str filter_conditions=None, bool rawParameters=False)
 
def iterOn (self, str table, group, str filter_conditions=None)
 
pd.DataFrame getBestScore (self, str errorName, int n=1, bool forEachCondition=False)
 
pd.DataFrame getBest (self, str table, str errorName, int n=1, bool forEachCondition=False)
 
pd.DataFrame getByID (self, str table, int ID_PARAMETER=None, int ID_CONDITION=None)
 
pd.DataFrame getPareto (self, list errorNameList, bool forEachCondition=False)
 
pd.DataFrame query (self, str query)
 
pd.DataFrame __getitem__ (self, str table)
 
str summarize (self)
 
str __str__ (self)
 

Public Attributes

 db_filename
 
 conn
 
 available_tables
 

Private Member Functions

def _parse_json_conditions (self, filter_conditions)
 
def _build_clauses (self, str table, str filter_conditions=None, str alias_prefix='main_table')
 
def _check_tables (self)
 
def _get_table_columns (self, table_name)
 
def _create_index (self, table_name, column_name)
 
def _flatten_dict (self, d, parent_key='', sep='$')
 
def _unflat (self, d, sep='$')
 

Private Attributes

 _table_columns_cache
 

Detailed Description

Manager for interacting with a SQLite database. This class provides methods to add data, query data, and manage tables in the database.

Parameters
----------
- **db_filename** : str
    The filename of the SQLite database.

Methods
-------
- **add_conditions** : Adds a DataFrame of conditions to the 'condition' table in the database.
- **add_parameters** : Adds a list of parameters to the 'parameters' table in the database.
- **add_data** : Adds a DataFrame of data to a specified table in the database.
- **add_error** : Adds a DataFrame of error data to the 'error' table in the database.
- **get** : Retrieves records from a table based on filter conditions.
- **getByID** : Retrieves records from a table based on ID_PARAMETER and/or ID_CONDITION.
- **getPareto** : Retrieves the Pareto front for given errors.
- **getBestScore** : Retrieves the best scores for a given error.
- **getBest** : Retrieves the best records from a table based on a given error.
- **summarize** : Returns a summary of the database.

Example
-------
```python
conditions = pd.DataFrame([
    [0.5, 1],
    [0.2, 2],
    [0.2, 5]
], columns=['info1', 'info2'])
parameters_list = LHSIterator(param,10)

with DatabaseManager("example.db") as db:
    db.add_parameters(parameters_list)
    db.add_conditions(conditions)
    
    for states,other in MultiSimu(run_simu,parameters_list,conditions=conditions.values.tolist(),replicat=5,withTqdm=True,batch_size_level='param'):
        db.add_data('states', states.reset_index())
        db.add_data('otherData', other.reset_index())
        error = your_error_function(states)
        db.add_error(error)
```

Definition at line 6 of file DatabaseManager.py.

Constructor & Destructor Documentation

◆ __init__()

def pythonlib.DatabaseManager.DatabaseManager.__init__ (   self,
str  db_filename 
)

Definition at line 50 of file DatabaseManager.py.

Member Function Documentation

◆ __enter__()

def pythonlib.DatabaseManager.DatabaseManager.__enter__ (   self)

Definition at line 72 of file DatabaseManager.py.

◆ __exit__()

def pythonlib.DatabaseManager.DatabaseManager.__exit__ (   self,
  exc_type,
  exc_val,
  exc_tb 
)

Definition at line 76 of file DatabaseManager.py.

◆ __getitem__()

pd.DataFrame pythonlib.DatabaseManager.DatabaseManager.__getitem__ (   self,
str  table 
)

Definition at line 497 of file DatabaseManager.py.

◆ __str__()

str pythonlib.DatabaseManager.DatabaseManager.__str__ (   self)

Definition at line 522 of file DatabaseManager.py.

◆ _build_clauses()

def pythonlib.DatabaseManager.DatabaseManager._build_clauses (   self,
str  table,
str   filter_conditions = None,
str   alias_prefix = 'main_table' 
)
private
Build JOIN and WHERE clauses based on filter_conditions, similar to get().
Returns: join_sql, where_sql, alias_dict

Definition at line 204 of file DatabaseManager.py.

◆ _check_tables()

def pythonlib.DatabaseManager.DatabaseManager._check_tables (   self)
private

Definition at line 526 of file DatabaseManager.py.

◆ _create_index()

def pythonlib.DatabaseManager.DatabaseManager._create_index (   self,
  table_name,
  column_name 
)
private

Definition at line 541 of file DatabaseManager.py.

◆ _flatten_dict()

def pythonlib.DatabaseManager.DatabaseManager._flatten_dict (   self,
  d,
  parent_key = '',
  sep = '$' 
)
private

Definition at line 548 of file DatabaseManager.py.

◆ _get_table_columns()

def pythonlib.DatabaseManager.DatabaseManager._get_table_columns (   self,
  table_name 
)
private

Definition at line 531 of file DatabaseManager.py.

◆ _parse_json_conditions()

def pythonlib.DatabaseManager.DatabaseManager._parse_json_conditions (   self,
  filter_conditions 
)
private

Definition at line 162 of file DatabaseManager.py.

◆ _unflat()

def pythonlib.DatabaseManager.DatabaseManager._unflat (   self,
  d,
  sep = '$' 
)
private

Definition at line 560 of file DatabaseManager.py.

◆ add_conditions()

def pythonlib.DatabaseManager.DatabaseManager.add_conditions (   self,
pd.DataFrame  conditions_df,
str   id_column = None 
)
Adds a DataFrame of conditions to the 'condition' table in the database.

Parameters
----------
- **conditions_df** : pd.DataFrame
    The DataFrame containing the conditions to add.
- **id_column** : str, optional
    The column to use as the ID_CONDITION. If not provided, the index will be used.

Definition at line 79 of file DatabaseManager.py.

◆ add_data()

def pythonlib.DatabaseManager.DatabaseManager.add_data (   self,
str  table_name,
pd.DataFrame  data_df 
)
Adds a DataFrame of data to a specified table in the database.

Parameters
----------
- **table_name** : str
    The name of the table to add data to.
- **data_df** : pd.DataFrame
    The DataFrame containing the data to add.

Definition at line 139 of file DatabaseManager.py.

◆ add_error()

def pythonlib.DatabaseManager.DatabaseManager.add_error (   self,
pd.DataFrame  data_df 
)
Adds a DataFrame of error data to the 'error' table in the database.

Parameters
----------
- **data_df** : pd.DataFrame
    The DataFrame containing the error data to add.

Definition at line 103 of file DatabaseManager.py.

◆ add_parameters()

def pythonlib.DatabaseManager.DatabaseManager.add_parameters (   self,
list  parameters_list 
)
Adds a list of parameters to the 'parameters' table in the database.

Parameters
----------
- **parameters_list** : list
    The list of parameters to add.

Definition at line 122 of file DatabaseManager.py.

◆ close()

def pythonlib.DatabaseManager.DatabaseManager.close (   self)
Closes the connection to the database.

Definition at line 66 of file DatabaseManager.py.

◆ get()

pd.DataFrame pythonlib.DatabaseManager.DatabaseManager.get (   self,
str  table,
str   filter_conditions = None,
bool   rawParameters = False 
)
Retrieves records from a table based on filter conditions.

Parameters
----------
- **table** : str
    The name of the table to retrieve records from.
- **filter_conditions** : str, optional
    The SQL filter conditions to apply.
- **rawParameters** : bool, optional
    If True, returns the raw DataFrame without JSON parsing.

Returns
-------
- **pd.DataFrame** : The DataFrame containing the retrieved records.

Definition at line 247 of file DatabaseManager.py.

◆ get_conditions()

pd.DataFrame pythonlib.DatabaseManager.DatabaseManager.get_conditions (   self,
str   filter_conditions = None 
)
Retrieves records from the 'condition' table based on filter conditions.

Parameters
----------
- **filter_conditions** : str, optional
    The SQL filter conditions to apply.

Returns
-------
- **pd.DataFrame** : The DataFrame containing the retrieved records.

Definition at line 188 of file DatabaseManager.py.

◆ get_parameters()

pd.DataFrame pythonlib.DatabaseManager.DatabaseManager.get_parameters (   self,
str   filter_conditions = None,
bool   raw = False 
)
Retrieves the parameters based on filter conditions.

Parameters
----------
- **filter_conditions** : str, optional
    The SQL filter conditions to apply.
- **raw** : bool, optional
    If True, returns the raw DataFrame without JSON parsing.

Returns
-------
- **pd.DataFrame** : The DataFrame containing the retrieved records.

Definition at line 171 of file DatabaseManager.py.

◆ getBest()

pd.DataFrame pythonlib.DatabaseManager.DatabaseManager.getBest (   self,
str  table,
str  errorName,
int   n = 1,
bool   forEachCondition = False 
)
Retrieves the best records from a table based on a given error.

Parameters
----------
- **table** : str
    The name of the table to retrieve records from.
- **errorName** : str
    The name of the error column to base the records on.
- **n** : int, optional
    The number of top records to retrieve.
- **forEachCondition** : bool, optional
    Whether to retrieve the best records for each condition separately.

Returns
-------
- **pd.DataFrame** : The DataFrame containing the best records.

Definition at line 338 of file DatabaseManager.py.

◆ getBestScore()

pd.DataFrame pythonlib.DatabaseManager.DatabaseManager.getBestScore (   self,
str  errorName,
int   n = 1,
bool   forEachCondition = False 
)
Retrieves the best scores for a given error.

Parameters
----------
- **errorName** : str
    The name of the error column to base the scores on.
- **n** : int, optional
    The number of top scores to retrieve.
- **forEachCondition** : bool, optional
    Whether to retrieve the best scores for each condition separately.

Returns
-------
- **pd.DataFrame** : The DataFrame containing the best scores.

Definition at line 297 of file DatabaseManager.py.

◆ getByID()

pd.DataFrame pythonlib.DatabaseManager.DatabaseManager.getByID (   self,
str  table,
int   ID_PARAMETER = None,
int   ID_CONDITION = None 
)
Retrieves records from a table based on ID_PARAMETER and/or ID_CONDITION.

Parameters
----------
- **table** : str
    The name of the table to retrieve records from.
- **ID_PARAMETER** : int or list of int, optional
    The ID_PARAMETER to filter by.
- **ID_CONDITION** : int or list of int, optional
    The ID_CONDITION to filter by.

Returns
-------
- **pd.DataFrame** : The DataFrame containing the retrieved records.

Definition at line 387 of file DatabaseManager.py.

◆ getPareto()

pd.DataFrame pythonlib.DatabaseManager.DatabaseManager.getPareto (   self,
list  errorNameList,
bool   forEachCondition = False 
)
Retrieves the Pareto front for given errors.

Parameters
----------
- **errorNameList** : list
    The list of error column names to consider.
- **forEachCondition** : bool, optional
    Whether to retrieve the Pareto front for each condition separately.

Returns
-------
- **pd.DataFrame** : The DataFrame containing the Pareto front.

Definition at line 427 of file DatabaseManager.py.

◆ iterOn()

def pythonlib.DatabaseManager.DatabaseManager.iterOn (   self,
str  table,
  group,
str   filter_conditions = None 
)

Definition at line 280 of file DatabaseManager.py.

◆ open()

def pythonlib.DatabaseManager.DatabaseManager.open (   self)
Opens a connection to the database.

Definition at line 54 of file DatabaseManager.py.

◆ query()

pd.DataFrame pythonlib.DatabaseManager.DatabaseManager.query (   self,
str  query 
)
Executes a raw SQL query and returns the result as a DataFrame.

Parameters
----------
- **query** : str
    The SQL query to execute.

Returns
-------
- **pd.DataFrame** : The DataFrame containing the query results.

Definition at line 481 of file DatabaseManager.py.

◆ summarize()

str pythonlib.DatabaseManager.DatabaseManager.summarize (   self)
Returns a summary of the database.

Returns
-------
- **str** : The summary of the database.

Definition at line 502 of file DatabaseManager.py.

Member Data Documentation

◆ _table_columns_cache

pythonlib.DatabaseManager.DatabaseManager._table_columns_cache
private

Definition at line 52 of file DatabaseManager.py.

◆ available_tables

pythonlib.DatabaseManager.DatabaseManager.available_tables

Definition at line 529 of file DatabaseManager.py.

◆ conn

pythonlib.DatabaseManager.DatabaseManager.conn

Definition at line 58 of file DatabaseManager.py.

◆ db_filename

pythonlib.DatabaseManager.DatabaseManager.db_filename

Definition at line 51 of file DatabaseManager.py.


The documentation for this class was generated from the following file: