import numpy as np
import pickle
from collections import defaultdict

from parsing import parser
from analysis import training

def main():
    parse = parser.Parser();

    train_digits = parse.parse_file('data/pendigits-train');
    test_digits = parse.parse_file('data/pendigits-test')

    centroids = training.get_digit_kmeans_centroids(
        train_digits, 256 - 3)

    training.set_digit_observations(
        train_digits, centroids, 256)
    training.set_digit_observations(
        test_digits, centroids, 256)





    train_sequences = defaultdict(list)
    test_sequences = []
    n_test_sequences = len(test_digits)
    test_expected_labels = np.ndarray(shape=(n_test_sequences,))



    for digit in train_digits:
        train_sequences[digit.label].append(digit.np_array_observations)

    for i, digit in enumerate(test_digits):
        test_sequences.append(digit.np_array_observations)
        test_expected_labels[i] = digit.label

    with open('train_sequences', 'wb') as f:
        pickle.dump(train_sequences, f)

    with open('test_sequences', 'wb') as f:
        pickle.dump(test_sequences, f)

    with open('test_expected_labels', 'wb') as f:
        pickle.dump(test_expected_labels, f)


if __name__ == '__main__':
    main()

[((9, 12, 9, 27), 'parsing.parser.Parser', 'parser.Parser', ({}, {}), '()', False, 'from parsing import parser\n'), ((14, 16, 15, 30), 'analysis.training.get_digit_kmeans_centroids', 'training.get_digit_kmeans_centroids', ({(15, 8, 15, 20): 'train_digits', (15, 22, 15, 29): '256 - 3'}, {}), '(train_digits, 256 - 3)', False, 'from analysis import training\n'), ((17, 4, 18, 37), 'analysis.training.set_digit_observations', 'training.set_digit_observations', ({(18, 8, 18, 20): 'train_digits', (18, 22, 18, 31): 'centroids', (18, 33, 18, 36): '(256)'}, {}), '(train_digits, centroids, 256)', False, 'from analysis import training\n'), ((19, 4, 20, 36), 'analysis.training.set_digit_observations', 'training.set_digit_observations', ({(20, 8, 20, 19): 'test_digits', (20, 21, 20, 30): 'centroids', (20, 32, 20, 35): '(256)'}, {}), '(test_digits, centroids, 256)', False, 'from analysis import training\n'), ((26, 22, 26, 39), 'collections.defaultdict', 'defaultdict', ({(26, 34, 26, 38): 'list'}, {}), '(list)', False, 'from collections import defaultdict\n'), ((29, 27, 29, 64), 'numpy.ndarray', 'np.ndarray', (), '', True, 'import numpy as np\n'), ((41, 8, 41, 39), 'pickle.dump', 'pickle.dump', ({(41, 20, 41, 35): 'train_sequences', (41, 37, 41, 38): 'f'}, {}), '(train_sequences, f)', False, 'import pickle\n'), ((44, 8, 44, 38), 'pickle.dump', 'pickle.dump', ({(44, 20, 44, 34): 'test_sequences', (44, 36, 44, 37): 'f'}, {}), '(test_sequences, f)', False, 'import pickle\n'), ((47, 8, 47, 44), 'pickle.dump', 'pickle.dump', ({(47, 20, 47, 40): 'test_expected_labels', (47, 42, 47, 43): 'f'}, {}), '(test_expected_labels, f)', False, 'import pickle\n')]