def test_blockwise_inner_join(): test_data = np.array([ (1.0, np.array([11,12,13]), np.array([1,0,1]), 0, np.array([1,2,3])), (2.0, np.array([21,22,23]), np.array([-1,2,-1]), 1, np.array([31,32,33]))], dtype=[ ('sl', np.float), ('al', 'O'), ('fk', 'O'), ('s_fk', np.int), ('ar', 'O')]) # vector join a1 = rnp.blockwise_inner_join( test_data, ['sl', 'al'], test_data['fk'], ['ar']) # specify fk with string a1 = rnp.blockwise_inner_join( test_data, ['sl', 'al'], 'fk', ['ar']) exp1 = np.array([ (1.0, 11, 2, 1), (1.0, 12, 1, 0), (1.0, 13, 2, 1), (2.0, 22, 33, 2)], dtype=[ ('sl', '<f8'), ('al', '<i8'), ('ar', '<i8'), ('fk', '<i8')]) assert_array_equal(a1, exp1, verbose=True) # vector join with force repeat a2 = rnp.blockwise_inner_join( test_data, ['sl','al'], test_data['fk'], ['ar'], force_repeat=['al']) exp2 = np.array([ (1.0, np.array([11, 12, 13]), 2, 1), (1.0, np.array([11, 12, 13]), 1, 0), (1.0, np.array([11, 12, 13]), 2, 1), (2.0, np.array([21, 22, 23]), 33, 2)], dtype=[ ('sl', '<f8'), ('al', '|O8'), ('ar', '<i8'), ('fk', '<i8')]) assert_equal(str(a2), str(exp2)) # numpy testing doesn't like subarray assert_equal(a2.dtype, exp2.dtype) # scalar join a3 = rnp.blockwise_inner_join( test_data, ['sl', 'al'], test_data['s_fk'], ['ar']) exp3 = np.array([ (1.0, [11, 12, 13], 1, 0), (2.0, [21, 22, 23], 32, 1)], dtype=[ ('sl', '<f8'), ('al', '|O8'), ('ar', '<i8'), ('fk', '<i8')]) assert_equal(str(a3), str(exp3)) # numpy testing doesn't like subarray assert_equal(a3.dtype, exp3.dtype)
# <codecell> #data to two block #left block is scalar sl and array al #right block is ar #two types of foreign key fk and s_fk #notice some foreign keys are invalid(-1) those are discarded test_data = np.array([ (1.0,np.array([11,12,13]),np.array([1,0,1]),0,np.array([1,2,3])), (2.0,np.array([21,22,23]),np.array([-1,2,-1]),1,np.array([31,32,33])) ], dtype=[('sl',np.float),('al','O'),('fk','O'),('s_fk',np.int),('ar','O')]) # <codecell> blockwise_inner_join(test_data, ['sl','al'], test_data['fk'], ['ar'] ) # <codecell> blockwise_inner_join(test_data, ['sl','al'], test_data['fk'], ['ar'], force_repeat=['al']) # <codecell> blockwise_inner_join(test_data, ['sl','al'], test_data['s_fk'], ['ar'] ) # <codecell> Bblock = [n for n in bb.dtype.names if n.startswith('B')] Dblock = [n for n in bb.dtype.names if n.startswith('D')] Kblock = [n for n in bb.dtype.names if n.startswith('K')]