pipeline-intersect-with-union.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 Intersection
  19. from pipeline.component import Union
  20. from pipeline.component import Reader
  21. from pipeline.interface import Data
  22. from pipeline.utils.tools import load_job_config
  23. def main(config="../../config.yaml", namespace=""):
  24. # obtain config
  25. if isinstance(config, str):
  26. config = load_job_config(config)
  27. parties = config.parties
  28. guest = parties.guest[0]
  29. host = parties.host[0]
  30. # specify input data name & namespace in database
  31. guest_train_data = {"name": "breast_hetero_guest", "namespace": f"experiment{namespace}"}
  32. host_train_data = {"name": "breast_hetero_host", "namespace": f"experiment{namespace}"}
  33. # initialize pipeline
  34. pipeline = PipeLine()
  35. # set job initiator
  36. pipeline.set_initiator(role='guest', party_id=guest)
  37. # set participants information
  38. pipeline.set_roles(guest=guest, host=host)
  39. # define Reader components to read in data
  40. reader_0 = Reader(name="reader_0")
  41. # configure Reader for guest
  42. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  43. # configure Reader for host
  44. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  45. param = {
  46. "intersect_method": "raw",
  47. "sync_intersect_ids": True,
  48. "only_output_key": True,
  49. "raw_params": {
  50. "use_hash": True,
  51. "hash_method": "sha256",
  52. "salt": "12345",
  53. "base64": True,
  54. "join_role": "host"
  55. }
  56. }
  57. # define Intersection components
  58. intersections = []
  59. for i in range(200):
  60. intersection_tmp = Intersection(name="intersection_" + str(i), **param)
  61. intersections.append(intersection_tmp)
  62. union_0 = Union(name="union_0")
  63. # add components to pipeline, in order of task execution
  64. pipeline.add_component(reader_0)
  65. # set data input sources of intersection components
  66. for i in range(len(intersections)):
  67. pipeline.add_component(intersections[i], data=Data(data=reader_0.output.data))
  68. # set data output of intersection components
  69. intersection_outputs = [intersection_tmp.output.data for intersection_tmp in intersections]
  70. pipeline.add_component(union_0, data=Data(data=intersection_outputs))
  71. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  72. pipeline.compile()
  73. # fit model
  74. pipeline.fit()
  75. if __name__ == "__main__":
  76. parser = argparse.ArgumentParser("PIPELINE DEMO")
  77. parser.add_argument("-config", type=str,
  78. help="config file")
  79. args = parser.parse_args()
  80. if args.config is not None:
  81. main(args.config)
  82. else:
  83. main()