public release 4.2.0 - see README.md and CHANGES.md for details
This commit is contained in:
71
tests/reports/test_results.py
Normal file
71
tests/reports/test_results.py
Normal file
@@ -0,0 +1,71 @@
|
||||
import datetime
|
||||
import numpy as np
|
||||
from pathlib import Path
|
||||
import unittest
|
||||
import pmsco.database.access as db_access
|
||||
import pmsco.database.orm as db_orm
|
||||
import pmsco.reports.results as rp_results
|
||||
import pmsco.dispatch as dispatch
|
||||
|
||||
|
||||
def setup_sample_database(session):
|
||||
p1 = db_orm.Project(name="oldproject", code="oldcode")
|
||||
p2 = db_orm.Project(name="unittest", code="testcode")
|
||||
j1 = db_orm.Job(project=p1, name="oldjob", mode="oldmode", machine="oldhost", datetime=datetime.datetime.now())
|
||||
j2 = db_orm.Job(project=p2, name="testjob", mode="testmode", machine="testhost", datetime=datetime.datetime.now())
|
||||
pk1 = db_orm.Param(key='parA')
|
||||
pk2 = db_orm.Param(key='parB')
|
||||
pk3 = db_orm.Param(key='parC')
|
||||
m1 = db_orm.Model(job=j1, model=91)
|
||||
m2 = db_orm.Model(job=j2, model=92)
|
||||
r1 = db_orm.Result(calc_id=dispatch.CalcID(91, -1, -1, -1, -1), rfac=0.534, secs=37.9)
|
||||
r1.model = m1
|
||||
pv1 = db_orm.ParamValue(model=m1, param=pk1, value=1.234, delta=0.1234)
|
||||
pv2 = db_orm.ParamValue(model=m1, param=pk2, value=5.678, delta=-0.5678)
|
||||
pv3 = db_orm.ParamValue(model=m2, param=pk3, value=6.785, delta=0.6785)
|
||||
objects = {'p1': p1, 'p2': p2, 'j1': j1, 'j2': j2, 'm1': m1, 'm2': m2, 'r1': r1,
|
||||
'pv1': pv1, 'pv2': pv2, 'pv3': pv3, 'pk1': pk1, 'pk2': pk2, 'pk3': pk3}
|
||||
session.add_all(objects.values())
|
||||
session.commit()
|
||||
return objects
|
||||
|
||||
|
||||
class TestResultsMethods(unittest.TestCase):
|
||||
def test_array_range(self):
|
||||
dtype = [('A', 'f8'), ('B', 'f8'), ('C', 'f8')]
|
||||
data = np.array([(1.5, 3.5, 3.5),
|
||||
(1.6, 2.6, 2.6),
|
||||
(1.7, 2.7, 3.7),
|
||||
(1.8, 2.8, 3.8)], dtype=dtype)
|
||||
exp_rmin = {'A': 1.5, 'B': 2.6, 'C': 2.6}
|
||||
exp_rmax = {'A': 1.8, 'B': 3.5, 'C': 3.8}
|
||||
rmin, rmax = rp_results.array_range(data)
|
||||
self.assertEqual(exp_rmin, rmin)
|
||||
self.assertEqual(exp_rmax, rmax)
|
||||
|
||||
|
||||
class TestResultData(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.db = db_access.DatabaseAccess()
|
||||
self.db.connect(":memory:")
|
||||
|
||||
def test_update_collections(self):
|
||||
data_dir = Path(__file__).parent.parent
|
||||
data_file = data_dir / "test_swarm.setup_with_results.1.dat"
|
||||
raw_values = np.atleast_1d(np.genfromtxt(data_file, names=True))
|
||||
|
||||
rd = rp_results.ResultData()
|
||||
rd.values = raw_values
|
||||
rd.update_collections()
|
||||
np.testing.assert_array_equal(rd.generations, np.array((1, 2, 3, 4)))
|
||||
|
||||
def test_load_from_db(self):
|
||||
with self.db.session() as session:
|
||||
setup_sample_database(session)
|
||||
rd = rp_results.ResultData()
|
||||
rd.levels = {'scan': -1}
|
||||
rd.load_from_db(session)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
69
tests/reports/test_rfactor.py
Normal file
69
tests/reports/test_rfactor.py
Normal file
@@ -0,0 +1,69 @@
|
||||
import numpy as np
|
||||
import unittest
|
||||
import pmsco.reports.rfactor as rp_rfactor
|
||||
|
||||
|
||||
class TestGridMethods(unittest.TestCase):
|
||||
def test_triplet_to_grid__basic(self):
|
||||
x = np.array([-1, 0, 1, 1, 0, -1])
|
||||
y = np.array([2, 2, 2, 3, 3, 3])
|
||||
z = np.array([0.1, 0.2, 0.3, 0.6, 0.5, 0.4])
|
||||
gx, gy, gz = rp_rfactor.triplet_to_grid(x, y, z)
|
||||
expected_gx = np.array([[-1, 0, 1], [-1, 0, 1]]).T
|
||||
expected_gy = np.array([[2, 2, 2], [3, 3, 3]]).T
|
||||
expected_gz = np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]).T
|
||||
np.testing.assert_array_almost_equal(gx, expected_gx, 1, "grid_x")
|
||||
np.testing.assert_array_almost_equal(gy, expected_gy, 1, "grid_y")
|
||||
np.testing.assert_array_almost_equal(gz, expected_gz, 2, "grid_z")
|
||||
|
||||
def test_triplet_to_grid__imprecise(self):
|
||||
x = np.array([-0.99, 0, 1, 1.001, 0, -1])
|
||||
y = np.array([1.999, 2.00001, 2, 3.01, 2.98, 3])
|
||||
z = np.array([0.1, 0.2, 0.3, 0.6, 0.5, 0.4])
|
||||
gx, gy, gz = rp_rfactor.triplet_to_grid(x, y, z)
|
||||
expected_gx = np.array([[-1, 0, 1], [-1, 0, 1]]).T
|
||||
expected_gy = np.array([[2, 2, 2], [3, 3, 3]]).T
|
||||
expected_gz = np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]).T
|
||||
np.testing.assert_array_almost_equal(gx, expected_gx, 1, "grid_x")
|
||||
np.testing.assert_array_almost_equal(gy, expected_gy, 1, "grid_y")
|
||||
np.testing.assert_array_almost_equal(gz, expected_gz, 2, "grid_z")
|
||||
|
||||
def test_triplet_to_grid__missing(self):
|
||||
x = np.array([-1, 0, 1, 0, -1])
|
||||
y = np.array([2, 2, 3, 3, 3])
|
||||
z = np.array([0.1, 0.2, 0.6, 0.5, 0.4])
|
||||
gx, gy, gz = rp_rfactor.triplet_to_grid(x, y, z)
|
||||
expected_gx = np.array([[-1, 0, 1], [-1, 0, 1]]).T
|
||||
expected_gy = np.array([[2, 2, 2], [3, 3, 3]]).T
|
||||
expected_gz = np.array([[0.1, 0.2, 0.2], [0.4, 0.5, 0.6]]).T
|
||||
np.testing.assert_array_almost_equal(gx, expected_gx, 1, "grid_x")
|
||||
np.testing.assert_array_almost_equal(gy, expected_gy, 1, "grid_y")
|
||||
np.testing.assert_array_almost_equal(gz, expected_gz, 2, "grid_z")
|
||||
|
||||
def test_triplet_to_grid__extra(self):
|
||||
x = np.array([-1, 0, 1, 1, 1, 0, -1])
|
||||
y = np.array([2, 2, 2, 2.01, 3, 3, 3])
|
||||
z = np.array([0.1, 0.2, 0.3, 0.35, 0.6, 0.5, 0.4])
|
||||
gx, gy, gz = rp_rfactor.triplet_to_grid(x, y, z)
|
||||
expected_gx = np.array([[-1, 0, 1], [-1, 0, 1]]).T
|
||||
expected_gy = np.array([[2, 2, 2], [3, 3, 3]]).T
|
||||
expected_gz = np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]]).T
|
||||
np.testing.assert_array_almost_equal(gx, expected_gx, 1, "grid_x")
|
||||
np.testing.assert_array_almost_equal(gy, expected_gy, 1, "grid_y")
|
||||
np.testing.assert_array_almost_equal(gz, expected_gz, 2, "grid_z")
|
||||
|
||||
def test_triplet_to_grid__split_column(self):
|
||||
x = np.array([-1, 0, 0.5, 1, 1, 0.5, 0, -1])
|
||||
y = np.array([2, 2, 2, 2, 3, 3, 3, 3])
|
||||
z = np.array([0.1, 0.2, 0.24, 0.3, 0.6, 0.45, 0.5, 0.4])
|
||||
gx, gy, gz = rp_rfactor.triplet_to_grid(x, y, z)
|
||||
expected_gx = np.array([[-1, -0.5, 0, 0.5, 1], [-1, -0.5, 0, 0.5, 1]]).T
|
||||
expected_gy = np.array([[2, 2, 2, 2, 2], [3, 3, 3, 3, 3]]).T
|
||||
expected_gz = np.array([[0.1, 0.1, 0.2, 0.24, 0.3], [0.4, 0.5, 0.5, 0.45, 0.6]]).T
|
||||
np.testing.assert_array_almost_equal(gx, expected_gx, 1, "grid_x")
|
||||
np.testing.assert_array_almost_equal(gy, expected_gy, 1, "grid_y")
|
||||
np.testing.assert_array_almost_equal(gz, expected_gz, 2, "grid_z")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user