Пример #1
0
class LazyDataset:
    """A lazily-loaded HDF5 dataset"""
    def __init__(self, obj, dataset_path):
        self._obj = obj
        self._dataset_path = dataset_path
        self._data = None

    def __getitem__(self, index):
        if self._data is None:
            with h5py.File(self._obj.data_file, 'r') as hd:
                dataset = hd[self._dataset_path]
                if 'unit' in dataset.attrs:
                    self._data = YTArray(dataset[:],
                                         dataset.attrs['unit'],
                                         registry=self._obj.unit_registry)
                else:
                    self._data = dataset[:]
        return self._data.__getitem__(index)
Пример #2
0
class LazyDataset:
    """A lazily-loaded HDF5 dataset"""
    def __init__(self, obj, dataset_path):
        self._obj = obj
        self._dataset_path = dataset_path
        self._data = None

    def __getitem__(self, index):
        if self._data is None:
            with h5py.File(self._obj.data_file, 'r') as hd:
                if self._dataset_path[:9] == "tree_data":
                    if isinstance(hd[self._dataset_path],
                                  h5py.Dataset):  # old prgen tree
                        self._data = hd[self._dataset_path][:]
                    else:  # new one
                        if 'galaxy' in self._dataset_path.split('_'):
                            self._data = [
                                hd[self._dataset_path + '/%d' % i][:]
                                for i in range(self._obj.ngalaxies)
                            ]
                        elif 'halo' in self._dataset_path.split('_'):
                            self._data = [
                                hd[self._dataset_path + '/%d' % i][:]
                                for i in range(self._obj.nhalos)
                            ]
                        elif 'cloud' in self._dataset_path.split('_'):
                            self._data = [
                                hd[self._dataset_path + '/%d' % i][:]
                                for i in range(self._obj.ncloud)
                            ]
                        else:
                            raise ValueError('The data set path not correct!!',
                                             self._dataset_path)
                else:
                    dataset = hd[self._dataset_path]
                    if 'unit' in dataset.attrs:
                        self._data = YTArray(dataset[:],
                                             dataset.attrs['unit'],
                                             registry=self._obj.unit_registry)
                    else:
                        self._data = dataset[:]
        return self._data.__getitem__(index)