column_expand_param.py 3.4 KB

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