pipeline-homo-onehot-string-partial_col-test.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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
  19. from pipeline.component import HomoOneHotEncoder
  20. from pipeline.component import Reader
  21. from pipeline.interface import Data
  22. from pipeline.interface import Model
  23. from pipeline.utils.tools import load_job_config
  24. import json
  25. def main(config="../../config.yaml", namespace=""):
  26. # obtain config
  27. if isinstance(config, str):
  28. config = load_job_config(config)
  29. parties = config.parties
  30. guest = parties.guest[0]
  31. host = parties.host[0]
  32. arbiter = parties.arbiter[0]
  33. guest_train_data = {"name": "mock_string", "namespace": f"experiment{namespace}"}
  34. host_train_data = {"name": "mock_string", "namespace": f"experiment{namespace}"}
  35. guest_eval_data = {"name": "mock_string", "namespace": f"experiment{namespace}"}
  36. host_eval_data = {"name": "mock_string", "namespace": f"experiment{namespace}"}
  37. # initialize pipeline
  38. pipeline = PipeLine()
  39. # set job initiator
  40. pipeline.set_initiator(role='guest', party_id=guest)
  41. # set participants information
  42. pipeline.set_roles(guest=guest, host=host, arbiter=arbiter)
  43. # define Reader components to read in data
  44. reader_0 = Reader(name="reader_0")
  45. # configure Reader for guest
  46. reader_0.get_party_instance(role='guest', party_id=guest).component_param(table=guest_train_data)
  47. # configure Reader for host
  48. reader_0.get_party_instance(role='host', party_id=host).component_param(table=host_train_data)
  49. reader_1 = Reader(name="reader_1")
  50. reader_1.get_party_instance(role='guest', party_id=guest).component_param(table=guest_eval_data)
  51. reader_1.get_party_instance(role='host', party_id=host).component_param(table=host_eval_data)
  52. # define DataTransform components
  53. data_transform_0 = DataTransform(name="data_transform_0", with_label=True, output_format="dense", label_name='y',
  54. data_type="str") # start component numbering at 0
  55. data_transform_1 = DataTransform(name="data_transform_1")
  56. homo_onehot_param = {
  57. "transform_col_indexes": [1, 2, 5, 6, 8, 10, 11, 12],
  58. "transform_col_names": [],
  59. "need_alignment": True
  60. }
  61. homo_onehot_0 = HomoOneHotEncoder(name='homo_onehot_0', **homo_onehot_param)
  62. homo_onehot_1 = HomoOneHotEncoder(name='homo_onehot_1')
  63. # add components to pipeline, in order of task execution
  64. pipeline.add_component(reader_0)
  65. pipeline.add_component(reader_1)
  66. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  67. # set data_transform_1 to replicate model from data_transform_0
  68. pipeline.add_component(
  69. data_transform_1, data=Data(
  70. data=reader_1.output.data), model=Model(
  71. data_transform_0.output.model))
  72. pipeline.add_component(homo_onehot_0, data=Data(data=data_transform_0.output.data))
  73. pipeline.add_component(homo_onehot_1, data=Data(data=data_transform_1.output.data),
  74. model=Model(homo_onehot_0.output.model))
  75. pipeline.compile()
  76. # fit model
  77. pipeline.fit()
  78. # query component summary
  79. if __name__ == "__main__":
  80. parser = argparse.ArgumentParser("PIPELINE DEMO")
  81. parser.add_argument("-config", type=str,
  82. help="config file")
  83. args = parser.parse_args()
  84. if args.config is not None:
  85. main(args.config)
  86. else:
  87. main()