Skip to content

Commit a8f2def

Browse files
Merge pull request #63 from Stanford-NavLab/derek/init-simplify
Derek/init simplify
2 parents c3a01b7 + 025f423 commit a8f2def

6 files changed

Lines changed: 60 additions & 52 deletions

File tree

gnss_lib_py/parsers/android.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@ def __init__(self, input_path):
3333
Loaded measurements with consistent column names
3434
"""
3535

36-
pd_df = pd.read_csv(input_path)
37-
col_map = self._column_map()
38-
pd_df.rename(columns=col_map, inplace=True)
39-
40-
super().__init__(pandas_df=pd_df)
41-
self.postprocess()
36+
super().__init__(csv_path=input_path)
4237

4338
def postprocess(self):
4439
"""Android derived specific postprocessing
@@ -57,15 +52,15 @@ def postprocess(self):
5752
self['corr_pr_m'] = pr_corrected
5853

5954
@staticmethod
60-
def _column_map():
55+
def _row_map():
6156
"""Map of column names from loaded to gnss_lib_py standard
6257
6358
Returns
6459
-------
65-
col_map : Dict
60+
row_map : Dict
6661
Dictionary of the form {old_name : new_name}
6762
"""
68-
col_map = {'collectionName' : 'trace_name',
63+
row_map = {'collectionName' : 'trace_name',
6964
'phoneName' : 'rx_name',
7065
'constellationType' : 'gnss_id',
7166
'svid' : 'sv_id',
@@ -84,7 +79,7 @@ def _column_map():
8479
'ionoDelayM' : 'iono_delay_m',
8580
'tropoDelayM' : 'tropo_delay_m',
8681
}
87-
return col_map
82+
return row_map
8883

8984

9085
class AndroidRawImu(NavData):
@@ -137,19 +132,18 @@ def preprocess(self, input_path):
137132
#NOTE: Assuming pandas index corresponds to measurements order
138133
#NOTE: Override times of gyro measurements with corresponding
139134
# accel times
140-
measurements.rename(columns=self._column_map(), inplace=True)
141135
return measurements
142136

143137
@staticmethod
144-
def _column_map():
145-
col_map = {'AccelXMps2' : 'acc_x_mps2',
138+
def _row_map():
139+
row_map = {'AccelXMps2' : 'acc_x_mps2',
146140
'AccelYMps2' : 'acc_y_mps2',
147141
'AccelZMps2' : 'acc_z_mps2',
148142
'GyroXRadPerSec' : 'ang_vel_x_radps',
149143
'GyroYRadPerSec' : 'ang_vel_y_radps',
150144
'GyroZRadPerSec' : 'ang_vel_z_radps',
151145
}
152-
return col_map
146+
return row_map
153147

154148

155149
class AndroidRawFixes(NavData):

gnss_lib_py/parsers/navdata.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ def __init__(self, csv_path=None, pandas_df=None, numpy_array=None,
5151
else:
5252
self.build_navdata()
5353

54+
self.rename(self._row_map())
55+
5456
self.postprocess()
5557

5658
def postprocess(self):
@@ -125,6 +127,23 @@ def from_numpy_array(self, numpy_array):
125127
for row_num in range(numpy_array.shape[0]):
126128
self.__setitem__(str(row_num), numpy_array[row_num,:])
127129

130+
@staticmethod
131+
def _row_map():
132+
"""Map of column names from loaded to gnss_lib_py standard
133+
134+
Initializes as an emptry dictionary, must be reimplemented for
135+
custom parsers.
136+
137+
Returns
138+
-------
139+
row_map : Dict
140+
Dictionary of the form {old_name : new_name}
141+
"""
142+
143+
row_map = {}
144+
145+
return row_map
146+
128147
def pandas_df(self):
129148
"""Return pandas DataFrame equivalent to class
130149
@@ -296,6 +315,7 @@ def __setitem__(self, key_idx, newvalue):
296315
#Creating an entire new row
297316
if isinstance(newvalue, np.ndarray) and newvalue.dtype==object:
298317
# Adding string values
318+
# print("\n",key_idx,"\n",newvalue)
299319
self.fillna(newvalue)
300320
new_str_vals = len(np.unique(newvalue))*np.ones(np.shape(newvalue),
301321
dtype=self.arr_dtype)
@@ -308,6 +328,7 @@ def __setitem__(self, key_idx, newvalue):
308328
self.array = np.vstack((self.array, np.reshape(new_str_vals, [1, -1])))
309329
self.map[key_idx] = self.shape[0]-1
310330
else:
331+
# print("\n",key_idx,"\n")#,newvalue)
311332
if not isinstance(newvalue, int) and not isinstance(newvalue, float):
312333
assert not isinstance(np.asarray(newvalue)[0], str), \
313334
"Cannot set a row with list of strings, please use np.ndarray with dtype=object"
@@ -665,7 +686,7 @@ def rename(self, mapper):
665686
for key, value in mapper.items():
666687
if not isinstance(value, str):
667688
raise TypeError("Column names must be strings")
668-
if key not in self.map.keys():
689+
if key not in self.map:
669690
raise KeyError("'" + str(key) \
670691
+ "' key doesn't exist in NavData class")
671692

notebooks/tutorials/parsers.ipynb

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@
193193
"source": [
194194
"One thing that we need to do to make use of the common functionality of `gnss_lib_py` is to standardize the names of our variables. See the [Standard Naming Conventions](https://gnss-lib-py.readthedocs.io/en/latest/reference/reference.html#standard-naming-conventions) section in the Reference tab of the documentation for the list of standardized names.\n",
195195
"\n",
196-
"In our case, we will convert `mySatId` to `sv_id` and `myPseudorange` to `raw_pr_m`. We make these conversions by creating the `_col_map` function and calling the `rename` function in our postprocessing step."
196+
"In our case, we will convert `mySatId` to `sv_id` and `myPseudorange` to `raw_pr_m`. We make these conversions by simply updating the `_row_map` function."
197197
]
198198
},
199199
{
@@ -203,23 +203,20 @@
203203
"source": [
204204
"```python\n",
205205
"\n",
206-
"# rename rows\n",
207-
"col_map = self._column_map()\n",
208-
"self.rename(col_map)\n",
209206
"\n",
210207
"@staticmethod\n",
211-
"def _column_map():\n",
208+
"def _row_map():\n",
212209
" \"\"\"Map of column names from loaded to gnss_lib_py standard\n",
213210
"\n",
214211
" Returns\n",
215212
" -------\n",
216-
" col_map : Dict\n",
213+
" row_map : Dict\n",
217214
" Dictionary of the form {old_name : new_name}\n",
218215
" \"\"\"\n",
219-
" col_map = {'mySatId' : 'sv_id',\n",
216+
" row_map = {'mySatId' : 'sv_id',\n",
220217
" 'myPseudorange' : 'raw_pr_m',\n",
221218
" }\n",
222-
" return col_map\n",
219+
" return row_map\n",
223220
"```"
224221
]
225222
},
@@ -284,11 +281,7 @@
284281
" \"\"\"MyReceiver specific postprocessing\n",
285282
"\n",
286283
" \"\"\"\n",
287-
" \n",
288-
" # rename rows\n",
289-
" col_map = self._column_map()\n",
290-
" self.rename(col_map)\n",
291-
" \n",
284+
"\n",
292285
" # correct pseudorange\n",
293286
" self['corr_pr_m'] = self['raw_pr_m'] + 100.\n",
294287
" \n",
@@ -297,18 +290,18 @@
297290
" \n",
298291
"\n",
299292
" @staticmethod\n",
300-
" def _column_map():\n",
293+
" def _row_map():\n",
301294
" \"\"\"Map of column names from loaded to gnss_lib_py standard\n",
302295
"\n",
303296
" Returns\n",
304297
" -------\n",
305-
" col_map : Dict\n",
298+
" row_map : Dict\n",
306299
" Dictionary of the form {old_name : new_name}\n",
307300
" \"\"\"\n",
308-
" col_map = {'mySatId' : 'sv_id',\n",
301+
" row_map = {'mySatId' : 'sv_id',\n",
309302
" 'myPseudorange' : 'raw_pr_m',\n",
310303
" }\n",
311-
" return col_map"
304+
" return row_map"
312305
]
313306
},
314307
{

tests/parsers/test_android.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,16 @@ def fixture_pd_df(derived_path):
107107
return derived_df
108108

109109

110-
@pytest.fixture(name="derived_col_map")
111-
def fixture_inverse_col_map():
110+
@pytest.fixture(name="derived_row_map")
111+
def fixture_inverse_row_map():
112112
"""Map from standard names to derived column names
113113
114114
Returns
115115
-------
116-
inverse_col_map : Dict
116+
inverse_row_map : Dict
117117
Column names for inverse map of form {standard_name : derived_name}
118118
"""
119-
inverse_col_map = {'trace_name' : 'collectionName',
119+
inverse_row_map = {'trace_name' : 'collectionName',
120120
'rx_name' : 'phoneName',
121121
'gnss_id' : 'constellationType',
122122
'sv_id' : 'svid',
@@ -135,7 +135,7 @@ def fixture_inverse_col_map():
135135
'iono_delay_m' : 'ionoDelayM',
136136
'tropo_delay_m' : 'tropoDelayM',
137137
}
138-
return inverse_col_map
138+
return inverse_row_map
139139

140140

141141
@pytest.fixture(name="derived")
@@ -156,7 +156,7 @@ def fixture_load_derived(derived_path):
156156
return derived
157157

158158

159-
def test_derived_df_equivalence(derived, pd_df, derived_col_map):
159+
def test_derived_df_equivalence(derived, pd_df, derived_row_map):
160160
"""Test if naive dataframe and AndroidDerived contain same data
161161
162162
Parameters
@@ -165,15 +165,17 @@ def test_derived_df_equivalence(derived, pd_df, derived_col_map):
165165
Instance of AndroidDerived for testing
166166
pd_df : pytest.fixture
167167
pd.DataFrame for testing measurements
168-
derived_col_map : pytest.fixture
168+
derived_row_map : pytest.fixture
169169
Column map to convert standard to original derived column names
170170
171171
"""
172172
# Also tests if strings are being converted back correctly
173173
measure_df = derived.pandas_df()
174-
measure_df.rename(columns=derived_col_map, inplace=True)
174+
measure_df.rename(columns=derived_row_map, inplace=True)
175175
measure_df = measure_df.drop(columns='corr_pr_m')
176-
pd.testing.assert_frame_equal(pd_df, measure_df, check_dtype=False)
176+
pd.testing.assert_frame_equal(pd_df.sort_index(axis=1),
177+
measure_df.sort_index(axis=1),
178+
check_dtype=False, check_names=True)
177179

178180

179181
@pytest.mark.parametrize('row_name, index, value',
@@ -328,10 +330,10 @@ def test_csv_equivalence(android_raw_path, root_path, file_type):
328330
csv_loc = make_csv(android_raw_path, output_directory, file_type)
329331
test_df = pd.read_csv(csv_loc)
330332
test_measure = AndroidRawImu(android_raw_path)
331-
col_map = test_measure._column_map()
333+
row_map = test_measure._row_map()
332334
for col_name in test_df.columns:
333-
if col_name in col_map:
334-
row_idx = col_map[col_name]
335+
if col_name in row_map:
336+
row_idx = row_map[col_name]
335337
else:
336338
row_idx = col_name
337339
if col_name in no_check or col_name :

tests/parsers/test_navdata.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,5 @@ def test_col_looping(csv_simple):
958958
for idx, col in enumerate(data):
959959
col_df = col.pandas_df().reset_index(drop=True)
960960
expected_df = compare_df.iloc[[idx], :].reset_index(drop=True)
961-
print(col_df)
962-
print(expected_df)
963961
pd.testing.assert_frame_equal(col_df, expected_df,
964962
check_index_type=False)

tests/utils/test_visualizations.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ def test_plot_metrics(derived):
113113
"tropo_delay_m",
114114
]
115115

116-
for r_idx, row in enumerate(derived.rows):
117-
if not derived.str_bool[r_idx]:
116+
for row in derived.rows:
117+
if not derived.str_bool[derived.map[row]]:
118118
if row in test_rows:
119119
fig = viz.plot_metric(derived, row, save=False)
120120
close_figures(fig)
@@ -129,8 +129,8 @@ def test_plot_metrics(derived):
129129
viz.plot_metric(derived, "raw_pr_m", save=True, prefix=1)
130130
assert "Prefix" in str(excinfo.value)
131131

132-
for r_idx, row in enumerate(derived.rows):
133-
if not derived.str_bool[r_idx]:
132+
for row in derived.rows:
133+
if not derived.str_bool[derived.map[row]]:
134134
if row in test_rows:
135135
fig = viz.plot_metric(derived, "raw_pr_m", row, save=False)
136136
close_figures(fig)
@@ -167,8 +167,8 @@ def test_plot_metrics_by_constellation(derived):
167167
"tropo_delay_m",
168168
]
169169

170-
for r_idx, row in enumerate(derived.rows):
171-
if not derived.str_bool[r_idx]:
170+
for row in derived.rows:
171+
if not derived.str_bool[derived.map[row]]:
172172
if row in test_rows:
173173
fig = viz.plot_metric_by_constellation(derived, row, save=False)
174174
close_figures(fig)

0 commit comments

Comments
 (0)