Skip to content

Commit

Permalink
fix: bad data caused TypeError in numpy.polynomial (close #25)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Nov 10, 2023
1 parent c9c5dbf commit 2d22bda
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- BREAKING CHANGE: Due to the new imputation of nan-valued features,
rating inference might change slightly. For the "zef18" dataset,
inference was off by about 0.1 on average.
- fix: bad data caused TypeError in numpy.polynomial (#25)
- enh: impute nan-valued feature data if corresponding response was 0
- enh: allow empty-valued groups in rating HDF5 file
- enh: when encountering inf values in a training set, replace them
Expand Down
7 changes: 5 additions & 2 deletions nanite/preproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,9 @@ def find_turning_point(tip_position, force, contact_point_index):
# Flip and normalize tip position so that maximum is at minimum
# z-position (set to 1) which coincides with maximum indentation.
x -= x[idp]
x /= x.min()
xmin = x.min()
if xmin != 0:
x /= x.min()
x[x < 0] = 0

# Flip and normalize force so that maximum force is set to 1.
Expand Down Expand Up @@ -336,7 +338,7 @@ def preproc_correct_force_slope(apret, region="baseline", strategy="shift",
force = apret["force"]

# Get the current contact point position computed by "correct_tip_offset".
idp = np.argmin(np.abs(tip_position))
idp = max(2, np.argmin(np.abs(tip_position)))
# Determine whether we want to do temporal or spatial correction:
# Fit a linear slope to the baseline part (all data up until idp)
mod = lmfit.models.LinearModel()
Expand All @@ -362,6 +364,7 @@ def preproc_correct_force_slope(apret, region="baseline", strategy="shift",
idturn = find_turning_point(tip_position=tip_position,
force=force_edit,
contact_point_index=idp)
idturn = max(2, idturn)
# Extend the best fit towards the turning point.
best_fit_approach = mod.eval(out.params, x=abscissa[:idturn])
force_edit[:idturn] -= best_fit_approach - best_fit_approach[-1]
Expand Down
31 changes: 31 additions & 0 deletions tests/test_preproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,37 @@ def test_get_steps_required():
assert req_act == req_exp


def test_preproc_correct_force_slope_bad_monotonic_data_issue_25():
fd = IndentationGroup(
data_path
/ "fmt-jpk-fd_single_tilted-baseline-drift-"
"mitotic_2021-01-29.jpk-force")[0]
# put a custom, bad tip position (this requires some hacking)
s = len(fd["force"])
raw_data = {}
for col in fd.columns_innate:
raw_data[col] = fd[col]
tippos = np.linspace(0, 5e-9, s)
tippos = np.roll(tippos, s//2)
tippos[:s//2] = 0
raw_data["tip position"] = tippos
fd._raw_data = raw_data
# sanity check
assert np.all(fd["tip position"] == tippos)
assert "tip position" in fd.columns_innate
# This caused "TypeError: expected non-empty vector for x" in
# np.polynomial in nanite 3.7.3.
fd.apply_preprocessing(
["compute_tip_position", "correct_tip_offset", "correct_force_slope",
"correct_force_offset"],
options={
"correct_tip_offset": {"method": "deviation_from_baseline"},
"correct_force_slope": {"region": "approach",
"strategy": "drift"},
},
ret_details=True)


def test_preproc_correct_force_slope_drift_approach():
fd = IndentationGroup(
data_path
Expand Down

0 comments on commit 2d22bda

Please sign in to comment.