-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathattack.py
1476 lines (1129 loc) · 69.2 KB
/
attack.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
import geopandas as gp
import pandas as pd
from tqdm import tqdm
import numpy as np
import libpysal
import itertools
from joblib import Parallel, delayed
import random
from sklearn import metrics
from scipy.optimize import linear_sum_assignment
import tslearn.metrics
import matplotlib.pyplot as plt
import datetime
import os.path
import os
import shutil
from sklearn.metrics import confusion_matrix
from scipy.optimize import linear_sum_assignment as linear_assignment
from sklearn.cluster import AffinityPropagation, AgglomerativeClustering
# this for jsd
from PIL import Image
from scipy.spatial.distance import jensenshannon
from rpy2.situation import get_r_home
os.environ["R_HOME"] = get_r_home()
import rpy2.robjects as robjects
# Parameters
LCSS_EPS = 200
LCSS_FLIP = True
HL_SP_START_TIME = '6:00'
HL_SP_END_TIME = '10:00'
HL_EP_START_TIME = '18:00'
HL_EP_END_TIME = '0:00'
CHAINING_INFLOW_HR_DIFF_THRESHOLD = 4
CHAINING_HR_DIFF_THRESHOLD = 8
HL_SP_OUTFLOW_THRESHOLD = 2
HL_EP_OUTFLOW_THRESHOLD = 4
RANDOMIZED_SIMULTANEOUS_SEARCH_ITERATIONS = 1000
SIM_THRESH_FOR_NO_MATCH_TRIPS = 0.5
# JSD Parameters
GRID_RESOLUTION_JSD = 1000
def plot_hour_of_day_distribution(gdf):
"""Plot the distribution of trips across hour of the day.
Args:
gdf (_type_): GeoDataFrame containing the trips.
"""
# Plot the distribution of trips over the day
gdf['HOUR'] = pd.to_datetime(gdf['TRIP_START'], format='%Y-%m-%d %H:%M:%S').dt.hour
gdf['HOUR'].hist(bins=24, figsize=(10, 5), ec='black', alpha=0.5)
plt.title('Distribution of trips across the day')
plt.xlabel('Hour of the day')
plt.ylabel('Number of trips')
plt.show()
def plot_distribution_of_trip_durations(gdf):
"""Plot the distribution of trip durations.
Args:
gdf (_type_): GeoDataFrame containing the trips.
"""
# Plot the distribution of trip durations
gdf['TRIP_DURATION_IN_MINS'].hist(bins=100, figsize=(10, 5), ec='black', alpha=0.5)
# add a vertical line at the mean and label it with the mean value
plt.axvline(gdf['TRIP_DURATION_IN_MINS'].mean(), color='k', linestyle='dashed', linewidth=1)
plt.title('Distribution of trip durations')
plt.xlabel('Trip duration (mins)')
plt.ylabel('Number of trips')
plt.show()
def plot_distribution_of_trip_distances(gdf):
"""Plot the distribution of trip distances.
Args:
gdf (_type_): GeoDataFrame containing the trips.
"""
# Plot the distribution of trip distances
gdf['TRIP_LEN_IN_MTRS'].hist(bins=100, figsize=(10, 5), ec='black', alpha=0.5)
# add a vertical line at the mean and label it with the mean value
plt.axvline(gdf['TRIP_LEN_IN_MTRS'].mean(), color='k', linestyle='dashed', linewidth=1)
plt.title('Distribution of trip distances')
plt.xlabel('Trip distance (m)')
plt.ylabel('Number of trips')
plt.show()
def plot_distribution_of_number_of_trips_per_user(gdf):
"""Plot the distribution of number of trips per user.
Args:
gdf (_type_): GeoDataFrame containing the trips.
"""
# Plot the distribution of number of trips per user
gdf['PERSON_ID'].value_counts().hist(bins=40, figsize=(10, 5), ec='black', alpha=0.5)
# add a vertical line at the mean and label it with the mean value
plt.axvline(gdf['PERSON_ID'].value_counts().mean(), color='k', linestyle='dashed', linewidth=1)
plt.title('Distribution of number of trips per user')
plt.xlabel('Number of trips')
plt.ylabel('Number of users')
plt.show()
def plot_distribution_of_JSD_dist_matrix(JSD_dist_matrix):
plt.hist(JSD_dist_matrix.flatten(), bins=100)
# Add labels to plot
plt.title('Distribution of JSD')
plt.xlabel('JSD')
plt.show()
def getGroundTruth(full_trip_gdf):
# Get ground truth labels
df = full_trip_gdf.copy()
df['ID'] = df.sort_values('TRIP_ID').groupby('PERSON_ID').ngroup() # Sort TRIP ID ascending and set cluster id corresponding to PERSON_ID
ground_truth = df.sort_values('TRIP_ID').ID.to_list()
return ground_truth
def _make_cost_matrix(cm):
s = np.max(cm)
return (- cm + s)
def cluster_acc(y_true, y_pred):
"""
Calculate clustering accuracy. Require scikit-learn installed
# Arguments
y: true labels, numpy.array with shape `(n_samples,)`
y_pred: predicted labels, numpy.array with shape `(n_samples,)`
# Return
accuracy, in [0,1]
"""
y_true = np.asarray(y_true.copy())
y_pred = np.asarray(y_pred.copy())
y_true = y_true.astype(np.int64)
y_pred = y_pred.astype(np.int64)
assert y_pred.size == y_true.size
cm = confusion_matrix(y_true, y_pred)
row_ind, col_ind = linear_assignment(_make_cost_matrix(cm))
cm_permuted = cm[:, col_ind][row_ind, :]
return np.trace(cm_permuted) / np.sum(cm_permuted)
def evaluate(clustering, full_trip_gdf):
# Get ground truth labels
ground_truth = getGroundTruth(full_trip_gdf)
# # Not symmetric and not accounting for chance
# print(f"Accuracy@1: {getAccuracyAtOne(ground_truth, clustering):.3f}")
# print(f"Precision: {getPrecision(ground_truth, clustering):.3f}")
# print(f"Recall: {getRecall(ground_truth, clustering):.3f}")
# print(f"F1: {getF1Score(ground_truth, clustering):.3f}")
print(f"Homogeneity: {metrics.homogeneity_score(ground_truth, clustering):.3f}")
print(f"Completeness: {metrics.completeness_score(ground_truth, clustering):.3f}")
# All of these metrics are symmetric and some of them are accounting for chance depending on the number of classes and clusters present in the data
print(f"V-measure: {metrics.v_measure_score(ground_truth, clustering):.3f}")
print(f"Rand index: {metrics.rand_score(ground_truth, clustering):.3f}")
print(f"ARI: {metrics.adjusted_rand_score(ground_truth, clustering):.3f}")
print(f"MI: {metrics.mutual_info_score(ground_truth, clustering):.3f}")
print(f"NMI: {metrics.normalized_mutual_info_score(ground_truth, clustering):.3f}")
print(f"AMI: {metrics.adjusted_mutual_info_score(ground_truth, clustering):.3f}")
print(f"Cluster accuracy: {cluster_acc(ground_truth, clustering):.3f}")
def store_results(clustering_concat, clustering_after_HL_assignment, clustering_after_assign_no_match, full_trip_gdf):
# Get ground truth labels
ground_truth = getGroundTruth(full_trip_gdf)
# Write all clustering metrics of evaluate() to csv and add columns for parameters
result_dicts = []
for clustering in [clustering_concat, clustering_after_HL_assignment, clustering_after_assign_no_match]:
result_dict = {}
result_dict['Homogeneity'] = metrics.homogeneity_score(ground_truth, clustering)
result_dict['Completeness'] = metrics.completeness_score(ground_truth, clustering)
result_dict['V-measure'] = metrics.v_measure_score(ground_truth, clustering)
result_dict['Rand index'] = metrics.rand_score(ground_truth, clustering)
result_dict['ARI'] = metrics.adjusted_rand_score(ground_truth, clustering)
result_dict['MI'] = metrics.mutual_info_score(ground_truth, clustering)
result_dict['NMI'] = metrics.normalized_mutual_info_score(ground_truth, clustering)
result_dict['AMI'] = metrics.adjusted_mutual_info_score(ground_truth, clustering)
result_dict['Cluster accuracy'] = cluster_acc(ground_truth, clustering)
result_dicts.append(result_dict)
df = pd.DataFrame(result_dicts)
# Add column with date and time
df['Date'] = datetime.datetime.now().strftime("%Y-%m-%d")
df['Time'] = datetime.datetime.now().strftime("%H:%M:%S")
# Create a new column for each parameter
df['LCSS_EPS'] = LCSS_EPS
df['LCSS_FLIP'] = LCSS_FLIP
df['CHAINING_INFLOW_HR_DIFF_THRESHOLD'] = CHAINING_INFLOW_HR_DIFF_THRESHOLD
df['CHAINING_HR_DIFF_THRESHOLD'] = CHAINING_HR_DIFF_THRESHOLD
df['HL_SP_START_TIME'] = HL_SP_START_TIME
df['HL_SP_END_TIME'] = HL_SP_END_TIME
df['HL_EP_START_TIME'] = HL_EP_START_TIME
df['HL_EP_END_TIME'] = HL_EP_END_TIME
df['RANDOMIZED_SIMULTANEOUS_SEARCH_ITERATIONS'] = RANDOMIZED_SIMULTANEOUS_SEARCH_ITERATIONS
df['SIM_THRESH_FOR_NO_MATCH_TRIPS'] = SIM_THRESH_FOR_NO_MATCH_TRIPS
# Check if file exists
file_exists = os.path.isfile('results.csv')
# Write to csv (append)
if not file_exists:
df.to_csv('results.csv', mode='a', header=True, index=False)
else:
df.to_csv('results.csv', mode='a', header=False, index=False)
def LCSS(traj1_linestr, traj2_linestr, eps=200, flip=True):
"""This function takes in two GeoSeries and takes the top entry linestring. It then calculates the Least Common Sub-Sequence metric for these two and returns the value.
Args:
traj1_linestr (_type_): _description_
traj2_linestr (_type_): _description_
eps (int, optional): This can be interpreted as the distance in meters between two points compared of the subsequences. Defaults to 10.
Returns:
_type_: float
"""
if isinstance(traj1_linestr, gp.GeoSeries):
s1 = traj1_linestr.iloc[0].coords
else:
s1 = traj1_linestr.coords
if isinstance(traj2_linestr, gp.GeoSeries):
s2 = traj2_linestr.iloc[0].coords
else:
s2 = traj2_linestr.coords
s1 = np.asarray(s1)
s2 = np.asarray(s2)
if flip:
return max(tslearn.metrics.lcss(s1, s2, eps=eps), tslearn.metrics.lcss(np.flip(s1, axis=0), s2, eps=eps))
else:
return tslearn.metrics.lcss(s1, s2, eps=eps)
def cdist(traj_linestrings, eps=200):
"""This function takes in a GeoSeries of linestrings and calculates the LCSS distance matrix.
Args:
traj_linestrings (_type_): _description_
eps (int, optional): _description_. Defaults to 200.
Returns:
_type_: The distance matrix of the LCSS metric
"""
assert isinstance(traj_linestrings, gp.GeoSeries), f"traj_linestrings is of type {type(traj_linestrings)}, need to be GeoSeries"
len_traj_list = len(traj_linestrings)
traj_linestrings = traj_linestrings.reset_index(drop=True)
M = np.zeros((len_traj_list, len_traj_list))
for i in tqdm(range(len_traj_list)):
traj_list_1_i = traj_linestrings[i]
for j in range(i+1,len_traj_list):
traj_list_2_j = traj_linestrings[j]
M[i, j] = LCSS(traj_list_1_i, traj_list_2_j,eps)
# Symmetrize
M = M + M.T
# Set diagonal to 1
np.fill_diagonal(M, 1)
return M
def raster_usage_count(city, GRID_RESOLUTION_JSD, input_file_path, output_file_path):
if city == 'beijing':
MIN_LNG, MIN_LAT, MAX_LNG, MAX_LAT = 116.08, 39.66, 116.69, 40.27 # Beijing center
TARGET_EPSG = 32650
elif city == 'berlin':
MIN_LNG, MIN_LAT, MAX_LNG, MAX_LAT = 12.562133, 52.099718, 14.129426, 52.803108 # Berlin center
TARGET_EPSG = 3035
robjects.r("""
library(sf)
raster_usage_count_R <- function(MIN_LNG, MIN_LAT, MAX_LNG, MAX_LAT,
resolution, TARGET_EPSG,
INPUT_FILE_PATH, OUTPUT_FILE_PATH){
# transform bounding box from 4326 to 3035
pts <- matrix(c(MIN_LNG, MIN_LAT, MIN_LNG, MAX_LAT, MAX_LNG,
MAX_LAT, MAX_LNG, MIN_LAT, MIN_LNG, MIN_LAT), ncol=2, byrow=TRUE)
polygon_ext <- st_polygon(list(pts)) |> st_sfc(crs=4326) |> st_transform(TARGET_EPSG)
extent <- st_as_sf(polygon_ext)|> terra::ext()
raster_template <- terra::rast(crs= terra::crs(paste0("epsg:", as.character(TARGET_EPSG))),
res=resolution,
extent=extent,
vals=0)
input_ls <- read_sf(INPUT_FILE_PATH)|> st_transform(TARGET_EPSG)
count_raster <- raster_template
for (i in input_ls$TRIP_ID){
line <- input_ls|> dplyr::filter(TRIP_ID == i) |> dplyr::select(geometry)
# if line only consists of at least two points
if (nrow(sf::st_cast(line, "POINT")) > 1) {
count_raster_temp <- terra::rasterize(terra::vect(line), raster_template, background=0)
count_raster <- count_raster + count_raster_temp
}
}
terra::writeRaster(count_raster, OUTPUT_FILE_PATH, overwrite=TRUE)
}
""")
raster_usage_count_R = robjects.globalenv["raster_usage_count_R"]
raster_usage_count_R(MIN_LNG, MIN_LAT, MAX_LNG, MAX_LAT, \
GRID_RESOLUTION_JSD, TARGET_EPSG, input_file_path, output_file_path)
def similarity_of_tifs(alt_path, base_path):
# Open image
im = Image.open(alt_path)
alt_counts = np.array(im)
# this is for cells whithout any value (no traj passed through there) -> set to 0
alt_counts[alt_counts<0] = 0
alt_counts = alt_counts
im = Image.open(base_path)
base_counts = np.array(im)
# this is for cells whithout any value (no traj passed through there) -> set to 0
base_counts[base_counts<0] = 0
base_counts = base_counts
jsd = jensenshannon(alt_counts.flatten(), base_counts.flatten())
return jsd
def build_raster_usage_count_tfifs(city, path_to_geojson_files='temp/'):
for geojson_file in tqdm(os.listdir(path_to_geojson_files)):
raster_usage_count(city, GRID_RESOLUTION_JSD, path_to_geojson_files+geojson_file, path_to_geojson_files+geojson_file[:-8]+'.tif')
# delete geojson file
os.remove(path_to_geojson_files+geojson_file)
def cdist_jsd(path_to_tifs='temp/'):
nr_clusters = len(os.listdir(path_to_tifs))
tfif_files = os.listdir(path_to_tifs)
M = np.zeros((nr_clusters, nr_clusters))
for i in tqdm(range(nr_clusters)):
cluster_alt = tfif_files[i]
for j in range(i+1,nr_clusters):
cluster_base = tfif_files[j]
M[i, j] = similarity_of_tifs(path_to_tifs + cluster_alt, path_to_tifs + cluster_base)
# Symmetrize
M = M + M.T
# Set diagonal to 1
np.fill_diagonal(M, 0)
return M
def match_boundary_points_with_tessellation(raw_trip_sp_gdf, raw_trip_ep_gdf, tesselation_gdf):
"""This function matches the boundary points of the raw trips with the tesselation.
Args:
raw_trip_sp_gdf (_type_): _description_
raw_trip_ep_gdf (_type_): _description_
tesselation_gdf (_type_): _description_
Returns:
_type_: This function returns two data frames, one for the start points and one for the end points. These data frames contain the tile_id of the tesselation that the point is located in.
"""
# SP
# Spatial join points to polygons
gdf_sp = gp.sjoin(
tesselation_gdf[["tile_id", "geometry"]],
raw_trip_sp_gdf,
how="inner"
).drop('index_right', axis=1)
# Spatial join points to polygons
gdf_ep = gp.sjoin(
tesselation_gdf[["tile_id", "geometry"]],
raw_trip_ep_gdf,
how="inner"
).drop('index_right', axis=1)
return gdf_sp, gdf_ep
def extract_trips_that_start_end_in_tessellation(raw_full_trip_gdf, raw_trip_sp_gdf, raw_trip_ep_gdf, gdf_sp, gdf_ep):
gdf_sp_ids = gdf_sp.TRIP_ID
gdf_ep_ids = gdf_ep.TRIP_ID
full_trip_gdf = raw_full_trip_gdf.query("TRIP_ID in @gdf_sp_ids and TRIP_ID in @gdf_ep_ids")
trip_sp_gdf = raw_trip_sp_gdf.query("TRIP_ID in @gdf_ep_ids and TRIP_ID in @gdf_sp_ids")
trip_ep_gdf = raw_trip_ep_gdf.query("TRIP_ID in @gdf_sp_ids and TRIP_ID in @gdf_ep_ids")
gdf_sp = gdf_sp.query("TRIP_ID in @gdf_ep_ids and TRIP_ID in @gdf_sp_ids")
gdf_ep = gdf_ep.query("TRIP_ID in @gdf_sp_ids and TRIP_ID in @gdf_ep_ids")
assert len(full_trip_gdf) == len(trip_sp_gdf) == len(trip_ep_gdf) == len(gdf_sp) == len(gdf_ep) == len(set(trip_sp_gdf.TRIP_ID).intersection(set(trip_ep_gdf.TRIP_ID))), "Not equal length." # this last intersection checks that for all unique trip ids we have exactly ONE SP and EP
print(f"Number of trips that start and end wihin tessellation area: {len(full_trip_gdf)}")
print(f"Number of trips outside and therefore dropped: {len(raw_full_trip_gdf) - len(full_trip_gdf)}")
return full_trip_gdf, trip_sp_gdf, trip_ep_gdf, gdf_sp, gdf_ep
def build_trip_chain_mapping(gdf_sp, gdf_ep, INFLOW_HR_DIFF_THRESHOLD=CHAINING_INFLOW_HR_DIFF_THRESHOLD, HR_DIFF_THRESHOLD=CHAINING_HR_DIFF_THRESHOLD):
"""This function returns a list of trip chains that are continued trips that happened subsequent to and from same tile within a given time threshold.
Args:
gdf_sp (_type_): _description_
gdf_ep (_type_): _description_
inflow_hr_diff_threshold (int, optional): _description_. Defaults to 4.
hr_diff_threshold (int, optional): _description_. Defaults to 8.
Returns:
_type_: _description_
"""
# Calculate mapping of continued trips that happened subsequent to and from same tile
mapping_cont_trips = []
for index, trip in tqdm(gdf_ep.sort_values('TRIP_ID').iterrows(), total=len(gdf_ep)):
te_1_id = trip.TRIP_ID
te_1_tid = trip.tile_id
te_1_dt = pd.to_datetime(trip['TRIP_END'], format='%Y-%m-%d %H:%M:%S')
ts_1_dt = pd.to_datetime(trip['TRIP_START'], format='%Y-%m-%d %H:%M:%S')
inflow = gdf_ep.query("tile_id == @te_1_tid").copy()
inflow['TRIP_END'] = inflow.TRIP_END.astype('datetime64[ns]')
inflow['TRIP_START'] = inflow.TRIP_START.astype('datetime64[ns]')
inflow['INFLOW_HR_DIFF'] = inflow.TRIP_END.apply(lambda x: (x - te_1_dt).total_seconds()/3600)
inflow = inflow.query("(INFLOW_HR_DIFF <= @HR_DIFF_THRESHOLD) and (INFLOW_HR_DIFF >= -@INFLOW_HR_DIFF_THRESHOLD)") # Take trips
inflow = inflow.query("~((TRIP_START <= @ts_1_dt and TRIP_END >= @te_1_dt) or (@ts_1_dt <= TRIP_START and @te_1_dt >= TRIP_END) or (@ts_1_dt <= TRIP_START and @te_1_dt >= TRIP_START) or (TRIP_START <= @ts_1_dt and TRIP_END >= @ts_1_dt))") # Ignore trips that have happened simultaneously
# if more than one trip has arrived in +- hour window, then do not merge this trip
if len(inflow) > 1:
continue
# Get all trips that started from same tile as t_1 has ended in
ts_2 = gdf_sp.query("tile_id == @te_1_tid").copy()
# get difference between two trips hours (seconds divided by 3600 gets hours)
ts_2['TRIP_START'] = ts_2.TRIP_START.astype('datetime64[ns]')
ts_2['TRIP_END'] = ts_2.TRIP_END.astype('datetime64[ns]')
ts_2['hr_diff'] = ts_2['TRIP_START'].apply(lambda x: (x - te_1_dt).total_seconds()/3600)
# Only consider trips that started within a certain time after the initial trip ended in the same tessellation tile
ts_2 = ts_2[(ts_2['hr_diff'].astype(str).astype(float) <= HR_DIFF_THRESHOLD) & (ts_2['hr_diff'].astype(str).astype(float) >= 0)]
# Only consider trips that are not simultaneously
ts_2 = ts_2.query("~((TRIP_START <= @ts_1_dt and TRIP_END >= @te_1_dt) or (@ts_1_dt <= TRIP_START and @te_1_dt >= TRIP_END) or (@ts_1_dt <= TRIP_START and @te_1_dt >= TRIP_START) or (TRIP_START <= @ts_1_dt and TRIP_END >= @ts_1_dt))")
# Only consider connection if exactly one trip started from same tile in time window
if len(ts_2) == 1:
mapping_cont_trips.append({
'TRIP_ID': te_1_id,
'TRIP_ID_CONT': ts_2.TRIP_ID.iloc[0]
})
return mapping_cont_trips
def evaluate_trip_chaining(mapping_cont_trips, full_trip_gdf):
"""This function evaluates the trip chaining by checking if the chained trips are from the same person.
Args:
mapping_cont_trips (_type_): Dictionary of trip ids that are chained. Output of build_trip_chain_mapping()
full_trip_gdf (_type_): The full trip gdf that contains all trips.
Returns:
_type_: None
"""
mistakes = []
for conn in mapping_cont_trips:
trip_ids = [conn['TRIP_ID'], conn['TRIP_ID_CONT']]
unique_person = full_trip_gdf.query("TRIP_ID in @trip_ids").PERSON_ID.nunique()
if unique_person > 1:
mistakes.append(full_trip_gdf.query("TRIP_ID in @trip_ids"))
print(f"Number of edges (matched) between trips: {len(mapping_cont_trips)}")
print(f"Number of wrong matches: {len(mistakes)}")
def getTripChain(trip_id, mapping_cont_trips, chain=[]):
""" Recursive function that returns a list for all chained trips for a give orig trip_id
Args:
trip_id (_type_): _description_
chain (list, optional): _description_. Defaults to [].
mapping_cont_trips (_type_): Mapping of continued trips. Output of build_trip_chain_mapping().
Returns:
_type_: _description_
"""
if type(trip_id) == str:
trip_id = int(trip_id)
# add orig trip_id to output list
if len(chain) == 0:
chain.append(trip_id)
# recursively find all chained trips originating from the orig trip_id
for edge in mapping_cont_trips:
if edge['TRIP_ID'] == trip_id:
chain.append(edge['TRIP_ID_CONT'])
getTripChain(edge['TRIP_ID_CONT'], mapping_cont_trips, chain)
return chain
def merge_trips_from_matching(gdf_sp, mapping_cont_trips, full_trip_gdf):
"""This function merges trips that are chained together from the matching done in build_trip_chain_mapping().
Args:
gdf_sp (_type_): GeoDataFrame of start points.
mapping_cont_trips (_type_): Mapping of continued trips. Output of build_trip_chain_mapping().
full_trip_gdf (_type_): GeoDataFrame of all trips.
Returns:
_type_: GeoDataFrame of merged trips.
"""
# Get trip chain for each trip (Start Point)
print("\nBuilding trip chains...")
trip_chains = [getTripChain(trip, mapping_cont_trips, chain=[]) for trip in tqdm(gdf_sp.TRIP_ID)]
print("Done.")
# Sort for longest chain first
trip_chains.sort(key = len, reverse = True)
# Create dictionary to store mappings for evaluation
trip_concat_dict = {}
covered_trips = []
merged_trips_gdf = []
print("\nMerging trips...")
for chain in tqdm(trip_chains, total=len(trip_chains)):
# Check if any of the trips in the current chain has already been merged as part of another chain
# Since we start with the longest chain and iterate through descending sorted list, we only retain the complete chains
if set(chain).intersection(set(covered_trips)):
continue
# add trip chain to dict for evaluation later
trip_concat_dict[chain[0]] = chain[1:]
# add all trip ids part of current chain to list so that every trip is only contained in longest chain of it
covered_trips += chain
trips = full_trip_gdf.query("TRIP_ID in @chain").sort_values("TRIP_START")
trips["temp"] = 1
trips = trips.groupby('temp').agg(list).reset_index(drop=True).rename(columns={'TRIP_ID': 'TRIP_ID_CHAIN'})
trips["wkt_trip"] = trips['geometry'].apply(lambda x: ", ".join([str(i) for i in x]).replace("), LINESTRING (", ", "))
trips['TRIP_START'] = trips['TRIP_START'].apply(lambda x: min(x))
trips['TRIP_END'] = trips['TRIP_END'].apply(lambda x: max(x))
trips['TRIP_LEN_IN_MTRS'] = trips['TRIP_LEN_IN_MTRS'].apply(lambda x: sum(x))
#trips['TRIP_DURATION_IN_SECS'] = trips['TRIP_DURATION_IN_SECS'].apply(lambda x: sum(x))
trips['TRIP_WD'] = trips['TRIP_WD'].apply(lambda x: x[0]) # see below
trips['TRIP_DATE'] = trips['TRIP_DATE'].apply(lambda x: x[0]) # see below
trips['TRIP_ID'] = trips['TRIP_ID_CHAIN'].apply(lambda x: x[0]) # assign trip_id of first trip in chain to concatenated trip
# This is the TRIP_ID of the last trip in the chain to be concatenated
trips['TRIP_ID_LAST'] = trips['TRIP_ID_CHAIN'].apply(lambda x: x[-1])
# Note: Here we are assigning the PERSON_ID of the first trip to the concatenated trip. This of course can be erroneous if the concatenation itself is wrong
trips['PERSON_ID'] = trips['PERSON_ID'].apply(lambda x: x[0])
trips = trips.drop(['geometry', 'TRIP_ID_CHAIN'], axis=1)
trips = gp.GeoDataFrame(trips, geometry=gp.GeoSeries.from_wkt(trips['wkt_trip'])).drop('wkt_trip', axis=1)
merged_trips_gdf.append(trips)
print("Done.")
trip_merged_gdf = pd.concat(merged_trips_gdf)
print(f"Number of trips that were matched at least once: {len(set(covered_trips))}/{len(set(gdf_sp.TRIP_ID))}")
# Concatenate all trips that were unmerged with the merged trips into a new gdf
print("Concatenating MERGED and UNMERGED trips...")
unmerged_trips = full_trip_gdf.query("TRIP_ID not in @covered_trips")
full_trips_concat_gdf = pd.concat([unmerged_trips, trip_merged_gdf])
full_trips_concat_gdf['TRIP_ID_FIRST'] = full_trips_concat_gdf['TRIP_ID'] # This is the same as TRIP_ID
print("Done.")
# Assign TRIP_ID as TRIP_ID_LAST in case TRIP has not been merged and first and last TRIP_Id are in fact the same
full_trips_concat_gdf['TRIP_ID_LAST'] = np.where(full_trips_concat_gdf.TRIP_ID_LAST.isnull(), full_trips_concat_gdf.TRIP_ID, full_trips_concat_gdf.TRIP_ID_LAST)
return full_trips_concat_gdf.reset_index(drop=True), trip_concat_dict
def extract_concatenated_trips(full_trips_concat_gdf, gdf_sp, trip_sp_gdf, gdf_ep, trip_ep_gdf):
# Filter for those trip_ids that are still the start of a trip even after the concatenation (of trip chains)
t_id_sp = full_trips_concat_gdf.TRIP_ID_FIRST
t_id_ep = full_trips_concat_gdf.TRIP_ID_LAST
# Also filter dfs that contain points
gdf_sp_concat = gdf_sp.query("TRIP_ID in @t_id_sp")
trip_sp_gdf_concat = trip_sp_gdf.query("TRIP_ID in @t_id_sp")
gdf_ep_concat = gdf_ep.query("TRIP_ID in @t_id_ep")
trip_ep_gdf_concat = trip_ep_gdf.query("TRIP_ID in @t_id_ep")
assert len(trip_sp_gdf_concat) == len(trip_ep_gdf_concat) == len(gdf_sp_concat) == len(gdf_ep_concat)
return gdf_sp_concat, trip_sp_gdf_concat, gdf_ep_concat, trip_ep_gdf_concat
def getIndexInList(trip_id, full_trip_gdf):
"""This function takes in a trip_id and returns the list index of this trip's position in the ground truth clustering.
Args:
trip_id (int): TRIP_ID
Returns:
int: The index of this TRIP_ID in the ground truth clustering vector.
"""
full_trip_gdf = full_trip_gdf.reset_index(drop=True)
index_list = full_trip_gdf.sort_values('TRIP_ID').TRIP_ID.to_list()
return index_list.index(trip_id)
def build_clustering_after_concatenation(full_trips_concat_gdf, trip_concat_dict, full_trip_gdf):
"""This function builds the clustering vector after the concatenation step.
Args:
full_trips_concat_gdf (GeoDataFrame): GeoDataFrame containing all trips after the concatenation step.
trip_concat_dict (dict): Dictionary containing the trip chains that were concatenated.
Returns:
int: The index of this TRIP_ID in the ground truth clustering vector.
"""
# This creates the array with clustering IDs after the concatenation step
clustering_concat = {}
for index, trip in full_trips_concat_gdf.reset_index(drop=True).sort_values('TRIP_ID').iterrows():
trip_order_index = getIndexInList(trip.TRIP_ID, full_trip_gdf)
clustering_concat[trip_order_index] = index
if trip.TRIP_ID in trip_concat_dict:
for t in trip_concat_dict[trip.TRIP_ID]:
clustering_concat[getIndexInList(t, full_trip_gdf)] = index
clustering_concat = list(dict(sorted(clustering_concat.items())).values())
print(f"Number of unique clusters: {len(set(clustering_concat))}")
return clustering_concat
def build_hl_from_start_points(gdf_sp, gdf_ep, HL_SP_START_TIME=HL_SP_START_TIME, HL_SP_END_TIME=HL_SP_END_TIME, HL_SP_OUTFLOW_THRESHOLD=HL_SP_OUTFLOW_THRESHOLD):
# Generate home locations (HL) from SPs
gdf_sp.index=pd.to_datetime(gdf_sp.TRIP_START)
gdf_sp['hl'] = gdf_sp['TRIP_START'].apply(lambda x: 1 if x in gdf_sp.between_time(HL_SP_START_TIME, HL_SP_END_TIME).TRIP_START else 0).astype(object)
gdf_sp.reset_index(inplace=True, drop=True)
# Extract only those cells that are HL
gdf_hl_sp = gdf_sp[gdf_sp['hl'] == 1]
# Filter those hl candidates where there are other trips leaving from within time window
for i, trip in gdf_hl_sp.iterrows():
tl_id = trip.tile_id
tr_id = trip.TRIP_ID
st = pd.to_datetime(trip['TRIP_START'], format='%Y-%m-%d %H:%M:%S')
outflow = gdf_sp.query('tile_id == @tl_id').copy()
outflow['TRIP_START'] = outflow.TRIP_START.astype('datetime64[ns]')
outflow['OUTFLOW_HR_DIFF'] = outflow.TRIP_START.apply(lambda x: (x - st).total_seconds()/3600)
if len(outflow) > 0:
outflow = outflow.query("(OUTFLOW_HR_DIFF <= @HL_SP_OUTFLOW_THRESHOLD) and (OUTFLOW_HR_DIFF >= -@HL_SP_OUTFLOW_THRESHOLD) and (TRIP_ID != @tr_id)") # Take trips
if len(outflow) > 0:
gdf_sp.loc[gdf_sp['TRIP_ID']==tr_id, 'hl'] = 0
# create spatial weights matrix
W = libpysal.weights.Queen.from_dataframe(gdf_hl_sp)
# get component labels
components = W.component_labels
gdf_hl_combined_sp = pd.merge(gp.sjoin(
gdf_hl_sp,
gdf_hl_sp.dissolve(by=components)[["geometry"]],
how="left"
), gdf_hl_sp.dissolve(by=components)[["geometry"]].reset_index(), left_on="index_right", right_on='index', suffixes=("__drop", "")).drop(['index', 'index_right', 'geometry__drop'], axis=1)
# Compute count of unique HL per Peson (HL here is already the merged tiles)
gdf_hl_combined_sp = pd.merge(gdf_hl_combined_sp, gdf_hl_combined_sp.astype({'geometry': 'string'}).groupby('PERSON_ID')[['geometry']].nunique().reset_index().rename(columns={'geometry': 'CNT_UNIQUE_HL'}), how="left")
# using dictionary to convert specific columns
convert_dict = {'PERSON_ID': object,
'CNT_UNIQUE_HL': int
}
gdf_hl_combined_sp = gdf_hl_combined_sp.astype(convert_dict)
return gdf_hl_combined_sp
def build_hl_from_end_points(gdf_sp, gdf_ep, HL_EP_START_TIME=HL_EP_START_TIME, HL_EP_END_TIME=HL_EP_END_TIME, HL_EP_OUTFLOW_THRESHOLD=HL_EP_OUTFLOW_THRESHOLD):
# Generate home locations (HL) from EPs
gdf_ep.index=pd.to_datetime(gdf_ep.TRIP_END)
gdf_ep['hl'] = gdf_ep['TRIP_END'].apply(lambda x: 1 if x in gdf_ep.between_time(HL_EP_START_TIME, HL_EP_END_TIME).TRIP_END else 0).astype(object)
gdf_ep.reset_index(inplace=True, drop=True)
# Extract only those cells that are HL
gdf_hl_ep = gdf_ep[gdf_ep['hl'] == 1]
# Filter those hl candidates where there are other trips leaving from within time window
for i, trip in gdf_hl_ep.iterrows():
tl_id = trip.tile_id
tr_id = trip.TRIP_ID
et = pd.to_datetime(trip['TRIP_END'], format='%Y-%m-%d %H:%M:%S')
outflow = gdf_sp.query('tile_id == @tl_id').copy()
outflow['TRIP_START'] = outflow.TRIP_START.astype('datetime64[ns]')
outflow['OUTFLOW_HR_DIFF'] = outflow.TRIP_START.apply(lambda x: float((x - et).total_seconds()/3600))
if len(outflow) > 0:
outflow = outflow.query("(OUTFLOW_HR_DIFF <= @HL_SP_OUTFLOW_THRESHOLD) and (OUTFLOW_HR_DIFF >= 0) and (TRIP_ID != @tr_id)") # Take trips
if len(outflow) > 0:
gdf_ep.loc[gdf_ep['TRIP_ID']==tr_id, 'hl'] = 0
### Merge hl cells that are adjacent (touching) to each other
# create spatial weights matrix
W = libpysal.weights.Queen.from_dataframe(gdf_hl_ep)
# get component labels
components = W.component_labels
gdf_hl_combined_ep = pd.merge(gp.sjoin(
gdf_hl_ep,
gdf_hl_ep.dissolve(by=components)[["geometry"]],
how="left"
), gdf_hl_ep.dissolve(by=components)[["geometry"]].reset_index(), left_on="index_right", right_on='index', suffixes=("__drop", "")).drop(['index', 'index_right', 'geometry__drop'], axis=1)
gdf_hl_combined_ep = pd.merge(gdf_hl_combined_ep, gdf_hl_combined_ep.astype({'geometry': 'string'}).groupby('PERSON_ID')[['geometry']].nunique().reset_index().rename(columns={'geometry': 'CNT_UNIQUE_HL'}), how="left")
# using dictionary to convert specific columns
convert_dict = {'PERSON_ID': object,
'CNT_UNIQUE_HL': int
}
gdf_hl_combined_ep = gdf_hl_combined_ep.astype(convert_dict)
return gdf_hl_combined_ep
def concatenate_hl(gdf_hl_combined_sp, gdf_hl_combined_ep):
gp_combined = pd.concat([gdf_hl_combined_ep, gdf_hl_combined_sp])
### Merge hl cells that are adjacent (touching) to each other
# create spatial weights matrix
W = libpysal.weights.Queen.from_dataframe(gp_combined)
# get component labels
components = W.component_labels
# We need to first join and then merge to first get the right index and then actually join the geometry
gp_combined = pd.merge(gp.sjoin(
gp_combined,
gp_combined.dissolve(by=components)[["geometry"]],
how="left"
), gp_combined.dissolve(by=components)[["geometry"]].reset_index(), left_on="index_right", right_on='index', suffixes=("__drop", "")).drop(['index', 'index_right', 'geometry__drop'], axis=1)
gp_combined = pd.merge(gp_combined.drop('CNT_UNIQUE_HL', axis=1), gp_combined.astype({'geometry': 'string'}).groupby('PERSON_ID')[['geometry']].nunique().reset_index().rename(columns={'geometry': 'CNT_UNIQUE_HL'}), how="left")
# using dictionary to convert specific columns
convert_dict = {'PERSON_ID': object,
'CNT_UNIQUE_HL': int
}
gp_combined = gp_combined.astype(convert_dict)
# Plot number of unique person IDs per HL count
gp_combined.astype({'geometry':'string'}).groupby(['geometry'])[['PERSON_ID']].nunique().value_counts().plot(kind='bar', title='Number of unique person IDs per HL count', xlabel='HL count', ylabel='Number of unique person IDs')
# Number of users for which at least one HL was identified
print('Number of users for which at least on Home Location has been identified: ', gp_combined.PERSON_ID.unique().size)
# Assign ID to HL
gp_combined['HL_ID'] = gp_combined.astype({'geometry': 'string'}).groupby('geometry').ngroup()
HL_table = gp_combined[['geometry', 'HL_ID']].drop_duplicates()
print(f"Number of unique HL tiles: {len(HL_table)}")
return gp_combined, HL_table
def match_trips_to_HL(gp_combined, HL_table, trip_sp_gdf_concat, trip_ep_gdf_concat, full_trips_concat_gdf):
# Merge all start and enpoints of all trajectories with HL tiles
# All successfully matched trips will have 0 in the "matched_sp/ep" column else NaN
matched_sp = gp.sjoin(
trip_sp_gdf_concat, # This data frame contains all SPs of the trips that are at the end of a concatenated trip (end of a concatenated trip)
gp_combined.dissolve()[['geometry']], # Here we do the matching on the dissolved HL tiles since we only want to have one match per point to detect binary whether it is matched at all or not
how="left"
).rename(columns={"index_right": "matched_sp"})
matched_ep = gp.sjoin(
trip_ep_gdf_concat, # This data frame contains all EPs of the trips that are at the end of a concatenated trip (end of a concatenated trip)
gp_combined.dissolve()[['geometry']], # same here, see above
how="left"
).rename(columns={"index_right": "matched_ep"})
# Merge start and endpoints of all trajectories with HL tiles to get HL_IDs for each trip
# Note: Since we here match with the dissolved tile, we also can at max get ONE match per SP since overlapping HL tiles are dissolved.
s = gp.sjoin(
trip_sp_gdf_concat,
HL_table,
how="right").drop('index_left', axis=1).dropna()
e = gp.sjoin(
trip_ep_gdf_concat,
HL_table,
how="right").drop('index_left', axis=1).dropna()
# Get unmatched start and endpoints
unmatched_sp_t_ids = matched_sp[matched_sp.matched_sp.isnull()].TRIP_ID.to_list()
unmatched_ep_t_ids = matched_ep[matched_ep.matched_ep.isnull()].TRIP_ID.to_list()
# Number of unmatched trajectories that do not start or end in an HL tile
nr_unmatched = len(full_trips_concat_gdf.query('TRIP_ID_FIRST in @unmatched_sp_t_ids and TRIP_ID_LAST in @unmatched_ep_t_ids'))
print(f"Number of unmatched trajectories (concatenated) that do neither start nor end in a HL tile: {nr_unmatched}/{len(full_trips_concat_gdf)}")
# Get TRIP_IDs of matched start and endpoints
matched_sp_t_ids = matched_sp[~matched_sp.matched_sp.isnull()].TRIP_ID.to_list()
matched_ep_t_ids = matched_ep[~matched_ep.matched_ep.isnull()].TRIP_ID.to_list()
print(f"Number of trajectories (concatenated) that start AND end in a HL tile: {len(full_trips_concat_gdf.query('TRIP_ID_FIRST in @matched_sp_t_ids and TRIP_ID_LAST in @matched_ep_t_ids'))}/{len(full_trips_concat_gdf)}")
# check whether number of unmatched trajectories plus number of matched trajectories do line up with the total number of trips in data (in this case concatenated trips)
assert (full_trips_concat_gdf.query("TRIP_ID_FIRST in @s.TRIP_ID or TRIP_ID_LAST in @e.TRIP_ID").TRIP_ID.nunique() + nr_unmatched) == len(full_trips_concat_gdf)
# Merge matched SP and EP to get the HL_IDs for each trip and drop duplicates.
HL_table_se_concat = pd.merge(full_trips_concat_gdf, s[['TRIP_ID', 'HL_ID']], left_on="TRIP_ID_FIRST", right_on="TRIP_ID", how="left")
HL_table_se_concat = pd.merge(HL_table_se_concat, e[['TRIP_ID', 'HL_ID']], left_on="TRIP_ID_LAST", right_on="TRIP_ID", how="left").drop(['TRIP_ID_y', 'TRIP_ID'], axis=1).rename(columns={'TRIP_ID_x': 'TRIP_ID'})
HL_table_se_concat = HL_table_se_concat[['TRIP_ID', 'HL_ID_x', 'HL_ID_y']].set_index('TRIP_ID').stack().droplevel(1).reset_index().rename(columns={0: 'HL_ID'}).drop_duplicates()
# Get trips that match different HL tiles with their SP and EP
double_assigned_trips = HL_table_se_concat.groupby('TRIP_ID').filter(lambda x: len(x) > 1)
# Get trips that are not assigned to any HL tile
unmatched_trips = full_trips_concat_gdf.query('TRIP_ID_FIRST in @unmatched_sp_t_ids and TRIP_ID_LAST in @unmatched_ep_t_ids')[['TRIP_ID']].reset_index(drop=True)
unmatched_trips['HL_ID'] = None # Get same format as HL_table_se_concat
print(f"Number of trips that match different HL tiles with their SP and EP: {double_assigned_trips.TRIP_ID.nunique()}")
return HL_table_se_concat, unmatched_trips, double_assigned_trips, nr_unmatched
def find_best_hl_id(hl_id_score_dict, verbose=False):
"""Finds the best hl_id for a trip based on the scores. Loops through the list of scores in descending order and finds the first unique highest score. If there is no unique highest score, the function finds the largest cluster of hl_ids that have the same score as the highest score.
Args:
hl_id_score_dict (_type_): Dictionary of scores for each hl_id. Scores are stored in a list.
Returns:
_type_: The best hl_id for the trip.
"""
# sort lists in hl_id_score_dict descending
hl_id_score_dict = {hl_id: sorted(hl_id_score_dict[hl_id], reverse=True) for hl_id in hl_id_score_dict}
best_hl_id = None
max_length_hl_id_list = len(hl_id_score_dict[max(hl_id_score_dict, key=lambda x: len(hl_id_score_dict[x]))])
max_score_in_hl_id_score_dict = max([max(hl_id_score_dict[hl_id], default=0.0) for hl_id in hl_id_score_dict]) # use default to avoid error if list is empty
if max_score_in_hl_id_score_dict == 0.0:
print("All scores are 0.0, assigning -1 as best_hl_id!")
return -1
# for i in max length of all hl_id_score_dict lists
for i in range(max_length_hl_id_list):
scores = [hl_id_score_dict[hl_id][i] for hl_id in hl_id_score_dict if len(hl_id_score_dict[hl_id]) > i]
# if there is no duplicated score
if len(scores) == len(set(scores)):
# Get the best_hl_id of the highest score
best_hl_id = [best_hl_id for best_hl_id in hl_id_score_dict if len(hl_id_score_dict[best_hl_id]) > i and hl_id_score_dict[best_hl_id][i] == max(scores)][0]
if verbose:
print("There is a unique highest score")
print("The highest score is {} and the best_hl_id is {}".format(max(scores), best_hl_id))
break
# if no unique best hl_id has been found
if best_hl_id is None:
# Find largest cluster for hl_ids that have the same score as the max_score_hl_id
best_hl_ids = [hl_id for hl_id in hl_id_score_dict if max_score_in_hl_id_score_dict in hl_id_score_dict[hl_id]]
# Assign the trip to the hl_id with the largest cluster
best_hl_id = max(best_hl_ids, key=lambda x: len(hl_id_score_dict[x]))
if verbose:
print("There is no unique highest score")
print("The hl_ids with the same highest score are", best_hl_ids)
print("The best hl_id is", best_hl_id)
return best_hl_id
def assign_double_matched_trips_to_unique_hl(HL_table_se_concat, full_trips_concat_gdf, unmatched_trips, double_assigned_trips, nr_unmatched):
# Get trips that match only one HL tile with their SP and EP
uniquely_assigned_trips = HL_table_se_concat.groupby('TRIP_ID').filter(lambda x: len(x) == 1)
def compute_lcss_scores(double_assigned_trip):
t_id = double_assigned_trip.TRIP_ID
hl_id = double_assigned_trip.HL_ID
# Create dict to store results for this trip and HL
result_dict = {t_id: {hl_id: list()}}
# Get trajectory linestring for this trip
trip = full_trips_concat_gdf.query("TRIP_ID == @t_id")
# Get trips that are uniquely assigned to this HL
assigned_trips = uniquely_assigned_trips.query("HL_ID == @hl_id")
# Loop through these trips and calc LCSS scores for each of them
for index, assigned_trip in assigned_trips.iterrows():
assigned_t_id = assigned_trip.TRIP_ID
# Skip the calc for the trip with itself
if assigned_t_id == t_id:
continue
# Get trajectory linestring for this trip (here we use the non-concated one since we are considering S and E points separately to match HL with the concated trips afterwards)
a_trip = full_trips_concat_gdf.query("TRIP_ID == @assigned_t_id")
score = LCSS(trip.geometry, a_trip.geometry)
# save scores in list
result_dict[t_id][hl_id].append(score)
return result_dict
parallel_scores = Parallel(n_jobs=-4, verbose=1)(delayed(compute_lcss_scores)(double_assigned_trip) for index, double_assigned_trip in double_assigned_trips.iterrows())
# Flatten the list of dicts from parallel processing
lcss_scores = {}
for idx, trip in enumerate(parallel_scores):
for t_id in trip:
# create dict for this trip if not yet existing (it would if another HL this trip was joined with has already been checked)
if t_id not in lcss_scores:
lcss_scores[t_id] = {}
for hl_id in parallel_scores[idx][t_id]:
# create new list for this HL under the trip key
lcss_scores[t_id][hl_id] = parallel_scores[idx][t_id][hl_id]
# Get and compare max scores across all matched HL for a trip and assign the HL with the max value of any trip
best_hl_ids_dict = {}
for trip in lcss_scores:
best_hl_ids_dict[trip] = find_best_hl_id(lcss_scores[trip], verbose=False)
# Assign resolved scores to se_HL_lookup table and drop duplicates
HL_table_se_concat['HL_ID'] = HL_table_se_concat.apply(lambda x: best_hl_ids_dict[x['TRIP_ID']] if x['TRIP_ID'] in best_hl_ids_dict else x['HL_ID'], axis=1)
HL_table_se_concat = HL_table_se_concat.drop_duplicates(['TRIP_ID', 'HL_ID']).reset_index(drop=True)
# Assert that the number of trips that are matched to a HL tile plus the the nr of trips that are unmatched is equal to the total number of concatenated trips
assert len(HL_table_se_concat) + nr_unmatched == len(full_trips_concat_gdf)
# Combine resolved HL HL_table_se_concat with the unmatched trips to get full HL_table for all trips
HL_table_trips_concat = pd.concat([HL_table_se_concat, unmatched_trips], ignore_index=True)
HL_table_trips_concat.loc[HL_table_trips_concat.HL_ID.isnull(), 'HL_ID'] = -1 # assign HL_ID -1 to unmatched trips
return HL_table_trips_concat
def getTripOverlaps(gdf):
"""This function calculates whether two trips overlap in time.
Args:
gdf (_type_): The GeoDataFrame containing the trips.
Returns:
_type_: Dictionary with trip IDs as keys and a list of trip IDs that overlap with the key trip as values.
"""
def getOverlaps(trip_x):
overlap_dict = {}
overlaps = []