Skip to content

Props and State

Properties

Bases: dict

Dictionary that stores properties of a flow.

Example usage:

from motion import Component

some_component = Component("SomeComponent")

@some_component.init_state
def setUp():
    return {"model": ...}

@some_component.serve("image")
def predict_image(state, props):
    # props["image_embedding"] is passed in at runtime
    return state["model"](props["image_embedding"])

@some_component.update("image")
def monitor_prediction(state, props):
    # props.serve_result is the result of the serve operation
    if props.serve_result > some_threshold:
        trigger_alert()

if __name__ == "__main__":
    c = some_component()
    c.run("image", props={"image_embedding": ...})

serve_result: Any property

Stores the result of the serve operation. Can be accessed in the update operation, not the serve operation.

Returns:

Name Type Description
Any Any

Result of the serve operation.

State

Bases: dict

Dictionary that stores state for a component instance. The instance id is stored in the instance_id attribute.

Example usage:

from motion import Component

some_component = Component("SomeComponent")

@some_component.init_state
def setUp():
    return {"model": ...}

@some_component.serve("retrieve")
def retrieve_nn(state, props):
    # model can be accessed via state["model"]
    prediction = state["model"](props["image_embedding"])
    # match the prediction to some other data to do a retrieval
    nn_component_instance = SomeOtherMotionComponent(state.instance_id)
    return nn_component_instance.run("retrieve", props={"prediction": prediction})

if __name__ == "__main__":
    c = some_component()
    nearest_neighbors = c.run("retrieve", props={"image_embedding": ...})

instance_id: str property

Returns the instance id of the component. Useful if wanting to create other component instances within a serve or update operation.

MDataFrame

Bases: DataFrame

Wrapper around pandas DataFrame that allows for pyarrow-based serialization. This is to be used in a motion component's state.

Simply use this class instead of pandas DataFrame. For example:

from motion import MDataFrame, Component

C = Component("MyDFComponent")

@C.init_state
def setUp():
    df = MDataFrame({"value": [0, 1, 2]})
    return {"df": df}

Source code in motion/df.py
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class MDataFrame(pd.DataFrame):
    """Wrapper around pandas DataFrame that allows for pyarrow-based
    serialization. This is to be used in a motion component's state.

    Simply use this class instead of pandas DataFrame. For example:
    ```python
    from motion import MDataFrame, Component

    C = Component("MyDFComponent")

    @C.init_state
    def setUp():
        df = MDataFrame({"value": [0, 1, 2]})
        return {"df": df}
    ```
    """

    def __getstate__(self) -> dict:
        # Serialize with pyarrow
        table = pa.Table.from_pandas(self)
        # Convert the PyArrow Table to a PyArrow Buffer
        sink = pa.BufferOutputStream()
        writer = pa.ipc.new_stream(sink, table.schema)
        writer.write_table(table)
        writer.close()

        buffer = sink.getvalue()
        return {"table": buffer}

    def __setstate__(self, state: dict) -> None:
        # Convert the PyArrow Buffer to a PyArrow Table
        buf = state["table"]
        reader = pa.ipc.open_stream(buf)
        df = reader.read_pandas()
        self.__init__(df)  # type: ignore

__getstate__() -> dict

Source code in motion/df.py
22
23
24
25
26
27
28
29
30
31
32
def __getstate__(self) -> dict:
    # Serialize with pyarrow
    table = pa.Table.from_pandas(self)
    # Convert the PyArrow Table to a PyArrow Buffer
    sink = pa.BufferOutputStream()
    writer = pa.ipc.new_stream(sink, table.schema)
    writer.write_table(table)
    writer.close()

    buffer = sink.getvalue()
    return {"table": buffer}

__setstate__(state: dict) -> None

Source code in motion/df.py
34
35
36
37
38
39
def __setstate__(self, state: dict) -> None:
    # Convert the PyArrow Buffer to a PyArrow Table
    buf = state["table"]
    reader = pa.ipc.open_stream(buf)
    df = reader.read_pandas()
    self.__init__(df)  # type: ignore