def test_sharing(): data = Frame(create_test_data()) f1 = FrameRef(data, index=toindex([0, 1, 2, 3])) f2 = FrameRef(data, index=toindex([2, 3, 4, 5, 6])) # test read for k, v in f1.items(): assert F.allclose(F.narrow_row(data[k].data, 0, 4), v) for k, v in f2.items(): assert F.allclose(F.narrow_row(data[k].data, 2, 7), v) f2_a1 = f2['a1'] # test write # update own ref should not been seen by the other. f1[Index(F.tensor([0, 1]))] = { 'a1': F.zeros([2, D]), 'a2': F.zeros([2, D]), 'a3': F.zeros([2, D]), } assert F.allclose(f2['a1'], f2_a1) # update shared space should been seen by the other. f1[Index(F.tensor([2, 3]))] = { 'a1': F.ones([2, D]), 'a2': F.ones([2, D]), 'a3': F.ones([2, D]), } F.narrow_row_set(f2_a1, 0, 2, F.ones([2, D])) assert F.allclose(f2['a1'], f2_a1)
def test_column2(): # Test frameref column getter/setter data = Frame(create_test_data()) f = FrameRef(data, toindex([3, 4, 5, 6, 7])) assert f.num_rows == 5 assert len(f) == 3 assert F.allclose(f['a1'], F.narrow_row(data['a1'].data, 3, 8)) # set column should reflect on the referenced data f['a1'] = F.zeros([5, D]) assert F.allclose(F.narrow_row(data['a1'].data, 3, 8), F.zeros([5, D])) # add new partial column should fail with error initializer f.set_initializer(lambda shape, dtype : assert_(False)) def failed_add_col(): f['a4'] = F.ones([5, D]) assert check_fail(failed_add_col)
def test_slicing(): data = Frame(create_test_data(grad=True)) f1 = FrameRef(data, index=toindex(slice(1, 5))) f2 = FrameRef(data, index=toindex(slice(3, 8))) # test read for k, v in f1.items(): assert F.allclose(F.narrow_row(data[k].data, 1, 5), v) f2_a1 = f2['a1'] # is a tensor # test write f1[Index(F.tensor([0, 1]))] = { 'a1': F.zeros([2, D]), 'a2': F.zeros([2, D]), 'a3': F.zeros([2, D]), } assert F.allclose(f2['a1'], f2_a1) f1[Index(F.tensor([2, 3]))] = { 'a1': F.ones([2, D]), 'a2': F.ones([2, D]), 'a3': F.ones([2, D]), } F.narrow_row_set(f2_a1, 0, 2, 1) assert F.allclose(f2['a1'], f2_a1) f1[toindex(slice(2, 4))] = { 'a1': F.zeros([2, D]), 'a2': F.zeros([2, D]), 'a3': F.zeros([2, D]), } F.narrow_row_set(f2_a1, 0, 2, 0) assert F.allclose(f2['a1'], f2_a1)