!pipeline --help

"""
Explanation: Pipeline Tutorial
install
Pipeline is distributed along with fate_client.
bash
pip install fate_client
To use Pipeline, we need to first specify which FATE Flow Service to connect to. Once fate_client installed, one can find an cmd enterpoint name pipeline:
End of explanation
"""


!pipeline init --ip 127.0.0.1 --port 9380

"""
Explanation: Assume we have a FATE Flow Service in 127.0.0.1:9380(defaults in standalone), then exec
End of explanation
"""


from pipeline.backend.pipeline import PipeLine
from pipeline.component import DataTransform
from pipeline.component import Reader
from pipeline.component import HomoNN
from pipeline.interface import Data

"""
Explanation: homo nn
The pipeline package provides components to compose a FATE pipeline.
End of explanation
"""


pipeline = PipeLine() \
        .set_initiator(role='guest', party_id=9999) \
        .set_roles(guest=9999, host=[10000], arbiter=10000)

"""
Explanation: Make a pipeline instance:
- initiator: 
    * role: guest
    * party: 9999
- roles:
    * guest: 9999
    * host: [10000, 9999]
    * arbiter: 9999
End of explanation
"""


reader_0 = Reader(name="reader_0")
# set guest parameter
reader_0.get_party_instance(role='guest', party_id=9999).component_param(
    table={"name": "breast_homo_guest", "namespace": "experiment"})
# set host parameter
reader_0.get_party_instance(role='host', party_id=10000).component_param(
    table={"name": "breast_homo_host", "namespace": "experiment"})

"""
Explanation: Define a Reader to load data
End of explanation
"""


data_transform_0 = DataTransform(name="data_transform_0", with_label=True)
# set guest parameter
data_transform_0.get_party_instance(role='guest', party_id=9999).component_param(
    with_label=True)
data_transform_0.get_party_instance(role='host', party_id=[10000]).component_param(
    with_label=True)

"""
Explanation: Add a DataTransform component to parse raw data into Data Instance
End of explanation
"""


homo_nn_0 = HomoNN(
    name="homo_nn_0", 
    max_iter=10, 
    batch_size=-1, 
    early_stop={"early_stop": "diff", "eps": 0.0001})

"""
Explanation: Now, we define the HomoNN component.
End of explanation
"""


from tensorflow.keras.layers import Dense
homo_nn_0.add(
    Dense(units=1, input_shape=(10,), activation="sigmoid"))

"""
Explanation: Add single Dense layer:
End of explanation
"""


from tensorflow.keras import optimizers
homo_nn_0.compile(
    optimizer=optimizers.Adam(learning_rate=0.05), 
    metrics=["accuracy", "AUC"],
    loss="binary_crossentropy")

"""
Explanation: Compile:
End of explanation
"""


pipeline.add_component(reader_0)
pipeline.add_component(data_transform_0, data=Data(data=reader_0.output.data))
pipeline.add_component(homo_nn_0, data=Data(train_data=data_transform_0.output.data))
pipeline.compile();

"""
Explanation: Add components to pipeline:
- data_transform_0 comsume reader_0's output data
- homo_nn_0 comsume data_transform_0's output data
End of explanation
"""


pipeline.fit()

"""
Explanation: Now, submit(fit) our pipeline:
End of explanation
"""


summary = pipeline.get_component("homo_nn_0").get_summary()
summary

"""
Explanation: Success! Now we can get model summary from homo_nn_0:
End of explanation
"""


%pylab inline
pylab.plot(summary['loss_history'])

"""
Explanation: And we can use the summary data to draw the loss curve:
End of explanation
"""


