flags_test.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. from view.python_core.flags import FlagsManager
  2. from view.python_core.utils.colors import interpret_flag_SO_MV_colortable
  3. from common import get_example_data_root_path
  4. from view.idl_translation_core.IDL import createPalette
  5. import numpy as np
  6. from matplotlib.colors import Colormap
  7. import os
  8. import tempfile
  9. import pathlib as pl
  10. def test_flags_internal():
  11. """
  12. Testing the initialization of flags with default values from definition csv
  13. """
  14. flags = FlagsManager()
  15. def test_SO_MV_colortable_flags():
  16. """
  17. Testing setting SO_MV_colortable values
  18. """
  19. flags = FlagsManager()
  20. values = [11, 12, 13, 14, "jet", "autumn"]
  21. for value in values:
  22. flags.update_flags({"SO_MV_colortable": value})
  23. def test_flag_fails():
  24. """
  25. Testing cases for which flag initialization must fail
  26. """
  27. flags = FlagsManager()
  28. must_fail = [["SO_MV_SO_MV_colortable", True], # expected value is int or str, bool given
  29. ["VIEW_VIEW_batchmode", "whatever"], # expected value is bool, string given
  30. ["VIEW_VIEW_batchmode", 15], # expected value is bool, incompatible integer given
  31. ["LE_loadExp", "whatever"] # expected value is integer, incompatible string given
  32. ]
  33. for k, v in must_fail:
  34. try:
  35. flags.update_flags({k: v})
  36. except AssertionError as ase:
  37. pass
  38. def test_flags_read_write():
  39. """
  40. Testing flags reading and writing functionalities using internal flags in FakeData test dataset
  41. """
  42. flags = FlagsManager()
  43. data_root = get_example_data_root_path()
  44. moaf_path = data_root / "FakeData"
  45. flags.update_flags({"STG_MotherOfAllFolders": str(moaf_path)})
  46. STG_flags = {"STG_OdorReportPath": "IDLoutput",
  47. "STG_OdorInfoPath": "Lists",
  48. "STG_OdormaskPath": "Coor",
  49. "STG_Datapath": "data",
  50. "STG_ProcessedDataPath": "ProcessedData",
  51. "STG_OdorAreaPath": "Areas"
  52. }
  53. flags.update_flags(STG_flags)
  54. flags_temp = FlagsManager()
  55. flags_temp.clear_flags()
  56. temp_yml_path = moaf_path / f"{tempfile.gettempprefix()}.yml"
  57. temp_yml_filename = str(temp_yml_path)
  58. flags.write_flags_to_yml(temp_yml_filename)
  59. flags_temp.read_flags_from_yml(temp_yml_filename)
  60. for flag_name in flags_temp.compound_path_flags:
  61. if flag_name in flags_temp.flags:
  62. path = pl.Path(flags_temp[flag_name])
  63. if path.is_absolute():
  64. flags_temp.update_flags({flag_name: os.path.relpath(path, flags_temp["STG_MotherOfAllFolders"])})
  65. assert len(flags_temp.flags) == len(flags.flags)
  66. assert len(set(flags_temp.flags.keys()) - set(flags.flags.keys())) == 0
  67. assert all(flags.flags[x] == flags_temp.flags[x] for x in flags.flags.keys())
  68. temp_yml_path.unlink()
  69. def test_interpret_flag_SO_MV_colortable():
  70. """
  71. Testing view.python_core.flags.interpret_flag_SO_MV_colortable
  72. """
  73. valid_SO_MV_colortable_values = [11, 12, 13, 14, "winter", "cool", "jet"]
  74. invalid_SO_MV_colortable_value = [50, 150, "whatever", "notacolormap"]
  75. for value in valid_SO_MV_colortable_values:
  76. cmap, bg, fg = interpret_flag_SO_MV_colortable(value)
  77. assert issubclass(cmap.__class__, Colormap)
  78. assert len(bg) in (3, 4)
  79. assert len(fg) in (3, 4)
  80. if type(value) is int:
  81. original_cmap = createPalette(value)
  82. original_cols = original_cmap(np.linspace(0, 1, original_cmap.N))
  83. interpreted_cols = cmap(np.linspace(0, 1, original_cmap.N - 2))
  84. assert np.allclose(original_cols[1:-1, :], interpreted_cols)
  85. for value in invalid_SO_MV_colortable_value:
  86. it_failed = False
  87. try:
  88. interpret_flag_SO_MV_colortable(value)
  89. except ValueError as ve:
  90. it_failed = True
  91. except NotImplementedError as se:
  92. it_failed = True
  93. assert it_failed
  94. if __name__ == "__main__":
  95. # test_flag_fails()
  96. test_flags_read_write()