Skip to content

Commit

Permalink
ref: return normalization reference type for details from POC and pre…
Browse files Browse the repository at this point in the history
…proc
  • Loading branch information
paulmueller committed Jul 22, 2023
1 parent fc2c251 commit e3f91c4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
3.7.0
- feat: subtract linear slope from indentation curve (#22)
- ref: return normalization reference type for details from POC and preproc
- setup: bump scipy to 1.10.0 due to memory leak vulnerability
3.6.0
- tests: make tests less strict and some cleanup
Expand Down
54 changes: 32 additions & 22 deletions nanite/poc.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def poc_deviation_from_baseline(force, ret_details=False):
bl_avg + bl_rng]]
details["plot poc"] = [[cp, cp],
[force.min(), force.max()]]
details["norm"] = "force"

if ret_details:
return cp, details
Expand Down Expand Up @@ -164,13 +165,15 @@ def model(params, x):
def residual(params, x, data):
return data - model(params, x)

y = np.array(force, copy=True)
cp = np.nan
details = {}
if y.size > 4: # 3 fit parameters
ymin, ymax = np.min(y), np.max(y)
y = (y - ymin) / (ymax - ymin)
if force.size > 4: # 3 fit parameters
# normalize force
fmin = np.min(force)
fptp = np.max(force) - fmin
y = (force - fmin) / fptp
x = np.arange(y.size)
# get estimate for cp
x0 = poc_frechet_direct_path(force)
if np.isnan(x0):
x0 = y.size // 2
Expand All @@ -183,11 +186,12 @@ def residual(params, x, data):
if out.success:
cp = int(out.params["x0"])
if ret_details:
details["plot force"] = [x, y]
details["plot force"] = [x, force]
details["plot fit"] = [np.arange(force.size),
model(out.params, x)]
model(out.params, x) * fptp + fmin]
details["plot poc"] = [[cp, cp],
[y.min(), y.max()]]
[fmin, fmin + fptp]]
details["norm"] = "force"

if ret_details:
return cp, details
Expand Down Expand Up @@ -244,12 +248,12 @@ def residual(params, x, data):
curve = model(params, x)
return data - curve

y = np.array(force, copy=True)
cp = np.nan
details = {}
if y.size > 6: # 5 fit parameters
ymin, ymax = np.min(y), np.max(y)
y = (y - ymin) / (ymax - ymin)
if force.size > 6: # 5 fit parameters
fmin = np.min(force)
fptp = np.max(force) - fmin
y = (force - fmin) / fptp
x = np.arange(y.size)
x0 = poc_frechet_direct_path(force)
if np.isnan(x0):
Expand All @@ -273,11 +277,12 @@ def residual(params, x, data):
if out.success:
cp = int(out.params["x0"])
if ret_details:
details["plot force"] = [x, y]
details["plot force"] = [x, force]
details["plot fit"] = [np.arange(force.size),
model(out.params, x)]
model(out.params, x) * fptp + fmin]
details["plot poc"] = [[cp, cp],
[y.min(), y.max()]]
[fmin, fmin + fptp]]
details["norm"] = "force"

if ret_details:
return cp, details
Expand Down Expand Up @@ -343,12 +348,12 @@ def residual(params, x, data):
curve = model(params, x)
return data - curve

y = np.array(force, copy=True)
cp = np.nan
details = {}
if y.size > 7: # 6 fit parameters
ymin, ymax = np.min(y), np.max(y)
y = (y - ymin) / (ymax - ymin)
if force.size > 7: # 6 fit parameters
fmin = np.min(force)
fptp = np.max(force) - fmin
y = (force - fmin) / fptp
x = np.arange(y.size)
x0 = poc_frechet_direct_path(force)
if np.isnan(x0):
Expand All @@ -374,11 +379,12 @@ def residual(params, x, data):
if out.success:
cp = int(out.params["x0"])
if ret_details:
details["plot force"] = [x, y]
details["plot force"] = [x, force]
details["plot fit"] = [np.arange(force.size),
model(out.params, x)]
model(out.params, x) * fptp + fmin]
details["plot poc"] = [[cp, cp],
[y.min(), y.max()]]
[fmin, fmin + fptp]]
details["norm"] = "force"

if ret_details:
return cp, details
Expand Down Expand Up @@ -418,7 +424,9 @@ def poc_frechet_direct_path(force, ret_details=False):
details = {"plot normalized rotated force": [np.arange(len(force)),
yr],
"plot poc": [[cp, cp],
[yr.min(), yr.max()]]}
[yr.min(), yr.max()]],
"norm": "force-rotated",
}
return cp, details
else:
return cp
Expand Down Expand Up @@ -463,12 +471,14 @@ def poc_gradient_zero_crossing(force, ret_details=False):
cp = y.size - np.where(gradpos[::-1])[0][0] - cutoff + filtsize

if ret_details:
# scale the gradient so that it aligns with the force
x = np.arange(gradn.size)
details["plot force gradient"] = [x, gradn]
details["plot threshold"] = [[x[0], x[-1]],
[thresh, thresh]]
details["plot poc"] = [[cp, cp],
[gradn.min(), gradn.max()]]
details["norm"] = "force-gradient"

if ret_details:
return cp, details
Expand Down
6 changes: 5 additions & 1 deletion nanite/preproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ def preproc_correct_force_slope(apret, region="baseline", ret_details=False):
out = mod.fit(apret["force"][:idp], pars, x=apret["tip position"][:idp])
# Subtract the linear slope from the region data.
force = apret["force"]
force_orig = np.copy(force[:idp])
if region == "baseline":
# Only subtract the force from data up until the contact point.
# Make sure that there is no offset/jump by pulling the last
Expand All @@ -321,7 +322,10 @@ def preproc_correct_force_slope(apret, region="baseline", ret_details=False):
apret["force"] = force

if ret_details:
return {"plot slope": [np.arange(idp), out.best_fit]}
return {
"plot slope data": [np.arange(idp), force_orig],
"plot slope fit": [np.arange(idp), out.best_fit],
"norm": "force"}


@preprocessing_step(
Expand Down

0 comments on commit e3f91c4

Please sign in to comment.