update_measurement_list_template.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. from view.python_core.measurement_list import MeasurementList
  2. from view.python_core.flags import FlagsManager
  3. import easygui
  4. import pandas as pd
  5. import logging
  6. import pathlib as pl
  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. # On Windows, if you copy paths from the file explorer, make sure the string below is always of the form r"......"
  15. STG_MotherOfAllFolders = r""
  16. # path of the "Data" folder in VIEW organization containing the data
  17. # On Windows, if you copy paths from the file explorer, make sure the string below is always of the form r"......"
  18. STG_Datapath = r""
  19. # path of the "Lists" folder in VIEW organization containing the list files
  20. # On Windows, if you copy paths from the file explorer, make sure the string below is always of the form r"......"
  21. STG_OdorInfoPath = r""
  22. # Choose measurement list output extension among ".lst", ".lst.xls", ".settings.xls"
  23. # please use ".lst.xls" when converting old list files
  24. measurement_output_extension = ".lst.xls"
  25. # ----------------------------------------------------------------------------------------------------------------------
  26. # ----------------- A function used to add new columns to the list file ------------------------------------------------
  27. # ----------------- This function indicates how to add new entries to a row --------------------------------------------
  28. # ----------------- possibly using other existing row values -----------------------------------------------------------
  29. # ----------------- The same logic is apply to all rows to create entire new columns -----------------------------------
  30. def custom_func(list_row: pd.Series, animal_tag: str) -> pd.Series:
  31. # NOTE: take care when modifying column values that already exist. Old values will be lost!
  32. # these changes are always required when updating a .LST file used with IDL
  33. # ------------------------------------------------------------------------------------------------------------------
  34. # update to comment to indicate that the list has been imported for use with pyVIEW
  35. list_row["Comment"] += "_2pyVIEW"
  36. stim_cols = ["StimON", "StimOFF", "Stim2ON", "Stim2OFF"]
  37. # frames in IDL .LST files were numbered 1, 2, 3... In pyVIEW they are numbered 0, 1, 2....
  38. for stim_col in stim_cols:
  39. if stim_col in list_row:
  40. list_row[stim_col] -= 1
  41. # ------------------------------------------------------------------------------------------------------------------
  42. # Examples:
  43. # new_columns["Stim2ON"] = 25
  44. # list_row["Odour"] = get_odor_from_label(list_row["Label"])
  45. # if list_row["Measu"]
  46. # get Odor from another file based on the value of <animal_tag> and list_row["Label"]
  47. return list_row
  48. # ----------------------------------------------------------------------------------------------------------------------
  49. if __name__ == "__main__":
  50. # initialize a FlagsManager object with values specified above
  51. flags = FlagsManager()
  52. flags.update_flags({"STG_MotherOfAllFolders": STG_MotherOfAllFolders,
  53. "STG_OdorInfoPath": STG_OdorInfoPath,
  54. "STG_Datapath": STG_Datapath})
  55. # open a dialog for choosing existing list files
  56. existing_measurement_list_files = easygui.fileopenbox(
  57. msg="Choose one or more measurement list files to update", multiple=True,
  58. default=f"{flags.get_list_dir_str()}/*"
  59. )
  60. # make sure some files were chosen
  61. assert len(existing_measurement_list_files) > 0, IOError("No files were chosen!")
  62. for existing_measurement_list_file in existing_measurement_list_files:
  63. # create a measurement list object
  64. measurement_list = MeasurementList.create_from_lst_file(
  65. lst_fle=existing_measurement_list_file, LE_loadExp=LE_loadExp)
  66. # parse animal tag from measurement list file name
  67. animal_tag = measurement_list.get_STG_ReportTag()
  68. # inform user if no usable measurements were found
  69. if len(measurement_list.get_measus()) == 0:
  70. logging.info(f"No usable measurements found in {existing_measurement_list_file}. "
  71. f"Not updating it.")
  72. else:
  73. # apply custom modifications
  74. measurement_list.update_from_custom_func(custom_func=custom_func, animal_tag=animal_tag)
  75. # set analyze to 0 if raw data files don't exist
  76. flags.update_flags({"STG_ReportTag": animal_tag})
  77. measurement_list.sanitize_based_on_loading(flags)
  78. # create the output file name
  79. existing_measurement_list_file_path = pl.Path(existing_measurement_list_file)
  80. ml_name_stem = existing_measurement_list_file_path.name.split(".")[0]
  81. output_file_path \
  82. = existing_measurement_list_file_path.parent / f"{ml_name_stem}{measurement_output_extension}"
  83. # write measurement file to list
  84. measurement_list.write_to_lst_file_cross_format(
  85. output_lst_file=str(output_file_path), backup_current_file=True
  86. )
  87. existing_measurement_list_file_path.unlink()