Example #1
0
def get_src_path(target):
    """Get the path of the folder under the source data directory.

    The folder name should be defined in the config file. See README and
    config.sample for reference.
    If the folder is not found, it will issue an error and quit.

    Args:
        target(str): The option name defined in '[folder]' section in the
            config file. The value of the option should be the folder name to
            read.

    Returns:
        The path of the target folder.
    """
    global config
    if not config.has_section('folder') or \
            not config.has_option('folder', 'src'):
        localizer_log.error("folder/src option is missing!")
    if not config.has_option('folder', target):
        localizer_log.error("{option} option is missing!".format(
            option='folder/' + str(target)))
    src_folder = config.get('folder', 'src')
    folder = config.get('folder', target)
    return get_joint_path(src_folder, folder)
Example #2
0
def get_meta_path(target):
    """Get the path of the file under the meta data directory.

    The file name should be defined in the config file. See README and
    config.sample for reference.
    If the file is not found, it will issue an error and quit.

    Args:
        target(str): The option name defined in '[meta]' section in the
            config file. The value of the option should be the folder name to
            read.

    Returns:
        The path of the target file.
    """
    global config
    if not config.has_section('folder') or not config.has_option(
            'folder', 'meta'):
        localizer_log.error("folder/meta option is missing!")
    if not config.has_section('meta') or not config.has_option('meta', target):
        localizer_log.error(
            "{option} option is missing!".format(option='meta/' + str(target)))
    meta_folder = config.get('folder', 'meta')
    f_name = config.get('meta', target)
    return get_joint_path(meta_folder, f_name)
Example #3
0
def load(filename):
    """Load/Reload config file which is organized in the .ini format and
    contains the global settings. See README and config.sample for reference.

    Args:
        filename(str): The config file to read.

    Returns:
        None
    """
    global config
    if os.path.isfile(filename):
        config.read(filename)
    else:
        localizer_log.error(
            "Config file {fname} not found".format(fname=filename))
Example #4
0
def get_plugin(plugin_type, klass_full):
    """Get a plugin class by reflection.

    Plugins are put under plugins. Different types are put in different folder.
    See README for more information.

    Args:
        plugin_type(str): Name of the type of the plugin.
        klass_name(str): Module and class name of the target, in the form of
            ${Module Name}.${Class Name}

    Returns:
        The target class of the plugin.
    """
    SUPER_CLASSES = {
        'predictor': 'general_predictor.GeneralPredictor',
        'exp_filter': 'general_expfilter.GeneralExpFilter',
        'exp_tag': 'general_exptag.GeneralExpTag',
        'denoiser': 'general_denoiser.GeneralDenoiser',
        'kpi_filter': 'general_kpifilter.GeneralKPIFilter',
        'rankingtime': 'general_selector.GeneralSelector',
        'ranker': 'general_ranker.GeneralRanker',
        'oracle': 'general_oracle.GeneralOracle'
    }
    PLUGIN_DIR = 'plugins'

    klass_terms = klass_full.strip().split('.')
    module_name = '.'.join(klass_terms[:-1])
    klass_name = klass_terms[-1]
    plugin_klass = get_klass('.'.join([PLUGIN_DIR, plugin_type, module_name]),
                             klass_name)

    if plugin_type in SUPER_CLASSES:
        superclass_full = SUPER_CLASSES[plugin_type]
        superklass_terms = superclass_full.split('.')
        superklass_module = '.'.join(superklass_terms[:-1])
        superklass_name = superklass_terms[-1]
        super_klass = get_klass(
            '.'.join([PLUGIN_DIR, plugin_type, superklass_module]),
            superklass_name)
        if not issubclass(plugin_klass, super_klass):
            localizer_log.error(
                "{subclass} should inherit {superclass}".format(
                    subclass=klass_full, superclass=superclass_full))

    return plugin_klass
Example #5
0
    def __init__(self, exp_dir, exp_name, exp_id):
        self.exp_info = {}
        self.time_list = []
        self.exp_data = []
        self.exp_data_inc = []
        self.prediction_seq = []
        self.rankings = {}

        self.exp_info['id'] = exp_id
        self.exp_info['full_name'] = exp_name

        if not os.path.isdir(exp_dir):
            localizer_log.error('Initializting Oberservation for ' + exp_dir +
                                ': Folder not found')
            return

        self.add_info(exp_name)
        self.add_data(exp_dir)
Example #6
0
def fmt_folder(folder):
    """Format the experiment in a folder.

    Args:
        folder(str): The name of the folder to be format, which should contains
            the folders, each folder represents an experiemnt.

    Returns:
        A list of PredData containing the data.
    """
    result = []

    folder_dir = localizer_config.get_src_path(folder)
    if not os.path.isdir(folder_dir):
        localizer_log.error("folder " + folder + ' not exist. Abort.')

    experiments = [
        x for x in os.listdir(folder_dir)
        if os.path.isdir(os.path.join(folder_dir, x))
    ]

    list_size = len(kpi_info.kpi_list)
    import util.runtime as runtime
    for exp_name in experiments:
        exp = runtime.find_exp_by_name(exp_name)
        if not exp:
            localizer_log.warning(
                "Experiment {exp} not found".format(exp=exp_name))
            continue
        exp_dir = os.path.join(folder_dir, exp_name)
        files = [x for x in os.listdir(exp_dir) if x.endswith('.txt')]
        timeset = [
            int(filename.strip().replace('.txt', '')) for filename in files
        ]

        for t in sorted(timeset):
            lst = [0 for i in range(list_size)]
            with open(os.path.join(exp_dir, str(t) + '.txt')) as f:
                for l in f:
                    idx = kpi_info.get_index(l.strip())
                    lst[idx] = 1
            result.append(PredData(exp, str(t), lst))

    return result
Example #7
0
def get_joint_path(folder, target):
    """Get the file/folder joint by the two parts(a folder and a target).

    If the file/folder is not found, it will issue an error and quit.

    Args:
        folder(str): The name of the file/folder.

    Returns:
        The path of the target file/folder.
    """
    if not folder or not target:
        localizer_log.error("Parameter cannot be empty!")

    path = os.path.join(folder, target)
    if not os.path.exists(path):
        localizer_log.error(
            "Path {fname} not found! Abort.".format(fname=target))
    return path
Example #8
0
def get_folder(target):
    """Get the path of the folder under root directory.

    The folder name should be defined in the config file. See README and
    config.sample for reference.

    Args:
        target(str): The option name defined in '[folder]' section in the
            config file. The value of the option should be the folder name to
            read.

    Returns:
        The path of the target folder.
    """
    global config
    if not config.has_section('folder') or \
            not config.has_option('folder', target):
        localizer_log.error("folder/{folder_name} option is missing!".format(
            folder_name=target))
    return config.get('folder', target)
Example #9
0
def get_scapidata_path(target):
    """Get the path of the folder under the SCAPI original data directory.

    The folder name should be defined in the config file. See README and
    config.sample for reference.
    If the folder is not found, it will issue an error and quit.

    Args:
        target(str): The option name defined in '[folder]' section in the
            config file. The value of the option should be the folder name to
            read.

    Returns:
        The path of the target folder.
    """
    global config
    if not config.has_section('folder') or \
            not config.has_option('folder', 'preprocess'):
        localizer_log.error("folder/preprocess option is missing!")
    scapi_folder = config.get('folder', 'preprocess')
    return get_joint_path(scapi_folder, target)
Example #10
0
def get_dst_path(target):
    """Get the path of the folder under the destination data directory.

    The folder name should be defined in the config file. See README and
    config.sample for reference.

    Args:
        target(str): The option name defined in '[folder]' section in the
            config file. The value of the option should be the folder name to
            read.

    Returns:
        The path of the target folder.
    """
    global config
    if not config.has_section('folder') or not config.has_option(
            'folder', 'dst'):
        localizer_log.error("folder/dst option is missing!")
    dst = config.get('folder', 'dst')

    return os.path.join(dst, target)
Example #11
0
def add_folder(folder, group):
    """Read all experiments in a folder.

    The folder must contain experiment subfolders which are named in the format
    of: ${experiment id}-${IP of the injected node}-${fault type}_${hostname of
    the injected fault}_${other parameters}

    Args:
        folder(str): The folder to read from.
        group(dict): The target global variable to put the result.

    Returns:
        None
    """
    if not os.path.exists(folder):
        localizer_log.error(
            "Experiments Folder {folder_name} not exist!".format(
                folder_name=folder))

    for f in [
            x for x in os.listdir(folder)
            if os.path.isdir(os.path.join(folder, x))
    ]:
        add_exp(os.path.join(folder, f), group)
Example #12
0
import json
from collections import OrderedDict
import util.localizer_config as localizer_config
import util.localizer_log as localizer_log

import weka.core.converters as converters
from weka.classifiers import Classifier
from weka.classifiers import Evaluation
from weka.core.classes import Random

if os.path.isfile('weka.json'):
    with open('weka.json') as f:
        try:
            __OPTIONS = json.load(f)
        except:
            localizer_log.error('weka.json not in right format.')
            __OPTIONS = []
else:
    localizer_log.error('weka.json not found.')
__classifiers = OrderedDict(__OPTIONS)
__predictors = {}


def load_model(model_cache_file_name):
    """ Loads the cached classifier model and writes it to the __predictors global variable
    :param model_cache_file_name: path of the classifier model file
    :return: N/A
    """
    global __predictors
    # __predictors[classifier_name], data = localizer_config.load_model(classifier_name)
    path = os.path.join('caches', 'model')