napari / napari-animation

A napari plugin for making animations
https://napari.github.io/napari-animation/
Other
74 stars 27 forks source link

examples/animateMixed.py #156

Closed AlexSauer closed 7 months ago

AlexSauer commented 1 year ago

Hi,

just discovered this plugin and already a big fan! I was trying to run the animatedMixed.py example but getting the following error message which seems to happen during the switch from 2D to 3D.

Rendering frames...
 50%|██████████████████████████████████████████████████▋                                                  | 166/331 [00:25<00:25,  6.51it/s]
Traceback (most recent call last):
  File "/Users/alex/Documents/python/animateMixed.py", line 45, in <module>
    animation.animate('demoMixed.mov', canvas_only=False)
  File "/Users/alex/anaconda3/envs/napari/lib/python3.9/site-packages/napari_animation/animation.py", line 214, in animate
    for frame_index, image in enumerate(frame_generator):
  File "/Users/alex/anaconda3/envs/napari/lib/python3.9/site-packages/napari_animation/frame_sequence.py", line 121, in iter_frames
    frame = state.render(viewer, canvas_only=canvas_only)
  File "/Users/alex/anaconda3/envs/napari/lib/python3.9/site-packages/napari_animation/viewer_state.py", line 79, in render
    self.apply(viewer)
  File "/Users/alex/anaconda3/envs/napari/lib/python3.9/site-packages/napari_animation/viewer_state.py", line 59, in apply
    setattr(layer, attribute_name, value)
  File "/Users/alex/anaconda3/envs/napari/lib/python3.9/site-packages/napari/layers/image/image.py", line 533, in interpolation
    self._interpolation[self._ndisplay] = Interpolation(interpolation)
  File "/Users/alex/anaconda3/envs/napari/lib/python3.9/site-packages/napari/utils/misc.py", line 270, in __call__
    return super().__call__(value.lower())
  File "/Users/alex/anaconda3/envs/napari/lib/python3.9/enum.py", line 384, in __call__
    return cls.__new__(cls, value)
  File "/Users/alex/anaconda3/envs/napari/lib/python3.9/enum.py", line 702, in __new__
    raise ve_exc
ValueError: 'linear' is not a valid Interpolation

Can anyone reproduce this problem or has an idea how I can fix it?

alisterburt commented 1 year ago

uh oh! Thanks for the report

If I remember rightly @brisvag made some changes to how napari handles interpolations in 3D which changed the API slightly. He might have a quick fix but otherwise @katherine-hutchings and I can take a look when we pair next week 🙂

brisvag commented 1 year ago

Yeah, it's probably that; odd error though, it seems like napari-animation is providing the new value but napari is older?. bilinear is now replaced by linear. Also, interpolation2d and interpolation3d are now separate for Image.

alisterburt commented 1 year ago

@brisvag is the above true in the released version or just on main? not sure where this is running - @AlexSauer what version of napari are you using?

brisvag commented 1 year ago

The 0.4.17 release has the new api (and deprecations for the older one).

alisterburt commented 1 year ago

sweet, thanks!

AlexSauer commented 1 year ago

ah yeah, I was still running 0.4.16, thanks a lot!

psobolewskiPhD commented 9 months ago

Would be good to know the version of napari-animation that was involved here. The current version 0.0.7 has multiple issues in this context: https://github.com/napari/napari-animation/issues/177 https://github.com/napari/napari-animation/issues/180

But I think the issue is that there are different options for interpolation in 2d and 3d. linear is valid in 0.4.16 but only in 3d. In current napari linear works in both, so it "fixes it" but not really? I this might be an issue with the way the attributes are handled between keyframes? I'll poke around.

psobolewskiPhD commented 7 months ago

I think I was on the right track. So the viewer_state is set correctly. But when napari-animation does the interpolation for the layer_state interpolation (in 0.4.16 shared between 2d and 3d) it uses: https://github.com/napari/napari-animation/blob/5760ee2902f59edf6eeb85bd10651452f39746a4/napari_animation/interpolation/base_interpolation.py#L93-L113 Which means that that interpolation mode is switched immediately.

However, dims.ndisplay is interpolated using default, so as int: https://github.com/napari/napari-animation/blob/5760ee2902f59edf6eeb85bd10651452f39746a4/napari_animation/interpolation/base_interpolation.py#L70-L90

You can see that the value is computed, but then coerced to int, which truncates the decimals, resulting in the opposite effect to the bool interpolation.

Importantly, this is still present in current napari. You don't get an error, because layers have both interpolation modes. But use 3D blobs and make a 2D keyframe, then change something along with ndisplay to 3, and set new keyframe. You will see the first parameter get changed for the steps and then 3D toggle at the end.

So I think because ndisplay can only be 2 or 3, it should be interpolated like bool, rather than like number. Which I think can be done here: https://github.com/napari/napari-animation/blob/5760ee2902f59edf6eeb85bd10651452f39746a4/napari_animation/frame_sequence.py#L80-L82

I'm going to give it a whirl and see what happens.