def main(): ctrl = Controller() ctrl.load_dataset_from_file(DATASET_FILE_PATH) if MODE == MODE_TRAIN: from scripts.test_model import BHSMMTestModel from hassbrain_algorithm.models.hmm.bhmm_hp import BernoulliHMM_HandcraftedPriors from hassbrain_algorithm.models.tads import TADS if MODEL_CLASS == BHMM: hmm_model = BHMMTestModel(ctrl) elif MODEL_CLASS == BHSMM: hmm_model = BHSMMTestModel(ctrl) hmm_model.set_training_steps(50) elif MODEL_CLASS == BHMMPC: hmm_model = BernoulliHMM_HandcraftedPriors(ctrl) elif MODEL_CLASS == MCTADS: hmm_model = TADS(ctrl) else: raise ValueError ctrl.register_model(hmm_model, MODEL_NAME) # load domain knowledge if MODEL_CLASS == BHMMPC: path = '/home/cmeier/code/data/hassbrain/datasets/hass_chris_final/data/domain_knowledge.json' act_data, loc_data = load_domain_knowledge(path) ctrl.register_location_info(MODEL_NAME, loc_data) ctrl.register_activity_info(MODEL_NAME, act_data) # load model elif MODE == MODE_BENCH: ctrl.load_model(MODEL_FILE_PATH, MODEL_NAME) else: raise ValueError ctrl.register_benchmark(MODEL_NAME) ctrl.init_model_on_dataset(MODEL_NAME) if MODE == MODE_TRAIN: ctrl.register_loss_file_path(MD_LOSS_FILE_PATH, MODEL_NAME) ctrl.train_model(MODEL_NAME) ctrl.save_model(MODEL_FILE_PATH, MODEL_NAME) # bench the model reports = ctrl.bench_models() # save metrics ctrl.save_df_metrics_to_file(MODEL_NAME, MD_METRICS_FILE_PATH) ctrl.save_df_confusion(MODEL_NAME, MD_CONF_MAT_FILE_PATH) ctrl.save_df_act_dur_dists(MODEL_NAME, MD_ACT_DUR_DISTS_DF_FILE_PATH, DATA_ACT_DUR_DISTS_DF_FILE_PATH) ctrl.save_df_class_accs(MODEL_NAME, MD_CLASS_ACTS_FILE_PATH) # plots if MODE == MODE_TRAIN and MODEL_CLASS != MCTADS: ctrl.save_plot_trainloss(MODEL_NAME, MD_LOSS_IMG_FILE_PATH) ctrl.save_plot_inferred_states(MODEL_NAME, MD_INFST_IMG_FILE_PATH) ctrl.save_plot_act_dur_dists([MODEL_NAME], MD_ACT_DUR_DISTS_IMG_FILE_PATH)
def main(): ctrl = Controller() ctrl.load_dataset_from_file(DATASET_FILE_PATH) # load model model_name = 'bhmm' ctrl.load_model(MODEL_FILE_PATH, model_name) ctrl.create_model_agnostics(model_name) hm = ctrl.get_model(model_name)._obs_lbl_hashmap file_path = MODEL_FOLDER_PATH + '/' + MODEL_NAME + '.lime_sleeping.png' file_paths = [file_path] labels_to_explain=['sleeping'] import numpy as np # typical for activation pattern for sleeping raw_feature_sleeping = np.array( [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], dtype=bool) ch_feature_sleeping = np.array( [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=bool) lf_feature_sleeping = np.array( [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=bool) data = ctrl._dataset # type: _Dataset ctrl.plot_and_save_explanation( model_name, ch_feature_sleeping, labels_to_explain, file_paths )
def main(): ctrl = Controller() ctrl.load_dataset_from_file(DATASET_FILE_PATH) # load model model_name = 'bhmm' ctrl.load_model(MODEL_FILE_PATH, model_name) ctrl.create_model_agnostics(model_name) file_path = MODEL_FOLDER_PATH + '/' + MODEL_NAME + '.feature_importance.png' ctrl.save_plot_feature_importance(model_name, file_path)
def main(): ctrl = Controller() ctrl.load_dataset_from_file(DATASET_FILE_PATH) print() # load model ctrl.load_model(MODEL_FILE_PATH, 'bhmm') ctrl.load_model(MODEL_FILE_PATH2, 'bhsmm') state_sel = ['dental_care', 'outside_activity', 'learning'] #state_sel = ['dental_care', 'enter_home', 'kitchen_social_activity'] img_file_path = MODEL_FOLDER_PATH + '/' + 'comparision.png' ctrl.compare_dur_dists_and_save('bhmm', 'bhsmm', state_sel, img_file_path)
class TestPendigitsModel(unittest.TestCase): def setUp(self): self.ctrl = Controller() self.ctrl.load_dataset(Dataset.PENDIGITS) pd_model = ModelPendigits(self.ctrl, "test") self.ctrl.register_model(pd_model) self.dset = self.ctrl._dataset # type: DatasetPendigits self.pd_model = self.ctrl._model def test_init_hmms(self): self.ctrl.init_model_on_dataset() def test_save_hmms(self): self.ctrl.init_model_on_dataset() self.ctrl.save_model() def test_load_hmms(self): self.ctrl.load_model() # attention manually refresh reference self.pd_model = self.ctrl._model def test_train_hmms(self): self.ctrl.init_model_on_dataset() self.ctrl.train_model() def test_train_n_save_hmms(self): self.ctrl.init_model_on_dataset() self.ctrl.train_model() self.ctrl.save_model() def test_pre_bench_hmms(self): self.ctrl.load_model() self.pd_model = self.ctrl._model y_true, y_pred = self.pd_model.create_pred_act_seqs(self.dset) def test_bench_hmms(self): self.ctrl.load_model() self.pd_model = self.ctrl._model self.ctrl.register_benchmark() rep = self.ctrl.create_report(conf_matrix=True, accuracy=True, precision=True, recall=True, f1=True) print(rep) def test_generate_obs(self): self.ctrl.load_model() self.pd_model = self.ctrl._model self.ctrl.generate_observations() def test_generate_obs_n_plot(self): # numbers that were good in benchmark (desc): # [0,8,4,9,3,7] self.ctrl.load_model() ds = self.ctrl._dataset # type: DatasetPendigits pdmod = self.ctrl._model # type: ModelPendigits num = 1 zero_start = [8, 1, 1] one_start = [8, 2, 1, 1, 1] start_seq = one_start hmm = pdmod._model_dict[num] hmm.set_format_full(True) print(hmm) pdmod.select_number(num) seq = pdmod.generate_observations(start_seq) ds.plot_obs_seq(seq, num) def tearDown(self): pass
class TestController(unittest.TestCase): def setUp(self): # set of observations self.ctrl = Controller() # type: Controller #self.hmm_model = ModelHMM(self.ctrl) #self.hmm_model = ModelHMM_scaled(self.ctrl) #self.hmm_model = ModelHMM_log(self.ctrl) self.hmm_model = ModelHMM_log_scaled(self.ctrl) def tearDown(self): pass def test_ctrl_get_bench_metrics(self): hmm_model = self.hmm_model dk = Dataset.KASTEREN self.ctrl.load_dataset(dk) self.ctrl.register_model(hmm_model) self.ctrl.init_model_on_dataset() self.ctrl.register_benchmark() self.ctrl.register # render #dot = self.ctrl.render_model() #dot.render('test.gv', view=True) self.ctrl.train_model(True) print(self.ctrl.get_bench_metrics()) def test_ctrl_train_seqs(self): hmm_model = self.hmm_model dk = Dataset.KASTEREN self.ctrl.set_dataset(dk) self.ctrl.load_dataset() self.ctrl.register_model(hmm_model) self.ctrl.init_model_on_dataset() self.ctrl.register_benchmark() # render #dot = self.ctrl.render_model() #dot.render('test.gv', view=True) self.ctrl.train_model() report = self.ctrl.create_report(conf_matrix=True, accuracy=True, precision=True, recall=True, f1=True) print(self.ctrl._model) print(report) #self.ctrl.show_plot() def test_ctrl_presentation(self): hmm_model = self.hmm_model dk = Dataset.KASTEREN self.ctrl.load_dataset(dk) self.ctrl.register_model(hmm_model) self.ctrl.init_model_on_dataset() self.ctrl.register_benchmark() # render #dot = self.ctrl.render_model() #dot.render('test.gv', view=True) self.ctrl.train_model(True) report = self.ctrl.create_report(conf_matrix=True, accuracy=True, precision=True, recall=True, f1=True) print(self.ctrl._model) print(report) #self.ctrl.show_plot() # render #dot = self.ctrl.render_model() #dot.render('test.gv', view=True) def test_generate_visualization(self): hmm_model = self.hmm_model dk = Dataset.KASTEREN self.ctrl.set_dataset(dk) self.ctrl.load_dataset() self.ctrl.register_model(hmm_model) self.ctrl.init_model_on_dataset() hmm_model._hmm.set_format_full(True) print('state_label_hm: ', hmm_model._state_lbl_hashmap) print('state_label_rev_hm: ', hmm_model._state_lbl_rev_hashmap) self.ctrl.save_visualization_to_file( '/home/cmeier/code/tmp/visualization.png') def test_bench_train_loss(self): hmm_model = self.hmm_model dk = Dataset.KASTEREN self.ctrl.set_dataset(dk) self.ctrl.load_dataset() self.ctrl.register_model(hmm_model) self.ctrl.init_model_on_dataset() self.ctrl.register_benchmark() self.ctrl.register_loss_file_path( '/home/cmeier/code/tmp/kasteren/train_loss.log') self.ctrl.train_model() self.ctrl.save_model( '/home/cmeier/code/tmp/kasteren/kasteren_model.joblib') def test_bench_reports_conf_matrix(self): hmm_model = self.hmm_model dk = Dataset.KASTEREN self.ctrl.load_dataset(dk) self.ctrl.register_model(hmm_model) self.ctrl.init_model_on_dataset() self.ctrl.register_benchmark() self.ctrl.train_model() report = self.ctrl.create_report(conf_matrix=True) print(report) def test_bench_reports(self): hmm_model = self.hmm_model dk = Dataset.KASTEREN self.ctrl.load_dataset(dk) self.ctrl.register_model(hmm_model) self.ctrl.init_model_on_dataset() self.ctrl.register_benchmark() self.ctrl.train_model() report = self.ctrl.create_report(conf_matrix=True, accuracy=True, precision=True, recall=True, f1=True) print(report) #self.ctrl.show_plot() def test_bench_q_fct(self): hmm_model = self.hmm_model dk = Dataset.KASTEREN self.ctrl.load_dataset(dk) self.ctrl.register_model(hmm_model) self.ctrl.init_model_on_dataset() self.ctrl.register_benchmark() print(self.ctrl._model) # use dataset Kasteren and q_fct self.ctrl.train_model(True) report = self.ctrl.create_report() print(self.ctrl._model) print(report) #self.ctrl.show_plot() def test_om(self): #plt.figure(figsize=(10,6)) #self.pom.plot() hmm_model = self.hmm_model dk = Dataset.KASTEREN self.ctrl.load_dataset(dk) self.ctrl.register_model(hmm_model) self.ctrl.init_model_on_dataset() dot = self.ctrl.render_model() dot.render('test.gv', view=True) def test_train_model(self): hmm_model = self.hmm_model dk = Dataset.KASTEREN self.ctrl.set_dataset(dk) self.ctrl.load_dataset() self.ctrl.register_model(hmm_model) self.ctrl.init_model_on_dataset() #print(hmm_model._hmm) self.ctrl.train_model() print('#' * 200) print(hmm_model) hmm_model._hmm.set_str_exp(True) hmm_model._hmm.set_format_full(True) print(hmm_model) #print(hmm_model._hmm.verify_transition_matrix()) #print(hmm_model._hmm.verify_emission_matrix()) #self._bench._model.draw() #self.ctrl.register_benchmark() #report = self.ctrl.create_report(True, True, True, True, True) #print(report) #self._bench.show_plot() def test_save_model(self): hmm_model = self.hmm_model dk = Dataset.KASTEREN self.ctrl.load_dataset(dk) self.ctrl.register_model(hmm_model) self.ctrl.init_model_on_dataset() hmm_model._hmm.set_format_full(True) self.ctrl.train_model() self.ctrl.save_model() def test_init_model(self): hmm_model = self.hmm_model dk = Dataset.KASTEREN self.ctrl.set_dataset(dk) self.ctrl.load_dataset() self.ctrl.register_model(hmm_model) self.ctrl.init_model_on_dataset() hmm_model._hmm.set_format_full(True) print(self.ctrl._model) def test_load_model(self): hmm_model = self.hmm_model dk = Dataset.KASTEREN self.ctrl.set_dataset(dk) self.ctrl.load_dataset() self.ctrl.register_model(hmm_model) self.ctrl.load_model() print(self.ctrl._model)