create_measurement_list_lif.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. '''
  2. Creates .lst file from/for Leica .lif files.
  3. Author: Giovanni, April 2022, based on template in VIEW folder by Ajay & others
  4. Expected data structure:
  5. In the folder "01_DATA", each animal has a .lif file
  6. i.e. all measurements of one animal are in that single file
  7. There is a sister folder "02_LISTS"
  8. Output:
  9. In the folder "02_LISTS":
  10. There will be a file Animal.lst.xlsx, e.g. "glom17_210923_bee11.lst.xlsx"
  11. That file contains one line for each measurement.
  12. Measurements that have no time dimension (snapshots, z-stacks) will have "0" in the column "analyze"
  13. What to do next?
  14. In this file, change values that are global
  15. or insert a function that can extract odor name or concentration name from somewhere
  16. In the Animal.lst.xlsx file, correct/complete entries (e.g. odor names, odor concentrations)
  17. Make sure stimulus timing information is correct
  18. Why do I need a .lst.xlsx file?
  19. Load measurements in pyVIEW using this .lst file, so that stimulus information is correct
  20. For off-line analysis, information is taken from this file.
  21. Good to know:
  22. Information, where possible, is taken from the OME header of the incoming .lif file.
  23. If that information is wrong, incomplete, or else, modify the code
  24. '''
  25. from view.python_core.measurement_list import MeasurementList
  26. from view.python_core.measurement_list.importers import get_importer_class
  27. from view.python_core.flags import FlagsManager
  28. from collections import OrderedDict
  29. import pandas as pd
  30. import logging
  31. import pathlib as pl
  32. logging.basicConfig(level=logging.INFO)
  33. # ------------------- Some parameters about experimental setup, data structure and output file type --------------------
  34. # 3 for single wavelength Till Photonics Measurements
  35. # 4 for two wavelength Till Photonics Measurements
  36. # 20 for Zeiss Confocal Measurements
  37. # 21 for Leica Confocal Measurements: .lif file
  38. LE_loadExp = 21 # 21 for .lif file
  39. # Mother of all Folders of your dataset
  40. # On Windows, if you copy paths from the file explorer, make sure the string below is always of the form r"......"
  41. STG_MotherOfAllFolders = r'/mnt/Data/ViewData/MP_LIF/'
  42. # path of the "Data" folder in VIEW organization containing the data
  43. # On Windows, if you copy paths from the file explorer, make sure the string below is always of the form r"......"
  44. STG_Datapath = r"01_DATA"
  45. # path of the "Lists" folder in VIEW organization containing the list files
  46. # On Windows, if you copy paths from the file explorer, make sure the string below is always of the form r"......"
  47. STG_OdorInfoPath = r"02_LISTS"
  48. # Choose measurement list output extension among ".lst", ".lst.xlsx", ".settings.xlsx"
  49. # VIEW does not support writing .xls list files anymore (nonetheless, it can read them and revise/update them to .xlsx)
  50. measurement_output_extension = ".lst.xlsx"
  51. # ------------------- A dictionary containing default values for metadata.----------------------------------------------
  52. # ------------------- Only metadata included in this dictionary will be written ----------------------------------------
  53. # ----Note that columns of the output measeurement list files will have the same order as below.------------------------
  54. default_values = OrderedDict()
  55. default_values['Measu'] = 0 # unique identifier for each line, corresponds to item in TILL photonics log file
  56. default_values['Label'] = "none"
  57. default_values['Odour'] = 'odor?' # stimulus name, maybe extracted from label in the function "custom_func" below
  58. default_values['OConc'] = 0 # odor concentration, maybe extracted from label in the function "custom_func" below
  59. default_values['Analyze'] = -1 # whether to analyze in VIEWoff. Default -1, which means "not checked yet"
  60. default_values['Cycle'] = 0 # how many ms per frame
  61. default_values['DBB1'] = 'none' # file name of raw data
  62. default_values['UTC'] = 0 # recording time, extracted from file
  63. default_values['PxSzX'] = '0.0' # um per pixel, 1.5625 for 50x air objective, measured by Hanna Schnell July 2017 on Till vision system, with a binning of 8
  64. default_values['PxSzY'] = '0.0' # um per pixel, 1.5625 for 50x air objective, measured by Hanna Schnell July 2017 on Till vision system, with a binning of 8
  65. default_values['Lambda'] = 0 # wavelength of stimulus. In TILL, from .log file, In Zeiss LSM, from .lsm file
  66. # These will be automatically filed for LE_loadExp=4
  67. default_values['dbb2'] = 'none' # file name of raw data in dual wavelength recordings (FURA)
  68. # To include more columns, uncomment entries below and specify a default value.
  69. # #
  70. # block for first stimulus
  71. # default_values['StimON'] = -1 # stimulus onset, unit: frames, count starts at frame 1.
  72. # default_values['StimOFF'] = -1 # stimulus offset, unit: frames, count starts at frame 1.
  73. # default_values['StimLen'] = 0 # stimulus onset in ms from beginning - alternative to StimON
  74. # default_values['StimONms'] = -1 # stimulus length in ms - alternative to StimOFF
  75. # #
  76. # block for second stimulus
  77. # default_values['Stim2ON'] = 0 # stimulus onset, unit: frames, count starts at frame 1.
  78. # default_values['Stim2OFF'] = 0 # stimulus offset, unit: frames, count starts at frame 1.
  79. # default_values['Stim2Len'] = 0 # stimulus onset in ms from beginning - alternative to StimON
  80. # default_values['Stim2ONms'] = -1 # stimulus length in ms - alternative to StimOFF
  81. # #
  82. # default_values['Age'] = -1
  83. # default_values['Sex'] = 'o'
  84. # default_values['Side'] = 'none'
  85. # default_values['Comment'] = 'none'
  86. # #
  87. # default_values['MTime'] = 0
  88. # default_values['Control'] = 0
  89. # default_values['Pharma'] = 'none'
  90. # default_values['PhTime'] = 0
  91. # default_values['PhConc'] = 0
  92. # default_values['ShiftX'] = 0
  93. # default_values['ShiftY'] = 0
  94. # default_values['StimISI'] = 0
  95. # default_values['setting'] = 'none'
  96. # default_values['dbb3'] = 'none'
  97. # default_values['PosZ'] = 0
  98. # default_values['Countl'] = 0
  99. # default_values['slvFlip'] = 0
  100. # ----------------------------------------------------------------------------------------------------------------------
  101. # ----------------- A function used to modify list entries after automatic parsing of metadata -------------------------
  102. # ----------------- This function indicates what needs to be done for a row --------------------------------------------
  103. # ----------------- The same is internally applied to all rows of the measurement list----------------------------------
  104. # ------------------ names of columns that will be overwritten by old values -------------------------------------------
  105. # -- if you run the same animal a second time!
  106. # ------ these will only be used if a measurement list file with the same name as current output file exists -----------
  107. overwrite_old_values = ["Line", "PxSzX", "PxSzY", "Age", "Sex", "Prefer",
  108. "Comment", "Analyze", "Odour", "OConc"]
  109. # ______________________________________________________________________________________________________________________
  110. def get_odorinfo_from_label(label):
  111. # format for file name (label) is:
  112. # odor_concentration_anything_else.tif
  113. # separating element is underscore
  114. # is the information for a concentration present? Detect "-"
  115. parts = label.split("_")
  116. if len(parts) > 1:
  117. odor = parts[0]
  118. concentration = parts[1]
  119. # in the case the name is odor_conc.tif:
  120. if concentration[-4:] == '.tif':
  121. concentration = concentration[:-4]
  122. else:
  123. odor = 'odor?'
  124. concentration = 'conc?'
  125. return [odor, concentration]
  126. def custom_func(list_row: pd.Series, animal_tag: str) -> pd.Series:
  127. #TODO
  128. #add info "analyze" and set to 0 for snapshots
  129. # Examples:
  130. # list_row["StimON"] = 25
  131. # list_row["Odour"] = get_odor_from_label(list_row["Label"])
  132. # if list_row["Measu"]
  133. # get Odor from another file based on the value of <animal_tag> and list_row["Label"]
  134. list_row["StimONms"] = '3000'
  135. list_row["StimLen"] = '2000'
  136. list_row["Comment"] = 'create_measurement_list_lif'
  137. list_row["Line"] = 'bee'
  138. #extract odor and concentration from name
  139. (list_row["Odour"],list_row["OConc"]) = get_odorinfo_from_label(list_row["Label"])
  140. try:
  141. float(list_row["OConc"])
  142. except: #Odour concentration is not a number, set to fictive 0
  143. list_row["OConc"] = '0.0'
  144. if list_row["Label"][-4:] == '.tif':
  145. list_row["Label"] = list_row["Label"][:-4]
  146. return list_row
  147. # ----------------------------------------------------------------------------------------------------------------------
  148. # ------------------ A function defining the criteria for excluding measurements ---------------------------------------
  149. # ------------------ Currently applicable only for tillvision setups ---------------------------------------------------
  150. def measurement_filter(s):
  151. # exclude blocks that have in the name "Snapshot" or "Delta"
  152. # or that do not have any "_"
  153. name = s["Label"]
  154. label_not_okay = name.count('Snapshot') > 0 or name.count('Delta') > 0 or name.count('_') < 1
  155. label_okay = not label_not_okay
  156. # exclude blocks with less than two frames or no calibration
  157. atleast_two_frames = False
  158. if type(s["Timing_ms"]) is str:
  159. if len(s["Timing_ms"].split(' ')) >= 2 and s["Timing_ms"] != "(No calibration available)":
  160. atleast_two_frames = True
  161. return label_okay and atleast_two_frames
  162. # ______________________________________________________________________________________________________________________
  163. if __name__ == "__main__":
  164. # initialize a FlagsManager object with values specified above
  165. flags = FlagsManager()
  166. flags.update_flags({"STG_MotherOfAllFolders": STG_MotherOfAllFolders,
  167. "STG_OdorInfoPath" : STG_OdorInfoPath,
  168. "STG_Datapath" : STG_Datapath})
  169. # initialize importer
  170. importer_class = get_importer_class(LE_loadExp)
  171. importer = importer_class(default_values)
  172. # open a dialog for choosing raw data files
  173. # this returns a dictionary where keys are animal tags (STG_ReportTag) and
  174. # values are lists of associated raw data files
  175. animal_tag_raw_data_mapping = importer.ask_for_files(default_dir=flags["STG_Datapath"])
  176. # make sure some files were chosen
  177. assert len(animal_tag_raw_data_mapping) > 0, IOError("No files were chosen!")
  178. for animal_tag, raw_data_files in animal_tag_raw_data_mapping.items():
  179. # automatically parse metadata
  180. metadata_df = importer.import_metadata(raw_data_files=raw_data_files,
  181. measurement_filter=measurement_filter)
  182. # inform user if no usable measurements were found
  183. if metadata_df.shape[0] == 0:
  184. logging.info(f"No usable measurements we found among the files "
  185. f"chosen for the animal {animal_tag}. Not creating a list file")
  186. else:
  187. # create a new Measurement list object from parsed metadata
  188. measurement_list = MeasurementList.create_from_df(LE_loadExp=LE_loadExp,
  189. df=metadata_df)
  190. # apply custom modifications
  191. measurement_list.update_from_custom_func(custom_func=custom_func, animal_tag=animal_tag)
  192. # set anaylze to 0 if raw data files don't exist
  193. flags.update_flags({"STG_ReportTag": animal_tag})
  194. measurement_list.sanitize(flags=flags,
  195. data_file_extensions=importer.movie_data_extensions)
  196. # construct the name of the output file
  197. out_file = f"{flags.get_lst_file_stem()}{measurement_output_extension}"
  198. # write measurement file to list
  199. measurement_list.write_to_list_file(lst_fle=out_file, columns2write=default_values.keys(),
  200. overwrite_old_values=overwrite_old_values)