Skip to content

Commit

Permalink
ref: properly test for valid strategy and region (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Jul 24, 2023
1 parent 93c3123 commit 2a998ea
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 18 deletions.
29 changes: 16 additions & 13 deletions nanite/preproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,10 @@ def preproc_correct_force_slope(apret, region="baseline", strategy="shift",
mod = lmfit.models.LinearModel()
if strategy == "shift":
abscissa = tip_position
else:
elif strategy == "drift":
abscissa = time_position
else:
raise ValueError(f"Invalid strategy '{strategy}'!")
pars = mod.guess(force[:idp], x=abscissa[:idp])
out = mod.fit(force[:idp], pars, x=abscissa[:idp])

Expand All @@ -354,21 +356,22 @@ def preproc_correct_force_slope(apret, region="baseline", strategy="shift",
# Make sure that there is no offset/jump by pulling the last
# element of the best fit array to zero.
force_edit[:idp] -= out.best_fit - out.best_fit[-1]
else:
elif region == "approach":
# Subtract the force from everything that is part of the
# indentation part.
idturn = find_turning_point(tip_position=tip_position,
force=force_edit,
contact_point_index=idp)
if region == "appraoch":
# Subtract the force from everything that is part of the
# indentation part.
# 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]
elif region == "all":
# Use the same approach as above, but subtract from the entire
# curve.
best_fit_all = mod.eval(out.params, x=abscissa)
force_edit -= best_fit_all - best_fit_all[idp]
# 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]
elif region == "all":
# Use the same approach as above, but subtract from the entire
# curve.
best_fit_all = mod.eval(out.params, x=abscissa)
force_edit -= best_fit_all - best_fit_all[idp]
else:
raise ValueError(f"Invalid region '{region}'!")

# Override the force information
apret["force"] = force_edit
Expand Down
97 changes: 92 additions & 5 deletions tests/test_preproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,99 @@ def test_get_steps_required():
assert req_act == req_exp


def test_preproc_correct_force_slope():
def test_preproc_correct_force_slope_drift_approach():
fd = IndentationGroup(
data_path
/ "fmt-jpk-fd_single_tilted-baseline-drift-mitotic_2021-01-29.jpk-force")[0]
/ "fmt-jpk-fd_single_tilted-baseline-drift-"
"mitotic_2021-01-29.jpk-force")[0]
details = fd.apply_preprocessing(
["compute_tip_position", "correct_tip_offset", "correct_force_slope",
"correct_force_offset"],
options={
"correct_tip_offset": {"method": "fit_line_polynomial"},
"correct_force_slope": {"region": "approach",
"strategy": "drift"},
},
ret_details=True)
slopedet = details["correct_force_slope"]
for key in ["plot slope data",
"plot slope fit",
"norm"]:
assert key in slopedet
# Sanity check for size of baseline (determined by POC via line+poly)
assert len(slopedet["plot slope data"][0]) == 15602
# Check for flatness of baseline.
assert np.ptp(fd["force"][:15000]) < .094e-9, "ptp less than 0.1 nN"
bl0 = np.mean(fd["force"][:100])
bl1 = np.mean(fd["force"][10000:10100])
assert np.allclose(bl0, bl1,
atol=1e-11,
rtol=0)
assert np.abs(bl1 - bl0) < 0.0085e-9
# Now check whether the maximum force is lower than normal.
assert np.allclose(np.max(fd["force"]), 1.6688553846662955e-09,
atol=0)
assert np.allclose(np.min(fd["force"]), -5.935613094149927e-10,
atol=0)
# Compare it to the correction obtained with just "baseline".
# For this dataset, the drift is resulting in a continuous increase
# in the force. Thus, if we compare the "approach" correction with
# just the "baseline" correction, we should arrive at a lower number
# for the "approach" correction.
assert np.max(fd["force"]) < 1.6903757572616587e-09


def test_preproc_correct_force_slope_drift_full():
fd = IndentationGroup(
data_path
/ "fmt-jpk-fd_single_tilted-baseline-drift-"
"mitotic_2021-01-29.jpk-force")[0]
details = fd.apply_preprocessing(
["compute_tip_position", "correct_tip_offset", "correct_force_slope"],
options={
"correct_tip_offset": {"method": "fit_line_polynomial"},
"correct_force_slope": {"region": "all",
"strategy": "drift"},
},
ret_details=True)
slopedet = details["correct_force_slope"]
for key in ["plot slope data",
"plot slope fit",
"norm"]:
assert key in slopedet
# Sanity check for size of baseline (determined by POC via line+poly)
assert len(slopedet["plot slope data"][0]) == 15602
# Check for flatness of baseline.
assert np.ptp(fd["force"][:15000]) < .094e-9, "ptp less than 0.1 nN"
bl0 = np.mean(fd["force"][:100])
bl1 = np.mean(fd["force"][10000:10100])
assert np.allclose(bl0, bl1,
atol=0,
rtol=.01)
assert np.abs(bl1 - bl0) < 0.0085e-9
# Now check for flatness of retract tail
rt0 = np.mean(fd["force"][-100])
rt1 = np.mean(fd["force"][-10100:-10000])
assert rt1 < rt0, "There is still a little drift left in the retract tail"
# The following test would fail if we set "strategy" to "shift" or if
# we set region to "baseline" or "approach".
assert np.allclose(rt0, rt1,
atol=0.06e-9, rtol=0)


def test_preproc_correct_force_slope_shift():
fd = IndentationGroup(
data_path
# Note: this dataset actually resembles a drift over time, not
# a shift. But we are only looking at the baseline, so it's OK.
/ "fmt-jpk-fd_single_tilted-baseline-drift-"
"mitotic_2021-01-29.jpk-force")[0]
details = fd.apply_preprocessing(
["compute_tip_position", "correct_tip_offset", "correct_force_slope"],
options={
"correct_tip_offset": {"method": "fit_line_polynomial"},
"correct_force_slope": {"region": "baseline"},
"correct_force_slope": {"region": "baseline",
"strategy": "shift"},
},
ret_details=True)
slopedet = details["correct_force_slope"]
Expand All @@ -106,11 +190,14 @@ def test_preproc_correct_force_slope():
assert np.abs(bl1 - bl0) < 0.009e-9


def test_preproc_correct_force_slope_control():
def test_preproc_correct_force_slope_shift_control():
"""Same test as above, but without slope correction"""
fd = IndentationGroup(
data_path
/ "fmt-jpk-fd_single_tilted-baseline-drift-mitotic_2021-01-29.jpk-force")[0]
# Note: this dataset actually resembles a drift over time, not
# a shift. But we are only looking at the baseline, so it's OK.
/ "fmt-jpk-fd_single_tilted-baseline-drift-"
"mitotic_2021-01-29.jpk-force")[0]
fd.apply_preprocessing(
["compute_tip_position", "correct_tip_offset"],
options={
Expand Down

0 comments on commit 2a998ea

Please sign in to comment.