Create Conditional Statements

This guide will walk you through creating Kale pipelines with conditional statements.

What You’ll Need

  • An EKF or MiniKF deployment with the default Kale Docker image.
  • An understanding of how the Kale SDK works.

Procedure

  1. Create a new Notebook server using the default Kale Docker image. The image will have the following naming scheme:

    gcr.io/arrikto/jupyter-kale-py38:<IMAGE_TAG>

    Note

    The <IMAGE_TAG> varies based on the MiniKF or EKF release.

  2. Create a new python file and name it condition.py:

    $ touch condition.py
  3. Copy and paste the following code inside condition.py:

    condition.py
    1# Copyright © 2021 Arrikto Inc. All Rights Reserved.
    2
    3"""Kale SDK.
    4
    5This script defines a pipeline with a conditional statement.
    6"""
    7
    8import random
    9
    10from kale.sdk import step, pipeline
    11
    12
    13@step(name="generate")
    14def generate():
    15 gen = random.sample(range(0, 20), 2)
    16 return sum(gen)
    17
    18
    19@step(name="printer")
    20def printer(value):
    21 print(value)
    22
    23
    24@pipeline(name="conditional", experiment="kale-tutorial")
    25def conditional_pipeline():
    26 res = generate()
    27 if res > 12:
    28 printer(res)
    29
    30
    31if __name__ == "__main__":
    32 conditional_pipeline()

    The pipeline above shows how you can influence the runtime of the pipeline by verifying the return value of a step.

  4. You can also use pipeline parameters in your conditional statements. The following snippet summarizes the changes in code:

    condition_param.py
    1# Copyright © 2021 Arrikto Inc. All Rights Reserved.
    2
    3"""Kale SDK.
    4-21
    4
    5This script defines a pipeline with a conditional statement.
    6"""
    7
    8import random
    9
    10from kale.sdk import step, pipeline
    11
    12
    13@step(name="generate")
    14def generate():
    15 gen = random.sample(range(0, 20), 2)
    16 return sum(gen)
    17
    18
    19@step(name="printer")
    20def printer(value):
    21 print(value)
    22
    23
    24@pipeline(name="conditional", experiment="kale-tutorial")
    25-def conditional_pipeline():
    26+def conditional_pipeline(threshold=12):
    27 res = generate()
    28- if res > 12:
    29+ if res > threshold:
    30 printer(res)
    31
    32
    33if __name__ == "__main__":
    34 conditional_pipeline()

    Using pipeline parameters in your conditional statements, instead of constant values, is particularly useful for quick iteration and experimentation. You can quickly clone your pipeline run (look for the :guilabel:’Clone run’ button on the top-right of the KFP run page) and start a new one with a different pipeline parameter value.

  5. Kale also supports nested conditionals. The following snippet summarizes the changes in code:

    condition_param_nested.py
    1# Copyright © 2021 Arrikto Inc. All Rights Reserved.
    2
    3"""Kale SDK.
    4-21
    4
    5This script defines a pipeline with a conditional statement.
    6"""
    7
    8import random
    9
    10from kale.sdk import step, pipeline
    11
    12
    13@step(name="generate")
    14def generate():
    15 gen = random.sample(range(0, 20), 2)
    16 return sum(gen)
    17
    18
    19@step(name="printer")
    20def printer(value):
    21 print(value)
    22
    23
    24@pipeline(name="conditional", experiment="kale-tutorial")
    25-def conditional_pipeline(threshold=12):
    26+def conditional_pipeline(upper=13, lower=10):
    27 res = generate()
    28- if res > threshold:
    29- printer(res)
    30+ if res < upper:
    31+ if res > lower:
    32+ printer(res)
    33
    34
    35if __name__ == "__main__":
    36 conditional_pipeline()

Note

A “conditional” is an if statement that is not followed by else or elif statements. Also, a conditional should be composed of two operators compared with ==, !=, >, >=, <, or <=. An operator is either

  • a pipeline parameter,
  • a step result, or
  • a constant (both numbers and strings are supported)

Summary

You have successfully created and run Kale pipelines with conditional statements.

What’s Next

Check out how you can create parallel steps using loop statements.