You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.9 KiB

  1. """
  2. Functions to create artificial nd2 data for testing purposes
  3. """
  4. import six
  5. import numpy as np
  6. import struct
  7. class ArtificialND2(object):
  8. """
  9. Artificial ND2 class (for testing purposes)
  10. """
  11. def __init__(self, file):
  12. self._fh = open(file, 'wb')
  13. self.write_version()
  14. def __enter__(self):
  15. return self
  16. def __exit__(self, exc_type, exc_value, traceback):
  17. self.close()
  18. @property
  19. def file_handle(self):
  20. """
  21. The file handle to the binary file
  22. Returns:
  23. file: the file handle
  24. """
  25. return self._fh
  26. def close(self):
  27. """
  28. Correctly close the file handle
  29. """
  30. if self._fh is not None:
  31. self._fh.close()
  32. def write_version(self):
  33. """
  34. Write file header
  35. """
  36. # write 16 empty bytes
  37. self._fh.write(bytearray(16))
  38. # write version info
  39. version_info = six.b('ND2 FILE SIGNATURE CHUNK NAME01!Ver3.0')
  40. self._fh.write(version_info)
  41. @staticmethod
  42. def create_label_map_bytes():
  43. """
  44. Construct a binary label map
  45. Returns:
  46. tuple: (binary data, dictionary data)
  47. """
  48. raw_text = six.b('')
  49. labels = {
  50. 'image_attributes': "ImageAttributesLV!",
  51. 'image_text_info': "ImageTextInfoLV!",
  52. 'image_metadata': "ImageMetadataLV!",
  53. 'image_metadata_sequence': "ImageMetadataSeqLV|0!",
  54. 'image_calibration': "ImageCalibrationLV|0!",
  55. 'x_data': "CustomData|X!",
  56. 'y_data': "CustomData|Y!",
  57. 'z_data': "CustomData|Z!",
  58. 'roi_metadata': "CustomData|RoiMetadata_v1!",
  59. 'pfs_status': "CustomData|PFS_STATUS!",
  60. 'pfs_offset': "CustomData|PFS_OFFSET!",
  61. 'guid': "CustomData|GUIDStore!",
  62. 'description': "CustomData|CustomDescriptionV1_0!",
  63. 'camera_exposure_time': "CustomData|Camera_ExposureTime1!",
  64. 'camera_temp': "CustomData|CameraTemp1!",
  65. 'acquisition_times': "CustomData|AcqTimesCache!",
  66. 'acquisition_times_2': "CustomData|AcqTimes2Cache!",
  67. 'acquisition_frames': "CustomData|AcqFramesCache!",
  68. 'lut_data': "CustomDataVar|LUTDataV1_0!",
  69. 'grabber_settings': "CustomDataVar|GrabberCameraSettingsV1_0!",
  70. 'custom_data': "CustomDataVar|CustomDataV2_0!",
  71. 'app_info': "CustomDataVar|AppInfo_V1_0!"
  72. }
  73. data = {}
  74. # generate random positions and lengths
  75. lengths = np.random.random_integers(1, 999, len(labels))
  76. positions = np.subtract(np.cumsum(lengths), lengths[0])
  77. for length, pos, label in zip(lengths, positions, labels):
  78. raw_text += six.b(labels[label])
  79. raw_text += struct.pack('QQ', pos, length)
  80. data[label] = (pos, length)
  81. return raw_text, data