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.

111 lines
3.1 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. import six
  2. import warnings
  3. from nd2reader2.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. # special ND experiment
  31. if six.b('pPeriod') not in loop_data:
  32. return []
  33. if six.b('uiPeriodCount') in loop_data and loop_data[six.b('uiPeriodCount')] > 0:
  34. loops = []
  35. for i, period in enumerate(loop_data[six.b('pPeriod')]):
  36. # exclude invalid periods
  37. if six.b('pPeriodValid') in loop_data:
  38. try:
  39. if loop_data[six.b('pPeriodValid')][i] == 1:
  40. loops.append(loop_data[six.b('pPeriod')][period])
  41. except IndexError:
  42. continue
  43. else:
  44. # we can't be sure, append all
  45. loops.append(loop_data[six.b('pPeriod')][period])
  46. return [loop_data]
  47. def guess_sampling_from_loops(duration, loop):
  48. """ In some cases, both keys are not saved. Then try to calculate it.
  49. Args:
  50. duration: the total duration of the loop
  51. loop: the raw loop data
  52. Returns:
  53. float: the guessed sampling interval in milliseconds
  54. """
  55. number_of_loops = get_from_dict_if_exists('uiCount', loop)
  56. number_of_loops = number_of_loops if number_of_loops is not None and number_of_loops > 0 else 1
  57. interval = duration / number_of_loops
  58. return interval
  59. def determine_sampling_interval(duration, loop):
  60. """Determines the loop sampling interval in milliseconds
  61. Args:
  62. duration: loop duration in milliseconds
  63. loop: loop dictionary
  64. Returns:
  65. float: the sampling interval in milliseconds
  66. """
  67. interval = get_from_dict_if_exists('dPeriod', loop)
  68. avg_interval = get_from_dict_if_exists('dAvgPeriodDiff', loop)
  69. if interval is None or interval <= 0:
  70. interval = avg_interval
  71. else:
  72. avg_interval_set = avg_interval is not None and avg_interval > 0
  73. if round(avg_interval) != round(interval) and avg_interval_set:
  74. message = ("Reported average frame interval (%.1f ms) doesn't"
  75. " match the set interval (%.1f ms). Using the average"
  76. " now.")
  77. warnings.warn(message % (avg_interval, interval), RuntimeWarning)
  78. interval = avg_interval
  79. if interval is None or interval <= 0:
  80. # In some cases, both keys are not saved. Then try to calculate it.
  81. interval = guess_sampling_from_loops(duration, loop)
  82. return interval