Skip to content

Commit

Permalink
tests: increase coverage; regressors: set default kws
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmueller committed Nov 5, 2018
1 parent 22415fa commit b7bfac2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 19 deletions.
9 changes: 4 additions & 5 deletions nanite/qmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,11 @@ def get_coords(self, which="px"):
ky = "position y [µm]"
coords = []
for idnt in self.ds:
if kx in idnt.metadata and ky in idnt.metadata:
cc = [idnt.metadata[kx], idnt.metadata[ky]]
else:
cc = [np.nan, np.nan]
# We assume that kx and ky are given. This has to be
# ensured by the file format reader for qmaps.
cc = [idnt.metadata[kx], idnt.metadata[ky]]
coords.append(cc)
return coords
return np.array(coords)

def get_qmap(self, feature, qmap_only=False):
"""Return the quantitative map for a feature
Expand Down
21 changes: 16 additions & 5 deletions nanite/rate/regressors.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,35 @@ def predict(self, *args, **kwargs):
"AdaBoost": [
ensemble.AdaBoostRegressor,
{"learning_rate": .5,
"n_estimators": 30},
"n_estimators": 30,
"random_state": 42,
},
],
"Decision Tree": [
tree.DecisionTreeRegressor,
{"max_depth": 6,
"min_samples_leaf": 4,
"min_samples_split": 4,
"random_state": 42}
"random_state": 42,
}
],
"Extra Trees": [
ensemble.ExtraTreesRegressor,
{"max_depth": 15,
"min_samples_leaf": 2,
"min_samples_split": 5,
"random_state": 42}
"n_estimators": 10,
"random_state": 42,
}
],
"Gradient Tree Boosting": [
ensemble.GradientBoostingRegressor,
{"max_depth": 5,
"min_samples_leaf": 4,
"min_samples_split": 7,
"random_state": 42}
"n_estimators": 100,
"random_state": 42,
}
],
"MERGE": [
AverageTreeRegressor,
Expand All @@ -59,19 +66,23 @@ def predict(self, *args, **kwargs):
{"max_depth": 15,
"min_samples_leaf": 2,
"min_samples_split": 7,
"random_state": 42}
"n_estimators": 10,
"random_state": 42,
}
],
"SVR (linear kernel)": [
svm.LinearSVR,
{"C": 2,
"epsilon": 1.0,
"random_state": 42,
},
],
"SVR (RBF kernel)": [
svm.SVR,
{"kernel": "rbf",
"C": 25,
"epsilon": 0.7,
"random_state": 42,
},
],
}
Expand Down
66 changes: 57 additions & 9 deletions tests/test_qmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,14 @@

import numpy as np

from nanite import model, qmap
from nanite import model, qmap, IndentationDataSet


datadir = pathlib.Path(__file__).resolve().parent / "data"
jpkfile = datadir / "map2x2_extracted.jpk-force-map"
jpkfile2 = datadir / "map-data-reference-points.jpk-force-map"


def test_metadata():
qm = qmap.QMap(jpkfile)
assert np.allclose(qm.extent,
[1.97265625, 601.97265625,
-783.53515625, -183.53515625000006])
assert qm.shape == (10, 10)


def test_feat_scan_order():
qm = qmap.QMap(jpkfile)
order = qm.get_qmap("meta scan order", qmap_only=True)
Expand Down Expand Up @@ -111,6 +103,62 @@ def test_feat_rating():
assert np.allclose(vals[1], 1.7713407492968665), "background"


def test_feat_rating_nofit():
qm = qmap.QMap(jpkfile)
with warnings.catch_warnings(record=True) as w:
# No data availabale, because there is no fit
qd = qm.get_qmap("meta rating", qmap_only=True)
assert len(w) == 4
assert w[0].category is qmap.DataMissingWarning
assert np.alltrue(np.isnan(qd))


def test_get_coords():
qm = qmap.QMap(jpkfile)

px = qm.get_coords(which="px")
refpx = np.array([[0, 0], [9, 0], [9, 9], [0, 9]])
assert np.all(px == refpx)

um = qm.get_coords(which="um")
refum = np.array([[31.972656250000004, -753.5351562500001],
[571.8359375000001, -753.90625],
[571.8359375000001, -213.73046875000003],
[31.855468750000004, -213.73046875000003]])
assert np.all(um == refum)


def test_get_coords_bad():
qm = qmap.QMap(jpkfile)
try:
qm.get_coords(which="mm")
except ValueError:
pass
else:
assert False, "Units [mm] should not be supported."


def test_get_qmap():
qm = qmap.QMap(jpkfile)
x, y, _ = qm.get_qmap(feature="data min height", qmap_only=False)
assert x.size == 10
assert y.size == 10


def test_init_with_dataset():
ds = IndentationDataSet(jpkfile)
qm = qmap.QMap(ds)
assert qm.shape == (10, 10)


def test_metadata():
qm = qmap.QMap(jpkfile)
assert np.allclose(qm.extent,
[1.97265625, 601.97265625,
-783.53515625, -183.53515625000006])
assert qm.shape == (10, 10)


if __name__ == "__main__":
# Run all tests
loc = locals()
Expand Down

0 comments on commit b7bfac2

Please sign in to comment.