metaflow icon indicating copy to clipboard operation
metaflow copied to clipboard

How to/can we load pickle files using IncludeFile parameter?

Open pomkos opened this issue 5 years ago • 1 comments

Problem

Using IncludeFile to load pickle files results in the error at the first instance of the file being called

Code:

'''
Uses metaflow to run datasets through the appropriate machine learning
algorithm.

HOW TO: CONDA_CHANNELS=conda-forge python ml_flow.py --environment=conda run --data_=datasets/1073841789.pkl --vid_id=1073841789
'''

from metaflow import FlowSpec, Parameter, IncludeFile, step, conda_base
from io import StringIO


@conda_base(
    libraries={"pandas": "1.2.3"},
    python="3.8.8",
)
class mlFlow(FlowSpec):
    data_ = IncludeFile(
        "data_", 
        help="Labeled and processed twitch stream chat log, pickle file"
    )

    @step
    def start(self):
        '''
        Loads user provided parameters
        '''
        import pandas as pd
        print('____________________________________')
        self.data = pd.read_pickle(StringIO(self.data_,'rb'))
        self.next(self.me)
    @step
    def me(self):
        print('step 1')
        self.next(self.end)
        
    @step
    def end(self):
        print(self.data.head())
        print('reached the end')

if __name__ == "__main__":
    mlFlow()

Call

(pillar_env) jupyter-pomkos@jupyterubuntu:~/projects/pillaralgos/prod/pillaralgos$ CONDA_CHANNELS=conda-forge python ml_flow.py --environment=conda run --data_=datasets/1073841789.pkl

Traceback

(pillar_env) jupyter-pomkos@jupyterubuntu:~/projects/pillaralgos/prod/pillaralgos$ CONDA_CHANNELS=conda-forge python ml_flow.py --environment=conda run --data_=datasets/1073841789.pkl
Metaflow 2.3.2 executing mlFlow for user:jupyter-pomkos
Validating your flow...
    The graph looks good!
Running pylint...
    Pylint is happy!
Bootstrapping conda environment...(this could take a few minutes)
Including file datasets/1073841789.pkl of size 37KB 
File persisted at local:///home/jupyter-pomkos/projects/pillaralgos/prod/pillaralgos/.metaflow/data/mlFlow/fc7cc521681a68b4ee394afadab21db271da8e9a
2021-08-27 23:19:46.073 Workflow starting (run-id 1630106386067302):
2021-08-27 23:19:47.066 [1630106386067302/start/1 (pid 235215)] Task is starting.
2021-08-27 23:19:47.646 [1630106386067302/start/1 (pid 235215)] ____________________________________
2021-08-27 23:19:47.648 [1630106386067302/start/1 (pid 235215)] <flow mlFlow step start> failed:
2021-08-27 23:19:47.652 [1630106386067302/start/1 (pid 235215)] Internal error
2021-08-27 23:19:47.653 [1630106386067302/start/1 (pid 235215)] Traceback (most recent call last):
2021-08-27 23:19:47.653 [1630106386067302/start/1 (pid 235215)] File "/tmp/tmpqjje4vcu/metaflow/cli.py", line 1005, in main
2021-08-27 23:19:47.653 [1630106386067302/start/1 (pid 235215)] start(auto_envvar_prefix='METAFLOW', obj=state)
2021-08-27 23:19:47.653 [1630106386067302/start/1 (pid 235215)] File "/home/jupyter-pomkos/.conda/envs/metaflow_mlFlow_linux-64_681bd198cd7f3ef91e5f708f7f41f3f631650377/lib/python3.8/site-packages/click/core.py", line 829, in __call__
2021-08-27 23:19:47.653 [1630106386067302/start/1 (pid 235215)] return self.main(args, kwargs)
2021-08-27 23:19:47.654 [1630106386067302/start/1 (pid 235215)] File "/home/jupyter-pomkos/.conda/envs/metaflow_mlFlow_linux-64_681bd198cd7f3ef91e5f708f7f41f3f631650377/lib/python3.8/site-packages/click/core.py", line 782, in main
2021-08-27 23:19:47.654 [1630106386067302/start/1 (pid 235215)] rv = self.invoke(ctx)
2021-08-27 23:19:47.654 [1630106386067302/start/1 (pid 235215)] File "/home/jupyter-pomkos/.conda/envs/metaflow_mlFlow_linux-64_681bd198cd7f3ef91e5f708f7f41f3f631650377/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
2021-08-27 23:19:47.654 [1630106386067302/start/1 (pid 235215)] return _process_result(sub_ctx.command.invoke(sub_ctx))
2021-08-27 23:19:47.654 [1630106386067302/start/1 (pid 235215)] File "/home/jupyter-pomkos/.conda/envs/metaflow_mlFlow_linux-64_681bd198cd7f3ef91e5f708f7f41f3f631650377/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
2021-08-27 23:19:47.654 [1630106386067302/start/1 (pid 235215)] return ctx.invoke(self.callback, ctx.params)
2021-08-27 23:19:47.780 [1630106386067302/start/1 (pid 235215)] File "/home/jupyter-pomkos/.conda/envs/metaflow_mlFlow_linux-64_681bd198cd7f3ef91e5f708f7f41f3f631650377/lib/python3.8/site-packages/click/core.py", line 610, in invoke
2021-08-27 23:19:47.781 [1630106386067302/start/1 (pid 235215)] return callback(args, kwargs)
2021-08-27 23:19:47.781 [1630106386067302/start/1 (pid 235215)] File "/home/jupyter-pomkos/.conda/envs/metaflow_mlFlow_linux-64_681bd198cd7f3ef91e5f708f7f41f3f631650377/lib/python3.8/site-packages/click/decorators.py", line 21, in new_func
2021-08-27 23:19:47.781 [1630106386067302/start/1 (pid 235215)] return f(get_current_context(), args, kwargs)
2021-08-27 23:19:47.781 [1630106386067302/start/1 (pid 235215)] File "/tmp/tmpqjje4vcu/metaflow/cli.py", line 514, in step
2021-08-27 23:19:47.781 [1630106386067302/start/1 (pid 235215)] task.run_step(step_name,
2021-08-27 23:19:47.781 [1630106386067302/start/1 (pid 235215)] File "/tmp/tmpqjje4vcu/metaflow/task.py", line 443, in run_step
2021-08-27 23:19:47.781 [1630106386067302/start/1 (pid 235215)] self._exec_step_function(step_func)
2021-08-27 23:19:47.781 [1630106386067302/start/1 (pid 235215)] File "/tmp/tmpqjje4vcu/metaflow/task.py", line 51, in _exec_step_function
2021-08-27 23:19:47.782 [1630106386067302/start/1 (pid 235215)] step_function()
2021-08-27 23:19:47.782 [1630106386067302/start/1 (pid 235215)] File "ml_flow.py", line 39, in start
2021-08-27 23:19:47.782 [1630106386067302/start/1 (pid 235215)] self.data = pd.read_pickle(StringIO(self.data_,'rb'))
2021-08-27 23:19:47.782 [1630106386067302/start/1 (pid 235215)] File "/tmp/tmpqjje4vcu/metaflow/task.py", line 63, in property_setter
2021-08-27 23:19:47.782 [1630106386067302/start/1 (pid 235215)] v = param.load_parameter(parameter_ds[var])
2021-08-27 23:19:47.782 [1630106386067302/start/1 (pid 235215)] File "/tmp/tmpqjje4vcu/metaflow/includefile.py", line 306, in load_parameter
2021-08-27 23:19:47.782 [1630106386067302/start/1 (pid 235215)] return file_type.load(val)
2021-08-27 23:19:47.782 [1630106386067302/start/1 (pid 235215)] File "/tmp/tmpqjje4vcu/metaflow/includefile.py", line 375, in load
2021-08-27 23:19:47.782 [1630106386067302/start/1 (pid 235215)] return io.TextIOWrapper(f, encoding=value_info.get('encoding')).read()
2021-08-27 23:19:47.782 [1630106386067302/start/1 (pid 235215)] File "/home/jupyter-pomkos/.conda/envs/metaflow_mlFlow_linux-64_681bd198cd7f3ef91e5f708f7f41f3f631650377/lib/python3.8/codecs.py", line 322, in decode
2021-08-27 23:19:47.782 [1630106386067302/start/1 (pid 235215)] (result, consumed) = self._buffer_decode(data, self.errors, final)
2021-08-27 23:19:47.782 [1630106386067302/start/1 (pid 235215)] UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
2021-08-27 23:19:47.783 [1630106386067302/start/1 (pid 235215)] 
2021-08-27 23:19:47.783 [1630106386067302/start/1 (pid 235215)] Task failed.
2021-08-27 23:19:47.783 Workflow failed.
2021-08-27 23:19:47.783 Terminating 0 active tasks...
2021-08-27 23:19:47.783 Flushing logs...
    Step failure:
    Step start (task-id 1) failed.

Examples

  1. print(self.data_)
  2. print(type(self.data_)
  3. print(open(self.data_)
  4. print(open(self.data_, 'rb')
  5. print(StringIO(self.data_)
  6. print(pandas.read_pickle(self.data_)

pomkos avatar Aug 27 '21 23:08 pomkos

The fix was to include the is_text parameter, then read the file in with BytesIO like so:

'''
Uses metaflow to run datasets through the appropriate machine learning
algorithm.

HOW TO: CONDA_CHANNELS=conda-forge python ml_flow.py --environment=conda run --data_=datasets/1073841789.pkl --vid_id=1073841789
'''

from metaflow import FlowSpec, Parameter, IncludeFile, step, conda_base
from io import BytesIO


@conda_base(
    libraries={"pandas": "1.2.3"},
    python="3.8.8",
)
class mlFlow(FlowSpec):
    data_ = IncludeFile(
        "data_", 
        help="Labeled and processed twitch stream chat log, pickle file",
        is_text=False
    )

    @step
    def start(self):
        '''
        Loads user provided parameters
        '''
        import pandas as pd
        print('____________________________________')
        self.data = pd.read_pickle(BytesIO(self.data_))
        self.next(self.me)
    @step
    def me(self):
        print('step 1')
        self.next(self.end)
        
    @step
    def end(self):
        print(self.data.head())
        print('reached the end')

if __name__ == "__main__":
    mlFlow()

pomkos avatar Aug 28 '21 00:08 pomkos