# import system libraries # from collections import defaultdict import datetime as dt import numpy as np import pandas as pd import pickle import os import sys # import nedc libraries # import nedc_debug_tools as ndt import nedc_file_tools as nft import nedc_cov_tools as nct import nedc_ml_tools as ml # define the algorithm name # TEMPLATE = "TEMPLATE" # the parameter block for TEMPLATE looks like this: # # defaultdict(, { # 'name': 'TEMPLATE" # 'params': { # 'name': 'TEMPLATE', # 'key' : 0, # 'key2': 0, # 'key3': 0 # } # }) # Example definitions # TEMPLATE_PRM_KEY_NAME = ml.ALG_PRM_KEY_NAME TEMPLATE_PRM_KEY_PARAM = ml.ALG_PRM_KEY_PARAM TEMPLATE_MDL_KEY_NAME = ml.ALG_MDL_KEY_NAME TEMPLATE_MDL_KEY_MODEL = ml.ALG_MDL_KEY_MODEL class TEMPLATE(): """ Class: TEMPLATE description: This is a template class """ def __init__(self): """ method: constructor arguments: none return: none description: this is the default constructor for the class. """ # set the class name # TEMPLATE.__CLASS_NAME__ = self.__class__.__name__ # initialize variables for the parameter block and model # self.params_d = defaultdict(dict) self.model_d = None # initialize a parameter dictionary # self.params_d[TEMPLATE_PRM_KEY_NAME] = self.__class__.__name__ self.params_d[TEMPLATE_PRM_KEY_PARAM] = defaultdict(dict) # # end of method #-------------------------------------------------------------------------- # # computational methods: train/predict # #-------------------------------------------------------------------------- def train(self, data: ml.MLToolData, write_train_labels: bool, fname_train_labels: str, ): """ method: train arguments: data: a list of numpy float matrices of feature vectors write_train_labels: a boolean to wehther write the train data fname_train_labels: the filename of the train file return: model: a dictionary of covariance, means and priors score: f1 score description: none """ # display an informational message # if ml.dbgl_g == ndt.FULL: print("%s (line: %s) %s: training a model" % (ml.__FILE__, ndt.__LINE__, ndt.__NAME__)) # exit gracefully # return self.model_d, score # # end of method def predict(self, data: ml.MLToolData, model = None): """ method: predict arguments: data: a numpy float matrix of feature vectors (each row is a vector) model: an algorithm model (None = use the internal model) return: labels: a list of predicted labels description: none """ # display an informational message # if ml.dbgl_g == ndt.FULL: print("%s (line: %s) %s: entering predict" % (ml.__FILE__, ndt.__LINE__, ndt.__NAME__)) # check if model is none # if model is None: model = self.model_d return p_labels, posteriors # # end of method # # end of class