def test_update_error(self): with self.assertRaises(DHError) as cm: t = empty_table(10) formulas = ["Col1 = i", "Col2 = Col * 2"] t2 = t.update(formulas) self.assertTrue(cm.exception.root_cause) self.assertIn("RuntimeError", cm.exception.compact_traceback)
def test_agg_by_2(self): test_table = empty_table(10) test_table = test_table.update( ["grp_id=(int)(i/5)", "var=(int)i", "weights=(double)1.0/(i+1)"]) aggs = [ group(["aggGroup=var"]), avg(["aggAvg=var"]), count_("aggCount"), first(["aggFirst=var"]), last(["aggLast=var"]), max_(["aggMax=var"]), median(["aggMed=var"]), min_(["aggMin=var"]), pct(0.20, ["aggPct=var"]), std(["aggStd=var"]), sum_(["aggSum=var"]), abs_sum(["aggAbsSum=var"]), var(["aggVar=var"]), weighted_avg("var", ["weights"]), ] result_table = test_table.agg_by(aggs, ["grp_id"]) self.assertGreaterEqual(result_table.size, 1) for agg in aggs: result_table = test_table.agg_by(agg, "grp_id") self.assertGreaterEqual(result_table.size, 1)
def test_snapshot_history(self): t = empty_table(1).update( formulas=["Timestamp=io.deephaven.time.DateTime.now()"]) snapshot_hist = t.snapshot_history(source_table=self.test_table) self.assertEqual(1 + len(self.test_table.columns), len(snapshot_hist.columns)) self.assertEqual(self.test_table.size, snapshot_hist.size)
def test_empty_table_error(self): with self.assertRaises(DHError) as cm: t = empty_table("abc") self.assertIn("RuntimeError", cm.exception.root_cause) self.assertIn("no matching Java method overloads found", cm.exception.compact_traceback)
def test_pyobj_field_access(self): t = empty_table(10) t2 = t.update(formulas=[ "SYM = `AAPL-` + (String)pyobj.name", "PRICE = i * 1000" ]).where("PRICE > (int)pyobj.price + 100") html_output = to_html(t2) self.assertIn("AAPL-GOOG", html_output) self.assertIn("2000", html_output)
def test_repr(self): regex = r"deephaven\.table\.Table\(io\.deephaven\.engine\.table\.Table\(objectRef=0x.+\{.+\}\)\)" for i in range(0, 8): t = empty_table(10**i).update("a=i") result = repr(t) self.assertRegex(result, regex) self.assertLessEqual(len(result), 120) self.assertIn(t.__class__.__name__, result)
def freeze_table(table: Table) -> Table: """ Returns a static snapshot of the source ticking table. Args: table (Table): the source table Returns: a new table """ return empty_table(0).snapshot(table, True)
def test_agg_all_by(self): test_table = empty_table(10) test_table = test_table.update( ["grp_id=(int)(i/5)", "var=(int)i", "weights=(double)1.0/(i+1)"]) aggs = [ group(), avg(), first(), last(), max_(), median(), min_(), pct(0.20), std(), sum_(), abs_sum(), var(), weighted_avg("var"), ] for agg in aggs: with self.subTest(agg): result_table = test_table.agg_all_by(agg, ["grp_id"]) self.assertGreaterEqual(result_table.size, 1) # column names in the Aggregation are ignored aggs = [ group(["aggGroup=var"]), avg(["aggAvg=var"]), pct(0.20, ["aggPct=var"]), std(["aggStd=var"]), sum_(["aggSum=var"]), abs_sum(["aggAbsSum=var"]), var(["aggVar=var"]), weighted_avg("var", ["weights"]), ] for agg in aggs: with self.subTest(agg): result_table = test_table.agg_all_by(agg, ["grp_id"]) self.assertGreaterEqual(result_table.size, 1) with self.assertRaises(DHError) as cm: test_table.agg_all_by(count_("aggCount"), "grp_id") self.assertIn("unsupported", cm.exception.root_cause) for agg in aggs: with self.subTest(agg): result_table = test_table.agg_all_by(agg) self.assertEqual(result_table.size, 1)
def test_crd(self): """ Test suite for reading, writing, and deleting a table to disk """ table = empty_table(3).update( formulas=["x=i", "y=(double)(i/10.0)", "z=(double)(i*i)"]) definition = table.columns base_dir = os.path.join(self.temp_dir.name, "testCreation") file_location = os.path.join(base_dir, 'table1.parquet') file_location2 = os.path.join(base_dir, 'table2.parquet') # make sure that the test workspace is clean if os.path.exists(file_location): shutil.rmtree(file_location) if os.path.exists(file_location2): shutil.rmtree(file_location2) # Writing with self.subTest(msg="write_table(Table, str)"): write(table, file_location) self.assertTrue(os.path.exists(file_location)) table2 = read(file_location) self.assert_table_equals(table, table2) shutil.rmtree(base_dir) with self.subTest( msg="write_tables(Table[], destinations, col_definitions"): batch_write([table, table], [file_location, file_location2], definition) self.assertTrue(os.path.exists(file_location)) self.assertTrue(os.path.exists(file_location2)) table2 = read(file_location) self.assert_table_equals(table, table2) # Delete with self.subTest(msg="delete(str)"): if os.path.exists(file_location): delete(file_location) self.assertFalse(os.path.exists(file_location)) if os.path.exists(file_location2): delete(file_location2) self.assertFalse(os.path.exists(file_location2)) shutil.rmtree(base_dir)
def test_snapshot(self): with self.subTest("do_init is False"): t = empty_table(0).update(formulas=[ "Timestamp=io.deephaven.time.DateTime.now()", "X = i * i", "Y = i + i" ]) snapshot = t.snapshot(source_table=self.test_table) self.assertEqual( len(t.columns) + len(self.test_table.columns), len(snapshot.columns)) self.assertEqual(0, snapshot.size) with self.subTest("do_init is True"): snapshot = t.snapshot(source_table=self.test_table, do_init=True) self.assertEqual(self.test_table.size, snapshot.size) with self.subTest("with cols"): snapshot = t.snapshot(source_table=self.test_table, cols="X") self.assertEqual(len(snapshot.columns), len(self.test_table.columns) + 1) snapshot = t.snapshot(source_table=self.test_table, cols=["X", "Y"]) self.assertEqual(len(snapshot.columns), len(self.test_table.columns) + 2)
def test_wrong_return_type(self): with self.assertRaises(Exception): t = empty_table(10).view(formulas=["I=ii", "J=(ii * 2)"])\ .where("vectorized_func_wrong_return_type(I, J)")
def setUpClass(cls): """ Inherited method allowing initialization of test environment. """ # Tables cls.bool_table = empty_table(100).update([ "X = true", "Y = false", "Z = (i % 2 == 0) ? true : false", ]) cls.byte_table = empty_table(100).update([ "X = (byte)i", "Y = (byte)(100 - X)", "Z = (byte)(-101 + X)", ]) cls.short_table = empty_table(100).update([ "X = (short)i", "Y = (short)(100 - X)", "Z = (short)(-101 + X)", ]) cls.int_table = empty_table(100).update([ "X = (int)i", "Y = 100 - X", "Z = -101 + X", ]) cls.long_table = empty_table(100).update([ "X = (long)i", "Y = 100 - X", "Z = -101 + X", ]) cls.float_table = empty_table(100).update([ "X = (float)i", "Y = (float)sqrt(X)", "Z = (float)sqrt(Y)", ]) cls.double_table = empty_table(100).update([ "X = (double)i", "Y = sqrt(X)", "Z = sqrt(Y)", ]) # NumPy arrays cls.bool_array = \ np.array([[True, False, True], [True, False, False]] * 50, dtype=np.bool_) cls.byte_array = np.vstack( (np.arange(0, 100, dtype=np.byte), np.arange(100, 0, -1, dtype=np.byte), np.arange(-101, -1, dtype=np.byte))).T cls.short_array = np.vstack( (np.arange(0, 100, dtype=np.short), np.arange(100, 0, -1, dtype=np.short), np.arange(-101, -1, dtype=np.short))).T cls.int_array = np.vstack( (np.arange(0, 100, dtype=np.intc), np.arange(100, 0, -1, dtype=np.intc), np.arange(-101, -1, dtype=np.intc))).T cls.long_array = np.vstack( (np.arange(0, 100, dtype=np.int_), np.arange(100, 0, -1, dtype=np.int_), np.arange(-101, -1, dtype=np.int_))).T cls.float_array = np.vstack( (np.arange(0, 100, dtype=np.single), np.sqrt(np.arange(0, 100, dtype=np.single)), np.sqrt(np.sqrt(np.arange(0, 100, dtype=np.single))))).T cls.double_array = np.vstack( (np.arange(0, 100, dtype=np.double), np.sqrt(np.arange(0, 100, dtype=np.double)), np.sqrt(np.sqrt(np.arange(0, 100, dtype=np.double))))).T
def create_some_counters(): t = empty_table(10).update(formulas=["X=i"]) t2 = empty_table(10).update(formulas=["X=i"]) return t.join(t2, on=["X"])
def test_crd_with_instructions(self): """ Test suite for reading, writing, and deleting a table to disk """ table = empty_table(3).update(formulas=[ "x=i", "y=String.valueOf((double)(i/10.0))", "z=(double)(i*i)" ]) col_definitions = table.columns base_dir = os.path.join(self.temp_dir.name, "testCreation") file_location = os.path.join(base_dir, 'table1.parquet') file_location2 = os.path.join(base_dir, 'table2.parquet') # make sure that the test workspace is clean if os.path.exists(file_location): shutil.rmtree(file_location) if os.path.exists(file_location2): shutil.rmtree(file_location2) # Writing col_inst = ColumnInstruction(column_name="x", parquet_column_name="px") col_inst1 = ColumnInstruction(column_name="y", parquet_column_name="py") with self.subTest(msg="write_table(Table, str, max_dictionary_keys)"): write(table, file_location, max_dictionary_keys=10) self.assertTrue(os.path.exists(file_location)) shutil.rmtree(base_dir) with self.subTest( msg= "write_table(Table, str, col_instructions, max_dictionary_keys)" ): write(table, file_location, col_instructions=[col_inst, col_inst1], max_dictionary_keys=10) self.assertTrue(os.path.exists(file_location)) shutil.rmtree(base_dir) with self.subTest( msg="write_tables(Table[], destinations, col_definitions, "): batch_write([table, table], [file_location, file_location2], col_definitions, col_instructions=[col_inst, col_inst1]) self.assertTrue(os.path.exists(file_location)) self.assertTrue(os.path.exists(file_location2)) shutil.rmtree(base_dir) with self.subTest( msg="write_table(Table, destination, col_definitions, "): write(table, file_location, col_instructions=[col_inst, col_inst1]) # self.assertTrue(os.path.exists(file_location)) # Reading with self.subTest(msg="read_table(str)"): table2 = read(path=file_location, col_instructions=[col_inst, col_inst1]) self.assert_table_equals(table, table2) # Delete with self.subTest(msg="delete(str)"): if os.path.exists(file_location): delete(file_location) self.assertFalse(os.path.exists(file_location)) if os.path.exists(file_location2): delete(file_location2) self.assertFalse(os.path.exists(file_location2)) shutil.rmtree(base_dir)
# # Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending # from test_helper import start_jvm start_jvm() from deephaven import empty_table from deephaven.constants import NULL_LONG, NULL_SHORT, NULL_INT, NULL_BYTE null_byte = NULL_BYTE null_short = NULL_SHORT null_int = NULL_INT null_long = NULL_LONG def return_null_long(): return NULL_LONG t = empty_table(9).update([ "X = null_byte", "Y = null_short", "YY = null_int", "Z = null_long", "ZZ = (long)return_null_long()" ]) assert t.to_string().count("null") == 45
def test_long_number_conversion(self): t = empty_table(1) result = t.update("X = long_value").to_string(1) self.assertEqual(long_value, int(result.split()[2]))
def test_empty_table(self): t = empty_table(10) self.assertEqual(0, len(t.columns))
def test_part_of_expr(self): with self.assertRaises(Exception): t = empty_table(10).view(formulas=["I=ii", "J=(ii * 2)"]).update( "K = 2 * vectorized_func(I, J)")
from test_helper import start_jvm start_jvm() from deephaven import dtypes from deephaven import empty_table from deephaven.constants import MAX_BYTE, MAX_SHORT, MAX_INT, MAX_LONG x = [MAX_BYTE, MAX_SHORT, MAX_INT, MAX_LONG, 0.98888, 999999.888888] n = len(x) def get_x(i): return x[i] t_list = empty_table(n).update(["X = x[i]"]) t_func = empty_table(n).update(["X = get_x(i)"]) # We want to test that casting on both PyObject and JObject works as expected. assert t_list.columns[0].data_type == dtypes.PyObject assert t_func.columns[0].data_type == dtypes.JObject t_list_integers = t_list.update([ "A = (byte)X", "B = (short)X", "C = (int)X", "D = (long)X", "E = (float)X", "F = (double)X" ]) assert t_list_integers.columns[1].data_type == dtypes.byte assert t_list_integers.columns[2].data_type == dtypes.short assert t_list_integers.columns[3].data_type == dtypes.int32 assert t_list_integers.columns[4].data_type == dtypes.long assert t_list_integers.columns[5].data_type == dtypes.float32 assert t_list_integers.columns[6].data_type == dtypes.double
def test_column(self): t = empty_table(10).view(formulas=["I=ii", "J=(ii * 2)"]).update( "K = vectorized_func(I, J)") html_output = to_html(t) self.assertIn("<td>9</td>", html_output)
def test_filter(self): t = empty_table(10).view( formulas=["I=ii", "J=(ii * 2)"]).where("vectorized_func(I, J)") html_output = to_html(t) self.assertIn("<td>5</td><td>10</td>", html_output)