openmc icon indicating copy to clipboard operation
openmc copied to clipboard

Providing openmc.Plot.filename a string that ends in png causes undesired behavior

Open lewisgross1296 opened this issue 2 years ago • 6 comments

I recently attempted to use the plotter to save a figure via plot.to_ipython_image(), but I encountered bad behavior when specifying a string that ended in .png for my plot.filename field. The "undesirable" behavior is a bit strange, but should be fixable pretty easily.

The plot is generated with the correct file name, but a FileNotFound error is raised, stopping the script at the plot._to_ipython_image() line and preventing anything else from happening after this point. Here is an MWE

import openmc
mat = openmc.Material(name="H")
mat.add_element("H", 1.0)
mats = openmc.Materials([mat])
mats.export_to_xml()
sphere = openmc.Sphere()
cell = openmc.Cell(fill=mat,region=-sphere)
geometry = openmc.Geometry([cell])
geometry.export_to_xml()
plot = openmc.Plot()
plot.from_geometry(geometry)
plot.filename = "test.png"
plot.to_ipython_image()

and the error it causes

Traceback (most recent call last):
  File "/home/lgross/mwe.py", line 13, in <module>
    plot.to_ipython_image()
  File "/home/lgross/openmc/openmc/plots.py", line 934, in to_ipython_image
    return _get_plot_image(self, cwd)
  File "/home/lgross/openmc/openmc/plots.py", line 177, in _get_plot_image
    raise FileNotFoundError(
FileNotFoundError: Could not find .png image for plot 1. Your version of OpenMC may not be built against libpng.

I can get the script to continue executing by providing a filename that either has no extension or ends in .ppm. When the file ends in .ppm, the .png is appended anyways (e.g. test.ppm.png is created), which is another thing that probably shouldn't happen. Since the variable is called filename, imo it should accept a string that includes an extension.

@pshriwise mentioned that OpenMC has a way of detecting if a filename has an extension already and properly handling it, so maybe this is the way to go here. According to my stack trace, this is the offending method, which should take advantage of the filename extension detection in the logic.

lewisgross1296 avatar May 30 '23 18:05 lewisgross1296

This might be my bad. I had changed the handling of plot file extensions in #1926 in plots.cpp. The code looks correct though, and I can't figure out why this would happen from looking at it.

Did you build with PNG support?

gridley avatar May 30 '23 18:05 gridley

In plot.cpp the .png extension is only appended to the filename if it isn't present, but when we look for that file in _get_plot_image we assume that the file extension isn't present. So if plot.filename is set to something like my_image.png then plot.cpp will generate my_image.png, but _get_plot_image will erroneously search for my_image.png.png.

So, the check for the file extension is present on the C++ side, but we don't currently have an equivalent check on the Python side. We could update the documentation to make it clear that the Plot.filename attribute is intended to be a basename for the image file, but I think ideally we'd do some detection of the specified file extension (if present) and replace it with the correct one and display a warning, depending on whether or not libpng is enabled in the OpenMC build configuration.

pshriwise avatar May 30 '23 19:05 pshriwise

Did you build with PNG support?

I'm not sure I did, which probably makes me think I didn't. I found this part of the documentation which references libpng. Is installing libpng all that is needed, or are there build flags as well?

Maybe if you have an install with libpng, could you try my MWE to see if you get the error as well? Though it seems like Patrick has identified there is an issue on the python side.

lewisgross1296 avatar May 30 '23 21:05 lewisgross1296

Has anyone looked into this yet? Another CNERG student (@Edgar-21) is having the same error. He can use the plotter to generate an image, but can't save a png from his python script, even without the file extension missing.

lewisgross1296 avatar Sep 07 '23 16:09 lewisgross1296

can you post the output of openmc -v? Will tell if you are using PPM or PNG backends.

gridley avatar Sep 07 '23 17:09 gridley

sure thing!

$ openmc -v
OpenMC version 0.13.3
Git SHA1: 50e39a4e20dc9e0f3d7ccf07333f6a5e6c797c8c
Copyright (c) 2011-2023 MIT, UChicago Argonne LLC, and contributors
MIT/X license at <https://docs.openmc.org/en/latest/license.html>
Build type:            Release
Compiler ID:           GNU 11.3.0
MPI enabled:           no
Parallel HDF5 enabled: no
PNG support:           yes
DAGMC support:         yes
libMesh support:       no
MCPL support:          no
NCrystal support:      no
Coverage testing:      no
Profiling flags:       no

Edgar-21 avatar Sep 07 '23 17:09 Edgar-21

Hi, are there any updates on this? I've also been getting the same error.

alyaajsmn avatar Jun 11 '24 06:06 alyaajsmn

I think currently the workaround is to just not add a .png in your filename, so instead of

plot.filename = "test.png"

just do

plot.filename = "test"

lewisgross1296 avatar Jun 11 '24 17:06 lewisgross1296

Just opened a PR that fixes this. It's very simple, luckily!

gridley avatar Jun 12 '24 16:06 gridley