-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
74 lines (61 loc) · 2.02 KB
/
utils.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
import os
import sys
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
def get_submit_folder(name):
""" Check submit details, remove warnings using tf logging warning and make directory
Returns
-------
str
path of the created directory
"""
try:
CONDOR_ID = os.environ['CONDOR_ID']
except KeyError:
sys.exit('Error: Run this script with "pygpu %file"')
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
folder = 'train-%s-%s' % (name, CONDOR_ID) # folder for training results
os.makedirs(folder)
return folder
def load_data(type, data):
""" Load toptagging data
Parameters
----------
type : str
Which dataset should be loaded: 'images' or '4vectors'?
data : str
Which dataset should be loaded: 'test' or 'train'?
Returns
-------
np.array, np.array
tuple of images, labels
"""
path = "/net/scratch/deeplearning/toptagging/"
if type == "images":
file = np.load(path + "images_" + data + ".npz")
elif type == "4vectors":
file = np.load(path + "4vectors_" + data + ".npz")
else:
raise AssertionError("type should be 'images or '4vectors'")
if type == "4vectors":
images = file["fourVectors"]
else:
images = file[type][..., np.newaxis]
labels = file["labels"]
return (images, labels)
def plot_average_images(images, labels, folder):
fig, axes = plt.subplots(1, 2, figsize=(10, 5))
for i, ax in enumerate(axes):
idx = np.where(labels == i)[0]
norm = LogNorm(10**-4, images.max(), clip='True')
im = ax.imshow(np.mean(images[idx], axis=(0, -1)), norm=norm)
ax.set_xlabel('eta')
ax.set_ylabel('phi')
axes[0].set_title("qcd background")
axes[1].set_title("top quark")
fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
fig.colorbar(im, cax=cbar_ax)
fig.savefig(folder+'/average_images.png')