forked from feilongwang92/mining-app-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidentify_trip_ends_integrated_5min_improved-18Jan.py
1942 lines (1815 loc) · 95.9 KB
/
identify_trip_ends_integrated_5min_improved-18Jan.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Env. Anaconda2-4.0.0-Windows-x86_64; 2019.03 does not work good
# Created on 5/20/2018 by WFL
# gps: trace segmentation clustering
# incremental clustering for common places
# grid-splitting for oscillation
# Cellular: incremental clustering for both stays and oscillations
# Combine cellular and gps stays; do another oscillation check via incremental clustering stays
# Excluding those having no stays and only having one stays during the entire study period
# Improved on 04/18/2019 for efficiency
from __future__ import print_function
infile_workdir = 'E:\\cuebiq_psrc_2019\\sorted\\'
outfile_workdir = 'E:\\cuebiq_psrc_2019\\processed\\201811\\'
### Important arguments ###
part_num = '00' # which data part to run
user_num_in_mem = 5000 # read how many users from the data into memory; depending on your PC memory size
dur_constr = 300 # seconds
spat_constr_gps = 0.20 # Km
spat_constr_cell = 1.0#1.0 # Km
spat_cell_split = 100 # meters; for spliting gps and cellular
import csv, time, collections, sys, os, gzip, copy, random, psutil
import numpy as np
from scipy import stats
from math import cos, asin, sqrt
import matplotlib.pyplot as plt
from operator import itemgetter
from random import randint
from multiprocessing import Pool
from operator import itemgetter, add
from itertools import combinations
from multiprocessing import current_process, Lock, cpu_count
import shutil, glob
# from geopy.distance import geodesic
def init(l):
global lock
lock = l
def distance(lat1, lon1, lat2, lon2):
# return geodesic((lat1, lon1),(lat2, lon2)).km
lat1 = float(lat1)
lon1 = float(lon1)
lat2 = float(lat2)
lon2 = float(lon2)
p = 0.017453292519943295
a = 0.5 - cos((lat2 - lat1) * p) / 2 + cos(lat1 * p) * cos(lat2 * p) * (1 - cos((lon2 - lon1) * p)) / 2
return 12742 * asin(sqrt(a))
# print (distance(47.628,-122.248,47.627,-122.248))
def update_duration(user):
for d in user.keys():
for trace in user[d]: trace[9] = -1 # clear needed! #modify grid
i = 0
j = i
while i < len(user[d]):
if j >= len(user[d]): # a day ending with a stay, j goes beyond the last observation
dur = str(int(user[d][j - 1][0]) + max(0, int(user[d][j - 1][9])) - int(user[d][i][0]))
for k in range(i, j, 1):
user[d][k][9] = dur
break
if user[d][j][6] == user[d][i][6] and user[d][j][7] == user[d][i][7] and j < len(user[d]):
j += 1
else:
dur = str(int(user[d][j - 1][0]) + max(0, int(user[d][j - 1][9])) - int(user[d][i][0]))
for k in range(i, j, 1):
user[d][k][9] = dur
i = j
return user
class cluster:
def __init__(self):
# self.name = name
self.pList = []
self.center = [0, 0]
self.radius = 0
def addPoint(self, point):
self.pList.append((float(point[0]),float(point[1])))
def updateCenter(self):
self.center[0] = np.mean([p[0] for p in self.pList])
self.center[1] = np.mean([p[1] for p in self.pList])
def distance_C_point(self, point):
self.updateCenter()
return distance(self.center[0], self.center[1], point[0], point[1])
def radiusC(self):
self.updateCenter()
r = 0
for p in self.pList:
d = distance(p[0], p[1], self.center[0], self.center[1])
if d > r:
r = d
return r
def has(self, point):
if [float(point[0]), float(point[1])] in self.pList:
return True
return False
def erase(self):
self.pList = []
self.center = [0, 0]
def empty(self):
if len(self.pList) == 0:
return True
return False
def oscillation_h1_oscill(user, dur_constr):
user = user#arg[0]
TimeWindow = dur_constr#arg[1]#5 * 60
oscillgpspairlist = []
tracelist = []
for d in sorted(user.keys()):
for trace in user[d]:
dur_i = 1 if int(trace[9]) == -1 else int(trace[9])
tracelist.append([trace[1], trace[0], dur_i, trace[6], trace[7], trace[8], 1, 1])
# integrate: only one record representing one stay (i-i records)
i = 0
while i < len(tracelist) - 1:
if tracelist[i + 1][2:5] == tracelist[i][2:5]:
del tracelist[i + 1]
else:
i += 1
flag_ppfound = False
# get gps list from tracelist
gpslist = [(trace[3], trace[4]) for trace in tracelist]
# unique gps list
uniqList = list(set(gpslist))
# give uniq code
tracelistno_original = [uniqList.index(gps) for gps in gpslist]
# count duration of each gps_no
gpsno_dur_count = {item: 0 for item in set(tracelistno_original)}
for t in range(len(tracelist)):
if int(tracelist[t][2]) == 0:
gpsno_dur_count[tracelistno_original[t]] += 1
else:
gpsno_dur_count[tracelistno_original[t]] += int(tracelist[t][2])
# All prepared
oscillation_pairs = []
t_start = 0
# replace pong by ping; be aware that "tracelistno_original==tracelist"
flag_find_circle = False
while t_start < len(tracelist):
flag_find_circle = False
suspSequence = []
suspSequence.append(t_start)
for t in range(t_start + 1, len(tracelist)): # get the suspicious sequence
if int(tracelist[t][1]) <= int(tracelist[t_start][1]) + int(tracelist[t_start][2]) + TimeWindow:
suspSequence.append(t)
if tracelist[t][3:5] == tracelist[t_start][3:5]:
flag_find_circle = True
break
else:
break
suspSequence_gpsno = [tracelistno_original[t] for t in suspSequence]
# check circles
# if len(set(suspSequence_gpsno)) < len(suspSequence_gpsno): # implying a circle in it
if flag_find_circle == True and len(set(suspSequence_gpsno)) != 1: # not itself
flag_ppfound = True
sequence_list = [(item, gpsno_dur_count[item]) for item in set(suspSequence_gpsno)] # ('gpsno','dur')
sequence_list = sorted(sequence_list, key=lambda x: x[1], reverse=True)
# get unique pairs
oscillation_pairs = list(
set([(sequence_list[0][0], sequence_list[i][0]) for i in range(1, len(sequence_list))]))
t_start = suspSequence[-1] # + 1
else:
t_start += 1
# record locations of oscill pairs
pinggps = [0, 0]
ponggps = [0, 0]
for pair in oscillation_pairs:
flag_ping = 0
flag_pong = 0
for ii in range(len(tracelist)): # find ping of this pair
if tracelistno_original[ii] == pair[0]:
pinggps = [tracelist[ii][3], tracelist[ii][4]]
flag_ping = 1
if tracelistno_original[ii] == pair[1]:
ponggps = [tracelist[ii][3], tracelist[ii][4]]
flag_pong = 1
if flag_ping * flag_pong == 1:
break
pairgps = [pinggps[0], pinggps[1], ponggps[0], ponggps[1]]
if pairgps not in oscillgpspairlist: oscillgpspairlist.append(pairgps)
i=0 # remove -1 in oscillpair
while i < len(oscillgpspairlist):
if -1 in oscillgpspairlist[i]:
del oscillgpspairlist[i]
else:
i += 1
# while True:
#
# for pair in oscillation_pairs: # find all pair[1]s in list, and replace it with pair[0]
# for ii in range(len(tracelist)): # find ping of this pair
# if tracelistno_original[ii] == pair[0]:
# ping = tracelist[ii]
# break
# for i in range(len(tracelist)): # replace all pong with ping
# if tracelistno_original[i] == pair[1]: # find pong
# # numOscillRecord += 1
# initime = tracelist[i][1]
# dur = tracelist[i][2] # time related attributes should remain unchanged!
# tracelist[i] = ping[:] # [:] very important!!
# tracelist[i][1] = initime
# tracelist[i][2] = dur
#
# if flag_ppfound == False:
# break
#
# # integrate duplicates (i-i records)
# i = 0
# while i < len(tracelist) - 1:
# # if tracelist[i + 1][3] == tracelist[i][3] and tracelist[i + 1][4] == tracelist[i][4] and \
# # int(tracelist[i + 1][1]) - int(tracelist[i][1]) - int(tracelist[i][2]) <= TimeWindow:
# if tracelist[i + 1][3:5] == tracelist[i][3:5]:
# tracelist[i][2] = str(int(tracelist[i + 1][1]) + int(tracelist[i + 1][2]) - int(tracelist[i][1]))
# del tracelist[i + 1]
# else:
# i += 1
return oscillgpspairlist
def cluster_agglomerative(user, Rc = 0.2):
L = []
def findClosestPair(L):
for c in L: c.updateCenter()
minDist = 1000000 #km
for i in range(len(L)-1):#what if only one c
for j in range(i+1, len(L)):
distC2C = distance(L[i].center[0],L[i].center[1],L[j].center[0],L[j].center[1])
# print (i,' ',j,' ',distC2C)
if distC2C < minDist:
minDist = distC2C
CaNo = i
CbNo = j
return (CaNo, CbNo)
def mergeCaCb(CaNo, CbNo):
Cnew = cluster()
for p in L[CaNo].pList:
Cnew.addPoint(p)
for p in L[CbNo].pList:
Cnew.addPoint(p)
return Cnew
def delCluser(L, CaNo, CbNo):
L[CaNo].erase()
L[CbNo].erase()
while True:
flag_del = False
for c in L:
if c.empty():
L.remove(c)
flag_del = True
break
if flag_del == False:
break
for day in user.keys():
traj = user[day]
for trace in traj:
if float(trace[9]) >= 5 * 60:
p = [trace[6], trace[7]]
if len(L) == 0:
Cnew = cluster()
Cnew.addPoint(p)
L.append(Cnew)
elif not any([c.has(p) for c in L]):
Cnew = cluster()
Cnew.addPoint(p)
L.append(Cnew)
while True:
if len(L) < 2:
break
closestPair = findClosestPair(L)
Cnew = mergeCaCb(closestPair[0], closestPair[1])
if Cnew.radiusC() > Rc:
break
else:
delCluser(L, closestPair[0], closestPair[1])
L.append(Cnew)
# cal radius
radiusPool = [[] for _ in range(len(L))]#L[i].radius = int(1000*L[i].radiusC())
for day in user.keys():
for trace in user[day]:
gps = [trace[6], trace[7]]
for i in range(len(L)):
if L[i].has(gps):
radiusPool[i].append([trace[3], trace[4]])
break
for c in L:
c.updateCenter()
for i in range(len(radiusPool)):
L[i].radius = 0
for j in range(len(radiusPool[i])):
dist_k = int(distance(L[i].center[0], L[i].center[1], radiusPool[i][j][0], radiusPool[i][j][1]) * 1000)
if dist_k > L[i].radius: L[i].radius = dist_k
for day in user.keys():
for trace in user[day]:
gps = [trace[6], trace[7]]
for i in range(len(L)):
if L[i].has(gps):
trace[6] = str(L[i].center[0])
trace[7] = str(L[i].center[1])
trace[8] = L[i].radius
trace[10] = 'stay_' + str(i)
break
L = []
return user
def K_meansCluster(L):
uniqMonthGPSList = []
for c in L:
uniqMonthGPSList.extend(c.pList)
Kcluster = [c.pList for c in L]
while True:
KcenterList = [(np.mean([p[0] for p in c]), np.mean([p[1] for p in c])) for c in Kcluster]
Kcluster = [[] for _ in range(len(Kcluster))]
for point in uniqMonthGPSList:
closestCluIndex = -1
closestDist2Clu = 1000000
for i in range(len(KcenterList)):
distP2C = distance(KcenterList[i][0], KcenterList[i][1], point[0], point[1])
if closestDist2Clu > distP2C:
closestDist2Clu = distP2C
closestCluIndex = i
Kcluster[closestCluIndex].append(point)
i = 0
while i < len(Kcluster):
if len(Kcluster[i]) == 0:
del Kcluster[i]
else:
i += 1
FlagChanged = False
for i in range(len(Kcluster)):
cent = (np.mean([p[0] for p in Kcluster[i]]), np.mean([p[1] for p in Kcluster[i]]))
if cent != KcenterList[i]:
FlagChanged = True
break
if FlagChanged == False:
break
return L
def cluster_incremental(user, spat_constr, dur_constr):
L = []
spat_constr = spat_constr #200.0/1000 #0.2Km
dur_constr = dur_constr#modify grid
MonthGPSList = list(set([(trace[6], trace[7]) for d in user.keys() for trace in user[d] if int(trace[9]) >= dur_constr]))# modify grid # only cluster stays
if len(MonthGPSList) == 0:
return (user)
Cnew = cluster()
Cnew.addPoint(MonthGPSList[0])
L.append(Cnew)
Ccurrent = Cnew
for i in range(1, len(MonthGPSList)):
if Ccurrent.distance_C_point(MonthGPSList[i]) < spat_constr:
Ccurrent.addPoint(MonthGPSList[i])
else:
Ccurrent = None
for C in L:
if C.distance_C_point(MonthGPSList[i]) < spat_constr:
C.addPoint(MonthGPSList[i])
Ccurrent = C
break
if Ccurrent == None:
Cnew = cluster()
Cnew.addPoint(MonthGPSList[i])
L.append(Cnew)
Ccurrent = Cnew
L = K_meansCluster(L)
uniqMonthGPSList = {}
for c in L:
r = int(1000*c.radiusC()) #
cent = [str(np.mean([p[0] for p in c.pList])), str(np.mean([p[1] for p in c.pList]))]
for p in c.pList:
uniqMonthGPSList[(str(p[0]),str(p[1]))] = (cent[0], cent[1], r)
for d in user.keys():
for trace in user[d]:
if (trace[6], trace[7]) in uniqMonthGPSList:
trace[6], trace[7], trace[8] = uniqMonthGPSList[(trace[6], trace[7])][0],\
uniqMonthGPSList[(trace[6], trace[7])][1],\
max(uniqMonthGPSList[(trace[6], trace[7])][2],int(trace[8]))
return (user)
def diameterExceedCnstr(traj,i,j,spat_constr):
#The Diameter function computes the greatest distance between any two locations in a set and compare with constraint
# remember, distance() is costly
loc = list(set([(round(float(traj[m][3]),5),round(float(traj[m][4]),5)) for m in range(i,j+1)]))# unique locations
if len(loc) <= 1:
return False
if distance(traj[i][3],traj[i][4],traj[j][3],traj[j][4])>spat_constr: # check the first and last trace
return True
else:
# guess the max distance pair; approximate distance
pairloc = list(combinations(loc, 2))
max_i = 0
max_d = 0
for i in range(len(pairloc)):
appx_d = abs(pairloc[i][0][0] - pairloc[i][1][0]) \
+ abs(pairloc[i][0][1] - pairloc[i][1][1])
if appx_d > max_d:
max_d = appx_d
max_i = i
if distance(pairloc[max_i][0][0], pairloc[max_i][0][1], pairloc[max_i][1][0],
pairloc[max_i][1][1]) > spat_constr:
return True
else:
#try to reduce the size of pairloc
max_ln_lat = (abs(pairloc[max_i][0][0] - pairloc[max_i][1][0]),
abs(pairloc[max_i][0][1] - pairloc[max_i][1][1]))
m = 0
while m < len(pairloc):
if abs(pairloc[m][0][0] - pairloc[m][1][0]) < max_ln_lat[0] \
and abs(pairloc[m][0][1] - pairloc[m][1][1]) < max_ln_lat[1]:
del pairloc[m]
else:
m += 1
diam_list = [distance(pair[0][0], pair[0][1], pair[1][0], pair[1][1]) for pair in pairloc]
if max(diam_list) > spat_constr:
return True
else:
return False
def clusterGPS(arg):
user = arg[0]
dur_constr = arg[1]
spat_constr = arg[2]
for day in user.keys():
traj = user[day]
i = 0
while (i<len(traj)-1):
j = i
flag = False
while (int(traj[j][0])-int(traj[i][0])<dur_constr):#j=min k s.t. traj_k - traj_i >= dur
j+=1
if (j==len(traj)):
flag = True
break
if flag:
break
if diameterExceedCnstr(traj,i,j,spat_constr):
i += 1
# print('exceed: ',i)
else:
# print(i)
j_prime = j
gps_set = set([(round(float(traj[m][3]),5),round(float(traj[m][4]),5)) for m in range(i,j+1)])
for k in range(j_prime+1, len(traj),1): # #j: max k subject to Diameter(R,i,k)<=spat_constraint
if (round(float(traj[k][3]), 5), round(float(traj[k][4]), 5)) in gps_set:
j = k
elif not diameterExceedCnstr(traj,i,k, spat_constr):
j = k
gps_set.add((round(float(traj[k][3]), 5), round(float(traj[k][4]), 5)))
else:
break
mean_lat, mean_long = str(np.mean([float(traj[k][3]) for k in range(i,j+1)])), \
str(np.mean([float(traj[k][4]) for k in range(i,j+1)]))
# traj[i][8] = 0 # give cluster radius #will give radius after agglomarative clustering
# for k in range(i, j + 1):
# dist_k = int(distance(mean_lat, mean_long, traj[k][3], traj[k][4])*1000)
# if dist_k > traj[i][8]: traj[i][8] = dist_k
dur = str(int(traj[j][0]) - int(traj[i][0])) # give duration
for k in range(i, j + 1): # give cluster center
traj[k][6], traj[k][7], traj[k][9] = mean_lat, mean_long, dur
# traj[k][8] = traj[i][8]
i = j+1
user[day] = traj
# for day in user.keys():
# for trace in user[day]:
# if float(trace[6])==-1: trace[6], trace[7] = trace[3], trace[4]
#incremental clustering
# user = cluster_agglomerative(user)
user = cluster_incremental(user, spat_constr, dur_constr)
#for those not clustered; use grid
# modify grid
MonthGPSList = list(set([(trace[6], trace[7], trace[8]) for d in user.keys() for trace in user[d] if int(trace[9]) >= dur_constr]))
for day in user.keys(): # modify grid
for trace in user[day]:
if float(trace[6]) == -1:
found_stay = False
for stay_i in MonthGPSList: #first check those observations belong to a gps stay or not
if distance(stay_i[0], stay_i[1], trace[3], trace[4]) < spat_constr:
trace[6], trace[7], trace[8] = stay_i[0], stay_i[1], stay_i[2]
found_stay = True
break
if found_stay == False:
trace[6] = trace[3] + '000' # in case do not have enough digits
trace[7] = trace[4] + '000'
digits = (trace[6].split('.'))[1]
digits = digits[:2] + str(int(digits[2]) / 2)
trace[6] = (trace[6].split('.'))[0] + '.' + digits
# trace[6] = trace[6][:5] + str(int(trace[6][5]) / 2) # 49.950 to 49.952 220 meters
digits = (trace[7].split('.'))[1]
digits = digits[:2] + str(int(digits[2:4]) / 25)
trace[7] = (trace[7].split('.'))[0] + '.' + digits
# trace[7] = trace[7][:7] + str(int(trace[7][7:9]) / 25) # -122.3400 to -122.3425 180 meters
# update duration
user = update_duration(user)
for d in user.keys():
# for trace in user[d]: trace[9] = -1 # clear needed! #modify grid
# i = 0
# j = i
# while i < len(user[d]):
# if j >= len(user[d]): # a day ending with a stay, j goes beyond the last observation
# dur = str(int(user[d][j - 1][0]) + max(0, int(user[d][j - 1][9])) - int(user[d][i][0]))
# for k in range(i, j, 1):
# user[d][k][9] = dur
# break
# if user[d][j][6] == user[d][i][6] and user[d][j][7] == user[d][i][7] and j < len(user[d]):
# j += 1
# else:
# dur = str(int(user[d][j - 1][0]) + max(0, int(user[d][j - 1][9])) - int(user[d][i][0]))
# for k in range(i, j, 1):
# user[d][k][9] = dur
# i = j
for trace in user[d]: # those trace with gps as -1,-1 (not clustered) should not assign a duration
if float(trace[6]) == -1: trace[9] = -1
if float(trace[9]) == 0: trace[9] = -1
# staysbefoscill = {(trace[6], trace[7]): None for d in user.keys() for trace in user[d] if
# int(trace[9]) > dur_constr}
# #oscillation
# OscillationPairList = oscillation_h1_oscill(user, dur_constr) #in format: [, [pinggps[0], pinggps[1], ponggps[0], ponggps[1]]]
# # find all pair[1]s in list, and replace it with pair[0]
# for pair in OscillationPairList:
# for d in user.keys():
# for trace in user[d]:
# if trace[6] == pair[2] and trace[7] == pair[3]:
# trace[6], trace[7] = pair[0], pair[1]
# # for those new added stays, combine with gps stay
# staysaftoscill = {(trace[6], trace[7]): None for d in user.keys() for trace in user[d] if
# int(trace[9]) > dur_constr}
# newstays = set(staysaftoscill.keys()).difference(staysbefoscill.keys())
#
### commented on april 14, 2019
# # update duration
# for d in user.keys():
# user = update_duration(user)
# # for trace in user[d]: # edit: not -1 at trace[6] any more.
# # those trace with gps as -1,-1 (not clustered) should not assign a duration
# # if float(trace[6]) == -1: trace[9] = -1
# # if float(trace[9]) == 0: trace[9] = -1
for d in user.keys():
for trace in user[d]:
if float(trace[9]) < dur_constr: # change back keep full trajectory: do not use center for those are not stays
trace[6], trace[7], trace[8], trace[9] = -1, -1, -1, -1 # for no stay, do not give center
return user
def clusterPhone(arg):
user = arg[0]
dur_constr = arg[1]
spat_constr = arg[2]
# spat_constr_cell = 1.0
L = []
# prepare
MonthGPSList = list(set([(trace[3], trace[4]) for d in user.keys() for trace in user[d]])) # not Unique, just used as a container
if len(MonthGPSList) == 0:
return (user)
Cnew = cluster()
Cnew.addPoint(MonthGPSList[0])
L.append(Cnew)
Ccurrent = Cnew
for i in range(1, len(MonthGPSList)):
if Ccurrent.distance_C_point(MonthGPSList[i]) < spat_constr:
Ccurrent.addPoint(MonthGPSList[i])
else:
Ccurrent = None
for C in L:
if C.distance_C_point(MonthGPSList[i]) < spat_constr:
C.addPoint(MonthGPSList[i])
Ccurrent = C
break
if Ccurrent == None:
Cnew = cluster()
Cnew.addPoint(MonthGPSList[i])
L.append(Cnew)
Ccurrent = Cnew
L = K_meansCluster(L)
uniqMonthGPSList = {}
for c in L:
r = int(1000*c.radiusC()) #
cent = [str(np.mean([p[0] for p in c.pList])), str(np.mean([p[1] for p in c.pList]))]
for p in c.pList:
uniqMonthGPSList[(str(p[0]),str(p[1]))] = (cent[0], cent[1], r)
for d in user.keys():
for trace in user[d]:
if (trace[3], trace[4]) in uniqMonthGPSList:
trace[6], trace[7], trace[8] = uniqMonthGPSList[(trace[3], trace[4])][0],\
uniqMonthGPSList[(trace[3], trace[4])][1],\
max(uniqMonthGPSList[(trace[3], trace[4])][2],int(trace[5]))
# update duration
user = update_duration(user)
for d in user.keys():
# for trace in user[d]: trace[9] = -1
# i = 0
# j = i
# while i < len(user[d]):
# if j >= len(user[d]): # a day ending with a stay, j goes beyond the last observation
# dur = str(int(user[d][j - 1][0]) + max(0, int(user[d][j - 1][9])) - int(user[d][i][0]))
# for k in range(i, j, 1):
# user[d][k][9] = dur
# break
# if user[d][j][6] == user[d][i][6] and user[d][j][7] == user[d][i][7] and j < len(user[d]):
# j += 1
# else:
# dur = str(int(user[d][j - 1][0]) + max(0, int(user[d][j - 1][9])) - int(user[d][i][0]))
# for k in range(i, j, 1):
# user[d][k][9] = dur
# i = j
for trace in user[d]: # those trace with gps as -1,-1 (not clustered) should not assign a duration
if float(trace[6]) == -1: trace[9] = -1
if float(trace[9]) == 0: trace[9] = -1
#oscillation
OscillationPairList = oscillation_h1_oscill(user, dur_constr) #in format: [, [pinggps[0], pinggps[1], ponggps[0], ponggps[1]]]
# find all pair[1]s in list, and replace it with pair[0]
for pair in OscillationPairList:
for d in user.keys():
for trace in user[d]:
if trace[6] == pair[2] and trace[7] == pair[3]:
trace[6], trace[7] = pair[0], pair[1]
# update duration
user = update_duration(user)
for d in user.keys():
for trace in user[d]: # those trace with gps as -1,-1 (not clustered) should not assign a duration
if float(trace[6]) == -1: trace[9] = -1
if float(trace[9]) == 0: trace[9] = -1
# # ijij oscillation
# OscillationPairList = oscillation_ijij_revision(user, dur_constr)
# # in format: [, [pinggps[0], pinggps[1], ponggps[0], ponggps[1]]]
# # find all pair[1]s in list, and replace it with pair[0]
# for pair in OscillationPairList:
# for d in user.keys():
# for trace in user[d]:
# if trace[6] == pair[2] and trace[7] == pair[3]:
# trace[6], trace[7] = pair[0], pair[1]
#
# # update duration
# for d in user.keys():
# user = update_duration(user)
return user
def combineGPSandPhoneStops(arg):
user_gps = arg[0]
user_cell = arg[1]
dur_constr = arg[2]
spat_constr_gps = arg[3]
spat_cell_split = arg[4]
# combine cellular stay if it is close to a gps stay
cell_stays = list(set([(trace[6],trace[7]) for d in user_cell for trace in user_cell[d] if int(trace[9]) >= dur_constr]))
gps_stays = list(set([(trace[6],trace[7]) for d in user_gps for trace in user_gps[d] if int(trace[9]) >= dur_constr]))
pairs_close = set()
for cell_stay in cell_stays:
for gps_stay in gps_stays:
if distance(cell_stay[0],cell_stay[1],gps_stay[0],gps_stay[1])<=spat_constr_gps:
pairs_close.add((gps_stay[0],gps_stay[1],cell_stay[0],cell_stay[1]))
break
# find all pair[1]s in list, and replace it with pair[0]
for pair in list(pairs_close):
for d in user_cell.keys():
for trace in user_cell[d]:
if trace[6] == pair[2] and trace[7] == pair[3]:
trace[5], trace[6], trace[7] = 99, pair[0], pair[1] #pretend as gps
user = user_gps
for d in user.keys():
if len(user_cell[d]):
user[d].extend(user_cell[d])
user[d] = sorted(user[d], key=itemgetter(0))
# address oscillation
OscillationPairList = oscillation_h1_oscill(user, dur_constr) # in format: [, [ping[0], ping[1], pong[0], pong[1]]]
gpslist_temp = {(trace[6], trace[7]):int(trace[5]) for d in user.keys() for trace in user[d]}
for pair_i in range(len(OscillationPairList)):#when replaced, can only replaced with a gps stay
if gpslist_temp[(OscillationPairList[pair_i][0],OscillationPairList[pair_i][1])] <= spat_constr_gps:
OscillationPairList[pair_i] = [OscillationPairList[pair_i][2],OscillationPairList[pair_i][3],
OscillationPairList[pair_i][0],OscillationPairList[pair_i][1]]
# find all pair[1]s in list, and replace it with pair[0]
for pair in OscillationPairList:
for d in user.keys():
for trace in user[d]:
if (trace[6], trace[7]) == (trace[2], trace[3]):
trace[6], trace[7] = pair[0], pair[1]
# update duration
user = update_duration(user)
for d in user:
for trace in user[d]: # those trace with gps as -1,-1 (not clustered) should not assign a duration
if float(trace[6]) == -1: trace[9] = -1
for d in user:
phone_index = [k for k in range(len(user[d])) if int(user[d][k][5]) > spat_cell_split]
if len(phone_index) == 0: # if no phone trace
continue
for i in range(len(user[d])):
if int(user[d][i][5]) > spat_cell_split and int(user[d][i][9]) < dur_constr: # passing phone observ
user[d][i].append('checked')
# combine consecutive obsv on a phone stay into two observ
i = min(phone_index) # i has to be a phone index
j = i + 1
while i < len(user[d]) - 1:
if j >= len(user[d]): # a day ending with a stay, j goes beyond the last observation
for k in range(i + 1, j - 1, 1):
user[d][k] = []
break
if int(user[d][j][5]) > spat_cell_split and user[d][j][6] == user[d][i][6] \
and user[d][j][7] == user[d][i][7] and j < len(user[d]):
j += 1
else:
for k in range(i + 1, j - 1, 1):
user[d][k] = []
phone_index = [k for k in range(j, len(user[d])) if int(user[d][k][5]) > spat_cell_split]
if len(phone_index) < 3: # if no phone trace
break
i = min(phone_index) ##i has to be a phone index
j = i + 1
i = 0 # remove []
while i < len(user[d]):
if len(user[d][i]) == 0:
del user[d][i]
else:
i += 1
# adress phone stay one by one
flag_changed = True
phone_list_check = []
while (flag_changed):
# print('while........')
flag_changed = False
gps_list = []
phone_list = []
for i in range(len(user[d])):
if int(user[d][i][5]) <= spat_cell_split:#or user[d][i][2] == 'addedphonestay': #changed on 0428
gps_list.append(user[d][i])
else:
phone_list.append(user[d][i])
# # update gps stay
# i = 0
# j = i
# while i < len(gps_list):
# if j >= len(gps_list): # a day ending with a stay, j goes beyond the last observation
# dur = str(int(gps_list[j - 1][0]) - int(gps_list[i][0]))
# for k in range(i, j, 1):
# gps_list[k][9] = dur
# break
# if gps_list[j][6] == gps_list[i][6] and gps_list[j][7] == gps_list[i][7] and j < len(
# gps_list):
# j += 1
# else:
# dur = str(int(gps_list[j - 1][0]) - int(gps_list[i][0]))
# for k in range(i, j, 1):
# gps_list[k][9] = dur
# i = j
# for trace in gps_list: # those trace with gps as -1,-1 (not clustered) should not assign a duration
# if int(trace[6]) == -1: trace[9] = -1
# if len(gps_list) == 1: gps_list[0][9] = -1
phone_list.extend(phone_list_check)
# when updating duration for phone stay, we have to put back passing obs
phone_list = sorted(phone_list, key=itemgetter(0))
# update phone stay
i = 0
j = i
while i < len(phone_list):
if j >= len(phone_list): # a day ending with a stay, j goes beyond the last observation
dur = str(int(phone_list[j - 1][0]) - int(phone_list[i][0]))
for k in range(i, j, 1):
if int(phone_list[k][9]) >= dur_constr:
# we don't want to change a pssing into a stay; as we have not process the combine this stay
# this is possible when a stay that prevents two passing is mergeed into gps as gps points
phone_list[k][9] = dur
break
if phone_list[j][6] == phone_list[i][6] and phone_list[j][7] == phone_list[i][7] and j < len(
phone_list):
j += 1
else:
dur = str(int(phone_list[j - 1][0]) - int(phone_list[i][0]))
for k in range(i, j, 1):
if int(phone_list[k][9]) >= dur_constr:
phone_list[k][9] = dur
i = j
for trace in phone_list: # those trace with gps as -1,-1 (not clustered) should not assign a duration
if float(trace[6]) == -1: trace[9] = -1
if len(phone_list) == 1: phone_list[0][9] = -1
# update check lable
for i in range(len(phone_list)):
if int(phone_list[i][5]) > spat_cell_split and int(phone_list[i][9]) < dur_constr \
and phone_list[i][-1] != 'checked':
# passing phone observ
phone_list[i].append('checked')
# put those not checked together with gps
user[d] = gps_list
phone_list_check = []
for i in range(len(phone_list)):
if phone_list[i][-1] == 'checked':
phone_list_check.append(phone_list[i])
else:
user[d].append(phone_list[i])
user[d] = sorted(user[d], key=itemgetter(0))
# find a stay which is not checked
flag_phonestay_notchecked = False
phonestay_left, phonestay_right = -1, -1
for i in range(max(0, phonestay_right+1), len(user[d])):
phonestay_left, phonestay_right = -1, -1
if int(user[d][i][5]) > spat_cell_split \
and int(user[d][i][9]) >= dur_constr and user[d][i][-1] != 'checked':
phonestay_left = phonestay_right
phonestay_right = i
if phonestay_left != -1 and phonestay_right != -1 \
and user[d][phonestay_left][9] == user[d][phonestay_right][9]:
flag_phonestay_notchecked = True
## modified on 04152019
if flag_phonestay_notchecked == False or len(phone_list) == 0: # if all phone observation are checked, end
break
# if they are not two consecutive observation
if phonestay_right != phonestay_left + 1: # attention: only phonestay_left is addressed
# not consecutive two observations
if any([int(user[d][j][9]) >= dur_constr for j in range(phonestay_left + 1, phonestay_right, 1)]):
# found a gps stay in betw
# print('23: found a gps stay in betw, just use one gps stay trade one phone stay')
temp = user[d][phonestay_left][6:]
user[d][phonestay_left][6:] = [-1, -1, -1, -1, -1, -1] # phone disappear
# user[d][phonestay_left].extend(temp)
user[d][phonestay_left].append('checked')
# del user[d][phonestay_left] # phone disappear
flag_changed = True
else: # find close gps
# print('24: do not found a gps stay in betw')
phone_uncernt = max([int(user[d][phonestay_left][8]), int(user[d][phonestay_left][5]),
int(user[d][phonestay_right][5])])
if all([(phone_uncernt + int(user[d][j][5])) > 1000 * distance(user[d][j][3], user[d][j][4],
user[d][phonestay_left][6],
user[d][phonestay_left][7])
for j in range(phonestay_left + 1, phonestay_right, 1)]):
#total uncerty larger than distance
# this case should be rare, as those close gps may be clustered
# print('241: all gps falling betw are close with phone stay')
temp = user[d][phonestay_left][3:] # copy neighbor gps
user[d][phonestay_left][3:] = user[d][phonestay_left + 1][3:]
user[d][phonestay_left][11] = temp[8]
# user[d][phonestay_left].extend(temp)
flag_changed = True
else:
# print('242: find a gps in betw,
# which is far away with phone stay, contradic with a stay (with phone obsv)')
temp = user[d][phonestay_left][6:]
user[d][phonestay_left][6:] = [-1, -1, -1, -1, -1, -1] # phone disappear
# user[d][phonestay_left].extend(temp)
user[d][phonestay_left].append('checked')
# del user[d][phonestay_left] # phone disappear
flag_changed = True
else: # if they are two consecutive traces
# two consecutive observation
# if phonestay_left != 0 and phonestay_right < len(user[d]) - 1:
# ignore if they are at the beginning or the end of traj
prev_gps = next_gps = 0 # find prevous and next gps
found_prev_gps = False
found_next_gps = False
for prev in range(phonestay_left - 1, -1, -1):
# if int(user[d][prev][5]) <= spat_cell_split: ########## changed on 04282018
if int(user[d][prev][5]) <= spat_cell_split and int(user[d][prev][9]) >= dur_constr:
prev_gps = prev
found_prev_gps = True
break
for nxt in range(phonestay_right + 1, len(user[d])):
if int(user[d][nxt][5]) <= spat_cell_split and int(user[d][nxt][9]) >= dur_constr:
next_gps = nxt
found_next_gps = True
break
if found_prev_gps and found_next_gps and user[d][prev_gps][6] == user[d][next_gps][6]:
# this is a phone stay within a gps stay
phone_uncernt = max([int(user[d][phonestay_left][8]), int(user[d][phonestay_left][5]),
int(user[d][phonestay_right][5])])
gps_uncernt = int(user[d][prev_gps][8])
dist = 1000 * distance(user[d][prev_gps][6],
user[d][prev_gps][7],
user[d][phonestay_left][6],
user[d][phonestay_left][7])
speed_dep = (dist - phone_uncernt - gps_uncernt) / \
(int(user[d][phonestay_left][0]) - int(user[d][prev_gps][0])) * 3.6
speed_retn = (dist - phone_uncernt - gps_uncernt) / \
(int(user[d][next_gps][0]) - int(user[d][phonestay_right][0])) * 3.6
if (dist - phone_uncernt - gps_uncernt) > 0 \
and dist > 1000*spat_constr_gps and speed_dep < 200 and speed_retn < 200:
# print('1111: distance larger than acc, and can travel, add phone stay, shorten gps stay')
# leave phone stay there, we later update duration for the gps stay
user[d][phonestay_left].append('checked')
# those phone stay not removed have to be marked with 'checked'!
user[d][phonestay_right].append('checked')
user[d][phonestay_left][2] = 'addedphonestay'
user[d][phonestay_right][2] = 'addedphonestay'
flag_changed = True
else: # merge into gps stay
# print('1112: distance less than acc, or cannot travel, merge into gps stay')
temp = user[d][phonestay_left][3:]
user[d][phonestay_left][3:] = user[d][prev_gps][3:]
user[d][phonestay_left][11] = temp[8]
# user[d][phonestay_left].extend(temp)
temp = user[d][phonestay_right][3:]
user[d][phonestay_right][3:] = user[d][prev_gps][3:]
user[d][phonestay_right][11] = temp[8]
# user[d][phonestay_right].extend(temp)
flag_changed = True
elif found_prev_gps and found_next_gps and user[d][prev_gps][6] != user[d][next_gps][6]:
phone_uncernt_l = max([int(user[d][phonestay_left][8]), int(user[d][phonestay_left][5]),
int(user[d][phonestay_right][5])])
gps_uncernt_l = int(user[d][prev_gps][8])
dist_l = 1000 * distance(user[d][prev_gps][6],
user[d][prev_gps][7],
user[d][phonestay_left][6],
user[d][phonestay_left][7])
speed_dep = (dist_l - phone_uncernt_l - gps_uncernt_l) / \
(int(user[d][phonestay_left][0]) - int(user[d][prev_gps][0])) * 3.6
phone_uncernt_r = max([int(user[d][phonestay_left][8]), int(user[d][phonestay_left][5]),
int(user[d][phonestay_right][5])])
gps_uncernt_r = int(user[d][next_gps][8])
dist_r = 1000 * distance(user[d][next_gps][6],
user[d][next_gps][7],
user[d][phonestay_right][6],
user[d][phonestay_right][7])
speed_retn = (dist_r - phone_uncernt_r - gps_uncernt_r) / \
(int(user[d][next_gps][0]) - int(user[d][phonestay_right][0])) * 3.6
comb_l = 0 #revised on 03202019 to pick up one gps stay to combine with; if spatial conti with multi
comb_r = 0
if (dist_l - phone_uncernt_l - gps_uncernt_l) < 0 \
or dist_l < 1000*spat_constr_gps or speed_dep > 200:
comb_l = 1
if (dist_r - phone_uncernt_r - gps_uncernt_r) < 0 \
or dist_r < 1000 * spat_constr_gps or speed_retn > 200:
comb_r = 1
if comb_l*comb_r == 1:
if dist_l < dist_r:
comb_r = 0
else:
comb_l = 0
if comb_l:
temp = user[d][phonestay_left][3:]
user[d][phonestay_left][3:] = user[d][prev_gps][3:]