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.

101 lines
2.8 KiB

6 years ago
6 years ago
  1. import six
  2. import warnings
  3. from nd2reader.common import get_from_dict_if_exists
  4. def parse_if_not_none(to_check, callback):
  5. if to_check is not None:
  6. return callback()
  7. return None
  8. def parse_dimension_text_line(line):
  9. if six.b("Dimensions:") in line:
  10. entries = line.split(six.b("\r\n"))
  11. for entry in entries:
  12. if entry.startswith(six.b("Dimensions:")):
  13. return entry
  14. return None
  15. def parse_roi_shape(shape):
  16. if shape == 3:
  17. return 'rectangle'
  18. elif shape == 9:
  19. return 'circle'
  20. return None
  21. def parse_roi_type(type_no):
  22. if type_no == 4:
  23. return 'stimulation'
  24. elif type_no == 3:
  25. return 'reference'
  26. elif type_no == 2:
  27. return 'background'
  28. return None
  29. def get_loops_from_data(loop_data):
  30. loops = [loop_data]
  31. if six.b('uiPeriodCount') in loop_data and loop_data[six.b('uiPeriodCount')] > 0:
  32. # special ND experiment
  33. if six.b('pPeriod') not in loop_data:
  34. return []
  35. # take the first dictionary element, it contains all loop data
  36. loops = loop_data[six.b('pPeriod')][list(loop_data[six.b('pPeriod')].keys())[0]]
  37. return loops
  38. def guess_sampling_from_loops(duration, loop):
  39. """ In some cases, both keys are not saved. Then try to calculate it.
  40. Args:
  41. duration: the total duration of the loop
  42. loop: the raw loop data
  43. Returns:
  44. float: the guessed sampling interval in milliseconds
  45. """
  46. number_of_loops = get_from_dict_if_exists('uiCount', loop)
  47. number_of_loops = number_of_loops if number_of_loops is not None and number_of_loops > 0 else 1
  48. interval = duration / number_of_loops
  49. return interval
  50. def determine_sampling_interval(duration, loop):
  51. """Determines the loop sampling interval in milliseconds
  52. Args:
  53. duration: loop duration in milliseconds
  54. loop: loop dictionary
  55. Returns:
  56. float: the sampling interval in milliseconds
  57. """
  58. interval = get_from_dict_if_exists('dPeriod', loop)
  59. avg_interval = get_from_dict_if_exists('dAvgPeriodDiff', loop)
  60. if interval is None or interval <= 0:
  61. interval = avg_interval
  62. else:
  63. avg_interval_set = avg_interval is not None and avg_interval > 0
  64. if round(avg_interval) != round(interval) and avg_interval_set:
  65. message = ("Reported average frame interval (%.1f ms) doesn't"
  66. " match the set interval (%.1f ms). Using the average"
  67. " now.")
  68. warnings.warn(message % (avg_interval, interval), RuntimeWarning)
  69. interval = avg_interval
  70. if interval is None or interval <= 0:
  71. # In some cases, both keys are not saved. Then try to calculate it.
  72. interval = guess_sampling_from_loops(duration, loop)
  73. return interval