export_traces_test.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. from common import initialize_test_yml_list_measurement
  2. from view import VIEW
  3. import pathlib as pl
  4. import shutil
  5. from view.python_core.ctvs import get_all_available_ctvs
  6. from view.python_core.gdm_generation.gdm_data_classes import GDMFile
  7. class TraceExporter(object):
  8. def __init__(self):
  9. super().__init__()
  10. test_yml, self.test_animal, self.test_measu = initialize_test_yml_list_measurement()
  11. self.view = VIEW()
  12. self.view.update_flags_from_ymlfile(test_yml)
  13. def load_and_export(self, flags_to_update, file_suffix, flags_suffix):
  14. self.view.update_flags(flags_to_update)
  15. self.view.initialize_animal(self.test_animal)
  16. roi_data_dict, roi_file = self.view.get_roi_info_for_current_animal()
  17. # initialize and empty data frame to accumulate data
  18. gdm_file = GDMFile()
  19. # iterate over measurements of the animal
  20. for measu in self.view.get_measus_for_current_animal(analyze_values_to_use=(1,)):
  21. # load a measurement for the animal
  22. self.view.load_measurement_data_from_current_animal(measu)
  23. # calculate signals
  24. self.view.calculate_signals()
  25. # create glodatamix for the loaded measurement
  26. gdm_file_this_measu, _ = self.view.get_gdm_file_for_current_measurement(roi_data_dict)
  27. # accumulate
  28. gdm_file.append_from_a_gdm_file(gdm_file_this_measu)
  29. # compose output file name
  30. output_file = self.view.flags.get_gloDatamix_file_for_current_animal()
  31. output_file_path = pl.Path(output_file)
  32. test_gdm_folder =\
  33. pl.Path(self.view.flags["STG_OdorReportPath"]) / "test_gdms" / \
  34. f"{output_file_path.stem}{file_suffix}"
  35. if not test_gdm_folder.is_dir():
  36. test_gdm_folder.mkdir(parents=True)
  37. test_output_file = test_gdm_folder / f"gdm{flags_suffix}{output_file_path.suffix}"
  38. # save gloDatamix file
  39. gdm_file.write_to_csv(test_output_file)
  40. def test_export_traces_rois():
  41. """
  42. Testing exporting traces using .roi files
  43. """
  44. exporter = TraceExporter()
  45. coor_path = pl.Path(exporter.view.flags["STG_OdormaskPath"])
  46. dest_roi_file = coor_path / "Fake_data.roi"
  47. for fle in coor_path.iterdir():
  48. if fle.name.startswith("FakeData") and fle.suffix == ".roi":
  49. shutil.copy(str(fle), str(dest_roi_file))
  50. exporter.load_and_export(
  51. flags_to_update={"RM_ROITrace": 3},
  52. file_suffix=f"_from_roi{fle.stem.lstrip('FakeData')}",
  53. flags_suffix="_defaults"
  54. )
  55. dest_roi_file.unlink()
  56. def test_export_traces_mask_tif():
  57. """
  58. Testing exporting traces using .roi.tif files
  59. """
  60. exporter = TraceExporter()
  61. exporter.load_and_export(
  62. flags_to_update={"RM_ROITrace": 4},
  63. file_suffix="_from_roi_tif",
  64. flags_suffix="_defaults"
  65. )
  66. def test_export_traces_different_ctvs():
  67. """
  68. Testing exporting traces with different CTVs
  69. """
  70. exporter = TraceExporter()
  71. for ctv in get_all_available_ctvs():
  72. exporter.load_and_export(
  73. flags_to_update={"RM_ROITrace": 3, "CTV_Method": ctv},
  74. file_suffix=f"_from_roi",
  75. flags_suffix=f"_ctv{ctv}"
  76. )
  77. def test_export_traces_within_ROI():
  78. """
  79. Testing exporting traces considering the area file
  80. """
  81. exporter = TraceExporter()
  82. exporter.load_and_export(
  83. flags_to_update={"RM_ROITrace": 3, "GDM_withinArea": True},
  84. file_suffix="_from_roi",
  85. flags_suffix="_withinArea_True"
  86. )
  87. def test_export_traces_chunks_only():
  88. """
  89. Testing exporting traces considering the area file
  90. """
  91. exporter = TraceExporter()
  92. exporter.load_and_export(
  93. flags_to_update=
  94. {
  95. "RM_ROITrace": 3,
  96. "GDM_outputType": "chunks_only",
  97. "GDM_chunkPostStim": 2, # in seconds
  98. "GDM_chunkPreStim": 2, # in seconds
  99. },
  100. file_suffix="_chunks_only",
  101. flags_suffix="_2secPrePostStim"
  102. )
  103. exporter.load_and_export(
  104. flags_to_update=
  105. {
  106. "RM_ROITrace": 3,
  107. "GDM_outputType": "chunks_only",
  108. "GDM_chunkPostStim": 100, # in seconds
  109. "GDM_chunkPreStim": 100, # in seconds
  110. },
  111. file_suffix="_chunks_only",
  112. flags_suffix="_full"
  113. )
  114. if __name__ == '__main__':
  115. test_export_traces_rois()
  116. # test_export_traces_mask_tif()
  117. # test_export_traces_within_ROI()
  118. test_export_traces_chunks_only()