In [1]:
import numpy as np
import scipy as sp
from matplotlib.pyplot import imshow
import matplotlib.pyplot as plt
%matplotlib inline
from PIL import Image

In [98]:
#These arrays define the 'basis' of the angled bar set. All angled bars here are either h_i (at zero or 180 degrees)
#d1_i (pi/4 or 5pi/4), v_i, (+- pi/2), or d2_i (3pi/4 or -pi/4) where the angle is measured from the left horizontal

#All data will be generated by adding noise to the image such that the values of the pixels are no longer either zero or one

v1 = np.array([[0,0,1,0,0],[0,0,1,0,0],[0,0,1,0,0],[0,0,1,0,0],[0,0,1,0,0]])
v2 = np.array([[0,0,0,1,0],[0,0,0,1,0],[0,0,0,1,0],[0,0,0,1,0],[0,0,0,1,0]])
v3 = np.array([[0,0,0,0,1],[0,0,0,0,1],[0,0,0,0,1],[0,0,0,0,1],[0,0,0,0,1]])
v4 = np.array([[0,1,0,0,0],[0,1,0,0,0],[0,1,0,0,0],[0,1,0,0,0],[0,1,0,0,0]])
v5 = np.array([[1,0,0,0,0],[1,0,0,0,0],[1,0,0,0,0],[1,0,0,0,0],[1,0,0,0,0]])
h1 = np.array([[0,0,0,0,0],[1,1,1,1,1],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]])
h2 = np.array([[1,1,1,1,1],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]])
h3 = np.array([[0,0,0,0,0],[0,0,0,0,0],[1,1,1,1,1],[0,0,0,0,0],[0,0,0,0,0]])
h4 = np.array([[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[1,1,1,1,1],[0,0,0,0,0]])
h5 = np.array([[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[1,1,1,1,1]])
d11 = np.array([[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[1,0,0,0,0],[0,1,0,0,0]])
d12 = np.array([[0,0,0,0,0],[0,0,0,0,0],[1,0,0,0,0],[0,1,0,0,0],[0,0,1,0,0]])
d13 = np.array([[0,0,0,0,0],[1,0,0,0,0],[0,1,0,0,0],[0,0,1,0,0],[0,0,0,1,0]])
d14 = np.array([[1,0,0,0,0],[0,1,0,0,0],[0,0,1,0,0],[0,0,0,1,0],[0,0,0,0,1]])
d15 = np.array([[0,1,0,0,0],[0,0,1,0,0],[0,0,0,1,0],[0,0,0,0,1],[0,0,0,0,0]])
d16 = np.array([[0,0,1,0,0],[0,0,0,1,0],[0,0,0,0,1],[0,0,0,0,0],[0,0,0,0,0]])
d17 = np.array([[0,0,0,1,0],[0,0,0,0,1],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]])
d21 = np.fliplr(d11)
d22 = np.fliplr(d12)
d23 = np.fliplr(d13)
d24 = np.fliplr(d14)
d25 = np.fliplr(d15)
d26 = np.fliplr(d16)
d27 = np.fliplr(d17)

size = (5,5)

In [213]:
#generates centered data
for i in range(2000):
    datah3 = h3 + 0.35*(0.5-np.random.random_sample(size))
    datav1 = v1 + 0.35*(0.5-np.random.random_sample(size))
    datad14 = d14 + 0.35*(0.5-np.random.random_sample(size))
    datad24 = d24 + 0.35*(0.5-np.random.random_sample(size))
    with open("datacentered.txt", "ab") as mydata:
        np.savetxt(mydata,[datah3.flatten()] )
        np.savetxt(mydata,[datav1.flatten()] )
        np.savetxt(mydata,[datad14.flatten()] )
        np.savetxt(mydata,[datad24.flatten()])
    with open("labelcentered.txt", "ab") as mylabel:
        np.savetxt(mylabel,[np.array([1,0,0,0])])
        np.savetxt(mylabel,[np.array([0,0,1,0])])
        np.savetxt(mylabel,[np.array([0,1,0,0])])
        np.savetxt(mylabel,[np.array([0,0,0,1])])

In [127]:
imshow(d27,interpolation = 'none',cmap='gray')


Out[127]:
<matplotlib.image.AxesImage at 0x7efe3bc57a90>

In [211]:
fig = imshow(d27+ 0.35*(0.5-np.random.random_sample(size)), interpolation = 'none',cmap = 'gray')



In [161]:
#generates all types of data
for i in range(2000):
    datah1 = h1 + 0.35*(0.5-np.random.random_sample(size))
    datah2 = h2 + 0.35*(0.5-np.random.random_sample(size))
    datah3 = h3 + 0.35*(0.5-np.random.random_sample(size))
    datah4 = h4 + 0.35*(0.5-np.random.random_sample(size))
    datah5 = h5 + 0.35*(0.5-np.random.random_sample(size))
    datav1 = v1 + 0.35*(0.5-np.random.random_sample(size))
    datav2 = v2 + 0.35*(0.5-np.random.random_sample(size))
    datav3 = v3 + 0.35*(0.5-np.random.random_sample(size))
    datav4 = v4 + 0.35*(0.5-np.random.random_sample(size))
    datav5 = v5 + 0.35*(0.5-np.random.random_sample(size))
    datad11 = d11 + 0.35*(0.5-np.random.random_sample(size))
    datad12 = d12 + 0.35*(0.5-np.random.random_sample(size))
    datad13 = d13 + 0.35*(0.5-np.random.random_sample(size))
    datad14 = d14 + 0.35*(0.5-np.random.random_sample(size))
    datad15 = d15 + 0.35*(0.5-np.random.random_sample(size))
    datad16 = d16 + 0.35*(0.5-np.random.random_sample(size))
    datad17 = d17 + 0.35*(0.5-np.random.random_sample(size))
    datad21 = d21 + 0.35*(0.5-np.random.random_sample(size))
    datad22 = d22 + 0.35*(0.5-np.random.random_sample(size))
    datad23 = d23 + 0.35*(0.5-np.random.random_sample(size))
    datad24 = d24 + 0.35*(0.5-np.random.random_sample(size))
    datad25 = d25 + 0.35*(0.5-np.random.random_sample(size))
    datad26 = d26 + 0.35*(0.5-np.random.random_sample(size))
    datad27 = d27 + 0.35*(0.5-np.random.random_sample(size))
    with open("data.txt", "ab") as mydata:
        np.savetxt(mydata,[datah1.flatten()] )
        np.savetxt(mydata,[datah2.flatten()] )
        np.savetxt(mydata,[datah3.flatten()] )
        np.savetxt(mydata,[datah4.flatten()] )
        np.savetxt(mydata,[datah5.flatten()] )
        np.savetxt(mydata,[datav1.flatten()] )
        np.savetxt(mydata,[datav2.flatten()] )
        np.savetxt(mydata,[datav3.flatten()] )
        np.savetxt(mydata,[datav4.flatten()] )
        np.savetxt(mydata,[datav5.flatten()] )
        np.savetxt(mydata,[datad11.flatten()] )
        np.savetxt(mydata,[datad12.flatten()] )
        np.savetxt(mydata,[datad13.flatten()] )
        np.savetxt(mydata,[datad14.flatten()] )
        np.savetxt(mydata,[datad15.flatten()] )
        np.savetxt(mydata,[datad16.flatten()] )
        np.savetxt(mydata,[datad17.flatten()] )
        np.savetxt(mydata,[datad21.flatten()] )
        np.savetxt(mydata,[datad22.flatten()])
        np.savetxt(mydata,[datad23.flatten()])
        np.savetxt(mydata,[datad24.flatten()])
        np.savetxt(mydata,[datad25.flatten()])
        np.savetxt(mydata,[datad26.flatten()])
        np.savetxt(mydata,[datad27.flatten()])
    with open("label.txt", "ab") as mylabel:
        np.savetxt(mylabel,[np.array([1,0,0,0])])
        np.savetxt(mylabel,[np.array([1,0,0,0])])
        np.savetxt(mylabel,[np.array([1,0,0,0])])
        np.savetxt(mylabel,[np.array([1,0,0,0])])
        np.savetxt(mylabel,[np.array([1,0,0,0])])
        np.savetxt(mylabel,[np.array([0,0,1,0])])
        np.savetxt(mylabel,[np.array([0,0,1,0])])
        np.savetxt(mylabel,[np.array([0,0,1,0])])
        np.savetxt(mylabel,[np.array([0,0,1,0])])
        np.savetxt(mylabel,[np.array([0,0,1,0])])
        np.savetxt(mylabel,[np.array([0,1,0,0])])
        np.savetxt(mylabel,[np.array([0,1,0,0])])
        np.savetxt(mylabel,[np.array([0,1,0,0])])
        np.savetxt(mylabel,[np.array([0,1,0,0])])
        np.savetxt(mylabel,[np.array([0,1,0,0])])
        np.savetxt(mylabel,[np.array([0,1,0,0])])
        np.savetxt(mylabel,[np.array([0,1,0,0])])
        np.savetxt(mylabel,[np.array([0,0,0,1])])
        np.savetxt(mylabel,[np.array([0,0,0,1])])
        np.savetxt(mylabel,[np.array([0,0,0,1])])
        np.savetxt(mylabel,[np.array([0,0,0,1])])
        np.savetxt(mylabel,[np.array([0,0,0,1])])
        np.savetxt(mylabel,[np.array([0,0,0,1])])
        np.savetxt(mylabel,[np.array([0,0,0,1])])

In [2]:
#loads centered data for testing
datacentered = np.loadtxt('datacentered.txt')
datacentered = np.reshape(datacentered, (datacentered.shape[0],5,5))
label = np.loadtxt('labelcentered.txt')

In [3]:
#Tests that labels are correctly assigned in data files
i = np.random.randint(datacentered.shape[0])
imshow(datacentered[i],interpolation='none',cmap = 'gray')
print('{0}th saved image'.format(i))
if (label[i] == [np.array([1,0,0,0])]).all():
    print('horizontal')
elif (label[i] == [np.array([0,1,0,0])]).all():
    print('first diagonal')
elif (label[i] == [np.array([0,0,1,0])]).all():
    print('vertical')
elif (label[i] == [np.array([0,0,0,1])]).all():
    print('second diagonal')
else:
    print('screwed up somewhere')


7564th saved image
horizontal

In [8]:
#generates random assortment of images from centered data
for w in range(4):
    j = np.random.randint(datacentered.shape[0], size = 1)
    print(j)
    i = [j[0],j[0]+1,j[0]+2,j[0]+3,j[0]+4,j[0]+5,j[0]+6,j[0]+7,j[0]+8,j[0]+9]
    print(i)
    plt.figure(1)
    plt.subplot(2,5,1)
    plt.imshow(datacentered[i[0]], interpolation = 'none', cmap = 'gray')

    plt.subplot(2,5,2)
    plt.imshow(datacentered[i[1]], interpolation = 'none', cmap = 'gray')

    plt.subplot(2,5,3)
    plt.imshow(datacentered[i[2]], interpolation = 'none', cmap = 'gray')

    plt.subplot(2,5,4)
    plt.imshow(datacentered[i[3]], interpolation = 'none', cmap = 'gray')

    plt.subplot(2,5,5)
    plt.imshow(datacentered[i[4]], interpolation = 'none', cmap = 'gray')

    plt.subplot(2,5,6)
    plt.imshow(datacentered[i[5]], interpolation = 'none', cmap = 'gray')

    plt.subplot(2,5,7)
    plt.imshow(datacentered[i[6]], interpolation = 'none', cmap = 'gray')

    plt.subplot(2,5,8)
    plt.imshow(datacentered[i[7]], interpolation = 'none', cmap = 'gray')

    plt.subplot(2,5,9)
    plt.imshow(datacentered[i[8]], interpolation = 'none', cmap = 'gray')

    plt.subplot(2,5,10)
    plt.imshow(datacentered[i[9]], interpolation = 'none', cmap = 'gray')
    plt.savefig('figure_examples_centered{}.jpg'.format(w))
    plt.show()


[1392]
[1392, 1393, 1394, 1395, 1396, 1397, 1398, 1399, 1400, 1401]
[597]
[597, 598, 599, 600, 601, 602, 603, 604, 605, 606]
[6606]
[6606, 6607, 6608, 6609, 6610, 6611, 6612, 6613, 6614, 6615]
[6297]
[6297, 6298, 6299, 6300, 6301, 6302, 6303, 6304, 6305, 6306]

In [ ]: