import json import logging import os from json import JSONDecodeError from logger import logger from jsonschema import validate, ValidationError logger.initialise_logger("config-reader", log_level=logging.INFO) module_logger = logging.getLogger('config-reader') def load_schema(filepath="./config/config.schema"): """ Load the schema. :param filepath: Location of the schema file. :return: Schema. """ # Preconditions assert type(filepath) == str # Read the schema return read_json_config(filepath) def read_json_config(path): """ Read the JSON-formatted config file.
import logging from etl.csv_reader import DelimitedSource from logger import logger logger.initialise_logger("timestep-names", log_level=logging.INFO) module_logger = logging.getLogger('timestep-names') class TimestepNames(object): # Name of the field in the CSV file for the timestep TIMESTEP_FIELD = "timestep" # Name of the field in the CSV file for the name of the timestep TIMESTEP_NAME_FIELD = "name" def __init__(self): """ Initialise the object to hold the names of the timesteps. """ self.timestep_to_name = {} def read_from_file(self, filepath, delimiter, encapsulator, encoding): """ Read the names of the timesteps from a CSV file. :param filepath: Location of the timestep data in CSV format. :param delimiter: Delimiter used in the CSV file. :param encapsulator: Encapsulator used in the CSV file. :param encoding: Encoding used.
import logging from etl.csv_reader import DelimitedSource from logger import logger logger.initialise_logger("infections", log_level=logging.INFO) module_logger = logging.getLogger('infections') class Infections(object): # Name of the field in the CSV file for the individual's ID INDIVIDUAL_ID_FIELD_NAME = 'individual_id' # Name of the field in the CSV file for whether the individual is infected INFECTION_STRAIN_FIELD_NAME = 'infection_strain' # Value for no infection NO_INFECTION = 'None' def __init__(self): """ Initialise the object that holds the infections for a given timestep. """ # Timestep for the data self.timestep = None # Dictionary of individual ID (key) to whether they are infected or not (value) self.individual_to_infection = {}
import logging from config.reader import read_json_config, validate_config from logger import logger # Initialise the module logger logger.initialise_logger("perform-inference", log_level=logging.INFO) module_logger = logging.getLogger('perform-inference') def run_inference(config_path): """ Perform inference. :param config_path: Location of the JSON config path. :return: """ # Preconditions assert type(config_path) == str # Read the JSON config module_logger.info("Reading config from: %s" % config_path) config = read_json_config(config_path) # Check the config is valid validates = validate_config(config) if not validates: module_logger.error("Config is invalid") exit(-1)
import logging from etl.csv_reader import DelimitedSource from logger import logger logger.initialise_logger("individuals", log_level=logging.INFO) module_logger = logging.getLogger('individuals') class Individuals(object): # Name of the field used for an individual's unique identifier ID_FIELD_NAME = 'id' def __init__(self): """ Initialise the object that holds the individuals. """ # Timestep for the data self.timestep = None # Dictionary of individuals self.individual_to_attributes = {} # Set of expected attributes for each individual self.expected_attributes = set() def update_individual(self, individual): """ Update an individual.
# -*- coding: utf-8 -*- import csv import logging import os from logger import logger # Initialise the module logger logger.initialise_logger("etl", log_level=logging.INFO) module_logger = logging.getLogger('etl') class DelimitedSource(object): # Maximum number of characters in a single field FIELD_LIMIT = 10000000 def __init__(self, filepath, delimiter, encapsulator, encoding): self.filepath = filepath self.delimiter = delimiter self.encapsulator = encapsulator self.encoding = encoding module_logger.info("Initialising CSV reader to read: %s" % self.filepath) module_logger.info("Delimiter set to: %s" % delimiter) module_logger.info("Encapsulator set to: %s" % encapsulator) module_logger.info("Encoding set to: %s " % self.encoding) def read(self): # Preconditions