Python h5py.Reference() Examples

The following are 12 code examples of h5py.Reference(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module h5py , or try the search function .
Example #1
Source File: test_group.py    From GraphicDesignPatternByPython with MIT License 6 votes vote down vote up
def test_invalid_ref(self):
        """ Invalid region references should raise ValueError """

        ref = h5py.h5r.Reference()

        with self.assertRaises(ValueError):
            self.f[ref]

        self.f.create_group('x')
        ref = self.f['x'].ref
        del self.f['x']

        with self.assertRaises(ValueError):
            self.f[ref]

    # TODO: check that regionrefs also work with __getitem__ 
Example #2
Source File: h5ad.py    From anndata with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def read_series(dataset) -> Union[np.ndarray, pd.Categorical]:
    if "categories" in dataset.attrs:
        categories = dataset.attrs["categories"]
        if isinstance(categories, h5py.Reference):
            categories_dset = dataset.parent[dataset.attrs["categories"]]
            categories = categories_dset[...]
            ordered = bool(categories_dset.attrs.get("ordered", False))
        else:
            # TODO: remove this code at some point post 0.7
            # TODO: Add tests for this
            warn(
                f"Your file {str(dataset.file.name)!r} has invalid categorical "
                "encodings due to being written from a development version of "
                "AnnData. Rewrite the file ensure you can read it in the future.",
                FutureWarning,
            )
        return pd.Categorical.from_codes(dataset[...], categories, ordered=ordered)
    else:
        return dataset[...]


# @report_read_key_on_error
# def read_sparse_dataset_backed(group: h5py.Group) -> sparse.spmatrix:
#     return SparseDataset(group) 
Example #3
Source File: test_group.py    From keras-lambda with MIT License 6 votes vote down vote up
def test_invalid_ref(self):
        """ Invalid region references should raise ValueError """

        ref = h5py.h5r.Reference()

        with self.assertRaises(ValueError):
            self.f[ref]

        self.f.create_group('x')
        ref = self.f['x'].ref
        del self.f['x']

        with self.assertRaises(ValueError):
            self.f[ref]

    # TODO: check that regionrefs also work with __getitem__ 
Example #4
Source File: test_group.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_reference_numpyobj(self):
        """ Object can be opened by numpy.object_ containing object ref

        Test for issue 181, issue 202.
        """
        g = self.f.create_group('test')

        rtype = h5py.special_dtype(ref=h5py.Reference)
        dt = np.dtype([('a', 'i'),('b',rtype)])
        dset = self.f.create_dataset('test_dset', (1,), dt)

        dset[0] =(42,g.ref)
        data = dset[0]
        self.assertEqual(self.f[data[1]], g) 
Example #5
Source File: test_h5t.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_ref(self):
        """ Reference types are correctly stored in compound types (issue 144)
        """
        ref = h5py.special_dtype(ref=h5py.Reference)
        dt = np.dtype([('a', ref), ('b', '<f4')])
        tid = h5t.py_create(dt, logical=True)
        t1, t2 = tid.get_member_type(0), tid.get_member_type(1)
        self.assertEqual(t1, h5t.STD_REF_OBJ)
        self.assertEqual(t2, h5t.IEEE_F32LE)
        self.assertEqual(tid.get_member_offset(0), 0)
        self.assertEqual(tid.get_member_offset(1), h5t.STD_REF_OBJ.get_size()) 
Example #6
Source File: test_slicing.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_reference(self):
        """ Indexing a reference dataset returns a h5py.Reference instance """
        dset = self.f.create_dataset('x', (1,), dtype=h5py.special_dtype(ref=h5py.Reference))
        dset[0] = self.f.ref
        self.assertEqual(type(dset[0]), h5py.Reference) 
Example #7
Source File: test_slicing.py    From GraphicDesignPatternByPython with MIT License 5 votes vote down vote up
def test_reference_field(self):
        """ Compound types of which a reference is an element work right """
        reftype = h5py.special_dtype(ref=h5py.Reference)
        dt = np.dtype([('a', 'i'),('b',reftype)])

        dset = self.f.create_dataset('x', (1,), dtype=dt)
        dset[0] = (42, self.f['/'].ref)

        out = dset[0]
        self.assertEqual(type(out[1]), h5py.Reference)  # isinstance does NOT work 
Example #8
Source File: test_group.py    From keras-lambda with MIT License 5 votes vote down vote up
def test_reference_numpyobj(self):
        """ Object can be opened by numpy.object_ containing object ref

        Test for issue 181, issue 202.
        """
        g = self.f.create_group('test')

        rtype = h5py.special_dtype(ref=h5py.Reference)
        dt = np.dtype([('a', 'i'),('b',rtype)])
        dset = self.f.create_dataset('test_dset', (1,), dt)

        dset[0] =(42,g.ref)
        data = dset[0]
        self.assertEqual(self.f[data[1]], g) 
Example #9
Source File: test_h5t.py    From keras-lambda with MIT License 5 votes vote down vote up
def test_ref(self):
        """ Reference types are correctly stored in compound types (issue 144)
        """
        ref = h5py.special_dtype(ref=h5py.Reference)
        dt = np.dtype([('a', ref), ('b', '<f4')])
        tid = h5t.py_create(dt, logical=True)
        t1, t2 = tid.get_member_type(0), tid.get_member_type(1)
        self.assertEqual(t1, h5t.STD_REF_OBJ)
        self.assertEqual(t2, h5t.IEEE_F32LE)
        self.assertEqual(tid.get_member_offset(0), 0)
        self.assertEqual(tid.get_member_offset(1), h5t.STD_REF_OBJ.get_size()) 
Example #10
Source File: test_slicing.py    From keras-lambda with MIT License 5 votes vote down vote up
def test_reference(self):
        """ Indexing a reference dataset returns a h5py.Reference instance """
        dset = self.f.create_dataset('x', (1,), dtype=h5py.special_dtype(ref=h5py.Reference))
        dset[0] = self.f.ref
        self.assertEqual(type(dset[0]), h5py.Reference) 
Example #11
Source File: test_slicing.py    From keras-lambda with MIT License 5 votes vote down vote up
def test_reference_field(self):
        """ Compound types of which a reference is an element work right """
        reftype = h5py.special_dtype(ref=h5py.Reference)
        dt = np.dtype([('a', 'i'),('b',reftype)])

        dset = self.f.create_dataset('x', (1,), dtype=dt)
        dset[0] = (42, self.f['/'].ref)

        out = dset[0]
        self.assertEqual(type(out[1]), h5py.Reference)  # isinstance does NOT work 
Example #12
Source File: pyMtsf.py    From PySimulator with GNU Lesser General Public License v3.0 4 votes vote down vote up
def WriteModelVariables(self):
        scalarVariables = self.modelVariable
        # Get maximum length of string vectors
        #maxLenName = self._getMaxLength(scalarVariables.keys())
        #maxLenDescription = self._getMaxLength([x.description for x in scalarVariables.values()])
        # Create dtype object
        numpyDataType = numpy.dtype({'names': ['name', 'simpleTypeRow',
                                              'causality', 'variability',
                                              'description', 'objectId', 'column', 'negated'],
                               'formats': [h5py.special_dtype(vlen=unicode),#'S' + str(max(maxLenName, 1)),
                                          'uint32',
                                          h5py.special_dtype(enum=(numpy.uint8, CausalityType)),  # 'uint8',
                                          h5py.special_dtype(enum=(numpy.uint8, VariabilityType)),  # 'uint8',
                                          h5py.special_dtype(vlen=unicode),#'S' + str(max(maxLenDescription, 1)),
                                          h5py.special_dtype(ref=h5py.Reference),
                                          'uint32',
                                          h5py.special_dtype(enum=(numpy.uint8, {'false':0, 'true':1}))]})  # 'uint8']})
        self.description = self.file.create_group("ModelDescription")
        # Write information on Simulation group
        description = self.modelDescription
        self.description.attrs['modelName'] = description.modelName
        self.description.attrs['description'] = description.description
        self.description.attrs['author'] = description.author
        self.description.attrs['version'] = description.version
        self.description.attrs['generationTool'] = description.generationTool
        self.description.attrs['generationDateAndTime'] = description.generationDateAndTime
        self.description.attrs['variableNamingConvention'] = description.variableNamingConvention
        dataset = self.description.create_dataset('Variables', (len(scalarVariables), 1), dtype=numpyDataType, maxshape=(len(scalarVariables), 1), compression='gzip')
        # Sort Variables by names
        nameList = [x for x in scalarVariables.keys()]
        nameList.sort()
        allData = []
        i = -1
        for variableName in nameList:
            variable = scalarVariables[variableName]
            i += 1
            variable.rowIndex = i
            x = variableName
            allData.append((x, variable.simpleTypeRow,
                            variable.causality, variable.variability,
                            variable.description,
                            variable.category.dataset.ref, variable.columnIndex, variable.aliasNegated))
        dataset[:, 0] = allData
        return