-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfrequent-start-end.pl
executable file
·444 lines (381 loc) · 15.3 KB
/
frequent-start-end.pl
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
#!/usr/bin/env perl
# alt: !/usr/bin/perl
# by Torben Menke https://entorb.net
# DESCRIPTION
# TODO
# IDEAS
# Use an algorithm from here: https://towardsdatascience.com/the-5-clustering-algorithms-data-scientists-need-to-know-a36d136ef68
# Zahl der Locations lässt sich verringern, wenn diese gerundet werden, aber Häufigkeit
# as we only have 2 digits for lat and lon, I should add caching
# DONE
# Modules: My Default Set
use strict;
use warnings;
use 5.010; # say
use Data::Dumper;
use utf8; # this script is written in UTF-8
binmode STDOUT, ':utf8'; # default encoding for linux print STDOUT
# # Modules: Perl Standard
# use Encode qw(encode decode);
# use open ":encoding(UFT-8)"; # for all files
# my $encodingSTDOUT = 'CP850'; # Windows/DOS: 'CP850'; Linux: UTF-8
# use Time::HiRes qw(time); # time in ms
# Modules: Web
use CGI;
my $cgi = CGI->new;
#use CGI ":all";
#use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
# Modules: File Access
use File::Basename; # for basename, dirname, fileparse
use File::Path qw(make_path remove_tree);
use Storable; # read/write hash from file system
# Modules: My Strava Module Lib
use lib ('.');
use lib ('/var/www/virtual/entorb/perl5/lib/perl5');
use lib "C:\\Users\\menketrb\\Documents\\Hacken\\Perl\\Strava-Web"
; # just for making Visual Studio Code happy
use lib "d:\\files\\Hacken\\Perl\\Strava-Web";
use TMsStrava qw( %o %s)
; # at entorb.net some modules require use local::lib!!!
my $runLocal = 0; # TODO: am I running local or on entorb.net
if ( $runLocal == 0 ) {
TMsStrava::htmlPrintHeader( $cgi, 'Frequent start/end locations' );
TMsStrava::initSessionVariables( $cgi->param("session") );
TMsStrava::htmlPrintNavigation();
}
else {
my $session = '-e1lFk8GfN4IuXse-wYFHQ';
$o{'tmpDataFolderBase'} = './temp-data';
TMsStrava::initSessionVariables($session);
TMsStrava::htmlPrintNavigation();
} ## end else [ if ( $runLocal == 0 ) ]
my @ListeDerPunkte;
my @ListeDerPunkteStart;
my @ListeDerPunkteEnd;
my $maxDistance = 0.75; # km
my $maxLocationsToCheckForFrequentOnes = 1000; # TODO: Test
my $debugPrintTiming = 0; # TODO
# my @matrix = (
# ['q','w', undef() ,'r','t'],
# ['q','w','e','r','t'],
# ['q','w','e','r','t'],
# ['a','s','d','f','g'],
# ['q','w','e','r','t']
# );
# print Dumper @matrix;
# @matrix = removeRowColFromMatrix(2,@matrix);
# print Dumper @matrix and die;
# my ($x1,$y1,$x2,$y2);
# $x1 = 46.110000;
# $y1 = 8.846000;
# $x2 = $x1 + 0.0001;
# $y2 = $y1 + 0.0001;
# # Runden auf 4 Kommastellen entspricht in Erlangen 13.5m
# # $x2 = 0 + sprintf("%.4f",$x1);
# # $y2 = 0 + sprintf("%.4f",$y1);
# say dist($x1,$y1,$x2,$y2);
# die;
my $tsStart = time;
my @allActivityHashes = ();
# for Local use
# my @L = <../Strava-Web/temp-data/KwdWm6bdssPjUXIDpPvbOw/activityList/per_page-200*.json>;
# my @allActivityHashes =
# reverse TMsStrava::convertJsonFilesToArrayOfHashes(@L); #
# if not already done, fetchActivityList, 200 per page into dir activityList
unless ( -f $s{'pathToActivityListHashDump'} ) {
die("E: activity cache missing");
}
# TMsStrava::logIt( "downloading data files" );
# TMsStrava::fetchActivityList( $s{ 'token' }, 200, 0 ); # max 200 per page/json file, max X days past
# # TMsStrava::fetchActivityList( $s{'token'}, 200, 365 ); # max 200 per page/json file, max X days past
# # fetch all of the filenames
# TMsStrava::logIt( "convertJsonFilesToArrayOfHashes and write .dmp to filesystem" );
# my @L = <$s{'tmpDataFolder'}/activityList/per_page-200*.json>;
# @allActivityHashes = reverse TMsStrava::convertJsonFilesToArrayOfHashes( @L ); # reverse here, since I now use parameter "after" in fetchActivityList, which leads to ASC sorting, which is not what I want for the display
# # write hash to file-system
# store \@allActivityHashes, $s{ 'pathToActivityListHashDump' };
# } else {
TMsStrava::logIt("reading activity data from .dmp file");
my $ref = retrieve( $s{'pathToActivityListHashDump'} );
# retrieve data from file (as ref)
@allActivityHashes = @{$ref}; # convert arrayref to array
# } ## end else
# remove the locations that are already known
my @knownLocations = TMsStrava::getKnownLocationsOfUser();
# @knownLocations = ();
foreach my $activityRef (@allActivityHashes) {
my %h = %{$activityRef};
next unless defined $h{'start_latlng'};
my @a = @{ $h{'start_latlng'} };
my $iKnowYou = 0;
foreach my $kl (@knownLocations) {
my @kl = @{$kl};
my $istNachbar = pruefeNachbar( $a[0], $a[1], $kl[0], $kl[1] );
if ( defined $istNachbar ) {
$iKnowYou = 1;
last;
}
} ## end foreach my $kl (@knownLocations)
if ( $iKnowYou == 0 ) {
push @ListeDerPunkteStart, [ $a[0], $a[1] ];
}
next unless defined $h{'end_latlng'};
@a = @{ $h{'end_latlng'} };
$iKnowYou = 0;
foreach my $kl (@knownLocations) {
my @kl = @{$kl};
my $istNachbar = pruefeNachbar( $a[0], $a[1], $kl[0], $kl[1] );
if ( defined $istNachbar ) {
$iKnowYou = 1;
last;
}
} ## end foreach my $kl (@knownLocations)
if ( $iKnowYou == 0 ) {
push @ListeDerPunkteEnd, [ $a[0], $a[1] ];
}
last
if ( $#ListeDerPunkteStart + 1 + $#ListeDerPunkteEnd + 1
>= $maxLocationsToCheckForFrequentOnes );
} ## end foreach my $activityRef (@allActivityHashes)
# hier werden Start und End Punkte zusammen in eine Liste aller Punkte gefügt.
@ListeDerPunkte = @ListeDerPunkteStart;
push @ListeDerPunkte, @ListeDerPunkteEnd;
@ListeDerPunkteStart = undef;
@ListeDerPunkteEnd = undef;
# say '<h1>List of frequently used start and end locations</h1>';
say "<p>"
. ( $#ListeDerPunkte + 1 )
. " not known start/end locations to check for cluster</p>";
say sprintf( "%.1fs after reading allActivityHashes", ( time - $tsStart ) )
if $debugPrintTiming;
# # Idee: Zum Beschleunigen Koordinaten runden und Punkte zusammenfassen
# my %h;
# my @ListeDerPunkteGerundet = ();
# my @ListeDerHaeufigkeiten = ();
# # for (my $i=$#ListeDerPunkte;$i>=0;$i--) {
# for (my $i=0;$i<=$#ListeDerPunkte;$i++) {
# $_ = sprintf("%.4f %.4f", $ListeDerPunkte[$i][0], $ListeDerPunkte[$i][1]);
# $h{$_} ++; # Koordinaten in String Form als Schlüssel des %h
# }
# # by values, reverse
# foreach my $k ( sort { $h{$b} <=> $h{$a} } keys(%h)) {
# # last if $h{$k}==1;
# @_ = split ' ', $k;
# push @ListeDerPunkteGerundet, [$_[0],$_[1]];
# push @ListeDerHaeufigkeiten, $h{$k};
# }
# say "Es bleiben ".($#ListeDerPunkteGerundet+1)." gerundete Locations";
# alt: Lesen aus Datei
# my $fileIn = "start-unknown.txt";
# open(my $fhIn, '<:encoding(UTF-8)', $fileIn)
# or die "ERROR: Can't read from file '$fileIn': $!";
# while (my $line = <$fhIn>) {
# $line =~ m/^\[([\d\.]+), ([\d\.]+)\]\n?/ or die "no match";
# my ($lat,$lng) = ($1,$2);
# push @ListeDerPunkte, [$lat,$lng];
# }
# close $fhIn;
# # say ($ListeDerPunkte[2][0] . " " . $ListeDerPunkte[2][1]);
# # say $#ListeDerPunkte;
# # say (dist(0,0,1,1));
# my @MatrixDerDistanzen;
# $MatrixDerDistanzen[$#ListeDerPunkte][$#ListeDerPunkte] = undef()
my @MatrixDerNachbarn;
$MatrixDerNachbarn[$#ListeDerPunkte][$#ListeDerPunkte] = undef()
; # ensure that matrix has the same size as @ListeDerPunkte (at least for the last row)
my @AnzDerNachbarn = (0) x ( 1 + $#ListeDerPunkte );
# Distanz zwischen allen Punkten berechnen und für jeden Punkt die Anz der Nachbarn zählen
for ( my $i = 0; $i <= $#ListeDerPunkte; $i++ ) {
for ( my $j = $i + 1; $j <= $#ListeDerPunkte; $j++ ) {
my $istNachbar = pruefeNachbar(
$ListeDerPunkte[$i][0], $ListeDerPunkte[$i][1],
$ListeDerPunkte[$j][0], $ListeDerPunkte[$j][1]
);
if ( defined $istNachbar ) {
$MatrixDerNachbarn[$i][$j] = $istNachbar;
$MatrixDerNachbarn[$j][$i] = $istNachbar;
$AnzDerNachbarn[$i]++;
$AnzDerNachbarn[$j]++;
} ## end if ( defined $istNachbar)
# my $dist = dist(
# $ListeDerPunkte[$i][0], $ListeDerPunkte[$i][1],
# $ListeDerPunkte[$j][0], $ListeDerPunkte[$j][1]
# );
# if ( defined $dist and $dist < 5 ) { # 5km hier als erster Filter
# $MatrixDerDistanzen[$i][$j] = $dist;
# $MatrixDerDistanzen[$j][$i] = $dist;
# }
} ## end for ( my $j = $i + 1; $j...)
} ## end for ( my $i = 0; $i <= ...)
say sprintf( "%.1fs after calc distances", ( time - $tsStart ) )
if $debugPrintTiming;
# anzDerNachbarnErmitteln(); # initial bereits oben
removeLocationsWithoutNeighbors();
say sprintf( "%.1fs after init removeLocationsWithoutNeighbors",
( time - $tsStart ) )
if $debugPrintTiming;
say "" . ( $#ListeDerPunkte + 1 ) . " Locations" if $debugPrintTiming;
# TODO: wie weiter?
# Einfachster Ansatz: Einen der Punkte mit den meisten Nachbarn nehmen, und aus dem und seinen Nachbarn einen Cluster definieren
# Dann den Cluster-Mittelwert bestimmen
# Was wenn mehrere andere genauso viele Nachbarn haben? -> vermutlich egal
say '<p>Clusters of unknown locations:</p>
<code>';
my $anzClustersFound = 0;
use List::Util qw( min max ); # or use List::MoreUtils qw( minmax );
while ( max(@AnzDerNachbarn) >= 5 ) {
searchForCluster();
$anzClustersFound++;
# Werte in @AnzDerNachbarn sind nun ggf nicht mehr korrekt, daher neu befüllen
anzDerNachbarnErmitteln();
removeLocationsWithoutNeighbors();
} ## end while ( max(@AnzDerNachbarn...))
if ( $anzClustersFound == 0 ) {
say 'none<br>';
}
say "</code>";
say sprintf( "Duration Total= %.1f", ( time - $tsStart ) )
if $debugPrintTiming;
if ( $anzClustersFound > 0 ) {
say
'<p>You might like to copy them to your <a href="knownLocations.pl?session='
. $s{'session'}
. '">list of known locations</a> or check them at Google Maps or <a href="https://www.openstreetmap.org">OpenStreetmap</a></p>';
} ## end if ( $anzClustersFound...)
TMsStrava::htmlPrintFooter($cgi);
# say "Übrig bleiben: ";
# say Dumper \@ListeDerPunkte;
# welcher Punkte hat wie viele dichte Nachbarn
# for (my $i=0;$i<=$#ListeDerPunkte;$i++) {
# my $anzNachbarn = 0;
# for (my $j=0;$j<=$#ListeDerPunkte;$j++) {
# next if not defined $MatrixDerDistanzen[$i][$j];
# $anzNachbarn ++ if ($MatrixDerDistanzen[$i][$j] < $maxDistance)
# }
# say "$i $anzNachbarn";
# }
sub anzDerNachbarnErmitteln {
# @AnzDerNachbarn aus @MatrixDerDistanzen ermitteln
# initial wird mit 0 befuellt
# es wird bei $dist < $maxDistance gezählt
@AnzDerNachbarn = (0) x ( 1 + $#ListeDerPunkte ); # fill by 0
for ( my $i = 0; $i <= $#ListeDerPunkte; $i++ ) {
for ( my $j = $i + 1; $j <= $#ListeDerPunkte; $j++ ) {
# my $dist = $MatrixDerDistanzen[$i][$j];
my $istNachbar = $MatrixDerNachbarn[$i][$j];
if ( defined $istNachbar ) {
$AnzDerNachbarn[$i]++;
$AnzDerNachbarn[$j]++;
}
} ## end for ( my $j = $i + 1; $j...)
} ## end for ( my $i = 0; $i <= ...)
} ## end sub anzDerNachbarnErmitteln
sub removeLocationsWithoutNeighbors {
# Locations mit weniger als 2 Nachbarn aus der Liste raus, um Rechenzeit zu sparen.
# Working on @ListeDerPunkte, @AnzDerNachbarn, @MatrixDerDistanzen
# Bringt nur bei der Suche nach vielen Clustern einen Performance Gewinn, da dieses Löschen recht langsam ist.
for ( my $i = $#ListeDerPunkte; $i >= 0; $i-- ) {
if ( $AnzDerNachbarn[$i] <= 1 ) { # 0 oder 1
splice @ListeDerPunkte, $i, 1; # remove it
splice @AnzDerNachbarn, $i, 1; # remove it
# @MatrixDerDistanzen = removeRowColFromMatrix( $i, @MatrixDerDistanzen );
@MatrixDerNachbarn = removeRowColFromMatrix( $i, @MatrixDerNachbarn );
} ## end if ( $AnzDerNachbarn[$i...])
} ## end for ( my $i = $#ListeDerPunkte...)
# say sprintf ("%.1fs after removing locations without neighbors", (time-$tsStart));
} ## end sub removeLocationsWithoutNeighbors
sub searchForCluster {
# Suche die Location mit den meisten Nachbarn
my $meisteNachbarnAnz = 0;
my $meisteNachbarnIndex = 0;
for ( my $i = 0; $i <= $#ListeDerPunkte; $i++ ) {
if ( $AnzDerNachbarn[$i] > $meisteNachbarnAnz ) {
$meisteNachbarnAnz = $AnzDerNachbarn[$i];
$meisteNachbarnIndex = $i;
}
} ## end for ( my $i = 0; $i <= ...)
# say "Das Cluster um index $meisteNachbarnIndex beinhaltet $meisteNachbarnAnz Nachbarn.";
# say sprintf( "%.1fs after searching for cluster members", ( time - $tsStart ) );
# welche sind es?
my @ImCluster = ();
for ( my $j = 0; $j <= $#ListeDerPunkte; $j++ ) {
# my $dist = $MatrixDerDistanzen[$meisteNachbarnIndex][$j];
my $istNachbar = $MatrixDerNachbarn[$meisteNachbarnIndex][$j];
if ( defined $istNachbar ) {
push @ImCluster, $j;
}
} ## end for ( my $j = 0; $j <= ...)
# say "Es sind diese: @ImCluster";
# foreach my $i (@ImCluster) {
# say "$i $ListeDerPunkte[$i][0] $ListeDerPunkte[$i][1]";
# }
# Schwerpunkt bestimmen. Dazu vektoriell Summe bilden und durch Anz teilen.
my ( $clusterSchwerpunktX, $clusterSchwerpunktY ) = ( 0, 0 );
foreach my $i (@ImCluster) {
$clusterSchwerpunktX += $ListeDerPunkte[$i][0];
$clusterSchwerpunktY += $ListeDerPunkte[$i][1];
}
$clusterSchwerpunktX /= ( 1 + $#ImCluster );
$clusterSchwerpunktY /= ( 1 + $#ImCluster );
$clusterSchwerpunktX = sprintf '%.6f', $clusterSchwerpunktX;
$clusterSchwerpunktY = sprintf '%.6f', $clusterSchwerpunktY;
say sprintf(
"<a href=\"https://maps.google.com/?q=$clusterSchwerpunktX,$clusterSchwerpunktY\" target=\"_blank\">%02d: $clusterSchwerpunktX $clusterSchwerpunktY %dx</a><br>",
( 1 + $anzClustersFound ),
( 1 + $#ImCluster )
);
# entfernen dieser aus den Orignal Listen
foreach my $i ( reverse @ImCluster ) {
splice @ListeDerPunkte, $i, 1; # remove it
splice @AnzDerNachbarn, $i, 1; # remove it
# @MatrixDerDistanzen = removeRowColFromMatrix( $i, @MatrixDerDistanzen );
@MatrixDerNachbarn = removeRowColFromMatrix( $i, @MatrixDerNachbarn );
} ## end foreach my $i ( reverse @ImCluster)
say sprintf( "%.01fs", ( time - $tsStart ) ) if $debugPrintTiming;
} ## end sub searchForCluster
sub pruefeNachbar {
my ( $x1, $y1, $x2, $y2 ) = @_;
return undef()
if ( abs( $x1 - $x2 ) > 0.1 or abs( $y1 - $y2 ) > 0.1 )
; # an angle of 0.1 is > 10km
my $geodist = TMsStrava::geoDistance( $x1, $y1, $x2, $y2 );
if ( $geodist < $maxDistance ) {
return 1;
}
else {
return undef();
}
} ## end sub pruefeNachbar
sub dist {
my ( $x1, $y1, $x2, $y2 ) = @_;
# die detail-kakulation kann man sich sparen wenn die Koordinaten offensichtlich weit auseinander liegen
return undef()
if ( abs( $x1 - $x2 ) > 0.1 or abs( $y1 - $y2 ) > 0.1 )
; # an angle of 0.1 is > 10km
# my $dx = $x2-$x1;
# my $dy = $y2-$y1;
# my $sqrtdist = sqrt($dx*$dx + $dy*$dy);
my $geodist = TMsStrava::geoDistance( $x1, $y1, $x2, $y2 );
# say "sqrtdist=$sqrtdist geodist=$geodist";
# return (sqrt($dx*$dx + $dy*$dy));
return $geodist;
} ## end sub dist
# TODO: remove multiple columns from matrix: first the rows (fast) than the columns (slow)
sub removeRowColFromMatrix {
# remove row $i and col $i form symmetric matrix
# in: $i, @matrix
# out: @matrix (with 1 row and 1 column less
my ( $i, @matrix ) = @_;
# remove row $i
splice @matrix, $i, 1;
# remove col $i
foreach my $row (@matrix) {
next unless defined $row;
my @L = @{$row};
next if ( $#L < $i );
splice @L, $i, 1;
$row = \@L;
} ## end foreach my $row (@matrix)
return @matrix;
} ## end sub removeRowColFromMatrix