create_measurement_list.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. from view.python_core.measurement_list import MeasurementList
  2. from view.python_core.measurement_list.importers import get_importer_class
  3. from view.python_core.flags import FlagsManager
  4. from collections import OrderedDict
  5. import pandas as pd
  6. import logging
  7. logging.basicConfig(level=logging.INFO)
  8. # ------------------- Some parameters about experimental setup, data structure and output file type --------------------
  9. # 3 for single wavelength Till Photonics Measurements
  10. # 4 for two wavelength Till Photonics Measurements
  11. # 20 for Zeiss Confocal Measurements
  12. LE_loadExp = 3
  13. # Mother of all Folders of your dataset
  14. STG_MotherOfAllFolders = "/home/aj/SharedWithWindows/view_test_data/Or47a_test"
  15. # path of the "Data" folder in VIEW organization containing the data
  16. STG_Datapath = "01_DATA"
  17. # path of the "Lists" folder in VIEW organization containing the list files
  18. STG_OdorInfoPath = "02_SETTINGS"
  19. # Choose measurement list output extension among ".lst", ".lst.xls", ".settings.xls"
  20. measurement_output_extension = ".lst.xls"
  21. # ------------------- A dictionary containing default values for metadata.----------------------------------------------
  22. # ------------------- Only metadata included in this dictionary will be written ----------------------------------------
  23. # ----Note that columns of the output measeurement list files will have the same order as below.------------------------
  24. default_values = OrderedDict()
  25. default_values['Measu'] = 0 # unique identifier for each line, corresponds to item in TILL photonics log file
  26. default_values['Label'] = "none"
  27. default_values['Odour'] = 'odor?' # stimulus name, maybe extracted from label in the function "custom_func" below
  28. default_values['OConc'] = 0 # odor concentration, maybe extracted from label in the function "custom_func" below
  29. default_values['Analyze'] = -1 # whether to analyze in VIEWoff. Default 1
  30. default_values['Cycle'] = 0 # how many ms per frame
  31. default_values['DBB1'] = 'none' # file name of raw data
  32. default_values['UTC'] = 0 # recording time, extracted from file
  33. default_values['PxSzX'] = '4.6' # um per pixel, 1.5625 for 50x air objective, measured by Hanna Schnell July 2017 on Till vision system, with a binning of 8
  34. default_values['PxSzY'] = '4.6' # um per pixel, 1.5625 for 50x air objective, measured by Hanna Schnell July 2017 on Till vision system, with a binning of 8
  35. default_values['Lambda'] = 0 # wavelength of stimulus. In TILL, from .log file, In Zeiss LSM, from .lsm file
  36. # These will be automatically filed for LE_loadExp=4
  37. default_values['dbb2'] = 'none' # file name of raw data in dual wavelength recordings (FURA)
  38. # To include more columns, uncomment entries below and specify a default value.
  39. # #
  40. # block for first stimulus
  41. default_values['StimON'] = 24 # stimulus onset, unit: frames, count starts at frame 1.
  42. default_values['StimOFF'] = 36 # stimulus offset, unit: frames, count starts at frame 1.
  43. # default_values['StimLen'] = 0 # stimulus onset in ms from beginning - alternative to StimON
  44. # default_values['StimONms'] = -1 # stimulus length in ms - alternative to StimOFF
  45. # #
  46. # block for second stimulus
  47. # default_values['Stim2ON'] = 0 # stimulus onset, unit: frames, count starts at frame 1.
  48. # default_values['Stim2OFF'] = 0 # stimulus offset, unit: frames, count starts at frame 1.
  49. # default_values['Stim2Len'] = 0 # stimulus onset in ms from beginning - alternative to StimON
  50. # default_values['Stim2ONms'] = -1 # stimulus length in ms - alternative to StimOFF
  51. # #
  52. # default_values['Age'] = -1
  53. # default_values['Sex'] = 'o'
  54. # default_values['Side'] = 'none'
  55. # default_values['Comment'] = 'none'
  56. # #
  57. # default_values['MTime'] = 0
  58. # default_values['Control'] = 0
  59. # default_values['Pharma'] = 'none'
  60. # default_values['PhTime'] = 0
  61. # default_values['PhConc'] = 0
  62. # default_values['ShiftX'] = 0
  63. # default_values['ShiftY'] = 0
  64. # default_values['StimISI'] = 0
  65. # default_values['setting'] = 'none'
  66. # default_values['dbb3'] = 'none'
  67. # default_values['PosZ'] = 0
  68. # default_values['Countl'] = 0
  69. # default_values['slvFlip'] = 0
  70. # ----------------------------------------------------------------------------------------------------------------------
  71. # ----------------- A function used to modify list entries after automatic parsing of metadata -------------------------
  72. # ----------------- This function indicates what needs to be done for a row --------------------------------------------
  73. # ----------------- The same is internally applied to all rows of the measurement list----------------------------------
  74. def custom_func(list_row: pd.Series) -> pd.Series:
  75. # Example:
  76. # list_row["StimON"] = 25
  77. # list_row["Odor"] = get_odor_from_label(list_row["Label"])
  78. # if list_row["Measu"]
  79. return list_row
  80. # ----------------------------------------------------------------------------------------------------------------------
  81. # ------------------ A function defining the criteria for excluding measurements ---------------------------------------
  82. # ------------------ Currently applicable only for tillvision setups ---------------------------------------------------
  83. def measurement_filter(s):
  84. # exclude blocks that have in the name "Snapshot" or "Delta"
  85. # or that do not have any "_"
  86. name = s["Label"]
  87. label_not_okay = name.count('Snapshot') > 0 or name.count('Delta') > 0 or name.count('_') < 1
  88. label_okay = not label_not_okay
  89. # exclude blocks with less than two frames or no calibration
  90. atleast_two_frames = False
  91. if type(s["Timing_ms"]) is str:
  92. if len(s["Timing_ms"].split(' ')) >= 2 and s["Timing_ms"] != "(No calibration available)":
  93. atleast_two_frames = True
  94. return label_okay and atleast_two_frames
  95. # ______________________________________________________________________________________________________________________
  96. # ------------------ names of columns that will be overwritten by old values -------------------------------------------
  97. # ------ these will only be used if a measurement list file with the same name as current output file exists -----------
  98. overwrite_old_values = ["Line", "PxSzX", "PxSzY", "Age", "Sex", "Prefer",
  99. "Comment", "Analyze", "Odour", "OConc"]
  100. # ______________________________________________________________________________________________________________________
  101. if __name__ == "__main__":
  102. # initialize a FlagsManager object with values specified above
  103. flags = FlagsManager()
  104. flags.update_flags({"STG_MotherOfAllFolders": STG_MotherOfAllFolders,
  105. "STG_OdorInfoPath": STG_OdorInfoPath,
  106. "STG_Datapath": STG_Datapath})
  107. # initialize importer
  108. importer_class = get_importer_class(LE_loadExp)
  109. importer = importer_class(default_values)
  110. # open a dialog for choosing raw data files
  111. # this returns a dictionary where keys are animal tags (STG_ReportTag) and
  112. # values are lists of associated raw data files
  113. animal_tag_raw_data_mapping = importer.ask_for_files(default_dir=flags["STG_Datapath"])
  114. # make sure some files were chosen
  115. assert len(animal_tag_raw_data_mapping) > 0, IOError("No files were chosen!")
  116. for animal_tag, raw_data_files in animal_tag_raw_data_mapping.items():
  117. # automatically parse metadata
  118. metadata_df = importer.import_metadata(raw_data_files=raw_data_files,
  119. measurement_filter=measurement_filter)
  120. # inform user if no usable measurements were found
  121. if metadata_df.shape[0] == 0:
  122. logging.info(f"No usable measurements we found among the files "
  123. f"chosen for the animal {animal_tag}. Not creating a list file")
  124. else:
  125. # create a new Measurementlist object from parsed metadata
  126. measurement_list = MeasurementList.create_from_df(LE_loadExp=LE_loadExp,
  127. df=metadata_df)
  128. flags.update_flags({"STG_ReportTag": animal_tag})
  129. # correct dbb1, dbb2, dbb3 if the paths are not correct, otherwise set analyze=0
  130. measurement_list.sanitize(flags=flags,
  131. data_file_extension=importer.movie_data_extension)
  132. # apply custom modifications
  133. measurement_list.update_from_custom_func(custom_func=custom_func)
  134. # construct the name of the output file
  135. out_file = f"{flags.get_lst_file_stem()}{measurement_output_extension}"
  136. # write measurement file to list
  137. measurement_list.write_to_list_file(lst_fle=out_file, columns2write=default_values.keys(),
  138. overwrite_old_values=overwrite_old_values)
  139. logging.info(f"Wrote {out_file}")