pipeline-homo-virtual-summary-binning-predict.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. import json
  18. from pipeline.backend.pipeline import PipeLine
  19. from pipeline.component import DataTransform
  20. from pipeline.component import HomoFeatureBinning
  21. from pipeline.component import Reader
  22. from pipeline.component import FeatureScale
  23. from pipeline.interface import Data
  24. from pipeline.interface import Model
  25. from pipeline.utils.tools import load_job_config
  26. def main(config="../../config.yaml", namespace=""):
  27. # obtain config
  28. if isinstance(config, str):
  29. config = load_job_config(config)
  30. parties = config.parties
  31. guest = parties.guest[0]
  32. host = parties.host[0]
  33. arbiter = parties.arbiter[0]
  34. guest_train_data = {"name": "breast_homo_guest", "namespace": f"experiment{namespace}"}
  35. host_train_data = {"name": "breast_homo_host", "namespace": f"experiment{namespace}"}
  36. # initialize pipeline
  37. pipeline = PipeLine()
  38. # set job initiator
  39. pipeline.set_initiator(role='guest', party_id=guest)
  40. # set participants information
  41. pipeline.set_roles(guest=guest, host=host, arbiter=arbiter)
  42. # define Reader components to read in data
  43. reader_0 = Reader(name="reader_0")
  44. # configure Reader for guest
  45. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  46. # configure Reader for host
  47. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  48. # define DataTransform components
  49. data_transform_0 = DataTransform(
  50. name="data_transform_0",
  51. with_label=True,
  52. output_format="dense") # start component numbering at 0
  53. homo_binning_0 = HomoFeatureBinning(name='homo_binning_0', sample_bins=1000)
  54. homo_binning_1 = HomoFeatureBinning(name='homo_binning_1', sample_bins=1000)
  55. # add components to pipeline, in order of task execution
  56. pipeline.add_component(reader_0)
  57. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  58. # set data input sources of intersection components
  59. pipeline.add_component(homo_binning_0, data=Data(data=data_transform_0.output.data))
  60. pipeline.add_component(homo_binning_1, data=Data(data=data_transform_0.output.data),
  61. model=Model(model=homo_binning_0.output.model))
  62. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  63. pipeline.compile()
  64. # fit model
  65. pipeline.fit()
  66. # query component summary
  67. # print(json.dumps(pipeline.get_component("homo_binning_0").get_summary(), indent=4, ensure_ascii=False))
  68. if __name__ == "__main__":
  69. parser = argparse.ArgumentParser("PIPELINE DEMO")
  70. parser.add_argument("-config", type=str,
  71. help="config file")
  72. args = parser.parse_args()
  73. if args.config is not None:
  74. main(args.config)
  75. else:
  76. main()