Skip to content

Commit

Permalink
ref: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Nov 20, 2020
1 parent b2a2e35 commit 538ca1d
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 50 deletions.
12 changes: 7 additions & 5 deletions nanite/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .group import IndentationGroup, load_group # noqa: F401
from .indent import Indentation # noqa: F401
from .qmap import QMap # noqa: F401
from .rate import IndentationRater # noqa: F401
# flake8: noqa: F401
from .group import IndentationGroup, load_group
from .indent import Indentation
from . import model
from .qmap import QMap
from .rate import IndentationRater

from ._version import version as __version__ # noqa: F401
from ._version import version as __version__
4 changes: 3 additions & 1 deletion nanite/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ def __init__(self, path=None, callback=None):
Parameters
----------
path: str
path: str or pathlib.Path or None
The path to the data file. The data format is determined
using the extension of the file and the data is loaded
with the correct method.
callback: callable or None
A method that accepts a float between 0 and 1
to externally track the process of loading the data.
"""
if path is not None:
path = pathlib.Path(path)
self._mmlist = []

if path is not None:
Expand Down
2 changes: 1 addition & 1 deletion nanite/indent.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def get_initial_fit_parameters(self, model_key=None,
model_key: str
Optionally set a model key. This will override the
"model_key" key in `self.fit_properties`.
global_ancillaries: bool
common_ancillaries: bool
Guess global ancillaries such as the contact point.
model_ancillaries: bool
Guess model-related ancillaries
Expand Down
4 changes: 2 additions & 2 deletions tests/test_cli_rating.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def setup_training_set(n=300):
for cc in IndentationRater.get_feature_names(which_type="continuous"):
cvals = np.random.random_sample(size=n)
np.savetxt(tdir / "train_{}.txt".format(cc), cvals)
rating = np.random.choice(range(11), size=n)
np.savetxt(tdir / "train_response.txt", rating)
thisrating = np.random.choice(range(11), size=n)
np.savetxt(tdir / "train_response.txt", thisrating)
return tdir


Expand Down
6 changes: 3 additions & 3 deletions tests/test_fit_ancillary.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
jpkfile = datapath / "spot3-0192.jpk-force"


class MockModel():
class MockModel:
def __init__(self, model_key, **kwargs):
# rebase on hertz model
md = nanite.model.models_available["hertz_para"]
for key in dir(md):
setattr(self, key, getattr(md, key))
for akey in dir(md):
setattr(self, akey, getattr(md, akey))
for kw in kwargs:
setattr(self, kw, kwargs[kw])
self.model_key = model_key
Expand Down
6 changes: 3 additions & 3 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import nanite.model


class MockModel():
class MockModel:
def __init__(self, model_key, **kwargs):
# rebase on hertz model
md = nanite.model.models_available["hertz_para"]
for key in dir(md):
setattr(self, key, getattr(md, key))
for akey in dir(md):
setattr(self, akey, getattr(md, akey))
for kw in kwargs:
setattr(self, kw, kwargs[kw])
self.model_key = model_key
Expand Down
2 changes: 1 addition & 1 deletion tests/test_model_expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
jpkfile = datadir / "spot3-0192.jpk-force"


class MockModelExpr():
class MockModelExpr:
def __init__(self, **kwargs):
"""E and E1 add up to the actual emodulus. E1 is varied indirectly"""
self.model_doc = """Mock model with constraint"""
Expand Down
8 changes: 4 additions & 4 deletions tests/test_model_sneddon_spherical_invert_delta.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
"""Test of inverse function"""
import numpy as np

from nanite.model import model_sneddon_spherical as hertzSpherical
from nanite.model import model_sneddon_spherical as hertz_spherical


def test_get_a_sneddon():
R = 40e-6
r = 40e-6
delta_real = [0.1e-6, 2e-6, 5e-6]
delta_predict = np.zeros_like(delta_real)
for i in range(len(delta_real)):
a = hertzSpherical.get_a(R, delta_real[i], accuracy=1e-09)
delta_predict[i] = hertzSpherical.delta_of_a(a, R)
a = hertz_spherical.get_a(r, delta_real[i], accuracy=1e-09)
delta_predict[i] = hertz_spherical.delta_of_a(a, r)

assert np.allclose(delta_real, delta_predict, rtol=1e-06, atol=0)

Expand Down
22 changes: 11 additions & 11 deletions tests/test_model_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,28 @@ def test_model_parameter_name_order():
def test_model_parameter_names():
for key in model.models_available:
md = model.models_available[key]
for key, nn in zip(md.parameter_keys, md.parameter_names):
if key in NAME_MAPPING:
assert nn in NAME_MAPPING[key], "bad {} in {}".format(key, md)
for key2, nn in zip(md.parameter_keys, md.parameter_names):
if key2 in NAME_MAPPING:
assert nn in NAME_MAPPING[key2], "bad {} in {}".format(key2, md)
else:
msg = "Parameter {} not registered for test!".format(key)
msg = "Parameter {} not registered for test!".format(key2)
assert False, msg


def test_model_parameter_units():
for key in model.models_available:
md = model.models_available[key]
for key, un in zip(md.parameter_keys, md.parameter_units):
if key in UNIT_MAPPING:
assert UNIT_MAPPING[key] == un, "bad {} in {}".format(key, md)
for key2, un in zip(md.parameter_keys, md.parameter_units):
if key2 in UNIT_MAPPING:
assert UNIT_MAPPING[key2] == un, "bad {} in {}".format(key2, md)
else:
msg = "Parameter {} not registered for test!".format(key)
msg = "Parameter {} not registered for test!".format(key2)
assert False, msg


if __name__ == "__main__":
# Run all tests
loc = locals()
for key in list(loc.keys()):
if key.startswith("test_") and hasattr(loc[key], "__call__"):
loc[key]()
for _key in list(loc.keys()):
if _key.startswith("test_") and hasattr(loc[_key], "__call__"):
loc[_key]()
6 changes: 3 additions & 3 deletions tests/test_rate_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,6 @@ def test_get_feature_names_indices():
if __name__ == "__main__":
# Run all tests
loc = locals()
for key in list(loc.keys()):
if key.startswith("test_") and hasattr(loc[key], "__call__"):
loc[key]()
for _key in list(loc.keys()):
if _key.startswith("test_") and hasattr(loc[_key], "__call__"):
loc[_key]()
34 changes: 18 additions & 16 deletions tests/test_rate_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ def setuph5(ret_idnt=False, path=jpkfile):
user_name="hans",
user_comment="this is a comment",
h5mode="a")
else:
idnt = grp[0]

if ret_idnt:
return tdir, h5path, idnt
Expand Down Expand Up @@ -116,12 +118,12 @@ def test_rate_manager_get_ts():
path = datadir / "map-data-reference-points.jpk-force-map"
tdir, h5path = setuph5(path=path)
rmg = RateManager(h5path)
X2, _ = rmg.get_training_set(which_type="binary")
assert np.all(X2 == 1)
X3, _ = rmg.get_training_set(which_type="continuous",
x2, _ = rmg.get_training_set(which_type="binary")
assert np.all(x2 == 1)
x3, _ = rmg.get_training_set(which_type="continuous",
prefilter_binary=True)
X4, _ = rmg.get_training_set(remove_nans=True)
assert np.all(np.hstack((X2, X3)) == X4)
x4, _ = rmg.get_training_set(remove_nans=True)
assert np.all(np.hstack((x2, x3)) == x4)
shutil.rmtree(tdir, ignore_errors=True)


Expand All @@ -130,25 +132,25 @@ def test_rate_manager_get_ts_bad():
path = datadir / "bad_map-data-2013.05.27-13.50.21.jpk-force-map"
tdir, h5path = setuph5(path=path)
rmg = RateManager(h5path)
X2, _ = rmg.get_training_set(which_type="binary")
assert np.allclose(X2.flatten(), [1, 0, 1])
X3, _ = rmg.get_training_set(which_type="continuous",
x2, _ = rmg.get_training_set(which_type="binary")
assert np.allclose(x2.flatten(), [1, 0, 1])
x3, _ = rmg.get_training_set(which_type="continuous",
prefilter_binary=True)
assert X3.size == 0
X4, _ = rmg.get_training_set(remove_nans=True)
assert X4.size == 0
assert x3.size == 0
x4, _ = rmg.get_training_set(remove_nans=True)
assert x4.size == 0
shutil.rmtree(tdir, ignore_errors=True)


def test_rate_manager_get_ts_single():
tdir, h5path = setuph5()
rmg = RateManager(h5path)
X2, _ = rmg.get_training_set(which_type="binary")
assert np.all(X2 == 1)
X3, _ = rmg.get_training_set(which_type="continuous",
x2, _ = rmg.get_training_set(which_type="binary")
assert np.all(x2 == 1)
x3, _ = rmg.get_training_set(which_type="continuous",
prefilter_binary=True)
X4, _ = rmg.get_training_set(remove_nans=True)
assert np.all(np.hstack((X2, X3)) == X4)
x4, _ = rmg.get_training_set(remove_nans=True)
assert np.all(np.hstack((x2, x3)) == x4)
shutil.rmtree(tdir, ignore_errors=True)


Expand Down

0 comments on commit 538ca1d

Please sign in to comment.