statistics_param.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright 2019 The FATE Authors. All Rights Reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. import re
  19. from pipeline.param.base_param import BaseParam
  20. from pipeline.param import consts
  21. class StatisticsParam(BaseParam):
  22. """
  23. Define statistics params
  24. Parameters
  25. ----------
  26. statistics: list, string, default "summary"
  27. Specify the statistic types to be computed.
  28. "summary" represents list: [consts.SUM, consts.MEAN, consts.STANDARD_DEVIATION,
  29. consts.MEDIAN, consts.MIN, consts.MAX,
  30. consts.MISSING_COUNT, consts.SKEWNESS, consts.KURTOSIS]
  31. "describe" represents list: [consts.COUNT, consts.MEAN,
  32. consts.STANDARD_DEVIATION, consts.MIN, consts.MAX]
  33. column_names: list of string, default []
  34. Specify columns to be used for statistic computation by column names in header
  35. column_indexes: list of int, default -1
  36. Specify columns to be used for statistic computation by column order in header
  37. -1 indicates to compute statistics over all columns
  38. bias: bool, default: True
  39. If False, the calculations of skewness and kurtosis are corrected for statistical bias.
  40. need_run: bool, default True
  41. Indicate whether to run this modules
  42. """
  43. LEGAL_STAT = [consts.COUNT, consts.SUM, consts.MEAN, consts.STANDARD_DEVIATION,
  44. consts.MEDIAN, consts.MIN, consts.MAX, consts.VARIANCE,
  45. consts.COEFFICIENT_OF_VARIATION, consts.MISSING_COUNT,
  46. consts.SKEWNESS, consts.KURTOSIS]
  47. LEGAL_QUANTILE = re.compile("^(100)|([1-9]?[0-9])%$")
  48. def __init__(self, statistics="summary", column_names=None,
  49. column_indexes=-1, need_run=True, abnormal_list=None,
  50. quantile_error=consts.DEFAULT_RELATIVE_ERROR, bias=True):
  51. super().__init__()
  52. self.statistics = statistics
  53. self.column_names = column_names
  54. self.column_indexes = column_indexes
  55. self.abnormal_list = abnormal_list
  56. self.need_run = need_run
  57. self.quantile_error = quantile_error
  58. self.bias = bias
  59. if column_names is None:
  60. self.column_names = []
  61. if column_indexes is None:
  62. self.column_indexes = []
  63. if abnormal_list is None:
  64. self.abnormal_list = []
  65. @staticmethod
  66. def extend_statistics(statistic_name):
  67. if statistic_name == "summary":
  68. return [consts.SUM, consts.MEAN, consts.STANDARD_DEVIATION,
  69. consts.MEDIAN, consts.MIN, consts.MAX,
  70. consts.MISSING_COUNT, consts.SKEWNESS, consts.KURTOSIS,
  71. consts.COEFFICIENT_OF_VARIATION]
  72. if statistic_name == "describe":
  73. return [consts.COUNT, consts.MEAN, consts.STANDARD_DEVIATION, consts.MIN, consts.MAX]
  74. @staticmethod
  75. def find_stat_name_match(stat_name):
  76. if stat_name in StatisticsParam.LEGAL_STAT or StatisticsParam.LEGAL_QUANTILE.match(stat_name):
  77. return True
  78. return False
  79. # match_result = [legal_name == stat_name for legal_name in StatisticsParam.LEGAL_STAT]
  80. # match_result.append(0 if LEGAL_QUANTILE.match(stat_name) is None else True)
  81. # match_found = sum(match_result) > 0
  82. # return match_found
  83. def check(self):
  84. model_param_descr = "Statistics's param statistics"
  85. BaseParam.check_boolean(self.need_run, model_param_descr)
  86. if not isinstance(self.statistics, list):
  87. if self.statistics in [consts.DESCRIBE, consts.SUMMARY]:
  88. self.statistics = StatisticsParam.extend_statistics(self.statistics)
  89. else:
  90. self.statistics = [self.statistics]
  91. for stat_name in self.statistics:
  92. match_found = StatisticsParam.find_stat_name_match(stat_name)
  93. if not match_found:
  94. raise ValueError(f"Illegal statistics name provided: {stat_name}.")
  95. model_param_descr = "Statistics's param column_names"
  96. if not isinstance(self.column_names, list):
  97. raise ValueError(f"column_names should be list of string.")
  98. for col_name in self.column_names:
  99. BaseParam.check_string(col_name, model_param_descr)
  100. model_param_descr = "Statistics's param column_indexes"
  101. if not isinstance(self.column_indexes, list) and self.column_indexes != -1:
  102. raise ValueError(f"column_indexes should be list of int or -1.")
  103. if self.column_indexes != -1:
  104. for col_index in self.column_indexes:
  105. if not isinstance(col_index, int):
  106. raise ValueError(f"{model_param_descr} should be int or list of int")
  107. if col_index < -consts.FLOAT_ZERO:
  108. raise ValueError(f"{model_param_descr} should be non-negative int value(s)")
  109. if not isinstance(self.abnormal_list, list):
  110. raise ValueError(f"abnormal_list should be list of int or string.")
  111. self.check_decimal_float(self.quantile_error, "Statistics's param quantile_error ")
  112. self.check_boolean(self.bias, "Statistics's param bias ")
  113. return True