Skip to content

Configuration

Configuration settings are explained here.

sleepecg.get_config()

Read SleepECG configuration.

For parameters not set in the user configuration file (~/.sleepecg/config.yml), this falls back to the default values defined in site-packages/sleepecg/config.yml. See configuration for a list of possible settings.

Returns:

  • dict[str, str]

    All configuration keys and values.

Source code in sleepecg/config.py
def get_config() -> dict[str, str]:
    """
    Read SleepECG configuration.

    For parameters not set in the user configuration file (`~/.sleepecg/config.yml`), this
    falls back to the default values defined in `site-packages/sleepecg/config.yml`. See
    [configuration](../configuration.md) for a list of possible settings.

    Returns
    -------
    dict[str, str]
        All configuration keys and values.
    """
    config = _read_yaml(_DEFAULT_CONFIG_PATH)
    user_config = _read_yaml(_USER_CONFIG_PATH)
    if invalid_keys := set(user_config) - set(config):
        raise ValueError(
            f"Invalid key(s) found in user config at {_USER_CONFIG_PATH}: {invalid_keys}"
        )
    config.update(user_config)
    return config

sleepecg.get_config_value(key)

Read specific SleepECG configuration value.

For parameters not set in the user configuration file (~/.sleepecg/config.yml), this falls back to the default values defined in site-packages/sleepecg/config.yml. See configuration for a list of possible settings.

Parameters:

  • key (str) –

    The configuration key to look for.

Returns:

  • str

    The configuration value.

Source code in sleepecg/config.py
def get_config_value(key: str) -> str:
    """
    Read specific SleepECG configuration value.

    For parameters not set in the user configuration file (`~/.sleepecg/config.yml`), this
    falls back to the default values defined in `site-packages/sleepecg/config.yml`. See
    [configuration](../configuration.md) for a list of possible settings.

    Parameters
    ----------
    key : str
        The configuration key to look for.

    Returns
    -------
    str
        The configuration value.
    """
    config = get_config()

    if key not in config:
        options = ", ".join(config)
        raise ValueError(
            f"Trying to get invalid config key: {key!r}, possible options: {options}"
        )
    return config[key]

sleepecg.set_config(**kwargs)

Set SleepECG preferences and store them to the user configuration file.

If a value is None, the corresponding key is deleted from the user configuration. See configuration for a list of possible settings.

Parameters:

  • **kwargs (Any) –

    The configuration keys and values to set.

Examples:

>>> set_config(data_dir='~/.sleepecg/datasets')
Source code in sleepecg/config.py
def set_config(**kwargs: Any) -> None:
    """
    Set SleepECG preferences and store them to the user configuration file.

    If a value is `None`, the corresponding key is deleted from the user configuration. See
    [configuration](../configuration.md) for a list of possible settings.

    Parameters
    ----------
    **kwargs: dict, optional
        The configuration keys and values to set.

    Examples
    --------
    >>> set_config(data_dir='~/.sleepecg/datasets')
    """
    default_config = _read_yaml(_DEFAULT_CONFIG_PATH)
    user_config = _read_yaml(_USER_CONFIG_PATH)

    # validate all parameters before setting anything
    for key, value in kwargs.items():
        if key not in default_config:
            options = ", ".join(default_config)
            raise ValueError(
                f"Trying to set invalid config key: {key!r}, possible options: {options}"
            )

    for key, value in kwargs.items():
        if value is None:
            user_config.pop(key, None)
        else:
            user_config[key] = value

    _USER_CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True)
    with open(_USER_CONFIG_PATH, "w") as user_config_file:
        yaml.dump(user_config, user_config_file)