Skip to content
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

Beam stats #129

Merged
merged 6 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions sirepo_bluesky/shadow_handler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import contextlib
import os

import numpy as np
import Shadow.ShadowLibExtensions as sd
import Shadow.ShadowTools

from . import utils


def read_shadow_file_col(filename, parameter=30):
"""Read specified parameter from the Shadow3 output binary file.
Expand Down Expand Up @@ -49,7 +54,9 @@ def read_shadow_file_col(filename, parameter=30):
32 S2-stokes = 2 |Es| |Ep| cos(phase_s-phase_p)
33 S3-stokes = 2 |Es| |Ep| sin(phase_s-phase_p)
"""
data = Shadow.ShadowTools.getshcol(filename, col=parameter)
with open(os.devnull, "w") as devnull:
with contextlib.redirect_stdout(devnull):
data = Shadow.ShadowTools.getshcol(filename, col=parameter)

mean_value = np.mean(data)

Expand All @@ -73,24 +80,35 @@ def read_shadow_file(filename, histogram_bins=None):
beam.load(filename)

# 1=X spatial coordinate; 3=Z spatial coordinate
data_dict = beam.histo2(1, 3, nolost=1, nbins=histogram_bins)
data = data_dict["histogram"]
with open(os.devnull, "w") as devnull:
with contextlib.redirect_stdout(devnull):
data_dict = beam.histo2(1, 3, nolost=1, nbins=histogram_bins)

# This returns a list of N values (N=number of rays)
photon_energy_list = Shadow.ShadowTools.getshcol(filename, col=11) # 11=Energy [eV]
# This returns a list of N values (N=number of rays)
photon_energy_list = Shadow.ShadowTools.getshcol(filename, col=11) # 11=Energy [eV]

data = data_dict["histogram"]
photon_energy = np.mean(photon_energy_list)

return {
# convert to um
horizontal_extent = 1e3 * np.array(data_dict["xrange"][:2])
vertical_extent = 1e3 * np.array(data_dict["yrange"][:2])
mrakitin marked this conversation as resolved.
Show resolved Hide resolved

ret = {
"data": data,
"shape": data.shape,
"mean": np.mean(data),
"flux": data.sum(),
"mean": data.mean(),
"photon_energy": photon_energy,
"horizontal_extent": data_dict["xrange"][:2],
"vertical_extent": data_dict["yrange"][:2],
# 'labels': labels,
# 'units': units,
"horizontal_extent": horizontal_extent,
"vertical_extent": vertical_extent,
"units": "um",
}

ret.update(utils.get_beam_stats(data, horizontal_extent, vertical_extent))

return ret


class ShadowFileHandler:
specs = {"shadow"}
Expand Down
19 changes: 17 additions & 2 deletions sirepo_bluesky/sirepo_ophyd.py
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just a question, are 'x' and 'y' the centroid position?

Copy link
Member

Choose a reason for hiding this comment

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

I think so.

Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ def trigger(self, *args, **kwargs):
class SirepoWatchpoint(DeviceWithJSONData):
image = Cpt(ExternalFileReference, kind="normal")
shape = Cpt(Signal)
mean = Cpt(Signal, kind="hinted")
flux = Cpt(Signal, kind="hinted")
mean = Cpt(Signal, kind="normal")
x = Cpt(Signal, kind="normal")
y = Cpt(Signal, kind="normal")
fwhm_x = Cpt(Signal, kind="hinted")
fwhm_y = Cpt(Signal, kind="hinted")
photon_energy = Cpt(Signal, kind="normal")
horizontal_extent = Cpt(Signal)
vertical_extent = Cpt(Signal)
Expand Down Expand Up @@ -152,7 +157,12 @@ def trigger(self, *args, **kwargs):

def update_components(_data):
self.shape.put(_data["shape"])
self.flux.put(_data["flux"])
self.mean.put(_data["mean"])
self.x.put(_data["x"])
self.y.put(_data["y"])
self.fwhm_x.put(_data["fwhm_x"])
self.fwhm_y.put(_data["fwhm_y"])
self.photon_energy.put(_data["photon_energy"])
self.horizontal_extent.put(_data["horizontal_extent"])
self.vertical_extent.put(_data["vertical_extent"])
Expand Down Expand Up @@ -233,7 +243,12 @@ def trigger(self, *args, **kwargs):

def update_components(_data):
self.shape.put(_data["shape"])
self.flux.put(_data["flux"])
self.mean.put(_data["mean"])
self.x.put(_data["x"])
self.y.put(_data["y"])
self.fwhm_x.put(_data["fwhm_x"])
self.fwhm_y.put(_data["fwhm_y"])
self.photon_energy.put(_data["photon_energy"])
self.horizontal_extent.put(_data["horizontal_extent"])
self.vertical_extent.put(_data["vertical_extent"])
Expand Down Expand Up @@ -401,7 +416,7 @@ def create_classes(sirepo_data, connection, create_objects=True, extra_model_fie

components[k] = Cpt(
cpt_class,
value=v,
value=(float(v) if type(v) is int else v),
sirepo_dict=sirepo_dict,
sirepo_param=k,
)
Expand Down
24 changes: 18 additions & 6 deletions sirepo_bluesky/srw_handler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
import srwpy.uti_plot_com as srw_io

from . import utils


def read_srw_file(filename, ndim=2):
data, mode, ranges, labels, units = srw_io.file_load(filename)
Expand All @@ -13,18 +15,28 @@ def read_srw_file(filename, ndim=2):
else:
raise ValueError(f"The value ndim={ndim} is not supported.")

return {
horizontal_extent = np.array(ranges[3:5])
vertical_extent = np.array(ranges[6:8])

ret = {
"data": data,
"shape": data.shape,
"mean": np.mean(data),
"flux": data.sum(),
"mean": data.mean(),
"photon_energy": photon_energy,
"horizontal_extent": ranges[3:5],
"vertical_extent": ranges[6:8],
# 'mode': mode,
"horizontal_extent": horizontal_extent,
"vertical_extent": vertical_extent,
"labels": labels,
"units": units,
"units": "units",
Copy link
Member

Choose a reason for hiding this comment

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

We have a variable returned from srw_io.file_load(filename) called units. Was the overwrite intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops, I'll fix this

}

if ndim == 1:
ret.update({key: np.nan for key in ["x", "y", "fwhm_x", "fwhm_y"]})
if ndim == 2:
ret.update(utils.get_beam_stats(data, horizontal_extent, vertical_extent))

return ret


class SRWFileHandler:
specs = {"srw"}
Expand Down
27 changes: 27 additions & 0 deletions sirepo_bluesky/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import numpy as np


def get_beam_stats(image, x_extent, y_extent):
n_y, n_x = image.shape

if image.sum() > 0:
Copy link
Member

Choose a reason for hiding this comment

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

image.sum() is used here and in return. Let's calculate it once and use in all 6 places.

X, Y = np.meshgrid(np.linspace(*x_extent, n_x), np.linspace(*y_extent, n_y))

mean_x = np.sum(X * image) / np.sum(image)
mean_y = np.sum(Y * image) / np.sum(image)

sigma_x = np.sqrt(np.sum((X - mean_x) ** 2 * image) / np.sum(image))
sigma_y = np.sqrt(np.sum((Y - mean_y) ** 2 * image) / np.sum(image))
Copy link
Member

Choose a reason for hiding this comment

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

np.sum(image) is done 4 times. Please calculate it once and reuse it.


else:
mean_x, mean_y, sigma_x, sigma_y = np.nan, np.nan, np.nan, np.nan

return {
"shape": (n_y, n_x),
"flux": image.sum(),
"mean": image.mean(),
"x": mean_x,
"y": mean_y,
"fwhm_x": 2 * np.sqrt(2 * np.log(2)) * sigma_x,
"fwhm_y": 2 * np.sqrt(2 * np.log(2)) * sigma_y,
Copy link
Member

Choose a reason for hiding this comment

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

The factor can be calculated once and reused. Please update.

}
Loading