tapestry_config.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from view.python_core.io import read_check_yml_file
  2. import numpy as np
  3. import pathlib as pl
  4. class TapestryConfig(object):
  5. def __init__(
  6. self, yml_file, text_below_func, text_right_top_func=None, text_right_bottom_func=None,
  7. ):
  8. """
  9. :param yml_file: str, path of a yml file on the file system
  10. :param extra_formats: iterable of string, each representing an image extension (without the leading dot)
  11. :param text_below_func: a function that takes one argument - row of the measurement list for a measurement,
  12. and returns a string. This string will be placed below the overview frame
  13. :param text_right_top_func: a function that takes one argument - row of the measurement list for a measurement,
  14. and returns a string. This string will be placed on the top to the right of the overview. If None, the upper
  15. limit of the data shown in the overview image will be printed
  16. :param text_right_bottom_func: a function that takes one argument - row of the measurement list for
  17. a measurement, and returns a string. This string will be placed on the top to the right of the overview.
  18. If None, the lower limit of the data shown in the overview image will be printed
  19. """
  20. super().__init__()
  21. self.yml_file = yml_file
  22. self.yml_contents = read_check_yml_file(yml_file, dict)
  23. self.name = pl.Path(yml_file).stem
  24. self.text_below_func = text_below_func
  25. self.text_right_top_func = text_right_top_func
  26. self.text_right_bottom_func = text_right_bottom_func
  27. def iterrows(self):
  28. """
  29. Generator function
  30. can be used to iterate over information about rows as dictionaries
  31. """
  32. optional_entry_def = {"flags": dict,
  33. "extra_formats": list,
  34. "animal": str,
  35. "measus": list,
  36. "corresponding_movies": bool,
  37. "extra_movie_flags": dict}
  38. yml_contents_items = list(self.yml_contents.items())
  39. yml_keys = self.yml_contents.keys()
  40. assert len(yml_keys) == len(set(yml_keys)), f"In {self.yml_file}, Duplicate row names found! Please make sure" \
  41. f"each row has a unique name."
  42. first_row = yml_contents_items[0][1]
  43. assert "animal" in first_row, f"First row of all tapestry config files must contain the entry 'animal'." \
  44. f"This was not found in {self.yml_file}. Please add one!"
  45. assert "measus" in first_row, f"First row of all tapestry config files must contain the entry 'measus'." \
  46. f"This was not found in {self.yml_file}. Please add one!"
  47. assert len(first_row["measus"]) > 0, f"Entry 'measus' in the first row of {self.yml_file} is empty, which is " \
  48. f"invalid. Please add some measurement to it!"
  49. # check if all entries have the correct type
  50. for ind, (k, v) in enumerate(yml_contents_items):
  51. for entry, expected_type in optional_entry_def.items():
  52. if entry in v:
  53. tpye = type(v[entry])
  54. assert tpye is expected_type, f"Entry '{entry}' in {self.yml_file} is expected to be " \
  55. f"a {expected_type}. Got {tpye} instead"
  56. yield k, v