test_artifact_correction.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. from view import VIEW
  2. from view.python_core.io import write_tif_2Dor3D
  3. from common import initialize_test_yml_list_measurement
  4. import numpy as np
  5. def run_artifact_correction(flags_to_update, output_suffix=None, tiny_dataset=False):
  6. test_yml, test_animal, test_measu = initialize_test_yml_list_measurement(tiny_dataset=tiny_dataset)
  7. flags = {
  8. "LE_BleachCorrMethod": "None",
  9. "LE_BleachExcludeStimulus": False,
  10. "LE_BleachExcludeArea": False,
  11. "LE_BleachCutBorder": 0,
  12. "LE_ScatteredLightFactor": 0,
  13. "LE_ScatteredLightRadius": 50,
  14. }
  15. flags.update(flags_to_update)
  16. vo = VIEW()
  17. vo.update_flags_from_ymlfile(yml_filename=test_yml)
  18. if isinstance(flags, dict):
  19. vo.update_flags(flags)
  20. vo.initialize_animal(animal=test_animal)
  21. vo.load_measurement_data_from_current_animal(measu=test_measu)
  22. if output_suffix is not None:
  23. op_dir = vo.flags.get_processed_data_dir_path() / "Artifact_corrected_raw"
  24. op_dir.mkdir(exist_ok=True)
  25. op_filename = op_dir / f"{test_animal}_{test_animal}{output_suffix}.tif"
  26. write_tif_2Dor3D(array_xy_or_xyt=vo.p1.raw1, tif_file=op_filename)
  27. return vo
  28. def test_replace_init_frames():
  29. """
  30. testing loading data with Data_ReplaceInitFrames = 5
  31. """
  32. frames2replace = 5
  33. vo = run_artifact_correction(flags_to_update={"Data_ReplaceInitFrames": frames2replace})
  34. temp = vo.p1.raw1
  35. # after replacement, the first <frames2replace + 1> frames are identical to the first frame,
  36. # while <frames2replace + 2>th frame isn't
  37. assert sum(np.allclose(temp[:, :, 0], temp[:, :, i]) for i in range(frames2replace + 2)) == frames2replace + 1
  38. def test_no_bleach_method():
  39. """
  40. testing loading data without bleach correction
  41. """
  42. run_artifact_correction(
  43. flags_to_update={},
  44. # output_suffix="_BC_None"
  45. )
  46. def test_no_bleach_with_scatter_light_correction():
  47. """
  48. testing loading data without bleach correction, but with scatter light correction
  49. """
  50. run_artifact_correction(
  51. flags_to_update={"LE_ScatteredLightFactor": 1},
  52. # output_suffix="_BC_None"
  53. )
  54. def test_log_bleach_pixelwise_1cpu():
  55. """
  56. testing loading data using pixelwise log bleach correction (non-parallel)
  57. """
  58. run_artifact_correction(
  59. flags_to_update={
  60. "LE_BleachCorrMethod": "log_pixelwise_1cpu"
  61. },
  62. tiny_dataset=True
  63. # output_suffix="_BC_log_pixelwise"
  64. )
  65. def test_log_bleach_pixelwise_parallel():
  66. """
  67. testing loading data using pixelwise log bleach correction (parallel)
  68. """
  69. run_artifact_correction(
  70. flags_to_update={
  71. "LE_BleachCorrMethod": "log_pixelwise_parallel"
  72. },
  73. # output_suffix="_BC_log_pixelwise"
  74. )
  75. def test_log_bleach_pixelwise_excluding_area():
  76. """
  77. testing loading data using pixelwise log bleach correction (parallel) with area exclusion
  78. """
  79. run_artifact_correction(
  80. flags_to_update={
  81. "LE_BleachCorrMethod": "log_pixelwise_parallel",
  82. "LE_BleachExcludeArea": True
  83. },
  84. # output_suffix="_BC_log_pixelwise_excludingArea"
  85. )
  86. def test_log_bleach_pixelwise_excluding_stimulus():
  87. """
  88. testing loading data using pixelwise log bleach correction (parallel) with stimulus exclusion
  89. """
  90. run_artifact_correction(
  91. flags_to_update={
  92. "LE_BleachCorrMethod": "log_pixelwise_parallel",
  93. "LE_BleachExcludeStimulus": True,
  94. "LELog_ExcludeSeconds": 5,
  95. "LE_PrestimEndBackground": 5
  96. },
  97. # output_suffix="_BC_log_pixelwise_excludingStimulus"
  98. )
  99. def test_log_bleach_uniform():
  100. """
  101. testing loading data using uniform log bleach correction
  102. """
  103. run_artifact_correction(
  104. flags_to_update={
  105. "LE_BleachCorrMethod": "log_uniform"
  106. },
  107. # output_suffix="_BC_log_uniform"
  108. )
  109. def test_log_bleach_uniform_excluding_area():
  110. """
  111. testing loading data using uniform log bleach correction with area exclusion
  112. """
  113. run_artifact_correction(
  114. flags_to_update={
  115. "LE_BleachCorrMethod": "log_uniform",
  116. "LE_BleachExcludeArea": True
  117. },
  118. # output_suffix="_BC_log_uniform_excludingArea"
  119. )
  120. def test_log_bleach_uniform_excluding_stimulus():
  121. """
  122. testing loading data using uniform log bleach correction with stimulus exclusion
  123. """
  124. run_artifact_correction(
  125. flags_to_update={
  126. "LE_BleachCorrMethod": "log_uniform",
  127. "LE_BleachExcludeStimulus": True,
  128. "LELog_ExcludeSeconds": 5,
  129. "LE_PrestimEndBackground": 5
  130. },
  131. # output_suffix="_BC_log_uniform_excludingStimulus"
  132. )
  133. def test_artifact_correction_filters_only():
  134. """
  135. testing loading data with no bleach correction, but with median filtering
  136. """
  137. flags_to_test = [
  138. {"Data_Median_Filter": 0},
  139. {"Data_Median_Filter": 1},
  140. {"Data_Median_Filter": 2},
  141. {"Data_Median_Filter": 3, "Data_Median_Filter_space": 2, "Data_Median_Filter_time": 3},
  142. {"Data_Mean_Filter": 0},
  143. {"Data_Mean_Filter": 1},
  144. {"Data_Mean_Filter": 2},
  145. {"Data_Mean_Filter": 3, "Data_Mean_Filter_space": 10, "Data_Mean_Filter_time": 10}
  146. ]
  147. for flags in flags_to_test:
  148. run_artifact_correction.description = f"Testing raw data filtering with {flags}"
  149. yield run_artifact_correction, flags
  150. if __name__ == '__main__':
  151. # test_no_bleach_method()
  152. # test_log_bleach_pixelwise_1cpu()
  153. # test_log_bleach_pixelwise_parallel()
  154. # test_log_bleach_pixelwise_excluding_stimulus()
  155. # test_log_bleach_uniform()
  156. # test_log_bleach_uniform_excluding_area()
  157. # test_log_bleach_uniform_excluding_stimulus()
  158. # test_no_bleach_with_scatter_light_correction()
  159. test_replace_init_frames()
  160. # run_artifact_correction(
  161. # flags_to_update={"Data_Median_Filter": 3, "Data_Median_Filter_space": 10, "Data_Median_Filter_time": 10},
  162. # output_suffix="_MedianFilteredSpace10Time10"
  163. # )
  164. #
  165. # run_artifact_correction(
  166. # flags_to_update={"Data_Mean_Filter": 3, "Data_Mean_Filter_space": 10, "Data_Mean_Filter_time": 10},
  167. # output_suffix="_MeanFilteredSpace10Time10"
  168. # )