Skip to content

Utils

Functions

check_make_dirs(paths, verbose=True, isdir=False)

Create base directories for given paths if they do not exist.

Parameters:

Name Type Description Default
paths Union[str, List[str]]

A path or list of paths for which to check the basedirectories

required
verbose bool

Whether to log the output path

True
isdir bool

Treats given path(s) as diretory instead of only checking the basedir.

False
Source code in src/utils.py
 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
def check_make_dirs(
    paths: Union[str, List[str]],
    verbose: bool = True,
    isdir: bool = False,
) -> None:
    """Create base directories for given paths if they do not exist.

    Parameters
    ----------
    paths: List[str] | str
        A path or list of paths for which to check the basedirectories
    verbose: bool, default=True
        Whether to log the output path
    isdir: bool, default=False
        Treats given path(s) as diretory instead of only checking the basedir.
    """

    if not isinstance(paths, list):
        paths = [paths]
    for path in paths:
        if isdir and path != "" and not os.path.exists(path):
            os.makedirs(path)
        elif os.path.dirname(path) != "" and not os.path.exists(os.path.dirname(path)):
            os.makedirs(os.path.dirname(path))
        if verbose:
            log.info(f"Output path: {path}")

get_logger(name=__name__, log_level=logging.INFO, log_file=None)

Initializes command line logger.

Source code in src/utils.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def get_logger(
    name=__name__,
    log_level=logging.INFO,
    log_file: Optional[str] = None,
) -> logging.Logger:
    """Initializes command line logger."""

    logger = logging.getLogger(name)
    logger.setLevel(log_level)

    if log_file is not None:
        formatter = logging.Formatter(FORMAT)
        fh = logging.FileHandler(log_file, encoding="utf-8")
        fh.setLevel(log_level)
        fh.setFormatter(formatter)
        logger.addHandler(fh)

    return logger

make_delayed(signal, delays, circpad=False)

Create delayed versions of the 2-D signal.

Parameters:

Name Type Description Default
signal ndarray

2-D array of shape (n_samples, n_features)

required
delays ndarray

1-D array of delays to apply to the signal can be positive or negative; negative values advance the signal (shifting it backward)

required
circpad bool

If True, use circular padding for delays If False, use zero padding for delays

False

Returns:

Type Description
ndarray

2-D array of shape (n_samples, n_features * n_delays)

Source code in src/utils.py
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
def make_delayed(signal: np.ndarray, delays: np.ndarray, circpad=False) -> np.ndarray:
    """
    Create delayed versions of the 2-D signal.

    Parameters
    -----------
    signal : np.ndarray
        2-D array of shape (n_samples, n_features)
    delays : np.ndarray
        1-D array of delays to apply to the signal
        can be positive or negative; negative values advance the signal (shifting it backward)
    circpad : bool
        If True, use circular padding for delays
        If False, use zero padding for delays

    Returns
    --------
    np.ndarray
        2-D array of shape (n_samples, n_features * n_delays)
    """

    delayed_signals = []
    n_samples, n_features = signal.shape

    for delay in delays:
        delayed_signal = np.zeros_like(signal)
        if circpad:
            delayed_signal = np.roll(signal, delay, axis=0)
        else:
            if delay > 0:
                delayed_signal[delay:, :] = signal[:-delay, :]
            elif delay < 0:
                delayed_signal[:delay, :] = signal[-delay:, :]
            else:
                delayed_signal = signal.copy()
        delayed_signals.append(delayed_signal)

    return np.hstack(delayed_signals)