Skip to content

Instantly share code, notes, and snippets.

@FeepingCreature
Created June 25, 2024 23:20
Show Gist options
  • Save FeepingCreature/1a1ad917a717a891ac2d280947003882 to your computer and use it in GitHub Desktop.
Save FeepingCreature/1a1ad917a717a891ac2d280947003882 to your computer and use it in GitHub Desktop.
import math
import numpy as np
import matplotlib.pyplot as plt
def apply_strength(strength: float, steps: int, min_steps: int = 0) -> tuple[int, int]:
start_at_step = round(steps * (1 - strength))
if min_steps and steps - start_at_step < min_steps:
steps = math.floor(min_steps / strength)
start_at_step = steps - min_steps
return steps, start_at_step
def plot_strengths(max_steps: int, min_steps: int):
# Generate 100 strengths from 0.01 to 1.00
original_strengths = np.linspace(0.01, 1.00, 100)
reconstructed_strengths_1 = []
reconstructed_strengths_2 = []
strength_differences = []
for strength in original_strengths:
# First application
steps_1, start_at_step_1 = apply_strength(strength, max_steps, min_steps)
reconstructed_strength_1 = (steps_1 - start_at_step_1) / steps_1
reconstructed_strengths_1.append(reconstructed_strength_1)
# Second application
steps_2, start_at_step_2 = apply_strength(reconstructed_strength_1, max_steps, min_steps)
reconstructed_strength_2 = (steps_2 - start_at_step_2) / steps_2
reconstructed_strengths_2.append(reconstructed_strength_2)
# Calculate difference
strength_differences.append(reconstructed_strength_1 - reconstructed_strength_2)
# Plotting
# Plotting
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(12, 12), height_ratios=[3, 1], sharex=True)
# Upper plot: Original and reconstructed strengths
ax1.plot(original_strengths, original_strengths, label='Original Strength', linestyle='--')
ax1.plot(original_strengths, reconstructed_strengths_1, label='1st Reconstruction')
ax1.plot(original_strengths, reconstructed_strengths_2, label='2nd Reconstruction')
ax1.set_ylabel('Strength')
ax1.set_title(f'Original vs Reconstructed Strengths (max_steps={max_steps}, min_steps={min_steps})')
ax1.legend()
ax1.grid(True)
# Lower plot: Difference between 1st and 2nd reconstruction
ax2.plot(original_strengths, strength_differences, label='Difference (1st - 2nd)', color='red')
ax2.set_xlabel('Original Strength')
ax2.set_ylabel('Strength Difference')
ax2.set_title('Difference between 1st and 2nd Reconstruction')
ax2.legend()
ax2.grid(True)
plt.tight_layout()
plt.show()
# Example usage
plot_strengths(max_steps=8, min_steps=3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment