コード例 #1
0
ファイル: autoencoder.py プロジェクト: vincent732/hw3
    def assign_label(self):
        all_labeled_y, all_labeled_x = read_input('./datas/all_label.p')
        all_labeled_imgs = self.encoder.predict(all_labeled_x)
        all_unlabeled_x = read_input('./datas/all_unlabel.p')
        all_unlabeled_imgs = self.encoder.predict(all_unlabeled_x)
        index = -1
        for unlabeled_img in tqdm(all_unlabeled_imgs):
            index += 1
            target_img = all_unlabeled_x[index]
            target_img = target_img.reshape((1,) + target_img.shape)

            all_distance = [euclidean_distance(unlabeled_img, labeled_img) for labeled_img in all_labeled_imgs]
            index_of_max = min(range(len(all_distance)), key = lambda i: all_distance[i])
            assigned_label = all_labeled_y[index_of_max]
            all_labeled_x = np.concatenate((all_labeled_x, target_img), axis=0)
            all_labeled_y = np.concatenate((all_labeled_y, np.array([assigned_label])), axis=0)

            # # for debug
            # origin_img = all_labeled_x[index_of_max]
            # target_img = np.reshape(target_img, (3, 32, 32))
            # origin_img = np.reshape(origin_img, (3, 32, 32))
            # pyplot.figure(figsize=[4, 4])
            # pyplot.subplot(2, 2, 1)
            # pyplot.imshow(toimage(origin_img))
            # pyplot.subplot(2, 2, 2)
            # pyplot.imshow(toimage(target_img))
            # pyplot.show()

        output_dict = dict({'data': all_labeled_x, 'labels': all_labeled_y})
        output_path = 'datas/relabeled_img.p'
        import pickle
        with open(output_path, 'wb') as handle:
            print("Save output at %s " % output_path)
            pickle.dump(output_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)
コード例 #2
0
ファイル: tests.py プロジェクト: iisojunn/advent-of-code-2020
 def test_trees_on_the_path(self):
     tree_map = read_input("test_input")
     self.assertEqual(trees_on_the_path(tree_map, 1, 1), 2)
     self.assertEqual(trees_on_the_path(tree_map, 3, 1), 7)
     self.assertEqual(trees_on_the_path(tree_map, 5, 1), 3)
     self.assertEqual(trees_on_the_path(tree_map, 7, 1), 4)
     self.assertEqual(trees_on_the_path(tree_map, 1, 2), 2)
コード例 #3
0
    def setUp(self):
        """setUp test.

        :return:
        """

        super(PizzaTestCase, self).setUp()

        try:
            unlink(input_file)
        except FileNotFoundError:
            pass

        with open(input_file, 'wb') as file_descriptor:
            file_descriptor.write(input_text)

        self.pizza = read_input(input_file)
コード例 #4
0
ファイル: test.py プロジェクト: cobaltine/aoc20
from datetime import datetime
from main import (
    decode_seats,
    find_my_seat,
    read_input,
)

# [TEST1] Verify that highest ID is correctly found from test dataset
input = read_input(path='input_test.txt')
decoded_file = decode_seats(enc=input)
if decoded_file[-1] != 820:
    raise Exception(
        f'{datetime.now()} - ERROR: Highest value should be 820, but got \'{decoded_file[-1]}\''
    )

# [TEST2] Verify that missing id '3' (my seat) is correctly found from test dataset
decoded_sim = [
    1,
    2,
    4,
    5,
]
if find_my_seat(seats=decoded_sim) != 3:
    raise Exception(f'{datetime.now()} - ERROR: My seat was not found')

print(f'{datetime.now()} - OK: all tests passed!')
コード例 #5
0
import pytest

from main import (Problem1ApplyRules, Problem1SourrandingSeatsFinder,
                  Problem2ApplyRules, Problem2SourrandingSeatsFinder,
                  SeatLayout, read_input, solver)

finder_1 = Problem1SourrandingSeatsFinder()
rules_1 = Problem1ApplyRules()
finder_2 = Problem2SourrandingSeatsFinder()
rules_2 = Problem2ApplyRules()


test_inputs = [
    (read_input("./input_test_1_1.txt"), finder_1, rules_1, 37),
    (read_input("./input_test_1_2.txt"), finder_1, rules_1, 11),
    (read_input("./input_test_2_1.txt"), finder_2, rules_2, 5),
    (read_input("./input_test_2_2.txt"), finder_2, rules_2, 9),
    (read_input("./input_test_1_1.txt"), finder_2, rules_2, 26),
]


@pytest.mark.parametrize("test_input,finder,rules,expected", test_inputs)
def test(test_input, finder, rules, expected):
    seat_layout = SeatLayout.load(finder=finder, rules=rules, input_str=test_input)
    assert solver(seat_layout) == expected
コード例 #6
0
ファイル: test.py プロジェクト: iisojunn/advent-of-code-2020
 def _get_tiles_corner_tiles():
     tiles = read_input("test_input")
     search_matches(tiles)
     corner_tiles = search_corners(tiles)
     return tiles, corner_tiles
コード例 #7
0
ファイル: autoencoder.py プロジェクト: vincent732/hw3
            # # for debug
            # origin_img = all_labeled_x[index_of_max]
            # target_img = np.reshape(target_img, (3, 32, 32))
            # origin_img = np.reshape(origin_img, (3, 32, 32))
            # pyplot.figure(figsize=[4, 4])
            # pyplot.subplot(2, 2, 1)
            # pyplot.imshow(toimage(origin_img))
            # pyplot.subplot(2, 2, 2)
            # pyplot.imshow(toimage(target_img))
            # pyplot.show()

        output_dict = dict({'data': all_labeled_x, 'labels': all_labeled_y})
        output_path = 'datas/relabeled_img.p'
        import pickle
        with open(output_path, 'wb') as handle:
            print("Save output at %s " % output_path)
            pickle.dump(output_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)


if __name__ == "__main__":
    x = read_input('./datas/all_unlabel.p')
    np.random.shuffle(x)
    x_train, x_test = x[:-500], x[-500:]
    x_train = x_train.astype(np.float32) / 255.
    x_test = x_test.astype(np.float32) / 255.
    autoencoder = AutoEncoder(x_train, x_test)
    autoencoder.start_train()
    autoencoder.predict(x_test[:10])
    autoencoder.assign_label()

コード例 #8
0
ファイル: tests.py プロジェクト: ajporrasr/url_validation
def test_read_input_ok(monkeypatch):
    monkeypatch.setattr('sys.stdin', io.StringIO('a\nb\nc\nd\n'))
    assert main.read_input() == ['a', 'b', 'c', 'd']
コード例 #9
0
import pytest

from main import problem_1, problem_2, read_input

input_1 = read_input("./input_test_1.txt")
input_2 = read_input("./input_test_2.txt")

testdata_1 = [(input_1, 7 * 5), (input_2, 22 * 10)]
testdata_2 = [(input_1, 8), (input_2, 19208)]


@pytest.mark.parametrize("test_input,expected", testdata_1)
def test_1(test_input, expected):
    assert problem_1(test_input) == expected


@pytest.mark.parametrize("test_input,expected", testdata_2)
def test_2(test_input, expected):
    assert problem_2(test_input) == expected
コード例 #10
0
 def test_read_file(self):
     read = read_input('test_input.txt')
     self.assertEqual(read, [
         'C - 3 - 4', 'M - 2 - 1', 'T - 1 - 2 - 3',
         'A - Lara - 1 - 1 - N - AADA'
     ], "Should be 6")
コード例 #11
0
 def setUp(self):
     self.rules, self.messages = read_input("test-input")
コード例 #12
0
ファイル: test.py プロジェクト: iisojunn/advent-of-code-2020
 def setUp(self):
     self.layout = read_input("test_input")