def test_DTA_membership(self): s = "and(or(and(true()false())true())and(true()or(false()true())))" tree = Tree.parse(s) tree.write_graphviz("test.txt") self.assertEqual(self.DTA.membership(tree), Result.accept) self.assertEqual(self.DTA.membership(Tree.parse("not(false())")), Result.accept)
def cut_discriminator(self, trees, value): if value < 0 or value > 100: Tools.print_log_line('Value must debe ser un valor entre 0 y 100', logging.ERROR) else: return Tree.sum_tree_list(trees, 'expan') * ((100 - value) / 100.0) return 0
def test_duta_membership(self): s = "and(or(and(true()false())true())and(true()or(false()true()true()false())))" tree = Tree.parse(s) # print tree # print self.drta.membership(tree) self.assertEqual(self.duta.membership(tree), Result.accept)
def apply_model(self, plot: Plot, years: int, value: float): Tools.print_log_line('Aplicando corta por el menor', logging.INFO) accumulator = 0 cut_all_the_rest = False new_plot = Plot() new_plot.clone(plot) order_criteria = OrderCriteria(ASC) order_criteria.add_criteria('dbh') search_criteria = SearchCriteria() search_criteria.add_criteria('status', None, EQUAL) trees = Tree.get_sord_and_order_tree_list(plot.trees, search_criteria=search_criteria, order_criteria=order_criteria) cut_discriminator = self.type.cut_discriminator(trees, value) for tree in trees: accumulator += self.type.accumulator(tree) if not cut_all_the_rest: new_tree = Tree() new_tree.clone(tree) if accumulator >= cut_discriminator: cut_all_the_rest = True new_expan = self.type.compute_expan(tree, accumulator, cut_discriminator) if new_expan <= 0: new_tree.add_value('expan', new_expan) new_tree.add_value('status', 'C') else: cut_tree = Tree() cut_tree.clone(tree) cut_tree.add_value('status', 'C') cut_tree.add_value('expan', new_expan) if cut_tree.expan > 0: new_plot.add_tree(cut_tree) new_tree.sub_value('expan', new_expan) new_tree.add_value('status', None) new_plot.add_tree(new_tree) else: new_tree = Tree() new_tree.clone(tree) new_tree.add_value('status', 'C') new_plot.add_tree(new_tree) return new_plot
def generate_y_sketch(y): y = str(y) if not y: return y return Tree(y).get_compact_form(True)
def generate_y_compact(y): y = str(y) if not y: return y return Tree(y).get_compact_form()
def apply_model(self, plot: Plot, years: int, value: float): Tools.print_log_line('Aplicando cut down sistemática', logging.INFO) new_plot = Plot() new_plot.clone(plot) order_criteria = OrderCriteria() order_criteria.add_criteria('dbh') search_criteria = SearchCriteria() search_criteria.add_criteria('status', None, EQUAL) trees = Tree.get_sord_and_order_tree_list(plot.trees, search_criteria=search_criteria, order_criteria=order_criteria) for tree in trees: new_tree = Tree() new_tree.clone(tree) new_tree.add_value('expan', tree.expan * ((100 - value) / 100)) new_tree.add_value('status', None) new_plot.add_tree(new_tree) cut_tree = Tree() cut_tree.clone(tree) cut_tree.add_value('status', 'C') cut_tree.add_value('expan', tree.expan - new_tree.expan) if cut_tree.expan > 0: new_plot.add_tree(cut_tree) return new_plot
def evaluate_predictions(gold_filename: str, mode: str) -> Dict: instance_count: int = 0 exact_matches: int = 0 non_terminal_matches: int = 0 terminal_matches: int = 0 invalid_preds: float = 0 labeled_bracketing_scores = Calculator(strict=False) tree_labeled_bracketing_scores = Calculator(strict=True) gold_list = [] pred_list = [] exact_match_list = [] tree_depth_list = [] tree_size_list = [] with open(gold_filename) as gold_file: # for gold_line, pred_line in zip_longest(gold_file, pred_file): for gold_line in gold_file: try: gold_lines = gold_line.strip().split("\t") gold_line = gold_lines[1] pred_line = gold_lines[2] #pred_line.strip() except AttributeError: print("WARNING: check format and length of files") quit() (non_terminal_match, terminal_match) = comp_gold_and_pred(gold_line, pred_line) non_terminal_matches += non_terminal_match terminal_matches += terminal_match try: gold_tree = Tree(gold_line) instance_count += 1 except ValueError: print("FATAL: found invalid line in gold file:", gold_line) quit() try: pred_tree = Tree(pred_line) labeled_bracketing_scores.add_instance(gold_tree, pred_tree) tree_labeled_bracketing_scores.add_instance( gold_tree, pred_tree) except ValueError: # print("WARNING: found invalid line in pred file:", pred_line) invalid_preds += 1 # (non_terminal_match, terminal_match) = comp_gold_and_pred(gold_line, pred_line) # non_terminal_matches += non_terminal_match # terminal_matches += terminal_match labeled_bracketing_scores.add_instance(gold_tree) tree_labeled_bracketing_scores.add_instance(gold_tree) continue if str(gold_tree) == str(pred_tree): exact_matches += 1 exact_match_list.append(1) else: exact_match_list.append(0) if mode == 'sketch': tree_depth_list.append(gold_tree.root.get_depth() - 1) else: tree_depth_list.append(gold_tree.root.get_depth() - 2) tree_size_list.append(gold_tree.root.get_size()) gold_list.append(gold_line) pred_list.append(pred_line) # if gold_tree.get_compact_form(sketch=True) == pred_tree.get_compact_form(sketch=True): # non_terminal_matches += 1 # # if gold_tree.root.get_str_token_spans() == pred_tree.root.get_str_token_spans(): # terminal_matches += 1 result_file = mode + '.final_result.tsv' result = { 'gold': gold_list, 'pred': pred_line, 'match': exact_match_list, 'size': tree_size_list, 'depth': tree_depth_list } result_df = pd.DataFrame.from_dict(result) result_df.to_csv(result_file, sep='\t', header=True, index=False) exact_match_fraction: float = (exact_matches / instance_count) if instance_count else 0 tree_validity_fraction: float = ( 1 - (invalid_preds / instance_count)) if instance_count else 0 # print("terminal list: {}".format(terminal_list)) return { "instance_count": instance_count, "invalid_preds": invalid_preds, "exact_match_count": exact_matches, "exact_match_accu": exact_match_fraction, "non_terminal_match_Error_count": (instance_count - non_terminal_matches), "terminal_match_Error_count": (instance_count - terminal_matches), "total_Error_count": (instance_count - exact_matches) # "labeled_bracketing_scores": # labeled_bracketing_scores.get_metrics(), # "tree_labeled_bracketing_scores": # tree_labeled_bracketing_scores.get_metrics(), # "tree_validity": # tree_validity_fraction }