robinhood / faust

Python Stream Processing
Other
6.7k stars 538 forks source link

NotImplementedError when using matplotlib/Pillow inside agent code #731

Closed cjh1 closed 2 years ago

cjh1 commented 2 years ago

Checklist

Steps to reproduce

Use matplotlib/Pillow inside faust, something like:

plt.imsave(path, img)

Expected behavior

Writes out the image.

Actual behavior

A NotImplementedError is throw from the mode package. It appears that sys.stdout is being set to an instance of FileLogProxy and Pillow is doing a comparison with sys.stdout.buffer here and FileLogProxy doesn't implement this property. Not sure where a fix should be applied.

Full traceback

Traceback (most recent call last):
  File "/home/XXX/.virtualenvs/4ddist/lib/python3.8/site-packages/faust/agents/agent.py", line 647, in _execute_actor
    await coro
  File "/home/XXX/work/source/still/backend/faust/worker.py", line 63, in watch_for_haadf_events
    image_path = await generate_image(tmp, path, scan_id)
  File "/home/XXX/work/source/still/backend/faust/worker.py", line 38, in generate_haadf_image
    plt.imsave(str(path), img)
  File "/home/XXX/.virtualenvs/4ddist/lib/python3.8/site-packages/matplotlib/pyplot.py", line 2412, in imsave
    return matplotlib.image.imsave(fname, arr, **kwargs)
  File "/home/XXX/.virtualenvs/4ddist/lib/python3.8/site-packages/matplotlib/image.py", line 1616, in imsave
    image.save(fname, **pil_kwargs)
  File "/home/XXX/.virtualenvs/4ddist/lib/python3.8/site-packages/PIL/Image.py", line 2235, in save
    save_handler(self, fp, filename)
  File "/home/XXX/.virtualenvs/4ddist/lib/python3.8/site-packages/PIL/PngImagePlugin.py", line 1349, in _save
    ImageFile._save(im, _idat(fp, chunk), [("zip", (0, 0) + im.size, 0, rawmode)])
  File "/home/XXX/.virtualenvs/4ddist/lib/python3.8/site-packages/PIL/ImageFile.py", line 497, in _save
    stdout = fp == sys.stdout or fp == sys.stdout.buffer
  File "/home/XXX/.virtualenvs/4ddist/lib/python3.8/site-packages/mode/utils/logging.py", line 849, in buffer
    raise NotImplementedError()
NotImplementedError

Versions

FR4NKESTI3N commented 2 years ago

@cjh1 Did you find a solution for this issue?

Edit: Nvm. Looks like latest pillow fixes it (>9.0.1).

cjh1 commented 2 years ago

@FR4NKESTI3N

I did the following:

  stdout = sys.stdout
  sys.stdout = sys.__stdout__
  plt.imsave(str(path), img)
  sys.stdout = stdout

Basically resetting sys.stdout, pretty hacky, good to know this is now resolved with > 9.0.1 I will have to give it a try ...