forked from XifengGuo/DCEC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcec_test.py
164 lines (148 loc) · 6.57 KB
/
dcec_test.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import argparse
import os
from pathlib import Path
import numpy as np
from keras.utils.vis_utils import plot_model
from sklearn.preprocessing import MinMaxScaler, FunctionTransformer
from traffic.core import Traffic
from traffic import algorithms
from dcec.utils import input_shape, input_shape1d, input_shape_dense, input_shape_local1d
from dcec.clustering import DCEC
from dcec.model import CAE, CAE1d, dense, local1d
import generateparamlist
import tensorflow as tf
from keras import backend as K
tf.config.threading.set_intra_op_parallelism_threads(2)
tf.config.threading.set_inter_op_parallelism_threads(2)
#conf =K.tf.compat.v1.ConfigProto(device_count={'CPU': 1},
# intra_op_parallelism_threads=2,
# inter_op_parallelism_threads=2)
#K.set_session(tf.Session(config=conf))
# def get_trajs_from_index_list(t,indexes):
# lres = [None] * len(indexes)
# lindexsort = sorted(enumerate(indexes),key= lambda x:x[-1],reverse=True)
# ires, it = lindexsort.pop()
# for i,ti in enumerate(t):
# if i == it:
# lres[ires]=ti
# if lindexsort == []:
# break
# else:
# ires, it = lindexsort.pop()
# return sum(lres)
def str2intlist(l):
return [int(x) for x in l.split("_")]
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="train")
parser.add_argument("--data_path", type=Path, default="data_64/lszh.parquet")
parser.add_argument("--n_clusters", default=5, type=int)
parser.add_argument("--epochs", default=1000, type=int)
parser.add_argument("--batch_size", default=1000, type=int)
parser.add_argument("--maxiter", default=10000, type=int)
parser.add_argument("--update_interval", default=140, type=int)
parser.add_argument("--save_dir", default=None)
parser.add_argument("--model", default="dense")
parser.add_argument("--lambda_kl", type=float, default=0.)
parser.add_argument("--filters", type=str, default="8_16_32")
parser.add_argument("--kernels", type=str, default="8_16_32")
parser.add_argument("--archidense", type=str, default="8_16_32")
parser.add_argument("--train_test", type=bool, default=True)
parser.add_argument("--csvlog", default="toto.csv")
parser.add_argument("--search", type=int, default=None)
args = parser.parse_args()
if args.search is not None:
r = generateparamlist.testrandomgen()
args.filters = r["--filters"][args.search]
args.kernels = r["--kernels"][args.search]
args.archidense = r["--archidense"][args.search]
args.model = r["--model"][args.search]
print("search",args.search,args.filters,args.kernels)
filters= str2intlist(args.filters)
kernels= str2intlist(args.kernels)
archidense = str2intlist(args.archidense)
if args.model == "dense":
sel_input_shape, sel_cae = input_shape_dense, lambda input_shape: dense(input_shape,archidense)
elif args.model == "cae2d":
sel_input_shape, sel_cae = input_shape, lambda input_shape: CAE(input_shape,filters,kernels)
elif args.model == "cae1d":
sel_input_shape, sel_cae = input_shape1d, lambda input_shape: CAE1d(input_shape,filters,kernels)
elif args.model == "local1d":
sel_input_shape, sel_cae = input_shape_local1d, lambda input_shape: local1d(input_shape,filters,kernels)
else:
raise Exception("bad model:", args.model)
print(args)
if args.save_dir is not None and not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
tall = Traffic.from_file(args.data_path)#.sample(frac=1).reset_index(drop=True)
# tall.data = tall.data.sample(frac=1).reset_index(drop=True)
if args.train_test:
toto = np.array([f for f in list(tall.flight_ids)])#()
print("toto.shape",toto.shape)
indexes = np.random.permutation(len(tall))
ntrain = int(len(tall) * 0.9)
t =tall[list(toto[indexes[:ntrain]])]#get_trajs_from_index_list(tall,[int(i) for i in indexes[:ntrain]])
ttest = tall[list(toto[indexes[ntrain:]])]#get_trajs_from_index_list(tall,[int(i) for i in indexes[ntrain:]])
else:
t = tall
ttest = tall
print(tall,ttest)
# print(t[0:1],len(t))
list_features = ["track_unwrapped", "longitude", "latitude", "altitude"]
nb_flights = len(t)
nb_samples = len(t[0])
nb_features = len(list_features)
print(f"nb_flights: {nb_flights}, nb_samples={nb_samples}")
# print(vars(args))
# raise Exception
csvlog = None if args.csvlog is None else (args.csvlog, vars(args))
input_shape=sel_input_shape(nb_samples, nb_features)
print("input_shape",input_shape)
dcec = DCEC(
input_shape=input_shape,
# filters= [int(x) for x in args.filters.split("_")],
n_clusters=args.n_clusters,
alpha=1.0,
cae = sel_cae,
lambda_kl = args.lambda_kl,
batch_size=args.batch_size,
epochs=args.epochs,
maxiter=args.maxiter,
update_interval=args.update_interval,
cae_weights=None,
save_dir = args.save_dir,
csvlog = csvlog,
)
transform = MinMaxScaler(feature_range=(-1, 1))
clustering = t.clustering(
nb_samples=None,
features=list_features,
clustering=dcec,
transform=transform,
)
dcec.testfeatures = algorithms.clustering.prepare_features(ttest,nb_samples=clustering.nb_samples,features=clustering.features,projection = clustering.projection)
dcec.transform = transform
#.reshape(-1,*dcec.input_shape)
t_c = clustering.fit_predict()
# print(clustering.transform.min_,clustering.transform.scale_)
def evalpred(clustering, traff):
X = algorithms.clustering.prepare_features(traff,nb_samples=clustering.nb_samples,features=clustering.features,projection = clustering.projection)
X = clustering.transform.transform(X).reshape(-1,*dcec.input_shape)
# print(
q, _ = dcec.model.predict(X, verbose=0)
p = dcec.target_distribution(q) # update the auxiliary target distribution p
re, scores = dcec.score_samples(X)
dcec.compile(loss_weights=[0,1])
loss = dcec.model.test_on_batch(
x=X,
y=[p,X,],
)
print(loss,len(loss),np.mean(re),np.mean(scores))
dcec.compile(loss_weights=[1,0])
loss = dcec.model.test_on_batch(
x=X,
y=[p,X,],
)
print((loss),len(loss),np.mean(re),np.mean(scores))
# evalpred(clustering,ttest)
#print(t_c.groupby(["cluster"]).agg({"flight_id": "nunique"}))
# t_c.to_pickle(f"{args.save_dir}/t_c_dcec.pkl")