pipeline_hetero_pearson.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #
  16. import argparse
  17. from pipeline.backend.pipeline import PipeLine
  18. from pipeline.component import DataTransform, HeteroPearson, Intersection, Reader
  19. from pipeline.interface import Data
  20. from pipeline.utils.tools import load_job_config
  21. def main(config="../../config.yaml", namespace=""):
  22. common_param = dict(column_indexes=-1)
  23. pipeline = run_pearson_pipeline(
  24. config=config,
  25. namespace=namespace,
  26. data=dataset.breast,
  27. common_param=common_param,
  28. )
  29. print(pipeline.get_component("hetero_pearson_0").get_model_param())
  30. print(pipeline.get_component("hetero_pearson_0").get_summary())
  31. def run_pearson_pipeline(
  32. config,
  33. namespace,
  34. data,
  35. common_param=None,
  36. guest_only_param=None,
  37. host_only_param=None,
  38. ):
  39. if isinstance(config, str):
  40. config = load_job_config(config)
  41. guest_data = data["guest"]
  42. host_data = data["host"][0]
  43. guest_data["namespace"] = f"{guest_data['namespace']}{namespace}"
  44. host_data["namespace"] = f"{host_data['namespace']}{namespace}"
  45. pipeline = (
  46. PipeLine()
  47. .set_initiator(role="guest", party_id=config.parties.guest[0])
  48. .set_roles(guest=config.parties.guest[0], host=config.parties.host[0])
  49. )
  50. reader_0 = Reader(name="reader_0")
  51. reader_0.get_party_instance(
  52. role="guest", party_id=config.parties.guest[0]
  53. ).component_param(table=guest_data)
  54. reader_0.get_party_instance(
  55. role="host", party_id=config.parties.host[0]
  56. ).component_param(table=host_data)
  57. data_transform_0 = DataTransform(name="data_transform_0")
  58. data_transform_0.get_party_instance(
  59. role="guest", party_id=config.parties.guest[0]
  60. ).component_param(with_label=True, output_format="dense")
  61. data_transform_0.get_party_instance(
  62. role="host", party_id=config.parties.host[0]
  63. ).component_param(with_label=False)
  64. intersect_0 = Intersection(name="intersection_0")
  65. if common_param is None:
  66. common_param = {}
  67. hetero_pearson_component = HeteroPearson(name="hetero_pearson_0", **common_param)
  68. if guest_only_param:
  69. hetero_pearson_component.get_party_instance(
  70. "guest", config.parties.guest[0]
  71. ).component_param(**guest_only_param)
  72. if host_only_param:
  73. hetero_pearson_component.get_party_instance(
  74. "host", config.parties.host[0]
  75. ).component_param(**host_only_param)
  76. pipeline.add_component(reader_0)
  77. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  78. pipeline.add_component(intersect_0, data=Data(data=data_transform_0.output.data))
  79. pipeline.add_component(
  80. hetero_pearson_component, data=Data(train_data=intersect_0.output.data)
  81. )
  82. pipeline.compile()
  83. pipeline.fit()
  84. return pipeline
  85. class dataset_meta(type):
  86. @property
  87. def breast(cls):
  88. return {
  89. "guest": {"name": "breast_hetero_guest", "namespace": "experiment"},
  90. "host": [{"name": "breast_hetero_host", "namespace": "experiment"}],
  91. }
  92. class dataset(metaclass=dataset_meta):
  93. ...
  94. if __name__ == "__main__":
  95. parser = argparse.ArgumentParser("PIPELINE DEMO")
  96. parser.add_argument("-config", type=str, help="config file")
  97. args = parser.parse_args()
  98. if args.config is not None:
  99. main(args.config)
  100. else:
  101. main()