Beispiel #1
0
def runOneWord():
	(tag2tag, tag2word) = buildDataModels('data/at_unigram.json', 'data/at_tag_to_word.json', ["", ""])

	devtestdata = ""
	with open('devtest.txt') as f:
		devtestdata = f.read()

	dataarray = []
	tagarray = []
	c = 0
	for word in devtestdata.split():
		ws = word.split('_')
		w = ws[0]
		t = ws[1]
		dataarray.append(w)
		tagarray.append(t)
		c+=1
		if(c > 5):
			break
	devtestdata = ' '.join(dataarray)

	v = Viterbi(tag2tag, tag2word)
	singlePath = v.run(devtestdata)

	confusionMatrix = ConfusionMatrix(tag2tag)
	confusionMatrix.build(tagarray, singlePath)
	results = confusionMatrix.getResults()

	def addFormatedMatrix(res):
		l = []
		t2tkeys = [x for x in tag2tag.keys()]
		l.append("".ljust(6))
		for key in t2tkeys:
			l.append(("%s" % (key, )).ljust(6))
		l.append('\n')
		c = 0
		for row in res['confusion_matrix']:
			l.append((t2tkeys[c]).ljust(6))
			for col in row:
				if(col > 0):
					l.append(("%d" % (col, )).ljust(6))
				else:
					l.append("".ljust(6))
			l.append("\n")
			c+=1
		res['formated_matrix'] = ''.join(l)
		return res

	results = addFormatedMatrix(results)

	with open('results_one.txt', 'w') as f:
		f.write("Path: %s" % (str([x.ljust(5) for x in singlePath]), ))
		f.write('\r\n')
		f.write("      %s" % (str([x.ljust(5) for x in results['results_matrix']]), ))
		f.write('\r\n')
		f.write("      %s" % (str([x.ljust(5) for x in tagarray]), ))
		f.write('\r\n')
		f.write("Tagging Accuracy: %f" % (results['accuracy'], ))
		f.write('\r\n')
		f.write(results['formated_matrix'])
Beispiel #2
0
    def train_epoch(self, epoch):
        if self.train_problems_loader is None:
            self.train_problems_loader = init_problems_loader(
                self.opts.train_dir)

        epoch_start = time.clock()

        epoch_train_cost = 0.0
        epoch_train_mat = ConfusionMatrix()

        train_problems, train_filename = self.train_problems_loader.get_next()
        for problem in train_problems:
            d = self.build_feed_dict(problem)
            _, logits, cost = self.sess.run(
                [self.apply_gradients, self.logits, self.cost], feed_dict=d)
            epoch_train_cost += cost
            epoch_train_mat.update(problem.is_sat, logits > 0)

        epoch_train_cost /= len(train_problems)
        epoch_train_mat = epoch_train_mat.get_percentages()
        epoch_end = time.clock()

        learning_rate = self.sess.run(self.learning_rate)
        self.save(epoch)

        return (train_filename, epoch_train_cost, epoch_train_mat,
                learning_rate, epoch_end - epoch_start)
Beispiel #3
0
    def test(self, test_data_dir):
        test_problems_loader = init_problems_loader(test_data_dir)
        results = []

        while test_problems_loader.has_next():
            test_problems, test_filename = test_problems_loader.get_next()

            epoch_test_cost = 0.0
            epoch_test_mat = ConfusionMatrix()

            for problem in test_problems:
                d = self.build_feed_dict(problem)
                logits, cost = self.sess.run([self.logits, self.cost], feed_dict=d)
                epoch_test_cost += cost
                epoch_test_mat.update(problem.is_sat, logits > 0)

            epoch_test_cost /= len(test_problems)
            epoch_test_mat = epoch_test_mat.get_percentages()

            results.append((test_filename, epoch_test_cost, epoch_test_mat))

        return results
Beispiel #4
0
    def train_epoch(self, epoch):
        if self.train_problems_loader is None:
            self.train_problems_loader = init_problems_loader(self.opts.train_dir)
        epoch_start = time.clock()
        epoch_train_cost = 0.0
        epoch_train_mat = ConfusionMatrix()
        train_problems, train_filename = self.train_problems_loader.get_next()

        for problem in train_problems:
            d = self.build_feed_dict(problem)
            self.batchnum = problem.n_vars//len(problem.is_sat)
            acc,feature_map_out, _, logits, cost, image, issat, XX = self.sess.run(
                 [self.accuracy, self.all_votes, self.apply_gradients, self.logits, self.cost, self.image, self.is_sat, self.merged], feed_dict=d)
            epoch_train_cost += cost
            epoch_train_mat.update(problem.is_sat, logits > 0)
            # print(acc)
        epoch_train_cost /= len(train_problems)
        epoch_train_mat = epoch_train_mat.get_percentages()
        epoch_end = time.clock()
        learning_rate = self.sess.run(self.learning_rate)
        self.save(epoch)
        self.train_writer.add_summary(XX, epoch)
        return (train_filename, epoch_train_cost, epoch_train_mat, learning_rate, epoch_end - epoch_start)
def get_individual_interstim_sensitivities_for_participants(
    participants: List[Participant],
    sector=None,
    epsilon=0.001,
):
    all_sensitivities = []
    for part in participants:
        confusion_matrix = ConfusionMatrix.of_indices(
            part.get_responses(sector=sector))
        participant_sensitivities = get_interstim_sensitivities_for_confusion_matrix(
            confusion_matrix,
            epsilon,
        )
        all_sensitivities += participant_sensitivities
    return all_sensitivities
Beispiel #6
0
    def train_epoch(self, epoch):
        if self.train_problems_loader is None:
            self.train_problems_loader = init_problems_loader(
                self.opts.train_dir)

        epoch_start = time.clock()

        epoch_train_cost = 0.0
        # accuracy_by_var = []
        # accuracy_by_problem = []
        epoch_train_mat = ConfusionMatrix()

        train_problems, train_filename = self.train_problems_loader.get_next()
        for problem in train_problems:
            d = self.build_feed_dict(problem)
            A_policy, L_policy = self.sess.run([self.A_policy, self.L_policy],
                                               feed_dict=d)
            A_move, L_move = self.sample_moves(A_policy, L_policy)
            ## TO HERE; need to evaluate is_sat
            ## serious concern: by random, only 4 percent of games are sat (the base is biased!!!)

            _, logits, cost = self.sess.run(
                [self.apply_gradients, self.logits, self.cost], feed_dict=d)
            epoch_train_cost += cost
            (av, ap) = self.accuracy(np.array(logits),
                                     np.array(problem.labels))
            accuracy_by_var.append(av)
            accuracy_by_problem.append(ap)
            # epoch_train_mat.update(problem.is_sat, logits > 0)

        epoch_train_cost /= len(train_problems)
        av_ac_by_var = np.mean(accuracy_by_var)
        av_ac_by_pro = np.mean(accuracy_by_problem)
        # epoch_train_mat = epoch_train_mat.get_percentages()
        epoch_end = time.clock()

        learning_rate = self.sess.run(self.learning_rate)
        self.save(epoch)

        return (train_filename, epoch_train_cost, av_ac_by_var, av_ac_by_pro,
                learning_rate, epoch_end - epoch_start)
Beispiel #7
0
from confusion import ConfusionMatrix
from sensitivity import get_interstim_sensitivities_for_confusion_matrix, log_fit_sensitivities


def parse_str_example_data(str_data):
    return np.array(
        [[int(n_str) for n_str in row.split("\t")] for row in str_data.strip().splitlines()]
    ).T


cm_1 = ConfusionMatrix(
    parse_str_example_data(
        """
        39	4	1	0	0
        135	107	26	3	0
        24	87	146	108	15
        1	2	27	81	98
        0	0	0	8	87
        """
    )
)

cm_2 = ConfusionMatrix(
    parse_str_example_data(
        """
        7	4	0	0	0
        50	32	13	3	0
        37	58	70	55	8
        1	2	12	36	43
        0	0	1	5	45
        """
columns = [
    "participant_id",
    "slowdown",
    "compensation",
    "sector",
    "dist_positions",
    "dist_degrees",
    "d_prime",
]

with open(file_path, "w") as file:
    writer = DictWriter(file, columns)
    writer.writeheader()
    for participant in echo_study.query_participants().get():
        for sector in ("left", "center", "right"):
            responses = participant.get_responses(sector=sector)
            matrix = ConfusionMatrix.of_azimuths(responses)
            sensitivities = get_interstim_sensitivities_for_confusion_matrix(
                matrix)
            for (dist, d_prime) in sensitivities:
                writer.writerow({
                    "participant_id": participant.prolific_pid,
                    "slowdown": participant.slowdown,
                    "compensation": participant.compensation,
                    "sector": sector,
                    "dist_positions": dist,
                    "dist_degrees": 10 * dist,
                    "d_prime": d_prime,
                })