Ejemplo n.º 1
0
def test_h5():
    try:
        import h5py
        dct1 = \
            {'/a': 'abcgs',
             '/b/c/x1': 3,
             '/b/c/x2': rand(2,3),
             }
        # writing a dct w/o leading slash will always be read back in *with*
        # leading slash
        dct2 = \
            {'a': 'abciqo4iki',
             'b/c/x1': 3,
             'b/c/x2': rand(2,3),
             }
        for idx, dct in enumerate([dct1, dct2]):
            h5fn = os.path.join(testdir, 'test_%i.h5' % idx)
            io.write_h5(h5fn, dct)
            read_dct = io.read_h5(h5fn)
            for kk in list(read_dct.keys()):
                assert kk.startswith('/')
            for kk in list(dct.keys()):
                key = '/' + kk if not kk.startswith('/') else kk
                tools.assert_all_types_equal(dct[kk], read_dct[key])

        # write mode='a', test appending
        h5fn = os.path.join(testdir, 'test_append.h5')
        io.write_h5(h5fn, {'/a': 1.0})
        read_dct = io.read_h5(h5fn)
        assert list(read_dct.keys()) == ['/a']
        assert read_dct['/a'] == 1.0
        # append '/b', using {'/a': 1.0, '/b': 2.0} would be an error since /a
        # already exists, use mode='w' then, but this overwrites all!
        io.write_h5(h5fn, {'/b': 2.0}, mode='a')
        read_dct2 = io.read_h5(h5fn)
        # sort(...): sort possible [/b, /a] -> [/a, /b]
        assert np.sort(np.array(list(
            read_dct2.keys()))).tolist() == ['/a', '/b']
        assert read_dct2['/a'] == 1.0
        assert read_dct2['/b'] == 2.0
        # overwrite
        io.write_h5(h5fn, {'/b': 22.0, '/c': 33.0}, mode='w')
        read_dct3 = io.read_h5(h5fn)
        assert np.sort(np.array(list(
            read_dct3.keys()))).tolist() == ['/b', '/c']

    except ImportError:
        tools.skip("skipping test_h5, no h5py importable")
Ejemplo n.º 2
0
def test_get_traj():
    st = Structure(coords_frac=rand(20, 3),
                   symbols=['H'] * 20,
                   forces=rand(20, 3),
                   stress=rand(3, 3),
                   etot=42.23)
    nstep = 5
    tr = st.get_traj(nstep)
    for name in st.attr_lst:
        print(name)
        attr = getattr(tr, name)
        if attr is not None:
            if name in tr.attrs_nstep:
                assert attr.shape[tr.timeaxis] == nstep
            else:
                attr_st = getattr(st, name)
                assert_all_types_equal(attr, attr_st)
Ejemplo n.º 3
0
def test_h5():
    try:
        import h5py
        dct1 = \
            {'/a': 'abcgs',
             '/b/c/x1': 3,
             '/b/c/x2': rand(2,3),
             }
        # writing a dct w/o leading slash will always be read back in *with*
        # leading slash             
        dct2 = \
            {'a': 'abciqo4iki',
             'b/c/x1': 3,
             'b/c/x2': rand(2,3),
             }
        for idx,dct in enumerate([dct1, dct2]):             
            h5fn = os.path.join(testdir, 'test_%i.h5' %idx)
            io.write_h5(h5fn, dct)
            read_dct = io.read_h5(h5fn)
            for kk in read_dct.keys():
                assert kk.startswith('/')
            for kk in dct.keys():
                key = '/'+kk if not kk.startswith('/') else kk
                tools.assert_all_types_equal(dct[kk], read_dct[key])
        
        # write mode='a', test appending
        h5fn = os.path.join(testdir, 'test_append.h5')
        io.write_h5(h5fn, {'/a': 1.0})
        read_dct = io.read_h5(h5fn)
        assert read_dct.keys() == ['/a']
        assert read_dct['/a'] == 1.0
        # append '/b', using {'/a': 1.0, '/b': 2.0} would be an error since /a
        # already exists, use mode='w' then, but this overwrites all!
        io.write_h5(h5fn, {'/b': 2.0}, mode='a')
        read_dct2 = io.read_h5(h5fn)
        # sort(...): sort possible [/b, /a] -> [/a, /b]
        assert np.sort(np.array(read_dct2.keys())).tolist() == ['/a', '/b']
        assert read_dct2['/a'] == 1.0
        assert read_dct2['/b'] == 2.0
        # overwrite
        io.write_h5(h5fn, {'/b': 22.0, '/c': 33.0}, mode='w')
        read_dct3 = io.read_h5(h5fn)
        assert np.sort(np.array(read_dct3.keys())).tolist() == ['/b', '/c']

    except ImportError:
        tools.skip("skipping test_h5, no h5py importable")
Ejemplo n.º 4
0
def test_get_traj():
    st = Structure(coords_frac=rand(20,3),
                   symbols=['H']*20,
                   forces=rand(20,3),
                   stress=rand(3,3),
                   etot=42.23)
    nstep = 5
    tr = st.get_traj(nstep)
    for name in st.attr_lst:
        print name
        attr = getattr(tr, name)
        if attr is not None:
            if name in tr.attrs_nstep:
                assert attr.shape[tr.timeaxis] == nstep
            else:
                attr_st = getattr(st, name)
                assert_all_types_equal(attr, attr_st)
Ejemplo n.º 5
0
def check_generated(calc_root, machine_dct, params_lst, revision):
    """Check consistency of calc database values, replacement params in
    `params_lst` and all written files.
    """
    dbfn = pj(calc_root, 'calc.db')
    db = sql.SQLiteDB(dbfn, table='calc')
    print("database content:")
    print(db.get_dict("select * from calc"))
    db_colnames = [x[0] for x in db.get_header()]
    for idx, hostname_str in db.execute(
            "select idx,hostname from calc \
                                        where revision==?",
        (revision, )).fetchall():
        for hostname in hostname_str.split(','):
            machine = machine_dct[hostname]
            calc_dir = pj(calc_root, 'calc_%s' % machine.hostname, str(idx))
            for base in ['pw.in', machine.get_jobfile_basename()]:
                fn = pj(calc_dir, base)
                assert os.path.exists(fn)
                lines = common.file_readlines(fn)
                # assemble all possible replacements in one list of SQLEntry
                # instances, some things are redundantely checked twice ...
                sql_lst = params_lst[idx] + list(
                    machine.get_sql_record().values())
                for db_key in db_colnames:
                    db_val = db.get_single(
                        "select %s from calc "
                        "where idx==?" % db_key, (idx, ))
                    if db_val is not None:
                        sql_lst.append(sql.SQLEntry(key=db_key, sqlval=db_val))
                # for each replacement key, check if they are correctly placed
                # in the database (if applicable) and in the written files
                for sqlentry in sql_lst:
                    if sqlentry.key in db_colnames:
                        db_val = db.get_single("select %s from calc "
                                               "where idx==?" \
                                               %sqlentry.key, (idx,))
                        assert_all_types_equal(db_val, sqlentry.sqlval)
                    else:
                        db_val = 'NOT_DEFINED_IN_DB'
                    print("check_generated: idx={0}, sqlentry.key={1}, "
                          "sqlentry.sqlval={2}, db_val={3}".format(
                              idx, sqlentry.key, sqlentry.sqlval, db_val))
                    check_key_in_file(lines, sqlentry.key, sqlentry.sqlval)
    db.finish()
Ejemplo n.º 6
0
def check_generated(calc_root, machine_dct, params_lst, revision):
    """Check consistency of calc database values, replacement params in
    `params_lst` and all written files.
    """
    db = sql.SQLiteDB(pj(calc_root, 'calc.db'), table='calc')
    db_colnames = [x[0] for x in db.get_header()]
    for idx,hostname_str in db.execute("select idx,hostname from calc \
                                        where revision==?", (revision,)).fetchall():
        for hostname in hostname_str.split(','):
            machine = machine_dct[hostname]
            calc_dir = pj(calc_root, 'calc_%s' %machine.hostname, str(idx))
            for base in ['pw.in', machine.get_jobfile_basename()]:
                fn = pj(calc_dir, base)
                assert os.path.exists(fn)
                lines = common.file_readlines(fn)
                # assemble all possible replacements in one list of SQLEntry
                # instances, some things are redundantely checked twice ...
                sql_lst = params_lst[idx] + machine.get_sql_record().values()
                for db_key in db_colnames:
                    db_val = db.get_single("select %s from calc "
                                           "where idx==?" %db_key,
                                           (idx,))
                    if db_val is not None:                                           
                        sql_lst.append(sql.SQLEntry(key=db_key, sqlval=db_val))
                # for each replacement key, check if they are correctly placed
                # in the database (if applicable) and in the written files
                for sqlentry in sql_lst:
                    if sqlentry.key in db_colnames:
                        db_val = db.get_single("select %s from calc "
                                               "where idx==?" \
                                               %sqlentry.key, (idx,))
                        assert_all_types_equal(db_val, sqlentry.sqlval)
                    else:
                        db_val = 'NOT_DEFINED_IN_DB'
                    print("check_generated: idx={0}, sqlentry.key={1}, "
                          "sqlentry.sqlval={2}, db_val={3}".format(idx, sqlentry.key, 
                                                                 sqlentry.sqlval,
                                                                 db_val))
                    check_key_in_file(lines, sqlentry.key, sqlentry.sqlval)
    db.finish()
Ejemplo n.º 7
0
def test_get_cont():
    filename = tools.unpack_compressed('files/pw.md.out.gz', prefix=__file__)
    pp = parse.PwMDOutputFile(filename=filename)
    tr1 = pp.get_traj()

    # Need new parser instance, since pp.cont is already used, i.e. set_all()
    # called -> all attrs set. Also units are already applied, thus won't be
    # applied again since self.units_applied=True.
    pp = parse.PwMDOutputFile(filename=filename)
    tr2 = pp.get_traj(auto_calc=False)

    # specific for the used pw.out file, None is everything which is not parsed
    # since nothing is calculated from parsed data
    none_attrs = [
        'coords_frac',
        'cryst_const',
        'pressure',
        'velocity',
        'volume',
        'mass',
        'mass_unique',
        'nspecies',
        'ntypat',
        'order',
        'symbols_unique',
        'typat',
        'time',
        'znucl',
        'znucl_unique',
    ]

    for name in tr1.attr_lst:
        a1 = getattr(tr1, name)
        a2 = getattr(tr2, name)
        if name in none_attrs:
            assert a1 is not None, ("a1 %s is None" % name)
            assert a2 is None, ("a2 %s is not None" % name)
        else:
            tools.assert_all_types_equal(a1, a2)
Ejemplo n.º 8
0
def test_struct():
    natoms = 10
    cell = np.array([[3, 0, 0], [1.1, 5, -0.04], [-0.33, 1.5, 7]])
    cryst_const = crys.cell2cc(cell)
    coords_frac = rand(natoms, 3)
    coords = crys.coord_trans(coords=coords_frac, old=cell, new=np.identity(3))
    symbols = ['H'] * natoms
    stress = rand(3, 3)
    forces = rand(natoms, 3)

    # Use ``cell`` instead of ``cryst_const` as input such that
    # atoms.get_cell() test passes (see below for why -- cell orientation)
    st = Structure(coords_frac=coords_frac,
                   symbols=symbols,
                   cell=cell,
                   stress=stress,
                   forces=forces,
                   etot=42)

    # Test if all getters work.
    for name in st.attr_lst:
        print(name)
        st.try_set_attr(name)
        assert getattr(st, name) is not None, "attr None: %s" % name
        assert eval('st.get_%s()' %
                    name) is not None, "getter returns None: %s" % name
    aaae(coords_frac, st.coords_frac)
    aaae(cryst_const, st.cryst_const)
    aaae(coords, st.coords)
    assert st.natoms == natoms

    st = Structure(coords_frac=coords_frac, symbols=symbols, cell=cell)
    aaae(coords, st.get_coords())

    # Cell calculated from cryst_const has defined orientation in space which may be
    # different from the original `cell`, but the volume and underlying cryst_const
    # must be the same.
    st = Structure(coords_frac=coords_frac,
                   symbols=symbols,
                   cryst_const=cryst_const)
    assert st.get_cell() is not None
    np.testing.assert_almost_equal(crys.volume_cell(cell),
                                   crys.volume_cell(st.get_cell()))
    aaae(cryst_const, crys.cell2cc(st.get_cell()))

    # units
    st = Structure(coords_frac=coords_frac,
                   cell=cell,
                   symbols=symbols,
                   stress=stress,
                   forces=forces,
                   units={
                       'length': 2,
                       'forces': 3,
                       'stress': 4
                   })
    aaae(2 * coords, st.coords)
    aaae(3 * forces, st.forces)
    aaae(4 * stress, st.stress)

    traj = crys.struct2traj(st)
    assert traj.is_traj

    # copy(): Assert everything has another memory location = is a new copy of
    # the object. IntTypes are NOT copied by copy.deepcopy(), which we use in
    # Structure.copy(), apparently b/c they are always automatically copied
    # before in-place operations. Same for float type.
    #
    # >>> a=10; b=a; print id(a); print id(b)
    # 36669152
    # 36669152
    # >>> a*=100; print id(a); print id(b)
    # 72538264
    # 36669152
    # >>> a
    # 100
    # >>> b
    # 10
    #
    # >>> a=[1,2,3]; b=a; print id(a); print id(b)
    # 72624320
    # 72624320
    # >>> a[0] = 44; print id(a); print id(b)
    # 72624320
    # 72624320
    # >>> a
    # [44, 2, 3]
    # >>> b
    # [44, 2, 3]
    st2 = st.copy()
    for name in st.attr_lst:
        val = getattr(st, name)
        if val is not None and not (isinstance(val, int) or \
            isinstance(val, float)):
            val2 = getattr(st2, name)
            assert id(val2) != id(val)
            assert_all_types_equal(val2, val)
Ejemplo n.º 9
0
def test_traj():
    natoms = 10
    nstep = 100
    cell = rand(nstep,3,3)
    stress = rand(nstep,3,3)
    forces = rand(nstep,natoms,3)
    etot = rand(nstep)
    cryst_const = crys.cell2cc3d(cell, axis=0)
    coords_frac = rand(nstep,natoms,3)
    coords = crys.coord_trans3d(coords=coords_frac,
                                old=cell,
                                new=num.extend_array(np.identity(3),
                                                     nstep,axis=0),
                                axis=1,
                                timeaxis=0)                                                    
    assert cryst_const.shape == (nstep, 6)
    assert coords.shape == (nstep,natoms,3)
    symbols = ['H']*natoms
    
    # automatically calculated:
    #   coords
    #   cell
    #   pressure
    #   velocity (from coords)
    #   temperature (from ekin)
    #   ekin (from velocity)
    traj = Trajectory(coords_frac=coords_frac,
                    cell=cell,
                    symbols=symbols,
                    forces=forces,
                    stress=stress,
                    etot=etot,
                    timestep=1,
                    )
    # Test if all getters work.
    for name in traj.attr_lst:
        print "test if getters work:", name
        traj.try_set_attr(name)
        assert getattr(traj, name) is not None, "attr None: %s" %name
        assert eval('traj.get_%s()'%name) is not None, "getter returns None: %s" %name
        print "test if getters work:", name, "... ok"
    aaae(coords_frac, traj.coords_frac)
    aaae(coords, traj.coords)
    aaae(cryst_const, traj.cryst_const)
    aaae(np.trace(stress, axis1=1, axis2=2)/3.0, traj.pressure)
    assert traj.coords.shape == (nstep,natoms,3)
    assert traj.cell.shape == (nstep,3,3)
    assert traj.velocity.shape == (nstep, natoms, 3)
    assert traj.temperature.shape == (nstep,)
    assert traj.ekin.shape == (nstep,)
    assert traj.nstep == nstep
    assert traj.natoms == natoms

    traj = Trajectory(coords_frac=coords_frac,
                    symbols=symbols,
                    cell=cell)
    aaae(coords, traj.coords)
    
    # Cell calculated from cryst_const has defined orientation in space which may be
    # different from the original `cell`, but the volume and underlying cryst_const
    # must be the same.
    traj = Trajectory(coords_frac=coords_frac,
                    symbols=symbols,
                    cryst_const=cryst_const)
    np.testing.assert_almost_equal(crys.volume_cell3d(cell),
                                   crys.volume_cell3d(traj.cell))
    aaae(cryst_const, crys.cell2cc3d(traj.cell))
    
    # extend arrays
    cell2d = rand(3,3)
    cc2d = crys.cell2cc(cell2d)
    traj = Trajectory(coords_frac=coords_frac,
                      cell=cell2d,
                      symbols=symbols)
    assert traj.cell.shape == (nstep,3,3)
    assert traj.cryst_const.shape == (nstep,6)
    for ii in range(traj.nstep):
        assert (traj.cell[ii,...] == cell2d).all()
        assert (traj.cryst_const[ii,:] == cc2d).all()
    
    traj = Trajectory(coords_frac=coords_frac,
                      cryst_const=cc2d,
                      symbols=symbols)
    assert traj.cell.shape == (nstep,3,3)
    assert traj.cryst_const.shape == (nstep,6)
    for ii in range(traj.nstep):
        assert (traj.cryst_const[ii,:] == cc2d).all()

    # units
    traj = Trajectory(coords_frac=coords_frac,
                    cell=cell,
                    symbols=symbols,
                    stress=stress,
                    forces=forces,
                    units={'length': 2, 'forces': 3, 'stress': 4})
    aaae(2*coords, traj.coords)                    
    aaae(3*forces, traj.forces)                    
    aaae(4*stress, traj.stress)                    
    
    # iterate, check if Structures are complete
    traj = Trajectory(coords=coords, 
                      symbols=symbols,
                      cell=cell,
                      forces=forces,
                      stress=stress,
                      etot=etot,
                      timestep=1.0)
    for struct in traj:
        assert struct.is_struct, "st is not Structure"
        assert not struct.is_traj, "st is Trajectory"
        assert_attrs_not_none(struct)
    struct = traj[0]
    for attr_name in traj.attr_lst:
        if attr_name in struct.attrs_only_traj:
            msg = "tr[0] %s is not None" %attr_name
            assert getattr(struct,attr_name) is None, msg
        else:            
            msg = "tr[0] %s is None" %attr_name
            assert getattr(struct,attr_name) is not None, msg
    
    # slices, return traj
    keys = traj.attr_lst[:]
    tsl = traj[10:80:2]
    assert tsl.nstep == traj.nstep / 2 - 15
    assert_attrs_not_none(tsl, attr_lst=keys)
    tsl = traj[slice(10,80,2)]
    assert tsl.nstep == traj.nstep / 2 - 15
    assert_attrs_not_none(tsl, attr_lst=keys)
    tsl = traj[np.s_[10:80:2]]
    assert tsl.nstep == traj.nstep / 2 - 15
    assert_attrs_not_none(tsl, attr_lst=keys)
    assert tsl.is_traj
    
    # iteration over sliced traj
    tsl = traj[10:80:2]
    for x in tsl:
        pass
    for x in tsl.copy():
        pass

    # repeat iter
    for i in range(2):
        cnt = 0
        for st in traj:
            cnt += 1
        assert cnt == nstep, "%i, %i" %(cnt, nstep)    
    
    # copy
    traj2 = traj.copy()
    for name in traj.attr_lst:
        val = getattr(traj,name)
        if val is not None and not (isinstance(val, types.IntType) or \
            isinstance(val, types.FloatType)):
            val2 = getattr(traj2,name)
            print "test copy:", name, type(val), type(val2)
            assert id(val2) != id(val)
            assert_all_types_equal(val2, val)
    assert_dict_with_all_types_equal(traj.__dict__, traj2.__dict__,
                                     keys=traj.attr_lst)
Ejemplo n.º 10
0
def test_tools():
    x1 = {
        'a': 3,
        type(1): 'foo',
        'b': {
            'aa': 1,
            'bb': np.array([1, 2]),
            'cc': {
                'aaa': np.array([1, 2.0]),
                'bbb': np.array([2, 4.0])
            }
        }
    }
    x2 = copy.deepcopy(x1)
    tools.assert_dict_with_all_types_equal(x1, x2)
    tools.assert_all_types_equal(x1, x2)

    # not equal array
    x2['b']['cc']['bbb'] *= 2.0
    assert not tools.all_types_equal(x1, x2)

    # almost equal array and float
    x2 = copy.deepcopy(x1)
    x2['b']['cc']['bbb'] += 1e-5
    x2['b']['aa'] += 1e-5
    tools.assert_all_types_almost_equal(x1, x2)

    # sub-dict different (keys don't match)
    x2 = copy.deepcopy(x1)
    x2['b']['cc']['fff'] = 'extra'
    assert not tools.all_types_equal(x1, x2)

    # test only some keys of a dict
    tools.assert_dict_with_all_types_equal({
        'a': 1,
        'b': 1
    }, {
        'a': 1,
        'b': 3
    },
                                           keys=['a'])

    # simple stuff
    tools.assert_all_types_equal(1, 1)
    tools.assert_all_types_equal(1.0, 1.0)
    tools.assert_all_types_equal(1.0, 1)
    tools.assert_all_types_equal(1, 1.0)
    tools.assert_all_types_equal([1], [1])
    tools.assert_all_types_equal([1], [1.0])
    tools.assert_all_types_equal('a', 'a')
    tools.assert_all_types_almost_equal(1.0, 1.0 + 1e-5)
    tools.assert_all_types_almost_equal(np.array([1.0]),
                                        np.array([1.0 + 1e-5]))
    assert not tools.all_types_equal(1, 2)
    assert not tools.all_types_equal(1.0, 1.1)
    assert not tools.all_types_equal([1], [1, 2])
    assert not tools.all_types_equal('a', 'b')

    # different types not allowed if strict=True
    assert not tools.all_types_equal(1.0, 1, strict=True)

    # test keys=[...], i.e. ignore some keys in both dicts
    x2 = copy.deepcopy(x1)
    x2['c'] = 1.0
    assert tools.dict_with_all_types_equal(x1, x2, keys=['a', 'b', type(1)])

    # fail on same array content but different dimensions
    a = np.random.rand(1, 2, 3)
    b = a[None, ...]
    # this is True and IMHO a numpy bug b/c the dimensions are
    # different
    assert (a == b).all()
    # must catch that here
    assert not tools.array_equal(a, b)

    a = np.random.rand(1, 2, 3)
    b = (a + 1e-8)[None, ...]
    assert not (a == b).all()  # make sure they are numerically differrent
    assert np.allclose(a, b)  # True but should be False
    assert not tools.array_almost_equal(a, b)  # ok

    flt = 1.0
    np_flt = np.array([1.0 + 1e-9])[0]
    assert not tools.all_types_equal(flt, np_flt)
    assert not tools.all_types_equal(np_flt, flt)
    assert tools.all_types_almost_equal(flt, np_flt)
    assert tools.all_types_almost_equal(np_flt, flt)
Ejemplo n.º 11
0
def test_polyfit():
    scale = [{'scale': False}, {'scale': True}]
    levels = [{}, {'levels': 2}]
    degs = [{'deg': 2}, {'degrange': [2, 3, 4, 5]}, {'degmin': 1, 'degmax': 5}]
    kwd_lst = make_kwd_lst(scale, levels, degs)
    for kwds in kwd_lst:
        print(kwds)
        # 1D
        x = np.linspace(-5, 5, 100)
        y = x**2.0 - 1.0
        f = num.PolyFit1D(x, y, **kwds)
        assert np.allclose(y, f(x))
        assert np.allclose(2.0 * x, f(x, der=1))
        assert np.allclose(2.0, f(x, der=2))
        assert np.allclose(-1.0, f(f.get_min()))
        assert np.allclose(-1.0, f(f.get_min(xtol=1e-10)))
        assert np.allclose(-1.0, f(f.get_min(x0=1.0, tol=1e-10)))
        assert np.allclose(-1.0,
                           f(f.get_min(xab=[-1, 1], xtol=1e-10, rtol=1e-14)))

        # API: PolyFit1D __call__ arg: scalar, 1d, 2d
        for xs in [2.0, np.array([2.0]), np.array([[2.0]])]:
            assert np.allclose(3.0, f(xs))
            assert type(np.array([3.0])) == type(np.array([f(xs)]))

        # API: 3rd arg is always 'deg'
        fit1 = num.polyfit(x[:, None], y, 2)
        fit2 = num.polyfit(x[:, None], y, deg=2)
        assert_all_types_equal(fit1, fit2)
        # avgpolyfit() returns a dict "fit1", where fit1['fits'] is a sequence of
        # dicts, that's too much for assert_all_types_equal() :), must fiddle
        # test by hand ...
        fit1 = num.avgpolyfit(x[:, None], y, 2)
        fit2 = num.avgpolyfit(x[:, None], y, deg=2)
        for f1, f2 in zip(fit1['fits'], fit2['fits']):
            assert_all_types_equal(f1, f2)
        keys = list(fit1.keys())
        keys.pop(keys.index('fits'))
        for key in keys:
            assert_all_types_equal(fit1[key], fit2[key])

        # copy self.fit and call avgpolyfit
        y = np.sin(x)
        f = num.PolyFit1D(x, y, **kwds)
        ret = f(x)
        for i in range(10):
            assert (ret == f(x)).all()

    scale = [{'scale': False}, {'scale': True}]
    levels = [{}, {'levels': 1}]
    degs = [{'deg': 4}, {'degrange': [3, 4]}, {'degmin': 2, 'degmax': 5}]
    kwd_lst = make_kwd_lst(scale, levels, degs)
    for kwds in kwd_lst:
        print(kwds)
        # 2D
        x = np.linspace(-5, 6, 20)
        y = np.linspace(-2, 4, 22)
        points = np.array([xy for xy in product(x, y)])
        xx = points[:, 0]
        yy = points[:, 1]
        zz = (xx - 2)**2.0 + (yy - 1)**4.0 - 1.0
        f = num.PolyFit(points, zz, **kwds)
        assert np.allclose(zz, f(points))
        print(f.get_min(xtol=1e-10, ftol=1e-10))
        assert np.allclose(np.array([2.0, 1.0]),
                           f.get_min(xtol=1e-10, ftol=1e-8),
                           atol=1e-3)
        assert np.allclose(-1.0, f(f.get_min()))

        for xs in [np.array([4.0, 3.0]), np.array([[4.0, 3.0]])]:
            assert np.allclose(19.0, f(xs))
            assert type(np.array([19.0])) == type(np.array([f(xs)]))

        # mix terms
        zz = xx**2.0 + yy**2.0 + xx * yy**2.0
        f = num.PolyFit(points, zz, **kwds)
        assert np.allclose(zz, f(points), atol=1e-3)
Ejemplo n.º 12
0
def test_tools():
    x1 = {'a': 3,
          type(1): 'foo',
          'b': {'aa': 1,  
                'bb': np.array([1,2]),
                'cc': {'aaa': np.array([1,2.0]),
                       'bbb': np.array([2,4.0])}}}
    x2 = copy.deepcopy(x1)
    tools.assert_dict_with_all_types_equal(x1, x2)
    tools.assert_all_types_equal(x1, x2)
    
    # not equal array
    x2['b']['cc']['bbb'] *= 2.0
    assert not tools.all_types_equal(x1, x2)
    
    # almost equal array and float
    x2 = copy.deepcopy(x1)
    x2['b']['cc']['bbb'] += 1e-5
    x2['b']['aa'] += 1e-5
    tools.assert_all_types_almost_equal(x1, x2)
    
    # sub-dict different (keys don't match)
    x2 = copy.deepcopy(x1)
    x2['b']['cc']['fff'] = 'extra'
    assert not tools.all_types_equal(x1, x2)
    
    # test only some keys of a dict
    tools.assert_dict_with_all_types_equal({'a':1,'b':1}, {'a':1, 'b':3},
        keys=['a'])

    # simple stuff
    tools.assert_all_types_equal(1, 1)
    tools.assert_all_types_equal(1.0, 1.0)
    tools.assert_all_types_equal(1.0, 1)
    tools.assert_all_types_equal(1, 1.0)
    tools.assert_all_types_equal([1], [1])
    tools.assert_all_types_equal([1], [1.0])
    tools.assert_all_types_equal('a', 'a')
    tools.assert_all_types_almost_equal(1.0, 1.0+1e-5)
    tools.assert_all_types_almost_equal(np.array([1.0]), np.array([1.0+1e-5]))
    assert not tools.all_types_equal(1, 2)
    assert not tools.all_types_equal(1.0, 1.1)
    assert not tools.all_types_equal([1], [1,2])
    assert not tools.all_types_equal('a', 'b')
   
    # different types not allowed if strict=True
    assert not tools.all_types_equal(1.0, 1, strict=True)
    
    # test keys=[...], i.e. ignore some keys in both dicts
    x2 = copy.deepcopy(x1)
    x2['c'] = 1.0
    assert tools.dict_with_all_types_equal(x1, x2, keys=['a','b',type(1)])

    # fail on same array content but different dimensions
    a = np.random.rand(1,2,3)
    b = a[None,...]
    # this is True and IMHO a numpy bug b/c the dimensions are
    # different
    assert (a==b).all()
    # must catch that here
    assert not tools.array_equal(a,b)

    a = np.random.rand(1,2,3)
    b = (a + 1e-8)[None,...]
    assert not (a==b).all() # make sure they are numerically differrent
    assert np.allclose(a,b) # True but should be False
    assert not tools.array_almost_equal(a,b) # ok
Ejemplo n.º 13
0
def test_traj():
    natoms = 10
    nstep = 100
    cell = rand(nstep, 3, 3)
    stress = rand(nstep, 3, 3)
    forces = rand(nstep, natoms, 3)
    etot = rand(nstep)
    cryst_const = crys.cell2cc3d(cell, axis=0)
    coords_frac = rand(nstep, natoms, 3)
    coords = crys.coord_trans3d(coords=coords_frac,
                                old=cell,
                                new=num.extend_array(np.identity(3),
                                                     nstep,
                                                     axis=0),
                                axis=1,
                                timeaxis=0)
    assert cryst_const.shape == (nstep, 6)
    assert coords.shape == (nstep, natoms, 3)
    symbols = ['H'] * natoms

    # automatically calculated:
    #   coords
    #   cell
    #   pressure
    #   velocity (from coords)
    #   temperature (from ekin)
    #   ekin (from velocity)
    traj = Trajectory(
        coords_frac=coords_frac,
        cell=cell,
        symbols=symbols,
        forces=forces,
        stress=stress,
        etot=etot,
        timestep=1,
    )
    # Test if all getters work.
    for name in traj.attr_lst:
        print("test if getters work:", name)
        traj.try_set_attr(name)
        assert getattr(traj, name) is not None, "attr None: %s" % name
        assert eval('traj.get_%s()' %
                    name) is not None, "getter returns None: %s" % name
        print("test if getters work:", name, "... ok")
    aaae(coords_frac, traj.coords_frac)
    aaae(coords, traj.coords)
    aaae(cryst_const, traj.cryst_const)
    aaae(np.trace(stress, axis1=1, axis2=2) / 3.0, traj.pressure)
    assert traj.coords.shape == (nstep, natoms, 3)
    assert traj.cell.shape == (nstep, 3, 3)
    assert traj.velocity.shape == (nstep, natoms, 3)
    assert traj.temperature.shape == (nstep, )
    assert traj.ekin.shape == (nstep, )
    assert traj.nstep == nstep
    assert traj.natoms == natoms

    traj = Trajectory(coords_frac=coords_frac, symbols=symbols, cell=cell)
    aaae(coords, traj.coords)

    # Cell calculated from cryst_const has defined orientation in space which may be
    # different from the original `cell`, but the volume and underlying cryst_const
    # must be the same.
    traj = Trajectory(coords_frac=coords_frac,
                      symbols=symbols,
                      cryst_const=cryst_const)
    np.testing.assert_almost_equal(crys.volume_cell3d(cell),
                                   crys.volume_cell3d(traj.cell))
    aaae(cryst_const, crys.cell2cc3d(traj.cell))

    # extend arrays
    cell2d = rand(3, 3)
    cc2d = crys.cell2cc(cell2d)
    traj = Trajectory(coords_frac=coords_frac, cell=cell2d, symbols=symbols)
    assert traj.cell.shape == (nstep, 3, 3)
    assert traj.cryst_const.shape == (nstep, 6)
    for ii in range(traj.nstep):
        assert (traj.cell[ii, ...] == cell2d).all()
        assert (traj.cryst_const[ii, :] == cc2d).all()

    traj = Trajectory(coords_frac=coords_frac,
                      cryst_const=cc2d,
                      symbols=symbols)
    assert traj.cell.shape == (nstep, 3, 3)
    assert traj.cryst_const.shape == (nstep, 6)
    for ii in range(traj.nstep):
        assert (traj.cryst_const[ii, :] == cc2d).all()

    # units
    traj = Trajectory(coords_frac=coords_frac,
                      cell=cell,
                      symbols=symbols,
                      stress=stress,
                      forces=forces,
                      units={
                          'length': 2,
                          'forces': 3,
                          'stress': 4
                      })
    aaae(2 * coords, traj.coords)
    aaae(3 * forces, traj.forces)
    aaae(4 * stress, traj.stress)

    # iterate, check if Structures are complete
    traj = Trajectory(coords=coords,
                      symbols=symbols,
                      cell=cell,
                      forces=forces,
                      stress=stress,
                      etot=etot,
                      timestep=1.0)
    for struct in traj:
        assert struct.is_struct, "st is not Structure"
        assert not struct.is_traj, "st is Trajectory"
        assert_attrs_not_none(struct)
    struct = traj[0]
    for attr_name in traj.attr_lst:
        if attr_name in struct.attrs_only_traj:
            msg = "tr[0] %s is not None" % attr_name
            assert getattr(struct, attr_name) is None, msg
        else:
            msg = "tr[0] %s is None" % attr_name
            assert getattr(struct, attr_name) is not None, msg

    # slices, return traj
    keys = traj.attr_lst[:]
    tsl = traj[10:80:2]
    assert tsl.nstep == traj.nstep / 2 - 15
    assert_attrs_not_none(tsl, attr_lst=keys)
    tsl = traj[slice(10, 80, 2)]
    assert tsl.nstep == traj.nstep / 2 - 15
    assert_attrs_not_none(tsl, attr_lst=keys)
    tsl = traj[np.s_[10:80:2]]
    assert tsl.nstep == traj.nstep / 2 - 15
    assert_attrs_not_none(tsl, attr_lst=keys)
    assert tsl.is_traj

    # iteration over sliced traj
    tsl = traj[10:80:2]
    for x in tsl:
        pass
    for x in tsl.copy():
        pass

    # repeat iter
    for i in range(2):
        cnt = 0
        for st in traj:
            cnt += 1
        assert cnt == nstep, "%i, %i" % (cnt, nstep)

    # copy
    traj2 = traj.copy()
    for name in traj.attr_lst:
        val = getattr(traj, name)
        if val is not None and not (isinstance(val, int) or \
            isinstance(val, float)):
            val2 = getattr(traj2, name)
            print("test copy:", name, type(val), type(val2))
            assert id(val2) != id(val)
            assert_all_types_equal(val2, val)
    assert_dict_with_all_types_equal(traj.__dict__,
                                     traj2.__dict__,
                                     keys=traj.attr_lst)
Ejemplo n.º 14
0
def test_polyfit():
    scale = [{'scale': False}, {'scale': True}]
    levels = [{}, {'levels': 2}]
    degs = [{'deg': 2}, {'degrange': [2,3,4,5]}, 
            {'degmin': 1, 'degmax': 5}]
    kwd_lst = make_kwd_lst(scale, levels, degs)        
    for kwds in kwd_lst:
        print kwds
        # 1D
        x = np.linspace(-5,5,100) 
        y = x**2.0 - 1.0
        f = num.PolyFit1D(x,y,**kwds)
        assert np.allclose(y, f(x))
        assert np.allclose(2.0*x, f(x, der=1))
        assert np.allclose(2.0, f(x, der=2))
        assert np.allclose(-1.0, f(f.get_min()))
        assert np.allclose(-1.0, f(f.get_min(xtol=1e-10)))                         
        assert np.allclose(-1.0, f(f.get_min(x0=1.0, tol=1e-10)))                  
        assert np.allclose(-1.0, f(f.get_min(xab=[-1,1], xtol=1e-10, rtol=1e-14))) 
        
        # API: PolyFit1D __call__ arg: scalar, 1d, 2d
        for xs in [2.0, np.array([2.0]), np.array([[2.0]])]:
            assert np.allclose(3.0, f(xs))
            assert type(np.array([3.0])) == type(np.array([f(xs)]))
        
        # API: 3rd arg is always 'deg'
        fit1 = num.polyfit(x[:,None],y,2)
        fit2 = num.polyfit(x[:,None],y,deg=2)
        assert_all_types_equal(fit1, fit2)
        # avgpolyfit() returns a dict "fit1", where fit1['fits'] is a sequence of
        # dicts, that's too much for assert_all_types_equal() :), must fiddle
        # test by hand ...
        fit1 = num.avgpolyfit(x[:,None],y,2)
        fit2 = num.avgpolyfit(x[:,None],y,deg=2)
        for f1,f2 in zip(fit1['fits'], fit2['fits']):
            assert_all_types_equal(f1, f2)
        keys = fit1.keys()
        keys.pop(keys.index('fits'))
        for key in keys:
            assert_all_types_equal(fit1[key], fit2[key])


        # copy self.fit and call avgpolyfit
        y = np.sin(x)
        f = num.PolyFit1D(x,y,**kwds)
        ret = f(x)
        for i in range(10):
            assert (ret == f(x)).all()
    
    
    scale = [{'scale': False}, {'scale': True}]
    levels = [{}, {'levels': 1}]
    degs = [{'deg': 4}, {'degrange': [3,4]}, 
            {'degmin': 2, 'degmax': 5}]
    kwd_lst = make_kwd_lst(scale, levels, degs)        
    for kwds in kwd_lst:
        print kwds
        # 2D
        x = np.linspace(-5,6,20)
        y = np.linspace(-2,4,22)
        points = np.array([xy for xy in product(x,y)])
        xx = points[:,0]
        yy = points[:,1]
        zz = (xx-2)**2.0 + (yy-1)**4.0 - 1.0
        f = num.PolyFit(points, zz, **kwds)
        assert np.allclose(zz, f(points))
        print f.get_min(xtol=1e-10, ftol=1e-10)
        assert np.allclose(np.array([2.0, 1.0]), f.get_min(xtol=1e-10, ftol=1e-8), atol=1e-3)
        assert np.allclose(-1.0, f(f.get_min()))

        for xs in [np.array([4.0,3.0]), np.array([[4.0,3.0]])]:
            assert np.allclose(19.0, f(xs))
            assert type(np.array([19.0])) == type(np.array([f(xs)]))
        
        # mix terms
        zz = xx**2.0 + yy**2.0 + xx*yy**2.0
        f = num.PolyFit(points, zz, **kwds)
        assert np.allclose(zz, f(points),atol=1e-3)
Ejemplo n.º 15
0
def test_struct():
    natoms = 10
    cell = np.array([[3,0,0],
                     [1.1,5,-0.04],
                     [-0.33,1.5,7]])
    cryst_const = crys.cell2cc(cell)                 
    coords_frac = rand(natoms,3)
    coords = crys.coord_trans(coords=coords_frac,
                              old=cell,
                              new=np.identity(3))
    symbols = ['H']*natoms
    stress = rand(3,3)
    forces = rand(natoms,3)

    # Use ``cell`` instead of ``cryst_const` as input such that
    # atoms.get_cell() test passes (see below for why -- cell orientation)
    st = Structure(coords_frac=coords_frac,
                   symbols=symbols,
                   cell=cell,
                   stress=stress,
                   forces=forces,
                   etot=42)
    
    # Test if all getters work.
    for name in st.attr_lst:
        print name
        st.try_set_attr(name)
        assert getattr(st, name) is not None, "attr None: %s" %name
        assert eval('st.get_%s()'%name) is not None, "getter returns None: %s" %name
    aaae(coords_frac, st.coords_frac)
    aaae(cryst_const, st.cryst_const)
    aaae(coords, st.coords)
    assert st.natoms == natoms

    st = Structure(coords_frac=coords_frac,
                   symbols=symbols,
                   cell=cell)
    aaae(coords, st.get_coords())

    # Cell calculated from cryst_const has defined orientation in space which may be
    # different from the original `cell`, but the volume and underlying cryst_const
    # must be the same.
    st = Structure(coords_frac=coords_frac,
                   symbols=symbols,
                   cryst_const=cryst_const)
    assert st.get_cell() is not None
    np.testing.assert_almost_equal(crys.volume_cell(cell),
                                   crys.volume_cell(st.get_cell()))
    aaae(cryst_const, crys.cell2cc(st.get_cell()))

    # units
    st = Structure(coords_frac=coords_frac,
                    cell=cell,
                    symbols=symbols,
                    stress=stress,
                    forces=forces,
                    units={'length': 2, 'forces': 3, 'stress': 4})
    aaae(2*coords, st.coords)                    
    aaae(3*forces, st.forces)                    
    aaae(4*stress, st.stress)                    
    
    traj = crys.struct2traj(st)
    assert traj.is_traj

    # copy(): Assert everything has another memory location = is a new copy of
    # the object. IntTypes are NOT copied by copy.deepcopy(), which we use in
    # Structure.copy(), apparently b/c they are always automatically copied
    # before in-place operations. Same for float type. 
    #
    # >>> a=10; b=a; print id(a); print id(b)
    # 36669152
    # 36669152
    # >>> a*=100; print id(a); print id(b)
    # 72538264
    # 36669152
    # >>> a
    # 100
    # >>> b
    # 10
    #
    # >>> a=[1,2,3]; b=a; print id(a); print id(b)
    # 72624320
    # 72624320
    # >>> a[0] = 44; print id(a); print id(b)
    # 72624320
    # 72624320
    # >>> a
    # [44, 2, 3]
    # >>> b
    # [44, 2, 3]
    st2 = st.copy()
    for name in st.attr_lst:
        val = getattr(st,name)
        if val is not None and not (isinstance(val, types.IntType) or \
            isinstance(val, types.FloatType)):
            val2 = getattr(st2,name)
            assert id(val2) != id(val)
            assert_all_types_equal(val2, val)