Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/modsim/test_modsim.py
Views: 531
import unittest1from modsim import *23import os45import pint6from pint.errors import UnitStrippedWarning78import warnings910warnings.simplefilter("error", Warning)111213class TestModSimSeries(unittest.TestCase):14def test_constructor(self):15s = ModSimSeries([1, 2, 3])16self.assertEqual(s[0], 1)1718q = Quantity(2, UNITS.meter)19s[q] = 420self.assertEqual(s[q], 4)21self.assertEqual(s[2], 4)222324class TestModSimDataFrame(unittest.TestCase):25def test_constructor(self):26msf = ModSimDataFrame(columns=["A", "T", "dt"])27msf.row[1000] = [1, 2, np.nan]28msf.row["label"] = ["4", 5, 6.0]2930col = msf.A31self.assertIsInstance(col, ModSimSeries)32self.assertEqual(col[1000], 1)3334col = msf.T35self.assertIsInstance(col, ModSimSeries)36self.assertEqual(col[1000], 2)3738col = msf.dt39self.assertIsInstance(col, ModSimSeries)40self.assertEqual(col["label"], 6.0)4142row = msf.row[1000]43self.assertIsInstance(row, ModSimSeries)4445self.assertEqual(row.A, 1)46self.assertEqual(row.T, 2)47self.assertTrue(np.isnan(row.dt))4849self.assertEqual(row["A"], 1)50self.assertEqual(row["T"], 2)51self.assertTrue(np.isnan(row["dt"]))525354class TestTimeFrame(unittest.TestCase):55def test_constructor(self):56msf = TimeFrame(columns=["A", "T", "dt"])57msf.row[1000] = [1, 2, np.nan]58msf.row["label"] = ["4", 5, 6.0]5960col = msf.A61self.assertIsInstance(col, TimeSeries)6263col = msf.T64self.assertIsInstance(col, TimeSeries)6566col = msf.dt67self.assertIsInstance(col, TimeSeries)6869row = msf.row[1000]70self.assertIsInstance(row, State)7172row = msf.row["label"]73self.assertIsInstance(row, State)747576class TestSweepFrame(unittest.TestCase):77def test_constructor(self):78msf = SweepFrame(columns=["A", "T", "dt"])79msf.row[1000] = [1, 2, np.nan]80msf.row["label"] = ["4", 5, 6.0]8182col = msf.A83self.assertIsInstance(col, SweepSeries)8485col = msf.T86self.assertIsInstance(col, SweepSeries)8788col = msf.dt89self.assertIsInstance(col, SweepSeries)9091row = msf.row[1000]92self.assertIsInstance(row, SweepSeries)9394row = msf.row["label"]95self.assertIsInstance(row, SweepSeries)969798class TestCartPol(unittest.TestCase):99def test_cart2pol(self):100theta, r = cart2pol(3, 4)101self.assertAlmostEqual(r, 5)102self.assertAlmostEqual(theta, 0.9272952180016122)103104theta, r, z = cart2pol(2, 2, 2)105self.assertAlmostEqual(r, 2 * np.sqrt(2))106self.assertAlmostEqual(theta, np.pi / 4)107self.assertAlmostEqual(z, 2)108109def test_pol2cart(self):110theta = 0.9272952180016122111r = 5112x, y = pol2cart(theta, r)113self.assertAlmostEqual(x, 3)114self.assertAlmostEqual(y, 4)115116angle = 45 * UNITS.degree117r = 2 * np.sqrt(2)118z = 2119x, y, z = pol2cart(angle, r, z)120self.assertAlmostEqual(x, 2)121self.assertAlmostEqual(y, 2)122self.assertAlmostEqual(z, 2)123124125class TestLinspaceLinRange(unittest.TestCase):126def test_linspace(self):127warnings.simplefilter("error", Warning)128array = linspace(0, 1, 11)129self.assertEqual(len(array), 11)130self.assertAlmostEqual(array[0], 0)131self.assertAlmostEqual(array[1], 0.1)132self.assertAlmostEqual(array[10], 1.0)133134array = linspace(0, 1, 10, endpoint=False)135self.assertEqual(len(array), 10)136self.assertAlmostEqual(array[0], 0)137self.assertAlmostEqual(array[1], 0.1)138self.assertAlmostEqual(array[9], 0.9)139140def test_linrange(self):141array = linrange(0, 1, 0.1)142self.assertEqual(len(array), 10)143self.assertAlmostEqual(array[0], 0)144self.assertAlmostEqual(array[1], 0.1)145self.assertAlmostEqual(array[9], 0.9)146147array = linrange(0, 1, 0.1, endpoint=True)148self.assertEqual(len(array), 11)149self.assertAlmostEqual(array[0], 0)150self.assertAlmostEqual(array[1], 0.1)151self.assertAlmostEqual(array[10], 1.0)152153154class TestAbsRelDiff(unittest.TestCase):155def test_abs_diff(self):156abs_diff = compute_abs_diff([1, 3, 7.5])157self.assertEqual(len(abs_diff), 3)158self.assertAlmostEqual(abs_diff[1], 4.5)159160ts = linrange(1950, 1960, endpoint=True)161ps = linspace(3, 4, len(ts))162abs_diff = compute_abs_diff(ps)163self.assertEqual(len(abs_diff), 11)164self.assertAlmostEqual(abs_diff[1], 0.1)165166# TODO: bring back this test when np.ediff1 is fixed167# self.assertTrue(np.isnan(abs_diff[-1]))168169series = TimeSeries(ps, index=ts)170abs_diff = compute_abs_diff(series)171self.assertEqual(len(abs_diff), 11)172self.assertAlmostEqual(abs_diff[1950], 0.1)173# self.assertTrue(np.isnan(abs_diff[1960]))174175def test_rel_diff(self):176rel_diff = compute_rel_diff([1, 3, 7.5])177self.assertEqual(len(rel_diff), 3)178self.assertAlmostEqual(rel_diff[1], 1.5)179180ts = linrange(1950, 1960, endpoint=True)181ps = linspace(3, 4, len(ts))182rel_diff = compute_rel_diff(ps)183self.assertEqual(len(rel_diff), 11)184self.assertAlmostEqual(rel_diff[0], 0.0333333333)185# self.assertTrue(np.isnan(rel_diff[-1]))186187series = TimeSeries(ps, index=ts)188rel_diff = compute_rel_diff(series)189self.assertEqual(len(rel_diff), 11)190self.assertAlmostEqual(rel_diff[1950], 0.0333333333)191# self.assertTrue(np.isnan(rel_diff[1960]))192193194class TestOdeSolvers(unittest.TestCase):195def test_run_euler(self):196s = UNITS.second197m = UNITS.meter198199init = State(y=2 * m)200system = System(init=init, t_0=1 * s, t_end=3 * s)201202def slope_func(state, t, system):203[y] = state204dydt = y / s + t * m / s ** 2205return [dydt]206207results, details = run_euler(system, slope_func)208y_end = get_last_value(results.y)209self.assertAlmostEqual(y_end, 24.9737147 * m)210211def test_run_ralston(self):212s = UNITS.second213m = UNITS.meter214215init = State(y=2 * m)216system = System(init=init, t_0=1 * s, t_end=3 * s)217218def slope_func(state, t, system):219[y] = state220dydt = y / s + t * m / s ** 2221return [dydt]222223results, details = run_ralston(system, slope_func)224y_end = get_last_value(results.y)225self.assertAlmostEqual(y_end, 25.8344700133 * m)226227def test_run_solve_ivp(self):228s = UNITS.second229m = UNITS.meter230231init = State(y=2 * m)232system = System(init=init, t_0=1 * s, t_end=3 * s)233234def slope_func(state, t, system):235[y] = state236dydt = y + t237return [dydt]238239results, details = run_solve_ivp(system, slope_func)240y_end = get_last_value(results.y)241self.assertAlmostEqual(y_end, 25.5571533)242243244class TestRootFinders(unittest.TestCase):245def test_root_scalar(self):246def func(x):247return (x - 1) * (x - 2) * (x - 3)248249res = root_scalar(func, [0, 1.9])250self.assertAlmostEqual(res.root, 1.0)251252def test_root_secant(self):253def func(x):254return (x - 1) * (x - 2) * (x - 3)255256res = root_bisect(func, [0, 1.9])257self.assertAlmostEqual(res.root, 1.0)258259res = root_bisect(func, [0, 0.5])260self.assertFalse(res.converged)261262263class TestRunInterpolate(unittest.TestCase):264def test_has_nan(self):265a = [1, 2, 3]266self.assertFalse(has_nan(a))267self.assertFalse(has_nan(np.array(a)))268self.assertFalse(has_nan(pd.Series(a)))269a.append(np.nan)270self.assertTrue(has_nan(a))271self.assertTrue(has_nan(np.array(a)))272self.assertTrue(has_nan(pd.Series(a)))273274def test_is_strictly_increasing(self):275a = [1, 2, 3]276self.assertTrue(is_strictly_increasing(a))277self.assertTrue(is_strictly_increasing(np.array(a)))278self.assertTrue(is_strictly_increasing(pd.Series(a)))279a.append(3)280self.assertFalse(is_strictly_increasing(a))281self.assertFalse(is_strictly_increasing(np.array(a)))282self.assertFalse(is_strictly_increasing(pd.Series(a)))283284def test_interpolate(self):285index = [1, 2, 3]286values = np.array(index) * 2 - 1287series = pd.Series(values, index=index)288i = interpolate(series)289self.assertAlmostEqual(i(1.5), 2.0)290291def test_interpolate_with_units(self):292index = [1, 2, 3]293values = np.array(index) * 2 - 1294series = pd.Series(values, index=index) * UNITS.meter295i = interpolate(series)296self.assertAlmostEqual(i(1.5), 2.0 * UNITS.meter)297298299class TestGradient(unittest.TestCase):300def test_gradient(self):301a = [1, 2, 4]302s = TimeSeries(a)303r = gradient(s)304self.assertTrue(isinstance(r, TimeSeries))305self.assertAlmostEqual(r[1], 1.5)306307def test_gradient_with_units(self):308s = SweepSeries()309s[0] = 1 * UNITS.meter310s[1] = 2 * UNITS.meter311s[2] = 4 * UNITS.meter312r = gradient(s)313self.assertTrue(isinstance(r, SweepSeries))314self.assertAlmostEqual(r[1], 1.5 * UNITS.meter)315316317class TestGolden(unittest.TestCase):318def test_minimize(self):319def min_func(x, system):320return (x - system.actual_min) ** 2321322system = System(actual_min=2)323res = minimize_golden(min_func, [0, 5], system, rtol=1e-7)324self.assertAlmostEqual(res.x, 2)325self.assertAlmostEqual(res.fun, 0)326327def test_maximize(self):328def max_func(x, system):329return -(x - system.actual_min) ** 2330331system = System(actual_min=2)332res = maximize_golden(max_func, [0, 5], system, rtol=1e-7)333self.assertAlmostEqual(res.x, 2)334self.assertAlmostEqual(res.fun, 0)335336337class TestVector(unittest.TestCase):338def assertArrayEqual(self, res, ans):339self.assertTrue(isinstance(res, np.ndarray))340self.assertTrue((res == ans).all())341342def assertVectorEqual(self, res, ans):343self.assertTrue(isinstance(res, ModSimVector))344self.assertTrue((res == ans).all())345346def assertVectorAlmostEqual(self, res, ans):347[self.assertQuantityAlmostEqual(x, y) for x, y in zip(res, ans)]348349def assertQuantityAlmostEqual(self, x, y):350self.assertEqual(get_units(x), get_units(y))351self.assertAlmostEqual(magnitude(x), magnitude(y))352353def test_vector_mag(self):354warnings.simplefilter("error", Warning)355m = UNITS.meter356357v = [3, 4]358self.assertEqual(vector_mag(v), 5)359v = Vector(3, 4)360self.assertEqual(vector_mag(v), 5)361v = Vector(3, 4) * m362self.assertEqual(vector_mag(v), 5 * m)363self.assertEqual(v.mag, 5 * m)364365def test_vector_mag2(self):366warnings.simplefilter("error", Warning)367m = UNITS.meter368369v = [3, 4]370self.assertEqual(vector_mag2(v), 25)371v = Vector(3, 4)372self.assertEqual(vector_mag2(v), 25)373v = Vector(3, 4) * m374self.assertEqual(vector_mag2(v), 25 * m * m)375376def test_vector_angle(self):377warnings.simplefilter("error", Warning)378m = UNITS.meter379ans = 0.927295218380v = [3, 4]381self.assertAlmostEqual(vector_angle(v), ans)382v = Vector(3, 4)383self.assertAlmostEqual(vector_angle(v), ans)384v = Vector(3, 4) * m385self.assertAlmostEqual(vector_angle(v), ans)386387def test_vector_hat(self):388warnings.simplefilter("error", Warning)389m = UNITS.meter390v = [3, 4]391ans = [0.6, 0.8]392self.assertArrayEqual(vector_hat(v), ans)393394v = Vector(3, 4)395self.assertVectorEqual(vector_hat(v), ans)396v = Vector(3, 4) * m397self.assertVectorEqual(vector_hat(v), ans)398399v = [0, 0]400ans = [0, 0]401self.assertArrayEqual(vector_hat(v), ans)402v = Vector(0, 0)403self.assertVectorEqual(vector_hat(v), ans)404v = Vector(0, 0) * m405self.assertVectorEqual(vector_hat(v), ans)406407def test_vector_perp(self):408warnings.simplefilter("error", Warning)409m = UNITS.meter410v = [3, 4]411ans = [-4, 3]412self.assertTrue((vector_perp(v) == ans).all())413v = Vector(3, 4)414self.assertTrue((vector_perp(v) == ans).all())415v = Vector(3, 4) * m416self.assertTrue((vector_perp(v) == ans * m).all())417418def test_vector_dot(self):419warnings.simplefilter("error", Warning)420m = UNITS.meter421s = UNITS.second422v = [3, 4]423w = [5, 6]424ans = 39425self.assertAlmostEqual(vector_dot(v, w), ans)426v = Vector(3, 4)427self.assertAlmostEqual(vector_dot(v, w), ans)428self.assertAlmostEqual(vector_dot(w, v), ans)429430v = Vector(3, 4) * m431self.assertAlmostEqual(vector_dot(v, w), ans * m)432self.assertAlmostEqual(vector_dot(w, v), ans * m)433434w = Vector(5, 6) / s435self.assertAlmostEqual(vector_dot(v, w), ans * m / s)436self.assertAlmostEqual(vector_dot(w, v), ans * m / s)437438def test_vector_cross_2D(self):439warnings.simplefilter("error", Warning)440m = UNITS.meter441s = UNITS.second442ans = -2443444v = [3, 4]445w = [5, 6]446self.assertAlmostEqual(vector_cross(v, w), ans)447self.assertAlmostEqual(vector_cross(w, v), -ans)448449v = Vector(3, 4)450self.assertAlmostEqual(vector_cross(v, w), ans)451self.assertAlmostEqual(vector_cross(w, v), -ans)452453v = Vector(3, 4) * m454self.assertAlmostEqual(vector_cross(v, w), ans * m)455self.assertAlmostEqual(vector_cross(w, v), -ans * m)456457w = Vector(5, 6) / s458self.assertAlmostEqual(vector_cross(v, w), ans * m / s)459self.assertAlmostEqual(vector_cross(w, v), -ans * m / s)460461def test_vector_cross_3D(self):462warnings.simplefilter("error", Warning)463m = UNITS.meter464s = UNITS.second465ans = [-2, 4, -2]466467v = [3, 4, 5]468w = [5, 6, 7]469self.assertArrayEqual(vector_cross(v, w), ans)470self.assertArrayEqual(-vector_cross(w, v), ans)471472v = Vector(3, 4, 5)473self.assertVectorEqual(vector_cross(v, w), ans)474self.assertVectorEqual(-vector_cross(w, v), ans)475476v = Vector(3, 4, 5) * m477self.assertVectorEqual(vector_cross(v, w), ans * m)478self.assertVectorEqual(-vector_cross(w, v), ans * m)479480w = Vector(5, 6, 7) / s481self.assertVectorEqual(vector_cross(v, w), ans * m / s)482self.assertVectorEqual(-vector_cross(w, v), ans * m / s)483484def test_scalar_proj(self):485warnings.simplefilter("error", Warning)486m = UNITS.meter487s = UNITS.second488ans = 4.9934383489ans2 = 7.8490491v = [3, 4]492w = [5, 6]493self.assertAlmostEqual(scalar_proj(v, w), ans)494self.assertAlmostEqual(scalar_proj(w, v), ans2)495496v = Vector(3, 4)497self.assertAlmostEqual(scalar_proj(v, w), ans)498self.assertAlmostEqual(scalar_proj(w, v), ans2)499500v = Vector(3, 4) * m501self.assertQuantityAlmostEqual(scalar_proj(v, w), ans * m)502self.assertAlmostEqual(scalar_proj(w, v), ans2)503504w = Vector(5, 6) / s505self.assertQuantityAlmostEqual(scalar_proj(v, w), ans * m)506self.assertQuantityAlmostEqual(scalar_proj(w, v), ans2 / s)507508def test_vector_proj(self):509warnings.simplefilter("error", Warning)510m = UNITS.meter511s = UNITS.second512ans = [3.19672131, 3.83606557]513ans2 = Quantity([4.68, 6.24])514515v = [3, 4]516w = [5, 6]517self.assertVectorAlmostEqual(vector_proj(v, w), ans)518self.assertVectorAlmostEqual(vector_proj(w, v), ans2)519520v = Vector(3, 4)521self.assertVectorAlmostEqual(vector_proj(v, w), ans)522self.assertVectorAlmostEqual(vector_proj(w, v), ans2)523524v = Vector(3, 4) * m525self.assertVectorAlmostEqual(vector_proj(v, w), ans * m)526self.assertVectorAlmostEqual(vector_proj(w, v), ans2)527528w = Vector(5, 6) / s529self.assertVectorAlmostEqual(vector_proj(v, w), ans * m)530self.assertVectorAlmostEqual(vector_proj(w, v), ans2 / s)531532def test_vector_dist(self):533warnings.simplefilter("error", Warning)534m = UNITS.meter535v = [3, 4]536w = [6, 8]537ans = 5538self.assertAlmostEqual(vector_dist(v, w), ans)539self.assertAlmostEqual(vector_dist(w, v), ans)540541v = Vector(3, 4)542self.assertAlmostEqual(vector_dist(v, w), ans)543self.assertAlmostEqual(vector_dist(w, v), ans)544545v = Vector(3, 4) * m546w = Vector(6, 8) * m547self.assertAlmostEqual(vector_dist(v, w), ans * m)548self.assertAlmostEqual(vector_dist(w, v), ans * m)549550def test_vector_diff_angle(self):551warnings.simplefilter("error", Warning)552m = UNITS.meter553v = [3, 4]554w = [5, 6]555ans = 0.0512371674556self.assertAlmostEqual(vector_diff_angle(v, w), ans)557self.assertAlmostEqual(vector_diff_angle(w, v), -ans)558559v = Vector(3, 4)560self.assertAlmostEqual(vector_diff_angle(v, w), ans)561self.assertAlmostEqual(vector_diff_angle(w, v), -ans)562563v = Vector(3, 4) * m564w = Vector(5, 6) * m565self.assertAlmostEqual(vector_diff_angle(v, w), ans)566self.assertAlmostEqual(vector_diff_angle(w, v), -ans)567568569class TestSeriesCopy(unittest.TestCase):570def test_series_copy(self):571series = TimeSeries()572res = series.copy()573self.assertTrue(isinstance(res, TimeSeries))574575576class TestMagnitudeUnits(unittest.TestCase):577def test_magnitudes(self):578# scalar579x = 5580res = magnitudes(x)581self.assertEqual(res, 5)582res = magnitudes(x * UNITS.meter)583self.assertEqual(res, 5)584585# list (result is NumPy array)586t = [1, 2, 3]587res = magnitudes(t)588self.assertEqual(res, [1, 2, 3])589res = magnitudes(t * UNITS.meter)590self.assertTrue((res == [1, 2, 3]).all())591592# Series (result is list)593s = ModSimSeries([1, 2, 3])594res = magnitudes(s)595self.assertTrue((res == [1, 2, 3]).all())596res = magnitudes(s * UNITS.meter)597self.assertTrue((res == [1, 2, 3]).all())598599# Quantity containing Series(result is Series)600res = magnitudes(UNITS.meter * s)601self.assertTrue((res == [1, 2, 3]).all())602603def test_units(self):604# scalar605x = 5606res = get_units(x)607self.assertEqual(res, 1)608res = get_units(x * UNITS.meter)609self.assertEqual(res, UNITS.meter)610611# list (result is list)612t = [1, 2, 3]613res = get_units(t)614self.assertEqual(res, [1, 1, 1])615res = get_units(t * UNITS.meter)616self.assertEqual(res, UNITS.meter)617618# Series (result Series)619s = ModSimSeries([1, 2, 3])620res = get_units(s)621self.assertTrue((res == [1, 1, 1]).all())622623# Series containing Quantities (result is a Series)624res = get_units(s * UNITS.meter)625self.assertTrue((res == [UNITS.meter] * 3).all())626627# Quantity containing Series(result is a single Unit object)628res = get_units(UNITS.meter * s)629self.assertEqual(res, UNITS.meter)630631632class TestPlot(unittest.TestCase):633def test_plot(self):634os.environ["QT_XKB_CONFIG_ROOT"] = "/usr/share/X11/xkb"635636t = [1, 2, 3]637plot(t)638639t = [1, 2, 3] * UNITS.meter640plot(t)641642x = [4, 5, 6]643plot(x, t)644645x = [4, 5, 6] * UNITS.second646plot(x, t)647648a = np.array(t)649plot(a)650651x = np.array(x)652plot(x, a)653654s = TimeSeries(t)655plot(s)656657s = TimeSeries(t, x)658plot(s)659660s = TimeSeries(a, x)661plot(s)662663664if __name__ == "__main__":665unittest.main()666667668