Skip to content

Commit

Permalink
Add HDF5 metadata unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Egor Savkin <[email protected]>
  • Loading branch information
thomasfire committed Dec 2, 2022
1 parent 71a2012 commit 5e8ec04
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
1 change: 0 additions & 1 deletion artiq/examples/no_hardware/repository/hdf5_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,4 @@ def run(self):
broadcast=True, archive=True)
self.set_dataset_metadata("dummy", {"k1": "v1", "k2": "v2"})

# Attach metadata to an absent key is no-op
self.set_dataset_metadata("nothing", {"no": "op"})
49 changes: 49 additions & 0 deletions artiq/test/test_hdf5_attributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import unittest
import numpy as np

from artiq.experiment import *
from artiq.test.hardware_testbench import ExperimentCase


class HDF5Attributes(EnvExperiment):
"""Archive data to HDF5 with attributes"""

def run(self):
# Attach attributes to the HDF5 group `datasets`
self.set_dataset_metadata(None, {
"arr": np.array([1, 2, 3]),
"description": "demo",
})

# `archive=True` is required in order to
# attach attributes to HDF5 datasets
self.set_dataset("dummy", np.full(20, np.nan), broadcast=True, archive=True)
self.set_dataset_metadata("dummy", {"k1": "v1", "k2": "v2"})

self.set_dataset("no_archive", np.full(30, np.nan), broadcast=False, archive=False)
self.set_dataset_metadata("no_archive", {"na_k": "na_v"})

self.set_dataset_metadata("nothing", {"k": "v"})
self.set_dataset_metadata(None, {"general": "metadata"})


class TestHDF5Attributes(ExperimentCase):
def setUp(self):
super().setUp()
self.exp = self.execute(HDF5Attributes)

def test_dataset_metadata(self):
self.assertNotEqual(self.dataset_mgr, None)
self.assertEqual(self.dataset_mgr.hdf5_attributes["datasets/dummy"], {"k1": "v1", "k2": "v2"})
self.assertTrue(np.all((self.dataset_mgr.local["dummy"], np.full(20, np.nan))))

def test_absent_key_metadata(self):
self.assertEqual(self.dataset_mgr.hdf5_attributes["datasets/nothing"], {"k": "v"})

def test_none_key_metadata(self):
self.assertEqual(self.dataset_mgr.hdf5_attributes["datasets"], {"general": "metadata"})

def test_no_archive(self):
self.assertEqual(self.dataset_mgr.hdf5_attributes["datasets/no_archive"], {"na_k": "na_v"})
with self.assertRaises(KeyError):
_ = self.dataset_mgr.local["no_archive"]

0 comments on commit 5e8ec04

Please sign in to comment.