-
Notifications
You must be signed in to change notification settings - Fork 27
/
google_decimeter.py
540 lines (439 loc) · 19.4 KB
/
google_decimeter.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
"""Functions to process Android measurements.
"""
__authors__ = "Ashwin Kanhere, Derek Knowles, Shubh Gupta, Adam Dai"
__date__ = "02 Nov 2021"
import os
import warnings
import numpy as np
import pandas as pd
from gnss_lib_py.navdata.navdata import NavData
from gnss_lib_py.navdata.operations import loop_time, concat, find_wildcard_indexes, interpolate
from gnss_lib_py.utils.coordinates import wrap_0_to_2pi
from gnss_lib_py.utils.coordinates import geodetic_to_ecef
from gnss_lib_py.utils.coordinates import ecef_to_geodetic
from gnss_lib_py.utils.time_conversions import unix_to_gps_millis
from gnss_lib_py.utils.time_conversions import gps_to_unix_millis
from gnss_lib_py.utils.constants import CONSTELLATION_ANDROID, QZSS_PRN_SVN
class AndroidDerived2021(NavData):
"""Class handling derived measurements from Android dataset.
Inherits from NavData().
Parameters
----------
input_path : string or path-like
Path to measurement csv file
remove_timing_outliers : bool
Flag for whether to remove measures that are too close or
too far away in time. Code from the competition hosts used
to implement changes. See note.
Notes
-----
Removes duplicate rows using correction 5 from competition hosts
implemented from https://www.kaggle.com/code/gymf123/tips-notes-from-the-competition-hosts/notebook
retrieved on 10 August, 2022
"""
def __init__(self, input_path, remove_timing_outliers=True):
pd_df = pd.read_csv(input_path)
# Correction 1: Mapping _derived timestamps to previous timestamp
# for correspondance with ground truth and Raw data
derived_timestamps = pd_df['millisSinceGpsEpoch'].unique()
mapper = dict(zip(derived_timestamps[1:],derived_timestamps[:-1]))
pd_df = pd_df[pd_df['millisSinceGpsEpoch'] != derived_timestamps[0]]
pd_df.replace({"millisSinceGpsEpoch" : mapper},inplace=True)
# Correction 5 implemented verbatim from competition tips
if remove_timing_outliers:
delta_millis = pd_df['millisSinceGpsEpoch'] - pd_df['receivedSvTimeInGpsNanos'] / 1e6
where_good_signals = (delta_millis > 0) & (delta_millis < 300)
pd_df = pd_df[where_good_signals].copy()
if np.all(~where_good_signals):
warnings.warn("All data removed due to timing outliers,"
+ " try setting remove_timing_outliers to"
+ " False", RuntimeWarning)
super().__init__(pandas_df=pd_df)
def postprocess(self):
"""Android derived specific postprocessing.
Adds corrected pseudoranges to measurements. Time step
corrections implemented from dataset webpage [1]_ retrieved on
10 August, 2022.
Correlates constellation type numbers with corresponding
constellation names. Mapping also comes from competition
website [1]_.
References
----------
.. [1] https://www.kaggle.com/c/google-smartphone-decimeter-challenge/data
"""
pr_corrected = self['raw_pr_m'] \
+ self['b_sv_m'] \
- self['intersignal_bias_m'] \
- self['tropo_delay_m'] \
- self['iono_delay_m']
self['corr_pr_m'] = pr_corrected
# rename gnss_id column to constellation type
self.replace(CONSTELLATION_ANDROID, rows="gnss_id", inplace=True)
# rename signal_type column to conform to standard convention
signal_map = {"GPS_L1" : "l1",
"GPS_L5" : "l5",
"GAL_E1" : "e1",
"GAL_E5A" : "e5a",
"GLO_G1" : "g1",
"QZS_J1" : "j1",
"QZS_J5" : "j5",
"BDS_B1I" : "b1i",
"BDS_B1C" : "b1c",
"BDS_B2A" : "b2a",
}
self.replace(signal_map, rows="signal_type", inplace=True)
@staticmethod
def _row_map():
"""Map of row names from loaded to gnss_lib_py standard
Returns
-------
row_map : Dict
Dictionary of the form {old_name : new_name}
"""
row_map = {'collectionName' : 'trace_name',
'phoneName' : 'rx_name',
'millisSinceGpsEpoch' : 'gps_millis',
'constellationType' : 'gnss_id',
'svid' : 'sv_id',
'signalType' : 'signal_type',
'xSatPosM' : 'x_sv_m',
'ySatPosM' : 'y_sv_m',
'zSatPosM' : 'z_sv_m',
'xSatVelMps' : 'vx_sv_mps',
'ySatVelMps' : 'vy_sv_mps',
'zSatVelMps' : 'vz_sv_mps',
'satClkBiasM' : 'b_sv_m',
'satClkDriftMps' : 'b_dot_sv_mps',
'rawPrM' : 'raw_pr_m',
'rawPrUncM' : 'raw_pr_sigma_m',
'isrbM' : 'intersignal_bias_m',
'ionoDelayM' : 'iono_delay_m',
'tropoDelayM' : 'tropo_delay_m',
}
return row_map
class AndroidDerived2022(NavData):
"""Class handling derived measurements from Android dataset.
Inherits from NavData().
The row nomenclature for the new derived dataset has changed.
We reflect this changed nomenclature in the _row_map() method.
Parameters
----------
input_path : string or path-like
Path to measurement csv file
"""
def __init__(self, input_path, **kwargs):
super().__init__(csv_path=input_path, **kwargs)
def postprocess(self):
"""Android derived specific postprocessing.
Notes
-----
Adds corrected pseudoranges to measurements. Time step corrections
implemented from https://www.kaggle.com/c/google-smartphone-decimeter-challenge/data
retrieved on 10 August, 2022.
"""
pr_corrected = self['raw_pr_m'] \
+ self['b_sv_m'] \
- self['intersignal_bias_m'] \
- self['tropo_delay_m'] \
- self['iono_delay_m']
self['corr_pr_m'] = pr_corrected
# rename gnss_id column to constellation type
self.replace(CONSTELLATION_ANDROID, rows="gnss_id", inplace=True)
# rename signal_type column to conform to standard convention
signal_map = {"GPS_L1" : "l1",
"GPS_L5" : "l5",
"GAL_E1" : "e1",
"GAL_E5A" : "e5a",
"GLO_G1" : "g1",
"QZS_J1" : "j1",
"QZS_J5" : "j5",
"BDS_B1I" : "b1i",
"BDS_B1C" : "b1c",
"BDS_B2A" : "b2a",
}
self.replace(signal_map, rows="signal_type", inplace=True)
# add gps milliseconds
self["gps_millis"] = unix_to_gps_millis(self["unix_millis"])
# update svn for QZSS constellation
if "qzss" in np.unique(self["gnss_id"]):
qzss_idxs = self.argwhere("gnss_id","qzss")
self["sv_id",qzss_idxs] = [QZSS_PRN_SVN[i] \
for i in self.where("gnss_id","qzss")["sv_id"]]
def get_state_estimate(self):
"""Extract relevant rows in a separate NavData for state estimate.
Returns
-------
state_estimate : gnss_lib_py.navdata.navdata.NavData
Instance of `NavData` containing state estimate rows present
in the instance of `AndroidDerived2022`.
"""
rx_rows_to_find = ['x_rx*_m', 'y_rx*_m', 'z_rx*_m',
'vx_rx*_mps', 'vy_rx*_mps', 'vz_rx*_mps',
'b_rx*_m', 'b_dot_rx*_mps']
rx_rows_in_measure = ['gps_millis']
for row_wildcard in rx_rows_to_find:
try:
row_map = find_wildcard_indexes(self,row_wildcard, max_allow=1)
row = row_map[row_wildcard][0]
rx_rows_in_measure.append(row)
except KeyError:
warnings.warn(f"Row wildcard: {row_wildcard} not found", RuntimeWarning)
continue
state_estimate = NavData()
for _, _, measure_frame in loop_time(self,'gps_millis', delta_t_decimals=-2):
temp_est = NavData()
for row_wildcard in rx_rows_in_measure:
temp_est[row_wildcard] = measure_frame[row_wildcard, 0]
if len(state_estimate)==0:
state_estimate = temp_est
else:
state_estimate = concat(state_estimate,temp_est)
return state_estimate
@staticmethod
def _row_map():
"""Map of row names from loaded to gnss_lib_py standard
Returns
-------
row_map : Dict
Dictionary of the form {old_name : new_name}
"""
row_map = {'utcTimeMillis' : 'unix_millis',
'ConstellationType' : 'gnss_id',
'Svid' : 'sv_id',
'SignalType' : 'signal_type',
'SvPositionXEcefMeters' : 'x_sv_m',
'SvPositionYEcefMeters' : 'y_sv_m',
'SvPositionZEcefMeters' : 'z_sv_m',
'SvElevationDegrees' : 'el_sv_deg',
'SvAzimuthDegrees' : 'az_sv_deg',
'SvVelocityXEcefMetersPerSecond' : 'vx_sv_mps',
'SvVelocityYEcefMetersPerSecond' : 'vy_sv_mps',
'SvVelocityZEcefMetersPerSecond' : 'vz_sv_mps',
'SvClockBiasMeters' : 'b_sv_m',
'SvClockDriftMetersPerSecond' : 'b_dot_sv_mps',
'RawPseudorangeMeters' : 'raw_pr_m',
'RawPseudorangeUncertaintyMeters' : 'raw_pr_sigma_m',
'IsrbMeters' : 'intersignal_bias_m',
'IonosphericDelayMeters' : 'iono_delay_m',
'TroposphericDelayMeters' : 'tropo_delay_m',
'Cn0DbHz': 'cn0_dbhz',
'AccumulatedDeltaRangeMeters' : 'accumulated_delta_range_m',
'AccumulatedDeltaRangeUncertaintyMeters': 'accumulated_delta_range_sigma_m',
'WlsPositionXEcefMeters' : 'x_rx_m',
'WlsPositionYEcefMeters' : 'y_rx_m',
'WlsPositionZEcefMeters' : 'z_rx_m',
}
return row_map
class AndroidGroundTruth2021(NavData):
"""Class handling ground truth from Android dataset.
Inherits from NavData().
Parameters
----------
input_path : string or path-like
Path to measurement csv file
"""
def __init__(self, input_path):
super().__init__(csv_path=input_path)
self.postprocess()
def postprocess(self):
"""Android derived specific postprocessing for NavData()
Notes
-----
Corrections incorporated from Kaggle notes hosted here:
https://www.kaggle.com/code/gymf123/tips-notes-from-the-competition-hosts
"""
# Correcting reported altitude
self['alt_rx_gt_m'] = self['alt_rx_gt_m'] - 61.
gt_lla = np.transpose(np.vstack([self['lat_rx_gt_deg'],
self['lon_rx_gt_deg'],
self['alt_rx_gt_m']]))
gt_ecef = geodetic_to_ecef(gt_lla)
self["x_rx_gt_m"] = gt_ecef[:,0]
self["y_rx_gt_m"] = gt_ecef[:,1]
self["z_rx_gt_m"] = gt_ecef[:,2]
# convert bearing degrees to heading in radians
self["heading_rx_gt_rad"] = np.deg2rad(self["heading_rx_gt_rad"])
self["heading_rx_gt_rad"] = wrap_0_to_2pi(self["heading_rx_gt_rad"])
@staticmethod
def _row_map():
"""Map of row names from loaded ground truth to gnss_lib_py standard
Returns
-------
row_map : Dict
Dictionary of the form {old_name : new_name}
"""
row_map = {'latDeg' : 'lat_rx_gt_deg',
'lngDeg' : 'lon_rx_gt_deg',
'heightAboveWgs84EllipsoidM' : 'alt_rx_gt_m',
'millisSinceGpsEpoch' : 'gps_millis',
'speedMps' : 'v_rx_gt_mps',
'courseDegree' : 'heading_rx_gt_rad',
}
return row_map
class AndroidGroundTruth2022(AndroidGroundTruth2021):
"""Class handling ground truth from Android dataset.
Inherits from AndroidGroundTruth2021().
"""
def postprocess(self):
"""Android derived specific postprocessing for NavData()
Notes
-----
"""
if np.any(np.isnan(self['alt_rx_gt_m'])):
warnings.warn("Some altitude values were missing, using 0m ", RuntimeWarning)
self['alt_rx_gt_m'] = np.nan_to_num(self['alt_rx_gt_m'])
gt_lla = np.transpose(np.vstack([self['lat_rx_gt_deg'],
self['lon_rx_gt_deg'],
self['alt_rx_gt_m']]))
gt_ecef = geodetic_to_ecef(gt_lla)
self["x_rx_gt_m"] = gt_ecef[:,0]
self["y_rx_gt_m"] = gt_ecef[:,1]
self["z_rx_gt_m"] = gt_ecef[:,2]
# add gps milliseconds
self["gps_millis"] = unix_to_gps_millis(self['unix_millis'])
# convert bearing degrees to heading in radians
self["heading_rx_gt_rad"] = np.deg2rad(self["heading_rx_gt_rad"])
self["heading_rx_gt_rad"] = wrap_0_to_2pi(self["heading_rx_gt_rad"])
@staticmethod
def _row_map():
"""Map row names from loaded data to gnss_lib_py standard
Returns
-------
row_map : Dict
Dictionary of the form {old_name : new_name}
"""
row_map = {'LatitudeDegrees' : 'lat_rx_gt_deg',
'LongitudeDegrees' : 'lon_rx_gt_deg',
'AltitudeMeters' : 'alt_rx_gt_m',
'SpeedMps' : 'v_rx_gt_mps',
'BearingDegrees' : 'heading_rx_gt_rad',
'UnixTimeMillis' : 'unix_millis',
}
return row_map
class AndroidDerived2023(AndroidDerived2022):
"""Class handling derived measurements from 2023 Android dataset.
Processes the Google Smartphone Decimeter Challenge 2023 [2]_.
Inherits from AndroidDerived2022().
Parameters
----------
input_path : string or path-like
Path to measurement csv file
References
----------
.. [2] https://www.kaggle.com/competitions/smartphone-decimeter-2023/overview
"""
def __init__(self, input_path):
super().__init__(input_path=input_path,
dtype={'AccumulatedDeltaRangeUncertaintyMeters':np.float64})
class AndroidGroundTruth2023(AndroidGroundTruth2022):
"""Class handling ground truth from Android dataset.
Inherits from AndroidGroundTruth2022().
"""
def solve_kaggle_baseline(navdata):
"""Convert Decimeter challenge baseline into state_estimate.
The baseline solution was provided in 2022 and 2023, but not in 2021.
Parameters
----------
navdata : gnss_lib_py.parsers.google_decimeter.AndroidDerived2022
Instance of the AndroidDerived2022 class.
Returns
-------
state_estimate : gnss_lib_py.navdata.navdata.NavData
Baseline state estimate.
"""
columns = ["unix_millis",
"x_rx_m",
"y_rx_m",
"z_rx_m",
]
navdata.in_rows(columns)
data_df = (navdata.pandas_df().drop_duplicates(subset='unix_millis')[columns]
.reset_index(drop=True))
lat,lon,alt = np.transpose(ecef_to_geodetic(data_df[["x_rx_m",
"y_rx_m",
"z_rx_m",
]].to_numpy()))
state_estimate = NavData()
state_estimate["gps_millis"] = unix_to_gps_millis(
data_df["unix_millis"].to_numpy())
state_estimate["lat_rx_deg"] = lat
state_estimate["lon_rx_deg"] = lon
state_estimate["alt_rx_m"] = alt
return state_estimate
def prepare_kaggle_submission(state_estimate, trip_id="trace/phone"):
"""Converts from gnss_lib_py receiver state to Kaggle submission.
Parameters
----------
state_estimate : gnss_lib_py.navdata.navdata.NavData
Estimated receiver position in latitude and longitude as an
instance of the NavData class with the following
rows: ``gps_millis``, ``lat_rx*_deg``, ``lon_rx*_deg``.
trip_id : string
Value for the tripId column in kaggle submission which is a
fusion of the data and phone type.
Returns
-------
output : gnss_lib_py.navdata.navdata.NavData
NavData structure ready for Kaggle submission.
"""
state_estimate.in_rows("gps_millis")
wildcards = find_wildcard_indexes(state_estimate,["lat_rx*_deg",
"lon_rx*_deg"],max_allow = 1)
output = NavData()
output["tripId"] = np.array([trip_id] * state_estimate.shape[1])
output["UnixTimeMillis"] = gps_to_unix_millis(state_estimate["gps_millis"])
output.orig_dtypes["UnixTimeMillis"] = np.int64
output["LatitudeDegrees"] = state_estimate[wildcards["lat_rx*_deg"]]
output["LongitudeDegrees"] = state_estimate[wildcards["lon_rx*_deg"]]
interpolate(output,"UnixTimeMillis",["LatitudeDegrees",
"LongitudeDegrees"],inplace=True)
return output
def solve_kaggle_dataset(folder_path, solver, verbose=False, *args, **kwargs):
"""Run solver on all kaggle traces.
Additional ``*args`` arguments are passed into the ``solver``
function.
Parameters
----------
folder_path: string or path-like
Path to folder containing all traces (e.g. full path to "train"
or "test" directories.
solver : function
State estimate solver that takes an instance of
AndroidDerived2022 and outputs a state_estimate NavData object.
Additional ``*args`` arguments are passed into this ``solver``
function.
verbose : bool
If verbose, will print each trace trajectory name and phone name
pair when it is solving the state estimate for that pair.
Returns
-------
solution : gnss_lib_py.navdata.navdata.NavData
Full solution submission across all traces. Can then be saved
using submission.to_csv().
"""
# create solution NavData object
solution = NavData()
# iterate through all trace options
for trace_name in sorted(os.listdir(folder_path)):
trace_path = os.path.join(folder_path, trace_name)
if not os.path.isdir(trace_path): # pragma: no cover
continue
# iterate through all phone types
for phone_type in sorted(os.listdir(trace_path)):
data_path = os.path.join(folder_path,trace_name,
phone_type,"device_gnss.csv")
try:
# convert data to Measurement class
derived_data = AndroidDerived2022(data_path)
if verbose:
print("solving:",trace_name,phone_type)
# compute state estimate using provided solver function
state_estimate = solver(derived_data, *args, **kwargs)
trip_id = "/".join([trace_name,phone_type])
output = prepare_kaggle_submission(state_estimate,
trip_id)
# concatenate solution to previous solutions
solution = concat(solution, output)
except FileNotFoundError:
continue
return solution