pipeline-hetero-feature-selection-manually-left.py 4.5 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 HeteroFeatureSelection
  20. from pipeline.component import Intersection
  21. from pipeline.component import Reader
  22. from pipeline.interface import Data
  23. from pipeline.utils.tools import load_job_config
  24. def main(config="../../config.yaml", namespace=""):
  25. # obtain config
  26. if isinstance(config, str):
  27. config = load_job_config(config)
  28. parties = config.parties
  29. guest = parties.guest[0]
  30. host = parties.host[0]
  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. # define DataTransform components
  46. data_transform_0 = DataTransform(name="data_transform_0") # start component numbering at 0
  47. # get DataTransform party instance of guest
  48. data_transform_0_guest_party_instance = data_transform_0.get_party_instance(role='guest', party_id=guest)
  49. # configure DataTransform for guest
  50. data_transform_0_guest_party_instance.component_param(with_label=True, output_format="dense")
  51. # get and configure DataTransform party instance of host
  52. data_transform_0.get_party_instance(role='host', party_id=host).component_param(with_label=False)
  53. # define Intersection components
  54. intersection_0 = Intersection(name="intersection_0")
  55. pipeline.add_component(reader_0)
  56. pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
  57. pipeline.add_component(intersection_0, data=Data(data=data_transform_0.output.data))
  58. guest_selection_param = {"select_col_indexes": -1,
  59. "select_names": [],
  60. "filter_methods": ["manually"],
  61. "manually_param": {
  62. "filter_out_indexes": None,
  63. "filter_out_names": None,
  64. "left_col_indexes": [0, 1, 2],
  65. "left_col_names": ["x3"]
  66. }}
  67. hetero_feature_selection_0 = HeteroFeatureSelection(name="hetero_feature_selection_0")
  68. hetero_feature_selection_0.get_party_instance(role='guest', party_id=guest).component_param(**guest_selection_param)
  69. host_selection_param = {"select_col_indexes": -1,
  70. "select_names": [],
  71. "filter_methods": ["manually"],
  72. "manually_param": {
  73. "filter_out_indexes": None,
  74. "filter_out_names": None,
  75. "left_col_indexes": [0, 1, 2]
  76. }}
  77. hetero_feature_selection_0.get_party_instance(role='guest', party_id=guest).component_param(**host_selection_param)
  78. pipeline.add_component(hetero_feature_selection_0, data=Data(data=intersection_0.output.data))
  79. # compile pipeline once finished adding modules, this step will form conf and dsl files for running job
  80. pipeline.compile()
  81. pipeline.fit()
  82. if __name__ == "__main__":
  83. parser = argparse.ArgumentParser("PIPELINE DEMO")
  84. parser.add_argument("-config", type=str,
  85. help="config file")
  86. args = parser.parse_args()
  87. if args.config is not None:
  88. main(args.config)
  89. else:
  90. main()