Flow-Matching icon indicating copy to clipboard operation
Flow-Matching copied to clipboard

Playing a bit with the notebook with more examples

Open rom1504 opened this issue 1 year ago • 1 comments

Hey, thanks for the notebook, it just works and simple to understand

I played a bit with it

I asked chatgpt to change the first cell to instead be a checkerboard of ellipses; and it worked on first try; here's the result

image

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Ellipse

# Parameters
N = 1000  # Number of points to sample
x_min, x_max = -4, 4
y_min, y_max = -4, 4
rows, cols = 4, 4  # Number of rows and columns in the checkerboard

# Ellipse parameters
a, b = 1, 0.5  # Semi-major and semi-minor axes
x_spacing = (x_max - x_min) / cols
y_spacing = (y_max - y_min) / rows

# Generate the centers of the ellipses
centers = [
    (x_min + (i + 0.5) * x_spacing, y_min + (j + 0.5) * y_spacing)
    for j in range(rows) for i in range(cols)
]

# Checkerboard pattern
checkerboard = np.indices((rows, cols)).sum(axis=0) % 2

# Efficient sampling
all_sampled_points = []
points_per_ellipse = N // (rows * cols)  # Approximate points per ellipse

for j in range(rows):
    for i in range(cols):
        # Skip inactive cells
        if checkerboard[j, i] == 0:
            continue
        
        # Center of the current ellipse
        x_center, y_center = centers[j * cols + i]
        
        # Sample points within this ellipse
        n_points = points_per_ellipse  # Number of points for this ellipse
        theta = np.random.uniform(0, 2 * np.pi, n_points)  # Random angles
        r = np.sqrt(np.random.uniform(0, 1, n_points))  # Uniform coverage in area
        
        # Scale points to cover the ellipse area fully
        x_sample = x_center + r * a * np.cos(theta)
        y_sample = y_center + r * b * np.sin(theta)
        
        # Append sampled points
        all_sampled_points.extend(zip(x_sample, y_sample))

# Convert to NumPy array
sampled_points = np.array(all_sampled_points[:N])  # Trim to exact N points

# Plot the checkerboard of ellipses
plt.figure(figsize=(8, 8))

# Draw ellipses
for j in range(rows):
    for i in range(cols):
        if checkerboard[j, i] == 1:
            x_center, y_center = centers[j * cols + i]
            ellipse = Ellipse(
                (x_center, y_center), width=2*a, height=2*b, edgecolor='purple', facecolor='none', linewidth=1.5
            )
            plt.gca().add_patch(ellipse)

# Plot sampled points
plt.scatter(sampled_points[:, 0], sampled_points[:, 1], color="red", marker="o", label="Sampled Points")
plt.xlim(x_min, x_max)
plt.ylim(y_min, y_max)
plt.gca().set_aspect('equal', adjustable='box')
plt.title(f"Checkerboard of Ellipses with Sampled Points (N={N})")
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.legend()
plt.show()

with this being the target image

rom1504 avatar Nov 17 '24 16:11 rom1504

Hey really cool rom!

dome272 avatar Nov 18 '24 19:11 dome272