Пример #1
0
    def test_recursive_annotation_read(self):
        with tb.File(self.h5_name, mode='a') as h5_file:
            io.annotate_database(h5_file)
            io.write_custom_annotation(
                h5_file,
                'test_ann_1',
                'a test 1',
                annotations_sub_group='zzz_test_group_1')
            io.write_custom_annotation(
                h5_file,
                'test_ann_2',
                'a test 2',
                annotations_sub_group='zzz_test_group_2')
            io.write_custom_annotation(
                h5_file,
                'test_ann_11',
                'a test 11',
                annotations_sub_group='zzz_test_group_1/zzz_test_group_11')

            annotations = io.read_annotations(h5_file)
            annotation_lvl_0_keys = sorted(annotations.keys())
            self.assertEqual(
                annotation_lvl_0_keys, self.ref_annotation_keys +
                ['zzz_test_group_1', 'zzz_test_group_2'])
            annotation_lvl_01_keys = sorted(annotations['zzz_test_group_1'])
            self.assertEqual(annotation_lvl_01_keys,
                             ['test_ann_1', 'zzz_test_group_11'])
            annotation_lvl_02_keys = sorted(annotations['zzz_test_group_2'])
            self.assertEqual(annotation_lvl_02_keys, ['test_ann_2'])
            annotation_lvl_11_keys = sorted(
                annotations['zzz_test_group_1']['zzz_test_group_11'])
            self.assertEqual(annotation_lvl_11_keys, ['test_ann_11'])
Пример #2
0
    def test_write_custom_annotation(self):
        with tb.File(self.h5_name, mode='a') as h5_file:
            # Roundtrip
            io.write_custom_annotation(h5_file, 'test_ann', 'a test')

            self.assertEqual(
                'a test',
                json.loads(h5_file.root.annotations.test_ann.read().decode()))

            io.remove_annotations(h5_file)

            with self.assertRaises(tb.NoSuchNodeError):
                h5_file.root.annotations.test_ann.read()

            # Invalid annotation
            with self.assertRaises(TypeError):
                io.write_custom_annotation(h5_file, 'fail_ann', lambda x: x)

            # Annotations subgroup
            io.write_custom_annotation(h5_file,
                                       'test_ann',
                                       'a test',
                                       annotations_sub_group='test_group')

            self.assertEqual(
                'a test',
                json.loads(h5_file.root.annotations.test_group.test_ann.read().
                           decode()))

            io.remove_annotations(h5_file)

            with self.assertRaises(tb.NoSuchNodeError):
                h5_file.root.annotations.test_group.test_ann.read()

            # Writing to existing annotation
            io.write_custom_annotation(h5_file, 'test_ann', 'a test')
            with self.assertRaises(tb.NodeError):
                io.write_custom_annotation(h5_file, 'test_ann', 'a test')