Closed flokno closed 1 month ago
edit: it seems to work in the colab notebook. will check my local setup
It seems that my images have slightly different sizes when saved locally, which then produces the error.
I changed comfit.tools.tool_animation.tool_make_animation
to
def tool_make_animation_gif(counter, name=None, fps=24):
"""
Creates an animation from a series of plot images and saves it as a GIF file.
Input:
- counter (int): The number of plot images to include in the animation.
- name (str, optional): The filename for the output video. Defaults to today's date followed by ' - output_video.mp4'.
- fps (int, optional): The frames per second for the video. Defaults to 24.
"""
if name is None:
name = datetime.now().strftime("%y%m%d_%H%M") + ' - output_animation.gif'
else:
name = datetime.now().strftime("%y%m%d_%H%M") + ' - ' + name + '.gif'
image_files = [f'plot_{counter}.png' for counter in range(counter)]
images = []
for image_file in image_files:
print(image_file)
image = imageio.imread(image_file)
print(image.shape)
images.append(image)
imageio.mimsave(name, images, fps=fps, loop=0)
# Delete the png files
for file in image_files:
os.remove(file)
then I get the output
plot_0.png
(435, 525, 4)
plot_1.png
(435, 525, 4)
plot_2.png
(435, 533, 4)
plot_3.png
(435, 533, 4)
plot_4.png
(435, 542, 4)
plot_5.png
(435, 542, 4)
...
i.e., the size of the images has changed
Thank you for raising the issue. It seems that the cf.tool_save_plot function might save figures in different formats depending on a users specific setup. I have changed the tool_save_plot-function to take as optional inputs image_size_inches=(6,5)' and
dpi=100' (dots per inch). If not provided explicitly, this should set the resolution of the plots to 600x500 which will hopefully fix the issue you are having. I tried it on my computer setup and it produced a .gif with the correct dimensions.
Can you pull the main branch and run your script again and see if it fixes your issue?
It does not work:
a) the wrong variable name is used in https://github.com/vidarsko/ComFiT/blob/main/comfit/tools/tool_animation.py#L33 (image_size
vs. image_size_inches
).
b) If I fix that, I still get the same error as before. I suspect that the images get cropped when the colorbar labels get too many digits. I attach the first 5 pngs I get below. You can see that they have different widths when the number of digits increases
It does not work:
a) the wrong variable name is used in https://github.com/vidarsko/ComFiT/blob/main/comfit/tools/tool_animation.py#L33 (
image_size
vs.image_size_inches
).b) If I fix that, I still get the same error as before. I suspect that the images get cropped when the colorbar labels get too many digits. I attach the first 5 pngs I get below. You can see that they have different widths when the number of digits increases
Strange! It seems your setup with matplotlb ignores the custom size that I tried to impose. Can you try to vary image_size_inches and dpi and see if you get any changes?
I tried different image_size_inches
, no effect.
I could make it work by forcing the colorbar
not to push the image boundaries, e.g., like this
from matplotlib.ticker import FuncFormatter
...
if colorbar:
_fmt = lambda x, pos: '{:15f}'.format(x)
cbar = plt.colorbar(pcm, ax=ax, format=FuncFormatter(_fmt), fraction=0.046, pad=0.04)
Now, I would recommend to find a more robust solution to create the GIFs than relying on matplotlib to create images of exact same pixel count. This seems to be an open issue in matplotlib, see e.g. https://github.com/matplotlib/matplotlib/issues/8543
I have added another check in the tool_make_animation_gif
, whereby it checks if all the images are of the same dimensions and crops them if not. As a side note, I realized that since imageio relies on the pillow package, which can do the same job, I was able to remove the dependency on imageio from comfit, which should be a little bit more stable.
I was not able to reproduce the error with the packages you listed, but let me suggest three ways of exploring this issue:
1) Can you try to run comfit with this latest patch and see if the proposed patch works (even though it is not super elegant)?
2) I see that in the list of dependencies, you have comfit==1.4.2 installed. Is it possible that when you are running the script with import comfit
, that uses the current version from pypi (which does not have the patch implemented yet)? Could you make a clean virtual environnement with a clone of the github repository and only the required packages and see if it fixes the issue?
I agree that saving images as pngs and then to export them is not an optimal strategy, and I will think of future ways of making it better. If neither step 1 or 2 works, would you be open to leaving this issue open and still continuing with the submission of the paper, or do you think that this is essential for the package to be published in JOSS?
I have added another check in the
tool_make_animation_gif
, whereby it checks if all the images are of the same dimensions and crops them if not. As a side note, I realized that since imageio relies on the pillow package, which can do the same job, I was able to remove the dependency on imageio from comfit, which should be a little bit more stable.I was not able to reproduce the error with the packages you listed, but let me suggest three ways of exploring this issue:
1. Can you try to run comfit with this latest patch and see if the proposed patch works (even though it is not super elegant)?
I think it works now. I see the warning when the files are cropped, and I get the GIFs 👍
2. I see that in the list of dependencies, you have comfit==1.4.2 installed. Is it possible that when you are running the script with `import comfit`, that uses the current version from pypi (which does not have the patch implemented yet)? Could you make a clean virtual environnement with a clone of the github repository and only the required packages and see if it fixes the issue?
I was working with a clean venv
and commented out the pip install.
I agree that saving images as pngs and then to export them is not an optimal strategy, and I will think of future ways of making it better. If neither step 1 or 2 works, would you be open to leaving this issue open and still continuing with the submission of the paper, or do you think that this is essential for the package to be published in JOSS?
I think making the GIFs is a very nice feature so it is nice when it actually works. This seems to be the case now!
Hi,
I am running the
base_system_make_your_own_model
tutorial on my own laptop and get an error when trying to create the gif in the cellThe error reads (a bit annoying output from vscode):
The error apparently has something to do with
pillow
, of which I have version 10.3.0 installed.I attach a list of my library versions for reference
packages.txt