Is there a recommended way to produce a LOT of gra...
# prefect-community
m
Is there a recommended way to produce a LOT of graphs in a prefect flow with matplotlib? If I use
import matplotlib; matplotlib.use('agg')
I get a segfault like this:
Copy code
./run_local.sh: line 14: 34109 Segmentation fault: 11  python cli.py local -pfile developer/test_input.json
make: *** [run_local] Error 139
(p39_ngs) MB-MFRANKLIN:amplicon-analysis meghanfranklin$ /Users/meghanfranklin/opt/anaconda3/envs/p39_ngs/lib/python3.9/multiprocessing/resource_tracker.py:216: UserWarning: resource_tracker: There appear to be 2 leaked semaphore objects to clean up at shutdown
  warnings.warn('resource_tracker: There appear to be %d '
but if I don’t use that, python crashes 🙃
Copy code
/Users/meghanfranklin/opt/anaconda3/envs/p39_ngs/lib/python3.9/site-packages/amp_analysis/plots.py:425: UserWarning: Starting a Matplotlib GUI outside of the main thread will likely fail.
  fig, ax = plt.subplots()
[2022-10-31 15:02:54-0400] INFO - prefect.TaskRunner | Task 'create_pipeline_run': Finished task run for task with final state: 'Success'
2022-10-31 15:02:54.121 python[34449:328037] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!'
*** First throw call stack:
(
	0   CoreFoundation                      0x00007ff80bf1e7c3 __exceptionPreprocess + 242
	1   libobjc.A.dylib                     0x00007ff80bc7ebc3 objc_exception_throw + 48
	2   CoreFoundation                      0x00007ff80bf47076 -[NSException raise] + 9
[a lot more lines here] 
)
libc++abi: terminating with uncaught exception of type NSException
./run_local.sh: line 14: 34449 Abort trap: 6           python cli.py local -pfile developer/test_input.json
make: *** [run_local] Error 134
(p39_ngs) MB-MFRANKLIN:amplicon-analysis meghanfranklin$ /Users/meghanfranklin/opt/anaconda3/envs/p39_ngs/lib/python3.9/multiprocessing/resource_tracker.py:216: UserWarning: resource_tracker: There appear to be 2 leaked semaphore objects to clean up at shutdown
  warnings.warn('resource_tracker: There appear to be %d '

-> cue Mac popup window about sending crash report to apple
I get the same kind of error but without the segfault if I reboot my computer, which is not a real solution. Running in a brand-new-10-mins-ago environment, so it’s also possible there’s a tiny mismatch in some underlying library. python=3.9, prefect=0.15.13
If I build a new docker container w/o setting the matplotlib backend and run the flow from there I get this error:
Copy code
[2022-10-31 19:07:35+0000] ERROR - prefect.TaskRunner | Unexpected error: Done(<matplotlib.backends.backend_agg.RendererAgg object at 0x7f337ba30c10>)
Traceback (most recent call last):
  File "/opt/conda/lib/python3.9/site-packages/prefect/engine/runner.py", line 48, in inner
    new_state = method(self, state, *args, **kwargs)
  File "/opt/conda/lib/python3.9/site-packages/prefect/engine/task_runner.py", line 876, in get_task_run_state
    value = prefect.utilities.executors.run_task_with_timeout(
  File "/opt/conda/lib/python3.9/site-packages/prefect/utilities/executors.py", line 467, in run_task_with_timeout
    return task.run(*args, **kwargs)  # type: ignore
  File "/opt/conda/lib/python3.9/site-packages/amp_analysis/analyze.py", line 48, in analyze_sam_info
    process_sam_stats(sample[0], sam_df, summary_folder, graph_folder)
  File "/opt/conda/lib/python3.9/site-packages/amp_analysis/sam.py", line 103, in process_sam_stats
    phred_histogram(mut_phreds, f"{graph_base}.Qscores_mismatches.png")
  File "/opt/conda/lib/python3.9/site-packages/amp_analysis/plots.py", line 71, in phred_histogram
    plt.savefig(outfile)
  File "/opt/conda/lib/python3.9/site-packages/matplotlib/pyplot.py", line 958, in savefig
    res = fig.savefig(*args, **kwargs)
  File "/opt/conda/lib/python3.9/site-packages/matplotlib/figure.py", line 3019, in savefig
    self.canvas.print_figure(fname, **kwargs)
  File "/opt/conda/lib/python3.9/site-packages/matplotlib/backend_bases.py", line 2319, in print_figure
    result = print_method(
  File "/opt/conda/lib/python3.9/site-packages/matplotlib/backend_bases.py", line 1648, in wrapper
    return func(*args, **kwargs)
  File "/opt/conda/lib/python3.9/site-packages/matplotlib/_api/deprecation.py", line 412, in wrapper
    return func(*inner_args, **inner_kwargs)
  File "/opt/conda/lib/python3.9/site-packages/matplotlib/backends/backend_agg.py", line 540, in print_png
    FigureCanvasAgg.draw(self)
  File "/opt/conda/lib/python3.9/site-packages/matplotlib/backends/backend_agg.py", line 436, in draw
    self.figure.draw(self.renderer)
  File "/opt/conda/lib/python3.9/site-packages/matplotlib/backend_bases.py", line 1533, in _draw
    def _draw(renderer): raise Done(renderer)
matplotlib.backend_bases._get_renderer.<locals>.Done: <matplotlib.backends.backend_agg.RendererAgg object at 0x7f337ba30c10>
[2022-10-31 19:07:35+0000] ERROR - prefect.TaskRunner | Task 'analyze_sam_info[1]': Unexpected error while running task: Done(<matplotlib.backends.backend_agg.RendererAgg object at 0x7f337ba30c10>)
At least there’s no segfault??
z
Hm this is peculiar. I’ve seen issues without Agg but with the Agg backend it should be fine: https://github.com/PrefectHQ/prefect/issues/5991#issuecomment-1213310687
Are you getting any warnings from matplotlib about threads?
m
No other warnings than the ones I posted.
I am on a Mac too, so I’m in the crash category of your post.
Ugh, if I set the matplolib backend, build a new docker, run the flow in the docker - segfault.
z
Can you open an issue with a MRE?
I’ll be working on a fix for this soon. In the meantime, using an
async
task or flow will run things in the main thread.
m
I haven’t done that before, can you point me to where to do that?
j