desy-ml / cheetah

Fast and differentiable particle accelerator optics simulation for reinforcement learning and optimisation applications.
https://cheetah-accelerator.readthedocs.io
GNU General Public License v3.0
25 stars 12 forks source link

Use KDE for beam screen images #200

Open cr-xu opened 5 days ago

cr-xu commented 5 days ago

Description

Add an option for Screen element. Now it can use KDE to get the beam image which allows differentiation. Behavior can be switched using method="histogram" and method="kde". It contains a new feature kde_bandwidth for smoothing the beam image.

The returned image has the convention [...,Y, X] where y-axis starts from the top of the image, e.g. plt.imshow() will show the image upright


import cheetah
import torch
import matplotlib.pyplot as plt

incoming = cheetah.ParticleBeam.from_parameters(num_particles = 10000, mu_x = torch.tensor([-0.5e-3]), mu_y=torch.tensor([1.0e-3]), sigma_x = torch.tensor([1e-4]), sigma_y = torch.tensor([3e-4]))

screen = cheetah.Screen(resolution=(1200, 800), pixel_size=(5e-6, 5e-6), binning=torch.tensor(2), is_active=True)

screen.track(incoming)

plt.imshow(screen.reading[0], extent=screen.extent, cmap='gray')
plt.xlabel('x [m]')
plt.ylabel('y [m]')

image

Motivation and Context

Types of changes

Checklist

Note: We are using a maximum length of 88 characters per line

cr-xu commented 5 days ago

Hmm... so KDE clearly makes the screen reading slower, as expected :)

@jank324 Shall we

jank324 commented 5 days ago

I think we should provide both. Im just wondering whether it should be use_kde=True or method="kde".