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.

106 lines
3.0 KiB

6 years ago
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. # exclude invalid periods
  38. if six.b('pPeriodValid') in loop_data:
  39. loops = [loops[i] for i in range(len(loops)) if loop_data[six.b('pPeriodValid')][i] == 1]
  40. return loops
  41. def guess_sampling_from_loops(duration, loop):
  42. """ In some cases, both keys are not saved. Then try to calculate it.
  43. Args:
  44. duration: the total duration of the loop
  45. loop: the raw loop data
  46. Returns:
  47. float: the guessed sampling interval in milliseconds
  48. """
  49. number_of_loops = get_from_dict_if_exists('uiCount', loop)
  50. number_of_loops = number_of_loops if number_of_loops is not None and number_of_loops > 0 else 1
  51. interval = duration / number_of_loops
  52. return interval
  53. def determine_sampling_interval(duration, loop):
  54. """Determines the loop sampling interval in milliseconds
  55. Args:
  56. duration: loop duration in milliseconds
  57. loop: loop dictionary
  58. Returns:
  59. float: the sampling interval in milliseconds
  60. """
  61. interval = get_from_dict_if_exists('dPeriod', loop)
  62. avg_interval = get_from_dict_if_exists('dAvgPeriodDiff', loop)
  63. if interval is None or interval <= 0:
  64. interval = avg_interval
  65. else:
  66. avg_interval_set = avg_interval is not None and avg_interval > 0
  67. if round(avg_interval) != round(interval) and avg_interval_set:
  68. message = ("Reported average frame interval (%.1f ms) doesn't"
  69. " match the set interval (%.1f ms). Using the average"
  70. " now.")
  71. warnings.warn(message % (avg_interval, interval), RuntimeWarning)
  72. interval = avg_interval
  73. if interval is None or interval <= 0:
  74. # In some cases, both keys are not saved. Then try to calculate it.
  75. interval = guess_sampling_from_loops(duration, loop)
  76. return interval