column_expand_param.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. from pipeline.param.base_param import BaseParam
  19. from pipeline.param import consts
  20. class ColumnExpandParam(BaseParam):
  21. """
  22. Define method used for expanding column
  23. Parameters
  24. ----------
  25. append_header : None or str or List[str], default: None
  26. Name(s) for appended feature(s). If None is given, module outputs the original input value without any operation.
  27. method : str, default: 'manual'
  28. If method is 'manual', use user-specified `fill_value` to fill in new features.
  29. fill_value : int or float or str or List[int] or List[float] or List[str], default: 1e-8
  30. Used for filling expanded feature columns. If given a list, length of the list must match that of `append_header`
  31. need_run: bool, default: True
  32. Indicate if this module needed to be run.
  33. """
  34. def __init__(self, append_header=None, method="manual",
  35. fill_value=consts.FLOAT_ZERO, need_run=True):
  36. super(ColumnExpandParam, self).__init__()
  37. self.append_header = [] if append_header is None else append_header
  38. self.method = method
  39. self.fill_value = fill_value
  40. self.need_run = need_run
  41. def check(self):
  42. descr = "column_expand param's "
  43. if not isinstance(self.method, str):
  44. raise ValueError(f"{descr}method {self.method} not supported, should be str type")
  45. else:
  46. user_input = self.method.lower()
  47. if user_input == "manual":
  48. self.method = consts.MANUAL
  49. else:
  50. raise ValueError(f"{descr} method {user_input} not supported")
  51. BaseParam.check_boolean(self.need_run, descr=descr)
  52. if not isinstance(self.append_header, list):
  53. raise ValueError(f"{descr} append_header must be None or list of str. "
  54. f"Received {type(self.append_header)} instead.")
  55. for feature_name in self.append_header:
  56. BaseParam.check_string(feature_name, descr + "append_header values")
  57. if isinstance(self.fill_value, list):
  58. if len(self.append_header) != len(self.fill_value):
  59. raise ValueError(
  60. f"{descr} `fill value` is set to be list, "
  61. f"and param `append_header` must also be list of the same length.")
  62. else:
  63. self.fill_value = [self.fill_value]
  64. for value in self.fill_value:
  65. if type(value).__name__ not in ["float", "int", "long", "str"]:
  66. raise ValueError(
  67. f"{descr} fill value(s) must be float, int, or str. Received type {type(value)} instead.")
  68. return True