fedbiomed.common.environ

Module: fedbiomed.common.environ

All environment/configuration variables are provided by the Environ dictionary.

Environ is a singleton class, meaning that only an instance of Environ is available.

Descriptions of global/environment variables

Researcher Global Variables:

  • RESEARCHER_ID : id of the researcher
  • ID : equals to researcher id
  • TENSORBOARD_RESULTS_DIR : path for writing tensorboard log files
  • EXPERIMENTS_DIR : folder for saving experiments
  • MESSAGES_QUEUE_DIR : Path for writing queue files

Nodes Global Variables:

  • NODE_ID : id of the node
  • ID : equals to node id
  • MESSAGES_QUEUE_DIR : Path for queues
  • DB_PATH : TinyDB database path where datasets/training_plans/loading plans are saved
  • DEFAULT_TRAINING_PLANS_DIR : Path of directory for storing default training plans
  • TRAINING_PLANS_DIR : Path of directory for storing registered training plans
  • TRAINING_PLAN_APPROVAL : True if the node enables training plan approval
  • ALLOW_DEFAULT_TRAINING_PLANS : True if the node enables default training plans for training plan approval

Common Global Variables:

  • COMPONENT_TYPE : Node or Researcher
  • CONFIG_DIR : Configuration file path
  • VAR_DIR : Var directory of Fed-BioMed
  • CACHE_DIR : Cache directory of Fed-BioMed
  • TMP_DIR : Temporary directory
  • MQTT_BROKER : MQTT broker IP address
  • MQTT_BROKER_PORT : MQTT broker port
  • UPLOADS_URL : Upload URL for file repository
  • MPSPDZ_IP : MP-SPDZ endpoint IP of component
  • DEFAULT_BIPRIMES_DIR : Path of directory for storing default secure aggregation biprimes
  • ALLOW_DEFAULT_BIPRIMES : True if the component enables the default secure aggregation biprimes
  • PORT_INCREMENT_FILE : File for storing next port to be allocated for MP-SPDZ
  • CERT_DIR : Directory for storing certificates for MP-SPDZ
  • DEFAULT_BIPRIMES_DIR : Directory for storing default biprimes files

Classes

Environ

CLASS
Environ(root_dir=None)

Singleton class contains all variables for researcher or node

Parameters:

Name Type Description Default
root_dir str

if not provided the directory is deduced from the package location (specifying root_dir is mainly used by the test files)

None

Raises:

Type Description
FedbiomedEnvironError

If component type is invalid

Source code in fedbiomed/common/environ.py
def __init__(self, root_dir: str = None):
    """Class constructor

    Args:
        root_dir: if not provided the directory is deduced from the package location
            (specifying root_dir is mainly used by the test files)

    Raises:
        FedbiomedEnvironError: If component type is invalid
    """
    # dict with contains all configuration values
    self._values = {}
    self._cfg = configparser.ConfigParser()
    self._root_dir = root_dir

Functions

default_config_file()
abstractmethod

Abstract method for retrieving default configuration file path

Source code in fedbiomed/common/environ.py
@abstractmethod
def default_config_file(self) -> str:
    """Abstract method for retrieving default configuration file path"""
from_config(section, key)

Gets values from config file

Parameters:

Name Type Description Default
section

the section of the key

required
key

the name of the key

required

Returns:

Type Description
Any

The value of the key

Raises:

Type Description
FedbiomedEnvironError

If the key does not exist in the configuration

Source code in fedbiomed/common/environ.py
def from_config(self, section, key) -> Any:
    """Gets values from config file
    Args:
        section: the section of the key
        key: the name of the key

    Returns:
        The value of the key

    Raises:
        FedbiomedEnvironError: If the key does not exist in the configuration
    """
    try:
        _cfg_value = self._cfg.get(section, key)
    except configparser.Error:
        _msg = f"{ErrorNumbers.FB600.value}: no {section}/{key} in config file. Please recreate a new config file"
        logger.critical(_msg)
        raise FedbiomedEnvironError(_msg)

    return _cfg_value
info()
abstractmethod

Abstract method to return component information

Source code in fedbiomed/common/environ.py
@abstractmethod
def info(self):
    """Abstract method to return component information"""
parse_write_config_file(new=False)

Parses configuration file.

Create new config file if it is not existing.

Parameters:

Name Type Description Default
new bool

True if configuration file is not expected to exist

False
Raise

FedbiomedEnvironError: cannot read configuration file

Source code in fedbiomed/common/environ.py
def parse_write_config_file(self, new: bool = False):
    """Parses configuration file.

    Create new config file if it is not existing.

    Args:
        new: True if configuration file is not expected to exist

    Raise:
        FedbiomedEnvironError: cannot read configuration file
    """
    # Sets configuration file path
    self.set_config_file()

    # Parse configuration if it is existing
    if os.path.isfile(self._values["CONFIG_FILE"]) and not new:
        # get values from .ini file
        try:
            self._cfg.read(self._values["CONFIG_FILE"])
        except configparser.Error:
            _msg = ErrorNumbers.FB600.value + ": cannot read config file, check file permissions"
            logger.critical(_msg)
            raise FedbiomedEnvironError(_msg)

    # Create new configuration file
    else:
        # Create new config file
        self._set_component_specific_config_parameters()

        # Updates config file with MQTT configuration
        self._configure_mqtt()

        # Update config with secure aggregation parameters
        self._configure_secure_aggregation()

        # Writes config file to a file
        self._write_config_file()
set_config_file()

Sets configuration file

Source code in fedbiomed/common/environ.py
def set_config_file(self):
    """Sets configuration file """
    config_file = os.getenv('CONFIG_FILE')
    if config_file:
        if not os.path.isabs(config_file):
            config_file = os.path.join(self._values['CONFIG_DIR'],
                                       os.getenv('CONFIG_FILE'))
    else:
        config_file = self.default_config_file()

    self._values["CONFIG_FILE"] = config_file
setup_environment()

Final environment setup function

Source code in fedbiomed/common/environ.py
def setup_environment(self):
    """Final environment setup function """
    # Initialize common environment variables
    self._initialize_common_variables()

    # Parse config file or create if not existing
    self.parse_write_config_file()

    # Configuring network variables
    self._set_network_variables()

    # Initialize environment variables
    self._set_component_specific_variables()