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.

54 lines
2.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. # -*- coding: utf-8 -*-
  2. import numpy as np # type: ignore
  3. import warnings
  4. def get_unwanted_bytes_ids(image_group_data, image_data_start, height, width):
  5. # Check if the byte array size conforms to the image axes size. If not, check
  6. # that the number of unexpected (unwanted) bytes is a multiple of the number of
  7. # rows (height), as the same unmber of unwanted bytes is expected to be
  8. # appended at the end of each row. Then, returns the indexes of the unwanted
  9. # bytes.
  10. number_of_true_channels = int(len(image_group_data[4:]) / (height * width))
  11. n_unwanted_bytes = (len(image_group_data[image_data_start:])) % (height * width)
  12. if not n_unwanted_bytes:
  13. return np.arange(0)
  14. assert 0 == n_unwanted_bytes % height, (
  15. "An unexpected number of extra bytes was encountered based on the expected"
  16. + " frame size, therefore the file could not be parsed."
  17. )
  18. return np.arange(
  19. image_data_start + height * number_of_true_channels,
  20. len(image_group_data) - n_unwanted_bytes + 1,
  21. height * number_of_true_channels,
  22. )
  23. def remove_bytes_by_id(byte_ids, image_group_data, height):
  24. # Remove bytes by ID.
  25. bytes_per_row = len(byte_ids) // height
  26. warnings.warn(
  27. f"{len(byte_ids)} ({bytes_per_row}*{height}) unexpected zero "
  28. + "bytes were found in the ND2 file and removed to allow further parsing."
  29. )
  30. for i in range(len(byte_ids)):
  31. del image_group_data[byte_ids[i] : (byte_ids[i] + bytes_per_row)]
  32. def remove_parsed_unwanted_bytes(image_group_data, image_data_start, height, width):
  33. # Stitched ND2 files have been reported to contain unexpected (according to
  34. # image shape) zero bytes at the end of each image data row. This hinders
  35. # proper reshaping of the data. Hence, here the unwanted zero bytes are
  36. # identified and removed.
  37. unwanted_byte_ids = get_unwanted_bytes_ids(
  38. image_group_data, image_data_start, height, width
  39. )
  40. if 0 != len(unwanted_byte_ids):
  41. assert np.all(
  42. image_group_data[unwanted_byte_ids + np.arange(len(unwanted_byte_ids))] == 0
  43. ), (
  44. f"{len(unwanted_byte_ids)} unexpected non-zero bytes were found"
  45. + " in the ND2 file, the file could not be parsed."
  46. )
  47. remove_bytes_by_id(unwanted_byte_ids, image_group_data, height)
  48. return image_group_data