-
Notifications
You must be signed in to change notification settings - Fork 18
/
test_label.py
125 lines (99 loc) · 5.54 KB
/
test_label.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
"""Unit test for GravitySpy
"""
__author__ = 'Scott Coughlin <scott.coughlin@ligo.org>'
import os
os.environ["KERAS_BACKEND"] = "theano"
import gravityspy.ML.read_image as read_image
import gravityspy.ML.labelling_test_glitches as label_glitches
import gravityspy.ML.train_classifier as train_classifier
import pandas as pd
import numpy
TEST_IMAGES_PATH = os.path.join(os.path.split(__file__)[0], 'data',
'images')
MODEL_NAME_CNN = os.path.join(os.path.split(__file__)[0], '..', '..', 'bin',
'multi_view_classifier.h5')
MODEL_NAME_FEATURE_SINGLE_VIEW = os.path.join(os.path.split(__file__)[0], '..', '..', 'bin',
'single_view_model.h5')
MODEL_NAME_FEATURE_MULTIVIEW = os.path.join(os.path.split(__file__)[0], '..', '..', 'bin',
'semantic_idx_model.h5')
MULTIVIEW_FEATURES_FILE = os.path.join(os.path.split(__file__)[0], 'data',
'MULTIVIEW_FEATURES.npy')
SCORE = 0.9997797608375549
FEATURES = numpy.array([[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
136.32681274414062, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 143.1021728515625, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 74.27071380615234, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 110.8934326171875, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
142.75534057617188, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 120.54851531982422, 132.58575439453125, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 125.55241394042969, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 154.26510620117188, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 34.673343658447266, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]])
MULTIVIEW_FEATURES = numpy.load(MULTIVIEW_FEATURES_FILE)
class TestGravitySpyML(object):
"""`TestCase` for the GravitySpy
"""
def test_label(self):
list_of_images = []
for ifile in os.listdir(TEST_IMAGES_PATH):
if 'spectrogram' in ifile:
list_of_images.append(ifile)
image_dataDF = pd.DataFrame()
for idx, image in enumerate(list_of_images):
image_data = read_image.read_grayscale(os.path.join(
TEST_IMAGES_PATH,
image),
resolution=0.3)
image_dataDF[image] = [image_data]
# Now label the image
scores, MLlabel = label_glitches.label_glitches(
image_dataDF,
'{0}'.format(
MODEL_NAME_CNN),
[140, 170],
False)
confidence = float(scores[0][MLlabel])
assert confidence == SCORE
def test_feature_space(self):
list_of_images = []
for ifile in os.listdir(TEST_IMAGES_PATH):
if 'spectrogram' in ifile:
list_of_images.append(ifile)
# Get ML semantic index image data
image_dataDF = pd.DataFrame()
for idx, image in enumerate(list_of_images):
if '1.0.png' in image:
image_data = read_image.read_grayscale(os.path.join(TEST_IMAGES_PATH, image), resolution=0.3)
image_dataDF[image] = [image_data]
# Determine features
features = label_glitches.get_feature_space(image_data=image_dataDF,
semantic_model_name='{0}'.format(MODEL_NAME_FEATURE_SINGLE_VIEW),
image_size=[140, 170],
verbose=False)
numpy.testing.assert_array_almost_equal(features, FEATURES, decimal=3)
def test_multiview_rgb(self):
list_of_images = []
for ifile in os.listdir(TEST_IMAGES_PATH):
if 'spectrogram' in ifile:
list_of_images.append(ifile)
image_dataDF = pd.DataFrame()
for idx, image in enumerate(list_of_images):
image_data_r, image_data_g, image_data_b = read_image.read_rgb(os.path.join(
TEST_IMAGES_PATH,
image),
resolution=0.3)
image_dataDF[image] = [[image_data_r, image_data_g, image_data_b]]
# Now label the image
features = label_glitches.get_multiview_feature_space(
image_dataDF,
'{0}'.format(
MODEL_NAME_FEATURE_MULTIVIEW),
[140, 170],
False)
numpy.testing.assert_array_almost_equal(features, MULTIVIEW_FEATURES,
decimal=3)