Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

pro2tapestry_conf.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import pkg_resources
  2. import pathlib as pl
  3. import re
  4. import pandas as pd
  5. import textfsm
  6. import yaml
  7. def get_template():
  8. """
  9. Read and return internal metadata definition as a DataFrame with internal metadata names as indices
  10. :return: pandas.DataFrame
  11. """
  12. pro_template_file = pkg_resources.resource_filename(
  13. 'view', "idl_folder_translation/pro_fsm_template.txt")
  14. return pro_template_file
  15. def parse_pro_file(pro_file):
  16. """
  17. Parse a .pro file to extract flag and measurement info for tapestry config files
  18. :param str pro_file: path of a .pro file
  19. :return: pandas.Dataframe
  20. """
  21. pro_template = get_template()
  22. with open(pro_file) as fle:
  23. raw_pro_file_str = fle.read()
  24. with open(pro_template) as fh:
  25. re_table = textfsm.TextFSM(fh)
  26. pro_data = re_table.ParseText(text=raw_pro_file_str)
  27. pro_data_df = pd.DataFrame(columns=re_table.header, data=pro_data)
  28. return pro_data_df
  29. def parse_animal_tag(pro_file):
  30. """
  31. Given a .pro file, parse the filename to extract the animal tag
  32. :param str pro_file: path of a .pro file
  33. :return: animal tag
  34. :rtype: str
  35. """
  36. pro_filename = pl.Path(pro_file).name
  37. match = re.fullmatch("gr[^_]*_(\w+).pro", pro_filename, re.IGNORECASE)
  38. if not match:
  39. raise ValueError(f"Could not figure out the animal id of the pro file: {pro_file}")
  40. return match.group(1)
  41. def convert_pro_to_tapestry_config(input_pro_file, output_yml_file, animal_tag, flags_to_override):
  42. """
  43. Parse info in a .pro file and convert it to a tapestry config file
  44. :param str input_pro_file: path of an input .pro file
  45. :param str output_yml_file: path of the output yml file to be created
  46. :param dict flags_to_override: these flags will override flags from .pro file if present, else be added
  47. """
  48. pro_df = parse_pro_file(input_pro_file)
  49. yml_dict = {}
  50. flags_to_change = {}
  51. for row_ind, row in pro_df.iterrows():
  52. if row["measus"]:
  53. measus = [int(x) for x in row["measus"]]
  54. yml_dict[f"row{row_ind}"] = {"measus": measus}
  55. else:
  56. flags_to_change = {k: v for k, v in zip(row["flag_names"], row["flag_values"])}
  57. first_row_key = list(yml_dict.keys())[0]
  58. flags_final = {}
  59. for k, v in flags_to_change.items():
  60. if k in ["scalemin", "scalemax"]:
  61. flags_final[f"SO_MV_{k}"] = v
  62. flags_final["SO_individualScale"] = 0
  63. else:
  64. flags_final[k] = v
  65. flags_final.update(flags_to_override)
  66. yml_dict[first_row_key]["flags"] = flags_final
  67. yml_dict[first_row_key]["animal"] = animal_tag
  68. with open(output_yml_file, 'w') as fh:
  69. yaml.dump(yml_dict, fh, Dumper=yaml.SafeDumper)