def test_merge_in_place(self): b1 = Box() b2 = Box() dct = get_dct() b1.add(dct, 'test', 7) b2.add(dct, 'test2', 8) b1.merge(b2) expected = [{ 'index': 0, 'independent': { 'a': 1, 'b': 2 }, 'dependent': { 'test': 7 } }, { 'index': 1, 'independent': { 'a': 1, 'b': 2 }, 'dependent': { 'test2': 8 } }] self.assertListEqual(b1, expected)
def test_vectors(self): b = Box(get_lst3()) keys = ['d', 'e'] vec0, vec1, labels = b.vectors(keys) expected_vec0 = [[12, 30], [13, 31]] expected_vec1 = [[1, 2], [1, 2]] expected_labels = ['a=1, b=1', 'a=1, b=2'] self.assertListEqual(expected_vec0, vec0) self.assertListEqual(expected_vec1, vec1) self.assertListEqual(expected_labels, labels)
def test_vectors_indep_keys(self): b = Box(get_lst4()) keys = ['c'] c, a, b = b.vectors(keys, indep_keys=['a', 'b']) expected_c = [4, 5, 6, 7] expected_a = [1, 1, 2, 2] expected_b = [1, 2, 1, 2] self.assertListEqual(expected_c, c) self.assertListEqual(expected_a, a) self.assertListEqual(expected_b, b)
def test_vectors_uncombined(self): b = Box(get_lst5()) keys = ['b', 'e'] vec0, vec1, labels = b.vectors(keys, combine=False) expected_vec0 = [[1, 2], [1, 2]] expected_vec1 = [[12, 30], [13, 31]] expected_labels = ['a=1', 'a=1'] self.assertListEqual(expected_vec0, vec0) self.assertListEqual(expected_vec1, vec1) self.assertListEqual(expected_labels, labels)
def test_vectors_nonstr(self): b = Box(get_lst3()) keys = ['d', 'e'] vec0, vec1, labels = b.vectors(keys, labels='dict') expected_vec0 = [[12, 30], [13, 31]] expected_vec1 = [[1, 2], [1, 2]] expected_labels = [{'a': 1, 'b': 1}, {'a': 1, 'b': 2}] self.assertListEqual(expected_vec0, vec0) self.assertListEqual(expected_vec1, vec1) self.assertListEqual(expected_labels, labels)
def test_add(self): b = Box() dct = get_dct() b.add(dct, 'test', 7) self.assertEqual(len(b), 1) expected = { 'index': 0, 'independent': { 'a': 1, 'b': 2 }, 'dependent': { 'test': 7 } } self.assertEqual(b[0], expected)
def test_tabulate3(self): t = Tabulator() box = Box(get_lst3()) index = ['a', 'b'] columns = ['e'] values = 'd' pt = t.tabulate(box=box, values=values, columns=columns, index=index) expected = 'e 1 2\na b \n1 1 12 30\n 2 13 31' self.assertEqual(str(pt), expected)
def test_tabulate(self): t = Tabulator() box = Box(get_lst()) index = ['a', 'b'] columns = ['c'] values = 'd' pt = t.tabulate(box=box, values=values, columns=columns, index=index) expected = 'c 1 2\na b \n1 1 11 14\n 2 13 12\n2 1 15 16\n 2 17 18' self.assertEqual(str(pt), expected)
def test_tabulate2b(self): t = Tabulator() box = Box(get_lst2()) index = ['c'] columns = ['a', 'b'] values = 'd' pt = t.tabulate(box=box, values=values, columns=columns, index=index) expected = 'a 1 2 \nb 1 2 1 2\nc \n1 12 13 16 19\n2 30 31 34 37' self.assertEqual(str(pt), expected)
def test_add_arr(self): b = Box() dct = get_dct() var = Variable('a', components=['x', 'y', 'z']) val = [[0, 1], [2, 3], [4, 5]] b.add(dct, var, val) self.assertEqual(len(b), 1) expected = { 'index': 0, 'independent': { 'a': 1, 'b': 2 }, 'dependent': { 'a': val } } self.assertEqual(b[0], expected)
def test_minimal(self): b = Box() dct = get_dct() b.add(dct, 'test', 7) b.add(dct, 'test2', 8) minimal = b.minimal() expected = [{'a': 1, 'b': 2, 'test': 7, 'test2': 8}] self.assertEqual(minimal, expected)
def test_vectors_scalar(self): b = Box(get_lst4()) keys = ['c'] c, labels = b.vectors(keys, labels='dict') expected_c = [4, 5, 6, 7] expected_labels = [{ 'a': 1, 'b': 1 }, { 'a': 1, 'b': 2 }, { 'a': 2, 'b': 1 }, { 'a': 2, 'b': 2 }] self.assertListEqual(expected_c, c) self.assertListEqual(expected_labels, labels)
def test_add_array(self): b = Box() dct = get_dct() arr = [[0, 1], [2, 3], [4, 5]] labels = ['a', 'b', 'c'] b.add_array(dct, labels, arr) self.assertEqual(len(b), 1) expected = { 'index': 0, 'independent': { 'a': 1, 'b': 2 }, 'dependent': { 'a': [0, 1], 'b': [2, 3], 'c': [4, 5] } } self.assertEqual(b[0], expected)
def test_combinations(self): b = Box(get_lst6()) combinations = b.combinations(['a', 'b']) expected = [{ 'a': 1, 'b': 1 }, { 'a': 1, 'b': 2 }, { 'a': 1, 'b': 3 }, { 'a': 2, 'b': 1 }, { 'a': 2, 'b': 2 }] self.assertListEqual(combinations, expected)
def test_serialise_vars(self): store = Store() v1 = store.add('a', 'docstring_1', 'mm') v2 = store.add('b', 'docstring_2', 'm/s') lst = [{'index': 0, 'independent': {v1: 1, v2: 2}, 'dependent': {v1: 5, v2: 7}}] b = Box(lst) res = persist.serialise_vars(b) expected = ['name=a;doc=docstring_1;unit=mm;components=None;sep= - ;category=None;tags=None;identifier=None', 'name=b;doc=docstring_2;unit=m/s;components=None;sep= - ;category=None;tags=None;identifier=None'] self.assertListEqual(res, expected)
def test_vector_table(self): t = Tabulator() box = Box(get_lst4()) values = 'd' index = 'c' index_vals = [1, 2] df = t.vector_table(box, values, index, index_vals) expected = '''a 1 2 b 1 2 1 2 c 1 12.0 13.0 16.0 19.0 2 30.0 31.0 34.0 37.0''' self.assertEqual(expected, str(df))
def test_grouped_as_dicts(self): b = Box(get_lst4()) keys = ['a', 'c'] res = b.grouped(keys, as_dicts=True) expected = [{ 'labels': { 'b': 1 }, 'values': { 'a': [1, 2], 'c': [4, 6] } }, { 'labels': { 'b': 2 }, 'values': { 'a': [1, 2], 'c': [5, 7] } }] self.assertListEqual(res, expected)
def test_tabulate_missing(self): t = Tabulator() box = Box(get_lst8()) index = ['a', 'b'] columns = ['c'] values = 'd' pt = t.tabulate(box=box, values=values, columns=columns, index=index) expected = '''c 1 2 a b 1 -999 11 12 2 1 15 16 2 17 18''' self.assertEqual(str(pt), expected)
def test_tabulate_guessed_indices(self): t = Tabulator() box = Box(get_lst()) columns = ['c'] values = 'd' pt = t.tabulate(box=box, values=values, columns=columns) expected = '''c 1 2 a b 1 1 11 14 2 13 12 2 1 15 16 2 17 18''' self.assertEqual(str(pt), expected)
def test_vector_table_cols(self): t = Tabulator() box = Box(get_lst4()) values = 'd' index = 'c' index_vals = [1, 2] df = t.vector_table(box, values, index, index_vals, orient='cols') expected = '''c 1 2 a b 1 1 12.0 30.0 2 13.0 31.0 2 1 16.0 34.0 2 19.0 37.0''' self.assertEqual(expected, str(df))
def test_vector_table_cols_default_index_uneven(self): t = Tabulator() box = Box(get_lst4_uneven()) values = 'd' index = 'c' index_vals = None df = t.vector_table(box, values, index, index_vals, orient='cols') expected = '''c 1 2 3 a b 1 1 12.0 30.0 NaN 2 13.0 31.0 20.0 2 1 16.0 34.0 NaN 2 19.0 37.0 NaN''' self.assertEqual(expected, str(df))
def test_vector_table_arr_single_entry_interpolated_2D(self): """ The index is n-by-1, and must be squeezed """ t = Tabulator() box = Box(get_lst7()) values = 'd' index = 'c' components = ['x', 'y', 'z'] df = t.vector_table(box, values, index, components=components) expected = '''a 1 b 1 d: x y z c 1 1.0 3.0 5.0 2 2.0 4.0 6.0''' self.assertEqual(expected, str(df))
def test_vector_table_arr_single_entry_interpolated(self): t = Tabulator() box = Box(get_lst6()) values = 'd' index = 'c' index_vals = [1, 2] components = ['x', 'y', 'z'] df = t.vector_table(box, values, index, components=components) expected = '''a 1 b 1 d: x y z c 1 1.0 3.0 5.0 2 2.0 4.0 6.0''' self.assertEqual(expected, str(df))
def test_make_pack(self): store = Store() v1 = store.add('a', 'docstring_1', 'mm') v2 = store.add('b', 'docstring_2', 'm/s') lst = [{'index': 0, 'independent': {v1: 1, v2: 2}, 'dependent': {v1: 5, v2: 7}}] box = Box(lst) dct = persist.make_pack(box) expected = {'data': [{'index': 0, 'independent': {'a [mm]': 1, 'b [m/s]': 2}, 'dependent': {'a [mm]': 5, 'b [m/s]': 7}}], 'vars': ['name=a;doc=docstring_1;unit=mm;components=None;sep= - ;category=None;tags=None;identifier=None', 'name=b;doc=docstring_2;unit=m/s;components=None;sep= - ;category=None;tags=None;identifier=None']} self.assertDictEqual(dct, expected)
def test_combined(self): b = Box() dct = get_dct() b.add(dct, 'test', 7) b.add(dct, 'test2', 8) combined = b.combined() expected = [{ 'independent': { 'a': 1, 'b': 2 }, 'dependent': { 'test': 7, 'test2': 8 } }] self.assertEqual(combined, expected)
def test_vector_table_arr(self): t = Tabulator() box = Box(get_lst5()) values = 'd' index = 'c' index_vals = [1, 2] components = ['x', 'y', 'z'] df = t.vector_table(box, values, index, index_vals, components=components) expected = '''a 1 b 1 2 d: x y z x y z c 1 1.0 3.0 5.0 7.0 9.0 11.0 2 2.0 4.0 6.0 8.0 10.0 12.0''' self.assertEqual(expected, str(df))
def test_to_csv_simple(self): t = Tabulator() box = Box(get_lst()) index = ['a', 'b'] columns = ['c'] values = 'd' pt = t.tabulate(box=box, values=values, columns=columns, index=index) with tempfile.TemporaryDirectory() as tmpdirname: fname = os.path.join(tmpdirname, 'test.csv') to_csv(pt, fname) with open(fname, 'r') as f: s = f.read() expected = '''c,,1,2 a,b,,, 1,1,11,14 1,2,13,12 2,1,15,16 2,2,17,18 ''' self.assertEqual(s, expected)
def test_to_csv_multiindex_cols(self): t = Tabulator() box = Box(get_lst4()) values = 'd' index = 'c' index_vals = [1, 2] df = t.vector_table(box, values, index, index_vals) with tempfile.TemporaryDirectory() as tmpdirname: fname = os.path.join(tmpdirname, 'test.csv') to_csv(df, fname) with open(fname, 'r') as f: s = f.read() expected = '''a,1,1,2,2 b,1,2,1,2 c,,,, 1,12.0,13.0,16.0,19.0 2,30.0,31.0,34.0,37.0 ''' self.assertEqual(s, expected)
def test_tabulate_store2(self): # TODO: FIX t = Tabulator() box = Box(get_lst2_b()) store = Store() d = store.new('d', components=['x', 'y']) index = d.label columns = ['a', 'b'] values = d pt = t.tabulate(box=box, values=values, columns=columns, index=index, store=store) expected = '''a 1 2 b 1 2 1 2 d: x 12 13 16 19 y 30 31 34 37''' self.assertEqual(str(pt), expected)
def test_tabulate_translated(self): dct = { 'a': Variable('one', 'blah', 'mm'), 'b': Variable('two', 'blah2', 's'), 'c': Variable('three', 'blah2', 'N') } a = Aliases(dct) t = Tabulator() box = Box(get_lst()) index = ['a', 'b'] columns = ['c'] values = 'd' pt = t.tabulate(box=box, values=values, columns=columns, index=index, aliases=a) expected = 'three [N] 1 2\none [mm] two [s] \n1 1 11 14\n 2 13 12\n2 1 15 16\n 2 17 18' self.assertEqual(str(pt), expected)