-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcommon_functions.py
More file actions
executable file
·87 lines (61 loc) · 2.67 KB
/
common_functions.py
File metadata and controls
executable file
·87 lines (61 loc) · 2.67 KB
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
import matplotlib.pyplot as plt
from matplotlib import cm
%matplotlib inline
# A function to plot a matrix
def plot_image(data, colorspace='binary'):
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
ax.imshow(data, cmap=colorspace)
def normalize_array(array):
array_min = array.min(axis=(0, 1), keepdims=True)
array_max = array.max(axis=(0, 1), keepdims=True)
normalized_array = (array - array_min)/(array_max - array_min)
return normalized_array
def normalize_with_preset(array, max_value, min_value):
normalized_array = (array - min_value)/(max_value - min_value)
return normalized_array
def recreate_image(labels, w, h):
"""Recreate the (compressed) image from the code book & labels"""
image = np.zeros((w, h))
label_idx = 0
for i in range(w):
for j in range(h):
image[i][j] = labels[label_idx]
label_idx += 1
return image
def overlay_images(image1, image2, colorspace1='binary', colorspace2='magma', transparency=0.5):
plt.imshow(image1, cmap=colorspace1, interpolation='nearest')
plt.imshow(image2, cmap=colorspace2, alpha=transparency, interpolation='bilinear')
plt.show()
def flatten_normalize_neighbors(training_set, global_max=233, global_min=69):
flattened_neighbors = []
neighbors = training_set['neighbors']
for i in range(len(neighbors)):
numpy_array = np.array(neighbors[i]).reshape(-1, 1)
normalized_neighbors = normalize_with_preset(numpy_array, global_max, global_min)
flattened_neighbors.append(normalized_neighbors)
return np.array(flattened_neighbors).reshape((len(flattened_neighbors), -1))
def extract_values_from_training_data_locations(training_data, image_channel):
data_values = []
for row in training_data:
data_values.append(np.array([(image_channel[row[0], row[1]]), row[2]]))
data_values = np.array(data_values)
return data_values
def extract_neighborhood_values_from_training_data_locations(training_data, image_channel):
data_values = []
for row in training_data:
data_values.append(np.array([(image_channel[row[0], row[1]]), row[2]]))
data_values = np.array(data_values)
return data_values
def flatten_neighbors(neighbors):
data_values = []
for neighbor in neighbors:
#data_values.append(neighbor[0].reshape(-1, 1))
data_values.append(neighbor[0].reshape(-1, 1))
data_values = np.array(data_values)
return data_values
def extract_labels_from_set(dataset):
labels = []
for neighbor in training_set:
labels.append(neighbor[1])
return np.array(labels).flatten()