Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

calc_methods.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import numpy as np
  2. from ..idl_translation_core.ViewCalcData import calc_deltaF
  3. def calc_method_0(raw_data: list, background_frames: list, area_mask: np.ndarray):
  4. """
  5. Calculate signal from raw data as: sig1 = raw1 / 1000
  6. :param list raw_data: list of numpy.ndarrays, raw data in format XYT
  7. :param list background_frames: list of two integers indicating the start and end frames of background
  8. :param numpy.ndarray area_mask: boolean numpy ndarray in format XY, with 0 indicating pixels
  9. outside the tissue of interest
  10. :return: numpy.ndarray in format XYT, the same size as any member of <raw_data>
  11. :rtype: numpy.ndarray
  12. """
  13. error = "Calc Method 0 could not handle the raw data provided. It expects a list of " \
  14. "at least one numpy ndarray, the first of which will be used"
  15. assert isinstance(raw_data, list) and len(raw_data) >= 1, error
  16. assert isinstance(raw_data[0], np.ndarray), error
  17. return raw_data[0].astype(float) / 1000
  18. def calc_method_1(raw_data: list, background_frames: list, area_mask: np.ndarray):
  19. """
  20. Calculate signal from raw data as: sig1 = raw2 / 1000
  21. :param list raw_data: list of numpy.ndarrays, raw data in format XYT
  22. :param list background_frames: list of two integers indicating the start and end frames of background
  23. :param numpy.ndarray area_mask: boolean numpy ndarray in format XY, with 0 indicating pixels
  24. outside the tissue of interest
  25. :return: numpy.ndarray in format XYT, the same size as any member of <raw_data>
  26. :rtype: numpy.ndarray
  27. """
  28. error = "Calc Method 1 could not handle the raw data provided. It expects a list of " \
  29. "at least one numpy ndarray, the second of which will be used"
  30. assert isinstance(raw_data, list) and len(raw_data) >= 1, error
  31. assert isinstance(raw_data[0], np.ndarray), error
  32. return raw_data[1].astype(float) / 1000
  33. def calc_method_3(raw_data: list, background_frames: list, area_mask: np.ndarray):
  34. """
  35. Calculate signal from raw data as: sig1 = deltaF/F0; F0=average intensity during <background_frames>
  36. :param list raw_data: list of numpy.ndarrays, raw data in format XYT
  37. :param list background_frames: list of two integers indicating the start and end frames of background
  38. :param numpy.ndarray area_mask: boolean numpy ndarray in format XY, with 0 indicating pixels
  39. outside the tissue of interest
  40. :return: numpy.ndarray in format XYT, the same size as any member of <raw_data>
  41. :rtype: numpy.ndarray
  42. """
  43. error = "Calc Method 3 could not handle the raw data provided. It expects a list of " \
  44. "at least one numpy ndarray, the first of which will be used"
  45. assert isinstance(raw_data, list) and len(raw_data) >= 1, error
  46. assert isinstance(raw_data[0], np.ndarray), error
  47. return calc_deltaF(raw_data[0], background_frames)
  48. def calc_method_4(raw_data: list, background_frames: list, area_mask: np.ndarray):
  49. """
  50. Calculate signal from raw data as: sig1 = raw1/raw2 - (raw1/raw2 averaged over <background_frames>)
  51. :param list raw_data: list of numpy.ndarrays, raw data in format XYT
  52. :param list background_frames: list of two integers indicating the start and end frames of background
  53. :param numpy.ndarray area_mask: boolean numpy ndarray in format XY, with 0 indicating pixels
  54. outside the tissue of interest
  55. :return: numpy.ndarray in format XYT, the same size as any member of <raw_data>
  56. :rtype: numpy.ndarray
  57. """
  58. error = "Calc Method 4 could not handle the raw data provided. It expects a list of " \
  59. "at least two numpy ndarrays, the first two of which will be used"
  60. assert isinstance(raw_data, list) and len(raw_data) >= 2, error
  61. assert isinstance(raw_data[0], np.ndarray), error
  62. assert isinstance(raw_data[1], np.ndarray), error
  63. bg_start, bg_end = background_frames
  64. raw1, raw2 = raw_data[:2]
  65. # normalizing by average background pixel intensity to nullify the effects of exposure time differences
  66. # on ratio calculation
  67. raw1_background_average = raw1[area_mask, bg_start: bg_end + 1].mean()
  68. raw2_background_average = raw2[area_mask, bg_start: bg_end + 1].mean()
  69. normalized_raw1 = raw1 / raw1_background_average
  70. normalized_raw2 = raw2 / raw2_background_average
  71. ratio = normalized_raw1 / normalized_raw2
  72. background_ratio = ratio[:, :, bg_start: bg_end + 1].mean(axis=2)
  73. # convert from XYT to format TXY as it is easy to subtract background frame from ratio
  74. ratio_txy = np.moveaxis(ratio, source=-1, destination=0)
  75. relative_ratio_txy = ratio_txy - background_ratio
  76. # convert back to XYT
  77. relative_ratio_xyt = np.moveaxis(relative_ratio_txy, source=0, destination=-1)
  78. return relative_ratio_xyt
  79. def get_calc_method(flags):
  80. calc_method = flags["LE_CalcMethod"]
  81. if calc_method == 0:
  82. return calc_method_0
  83. if calc_method == 1:
  84. return calc_method_1
  85. elif calc_method == 3 or 3000 <= calc_method < 4000:
  86. return calc_method_3
  87. elif calc_method == 4 or 4000 <= calc_method < 5000:
  88. return calc_method_4
  89. else:
  90. raise NotImplementedError