Ejemplo n.º 1
0
 def setup_net(self):
     if not self.setup:
         self.setup = True
         self.net = Net(*SimpleParser(open(self.sisc_file)).parse(), **self.options)
         for c,i in enumerate(self.net.inputs):
             self.net.inputs[i] = self.inputs[c] == 1
         for c,i in enumerate(self.net.outputs):
             self.net.outputs[i] = self.outputs[c] == 1
         self.components = TwoWayDict(dict(enumerate(self.net.gates.keys())))
Ejemplo n.º 2
0
 def __init__(self, sisc_file, inputs, mutated_gates, **options):
     super(IscasOracleOld, self).__init__(None, [], [], **options)
     self.sisc_file = sisc_file
     self.mutated_gates = mutated_gates
     self.net = Net(*SimpleParser(open(sisc_file)).parse(), **options)
     for c,i in enumerate(self.net.inputs):
         self.net.inputs[i] = inputs[c] == 1
     orig_gates = self.net.mutate_net(mutated_gates)
     self.net.calculate_outputs()
     self.net.mutate_net(orig_gates)
     self.comp_calls = 0
     self.check_calls = 0
     self.comp_time = 0
     self.check_time = 0
     self.scs = set()
     self.components = TwoWayDict(dict(enumerate(self.net.gates.keys())))
Ejemplo n.º 3
0
def generate_iscas_faults(file, num_mutated_gates):

    net = Net(*SimpleParser(open(file)).parse())
    net.generate_random_inputs()
    net.calculate_outputs()
    original_outputs = dict(net.outputs)
    tries = 0
    while tries < 100:
        gates = random.sample(net.gates.values(), num_mutated_gates)
        mutated_gates = []
        tries += 1
        for g in gates:
            m = Gate(*g)
            m.type = GATE_MUTATION[m.type]
            mutated_gates.append(m)

        original_gates = net.mutate_net(mutated_gates)
        net.reset_outputs()
        net.calculate_outputs()
        net.mutate_net(original_gates)
        if net.outputs != original_outputs:
            return [file, net.input_values_compact(), mutated_gates]
    print "Failed to generate fault for " + file + " within 100 tries."
    net.finished()
Ejemplo n.º 4
0
from arc_standard import ArcStandard
from arc_eager import ArcEager
from parser import SimpleParser
import feature_extractor as fx
from train import main

if __name__ == '__main__':
    arcsys = ArcEager()
    parser = SimpleParser(arcsys, fx.rich, arcsys.dynamic_oracle)
    main(arcsys, parser, True)
Ejemplo n.º 5
0
from arc_standard import ArcStandard
from arc_eager import ArcEager
from parser import SimpleParser
import feature_extractor as fx
from train import main

if __name__ == '__main__':
    arcsys = ArcStandard()
    parser = SimpleParser(arcsys, fx.baseline, arcsys.static_oracle)
    main(arcsys, parser, False)
def recursive_feature_elimination():
    model = LogisticRegression()
    rfe = RFE(model, 3)
    fit = rfe.fit(X, Y)
    print("Num Features: ", fit.n_features_)
    print("Selected Features: ", fit.support_)
    print("Feature Ranking: ", fit.ranking_)


def principal_component_analysis():
    # feature extraction
    pca = PCA(n_components=3)
    fit = pca.fit(X)
    # summarize components
    print("Explained Variance: %s") % fit.explained_variance_ratio_
    print(fit.components_)


def feature_importance():
    model = ExtraTreesClassifier()
    model.fit(X, Y)
    print(model.feature_importances_)


X, Y = SimpleParser('AM_RevisoesHoteisCaldas.csv').get_data()

univariate_selection()
# recursive_feature_elimination()
# principal_component_analysis()
# feature_importance()
Ejemplo n.º 7
0
from sklearn import tree
from sklearn import neighbors
from parser import Parser, SimpleParser

SP = SimpleParser('AM_RevisoesHoteisCaldas.csv')
P = Parser('AM_RevisoesHoteisCaldas.csv')

interval = 50


def slice(array, a, b):
    return array[a:b]


for parser in [P, SP]:
    A = [0, 0]
    X, Y = parser.get_data()
    for z in range(0, 500, interval):
        x = X[0:z] + X[z + interval:]
        y = Y[0:z] + Y[z + interval:]

        clf = tree.DecisionTreeClassifier()
        knn = neighbors.KNeighborsClassifier(n_neighbors=50)

        clf = clf.fit(x, y)
        knn.fit(x, y)
        a = [0, 0]
        check = slice(X, z, z + interval), slice(Y, z, z + interval)
        for data in zip(check[0], check[1]):
            trc = clf.predict([data[0]])
            knc = knn.predict([data[0]])
Ejemplo n.º 8
0
def generate_independent_faults(file, num_gates_to_mutate, exclude_gates=[]):
    net = Net(*SimpleParser(open(file)).parse())
    if len(net.outputs) < num_gates_to_mutate:
        net.finished()
        raise Exception(
            "Cannot create %d independent mutations for net with %d outputs" %
            (num_gates_to_mutate, len(net.outputs)))
    try_num = 1
    log.info("Trying to generate %d independent fault(s) for %s" %
             (num_gates_to_mutate, file))
    while try_num < 2 * (2**len(
            net.inputs
    )):  # try about two times the number of possible input combinations
        try_num += 1
        net.generate_random_inputs()
        log.debug("=== Trying input %s" % net.inputs)
        net.reset_outputs()  # unconstrain all outputs at the beginning
        net.calculate_outputs()
        previous_outputs = dict(net.outputs)
        log.debug("Original Outputs: %s" % previous_outputs)
        net.reset_outputs()
        gates_to_try = filter(lambda g: g.output not in exclude_gates,
                              net.gates.values())
        mutated_gates = []
        immutable_outputs = set()
        while len(mutated_gates) < num_gates_to_mutate:
            while gates_to_try:
                log.debug("Previous output: %s" % previous_outputs)
                random.shuffle(gates_to_try)
                gate = gates_to_try.pop()
                mutated_gate = Gate(*gate)
                mutated_gate.type = GATE_MUTATION[mutated_gate.type]
                log.debug("Mutating %s to %s" % (gate, mutated_gate))
                net.mutate_net([mutated_gate])
                if not net.calculate_outputs():
                    log.debug("Unsatisfiable, undoing mutation.")
                    net.mutate_net([gate])  # restore original gate
                    break
                new_outputs = dict(net.outputs)
                log.debug("New output: %s" % new_outputs)
                changed_outputs = set([
                    g for g in new_outputs
                    if new_outputs[g] != previous_outputs[g]
                ])
                if changed_outputs:
                    log.debug("found mutation")
                    immutable_outputs |= changed_outputs
                    log.debug("Immutable outputs: %s" % immutable_outputs)
                    mutated_gates.append(mutated_gate)
                    previous_outputs = new_outputs
                    for o in net.outputs:  # unconstrain all outputs that didn't change
                        if o not in immutable_outputs:
                            net.outputs[o] = None
                    break
                else:
                    log.debug("undoing mutation, trying next")
                    net.mutate_net([gate])
                    for o in net.outputs:  # unconstrain all outputs that didn't change (in the last step)
                        if o not in immutable_outputs:
                            net.outputs[o] = None
            # end while gates_to_try
            if not gates_to_try and len(mutated_gates) < num_gates_to_mutate:
                log.debug("no more gates to try :(")
                break
        # end while len(mutated_gates) < num_gates_to_mutate:
        if not len(mutated_gates) < num_gates_to_mutate:
            break

    net.finished()
    # end while try_num < 2*(2**len(net.inputs)):
    if len(mutated_gates) < num_gates_to_mutate:
        return None
    return [file, net.input_values_compact(), mutated_gates]