Skip to content

Commit c8edbcb

Browse files
committed
create is_str and move old str_bool to private function
1 parent a8f2def commit c8edbcb

3 files changed

Lines changed: 57 additions & 10 deletions

File tree

gnss_lib_py/parsers/navdata.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,14 @@ def _get_str_rows(self, rows):
255255
row_str : list
256256
List of boolean values indicating which rows contain strings
257257
"""
258-
str_bool = self.str_bool
258+
_row_idx_str_bool = self._row_idx_str_bool
259259
if isinstance(rows, slice):
260260
slice_idx = rows.indices(self.shape[0])
261261
row_list = np.arange(slice_idx[0], slice_idx[1], slice_idx[2])
262-
row_str = [str_bool[row] for row in row_list]
262+
row_str = [_row_idx_str_bool[row] for row in row_list]
263263
else:
264264
row_list = list(rows)
265-
row_str = [str_bool[row] for row in rows]
265+
row_str = [_row_idx_str_bool[row] for row in rows]
266266
return row_list, row_str
267267

268268
def __getitem__(self, key_idx):
@@ -633,19 +633,42 @@ def rows(self):
633633

634634

635635
@property
636-
def str_bool(self):
636+
def _row_idx_str_bool(self):
637637
"""Dictionary of index : if data entry is string.
638638
639639
Row has string values if the string map is nonempty for a
640640
given row.
641641
642642
Returns
643643
-------
644-
str_bool : Dict
644+
_row_idx_str_bool : Dict
645645
Dictionary of whether data at row number key is string or not
646646
"""
647-
str_bool = {self.map[k]: bool(len(self.str_map[k])) for k in self.str_map.keys()}
648-
return str_bool
647+
_row_idx_str_bool = {self.map[k]: bool(len(self.str_map[k])) for k in self.str_map.keys()}
648+
return _row_idx_str_bool
649+
650+
def is_str(self, row_name):
651+
"""Check whether a row contained string values.
652+
653+
Parameters
654+
----------
655+
row_name : string
656+
Name of the row to check whether it contains string values.
657+
658+
Returns
659+
-------
660+
contains_str : bool
661+
True if the row contains string values, False otherwise.
662+
663+
"""
664+
665+
if row_name not in self.map:
666+
raise KeyError("'" + str(row_name) \
667+
+ "' key doesn't exist in NavData class")
668+
669+
contains_str = self._row_idx_str_bool[self.map[row_name]]
670+
671+
return contains_str
649672

650673
@property
651674
def inv_map(self):

tests/parsers/test_navdata.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,3 +960,27 @@ def test_col_looping(csv_simple):
960960
expected_df = compare_df.iloc[[idx], :].reset_index(drop=True)
961961
pd.testing.assert_frame_equal(col_df, expected_df,
962962
check_index_type=False)
963+
964+
def test_is_str(df_simple):
965+
"""Test the is_str function.
966+
967+
Parameters
968+
----------
969+
df_simple : pd.DataFrame
970+
Simple pd.DataFrame with which to initialize NavData.
971+
972+
"""
973+
navdata = NavData(pandas_df=df_simple)
974+
975+
# check on simple dataframe rows
976+
assert navdata.is_str("names")
977+
assert not navdata.is_str("integers")
978+
assert not navdata.is_str("floats")
979+
assert navdata.is_str("strings")
980+
981+
# should raise error if key doesn't exist
982+
with pytest.raises(KeyError):
983+
navdata.is_str("bananas")
984+
985+
with pytest.raises(KeyError):
986+
navdata.is_str(0)

tests/utils/test_visualizations.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def test_plot_metrics(derived):
114114
]
115115

116116
for row in derived.rows:
117-
if not derived.str_bool[derived.map[row]]:
117+
if not derived.is_str(row):
118118
if row in test_rows:
119119
fig = viz.plot_metric(derived, row, save=False)
120120
close_figures(fig)
@@ -130,7 +130,7 @@ def test_plot_metrics(derived):
130130
assert "Prefix" in str(excinfo.value)
131131

132132
for row in derived.rows:
133-
if not derived.str_bool[derived.map[row]]:
133+
if not derived.is_str(row):
134134
if row in test_rows:
135135
fig = viz.plot_metric(derived, "raw_pr_m", row, save=False)
136136
close_figures(fig)
@@ -168,7 +168,7 @@ def test_plot_metrics_by_constellation(derived):
168168
]
169169

170170
for row in derived.rows:
171-
if not derived.str_bool[derived.map[row]]:
171+
if not derived.is_str(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)