def test_sort_particle_quantities(given, order, expected): given = given.split(",") expected = expected.split(",") actual = sort_particle_quantities(given, order=order) assert len(actual) == len(expected) assert all([a == b for a, b in zip(actual, expected)])
def __init__(self, location=Union[str, Path]) -> None: self.location = (location if isinstance(location, Path) else Path(location)) info(f"Obtaining backend props for '{self.location}'") with h5.File(self.location, mode="r") as fp: self._dataset_name = fp.attrs["NAME"].astype(str)[0] self._num_particles = fp["q"].shape[0] if fp["q"].shape else 0 # quantaties unordered_names = list(fp.attrs["QUANTS"].astype(str)) unordered_labels = list(fp.attrs["LABELS"].astype(str)) unordered_units = list(fp.attrs["UNITS"].astype(str)) clean_names = [name for name in unordered_names if name != "tag"] clean_names = sort_particle_quantities(clean_names, ("x", "p")) clean_labels = [ unordered_labels[unordered_names.index(s)] for s in clean_names ] clean_units = [ unordered_units[unordered_names.index(s)] for s in clean_names ] dtype = [(name, fp[name].dtype) for name in clean_names] self._quantity_names = clean_names self._quantity_labels = clean_labels self._quantity_units = clean_units self._dtype = np.dtype(dtype) # temporal information self._iteration = fp.attrs["ITER"][0] self._time_step = fp.attrs["TIME"][0] self._time_unit = fp.attrs["TIME UNITS"].astype(str)[0]
def quantity_names(self) -> List[str]: info(f"Accessing '{self.location}' for 'quantity_names'") quantities = [] with h5.File(self.location, mode="r") as fp: for key, item in fp.items(): if key == "tag": continue if isinstance(item, h5.Dataset): quantities.append(key) return sort_particle_quantities(quantities, ["x", "p"])
def __init__(self, location: FileLocation) -> None: self.location = location if isinstance(location, Path) else Path(location) info(f"Obtaining backend props for '{self.location}'") with h5.File(self.location, mode="r") as fp: ds_name = fp.attrs["NAME"].astype(str)[0] # ensures name is valid identifier self._dataset_name = ds_name.replace(" ", "_") self._dataset_label = ds_name self._shape = fp["q"].shape self._ndim = len(self._shape) # find first all quantaties - with their names names = [] for key, item in fp.items(): if key == "tag": continue if isinstance(item, h5.Dataset): names.append(key) self._quantity_names = sort_particle_quantities(names, ("x", "p")) # iterate over all the sorted names and fill other props labels = [] units = [] dtype = [] for name in self._quantity_names: labels.append(fp[name].attrs["LONG_NAME"].astype(str)[0]) units.append(fp[name].attrs["UNITS"].astype(str)[0]) dtype.append((name, fp[name].dtype)) self._quantity_labels = labels self._quantity_units = units self._dtype = np.dtype(dtype) # temporal information self._iteration = fp.attrs["ITER"][0] self._time_step = fp.attrs["TIME"][0] self._time_unit = fp.attrs["TIME UNITS"].astype(str)[0]