Skip to content

Fixed there_and_back_with_pause() rate function behaviour with different pause_ratio values #3778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 22, 2024
Merged

Fixed there_and_back_with_pause() rate function behaviour with different pause_ratio values #3778

merged 2 commits into from
May 22, 2024

Conversation

jkjkil4
Copy link
Contributor

@jkjkil4 jkjkil4 commented May 22, 2024

Overview: What does this pull request change?

fix the behaviour of there_and_back_with_pause when setting pause_ratio with other values.

Further Information and Comments

the picture below shows the bug of there_and_back_with_pause (left), and the corrected result (right)

image

this picture is generated by the following code:

# In[]
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.axes import Axes


def sigmoid(x: float) -> float:
    return 1.0 / (1 + np.exp(-x))


def smooth(t: float, inflection: float = 10.0) -> float:
    error = sigmoid(-inflection / 2)
    return min(
        max((sigmoid(inflection * (t - 0.5)) - error) / (1 - 2 * error), 0),
        1,
    )


def there_and_back_with_pause(t: float, pause_ratio: float = 1.0 / 3) -> float:
    a = 1.0 / pause_ratio
    if t < 0.5 - pause_ratio / 2:
        return smooth(a * t)
    elif t < 0.5 + pause_ratio / 2:
        return 1
    else:
        return smooth(a - a * t)


def there_and_back_with_pause_corrected(t: float, pause_ratio: float = 1.0 / 3) -> float:
    a = 2.0 / (1.0 - pause_ratio)
    if t < 0.5 - pause_ratio / 2:
        return smooth(a * t)
    elif t < 0.5 + pause_ratio / 2:
        return 1
    else:
        return smooth(a - a * t)


def plot(axes: Axes, func, pause_ratio: float, aspect_eq=True) -> None:
    axes.set_title(f'...{func.__name__[14:]}(t, pause_ratio={pause_ratio:.3f})',
                   fontsize=9)
    axes.set_aspect('equal')
    x = np.linspace(0, 1, 100)
    y = [func(v, pause_ratio=pause_ratio) for v in x]
    axes.scatter(x, y, s=3)


fig = plt.figure(figsize=(10, 10))

plot(fig.add_subplot(4, 2, 1), there_and_back_with_pause, 0.1)
plot(fig.add_subplot(4, 2, 3), there_and_back_with_pause, 1 / 3)
plot(fig.add_subplot(4, 2, 5), there_and_back_with_pause, 0.5)
plot(fig.add_subplot(4, 2, 7), there_and_back_with_pause, 0.8)

plot(fig.add_subplot(4, 2, 2), there_and_back_with_pause_corrected, 0.1)
plot(fig.add_subplot(4, 2, 4), there_and_back_with_pause_corrected, 1 / 3)
plot(fig.add_subplot(4, 2, 6), there_and_back_with_pause_corrected, 0.5)
plot(fig.add_subplot(4, 2, 8), there_and_back_with_pause_corrected, 0.8)

fig.tight_layout()
fig.subplots_adjust(wspace=-0.6)
fig.show()

# %%

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

Copy link
Member

@behackl behackl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for your contribution!

@behackl behackl added the pr:bugfix Bug fix for use in PRs solving a specific issue:bug label May 22, 2024
@behackl behackl added this to the v0.19.0 milestone May 22, 2024
@chopan050 chopan050 changed the title fix: there_and_back_with_pause Fixed there_and_back_with_pause() rate function behaviour with different pause_ratio values May 22, 2024
@chopan050 chopan050 enabled auto-merge (squash) May 22, 2024 18:11
@chopan050 chopan050 merged commit 0a2fbbe into ManimCommunity:main May 22, 2024
17 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:bugfix Bug fix for use in PRs solving a specific issue:bug
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants