bci_essentials.session_saving

This module contains functions for saving and loading sessions.

 1"""
 2This module contains functions for saving and loading sessions.
 3"""
 4
 5import os
 6import pickle
 7
 8from .utils.logger import Logger  # Logger wrapper
 9
10# Instantiate a logger for the module at the default level of logging.INFO
11# Logs to bci_essentials.__module__) where __module__ is the name of the module
12logger = Logger(name=__name__)
13
14# Define a module-level variable for the session save path
15# This is the local path to the session_saves directory in the package root
16session_save_path = os.path.join(
17    os.path.dirname(os.path.dirname(__file__)), "session_saves"
18)
19
20
21def save_classifier(classifier, filename):
22    """
23    Saves a classifier to a file.
24
25    Parameters
26    ----------
27    classifier : object
28        The classifier object to be saved.
29    filename : str
30        The name of the file to save the classifier to.
31    """
32
33    # Join filename to session_saves directory in package root
34    filepath = os.path.join(session_save_path, filename)
35
36    with open(filepath, "wb") as f:
37        pickle.dump(classifier, f)
38    logger.debug("Saved classifier to %s", filepath)
39
40
41def load_classifier(filename):
42    """
43    Loads a classifier from a file.
44
45    Parameters
46    ----------
47    filename : str
48        The name of the file to load the classifier from.
49
50    Returns
51    -------
52    classifier : object
53        The classifier object that was loaded.
54    """
55
56    # Join filename to session_saves directory in package root
57    filepath = os.path.join(session_save_path, filename)
58
59    with open(filepath, "rb") as f:
60        classifier = pickle.load(f)
61    logger.debug("Loaded classifier from %s", filepath)
62    return classifier
session_save_path = '/home/runner/work/APIdocs-for-bci-essentials-python/APIdocs-for-bci-essentials-python/./session_saves'
def save_classifier(classifier, filename):
22def save_classifier(classifier, filename):
23    """
24    Saves a classifier to a file.
25
26    Parameters
27    ----------
28    classifier : object
29        The classifier object to be saved.
30    filename : str
31        The name of the file to save the classifier to.
32    """
33
34    # Join filename to session_saves directory in package root
35    filepath = os.path.join(session_save_path, filename)
36
37    with open(filepath, "wb") as f:
38        pickle.dump(classifier, f)
39    logger.debug("Saved classifier to %s", filepath)

Saves a classifier to a file.

Parameters
  • classifier (object): The classifier object to be saved.
  • filename (str): The name of the file to save the classifier to.
def load_classifier(filename):
42def load_classifier(filename):
43    """
44    Loads a classifier from a file.
45
46    Parameters
47    ----------
48    filename : str
49        The name of the file to load the classifier from.
50
51    Returns
52    -------
53    classifier : object
54        The classifier object that was loaded.
55    """
56
57    # Join filename to session_saves directory in package root
58    filepath = os.path.join(session_save_path, filename)
59
60    with open(filepath, "rb") as f:
61        classifier = pickle.load(f)
62    logger.debug("Loaded classifier from %s", filepath)
63    return classifier

Loads a classifier from a file.

Parameters
  • filename (str): The name of the file to load the classifier from.
Returns
  • classifier (object): The classifier object that was loaded.