update_measurement_list.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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"/home/aj/UKN_network_drives/ag_galizia/AjayramaKumaraswamy/Ana_RNPN_SampleSet"
  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"IDL_Data"
  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"IDL_Lists"
  22. # Choose measurement list output extension among ".lst", ".lst.xls", ".settings.xls"
  23. measurement_output_extension = ".lst.xls"
  24. # ----------------------------------------------------------------------------------------------------------------------
  25. # ----------------- A function used to add new columns to the list file ------------------------------------------------
  26. # ----------------- This function indicates how to add new entries to a row --------------------------------------------
  27. # ----------------- possibly using other existing row values -----------------------------------------------------------
  28. # ----------------- The same logic is apply to all rows to create entire new columns -----------------------------------
  29. def custom_func(list_row: pd.Series, animal_tag: str) -> pd.Series:
  30. # NOTE: take care when modifying column values that already exist. Old values will be lost!
  31. if list_row["Odour"] == "Missing":
  32. list_row["Analyze"] = 0
  33. list_row["StimON"] -= 1
  34. list_row["StimOFF"] -= 1
  35. list_row["Stim2ON"] -= 1
  36. list_row["Stim2OFF"] -= 1
  37. list_row["Comment"] += "_2pyVIEW"
  38. # Examples:
  39. # new_columns["Stim2ON"] = 25
  40. # list_row["Odour"] = get_odor_from_label(list_row["Label"])
  41. # if list_row["Measu"]
  42. # get Odor from another file based on the value of <animal_tag> and list_row["Label"]
  43. return list_row
  44. # ----------------------------------------------------------------------------------------------------------------------
  45. if __name__ == "__main__":
  46. # initialize a FlagsManager object with values specified above
  47. flags = FlagsManager()
  48. flags.update_flags({"STG_MotherOfAllFolders": STG_MotherOfAllFolders,
  49. "STG_OdorInfoPath": STG_OdorInfoPath,
  50. "STG_Datapath": STG_Datapath})
  51. # open a dialog for choosing existing list files
  52. existing_measurement_list_files = easygui.fileopenbox(
  53. msg="Choose one or more measurement list files to update", multiple=True,
  54. default=f"{flags.get_list_dir_str()}/*"
  55. )
  56. # make sure some files were chosen
  57. assert len(existing_measurement_list_files) > 0, IOError("No files were chosen!")
  58. for existing_measurement_list_file in existing_measurement_list_files:
  59. # create a measurement list object
  60. measurement_list = MeasurementList.create_from_lst_file(
  61. lst_fle=existing_measurement_list_file, LE_loadExp=LE_loadExp)
  62. # parse animal tag from measurement list file name
  63. animal_tag = measurement_list.get_STG_ReportTag()
  64. # inform user if no usable measurements were found
  65. if len(measurement_list.get_measus()) == 0:
  66. logging.info(f"No usable measurements found in {existing_measurement_list_file}. "
  67. f"Not updating it.")
  68. else:
  69. # apply custom modifications
  70. measurement_list.update_from_custom_func(custom_func=custom_func, animal_tag=animal_tag)
  71. # set analyze to 0 if raw data files don't exist
  72. flags.update_flags({"STG_ReportTag": animal_tag})
  73. measurement_list.sanitize_based_on_loading(flags)
  74. # create the output file name
  75. existing_measurement_list_file_path = pl.Path(existing_measurement_list_file)
  76. ml_name_stem = existing_measurement_list_file_path.name.split(".")[0]
  77. output_file_path \
  78. = existing_measurement_list_file_path.parent / f"{ml_name_stem}{measurement_output_extension}"
  79. # write measurement file to list
  80. measurement_list.write_to_lst_file_cross_format(
  81. output_lst_file=str(output_file_path), backup_current_file=True
  82. )
  83. existing_measurement_list_file_path.unlink()