From 010ee2a499bd51462b0276b136c7e4aa534cab8c Mon Sep 17 00:00:00 2001 From: Ruben Verweij Date: Wed, 15 Mar 2017 16:26:37 +0100 Subject: [PATCH] Test the LabelMap --- nd2reader/artificial.py | 47 ++++++++++++++++++++++++++ nd2reader/label_map.py | 12 +++---- tests/test_label_map.py | 75 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 tests/test_label_map.py diff --git a/nd2reader/artificial.py b/nd2reader/artificial.py index 419c8b1..d8c56f2 100644 --- a/nd2reader/artificial.py +++ b/nd2reader/artificial.py @@ -2,6 +2,8 @@ Functions to create artificial nd2 data for testing purposes """ import six +import numpy as np +import struct class ArtificialND2(object): @@ -45,3 +47,48 @@ class ArtificialND2(object): # write version info version_info = six.b('ND2 FILE SIGNATURE CHUNK NAME01!Ver3.0') self._fh.write(version_info) + + @staticmethod + def create_label_map_bytes(): + """ + Construct a binary label map + Returns: + tuple: (binary data, dictionary data) + """ + raw_text = six.b('') + labels = { + 'image_attributes': "ImageAttributesLV!", + 'image_text_info': "ImageTextInfoLV!", + 'image_metadata': "ImageMetadataLV!", + 'image_metadata_sequence': "ImageMetadataSeqLV|0!", + 'image_calibration': "ImageCalibrationLV|0!", + 'x_data': "CustomData|X!", + 'y_data': "CustomData|Y!", + 'z_data': "CustomData|Z!", + 'roi_metadata': "CustomData|RoiMetadata_v1!", + 'pfs_status': "CustomData|PFS_STATUS!", + 'pfs_offset': "CustomData|PFS_OFFSET!", + 'guid': "CustomData|GUIDStore!", + 'description': "CustomData|CustomDescriptionV1_0!", + 'camera_exposure_time': "CustomData|Camera_ExposureTime1!", + 'camera_temp': "CustomData|CameraTemp1!", + 'acquisition_times': "CustomData|AcqTimesCache!", + 'acquisition_times_2': "CustomData|AcqTimes2Cache!", + 'acquisition_frames': "CustomData|AcqFramesCache!", + 'lut_data': "CustomDataVar|LUTDataV1_0!", + 'grabber_settings': "CustomDataVar|GrabberCameraSettingsV1_0!", + 'custom_data': "CustomDataVar|CustomDataV2_0!", + 'app_info': "CustomDataVar|AppInfo_V1_0!" + } + data = {} + + # generate random positions and lengths + lengths = np.random.random_integers(1, 999, len(labels)) + positions = np.subtract(np.cumsum(lengths), lengths[0]) + + for length, pos, label in zip(lengths, positions, labels): + raw_text += six.b(labels[label]) + raw_text += struct.pack('QQ', pos, length) + data[label] = (pos, length) + + return raw_text, data diff --git a/nd2reader/label_map.py b/nd2reader/label_map.py index dc62b6a..790d541 100644 --- a/nd2reader/label_map.py +++ b/nd2reader/label_map.py @@ -100,30 +100,30 @@ class LabelMap(object): @property def x_data(self): - """Get the location of the x data + """Get the location of the custom x data Returns: - int: The location of the x data + int: The location of the custom x data """ return self._get_location(six.b("CustomData|X!")) @property def y_data(self): - """Get the location of the y data + """Get the location of the custom y data Returns: - int: The location of the y data + int: The location of the custom y data """ return self._get_location(six.b("CustomData|Y!")) @property def z_data(self): - """Get the location of the z data + """Get the location of the custom z data Returns: - int: The location of the z data + int: The location of the custom z data """ return self._get_location(six.b("CustomData|Z!")) diff --git a/tests/test_label_map.py b/tests/test_label_map.py new file mode 100644 index 0000000..3fb13dd --- /dev/null +++ b/tests/test_label_map.py @@ -0,0 +1,75 @@ +import unittest +from nd2reader.label_map import LabelMap +from nd2reader.artificial import ArtificialND2 + + +class TestLabelMap(unittest.TestCase): + def setUp(self): + self.raw_text, self.locations = ArtificialND2.create_label_map_bytes() + self.label_map = LabelMap(self.raw_text) + + def test_image_text_info(self): + self.assertEqual(self.locations['image_text_info'][0], self.label_map.image_text_info) + + def test_image_metadata(self): + self.assertEqual(self.locations['image_metadata'][0], self.label_map.image_metadata) + + def test_image_attributes(self): + self.assertEqual(self.locations['image_attributes'][0], self.label_map.image_attributes) + + def test_image_metadata_sequence(self): + self.assertEqual(self.locations['image_metadata_sequence'][0], self.label_map.image_metadata_sequence) + + def test_image_calibration(self): + self.assertEqual(self.locations['image_calibration'][0], self.label_map.image_calibration) + + def test_x_data(self): + self.assertEqual(self.locations['x_data'][0], self.label_map.x_data) + + def test_y_data(self): + self.assertEqual(self.locations['y_data'][0], self.label_map.y_data) + + def test_z_data(self): + self.assertEqual(self.locations['z_data'][0], self.label_map.z_data) + + def test_roi_metadata(self): + self.assertEqual(self.locations['roi_metadata'][0], self.label_map.roi_metadata) + + def test_pfs_status(self): + self.assertEqual(self.locations['pfs_status'][0], self.label_map.pfs_status) + + def test_pfs_offset(self): + self.assertEqual(self.locations['pfs_offset'][0], self.label_map.pfs_offset) + + def test_guid(self): + self.assertEqual(self.locations['guid'][0], self.label_map.guid) + + def test_description(self): + self.assertEqual(self.locations['description'][0], self.label_map.description) + + def test_camera_exposure_time(self): + self.assertEqual(self.locations['camera_exposure_time'][0], self.label_map.camera_exposure_time) + + def test_camera_temp(self): + self.assertEqual(self.locations['camera_temp'][0], self.label_map.camera_temp) + + def test_acquisition_times(self): + self.assertEqual(self.locations['acquisition_times'][0], self.label_map.acquisition_times) + + def test_acquisition_times_2(self): + self.assertEqual(self.locations['acquisition_times_2'][0], self.label_map.acquisition_times_2) + + def test_acquisition_frames(self): + self.assertEqual(self.locations['acquisition_frames'][0], self.label_map.acquisition_frames) + + def test_lut_data(self): + self.assertEqual(self.locations['lut_data'][0], self.label_map.lut_data) + + def test_grabber_settings(self): + self.assertEqual(self.locations['grabber_settings'][0], self.label_map.grabber_settings) + + def test_custom_data(self): + self.assertEqual(self.locations['custom_data'][0], self.label_map.custom_data) + + def test_app_info(self): + self.assertEqual(self.locations['app_info'][0], self.label_map.app_info)