Exemple #1
0
def test_read_csv_raises(tmp_path):
    """Test various exception raising circumstances with read_csv."""
    temp = tmp_path / 'points.csv'

    # test that points data is detected with require_type = None, any, points
    # but raises for other shape types.
    data = [['index', 'axis-0', 'axis-1']]
    data.extend(np.random.random((3, 3)).tolist())
    with open(temp, mode='w', newline='') as csvfile:
        csv.writer(csvfile).writerows(data)
    assert io.read_csv(temp, require_type=None)[2] == 'points'
    assert io.read_csv(temp, require_type='any')[2] == 'points'
    assert io.read_csv(temp, require_type='points')[2] == 'points'
    with pytest.raises(ValueError):
        io.read_csv(temp, require_type='shapes')

    # test that unrecognized data is detected with require_type = None
    # but raises for specific shape types or "any"
    data = [['some', 'random', 'header']]
    data.extend(np.random.random((3, 3)).tolist())
    with open(temp, mode='w', newline='') as csvfile:
        csv.writer(csvfile).writerows(data)
    assert io.read_csv(temp, require_type=None)[2] is None
    with pytest.raises(ValueError):
        assert io.read_csv(temp, require_type='any')
    with pytest.raises(ValueError):
        assert io.read_csv(temp, require_type='points')
    with pytest.raises(ValueError):
        io.read_csv(temp, require_type='shapes')
Exemple #2
0
 def reader(path):
     return (
         io.read_csv(path)[0][:, 1:3],
         {
             'properties': {
                 io.read_csv(path)[1][3]: io.read_csv(path)[0][:, 3]
             }
         },
     )
Exemple #3
0
def test_read_csv(tmpdir):
    expected_filename = os.path.join(tmpdir, 'test.csv')
    column_names = ['column_1', 'column_2', 'column_3']
    expected_data = np.random.random((5, len(column_names)))

    # Write csv file
    io.write_csv(expected_filename, expected_data, column_names=column_names)
    assert os.path.exists(expected_filename)

    # Read csv file
    read_data, read_column_names, _ = io.read_csv(expected_filename)
    read_data = np.array(read_data).astype('float')
    np.testing.assert_allclose(expected_data, read_data)

    assert column_names == read_column_names
Exemple #4
0
 def reader(path):
     return (
         io.read_csv(path)[0][:, 1:3],
         {},  # metadata
     )