Ejemplo n.º 1
0
def test_memory_storage():
    """test methods specific to memory storage"""
    sf = ScalarField(UnitGrid([1]))
    s1 = MemoryStorage()
    s1.start_writing(sf)
    sf.data = 0
    s1.append(sf, 0)
    sf.data = 2
    s1.append(sf, 1)

    s2 = MemoryStorage()
    s2.start_writing(sf)
    sf.data = 1
    s2.append(sf, 0)
    sf.data = 3
    s2.append(sf, 1)

    # test from_fields
    s3 = MemoryStorage.from_fields(s1.times, [s1[0], s1[1]])
    assert s3.times == s1.times
    np.testing.assert_allclose(s3.data, s1.data)

    # test from_collection
    s3 = MemoryStorage.from_collection([s1, s2])
    assert s3.times == s1.times
    np.testing.assert_allclose(np.ravel(s3.data), np.arange(4))
Ejemplo n.º 2
0
def test_storage_copy(tmp_path):
    """test the copy function of StorageBase"""
    grid = UnitGrid([2])
    field = ScalarField(grid)

    storage_classes = {"None": None, "MemoryStorage": MemoryStorage}
    if module_available("h5py"):
        file_path = tmp_path / "test_storage_apply.hdf5"
        storage_classes["FileStorage"] = functools.partial(
            FileStorage, file_path)

    s1 = MemoryStorage()
    s1.start_writing(field, info={"b": 2})
    field.data = np.array([0, 1])
    s1.append(field, 0)
    field.data = np.array([1, 2])
    s1.append(field, 1)
    s1.end_writing()

    for name, storage_cls in storage_classes.items():
        out = None if storage_cls is None else storage_cls()
        s2 = s1.copy(out=out)
        assert storage_cls is None or s2 is out
        assert len(s2) == 2
        np.testing.assert_allclose(s2.times, s1.times)
        assert s2[0] == s1[0], name
        assert s2[1] == s1[1], name

    # test empty storage
    s1 = MemoryStorage()
    s2 = s1.copy()
    assert len(s2) == 0
Ejemplo n.º 3
0
def test_storage_write(tmp_path):
    """test simple memory storage"""
    dim = 5
    grid = UnitGrid([dim])
    field = ScalarField(grid)

    storage_classes = {"MemoryStorage": MemoryStorage}
    if module_available("h5py"):
        file_path = tmp_path / "test_storage_write.hdf5"
        storage_classes["FileStorage"] = functools.partial(
            FileStorage, file_path)

    for name, storage_cls in storage_classes.items():
        storage = storage_cls(info={"a": 1})
        storage.start_writing(field, info={"b": 2})
        field.data = np.arange(dim)
        storage.append(field, 0)
        field.data = np.arange(dim)
        storage.append(field, 1)
        storage.end_writing()

        assert not storage.has_collection

        np.testing.assert_allclose(storage.times, np.arange(2))
        for f in storage:
            np.testing.assert_array_equal(f.data, np.arange(dim))
        for i in range(2):
            np.testing.assert_array_equal(storage[i].data, np.arange(dim))
        assert {"a": 1, "b": 2}.items() <= storage.info.items()

        storage = storage_cls()
        storage.clear()
        for i in range(3):
            storage.start_writing(field)
            field.data = np.arange(dim) + i
            storage.append(field, i)
            storage.end_writing()

        np.testing.assert_allclose(storage.times,
                                   np.arange(3),
                                   err_msg="storage class: " + name)
Ejemplo n.º 4
0
def test_storing_extract_range(tmp_path):
    """test methods specific to FieldCollections in memory storage"""
    sf = ScalarField(UnitGrid([1]))

    storage_classes = {"MemoryStorage": MemoryStorage}
    if module_available("h5py"):
        file_path = tmp_path / "test_storage_write.hdf5"
        storage_classes["FileStorage"] = functools.partial(
            FileStorage, file_path)

    for storage_cls in storage_classes.values():
        # store some data
        s1 = storage_cls()
        s1.start_writing(sf)
        sf.data = np.array([0])
        s1.append(sf, 0)
        sf.data = np.array([2])
        s1.append(sf, 1)
        s1.end_writing()

        np.testing.assert_equal(s1[0].data, 0)
        np.testing.assert_equal(s1[1].data, 2)
        np.testing.assert_equal(s1[-1].data, 2)
        np.testing.assert_equal(s1[-2].data, 0)

        with pytest.raises(IndexError):
            s1[2]
        with pytest.raises(IndexError):
            s1[-3]

        # test extraction
        s2 = s1.extract_time_range()
        assert s2.times == list(s1.times)
        np.testing.assert_allclose(s2.data, s1.data)
        s3 = s1.extract_time_range(0.5)
        assert s3.times == s1.times[:1]
        np.testing.assert_allclose(s3.data, s1.data[:1])
        s4 = s1.extract_time_range((0.5, 1.5))
        assert s4.times == s1.times[1:]
        np.testing.assert_allclose(s4.data, s1.data[1:])
Ejemplo n.º 5
0
def test_kymograph_single(tmp_path):
    """test making kymographs for single fields"""
    # create some storage
    field = ScalarField(UnitGrid(8))
    with get_memory_storage(field) as storage:
        for i in range(8):
            field.data = i
            storage.append(field, i)

    # create single kymograph
    path = tmp_path / "test1.png"
    plotting.plot_kymograph(storage,
                            colorbar=True,
                            transpose=True,
                            filename=path)
    assert path.stat().st_size > 0

    # create multiple kymographs
    path = tmp_path / "test2.png"
    plotting.plot_kymographs(storage, filename=path)
    assert path.stat().st_size > 0