utils.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Get Python six functionality:
  2. from __future__ import\
  3. absolute_import, print_function, division, unicode_literals
  4. ###############################################################################
  5. ###############################################################################
  6. ###############################################################################
  7. __all__ = [
  8. "assert_lrp_epsilon_param",
  9. "assert_infer_lrp_alpha_beta_param"
  10. ]
  11. ###############################################################################
  12. ###############################################################################
  13. ###############################################################################
  14. def assert_lrp_epsilon_param(epsilon, caller):
  15. """
  16. Function for asserting epsilon parameter choice
  17. passed to constructors inheriting from EpsilonRule
  18. and LRPEpsilon.
  19. The following conditions can not be met:
  20. epsilon > 1
  21. :param epsilon: the epsilon parameter.
  22. :param caller: the class instance calling this assertion function
  23. """
  24. if epsilon <= 0:
  25. err_head = "Constructor call to {} : ".format(caller.__class__.__name__)
  26. err_msg = err_head + "Parameter epsilon must be > 0 but was {}".format(epsilon)
  27. raise ValueError(err_msg)
  28. return epsilon
  29. def assert_infer_lrp_alpha_beta_param(alpha, beta, caller):
  30. """
  31. Function for asserting parameter choices for alpha and beta
  32. passed to constructors inheriting from AlphaBetaRule
  33. and LRPAlphaBeta.
  34. since alpha - beta are subjected to sum to 1,
  35. it is sufficient for only one of the parameters to be passed
  36. to a corresponding class constructor.
  37. this method will cause an assertion error if both are None
  38. or the following conditions can not be met
  39. alpha >= 1
  40. beta >= 0
  41. alpha - beta = 1
  42. :param alpha: the alpha parameter.
  43. :param beta: the beta parameter
  44. :param caller: the class instance calling this assertion function
  45. """
  46. err_head = "Constructor call to {} : ".format(caller.__class__.__name__)
  47. if alpha is None and beta is None:
  48. err_msg = err_head + "Neither alpha or beta were given"
  49. raise ValueError(err_msg)
  50. #assert passed parameter choices
  51. if alpha is not None and alpha < 1:
  52. err_msg = err_head +"Passed parameter alpha invalid. Expecting alpha >= 1 but was {}".format(alpha)
  53. raise ValueError(err_msg)
  54. if beta is not None and beta < 0:
  55. err_msg = err_head +"Passed parameter beta invalid. Expecting beta >= 0 but was {}".format(beta)
  56. raise ValueError(err_msg)
  57. #assert inferred parameter choices
  58. if alpha is None:
  59. alpha = beta + 1
  60. if alpha < 1:
  61. err_msg = err_head +"Inferring alpha from given beta {} s.t. alpha - beta = 1, with condition alpha >= 1 not possible.".format(beta)
  62. raise ValueError(err_msg)
  63. if beta is None:
  64. beta = alpha - 1
  65. if beta < 0:
  66. err_msg = err_head +"Inferring beta from given alpha {} s.t. alpha - beta = 1, with condition beta >= 0 not possible.".format(alpha)
  67. raise ValueError(err_msg)
  68. #final check: alpha - beta = 1
  69. amb = alpha - beta
  70. if amb != 1:
  71. err_msg = err_head +"Condition alpha - beta = 1 not fulfilled. alpha={} ; beta={} -> alpha - beta = {}".format(alpha, beta, amb)
  72. raise ValueError(err_msg)
  73. #return benign values for alpha and beta
  74. return alpha, beta
  75. ###############################################################################
  76. ###############################################################################
  77. ###############################################################################