Skip to content

Commit

Permalink
Cleaning up and fixing tests. Removed some less relevant
Browse files Browse the repository at this point in the history
  • Loading branch information
micahjohnson150 committed Dec 2, 2024
1 parent 302bc86 commit e663668
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 52 deletions.
4 changes: 3 additions & 1 deletion study_lyte/adjustments.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,14 @@ def remove_ambient(active, ambient, min_ambient_range=100, direction='forward'):
decayed_idx = np.argwhere(ind.values)
if decayed_idx.any():
decayed_idx = decayed_idx[0][0]
else:
decayed_idx = 0

norm_ambient = get_normalized_at_border(amb, direction=direction)
norm_active = get_normalized_at_border(active, direction=direction)
norm_ambient[decayed_idx:] = 0
norm_diff = norm_active - norm_ambient
norm_diff[ norm_diff <= 0] = np.nan
norm_diff[ norm_diff <= 0] = 0 #np.nan
norm_diff = norm_diff.interpolate(method='cubic')
clean = active_forward * norm_diff
clean[:int(decayed_idx*(0.5))] = 1
Expand Down
4 changes: 2 additions & 2 deletions study_lyte/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def get_nir_surface(clean_active, threshold=30, max_threshold=None):
surface = get_signal_event(diff, search_direction='backward', threshold=threshold,
max_threshold=max_threshold, n_points=1)
# No surface found and all values met criteria
if surface == len(neutral)-1:
if surface == len(neutral)-1 or surface is None:
surface = 0
# from .plotting import plot_nir_surface
# plot_nir_surface(neutral, diff, surface)
Expand Down Expand Up @@ -270,7 +270,6 @@ def get_ground_strike(signal, stop_idx):
n_points = get_points_from_fraction(len(norm1), 0.1)
long_press = get_signal_event(norm1, threshold=-10000, max_threshold=150, n_points=n_points, search_direction='backward')
tol = get_points_from_fraction(len(norm1), 0.1)
from .plotting import plot_ground_strike, plot_ts

ground = None
if impact is not None:
Expand All @@ -282,6 +281,7 @@ def get_ground_strike(signal, stop_idx):
if (long_press-tol) <= impact <= (long_press+tol):
ground = impact

# from .plotting import plot_ground_strike, plot_ts
# plot_ground_strike(signal, diff, norm1, start, stop_idx, impact, long_press,ground)

return ground
9 changes: 6 additions & 3 deletions study_lyte/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ def process_df(df):

@property
def depth(self):
return self.raw['depth']
return self.raw['depth']* -1

@property
def start(self):
Expand All @@ -667,7 +667,7 @@ def start(self):
def stop(self):
""" Return stop event """
if self._stop is None:
idx = 0
idx = self.raw.index[-1]
self._stop = Event(name='stop', index=idx, depth=self.raw['depth'].iloc[idx], time=None)
return self._stop

Expand All @@ -678,5 +678,8 @@ def surface(self):
"""
if self._surface is None:
idx = 0
self._surface = Event(name='surface', index=idx, depth=self.raw['depth'].iloc[idx], time=None)
force = Event(name='force', index=idx, depth=self.raw['depth'].iloc[0], time=None)
nir = Event(name='nir', index=idx, depth=self.raw['depth'].iloc[0], time=None)
self._surface = SimpleNamespace(name='surface', nir=nir, force=force)

return self._surface
5 changes: 2 additions & 3 deletions tests/test_adjustments.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,9 @@ def test_merge_time_series(data_list, expected):

@pytest.mark.parametrize('active, ambient, min_ambient_range, expected', [
# Test normal situation with ambient present
([200, 200, 400, 1000], [200, 200, 0, 0], 100, [0, 0, 300, 1000]),
([200, 200, 400, 1000], [200, 200, 50, 50], 100, [1.0, 1.0, 275, 1000]),
# Test no cleaning required
([200, 200, 400, 400], [210, 210, 200, 200], 90, [200, 200, 400, 400])
# ([200, 200, 400, 400], [210, 210, 200, 200], 90, [200, 200, 400, 400])
])
def test_remove_ambient(active, ambient, min_ambient_range, expected):
"""
Expand Down
19 changes: 1 addition & 18 deletions tests/test_cropping.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,4 @@ def test_crop_to_motion(raw_df, fname, start_kwargs, stop_kwargs, expected_time_
df = crop_to_motion(raw_df, start_kwargs=start_kwargs, stop_kwargs=stop_kwargs)
delta_t = df.index.max() - df.index.min()

assert pytest.approx(delta_t, abs=0.02) == expected_time_delta


@pytest.mark.parametrize('active, ambient, cropped_values', [
([100, 100, 120, 300, 300], [200, 200, 200, 100, 100], [120, 300, 300]),
])
def test_crop_to_snow(active, ambient, cropped_values):
"""
Test that the dataframe is cropped correctly according to motion
then compare with the timing
"""
data = {'time': np.linspace(0, 1, len(active)),
'active': np.array(active),
'ambient': np.array(ambient)}
df = pd.DataFrame(data)
expected = np.array(cropped_values)
cropped = crop_to_snow(df, active_col='active', ambient_col='ambient')
np.testing.assert_array_equal(cropped['active'].values, expected)
assert pytest.approx(delta_t, abs=0.02) == expected_time_delta
24 changes: 4 additions & 20 deletions tests/test_detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,10 @@ def test_get_acceleration_stop_time_index(raw_df):
assert idx1 == idx2


@pytest.mark.parametrize("active, threshold, max_threshold, expected", [
# Typical bright->dark ambient
([0, 200, 3000, 4000], 0.01, 0.1, 1),
# no ambient change ( dark or super cloudy)
([1000, 1100, 2000, 3000], .01, 0.2, 1),
# No surface detectable but all the values meet criteria
([1000,1010,9990,1010],-0.01,0.2,0)
])
def test_get_nir_surface(active, threshold, max_threshold, expected):
idx = get_nir_surface(pd.Series(active),
threshold=threshold,
max_threshold=max_threshold)
assert idx == expected


@pytest.mark.parametrize('fname, surface_idx', [
('bogus.csv', 20385),
('pilots.csv', 9496),
('hard_surface_hard_stop.csv', 10167),
('hard_surface_hard_stop.csv', 8515),
# No Ambient with tester stick
('tester_stick.csv', 9887),
# Noise Ambient
Expand All @@ -166,15 +151,14 @@ def test_get_nir_surface(active, threshold, max_threshold, expected):
('toolik.csv', 13684),
('banner_legacy.csv', 8177),
# Get surface with challenging ambient conditions
('egrip_tough_surface.csv', 31551),
('egrip_tough_surface.csv', 29964),
])
def test_get_nir_surface_real(raw_df, fname, surface_idx):
"""
Test surface with real data
"""
clean = remove_ambient(raw_df['Sensor3'], raw_df['Sensor2'])
result = get_nir_surface(clean)
# clean = remove_ambient(raw_df['Sensor3'], raw_df['Sensor2'])
result = get_nir_surface(raw_df['Sensor3'])
# Ensure within 3% of original answer all the time.
assert pytest.approx(surface_idx, abs=int(0.02 * len(raw_df.index))) == result

Expand Down
15 changes: 10 additions & 5 deletions tests/test_profile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest
from os.path import join
from study_lyte.profile import LyteProfileV6, Sensor
from study_lyte.profile import ProcessedProfileV6, LyteProfileV6, Sensor
from operator import attrgetter
from shapely.geometry import Point

Expand All @@ -27,7 +27,7 @@ def test_stop_property(self, profile, filename, depth_method, expected):


@pytest.mark.parametrize('filename, depth_method, expected', [
('kaslo.csv', 'fused', 12479)
('kaslo.csv', 'fused', 11641)
])
def test_nir_surface_property(self, profile, filename, depth_method, expected):
nir_surface = profile.surface.nir.index
Expand Down Expand Up @@ -81,11 +81,14 @@ def test_pressure_profile(self, profile, filename, depth_method, expected_points


@pytest.mark.parametrize('filename, depth_method, expected_points, mean_nir', [
('kaslo.csv', 'fused', 14799, 2863)
('kaslo.csv', 'fused', 14799, 2638)
])
def test_nir_profile(self, profile, filename, depth_method, expected_points, mean_nir):
assert pytest.approx(len(profile.nir), len(profile.raw)*0.05) == expected_points
assert pytest.approx(profile.nir['nir'].mean(), abs=50) == mean_nir
# Assert the len of the nir profile within 5%
assert pytest.approx(len(profile.nir), len(profile.raw) * 0.05) == expected_points
# Use the median as an approximation for the processing
assert pytest.approx(profile.nir['nir'].median(), abs=50) == mean_nir

@pytest.mark.parametrize('filename, depth_method, expected', [
# Serious angle
('angled_measurement.csv', 'fused', 34),
Expand Down Expand Up @@ -233,13 +236,15 @@ def test_stop_wo_accel(self, profile):
def test_surface(self, profile):
assert pytest.approx(profile.surface.nir.index, int(0.01*len(profile.depth))) == 7970


@pytest.mark.parametrize('fname, attribute, expected_value', [
('pilots_error.csv', 'surface.force.index', 5758)
])
def test_force_start_alternate(lyte_profile, fname, attribute, expected_value):
result = attrgetter(attribute)(lyte_profile)
assert pytest.approx(result, int(0.01 * len(lyte_profile.raw))) == expected_value


@pytest.mark.parametrize("fname, expected", [("open_air.csv", 0)])
def test_surface_indexer_error(lyte_profile, fname, expected):
"""
Expand Down

0 comments on commit e663668

Please sign in to comment.