def setup(self): counter = collections.Counter({ (0, 0): 1.0, (0, 2): 2.0, (1, 4): 4.0, (-1, -1): 5.0 }) self.lookup = VoxelLookupFunction(left_bin_edges=(-1.0, 1.0), bin_widths=(0.5, 0.25), counter=counter)
class TestVoxelLookupFunction(object): def setup(self): counter = collections.Counter({ (0, 0): 1.0, (0, 2): 2.0, (1, 4): 4.0, (-1, -1): 5.0 }) self.lookup = VoxelLookupFunction(left_bin_edges=(-1.0, 1.0), bin_widths=(0.5, 0.25), counter=counter) def test_keys(self): keys = [(0, 0), (0, 2), (1, 4), (-1, -1)] assert_equal(len(list(self.lookup.keys())), len(keys)) for key in list(self.lookup.keys()): assert_in(key, keys) def test_values(self): values = [1.0, 2.0, 4.0, 5.0] assert_equal(len(list(self.lookup.values())), len(values)) for val in list(self.lookup.values()): assert_in(val, values) def test_bin_to_left_edge(self): assert_items_equal(self.lookup.bin_to_left_edge((0, 0)), (-1.0, 1.0)) assert_items_equal(self.lookup.bin_to_left_edge((2, -1)), (0.0, 0.75)) def test_val_to_bin(self): result_bin = self.lookup.val_to_bin((-0.25, 2.0)) assert_equal(len(result_bin), 2) assert_items_equal(result_bin, [1.5, 4.0]) def test_counter_by_bin_edges(self): bin_edge_counter = self.lookup.counter_by_bin_edges expected = { (-1.0, 1.0): 1.0, (-1.0, 1.5): 2.0, (-0.5, 2.0): 4.0, (-1.5, 0.75): 5.0 } assert_equal(len(expected), len(bin_edge_counter)) for k in list(expected.keys()): assert_equal(bin_edge_counter[k], expected[k]) def test_df_2d(self): df1 = self.lookup.df_2d() assert_items_equal(df1.index, [-1, 0, 1]) assert_items_equal(df1.columns, [-1, 0, 2, 4]) assert_equal(df1.at[-1, -1], 5.0) assert_equal(isnan(df1.at[0, 4]), True) df2 = self.lookup.df_2d(x_range=(-1, 3), y_range=(-1, 4)) assert_items_equal(df2.index, [-1, 0, 1, 2, 3]) assert_items_equal(df2.columns, [-1, 0, 1, 2, 3, 4]) assert_equal(df2.at[-1, -1], 5.0) assert_equal(isnan(df2.at[0, 4]), True) assert_equal(isnan(df2.at[2, 3]), True) @raises(RuntimeError) def test_df_2d_not_2d(self): counter = collections.Counter({(0, 0, 0): 1.0}) luf = VoxelLookupFunction(left_bin_edges=(0, 0, 0), bin_widths=(1, 1, 1), counter=counter) luf.df_2d() def test_call(self): assert_equal(self.lookup((0.0, 0.0)), 0.0) # no bin assert_equal(self.lookup((-0.5, 2.0)), 4.0) # bin edge assert_equal(self.lookup((-0.4, 2.1)), 4.0) # in bin
def test_df_2d_not_2d(self): counter = collections.Counter({(0, 0, 0): 1.0}) luf = VoxelLookupFunction(left_bin_edges=(0, 0, 0), bin_widths=(1, 1, 1), counter=counter) luf.df_2d()
def setup(self): counter = collections.Counter({(0,0): 1.0, (0,2): 2.0, (1,4): 4.0, (-1,-1): 5.0}) self.lookup = VoxelLookupFunction(left_bin_edges=(-1.0, 1.0), bin_widths=(0.5, 0.25), counter=counter)
class testVoxelLookupFunction(object): def setup(self): counter = collections.Counter({(0,0): 1.0, (0,2): 2.0, (1,4): 4.0, (-1,-1): 5.0}) self.lookup = VoxelLookupFunction(left_bin_edges=(-1.0, 1.0), bin_widths=(0.5, 0.25), counter=counter) def test_keys(self): keys = [(0,0), (0,2), (1,4), (-1,-1)] assert_equal(len(self.lookup.keys()), len(keys)) for key in self.lookup.keys(): assert_in(key, keys) def test_values(self): values = [1.0, 2.0, 4.0, 5.0] assert_equal(len(self.lookup.values()), len(values)) for val in self.lookup.values(): assert_in(val, values) def test_bin_to_left_edge(self): assert_items_equal(self.lookup.bin_to_left_edge((0,0)), (-1.0, 1.0)) assert_items_equal(self.lookup.bin_to_left_edge((2,-1)), (0.0, 0.75)) def test_val_to_bin(self): result_bin = self.lookup.val_to_bin((-0.25, 2.0)) assert_equal(len(result_bin), 2) assert_items_equal(result_bin, [1.5, 4.0]) def test_counter_by_bin_edges(self): bin_edge_counter = self.lookup.counter_by_bin_edges expected = {(-1.0, 1.0): 1.0, (-1.0,1.5): 2.0, (-0.5, 2.0): 4.0, (-1.5, 0.75): 5.0} assert_equal(len(expected), len(bin_edge_counter)) for k in expected.keys(): assert_equal(bin_edge_counter[k], expected[k]) def test_df_2d(self): df1 = self.lookup.df_2d() assert_items_equal(df1.index, [-1, 0, 1]) assert_items_equal(df1.columns, [-1, 0, 2, 4]) assert_equal(df1.get_value(-1, -1), 5.0) assert_equal(isnan(df1.get_value(0, 4)), True) df2 = self.lookup.df_2d(x_range=(-1, 3), y_range=(-1, 4)) assert_items_equal(df2.index, [-1, 0, 1, 2, 3]) assert_items_equal(df2.columns, [-1, 0, 1, 2, 3, 4]) assert_equal(df2.get_value(-1, -1), 5.0) assert_equal(isnan(df2.get_value(0, 4)), True) assert_equal(isnan(df2.get_value(2, 3)), True) @raises(RuntimeError) def test_df_2d_not_2d(self): counter = collections.Counter({(0,0,0): 1.0}) luf = VoxelLookupFunction(left_bin_edges=(0,0,0), bin_widths=(1,1,1), counter=counter) luf.df_2d() def test_call(self): assert_equal(self.lookup((0.0, 0.0)), 0.0) # no bin assert_equal(self.lookup((-0.5, 2.0)), 4.0) # bin edge assert_equal(self.lookup((-0.4, 2.1)), 4.0) # in bin
def test_df_2d_not_2d(self): counter = collections.Counter({(0,0,0): 1.0}) luf = VoxelLookupFunction(left_bin_edges=(0,0,0), bin_widths=(1,1,1), counter=counter) luf.df_2d()