Exemple #1
0
def test_instance_attribute():
    iattr = InstanceAttribute('type_array', dim='atom', dtype='str')
    eq_(iattr.size, 0)

    # Now we initialize as empty
    iattr.empty(10)
    eq_(iattr.size, 10)

    # We get another one
    iattr = InstanceAttribute('type_array', dim='atom', dtype='str')
    iattr.value = ['A', 'B', 'C', 'D']

    # Automatic casting
    ok_(isinstance(iattr.value, np.ndarray))
    assert_npequal(iattr.value, ['A', 'B', 'C', 'D'])

    # Reordering
    iattr2 = iattr.reorder([1, 0, 3, 2], inplace=False)
    assert_npequal(iattr2.value, ['B', 'A', 'D', 'C'])

    iattr.reorder([1, 0, 3, 2])
    assert_npequal(iattr.value, ['B', 'A', 'D', 'C'])

    # Reordering with wrong input raises ValueError
    assert_raises(ValueError, iattr.reorder, [1, 2])
    assert_raises(ValueError, iattr.reorder, [0, 1, 2, 100])
Exemple #2
0
def test_instance_attribute():
    iattr = InstanceAttribute("type_array", dim="atom", dtype="str")
    eq_(iattr.size, 0)

    # Now we initialize as empty
    iattr.empty(10)
    eq_(iattr.size, 10)

    # We get another one
    iattr = InstanceAttribute("type_array", dim="atom", dtype="str")
    iattr.value = ["A", "B", "C", "D"]

    # Automatic casting
    ok_(isinstance(iattr.value, np.ndarray))
    assert_npequal(iattr.value, ["A", "B", "C", "D"])

    # Reordering
    iattr2 = iattr.reorder([1, 0, 3, 2], inplace=False)
    assert_npequal(iattr2.value, ["B", "A", "D", "C"])

    iattr.reorder([1, 0, 3, 2])
    assert_npequal(iattr.value, ["B", "A", "D", "C"])

    # Reordering with wrong input raises ValueError
    assert_raises(ValueError, iattr.reorder, [1, 2])
    assert_raises(ValueError, iattr.reorder, [0, 1, 2, 100])
Exemple #3
0
def test_concatenate_attributes():
    a1 = InstanceAttribute('type_array', dim='atom', dtype='str')
    newattr = concatenate_attributes([a1, a1, a1])
    eq_(newattr.size, 0)
    
    a2 = a1.empty(10, inplace=False)
    newattr = concatenate_attributes([a1, a2, a2])
    eq_(newattr.size, 20)
    
    # Shape parameter
    r_array = InstanceAttribute('r_array', shape=(3,), dtype='f', dim='atom')
    r_array.value = [[0, 1, 2]]
    newattr = concatenate_attributes([r_array, r_array, r_array])
    eq_(newattr.size, 3)
    assert_npequal(newattr.value, [[0, 1, 2], [0, 1, 2], [0, 1, 2]])
Exemple #4
0
def test_instance_attribute():
    iattr = InstanceAttribute('type_array', dim='atom', dtype='str')
    eq_(iattr.size, 0)
    
    # Now we initialize as empty
    iattr.empty(10)
    eq_(iattr.size, 10) 
    
    # We get another one
    iattr = InstanceAttribute('type_array', dim='atom', dtype='str')
    iattr.value = ['A', 'B', 'C', 'D']
    
    # Automatic casting
    ok_(isinstance(iattr.value, np.ndarray))
    assert_npequal(iattr.value, ['A', 'B', 'C', 'D'])
    
    # Reordering
    iattr2 = iattr.reorder([1, 0, 3, 2], inplace=False)
    assert_npequal(iattr2.value, ['B', 'A', 'D', 'C'])
    
    iattr.reorder([1, 0, 3, 2])
    assert_npequal(iattr.value, ['B', 'A', 'D', 'C'])
    
    # Reordering with wrong input raises ValueError
    assert_raises(ValueError, iattr.reorder, [1, 2])
    assert_raises(ValueError, iattr.reorder, [0, 1, 2, 100])
    
    # Test Copy
    iattr = InstanceAttribute('type_array', dim='atom', dtype='str')
    iattr.empty(3)
    iattr.value = ['O', 'H', 'H']

    
    iattr2 = iattr.copy()
    assert_npequal(iattr2.value, iattr.value)
    
    iattr2.value = ['A', 'B', 'C']
    assert_npequal(iattr.value, ['O', 'H', 'H'])
    assert_npequal(iattr2.value, ['A', 'B', 'C'])
    
    iattr = InstanceAttribute('type_array', dim='atom', dtype='str')
    iattr.empty(3)
    iattr.value = ['O', 'H', 'H']
    
    iattr1 = iattr.sub([0, 1]) 
    assert_npequal(iattr1.value, ['O', 'H'])
    iattr1 = iattr.sub([False, True, False]) 
    assert_npequal(iattr1.value, ['H'])