Example #1
0
 def draw(self, idx):
     mask = self.mask.values[idx] if self.mask is not None else True
     if not mask:
         return " ".join("{:<15s}".format(""))
     topk_desc = " ".join(
         "{:.2f}:{:<10s}".format(p, trim(self.target_field.vocab.itos[t],
                                         10))
         for p, t in zip(self.topk_probs[idx], self.topk_targets[idx]))
     return topk_desc
Example #2
0
 def __repr__(self):
     return "Node: {:6d}, Value: {:<20s}, Tree: {:6d}, Conflits: {}\n\t#candidates: {:6d}, usages: {}".format(
         self.node_id,
         trim(self.value, 20),
         self.tree_id,
         self.conflicts,
         len(self.candidate_values),
         self.usages,
     )
Example #3
0
    def dumpAsString(self, depth=0, max_depth=None, length=None, label=None):
        max_depth = self.max_depth() if max_depth is None else max_depth
        length = len(self) if length is None else length

        s = "{}{:<{}d}{} {:<30s} {:<20s}".format(
            depth * " ",
            self.id,
            math.ceil(math.log10(min(2, length))) + 2,
            " " * (max_depth - depth),
            trim(self.type, 30),
            trim(self.value, 20),
        )
        if label is not None:
            s += label(self)
        s += "\n"

        for child in self.children:
            s += child.dumpAsString(depth + 1, max_depth, length, label)
        return s
Example #4
0
 def node_label(node: AstNode):
     pos = rules.nodes_to_rule.get(node.id, None)
     s = "{:<20s}".format(trim(sample.target[node.id], 20))
     if pos is not None:
         return s + "{:>6s} {:6d} {} {}".format(
             str(pos.decl_id),
             len(pos.candidate_values),
             pos.usages,
             [
                 self.value_index.dataset.VALUES.vocab.itos[c]
                 for c in pos.candidate_values
             ],
         )
     return s
Example #5
0
    def draw(self, idx):
        mask = self.mask.values[idx] if self.mask is not None else True
        if not mask:
            return "  {:10s}".format("")
        prediction_label = self.target_field.vocab.itos[self.predictions[idx]]

        def make_marker(label, ground_truth):
            if label == "reject":
                return bg("da_black") + " " + rs.bg
            return marker(label == ground_truth)

        return "{} {:10s}".format(
            make_marker(prediction_label, self.ground_truth[idx]),
            trim(prediction_label, 10),
        )
Example #6
0
    def dumpAsString(self):
        if not self.analyzer:
            return self.root.dumpAsString()

        per_node_edges = [{key: []
                           for key in AstTree.edge_types} for _ in self.nodes]
        for edge_type in AstTree.edge_types:
            edges = getattr(self, edge_type)()
            for src, tgt in edges:
                per_node_edges[src][edge_type].append(tgt)

        return (
            " ".join(AstTree.edge_types) + "\n" +
            self.root.dumpAsString(label=lambda node: " ".join(
                "{:<12s}".format(trim(per_node_edges[node.id][edge_type], 12))
                for edge_type in AstTree.edge_types)))
Example #7
0
    def make_rules(self, dataset: Dataset, trees: Dict[int, AstTree],
                   trees_num: Dict[int, AstTree]):
        num_valid = 0
        num_pos = 0
        index = RenameRulesIndex()
        for tree_id, tree in trees.items():
            sample = dataset.id_to_sample[tree_id]
            rules = RenameRulesForTree(tree_id, trees_num[tree_id])
            try:
                rules.update(self.compute_constant_replacement(tree_id, tree))
                rules.update(self.compute_variable_renaming(tree_id, tree))
                rules.update(
                    self.compute_prop_declaration_renaming(tree_id, tree))
                rules.update(
                    self.compute_property_assignment_renaming(tree_id, tree))
                index.add(rules)
            except Exception as e:
                print(
                    tree.root.dumpAsString(label=lambda node: "{:<20s}".format(
                        trim(sample.target[node.id], 20))))
                raise e

            num_valid += sum(sample.mask_valid)
            for valid, node in zip(sample.mask_valid, tree.nodes):
                if valid and node.id in rules.nodes_to_rule:
                    num_pos += 1

            def node_label(node: AstNode):
                pos = rules.nodes_to_rule.get(node.id, None)
                s = "{:<20s}".format(trim(sample.target[node.id], 20))
                if pos is not None:
                    return s + "{:>6s} {:6d} {} {}".format(
                        str(pos.decl_id),
                        len(pos.candidate_values),
                        pos.usages,
                        [
                            self.value_index.dataset.VALUES.vocab.itos[c]
                            for c in pos.candidate_values
                        ],
                    )
                return s

        print("{:7d}/{:7d} ({:.2f}%)".format(num_pos, num_valid,
                                             acc(num_pos, num_valid)))
        return index
Example #8
0
 def draw(self, idx):
     return "{:>14s}".format(
         trim(self.field.vocab.itos[self.values[idx]], 14))
Example #9
0
 def dumpFieldsAsString(self, fields):
     return self.dumpAsString(label=lambda node: " ".join("{:<60s}".format(
         trim(node.fields.get(field, " "), 60)) for field in fields))
Example #10
0
 def node_label(n: AstNode):
     valid_rules = [rule for rule in rules if rule.matches(n)]
     return "{:>20s} {}".format(
         trim(sample.target[n.id], 20),
         str([rule.name for rule in valid_rules]),
     )