@@ -123,19 +123,24 @@ def remove_similar_locations(shapelet_locations, shapelet_distances):
123
123
124
124
# Given the shapelet_distances matrix of a given shapelet, get the locations of
125
125
# the closest shapelets from the entire dataset
126
- def get_shapelet_locations_scaled_threshold (shapelet_distances , ts_length , threshold ):
126
+ def get_shapelet_locations_scaled_threshold (shapelet_distances , ts_length , threshold , shapelets = None ):
127
127
# Compute the length of the shapelet
128
128
shapelet_length = ts_length - shapelet_distances .shape [1 ] + 1
129
129
130
130
# Get the indices of the n closest shapelets to the original shapelet
131
131
s_indices = []
132
132
for i in range (shapelet_distances .shape [0 ]):
133
133
for j in range (shapelet_distances .shape [1 ]):
134
+ # i Iterates Items
135
+ # j iterates Shapelets
134
136
# Compare to the threshold, scaled to shapelet length
137
+ #shapelet_length = ts_length - len(shapelet_distances[j]) + 1
135
138
if shapelet_distances [i ][j ] / shapelet_length <= threshold :
139
+ #j is the number of the shapelet
136
140
s_indices .append (np .array ([i , j ]))
137
141
138
142
if len (s_indices ) > 0 :
143
+ # Relevant shaplet indicies
139
144
s_indices = np .asarray (s_indices )
140
145
141
146
# Create an array to store the locations of the closest n shapelets
@@ -147,7 +152,6 @@ def get_shapelet_locations_scaled_threshold(shapelet_distances, ts_length, thres
147
152
shapelet_locations [i ] = np .append (
148
153
s_indices [i ], s_indices [i ][1 ] + shapelet_length
149
154
)
150
-
151
155
# Remove overlapping shapelets and keep the closest one to th original shapelet
152
156
shapelet_locations = remove_similar_locations (
153
157
shapelet_locations , shapelet_distances
@@ -176,9 +180,9 @@ def get_occurences_threshold(shapelets_distances, ts_length, percentage):
176
180
# Sort the distances ascendingly
177
181
sds .sort ()
178
182
183
+
179
184
# Number of shapelet occurences to keep (per shapelet)
180
185
n = int (percentage * len (sds ))
181
-
182
186
# Return the threshold distance to select the shapelet occurences to keep
183
187
return sds [n ]
184
188
@@ -188,6 +192,7 @@ def get_occurences_threshold(shapelets_distances, ts_length, percentage):
188
192
def get_all_shapelet_locations_scaled_threshold (
189
193
shapelets_distances , ts_length , percentage
190
194
):
195
+
191
196
# Get the threshold to be used for selecting shapelet occurences
192
197
threshold = get_occurences_threshold (shapelets_distances , ts_length , percentage )
193
198
@@ -213,19 +218,28 @@ def get_all_shapelet_locations_scaled_threshold(
213
218
# Get the locations of the closest shapelets for each timeseries across the
214
219
# entire dataset based on the training threshold
215
220
def get_all_shapelet_locations_scaled_threshold_test (
216
- shapelets_distances , ts_length , threshold
221
+ shapelets_distances , ts_length , threshold , shapelets = None
217
222
):
223
+
224
+ threshold = 5
218
225
all_shapelet_locations = []
219
226
all_no_occurences = []
220
227
221
228
for dim in shapelets_distances :
229
+ # Itreate DIMs
222
230
dim_shapelet_locations = []
223
231
no_occurences = []
232
+ if type (dim ) == int :
233
+ dim = shapelets_distances [0 ]
224
234
for i , shapelet in enumerate (dim ):
235
+
236
+ # Iterate the shapelet [0. Num Shapelts]?
237
+ # Get the shapelet Locations
225
238
sls = get_shapelet_locations_scaled_threshold (
226
- shapelet , ts_length , threshold
239
+ shapelet , ts_length , threshold , shapelets
227
240
)
228
241
if sls [0 ][0 ] != 4294967295 :
242
+ #print('Append',sls)
229
243
dim_shapelet_locations .append (sls )
230
244
else :
231
245
no_occurences .append (i )
@@ -236,31 +250,34 @@ def get_all_shapelet_locations_scaled_threshold_test(
236
250
237
251
238
252
def get_shapelets_locations_test (idx , all_sls , dim , all_shapelets_class ):
253
+ if len (np .array (all_shapelets_class ).shape ):
254
+ all_shapelets_class = [all_shapelets_class ]
239
255
all_locs = {}
240
- try :
256
+
257
+ if True :
241
258
for i , s in enumerate ([all_sls [dim ][j ] for j in all_shapelets_class [dim ]]):
259
+
242
260
i_locs = []
243
261
for loc in s :
244
- if loc [0 ] == idx :
262
+ if True :
263
+ # TODO not necessary?
264
+ #if loc[0] == idx:
245
265
loc = (loc [1 ], loc [2 ])
246
266
i_locs .append (loc )
247
267
all_locs [i ] = i_locs
248
- except Exception as ex :
249
- pass
268
+
250
269
return all_locs
251
270
252
271
253
272
##Optimize by fitting outside or returning a list of all nns at once
254
273
## Reworked so that only training data is available.
255
274
def get_nearest_neighbor (knn , instance_x , pred_label , x_train , y_train ):
256
- # pred_label = y_pred[idx]
257
275
target_labels = np .argwhere (y_train != pred_label )
258
276
259
277
X_train_knn = instance_x .reshape (1 , instance_x .shape [0 ], instance_x .shape [1 ])
260
278
X_train_knn = np .swapaxes (X_train_knn , 1 , 2 )
261
279
262
280
_ , nn = knn .kneighbors (X_train_knn )
263
- # print("TARGETLABELS", [t[0] for t in target_labels], [int(nn[0][0])])
264
281
nn_idx = None
265
282
try :
266
283
nn_idx = [t [0 ] for t in target_labels ][int (nn [0 ][0 ])]
0 commit comments