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.

93 lines
2.9 KiB

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