Skip to content

Commit 1e038a0

Browse files
Merge pull request #115 from Stanford-NavLab/derek/pathlib
derek/pathlib
2 parents 99ede9a + 91d4572 commit 1e038a0

11 files changed

Lines changed: 119 additions & 30 deletions

File tree

gnss_lib_py/parsers/android.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def __init__(self, input_path, remove_timing_outliers=True):
3030
3131
Parameters
3232
----------
33-
input_path : string
33+
input_path : string or path-like
3434
Path to measurement csv file
3535
remove_timing_outliers : bool
3636
Flag for whether to remove measures that are too close or
@@ -157,7 +157,7 @@ def __init__(self, input_path):
157157
158158
Parameters
159159
----------
160-
input_path : string
160+
input_path : string or path-like
161161
Path to measurement csv file
162162
"""
163163
super().__init__(csv_path=input_path)
@@ -255,7 +255,7 @@ def __init__(self, input_path):
255255
256256
Parameters
257257
----------
258-
input_path : string
258+
input_path : string or path-like
259259
Path to measurement csv file
260260
"""
261261

@@ -369,7 +369,7 @@ def preprocess(self, input_path):
369369
370370
Parameters
371371
----------
372-
input_path : string
372+
input_path : string or path-like
373373
File location of data file to read.
374374
375375
Returns
@@ -380,6 +380,12 @@ def preprocess(self, input_path):
380380
Dataframe that contains the gyro measurements from the log.
381381
382382
"""
383+
384+
if not isinstance(input_path, (str, os.PathLike)):
385+
raise TypeError("input_path must be string or path-like")
386+
if not os.path.exists(input_path):
387+
raise FileNotFoundError("file not found")
388+
383389
with open(input_path, encoding="utf8") as csvfile:
384390
reader = csv.reader(csvfile)
385391
for row in reader:
@@ -435,7 +441,7 @@ def preprocess(self, input_path):
435441
436442
Parameters
437443
----------
438-
input_path : string
444+
input_path : string or path-like
439445
File location of data file to read.
440446
441447
Returns
@@ -444,6 +450,12 @@ def preprocess(self, input_path):
444450
Dataframe that contains the location fixes from the log.
445451
446452
"""
453+
454+
if not isinstance(input_path, (str, os.PathLike)):
455+
raise TypeError("input_path must be string or path-like")
456+
if not os.path.exists(input_path):
457+
raise FileNotFoundError("file not found")
458+
447459
with open(input_path, encoding="utf8") as csvfile:
448460
reader = csv.reader(csvfile)
449461
for row in reader:
@@ -463,7 +475,7 @@ def make_csv(input_path, output_directory, field, show_path=False):
463475
464476
Parameters
465477
----------
466-
input_path : string
478+
input_path : string or path-like
467479
File location of data file to read.
468480
output_directory : string
469481
Directory where new csv file should be created
@@ -488,6 +500,12 @@ def make_csv(input_path, output_directory, field, show_path=False):
488500
output_path = os.path.join(output_directory, field + ".csv")
489501
with open(output_path, 'w', encoding="utf8") as out_csv:
490502
writer = csv.writer(out_csv)
503+
504+
if not isinstance(input_path, (str, os.PathLike)):
505+
raise TypeError("input_path must be string or path-like")
506+
if not os.path.exists(input_path):
507+
raise FileNotFoundError("file not found")
508+
491509
with open(input_path, 'r', encoding="utf8") as in_txt:
492510
for line in in_txt:
493511
# Comments in the log file

gnss_lib_py/parsers/navdata.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class NavData():
2424
2525
Parameters
2626
----------
27-
csv_path : string
27+
csv_path : string or path-like
2828
Path to csv file containing data
2929
pandas_df : pd.DataFrame
3030
Data used to initialize NavData instance.
@@ -93,7 +93,7 @@ def from_csv_path(self, csv_path, **kwargs):
9393
9494
Parameters
9595
----------
96-
csv_path : string
96+
csv_path : string or path-like
9797
Path to csv file containing data
9898
header : string, int, or None
9999
"infer" uses the first row as column names, setting to
@@ -102,8 +102,8 @@ def from_csv_path(self, csv_path, **kwargs):
102102
Delimiter to use when reading in csv file.
103103
104104
"""
105-
if not isinstance(csv_path, str):
106-
raise TypeError("csv_path must be string")
105+
if not isinstance(csv_path, (str, os.PathLike)):
106+
raise TypeError("csv_path must be string or path-like")
107107
if not os.path.exists(csv_path):
108108
raise FileNotFoundError("file not found")
109109

gnss_lib_py/parsers/nmea.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
__authors__ = "Ashwin Kanhere, Dalton Vega"
66
__date__ = "24 Jun, 2023"
77

8+
import os
89
import datetime
910

1011
import pynmea2
@@ -31,7 +32,7 @@ def __init__(self, filename, msg_types=None,
3132
3233
Parameters
3334
----------
34-
filename : str
35+
filename : str or path-like
3536
filepath to NMEA file to read.
3637
msg_types : list
3738
List of strings describing messages that can be parsed.
@@ -63,6 +64,12 @@ def __init__(self, filename, msg_types=None,
6364
pd_df = pd.DataFrame()
6465
field_dict = {}
6566
prev_timestamp = None
67+
68+
if not isinstance(filename, (str, os.PathLike)):
69+
raise TypeError("filename must be string or path-like")
70+
if not os.path.exists(filename):
71+
raise FileNotFoundError("file not found")
72+
6673
with open(filename, "r", encoding='UTF-8') as open_file:
6774
for line in open_file:
6875
check_ind = line.find('*')

gnss_lib_py/parsers/precise_ephemerides.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def parse_sp3(input_path, constellation = 'gps'):
6666
6767
Parameters
6868
----------
69-
input_path : string
69+
input_path : string or path-like
7070
Path to sp3 file
7171
constellation : string
7272
Key from among {gps, galileo, glonass, beidou, qzss, etc} that
@@ -99,8 +99,8 @@ def parse_sp3(input_path, constellation = 'gps'):
9999
Accessed as of August 20, 2022
100100
"""
101101
# Initial checks for loading sp3_path
102-
if not isinstance(input_path, str):
103-
raise TypeError("input_path must be string")
102+
if not isinstance(input_path, (str, os.PathLike)):
103+
raise TypeError("input_path must be string or path-like")
104104
if not os.path.exists(input_path):
105105
raise FileNotFoundError("file not found")
106106

@@ -191,7 +191,7 @@ def parse_clockfile(input_path, constellation = 'gps'):
191191
192192
Parameters
193193
----------
194-
input_path : string
194+
input_path : string or path-like
195195
Path to clk file
196196
constellation : string
197197
Key from among {gps, galileo, glonass, beidou, qzss, etc} that
@@ -225,8 +225,8 @@ def parse_clockfile(input_path, constellation = 'gps'):
225225
"""
226226

227227
# Initial checks for loading sp3_path
228-
if not isinstance(input_path, str):
229-
raise TypeError("input_path must be string")
228+
if not isinstance(input_path, (str, os.PathLike)):
229+
raise TypeError("input_path must be string or path-like")
230230
if not os.path.exists(input_path):
231231
raise FileNotFoundError("file not found")
232232

gnss_lib_py/parsers/smartloc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self, input_path):
3636
3737
Parameters
3838
----------
39-
input_path : string
39+
input_path : string or path-like
4040
Path to measurement csv file
4141
4242
"""

notebooks/tutorials/parsers.ipynb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
]
1010
},
1111
{
12-
"attachments": {},
1312
"cell_type": "markdown",
1413
"id": "d5632d85",
1514
"metadata": {},
@@ -317,7 +316,6 @@
317316
]
318317
},
319318
{
320-
"attachments": {},
321319
"cell_type": "markdown",
322320
"id": "b0a917b6",
323321
"metadata": {},
@@ -326,7 +324,6 @@
326324
]
327325
},
328326
{
329-
"attachments": {},
330327
"cell_type": "markdown",
331328
"id": "f166df97",
332329
"metadata": {},
@@ -336,7 +333,6 @@
336333
]
337334
},
338335
{
339-
"attachments": {},
340336
"cell_type": "markdown",
341337
"id": "aa0ea320",
342338
"metadata": {},
@@ -356,14 +352,13 @@
356352
"outputs": [],
357353
"source": [
358354
"# download NMEA data and load it into NavData instance\n",
359-
"!wget https://raw.githubusercontent.com/Stanford-NavLab/gnss_lib_py/dalton/nmea/data/unit_test/nmea/nmea_w_correct_checksum.nmea --quiet -O \"nmea_w_correct_checksum.nmea\"\n",
355+
"!wget https://raw.githubusercontent.com/Stanford-NavLab/gnss_lib_py/main/data/unit_test/nmea/nmea_w_correct_checksum.nmea --quiet -O \"nmea_w_correct_checksum.nmea\"\n",
360356
"# Load the NMEA file into a NavData structure\n",
361357
"nmea_navdata = glp.Nmea('nmea_w_correct_checksum.nmea')\n",
362358
"print('Loaded NMEA data\\n', nmea_navdata)"
363359
]
364360
},
365361
{
366-
"attachments": {},
367362
"cell_type": "markdown",
368363
"id": "6606e32a",
369364
"metadata": {},
@@ -378,14 +373,13 @@
378373
"metadata": {},
379374
"outputs": [],
380375
"source": [
381-
"!wget https://raw.githubusercontent.com/Stanford-NavLab/gnss_lib_py/dalton/nmea/data/unit_test/nmea/nmea_no_checksum.nmea --quiet -O \"nmea_w_no_checksum.nmea\"\n",
376+
"!wget https://raw.githubusercontent.com/Stanford-NavLab/gnss_lib_py/main/data/unit_test/nmea/nmea_no_checksum.nmea --quiet -O \"nmea_w_no_checksum.nmea\"\n",
382377
"# Load the NMEA file into a NavData structure\n",
383378
"nmea_navdata = glp.Nmea('nmea_w_no_checksum.nmea', check=False)\n",
384379
"print('Loaded NMEA data\\n', nmea_navdata)"
385380
]
386381
},
387382
{
388-
"attachments": {},
389383
"cell_type": "markdown",
390384
"id": "a87c37c2",
391385
"metadata": {},
@@ -884,7 +878,7 @@
884878
],
885879
"metadata": {
886880
"kernelspec": {
887-
"display_name": "Python 3",
881+
"display_name": "Python 3 (ipykernel)",
888882
"language": "python",
889883
"name": "python3"
890884
},

tests/parsers/test_android.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
__date__ = "10 Nov 2021"
77

88
import os
9+
import pathlib
910

1011
import pytest
1112
import numpy as np
@@ -341,9 +342,22 @@ def test_imu_raw(android_raw_path):
341342
test_imu = android.AndroidRawImu(android_raw_path)
342343
isinstance(test_imu, NavData)
343344

345+
test_imu = android.AndroidRawImu(pathlib.Path(android_raw_path))
346+
isinstance(test_imu, NavData)
347+
348+
# raises exception if not a file path
349+
with pytest.raises(FileNotFoundError):
350+
android.AndroidRawImu("not_a_file.txt")
351+
with pytest.raises(FileNotFoundError):
352+
android.AndroidRawImu(pathlib.Path("not_a_file.txt"))
353+
354+
# raises exception if input not string or path-like
355+
with pytest.raises(TypeError):
356+
android.AndroidRawImu([])
357+
344358

345359
def test_fix_raw(android_raw_path):
346-
"""Test that AndroidRawImu initialization
360+
"""Test that AndroidRawFixes initialization
347361
348362
Parameters
349363
----------
@@ -353,6 +367,19 @@ def test_fix_raw(android_raw_path):
353367
test_fix = android.AndroidRawFixes(android_raw_path)
354368
isinstance(test_fix, NavData)
355369

370+
test_fix = android.AndroidRawFixes(pathlib.Path(android_raw_path))
371+
isinstance(test_fix, NavData)
372+
373+
# raises exception if not a file path
374+
with pytest.raises(FileNotFoundError):
375+
android.AndroidRawFixes("not_a_file.txt")
376+
with pytest.raises(FileNotFoundError):
377+
android.AndroidRawFixes(pathlib.Path("not_a_file.txt"))
378+
379+
# raises exception if input not string or path-like
380+
with pytest.raises(TypeError):
381+
android.AndroidRawFixes([])
382+
356383

357384
def test_navdata_type(derived):
358385
"""Test that all subclasses inherit from NavData
@@ -449,8 +476,18 @@ def test_csv_equivalence(android_raw_path, root_path, file_type):
449476
df_slice = test_df[col_name].values
450477
np.testing.assert_almost_equal(measure_slice, df_slice)
451478
os.remove(csv_loc)
479+
for file in os.listdir(output_directory):
480+
os.remove(os.path.join(output_directory, file))
452481
os.rmdir(output_directory)
453482

483+
# raises exception if not a file path
484+
with pytest.raises(FileNotFoundError):
485+
android.make_csv("", output_directory, file_type)
486+
487+
# raises exception if input not string or path-like
488+
with pytest.raises(TypeError):
489+
android.make_csv([], output_directory, file_type)
490+
454491
@pytest.fixture(name="android_gtruth_path")
455492
def fixture_gtruth_path(root_path):
456493
"""Filepath of Android Ground Truth data

tests/parsers/test_ephemeris.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,15 @@ def test_request_igs(ephem_download_path, fileinfo):
287287

288288
requests_url = fileinfo['url'] + fileinfo['filepath']
289289

290-
response = requests.get(requests_url, timeout=5)
290+
291+
fail_count = 0
292+
while fail_count < 3:
293+
try:
294+
response = requests.get(requests_url, timeout=5)
295+
break
296+
except ConnectionError:
297+
fail_count += 1
298+
291299
with open(dest_filepath,'wb') as file:
292300
file.write(response.content)
293301

tests/parsers/test_navdata.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88

99
import os
10+
import pathlib
1011
import itertools
1112

1213
import pytest
@@ -211,7 +212,11 @@ def test_init_csv(csv_path):
211212

212213
# should work when csv is passed
213214
data = NavData(csv_path=csv_path)
215+
# data should contain full csv
216+
assert data.shape == (4,6)
214217

218+
# should work when csv is passed as pathlib object
219+
data = NavData(csv_path=pathlib.Path(csv_path))
215220
# data should contain full csv
216221
assert data.shape == (4,6)
217222

0 commit comments

Comments
 (0)