Пример #1
0
    def test_from_hdf5_condition(self):

        # setup HDF5 file
        node_path = 'test'
        tf = tempfile.NamedTemporaryFile(delete=False)
        file_path = tf.name
        tf.close()
        with h5py.File(file_path, mode='w') as h5f:
            h5f.create_dataset(node_path,
                               data=diploid_genotype_data,
                               chunks=(2, 3, 2))

        # selection
        condition = [False, True, False, True, False]

        # file and node path
        g = GenotypeCArray.from_hdf5(file_path, node_path, condition=condition)
        expect = GenotypeArray(diploid_genotype_data).compress(condition,
                                                               axis=0)
        aeq(expect, g)

        # dataset
        with h5py.File(file_path, mode='r') as h5f:
            dataset = h5f[node_path]
            g = GenotypeCArray.from_hdf5(dataset, condition=condition)
            aeq(expect, g)
Пример #2
0
    def test_to_hdf5(self):

        # setup HDF5 file
        tf = tempfile.NamedTemporaryFile(delete=False)
        file_path = tf.name
        tf.close()

        # setup genotype array
        node_path = 'test'
        g = GenotypeCArray(diploid_genotype_data, dtype='i1')

        # write using file path and node path
        g.to_hdf5(file_path, node_path)

        # test outcome
        with h5py.File(file_path, mode='r') as h5f:
            h5d = h5f[node_path]
            aeq(g[:], h5d[:])

        # write using group
        with h5py.File(file_path, mode='w') as h5f:
            g.to_hdf5(h5f, node_path)

        # test outcome
        with h5py.File(file_path, mode='r') as h5f:
            h5d = h5f[node_path]
            aeq(g[:], h5d[:])
Пример #3
0
    def test_from_hdf5_condition(self):

        # setup HDF5 file
        node_path = 'test'
        tf = tempfile.NamedTemporaryFile(delete=False)
        file_path = tf.name
        tf.close()
        with h5py.File(file_path, mode='w') as h5f:
            h5f.create_dataset(node_path,
                               data=diploid_genotype_data,
                               chunks=(2, 3, 2))

        # selection
        condition = [False, True, False, True, False]

        # file and node path
        g = GenotypeCArray.from_hdf5(file_path, node_path, condition=condition)
        expect = GenotypeArray(diploid_genotype_data).compress(condition,
                                                               axis=0)
        aeq(expect, g)

        # dataset
        with h5py.File(file_path, mode='r') as h5f:
            dataset = h5f[node_path]
            g = GenotypeCArray.from_hdf5(dataset, condition=condition)
            aeq(expect, g)
Пример #4
0
    def test_from_hdf5(self):

        # setup HDF5 file
        node_path = 'test'
        tf = tempfile.NamedTemporaryFile(delete=False)
        file_path = tf.name
        tf.close()
        with h5py.File(file_path, mode='w') as h5f:
            h5f.create_dataset(node_path,
                               data=diploid_genotype_data,
                               chunks=(2, 3, 2))

        # file and node path
        g = GenotypeCArray.from_hdf5(file_path, node_path)
        aeq(diploid_genotype_data, g)

        # dataset
        with h5py.File(file_path, mode='r') as h5f:
            dataset = h5f[node_path]
            g = GenotypeCArray.from_hdf5(dataset)
            aeq(diploid_genotype_data, g)
Пример #5
0
    def test_from_hdf5(self):

        # setup HDF5 file
        node_path = 'test'
        tf = tempfile.NamedTemporaryFile(delete=False)
        file_path = tf.name
        tf.close()
        with h5py.File(file_path, mode='w') as h5f:
            h5f.create_dataset(node_path,
                               data=diploid_genotype_data,
                               chunks=(2, 3, 2))

        # file and node path
        g = GenotypeCArray.from_hdf5(file_path, node_path)
        aeq(diploid_genotype_data, g)

        # dataset
        with h5py.File(file_path, mode='r') as h5f:
            dataset = h5f[node_path]
            g = GenotypeCArray.from_hdf5(dataset)
            aeq(diploid_genotype_data, g)
Пример #6
0
    def test_to_hdf5(self):

        # setup HDF5 file
        tf = tempfile.NamedTemporaryFile(delete=False)
        file_path = tf.name
        tf.close()

        # setup genotype array
        node_path = 'test'
        g = GenotypeCArray(diploid_genotype_data, dtype='i1')

        # write using file path and node path
        g.to_hdf5(file_path, node_path)

        # test outcome
        with h5py.File(file_path, mode='r') as h5f:
            h5d = h5f[node_path]
            aeq(g[:], h5d[:])

        # write using group
        with h5py.File(file_path, mode='w') as h5f:
            g.to_hdf5(h5f, node_path)

        # test outcome
        with h5py.File(file_path, mode='r') as h5f:
            h5d = h5f[node_path]
            aeq(g[:], h5d[:])
Пример #7
0
    def test_constructor(self):

        # missing data arg
        with assert_raises(ValueError):
            # noinspection PyArgumentList
            GenotypeCArray()

        # data has wrong dtype
        data = 'foo bar'
        with assert_raises(TypeError):
            GenotypeCArray(data)

        # data has wrong dtype
        data = [4., 5., 3.7]
        with assert_raises(TypeError):
            GenotypeCArray(data)

        # data has wrong dimensions
        data = [1, 2, 3]
        with assert_raises(TypeError):
            GenotypeCArray(data)

        # data has wrong dimensions
        data = [[1, 2], [3, 4]]  # use HaplotypeCArray instead
        with assert_raises(TypeError):
            GenotypeCArray(data)

        # diploid data (typed)
        g = GenotypeCArray(diploid_genotype_data, dtype='i1')
        aeq(diploid_genotype_data, g)
        eq(np.int8, g.dtype)

        # polyploid data (typed)
        g = GenotypeCArray(triploid_genotype_data, dtype='i1')
        aeq(triploid_genotype_data, g)
        eq(np.int8, g.dtype)

        # cparams
        g = GenotypeCArray(diploid_genotype_data,
                           cparams=bcolz.cparams(clevel=10))
        aeq(diploid_genotype_data, g)
        eq(10, g.cparams.clevel)
Пример #8
0
    def test_slice_types(self):

        g = GenotypeCArray(diploid_genotype_data, dtype='i1')

        # row slice
        s = g[1:]
        self.assertNotIsInstance(s, GenotypeCArray)
        self.assertIsInstance(s, GenotypeArray)

        # col slice
        s = g[:, 1:]
        self.assertNotIsInstance(s, GenotypeCArray)
        self.assertIsInstance(s, GenotypeArray)

        # row index
        s = g[0]
        self.assertNotIsInstance(s, GenotypeCArray)
        self.assertNotIsInstance(s, GenotypeArray)
        self.assertIsInstance(s, np.ndarray)

        # col index
        s = g[:, 0]
        self.assertNotIsInstance(s, GenotypeCArray)
        self.assertNotIsInstance(s, GenotypeArray)
        self.assertIsInstance(s, np.ndarray)

        # ploidy index
        s = g[:, :, 0]
        self.assertNotIsInstance(s, GenotypeCArray)
        self.assertNotIsInstance(s, GenotypeArray)
        self.assertIsInstance(s, np.ndarray)

        # item
        s = g[0, 0, 0]
        self.assertNotIsInstance(s, GenotypeCArray)
        self.assertNotIsInstance(s, GenotypeArray)
        self.assertIsInstance(s, np.int8)
Пример #9
0
 def setup_instance(self, data):
     return GenotypeCArray(data)