# Created by ay27 at 17/4/9
import os

import matplotlib.pyplot as plt
import struct
import numpy as np


def trans(row):
    return list(map(lambda x: np.uint8(x), row))


def read_image(filename):
    with open(filename, mode='rb') as file:
        n = file.read(8)
        n = struct.unpack("<Q", n)[0]
        c = file.read(8)
        c = struct.unpack("<Q", c)[0]
        h = file.read(8)
        h = struct.unpack("<Q", h)[0]
        w = file.read(8)
        w = struct.unpack("<Q", w)[0]
        print(n, c, h, w)

        for ii in range(n):
            r = trans(file.read(h*w))
            g = trans(file.read(h*w))
            b = trans(file.read(h*w))

            if ii == 100:
                break

        print(file.tell() == os.fstat(file.fileno()).st_size)

        img = np.array([r,g,b]).transpose(1,0).reshape(h,w,c)
        print(img.shape)
        plt.imshow(img)
        plt.show()


def read_label(path, ground_truth=None):
    with open(path, 'rb') as file:
        n = file.read(8)
        n = struct.unpack("<Q", n)[0]
        c = file.read(8)
        c = struct.unpack("<Q", c)[0]
        h = file.read(8)
        h = struct.unpack("<Q", h)[0]
        w = file.read(8)
        w = struct.unpack("<Q", w)[0]
        print(n, c, h, w)

        label = []
        sets = set()
        while not (file.tell() == os.fstat(file.fileno()).st_size):

            ch = file.read(4)
            num = struct.unpack("<l", ch)[0]
            label.append(num)
            sets.add(num)
        # print(file.tell() == os.fstat(file.fileno()).st_size)
        print(label)
        print(len(label))

        # print(label[900],label[901], label[902], label[903], label[904])

        return label

        # if ground_truth:
        #     g = []
        #     with open(ground_truth) as file:
        #         for line in file:
        #             g.append(int(line.split(' ')[1]))
        #     np.testing.assert_array_equal(g, label)

if __name__ == '__main__':
    # read_image('../../data/ilsvrc2012/img.bin')
    # read_label('../../data/ilsvrc2012/label.bin', '../../data/ilsvrc2012/val.txt')
    # read_image('../../build/cifar100_train_image.bin')
    # read_label('../../build/cifar100_train_label.bin')

    read_image('../../build/val_data_8.bin')
    for i in range(10):
        read_label('../../build/val_label_%d.bin' % i)

    # labels = []
    # for i in range(10):
    #     labels.append(read_label('../../build/val_label_%d.bin' % i))
    #
    # ground = []
    # with open('../../build/shuffled_list') as file:
    #     ground.append()
[((37, 8, 37, 23), 'matplotlib.pyplot.imshow', 'plt.imshow', ({(37, 19, 37, 22): 'img'}, {}), '(img)', True, 'import matplotlib.pyplot as plt\n'), ((38, 8, 38, 18), 'matplotlib.pyplot.show', 'plt.show', ({}, {}), '()', True, 'import matplotlib.pyplot as plt\n'), ((16, 12, 16, 34), 'struct.unpack', 'struct.unpack', ({(16, 26, 16, 30): '"""<Q"""', (16, 32, 16, 33): 'n'}, {}), "('<Q', n)", False, 'import struct\n'), ((18, 12, 18, 34), 'struct.unpack', 'struct.unpack', ({(18, 26, 18, 30): '"""<Q"""', (18, 32, 18, 33): 'c'}, {}), "('<Q', c)", False, 'import struct\n'), ((20, 12, 20, 34), 'struct.unpack', 'struct.unpack', ({(20, 26, 20, 30): '"""<Q"""', (20, 32, 20, 33): 'h'}, {}), "('<Q', h)", False, 'import struct\n'), ((22, 12, 22, 34), 'struct.unpack', 'struct.unpack', ({(22, 26, 22, 30): '"""<Q"""', (22, 32, 22, 33): 'w'}, {}), "('<Q', w)", False, 'import struct\n'), ((44, 12, 44, 34), 'struct.unpack', 'struct.unpack', ({(44, 26, 44, 30): '"""<Q"""', (44, 32, 44, 33): 'n'}, {}), "('<Q', n)", False, 'import struct\n'), ((46, 12, 46, 34), 'struct.unpack', 'struct.unpack', ({(46, 26, 46, 30): '"""<Q"""', (46, 32, 46, 33): 'c'}, {}), "('<Q', c)", False, 'import struct\n'), ((48, 12, 48, 34), 'struct.unpack', 'struct.unpack', ({(48, 26, 48, 30): '"""<Q"""', (48, 32, 48, 33): 'h'}, {}), "('<Q', h)", False, 'import struct\n'), ((50, 12, 50, 34), 'struct.unpack', 'struct.unpack', ({(50, 26, 50, 30): '"""<Q"""', (50, 32, 50, 33): 'w'}, {}), "('<Q', w)", False, 'import struct\n'), ((10, 30, 10, 41), 'numpy.uint8', 'np.uint8', ({(10, 39, 10, 40): 'x'}, {}), '(x)', True, 'import numpy as np\n'), ((58, 18, 58, 41), 'struct.unpack', 'struct.unpack', ({(58, 32, 58, 36): '"""<l"""', (58, 38, 58, 40): 'ch'}, {}), "('<l', ch)", False, 'import struct\n'), ((35, 14, 35, 31), 'numpy.array', 'np.array', ({(35, 23, 35, 30): '[r, g, b]'}, {}), '([r, g, b])', True, 'import numpy as np\n')]