Ejemplo n.º 1
0
def _TestMultiBackend():
    """Fixture for returning multiple backends"""
    class MultiBackend:
        """Test backend for MultiBackend

        * simple backend to provide data based on the path
        * `MultiBackend.*` triggers this backend but not `MultiBackend.*.*`
        * `iteration` is based on suffix `*.42` means iteration is `42`
        * `time_step` is based on iteration and is `float(iteration * 10)`
        """

        name = "MultiTestBackend"
        location = None

        dataset_name = "test_dataset_name"
        dataset_label = "test dataset label"
        dataset_unit = "test dataset unit"

        axes_names = ("axes0", "axes1", "axes2")
        axes_labels = ("axes 0", "axes 1", "axes 3")
        axes_units = ("axes unit 0", "axes unit 1", "axes unit 2")
        axes_min = np.array([-1.0, -2.0, -3.0])
        axes_max = np.array([0.5, 1.5, 2.5])

        iteration = None
        time_step = None
        time_unit = "time unit"

        shape = (4, 5, 6)
        dtype = np.float

        ndim = 3

        def __init__(
            self,
            location: Optional[FileLocation] = None,
            raise_on_read: bool = True,
        ):
            self.location = location
            self._raise_on_read = raise_on_read
            self.iteration = int(Path(location).suffix.strip("."))
            self.time_step = float(self.iteration * 10)

        @staticmethod
        def is_valid_backend(location: Union[Path, str]) -> bool:
            return Path(location).stem == "MultiGridBackend"

        def get_data(self,
                     indexing: Optional[BasicIndexing] = None) -> np.ndarray:
            data = np.arange(4 * 5 * 6).reshape(self.shape)
            return data[indexing] if indexing else data

    # ensure dummy backend is of valid type
    assert isinstance(MultiBackend, GridBackendType)

    GridDataset.add_backend(MultiBackend)
    yield MultiBackend
    # teardown
    GridDataset.remove_backend(MultiBackend)
Ejemplo n.º 2
0
def _TestGridBackend():
    """Fixture for returning GridBackend"""
    class TestGridBackend:
        """Test backend for GridDatsets

        * Backend provides 3D random data.
        """

        name = "TetsGridBackend"
        location = None

        dataset_name = "test_dataset_name"
        dataset_label = "test dataset label"
        dataset_unit = "test dataset unit"

        axes_names = ("axes0", "axes1", "axes2")
        axes_labels = ("axes 0", "axes 1", "axes 3")
        axes_units = ("axes unit 0", "axes unit 1", "axes unit 2")
        axes_min = np.array([-1.0, -2.0, -3.0])
        axes_max = np.array([0.5, 1.5, 2.5])

        iteration = 42

        time_step = 12.3
        time_unit = "time unit"

        shape = (4, 5, 6)
        dtype = np.float

        ndim = 3

        def __init__(
            self,
            location: Optional[FileLocation] = None,
            raise_on_read: bool = True,
        ):
            self.location = location
            self._raise_on_read = raise_on_read

        @staticmethod
        def is_valid_backend(location: Union[Path, str]) -> bool:
            return Path(location) == Path("TestGridBackend_location")

        def get_data(self,
                     indexing: Optional[BasicIndexing] = None) -> np.ndarray:
            if self._raise_on_read:
                raise IOError("Should not read any file")

            data = np.arange(4 * 5 * 6).reshape(self.shape)
            return data[indexing] if indexing else data

    # ensure dummy backend is of valid type
    assert isinstance(TestGridBackend, GridBackendType)

    GridDataset.add_backend(TestGridBackend)
    yield TestGridBackend
    # teardown
    GridDataset.remove_backend(TestGridBackend)