lissajou package

Submodules

File: animation_framework.py Author: Hermann Agossou

Description:

This file contains a framework for creating animations using matplotlib. It defines abstract base classes and subclasses for creating various types of animations, including parametric animations and 3D parametric animations.

class lissajou.anim.BaseAnimation

Bases: object

An abstract base class designed to simplify the creation of animations using matplotlib.

This class defines a framework for creating animations by specifying setup steps, frame updates, and parameters. Subclasses should provide implementations for abstract methods to define specific animation behaviors.

__animate__(t)

Private method to update the animation at each frame.

Parameters:

t (int) – The current frame number.

Returns:

A tuple containing the elements to update for the current frame.

__init__()

Constructor for the BaseAnimation class.

abstract __initialize_view_data__(t, ax)

Abstract method to initialize the view data for the first frame of the animation.

Parameters:
  • t (int) – The current frame number.

  • ax (matplotlib.axes.Axes) – The axes object for the animation.

Returns:

Initial view data for the animation. This can be matplotlib artists or any data structure that represents the initial state of the animated elements.

__is_first_it__(t)

Check if the current frame is the first one.

Parameters:

t (int) – The current frame number.

Returns:

True if this is the first frame, False otherwise.

Return type:

bool

abstract __reset_view_data__(t, view)

Abstract method to update the view data for each subsequent frame after the first.

This method should update the state of the animated elements based on the current frame number.

Parameters:
  • t (int) – The current frame number.

  • view – The view data from the previous frame, to be updated for the current frame.

Returns:

Updated view data for the current frame. This is typically the same object(s) as view, but updated to reflect the new frame’s state.

abstract __set_axes__()

Abstract method for setting up the axes of the figure.

Subclasses must implement this method to define the axes setup.

Returns:

A tuple containing the figure and axes objects.

__setup__(**kwargs)

Private method for initial setup of the animation. It configures parameters, sets up the figure and axes, and initializes the animation.

Keyword Arguments:

**kwargs – Arbitrary keyword arguments for animation parameters.

_before_all_iterations()

Hook method called before the animation starts.

Subclasses can override this method to perform any necessary setup before the animation iterations begin. By default, this method does nothing.

abstract _before_each_iteration(t)

Abstract method called before each frame is processed.

Parameters:

t (int) – The current frame number.

abstract _set_params(max_frames: int | None = None, speed: float | None = None)

Abstract method for setting animation parameters.

Subclasses should implement this method to update animation-specific parameters.

Parameters:
  • max_frames (int, optional) – The maximum number of frames for the animation.

  • speed (float, optional) – The time interval between frames in milliseconds.

max_frames = 100
save(file_path: str | Path, **kwargs)

Saves the animation to a file based on the file extension.

Parameters:
  • file_path (str) – The path where the animation will be saved.

  • **kwargs – Additional keyword arguments passed to the animation writer.

Supported file formats:
  • Images: png, jpeg, jpg, tiff

  • Animated Images: gif

  • HTML: html, htm

  • Video: mp4

For formats requiring external dependencies (e.g., ffmpeg), ensure the corresponding dependencies are installed in the system.

If the specified format is unsupported, the function attempts to save the animation using the specified format anyway.

Note: Some formats may require specific writers (e.g., ‘pillow’ for GIFs, ‘ffmpeg’ for videos). If a writer is not explicitly provided, default writers are used based on the file extension.

Examples

anim.save(‘animation.mp4’) # Save as MP4 video with default parameters. anim.save(‘animation.gif’, writer=’pillow’, fps=20) # Save as GIF with custom parameters.

setup(**kwargs)

Sets up the animation with specified parameters.

This method initializes the animation by configuring parameters, setting up the figure and axes, and initializing the animation itself.

Keyword Arguments:

**kwargs – Arbitrary keyword arguments for animation parameters.

show(**kwargs)

Sets up the animation and displays it.

Accepts various keyword arguments to customize animation parameters before display.

speed = 33.333333333333336
class lissajou.anim.ImageAnimation

Bases: BaseAnimation

A subclass of BaseAnimation designed for creating animations with image data.

This class initializes animation view data with random binary images and allows for updating the view data using a custom image function ‘_img_fct’ for each frame.

__get_img_vals__(t) ndarray

Computes the image values for the current frame using a custom image function.

Parameters:

t (int) – The current frame number.

Returns:

Computed image values as a NumPy array.

__initialize_view_data__(t, ax)

Initializes the view data for the animation with random binary images.

Parameters:
  • t (int) – The current frame number (not used in this method).

  • ax (matplotlib.axes.Axes) – The axes object for the animation.

Returns:

The initial view data for the animation, represented as an image.

__reset_view_data__(t, view)

Updates the view data for each frame of the animation with computed image values.

Parameters:
  • t (int) – The current frame number.

  • view – The view data from the previous frame (image object).

Returns:

The updated view data for the current frame.

__set_axes__()

Sets up the figure and axes for the animation.

Returns:

A tuple (fig, ax) with the matplotlib figure and axes objects.

abstract _img_fct(t) ndarray

Abstract method to compute image values for each frame of the animation.

Parameters:

t (int) – The current frame number.

Returns:

Computed image values as a NumPy array.

_set_params(shape: Tuple[int], **kwargs)

Sets the parameters for the animation, including the shape of the image.

Parameters:

shape (Tuple[int]) – The shape (height, width) of the image.

height: int = None
width: int = None
class lissajou.anim.ParametricAnimation

Bases: BaseAnimation

A subclass of BaseAnimation designed for creating parametric animations.

This class introduces x and y ranges for the plot and requires the definition of parametric functions _fx and _fy to compute the x and y coordinates of the plot for each frame based on a parameter t.

__initialize_view_data__(t, ax)

Initializes the view data for the animation based on the parametric functions.

Parameters:
  • t (int) – The current frame number.

  • ax (matplotlib.axes.Axes) – The axes object for the animation.

Returns:

The initial view data for the animation.

__reset_view_data__(t, view)

Updates the view data for each frame.

Parameters:
  • t (int) – The current frame number.

  • view – The view data from the previous frame.

Returns:

The updated view data for the current frame.

__set_axes__()

Sets up the figure and axes for the animation with optional x and y ranges.

Returns:

A tuple (fig, ax) with the matplotlib figure and axes objects.

abstract _fx(t)

Abstract method to compute the x-coordinate based on the parameter t.

Parameters:

t (int) – The current frame number or time parameter.

Returns:

The x-coordinate for the plot at time t.

abstract _fy(t)

Abstract method to compute the y-coordinate based on the parameter t.

Parameters:

t (int) – The current frame number or time parameter.

Returns:

The y-coordinate for the plot at time t.

_set_params(x_range: ndarray | List[float] | None = None, y_range: ndarray | List[float] | None = None, **kwargs)

Sets the parameters for the animation, including the x and y ranges.

Parameters:
  • x_range (Union[np.ndarray, List[float]], optional) – The range of the x-axis as [min, max].

  • y_range (Union[np.ndarray, List[float]], optional) – The range of the y-axis as [min, max].

x_range = None
y_range = None
class lissajou.anim.ThreeDimensionalParametricAnimation

Bases: ParametricAnimation

A subclass of ParametricAnimation for creating 3D parametric animations.

This class adds a third dimension to the animations, allowing for the creation of dynamic 3D visualizations. It introduces a z-axis range and requires the definition of a function _fz to compute the z-coordinate for each frame.

__initialize_view_data__(t, ax)

Initializes the view data for the 3D animation based on the parametric functions.

Parameters:
  • t (int) – The current frame number.

  • ax (matplotlib.axes._subplots.Axes3DSubplot) – The 3D axes object for the animation.

Returns:

The initial view data for the 3D animation.

__reset_view_data__(t, view)

Updates the view data for each frame of the 3D animation.

Parameters:
  • t (int) – The current frame number.

  • view – The view data from the previous frame.

Returns:

The updated view data for the current frame.

__set_axes__()

Sets up the figure and axes for the 3D animation with optional x, y, and z ranges.

Returns:

A tuple (fig, ax) with the matplotlib figure and 3D axes objects.

abstract _fz(t)

Abstract method to compute the z-coordinate based on the parameter t.

Parameters:

t (int) – The current frame number or time parameter.

Returns:

The z-coordinate for the plot at time t.

_set_params(z_range: ndarray | List[float] | None = None, **kwargs)

Sets the parameters for the animation, including the z range, in addition to the base class parameters.

Parameters:

z_range (Union[np.ndarray, List[float]], optional) – The range of the z-axis as [min, max].

z_range = None

Lissajous Animation Simulation

This module provides classes for simulating Lissajous curves and generating animations based on various parameters and configurations.

Classes: - GenericLissajou: Simulates a generic Lissajous curve animation. - VaryingAmplitudeLissajou: Simulates a Lissajous curve with varying amplitude. - SinusoidalAmplitudeLissajou: Simulates a Lissajous curve with a sinusoidal variation in amplitude. - FixedRatioLissajou: Simulates a Lissajous curve with a fixed ratio of amplitudes. - EllipticLissajou: Simulates a Lissajous ellipse animation. - CircularLissajou: Simulates a Lissajous circle animation.

Usage: Each class provides a method ‘show()’ to display the animation. Instantiate the desired class object and call ‘show()’ to view the animation.

Example

# Simulate and display a Lissajous ellipse animation animation = EllipticLissajou() animation.show()

class lissajou.lissajou.CircularLissajou(a=5, range=20, nb_points=10000)

Bases: EllipticLissajou

A class for simulating Lissajous circles.

Usage:

animation = CircularLissajou() animation.show()

Note

  • The ratio ‘b/a’ is kept constant to produce circles.

  • The phase difference ‘delta’ is set to pi/2 to produce circles.

class lissajou.lissajou.EllipticLissajou(a=5, delta=0.3141592653589793, range=20, nb_points=10000)

Bases: FixedRatioLissajou

A class for simulating Lissajous ellipses.

Usage:

animation = EllipticLissajou() animation.show()

Note

  • The ratio ‘b/a’ is kept constant to produce ellipses.

class lissajou.lissajou.FixedRatioLissajou(a=5, delta=0.3141592653589793, range=20, nb_points=10000, ratio=5)

Bases: GenericLissajou

A class for simulating closed Lissajous curves with fixed ratio ‘b/a’.

Parameters:
  • a (float) – The parameter controlling the x-coordinate.

  • delta (float) – The phase difference between the two sine functions.

  • range (float) – The range of the parameter ‘t’ for the simulation.

  • nb_points (int) – The number of points to generate within the range.

  • ratio (float) – The fixed ratio between ‘b’ and ‘a’.

Usage:

animation = FixedRatioLissajou() animation.show()

Note

  • The ratio ‘b/a’ is kept constant to produce closed curves.

class lissajou.lissajou.GenericLissajou(a=5, b=4, delta=1.5707963267948966, range=20, nb_points=10000)

Bases: ParametricAnimation

A class for simulating Lissajous curves.

Parameters:
  • a (float) – The parameter controlling the x-coordinate.

  • b (float) – The parameter controlling the y-coordinate.

  • delta (float) – The phase difference between the two sine functions.

  • range (float) – The range of the parameter ‘t’ for the simulation. A higher range will put more points on each frame

  • nb_points (int) – The number of points to generate within the range.

Usage:

```python # example 1 animation = GenericLissajou() animation.show() # exmple2: change lissajou parameters animation = GenericLissajou(a=3, b=2, delta=np.pi / 2) # example2: change the visualization params animation = GenericLissajou(

a=1, b=2, delta=np.pi / 2, range=50, nb_points=100000

) animation.max_frames = 10000 animation.show() ```

class lissajou.lissajou.SinusoidalAmplitudeLissajou(a=5, b=4, delta=1.5707963267948966, range=20, nb_points=10000)

Bases: GenericLissajou

A class for simulating Lissajous curves with a constant sinusoidal effect on ‘b’.

Usage:

animation = SinusoidalAmplitudeLissajou() animation.show()

Note

  • The parameter ‘a’ is fixed.

  • The parameter ‘delta’ is fixed at pi/2.

  • The parameter ‘b’ varies with cosine of time for interesting effects.

class lissajou.lissajou.VaryingAmplitudeLissajou(a=5, b=4, delta=1.5707963267948966, range=20, nb_points=10000)

Bases: GenericLissajou

A class for simulating Lissajous curves with varying ratios and sinusoidal ‘b’.

Usage:

animation = VaryingAmplitudeLissajou() animation.show()

Note

  • The parameter ‘a’ is fixed.

  • The parameter ‘delta’ is fixed at pi/2.

  • The parameter ‘b’ follows a sinusoidal function, resulting in dancing curves.

Module contents