Skip to content

Commit

Permalink
tests: increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Nov 6, 2018
1 parent caaa694 commit 5c053da
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 5 deletions.
2 changes: 1 addition & 1 deletion nanite/cli/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"range_x": [0, 0],
"segment": "approach",
"weight_cp": 5e-7,
"rating method": "MERGE",
"rating method": "Extra Trees",
"rating train set": "zef18",
}

Expand Down
10 changes: 7 additions & 3 deletions nanite/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class FitDataError(BaseException):
pass


class FitWarning(UserWarning):
pass


class FitProperties(dict):
"""Fit property manager class
Expand Down Expand Up @@ -232,7 +236,7 @@ def __init__(self, data_set, **kwargs):

if self.fp["range_x"][0] > self.fp["range_x"][1]:
msg = "Fitting range is inverted: {}".format(self.fp["range_x"])
warnings.warn(msg)
warnings.warn(msg, FitWarning)

def compute_emodulus_vs_mindelta(self, callback=None):
"""Compute elastic modulus vs. minimal indentation curve"""
Expand Down Expand Up @@ -339,7 +343,7 @@ def compute_opt_mindelta(emoduli, indentations):
counts.pop(labmax)
else:
# Nothing found: select the middle value
warnings.warn("Could not find correct plateau.")
warnings.warn("Could not find correct plateau.", FitWarning)
labmax = 5
# Determine the interval in the original array
indices = np.where(labelarray == labmax)[0]
Expand Down Expand Up @@ -490,7 +494,7 @@ def get_initial_parameters(self, data_set=None, model_key="hertz_para"):
else:
msg = "Cannot estimate contact point, because of missing "\
+ "column 'tip position'"
warnings.warn(msg)
warnings.warn(msg, FitWarning)
return params

def _hash(self):
Expand Down
2 changes: 1 addition & 1 deletion nanite/indent.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def get_initial_fit_parameters(self):
parms = IndentationFitter(self).fp["params_initial"]
return parms

def rate_quality(self, method="MERGE", ts_label="zef18",
def rate_quality(self, method="Extra Trees", ts_label="zef18",
names=None, lda=None):
"""Compute the quality of the obtained curve
Expand Down
86 changes: 86 additions & 0 deletions tests/test_indent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import tempfile

import numpy as np
import pytest

import nanite

Expand Down Expand Up @@ -142,13 +143,98 @@ def test_fitting():
assert np.allclose(params3["E"].value, 14839.821714634612)


@pytest.mark.filterwarnings('ignore::nanite.fit.FitWarning')
def test_get_initial_fit_parameters():
"""This is a convenience function"""
ds1 = nanite.IndentationDataSet(jpkfile)
idnt = ds1[0]
# A: sanity check
try:
idnt.get_initial_fit_parameters()
except KeyError:
pass
else:
assert False, "need to get tip position first"
# B: get default fit parameters (hertz_para)
idnt.apply_preprocessing(["compute_tip_position"])
fp = idnt.get_initial_fit_parameters()
for kk in ['E', 'R', 'nu', 'contact_point', 'baseline']:
assert kk in fp.keys()
# C: set other model and get fit parameters
idnt.fit_properties["model_key"] = "hertz_pyr3s"
fp2 = idnt.get_initial_fit_parameters()
for kk in ['E', 'alpha', 'nu', 'contact_point', 'baseline']:
assert kk in fp2.keys()
# D: fit and get from fit_properties
idnt.fit_model(params_initial=fp2)
fp3 = idnt.get_initial_fit_parameters()
assert fp2 == fp3


def test_get_model():
md = nanite.model.model_hertz_parabolic
model_name = "parabolic indenter (Hertz)"
md2 = nanite.model.get_model_by_name(model_name)
assert md2 is md


def test_rate_quality_cache():
ds1 = nanite.IndentationDataSet(jpkfile)
idnt = ds1[0]
idnt.apply_preprocessing(["compute_tip_position"])

inparams = nanite.model.model_hertz_parabolic.get_parameter_defaults()
inparams["baseline"].vary = True
inparams["contact_point"].set(1.8e-5)

# Fit with absolute full range
idnt.fit_model(model_key="hertz_para",
params_initial=inparams,
range_x=(0, 0),
range_type="absolute",
x_axis="tip position",
y_axis="force",
segment="approach",
weight_cp=False)
r1 = idnt.rate_quality(ts_label="zef18",
method="Extra Trees")
assert idnt._rating[-1] == r1
r2 = idnt.rate_quality(ts_label="zef18",
method="Extra Trees")
assert r1 == r2


def test_rate_quality_disabled():
ds1 = nanite.IndentationDataSet(jpkfile)
idnt = ds1[0]
idnt.apply_preprocessing(["compute_tip_position"])

inparams = nanite.model.model_hertz_parabolic.get_parameter_defaults()
inparams["baseline"].vary = True
inparams["contact_point"].set(1.8e-5)

# Fit with absolute full range
idnt.fit_model(model_key="hertz_para",
params_initial=inparams,
range_x=(0, 0),
range_type="absolute",
x_axis="tip position",
y_axis="force",
segment="approach",
weight_cp=False)

r1 = idnt.rate_quality(ts_label="zef18",
method="none")
assert r1 == -1


def test_rate_quality_nofit():
ds1 = nanite.IndentationDataSet(jpkfile)
idnt = ds1[0]
r1 = idnt.rate_quality()
assert r1 == -1


if __name__ == "__main__":
# Run all tests
loc = locals()
Expand Down
File renamed without changes.

0 comments on commit 5c053da

Please sign in to comment.