-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
147 lines (119 loc) · 3.71 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import numpy as np
import matplotlib.pyplot as plt
import random
import os
def corrupt(I, n, with_replacement=False):
"""Corrupts the pattern 'I' by flipping exactly `n` bits.
Python version of Bruno Olshausen's `corrupt.m` pythonized by Paul
Ivanov (and then further modified by Jessica Hamrick).
"""
cI = I.copy()
if I.ndim == 1:
cI = I.copy()[:, None]
N, k = cI.shape
idx = np.arange(N)
for i in xrange(k):
# cidx = np.random.choice(idx, size=n, replace=False)
if with_replacement:
cidx = np.random.randint(0, N, n)
else:
cidx = random.sample(idx, n)
cI[cidx, i] = 1 - cI[cidx, i]
if I.ndim == 1:
cI = cI[:, 0]
return cI
def plot_io(input, output):
kwargs = {
'vmin': 0,
'vmax': 1,
'cmap': 'gray',
'interpolation': 'nearest'
}
plt.clf()
plt.subplot(1, 2, 1)
plt.imshow(input, **kwargs)
plt.xticks([], [])
plt.yticks([], [])
plt.title("Input")
plt.subplot(1, 2, 2)
plt.imshow(output, **kwargs)
plt.xticks([], [])
plt.yticks([], [])
plt.title("Output")
def random_input(n, k=0, rso=None):
"""Generate `k` random binary vectors of length `n`.
If k=0, then the output will be a vector of shape (n,). If k>0,
then the output will be a matrix of shape (n, k).
Parameters
----------
n : vector length
k : number of vectors (default=0)
rso : random number object (default=None)
Returns
-------
out : np.ndarray of shape (n,) or (n, k)
"""
# determine the size of the array
if k == 0:
size = (n,)
elif k > 0:
size = (n, k)
else:
raise ValueError("k < 0")
# generate random vectors
if rso:
out = rso.randint(0, 2, size=size)
else:
out = np.random.randint(0, 2, size=size)
return out
def set_fig_properties():
fig = plt.gcf()
fig.clf()
fig.set_figwidth(8)
fig.set_figheight(6)
def plot_error(x, data, color, alpha, label, linestyle='-'):
mean = data[:, 0]
sem = data[:, 1]
loerr = mean - sem
hierr = mean + sem
plt.fill_between(x, loerr, hierr, color=color, alpha=alpha)
plt.plot(x, mean, label=label, color=color, linestyle=linestyle)
def save(path, ext='png', close=True, verbose=True):
"""Save a figure from pyplot.
Parameters
----------
path : string
The path (and filename, without the extension) to save the
figure to.
ext : string (default='png')
The file extension. This must be supported by the active
matplotlib backend (see matplotlib.backends module). Most
backends support 'png', 'pdf', 'ps', 'eps', and 'svg'.
close : boolean (default=True)
Whether to close the figure after saving. If you want to save
the figure multiple times (e.g., to multiple formats), you
should NOT close it in between saves or you will have to
re-plot it.
verbose : boolean (default=True)
Whether to print information about when and where the image
has been saved.
"""
# Extract the directory and filename from the given path
directory = os.path.split(path)[0]
filename = "%s.%s" % (os.path.split(path)[1], ext)
if directory == '':
directory = '.'
# If the directory does not exist, create it
if not os.path.exists(directory):
os.makedirs(directory)
# The final path to save to
savepath = os.path.join(directory, filename)
if verbose:
print("Saving figure to '%s'..." % savepath),
# Actually save the figure
plt.savefig(savepath)
# Close it
if close:
plt.close()
if verbose:
print("Done")