Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix: Assure that feature dataframe indices are unique #187

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tobac/feature_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,9 @@ def feature_detection_multithreshold_timestep(
idx_start=idx_start,
)
if any([x is not None for x in features_threshold_i]):
features_thresholds = pd.concat([features_thresholds, features_threshold_i])
features_thresholds = pd.concat(
[features_thresholds, features_threshold_i], ignore_index=True
)

# For multiple threshold, and features found both in the current and previous step, remove "parent" features from Dataframe
if i_threshold > 0 and not features_thresholds.empty and regions_old:
Expand Down
70 changes: 67 additions & 3 deletions tobac/tests/test_feature_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ def test_feature_detection_multithreshold_timestep(
test_threshs, dxy, wavelength_filtering
):
"""
Tests ```tobac.feature_detection.feature_detection_multithreshold_timestep
Tests ```tobac.feature_detection.feature_detection_multithreshold_timestep```
"""
import numpy as np
from tobac import feature_detection

# start by building a simple dataset with a single feature and seeing
# if we identify it
Expand All @@ -37,7 +36,7 @@ def test_feature_detection_multithreshold_timestep(
amplitude=test_amp,
)
test_data_iris = tbtest.make_dataset_from_arr(test_data, data_type="iris")
fd_output = feature_detection.feature_detection_multithreshold_timestep(
fd_output = feat_detect.feature_detection_multithreshold_timestep(
test_data_iris,
0,
threshold=test_threshs,
Expand All @@ -53,6 +52,71 @@ def test_feature_detection_multithreshold_timestep(
assert fd_output.iloc[0]["hdim_2"] == pytest.approx(test_hdim_2_pt)


@pytest.mark.parametrize(
"test_threshs, min_distance, dxy", [([1, 2, 3], 100000, 10000)]
)
def test_filter_min_distance(test_threshs, min_distance, dxy):
"""
Tests ```tobac.feature_detection.filter_min_distance```
"""
# start by building a simple dataset with two features close to each other
import numpy as np

test_dset_size = (50, 50)
test_hdim_1_pt = 20.0
test_hdim_2_pt = 20.0
test_hdim_1_sz = 5
test_hdim_2_sz = 5
test_amp = 5
test_min_num = 2

test_data = np.zeros(test_dset_size)
test_data = tbtest.make_feature_blob(
test_data,
test_hdim_1_pt,
test_hdim_2_pt,
h1_size=test_hdim_1_sz,
h2_size=test_hdim_2_sz,
amplitude=test_amp,
)

## add another blob with smaller value
test_hdim_1_pt2 = 25.0
test_hdim_2_pt2 = 25.0
test_hdim_1_sz2 = 2
test_hdim_2_sz2 = 2
test_amp2 = 3
test_data = tbtest.make_feature_blob(
test_data,
test_hdim_1_pt2,
test_hdim_2_pt2,
h1_size=test_hdim_1_sz2,
h2_size=test_hdim_2_sz2,
amplitude=test_amp2,
)
test_data_iris = tbtest.make_dataset_from_arr(test_data, data_type="iris")

# identify these features
fd_output = feat_detect.feature_detection_multithreshold_timestep(
test_data_iris,
0,
threshold=test_threshs,
n_min_threshold=test_min_num,
min_distance=min_distance,
dxy=dxy,
)

# check if it function to filter
fd_filtered = feat_detect.filter_min_distance(fd_output, dxy, min_distance)

# Make sure we have only one feature (small feature in minimum distance should be removed )
assert len(fd_output.index) == 2
assert len(fd_filtered.index) == 1
# Make sure that the locations of the features is correct (should correspond to locations of first feature)
assert fd_filtered.iloc[0]["hdim_1"] == pytest.approx(test_hdim_1_pt)
assert fd_filtered.iloc[0]["hdim_2"] == pytest.approx(test_hdim_2_pt)


@pytest.mark.parametrize(
"position_threshold", [("center"), ("extreme"), ("weighted_diff"), ("weighted_abs")]
)
Expand Down