binning_model_converter.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from typing import Dict, Tuple
  16. from federatedml.protobuf.generated.feature_binning_meta_pb2 import FeatureBinningMeta
  17. from federatedml.protobuf.generated.feature_binning_param_pb2 import FeatureBinningParam, IVParam
  18. from federatedml.protobuf.model_migrate.converter.converter_base import AutoReplace
  19. from federatedml.protobuf.model_migrate.converter.converter_base import ProtoConverterBase
  20. from google.protobuf.json_format import MessageToDict
  21. class FeatureBinningConverter(ProtoConverterBase):
  22. def convert(self, param: FeatureBinningParam, meta: FeatureBinningMeta,
  23. guest_id_mapping: Dict,
  24. host_id_mapping: Dict,
  25. arbiter_id_mapping: Dict
  26. ) -> Tuple:
  27. header_anonymous = list(param.header_anonymous)
  28. replacer = AutoReplace(guest_id_mapping, host_id_mapping, arbiter_id_mapping)
  29. param.header_anonymous[:] = replacer.migrate_anonymous_header(header_anonymous)
  30. self._migrate_binning_result(param, replacer, guest_id_mapping, host_id_mapping)
  31. if param.multi_class_result.host_party_ids:
  32. migrate_host_party_ids = []
  33. for host_party_id in param.multi_class_result.host_party_ids:
  34. migrate_host_party_ids.append(str(host_id_mapping[int(host_party_id)]))
  35. param.multi_class_result.host_party_ids[:] = migrate_host_party_ids
  36. self._migrate_binning_result(param.multi_class_result, replacer, guest_id_mapping, host_id_mapping, multi=True)
  37. return param, meta
  38. def _migrate_binning_result(self, param, replacer, guest_id_mapping, host_id_mapping, multi=False):
  39. if multi:
  40. for binning_result in param.results:
  41. migrate_party_id = self.migrate_binning_result(binning_result, guest_id_mapping, host_id_mapping)
  42. if migrate_party_id is not None:
  43. binning_result.party_id = migrate_party_id
  44. else:
  45. migrate_party_id = self.migrate_binning_result(param.binning_result, guest_id_mapping, host_id_mapping)
  46. if migrate_party_id is not None:
  47. param.binning_result.party_id = migrate_party_id
  48. for host_binning_result in param.host_results:
  49. migrate_party_id = self.migrate_binning_result(host_binning_result, guest_id_mapping, host_id_mapping)
  50. if migrate_party_id is not None:
  51. host_binning_result.party_id = migrate_party_id
  52. kv_binning_result = dict(host_binning_result.binning_result)
  53. for col_name, iv_param in kv_binning_result.items():
  54. migrate_col_name = replacer.migrate_anonymous_header(col_name)
  55. host_binning_result.binning_result[migrate_col_name].CopyFrom(iv_param)
  56. del host_binning_result.binning_result[col_name]
  57. @staticmethod
  58. def migrate_binning_result(binning_result, guest_id_mapping, host_id_mapping):
  59. if binning_result.role and binning_result.party_id:
  60. party_id = int(binning_result.party_id)
  61. role = binning_result.role
  62. if role == "guest":
  63. migrate_party_id = guest_id_mapping[party_id]
  64. elif role == "host":
  65. migrate_party_id = host_id_mapping[party_id]
  66. else:
  67. raise ValueError(f"unsupported role {role} in binning migration")
  68. return str(migrate_party_id)
  69. return None