Beispiel #1
0
 def test_annotations_roundtrip(self):
     with tb.File(self.h5_name, mode='a') as h5_file:
         io.annotate_database(h5_file)
         annotation_keys = sorted(io.read_annotations(h5_file).keys())
         self.assertEqual(annotation_keys, self.ref_annotation_keys)
         io.remove_annotations(h5_file)
         with self.assertRaises(tb.NoSuchNodeError):
             io.read_annotations(h5_file)
Beispiel #2
0
    def test_annotations_exceptions(self):
        with tb.File(self.h5_name, mode='a') as h5_file:
            # No annotations
            io.remove_annotations(h5_file)  # Pass if no annotations
            with self.assertRaises(tb.NoSuchNodeError):
                io.read_annotations(h5_file)

            # Non-compliant annotation
            annotations_group = h5_file.create_group('/', 'annotations')
            h5_file.create_array(annotations_group,
                                 'fail_ann',
                                 obj='[fail)'.encode())
            h5_file.flush()
            with self.assertRaises(ValueError):
                io.read_annotations(h5_file)

            # Already annotated database
            with self.assertRaises(tb.NodeError):
                io.annotate_database(h5_file)
Beispiel #3
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')