export_movie_test.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. from common import initialize_test_yml_list_measurement
  2. from view import VIEW
  3. import logging
  4. import pathlib as pl
  5. import shutil
  6. from nose.tools import raises
  7. def export_fake_data_movie(flags_to_update, movie_name_suffix):
  8. test_yml, test_animal, test_measu = initialize_test_yml_list_measurement()
  9. view = VIEW()
  10. view.update_flags_from_ymlfile(test_yml)
  11. view.update_flags(flags_to_update)
  12. view.load_measurement_data(test_animal, test_measu)
  13. view.calculate_signals()
  14. op_filename = view.export_movie_for_current_measurement()
  15. test_movies_folder = pl.Path(view.flags["STG_OdorReportPath"]) / "test_movies"
  16. if not test_movies_folder.is_dir():
  17. test_movies_folder.mkdir()
  18. op_filepath = pl.Path(op_filename)
  19. test_movie_path = test_movies_folder / f"{op_filepath.stem}{movie_name_suffix}{op_filepath.suffix}"
  20. if test_movie_path.is_dir():
  21. shutil.rmtree(test_movie_path)
  22. op_filepath.replace(test_movie_path)
  23. def test_defaults():
  24. """
  25. Testing exporting movie with default flags
  26. """
  27. export_fake_data_movie({}, "defaults")
  28. def test_rotate_flags():
  29. """
  30. Testing export_movie with different rotate flags
  31. """
  32. for rot in range(1, 8):
  33. flags = {"mv_rotateImage": rot}
  34. export_fake_data_movie(flags, f"mv_rotateImage_{rot}")
  35. flags = {"mv_reverseIt": True}
  36. export_fake_data_movie(flags, "mv_reverseIt_True")
  37. def test_scale_flags():
  38. """
  39. Testing export_movie with different scale flags
  40. """
  41. percentile_value = 20
  42. flags_with_percentile = {"mv_percentileScale": True, "mv_percentileValue": percentile_value}
  43. flags_without_percentile = {}
  44. cutborder = 5
  45. flags_with_cutborder = {"mv_cutborder": cutborder}
  46. flag_types = {f"_percentileValue{percentile_value}": flags_with_percentile,
  47. "": flags_without_percentile,
  48. f"_cutborder{cutborder}": flags_with_cutborder}
  49. for label, flags_to_copy in flag_types.items():
  50. for indiscale in [1, 2, 4, 5, 6,
  51. 11, 12, 14, 15, 16,
  52. 21, 22, 24, 25, 26]:
  53. flags = flags_to_copy.copy()
  54. flags["mv_individualScale"] = indiscale
  55. export_fake_data_movie(flags, f"mv_individualScale{indiscale}{label}")
  56. for indiscale in [3, 13, 23]:
  57. flags = flags_to_copy.copy()
  58. flags["mv_individualScale"] = indiscale
  59. flags["mv_indiScale3factor"] = 0.25
  60. export_fake_data_movie(flags, f"mv_individualScale{indiscale}_factor0p25{label}")
  61. flags = flags_to_copy.copy()
  62. flags["mv_individualScale"] = indiscale
  63. flags["mv_indiScale3factor"] = 0.4
  64. export_fake_data_movie(flags, f"mv_individualScale{indiscale}_factor0p4{label}")
  65. flags = flags_to_copy.copy()
  66. flags["mv_individualScale"] = indiscale
  67. flags["mv_indiScale3factor"] = 0
  68. export_fake_data_movie(flags, f"mv_individualScale{indiscale}_factor0{label}")
  69. def test_displayTime_flags():
  70. """
  71. Testing export movie with different flags for displaying frame time
  72. """
  73. export_fake_data_movie({"mv_displayTime": 0}, "without_frame_time")
  74. export_fake_data_movie({"mv_displayTime": 0.8}, "with_frame_time")
  75. export_fake_data_movie({"mv_displayTime": 0.5}, "with_frame_time_0p5")
  76. export_fake_data_movie({"mv_displayTime": 0.25}, "with_frame_time_0p25")
  77. export_fake_data_movie({"mv_displayTime": 0.8, "mv_suppressMilliseconds": True}, "with_frame_time_no_ms")
  78. def test_mark_stimulus_flags():
  79. """
  80. Testing export movie with different mark stimulus flags
  81. """
  82. possible_values = [0, 1, 2, 3, 21]
  83. for ms in possible_values:
  84. export_fake_data_movie({"mv_markStimulus": ms}, f"with_mark_stimulus_{ms}")
  85. def test_different_export_formats():
  86. """
  87. Testing export movie with different export formats
  88. """
  89. export_fake_data_movie({"mv_exportFormat": "libx264", 'mv_individualScale': 2}, "_indScale2_libx264")
  90. export_fake_data_movie({"mv_exportFormat": "single_tif", 'mv_individualScale': 2}, "_indScale2")
  91. export_fake_data_movie({"mv_exportFormat": "ayuv", 'mv_individualScale': 2}, "_indScale2_ayuv")
  92. def test_filters():
  93. """
  94. Testing export movie with temporal and spatial filters
  95. """
  96. export_fake_data_movie({"Signal_Signal_FilterSpaceFlag": True, "Signal_Signal_FilterSpaceSize": 3}, "space_filter_3")
  97. export_fake_data_movie({"Signal_Signal_FilterTimeFlag": True, "Signal_Signal_FilterTimeSize": 3}, "time_filter_3")
  98. def test_cutters():
  99. """
  100. Testing export movie with temporal and spatial cutters
  101. """
  102. export_fake_data_movie({"mv_FirstFrame": 5, "mv_LastFrame": 75,
  103. 'mv_individualScale': 3,
  104. "mv_indiScale3factor": 0.25
  105. }, "five_frame_cut_start_end")
  106. export_fake_data_movie({"mv_cutborder": 5,
  107. 'mv_individualScale': 3,
  108. "mv_indiScale3factor": 0.25
  109. }, "mv_cutborder_5")
  110. def test_correct_stimulus_onset():
  111. """
  112. Testing export movie with stimulus onset correction
  113. """
  114. export_fake_data_movie({"mv_correctStimulusOnset": 10, "mv_markStimulus": 1}, "mv_correct_stimulus_onset_10")
  115. export_fake_data_movie({"mv_correctStimulusOnset": 1300, "mv_markStimulus": 1}, "mv_correct_stimulus_onset_1300")
  116. def test_thresholdOn():
  117. """
  118. Testing export movie with different values of mv_thresholdOn
  119. """
  120. threshold_on_vals = {"foto1": [("a1000", "a400"), ("r50", "r30")],
  121. "raw1": [("a1000", "a400"), ("r60", "r10")],
  122. "sig1": [("a0.75", "a0.65"), ("r60", "r10")]}
  123. for within_area in (True, False):
  124. for threshold_on, threshold_vals in threshold_on_vals.items():
  125. for (threshold_pos, threshold_neg) in threshold_vals:
  126. export_fake_data_movie({
  127. 'mv_withinArea': within_area,
  128. 'mv_thresholdOn': threshold_on,
  129. 'mv_lowerThreshPositiveResps': threshold_pos,
  130. 'mv_upperThreshNegativeResps': threshold_neg,
  131. 'mv_individualScale': 3,
  132. "mv_indiScale3factor": 0.25},
  133. f"mv_thresholdOn_{threshold_on}"
  134. f"_vals_{threshold_pos}{threshold_neg}_withinArea_{within_area}")
  135. def test_thresholdShowImage():
  136. """
  137. Testing export movie with different settings for mv_thresholdShowImage
  138. """
  139. for threshold_show_image in ["foto1", "raw1", "bgColor"]:
  140. for threshold_scale in ["full", "onlyShown"]:
  141. export_fake_data_movie({"mv_thresholdShowImage": threshold_show_image,
  142. "mv_thresholdScale": threshold_scale,
  143. 'mv_thresholdOn': "foto1",
  144. 'mv_lowerThreshPositiveResps': "a1000",
  145. 'mv_individualScale': 3,
  146. "mv_indiScale3factor": 0.25
  147. },
  148. f"mv_thresholdOn_foto1_posVal_a1000_Image_"
  149. f"{threshold_show_image}_scale_{threshold_scale}")
  150. def test_withinArea():
  151. """
  152. Testing export movie with mv_withinArea set
  153. """
  154. export_fake_data_movie({'mv_withinArea': True,
  155. 'mv_individualScale': 3,
  156. "mv_indiScale3factor": 0.25},
  157. "mv_within_Mask_True")
  158. export_fake_data_movie({'mv_withinArea': True,
  159. 'mv_individualScale': 3,
  160. "mv_indiScale3factor": 0.25,
  161. "mv_cutborder": 5},
  162. "mv_within_Mask_True_cutBorder5")
  163. def test_bgColor():
  164. """
  165. Testing export movie with mv_bgColor set
  166. """
  167. export_fake_data_movie({"mv_bgColor": "g"},
  168. "mv_with_bgColor_green")
  169. def test_fgColor():
  170. """
  171. Testing export movie with mv_fgColor set
  172. """
  173. export_fake_data_movie({"mv_fgColor": "m"},
  174. "mv_with_fgColor_magenta")
  175. def test_mark_rois():
  176. """
  177. Testing export movie with ROIs marked
  178. """
  179. base_flags = {'mv_individualScale': 3, "mv_indiScale3factor": 0.25}
  180. test_values = [10, 13, 14, 15]
  181. for test_value in test_values:
  182. flags2use = base_flags.copy()
  183. flags2use["mv_showROIs"] = test_value
  184. export_fake_data_movie(flags_to_update=flags2use, movie_name_suffix=f"mv_showROIs{test_value}")
  185. flags2use_new = flags2use.copy()
  186. flags2use_new["mv_rotateImage"] = 3
  187. export_fake_data_movie(flags_to_update=flags2use_new, movie_name_suffix=f"mv_showROIs{test_value}_rotate3")
  188. flags2use_new = flags2use.copy()
  189. flags2use_new["mv_cutborder"] = 5
  190. export_fake_data_movie(flags_to_update=flags2use_new, movie_name_suffix=f"mv_showROIs{test_value}_cutborder5")
  191. @raises(ValueError)
  192. def test_large_bordercut():
  193. """
  194. Testing export movie when mv_cutborder is inappropriately large
  195. """
  196. export_fake_data_movie({"mv_cutborder": 106}, "mv_impossible")
  197. def test_fonts():
  198. """
  199. Testing export movie with different fonts
  200. """
  201. export_fake_data_movie({"mv_markStimulus": 2, "mv_fontName": "DroidSerif-Bold"}, "mv_fontName_DroidSerifBold")
  202. export_fake_data_movie({"mv_markStimulus": 2, "mv_fontName": "OpenSans-Regular"}, "mv_fontName_OpenSansRegular")
  203. export_fake_data_movie({"mv_markStimulus": 2, "mv_fontName": "DejaVuSerif-Bold"}, "mv_fontName_DejaVuSerifBold")
  204. def test_mv_ygap():
  205. """
  206. Testing different settings of mv_ygap
  207. """
  208. for mv_ygap in [0, 10, 50, 100]:
  209. export_fake_data_movie({"mv_ygap": mv_ygap}, f"mv_ygap_{mv_ygap}")
  210. def test_scale_legend_factor():
  211. """
  212. Testing setting mv_scaleLegendFactor
  213. """
  214. for factor in (10, 100):
  215. export_fake_data_movie({"mv_individualScale": 2,
  216. "mv_percentileScale": True,
  217. "mv_percentileValue": 20,
  218. "mv_scaleLegendFactor": factor},
  219. f"_scaleLegendFactor{factor}")
  220. def test_bit_rate():
  221. """
  222. Testing setting mv_bitrate
  223. """
  224. export_fake_data_movie(
  225. {
  226. "mv_individualScale": 2,
  227. "mv_bitrate": f"{12 * 1024}k",
  228. "mv_exportFormat": "libx264"
  229. },
  230. "_bitrate_12M"
  231. )
  232. # def test_with_recorded_data():
  233. # """
  234. # Testing view.python_core.movies.export_movie with recorded data
  235. # :return:
  236. # """
  237. #
  238. # example_data_path = get_example_data_root_path()
  239. #
  240. # test_list = example_data_path / "IP_Fura" / "IDLlist" / "190112_locust_ip.lst.xls"
  241. # test_yml = example_data_path / "IP_Fura" / "usage_till.yml"
  242. # full_output_name_without_extension = str(example_data_path / "IP_Fura" / "IDLoutput" / "movie_test")
  243. # test_measu = 0
  244. #
  245. # # test_list = example_data_path / "20190821_SetupB_Pixel_Calibration"/ "Lists" / "pixel_calibration.lst"
  246. # # test_yml = example_data_path/ "20190821_SetupB_Pixel_Calibration"/ "calibration.yml"
  247. # # full_output_name_without_extension = \
  248. # # example_data_path/"20190821_SetupB_Pixel_Calibration"/ "IDLoutput"/ "test_movie"
  249. # # test_measu = 2
  250. #
  251. #
  252. # # test_list = "/home/aj/SharedWithWindows/Sercan_CalciumGreen/Lists/20190809_SS_locust004_CaGreen.lst.xls"
  253. # # test_yml = '/home/aj/SharedWithWindows/Sercan_CalciumGreen/usage_till.yml'
  254. # # full_output_name_without_extension = "/home/aj/SharedWithWindows/Sercan_CalciumGreen/IDLoutput/movie"
  255. # # test_measu = 0
  256. #
  257. #
  258. # flags = FlagsManager()
  259. # flags.read_flags_from_yml(str(test_yml))
  260. #
  261. #
  262. # # flags.update_flags({"mv_exportFormat": "rawvideo"})
  263. # # flags.update_flags({"mv_exportFormat": "libx264"})
  264. # # flags.update_flags({"mv_bitrate": "2M"})
  265. #
  266. # # flags.update_flags({"mv_exportFormat": "single_tif"})
  267. #
  268. # flags.update_flags({"mv_exportFormat": "stack_tif"})
  269. #
  270. # flags.update_flags({"mv_displayTime": True})
  271. # # flags.update_flags({"mv_markStimulus": 0})
  272. # # flags.update_flags({"mv_markStimulus": 1})
  273. # flags.update_flags({"mv_markStimulus": 2})
  274. #
  275. # flags.update_flags({"mv_individualScale": 2})
  276. #
  277. # # flags.update_flags({"mv_individualScale": 3})
  278. # # flags.update_flags({"mv_indiScale3factor": 0.2})
  279. #
  280. # flags.update_flags({"mv_suppressMilliseconds": False})
  281. #
  282. # flags.update_flags({"CTV_scalebar": True})
  283. #
  284. # flags.update_flags({"mv_xgap": 60})
  285. # flags.update_flags({"mv_ygap": 60})
  286. #
  287. # # flags.update_flags({"mv_reverseIt": True})
  288. #
  289. # # flags.update_flags({"mv_rotateImage": 7})
  290. #
  291. # # flags.update_flags({"mv_SpeedFactor": 0.333})
  292. #
  293. # # flags.update_flags({"mv_percentileScale": True})
  294. # # flags.update_flags({"mv_percentileValue": 10})
  295. #
  296. # measurement_list = MeasurementList(str(test_list), LE_loadExp="3")
  297. #
  298. # p1 = measurement_list.get_p1_metadata_by_measu(test_measu)
  299. #
  300. # loadDataMaster(p1=p1, flag=flags.to_series())
  301. #
  302. # CalcSigMaster(p1=p1, flag=flags.to_series())
  303. #
  304. # export_movie(p1, flags=flags,
  305. # full_filename_without_extension=full_output_name_without_extension)
  306. #
  307. if __name__ == "__main__":
  308. logging.basicConfig(level=logging.INFO)
  309. # test_with_recorded_data()
  310. # test_scale_flags()
  311. test_large_bordercut()
  312. # test_withinArea()
  313. # test_correct_stimulus_onset()
  314. # test_mark_rois()
  315. # test_different_export_formats()
  316. # test_bit_rate()