-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprototype_extractor.py
executable file
·84 lines (57 loc) · 2 KB
/
prototype_extractor.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
import sys
import time
import os
import glob
import itertools
from dtw import dtw
from itertools import combinations
from itertools import permutations
from itertools import combinations_with_replacement
first_run = None
directory = sys.argv[1]
samples = r"."+"/"+directory+"/training/"+"*.dat"
filenames = []
time_list = []
count = 0
first_run = None
minimum = None
maximum = None
prototype = None
prototype_max = None
distance_dict = {}
somma = None
for path in glob.glob(samples):
dirname, filename = os.path.split(path)
filename_split = filename.split('-')
sample_family = filename_split[1]
id = filename_split[0]
sample_load = [int(x) for x in open("./"+directory+"/training/"+filename).readlines()]
filenames.append(filename)
time_list.append(sample_load)
time_combinations = list(combinations(time_list, 2))
file_combinations = list(combinations(filenames, 2))
for round in range(0, len(file_combinations)):
filenameA = str(file_combinations[count][0])
filenameB = str(file_combinations[count][1])
#if filenameA == filenameB:
#print "Files are the same, not calculating distance"
#count = count + 1
dist, cost, path = dtw(time_combinations[count][0], time_combinations[count][1])
distance_dict.setdefault(filenameA, []).append(dist)
distance_dict.setdefault(filenameB, []).append(dist)
print "Distance between "+str(file_combinations[count][0])+" and "+str(file_combinations[count][1])+" is :"+str(dist)
count = count + 1
somma_list = []
for key in distance_dict:
somma = sum(distance_dict[key])
somma_list.append(somma)
if minimum is None or float(somma) < minimum:
minimum = somma
prototype = key
if maximum is None or float(somma) > maximum:
maximum = somma
prototype_max = key
media_somma = (sum(somma_list)/len(somma_list))
print("PROTOTYPE: "+str(prototype)+"with sum distances of : "+str(minimum))
print("PROTOTYPE: "+str(prototype_max)+"with sum distances of : "+str(maximum))
print("La distanza media intracluster :"+str(media_somma))