示例#1
0
def test_new_id():
    id1 = TrajectoryData.new_id()
    assert id1 == str(uuid.UUID(id1))
    id2 = TrajectoryData.new_id()
    id3 = TrajectoryData.new_id('foo')
    assert (id1 != id2 != id3)
    id4 = TrajectoryData.new_id('foo')
    assert (id3 == id4)
    id5 = TrajectoryData.new_id('bar')
    assert (id5 != id4)
示例#2
0
 def proto_fixture(datadir):
     operators = OrderedDict()
     datafiles = sorted(glob(os.path.join(datadir, folder, '*.out')))
     assert len(datafiles) > 0, "No files *.out in %s" % folder
     for file in datafiles:
         op_name = os.path.splitext(os.path.split(file)[1])[0]
         operators[op_name] = file
     return TrajectoryData.from_qsd_data(operators, seed=seed)
示例#3
0
def test_parse_header_line():
    line = '#          t    Re[<X1>]    Im[<X1>] Re[var(X1)] Im[var(X1)]    Re[<X2>]    Im[<X2>] Re[var(X2)] Im[var(X2)]'
    fields = TrajectoryData._parse_header_line(line)
    assert fields == [
        '#          t', '    Re[<X1>]', '    Im[<X1>]', ' Re[var(X1)]',
        ' Im[var(X1)]', '    Re[<X2>]', '    Im[<X2>]', ' Re[var(X2)]',
        ' Im[var(X2)]'
    ]
    fields = TrajectoryData._parse_header_line(line, strip=True)
    assert fields == [
        '#          t', 'Re[<X1>]', 'Im[<X1>]', 'Re[var(X1)]', 'Im[var(X1)]',
        'Re[<X2>]', 'Im[<X2>]', 'Re[var(X2)]', 'Im[var(X2)]'
    ]
    line = '#tRe[<X1>]Im[<X1>]'
    fields = TrajectoryData._parse_header_line(line)
    assert fields == ['#t', 'Re[<X1>]', 'Im[<X1>]']
    line = '# t Re[<A[1]>] Im[<A[1]>]'
    fields = TrajectoryData._parse_header_line(line, strip=True)
    assert fields == ['# t', 'Re[<A[1]>]', 'Im[<A[1]>]']
    with pytest.raises(TrajectoryParserError) as excinfo:
        line = '# t Re[<A]>] Im[<A]>]'
        fields = TrajectoryData._parse_header_line(line, strip=True)
    assert "unbalanced brackets" in str(excinfo.value)
    with pytest.raises(TrajectoryParserError) as excinfo:
        line = '# t Re[<A[1]>] Im[<A[1]>] X1 X2'
        fields = TrajectoryData._parse_header_line(line, strip=True)
    assert "trailing characters" in str(excinfo.value)
示例#4
0
def test_get_op_names_from_header_line(traj1):
    # use of traj1 is arbitrary, we just need some instance
    line = '#          t    Re[<X1>]    Im[<X1>] Re[var(X1)] Im[var(X1)]    Re[<X2>]    Im[<X2>] Re[var(X2)] Im[var(X2)]'
    ops = traj1._get_op_names_from_header_line(line)
    assert ops == ['X1', 'X2']

    op = 'X' * 20
    line = '#  t ' + " ".join(TrajectoryData._operator_cols(op))
    ops = traj1._get_op_names_from_header_line(line)
    assert ops == [
        op,
    ]

    with pytest.raises(TrajectoryParserError) as excinfo:
        line = '#          t    Re[<X1>]    Im[<X1>] Re[var(X1)]'
        ops = traj1._get_op_names_from_header_line(line)
    assert "Unexpected number of columns" in str(excinfo.value)
    with pytest.raises(TrajectoryParserError) as excinfo:
        line = '#  t    Re[<X\t2>]'
        ops = traj1._get_op_names_from_header_line(line)
    assert "contains invalid characters" in str(excinfo.value)
示例#5
0
def test_from_qsd_data(traj1, datadir):
    X1_file = join(datadir, 'traj1', 'X1.out')
    X2_file = join(datadir, 'traj1', 'X2.out')
    traj2 = TrajectoryData.from_qsd_data(
        OrderedDict([('X2', X2_file), ('X1', X1_file)]), TRAJ1_SEED)
    # traj1 and traj2 are the same except that the order of the operators is
    # reversed => files are read in a differnt order
    assert traj1.ID == traj2.ID

    traj = traj1
    md5sum = lambda f: hashlib.md5(open(f, 'rb').read()).hexdigest()
    md5 = "".join(sorted([md5sum(X1_file), md5sum(X2_file)]))
    assert traj.ID == str(uuid.uuid3(TrajectoryData._uuid_namespace, md5))
    assert traj.dt == 0.1
    assert traj.nt == 51
    assert traj.shape == (51, 9)
    assert len(traj.record) == 1
    assert list(traj.operators) == ['X1', 'X2']
    for (id, (seed, n_traj, op_list)) in traj.record.items():
        assert seed == TRAJ1_SEED
        assert n_traj == 1
        assert op_list == ['X1', 'X2']
    assert traj.record_seeds == set([
        TRAJ1_SEED,
    ])
    assert traj.record_IDs == set([
        traj.ID,
    ])
    assert list(traj.table.keys()) == [
        'Re[<X1>]', 'Im[<X1>]', 'Re[var(X1)]', 'Im[var(X1)]', 'Re[<X2>]',
        'Im[<X2>]', 'Re[var(X2)]', 'Im[var(X2)]'
    ]
    fname = join(datadir, 'traj1', 'X1.out')
    (tgrid, re_exp, im_exp, re_var, im_var) \
    = np.genfromtxt(fname, dtype=np.float64, skip_header=1, unpack=True)
    assert np.max(np.abs(re_exp - traj.table['Re[<X1>]'])) < 1.0e-15
    assert np.max(np.abs(im_exp - traj.table['Im[<X1>]'])) < 1.0e-15
    assert np.max(np.abs(re_var - traj.table['Re[var(X1)]'])) < 1.0e-15
    assert np.max(np.abs(im_var - traj.table['Im[var(X1)]'])) < 1.0e-15
示例#6
0
def test_read(datadir, tmpdir, traj1, traj2_10, traj11_20):
    filename = join(datadir, 'read_input_files', 'good.dat')
    traj = TrajectoryData.read(filename)
    file_str = open(filename).read()
    assert str(traj) == file_str.rstrip()

    filename = str(tmpdir.join("traj1.dat"))
    traj1.write(filename)
    traj_r = TrajectoryData.read(filename)
    assert str(traj_r) == str(traj1)

    filename = str(tmpdir.join("traj1-20.dat"))
    traj = traj11_20 + traj2_10 + traj1
    traj.col_width = 12
    traj.write(filename)
    traj_r = TrajectoryData.read(filename)
    assert str(traj_r) == str(traj)

    with pytest.raises(TrajectoryParserError) as exc_info:
        filename = join(datadir, 'read_input_files', 'no_header.dat')
        traj = TrajectoryData.read(filename)
    assert "does not define an ID" in str(exc_info.value)

    with pytest.raises(TrajectoryParserError) as exc_info:
        filename = join(datadir, 'read_input_files', 'no_record.dat')
        traj = TrajectoryData.read(filename)
    assert "does not contain a record" in str(exc_info.value)

    with pytest.raises(TrajectoryParserError) as exc_info:
        filename = join(datadir, 'read_input_files', 'no_col_labels.dat')
        traj = TrajectoryData.read(filename)
    assert "does not contain a header" in str(exc_info.value)

    with pytest.raises(TrajectoryParserError) as exc_info:
        filename = join(datadir, 'read_input_files', 'no_data.dat')
        traj = TrajectoryData.read(filename)
    assert "contains no data" in str(exc_info.value)

    with pytest.raises(TrajectoryParserError) as exc_info:
        filename = join(datadir, 'read_input_files', 'empty.dat')
        traj = TrajectoryData.read(filename)
    assert "does not define an ID" in str(exc_info.value)

    with pytest.raises(TrajectoryParserError) as exc_info:
        filename = join(datadir, 'read_input_files', 'wrong_col_labels.dat')
        traj = TrajectoryData.read(filename)
    assert "Malformed header" in str(exc_info.value)

    with pytest.raises(TrajectoryParserError) as exc_info:
        filename = join(datadir, 'read_input_files', 'wrong_col_labels2.dat')
        traj = TrajectoryData.read(filename)
    assert "Invalid header line" in str(exc_info.value)

    with pytest.raises(TrajectoryParserError) as exc_info:
        filename = join(datadir, 'read_input_files', 'no_tgrid.dat')
        traj = TrajectoryData.read(filename)
    assert "missing time grid" in str(exc_info.value)

    with pytest.raises(TrajectoryParserError) as exc_info:
        filename = join(datadir, 'read_input_files', 'missing_im.dat')
        traj = TrajectoryData.read(filename)
    assert "Unexpected number of columns" in str(exc_info.value)

    with pytest.raises(TrajectoryParserError) as exc_info:
        filename = join(datadir, 'read_input_files',
                        'header_data_mismatch.dat')
        traj = TrajectoryData.read(filename)
    assert "number of data columns differs from the number indicated in "\
           "the header" in str(exc_info.value)

    with pytest.raises(TrajectoryParserError) as exc_info:
        filename = join(datadir, 'read_input_files', 'single_row.dat')
        traj = TrajectoryData.read(filename)
    assert "Too few rows" in str(exc_info.value)

    with pytest.raises(TrajectoryParserError) as exc_info:
        filename = join(datadir, 'read_input_files', 'truncated.dat')
        traj = TrajectoryData.read(filename)
    assert "Wrong number of columns at line 3" in str(exc_info.value)
示例#7
0
def test_init_validation():
    dt_ok = 0.1
    ID_ok = 'd9831647-f2e7-3793-8b24-7c49c5c101a7'
    data_ok = OrderedDict([
        ('X', (np.zeros(3), np.zeros(3), np.zeros(3), np.zeros(3))),
        ('Y', (np.zeros(3), np.zeros(3), np.zeros(3), np.zeros(3)))
    ])
    TrajectoryData(ID=ID_ok,
                   dt=dt_ok,
                   seed=None,
                   n_trajectories=None,
                   data=data_ok)
    with pytest.raises(ValueError) as excinfo:
        TrajectoryData(ID='1232',
                       dt=dt_ok,
                       seed=None,
                       n_trajectories=None,
                       data=data_ok)
    assert 'badly formed hexadecimal UUID string' in str(excinfo.value)
    with pytest.raises(ValueError) as excinfo:
        TrajectoryData(ID=ID_ok,
                       dt=0.0,
                       seed=None,
                       n_trajectories=None,
                       data=data_ok)
    assert 'dt must be a value >0' in str(excinfo.value)
    with pytest.raises(ValueError) as excinfo:
        TrajectoryData(ID=ID_ok,
                       dt="bla",
                       seed=None,
                       n_trajectories=None,
                       data=data_ok)
    assert 'could not convert string' in str(excinfo.value)
    with pytest.raises(ValueError) as excinfo:
        TrajectoryData(ID=ID_ok,
                       dt=None,
                       seed=None,
                       n_trajectories=None,
                       data=data_ok)
    assert 'dt must be a float with value >0' in str(excinfo.value)
    with pytest.raises(AttributeError) as excinfo:
        TrajectoryData(ID=ID_ok,
                       dt=dt_ok,
                       seed=None,
                       n_trajectories=None,
                       data=None)
    with pytest.raises(ValueError) as excinfo:
        data = OrderedDict([('X', (np.zeros(3), np.zeros(3), np.zeros(3))),
                            ('Y', (np.zeros(3), np.zeros(3), np.zeros(3)))])
        TrajectoryData(ID=ID_ok,
                       dt=dt_ok,
                       seed=None,
                       n_trajectories=None,
                       data=data)
    assert 'values to unpack' in str(excinfo.value)
    with pytest.raises(ValueError) as excinfo:
        data = OrderedDict([('X', ("a", "b", "c", "d")),
                            ('Y', ("a", "b", "c", "d"))])
        TrajectoryData(ID=ID_ok,
                       dt=dt_ok,
                       seed=None,
                       n_trajectories=None,
                       data=data)
    assert 'could not convert string to float' in str(excinfo.value)
    assert TrajectoryData.col_width == 25
    data = OrderedDict([
        ('Y' * 20, data_ok['X']),
    ])
    traj = TrajectoryData(ID=ID_ok,
                          dt=dt_ok,
                          seed=None,
                          n_trajectories=None,
                          data=data)
    assert traj.col_width == 20 + TrajectoryData._col_padding
    with pytest.raises(ValueError) as excinfo:
        data = OrderedDict([
            ('a\tb', data_ok['X']),
        ])
        TrajectoryData(ID=ID_ok,
                       dt=dt_ok,
                       seed=None,
                       n_trajectories=None,
                       data=data)
    assert 'contains invalid characters' in str(excinfo.value)
    with pytest.raises(ValueError) as excinfo:
        data = OrderedDict([
            ('A[[2]', data_ok['X']),
        ])
        TrajectoryData(ID=ID_ok,
                       dt=dt_ok,
                       seed=None,
                       n_trajectories=None,
                       data=data)
    assert 'contains unbalanced brackets' in str(excinfo.value)
    with pytest.raises(ValueError) as excinfo:
        data = OrderedDict([
            ('A]', data_ok['X']),
        ])
        TrajectoryData(ID=ID_ok,
                       dt=dt_ok,
                       seed=None,
                       n_trajectories=None,
                       data=data)
    assert 'contains unbalanced brackets' in str(excinfo.value)