Skip to content

Commit dc53928

Browse files
committed
add license and plots
1 parent c22f958 commit dc53928

7 files changed

+240
-35
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Iryna Korshunova
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

plots/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__author__ = 'user'

plots/plot_examples.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import numpy as np
2+
import json
3+
import os
4+
import cPickle
5+
import copy
6+
7+
import matplotlib.pyplot as plt
8+
from theano import config
9+
10+
from utils.loader import load_train_data
11+
from utils.config_name_creator import *
12+
from utils.data_scaler import scale_across_features, scale_across_time
13+
14+
15+
config.floatX = 'float32'
16+
17+
18+
def plot_examples(subject, data_path, scale):
19+
d = load_train_data(data_path, subject)
20+
x, y, filename_to_idx = d['x'], d['y'], d['filename_to_idx']
21+
if scale:
22+
x, _ = scale_across_time(x=x)
23+
24+
for filename, idx in filename_to_idx.items():
25+
fig = plt.figure()
26+
fig.suptitle(filename)
27+
for i in range(x[idx].shape[0]):
28+
fig.add_subplot(4, 4, i)
29+
plt.imshow(x[idx, i, :, :], aspect='auto', origin='lower', interpolation='none')
30+
plt.colorbar()
31+
plt.show()
32+
33+
34+
if __name__ == '__main__':
35+
36+
with open('SETTINGS.json') as f:
37+
settings_dict = json.load(f)
38+
39+
# path
40+
data_path = settings_dict['path']['processed_data_path'] + '/' + create_fft_data_name(settings_dict)
41+
write_dir = data_path + '/img'
42+
if not os.path.exists(write_dir):
43+
os.mkdir(write_dir)
44+
45+
# params
46+
model_params = settings_dict['model']
47+
validation_params = settings_dict['validation']
48+
49+
names = ['Dog_1', 'Dog_3', 'Dog_2', 'Dog_5', 'Dog_4', 'Patient_1', 'Patient_2']
50+
for subject in names:
51+
print '***********************', subject, '***************************'
52+
plot_examples(subject, data_path, scale=False)

plots/plot_filters.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import json
2+
from utils.config_name_creator import create_cnn_model_name
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
import cPickle
6+
import PIL.Image as Image
7+
8+
9+
def paint_filter(patient_name, model_path):
10+
with open(model_path + '/' + patient_name + '.pickle', 'rb') as f:
11+
state = cPickle.load(f)
12+
13+
# first layer
14+
W1 = state['weights'][0]
15+
width, heights = W1.shape[3], W1.shape[2]
16+
n_filters = W1.shape[0]
17+
n_fbins = state['params']['n_fbins']
18+
print W1.shape
19+
x = np.zeros((heights, width * n_filters))
20+
for i in range(n_filters):
21+
x[:, i] = np.reshape(W1[i, 0, :, :], heights * width)
22+
23+
ax = plt.gca()
24+
ax.set_yticks(range(0, heights, n_fbins))
25+
ax.yaxis.grid(True, which='major', linestyle='-')
26+
plt.imshow(x, interpolation='none')
27+
plt.show()
28+
29+
30+
if __name__ == '__main__':
31+
with open('SETTINGS.json') as f:
32+
settings_dict = json.load(f)
33+
34+
s1 = '0.81448_nfreq6featumeanloghighc180lowcu0.1win_l60strid60globa0recep[1, 2]use_t0activ[urelu, urelu, urelu]dropo[0.2, 0.5]overl0strid[1, 1]train10weigh0.01scale0nkern[16, 32, 128]pool_[1, 1]l2_re0.0001valid10max_i150000rando1'
35+
s2 = '0.80192_nfreq8featumeanlog_stdhighc180lowcu0.1win_l120strid120globa1recep[1, 1]use_t0activ[urelu, urelu, utanh]dropo[0.3, 0.6]overl4strid[1, 1]train10weigh0.01scale1nkern[16, 32, 512]pool_[1, 1]l2_re0.0001valid10max_i150000rando1'
36+
model_path = settings_dict['path']['model_path'] + '/' + s2 # create_cnn_model_name(settings_dict)
37+
names = ['Dog_1', 'Dog_2', 'Dog_3', 'Dog_4', 'Dog_5', 'Patient_1', 'Patient_2']
38+
for patient_name in names:
39+
paint_filter(patient_name, model_path)

plots/plot_hour_means.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import numpy as np
2+
import json
3+
import os
4+
import cPickle
5+
import copy
6+
7+
import matplotlib.pyplot as plt
8+
from theano import config
9+
10+
from utils.loader import load_train_data
11+
from utils.config_name_creator import *
12+
from utils.data_scaler import scale_across_features, scale_across_time
13+
14+
15+
config.floatX = 'float32'
16+
17+
18+
def plot_hour_means(x, filename_to_idx, hours, type, write_dir):
19+
for n_hour, hour in enumerate(hours):
20+
mean_fft = np.zeros((x.shape[1], x.shape[2], x.shape[3]))
21+
for clip in hour:
22+
idx = filename_to_idx[clip]
23+
mean_fft += x[idx]
24+
mean_fft /= len(hour)
25+
26+
fig = plt.figure()
27+
fig.suptitle(type+str(n_hour))
28+
for i in range(mean_fft.shape[0]):
29+
fig.add_subplot(4, 4, i)
30+
plt.imshow(mean_fft[i, :, :], aspect='auto', origin='lower', interpolation='none')
31+
plt.colorbar()
32+
plt.show()
33+
#plt.savefig(write_dir + '/' + subject + type + str(n_hour) + '.jpg')
34+
35+
36+
def plot(subject, data_path, write_dir, scale):
37+
d = load_train_data(data_path, subject)
38+
x, y, filename_to_idx = d['x'], d['y'], d['filename_to_idx']
39+
if scale:
40+
x, _ = scale_across_time(x=x)
41+
42+
filenames_grouped_by_hour = cPickle.load(open('filenames.pickle'))
43+
preictal_hours = copy.deepcopy(filenames_grouped_by_hour[subject]['preictal'])
44+
interictal_hours = copy.deepcopy(filenames_grouped_by_hour[subject]['interictal'])
45+
46+
plot_hour_means(x, filename_to_idx, preictal_hours, type='preictal', write_dir=write_dir)
47+
plot_hour_means(x, filename_to_idx, interictal_hours[:10], type='interictal', write_dir=write_dir)
48+
49+
50+
if __name__ == '__main__':
51+
52+
with open('SETTINGS.json') as f:
53+
settings_dict = json.load(f)
54+
55+
# path
56+
data_path = settings_dict['path']['processed_data_path'] + '/' + create_fft_data_name(settings_dict)
57+
write_dir = data_path + '/img'
58+
if not os.path.exists(write_dir):
59+
os.mkdir(write_dir)
60+
61+
# params
62+
model_params = settings_dict['model']
63+
validation_params = settings_dict['validation']
64+
65+
names = ['Dog_1', 'Dog_3', 'Dog_2', 'Dog_5', 'Dog_4', 'Patient_1', 'Patient_2']
66+
for subject in names:
67+
print '***********************', subject, '***************************'
68+
plot(subject, data_path, write_dir, scale=True)

plots/plot_train_test.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import numpy as np
2+
import json
3+
from theano import config
4+
import cPickle
5+
from utils.loader import load_grouped_train_data, load_train_data, load_test_data
6+
from utils.config_name_creator import *
7+
from matplotlib import cm
8+
import matplotlib.pyplot as plt
9+
10+
config.floatX = 'float32'
11+
12+
13+
def plot_train_test_sizes(subjects, data_path):
14+
filenames_grouped_by_hour = cPickle.load(open('filenames.pickle'))
15+
dict_train, dict_test = {}, {}
16+
for subject in subjects:
17+
x_train = load_train_data(data_path, subject)['x']
18+
x_test = load_test_data(data_path, subject)['x']
19+
dict_train[subject] = x_train.shape[0]
20+
dict_test[subject] = x_test.shape[0]
21+
22+
plt_data = [dict_train, dict_test]
23+
data_orders = [subjects, subjects]
24+
colors = [cm.Greys(1. * i / (len(subjects) + 2)) for i in range(len(subjects))]
25+
26+
values = np.array([[data[name] for name in subjects] for data, order in zip(plt_data, data_orders)])
27+
lefts = np.insert(np.cumsum(values, axis=1), 0, 0, axis=1)[:, :-1]
28+
orders = np.array(data_orders)
29+
bottoms = np.array([0.1, 0.8])
30+
31+
for name, color in zip(subjects, colors):
32+
idx = np.where(orders == name)
33+
value = values[idx]
34+
left = lefts[idx]
35+
plt.bar(left=left, height=0.3, width=value, bottom=bottoms,
36+
color=color, orientation="horizontal", label=name)
37+
left_margin0 = left[0] if value[0] < 100 else left[0] + value[0] / 3.0
38+
left_margin1 = left[1] if value[1] < 100 else left[1] + value[1] / 3.0
39+
plt.text(left_margin0, bottoms[0] + 0.15, value[0])
40+
plt.text(left_margin1, bottoms[1] + 0.15, value[1])
41+
if value[0] < 100:
42+
ratio = str(len(filenames_grouped_by_hour[name]['interictal'])) + '/\n' + str(len(filenames_grouped_by_hour[name]['preictal']))
43+
else:
44+
ratio = str(len(filenames_grouped_by_hour[name]['interictal'])) + '/' + str(len(filenames_grouped_by_hour[name]['preictal']))
45+
plt.text(left_margin0, bottoms[0]+0.08, ratio)
46+
47+
plt.yticks(bottoms + 0.15, ["train", "test"])
48+
plt.legend(loc="best", bbox_to_anchor=(1.0, 1.00))
49+
plt.subplots_adjust(right=0.85)
50+
plt.show()
51+
52+
53+
if __name__ == '__main__':
54+
with open('SETTINGS.json') as f:
55+
settings_dict = json.load(f)
56+
57+
data_path = settings_dict['path']['processed_data_path'] + '/' + create_fft_data_name(settings_dict)
58+
subjects = ['Dog_1', 'Dog_2', 'Dog_3', 'Dog_4', 'Dog_5', 'Patient_1', 'Patient_2']
59+
plot_train_test_sizes(subjects, data_path)

utils/filter_painter.py

-35
This file was deleted.

0 commit comments

Comments
 (0)