Skip to content
Snippets Groups Projects
Commit 2e6e8aac authored by Egor Sobolev's avatar Egor Sobolev
Browse files

Update powder notebook

parent bd140591
No related branches found
No related tags found
1 merge request!3Center refinement using powder diffraction data
%% Cell type:code id:c134ff02-0eba-4146-803b-2b69f9ccaef9 tags: %% Cell type:code id:c134ff02-0eba-4146-803b-2b69f9ccaef9 tags:
``` python ``` python
runno = 189 propno, runno = 2671, 409
propno = 900463 propno, runno = 6880, 42
propno, runno = 7955, 1
ref_geom_fn = "/gpfs/exfel/exp/SPB/202430/p900429/usr/Shared/geom/p5681_r64_67_refined.geom" # initial geometry file name detector_id = "SPB_DET_AGIPD1M-1"
legacy = True
ref_geom_fn = "/gpfs/exfel/exp/SPB/202530/p900510/usr/Shared/geom/agipd_p008316_r0024_v04.geom" # initial geometry file name
# image averager parameters
mask_fn = "/gpfs/exfel/exp/SPB/202530/p900510/usr/Shared/geom/mask_hvoff_20250124.h5"
#propno, runno = 8376, 5
#detector_id = "SPB_IRDA_JF4M"
#ref_geom_fn = "/gpfs/exfel/exp/SPB/202425/p008376/usr/Shared/amore/geom/jf4m_p7216_v105.geom" # initial geometry file name
# image averager parameters # image averager parameters
mask_fn = "/gpfs/exfel/exp/SPB/202431/p900463/usr/Shared/mask/mask_hvoff_20240807.h5" #mask_fn = None
# slurm paramenters # slurm paramenters
num_nodes = 16 num_nodes = 16
slurm_partition = "exfel" slurm_partition = "exfel"
``` ```
%% Cell type:code id:0bdf665e-9ad7-4a40-a59e-23b428312f4f tags: %% Cell type:code id:0bdf665e-9ad7-4a40-a59e-23b428312f4f tags:
``` python ``` python
# !geomtools-powder-center -g {ref_geom_fn} -c AgBh ../../../nb/powder_sum_p{propno:06d}_r{runno:04d}.h5 # !geomtools-powder-center -g {ref_geom_fn} -c AgBh ../../../nb/powder_sum_p{propno:06d}_r{runno:04d}.h5
``` ```
%% Cell type:code id:504eca33-7f3d-4b7e-9ce1-9edcadff6387 tags: %% Cell type:code id:504eca33-7f3d-4b7e-9ce1-9edcadff6387 tags:
``` python ``` python
import os import os
import numpy as np import numpy as np
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import h5py import h5py
from extra_data import open_run from extra_data import open_run
from extra_geom import agipd_asic_seams, AGIPD_1MGeometry from extra_geom import agipd_asic_seams, AGIPD_1MGeometry, JUNGFRAUGeometry
from extra_geom.motors import AGIPD_1MMotors from extra_geom.motors import AGIPD_1MMotors, JF4MMotors
from extra.components import AGIPD1MQuadrantMotors from extra.components import AGIPD1MQuadrantMotors, JF4MHalfMotors
from geomtools.powder.calibrants import get_calibrant from geomtools.powder.calibrants import get_calibrant
from geomtools.powder.misc import agipd_borders from geomtools.powder.misc import agipd_borders, jungfrau_borders
from geomtools.powder import PowderDiffraction from geomtools.powder import PowderDiffraction
``` ```
%% Cell type:code id:33f4f527-d61e-45c1-8301-1be7c6aa8edf tags: %% Cell type:code id:33f4f527-d61e-45c1-8301-1be7c6aa8edf tags:
``` python ``` python
fn = f"powder_sum_p{propno:06d}_r{runno:04d}.h5" fn = f"powder_sum_p{propno:06d}_r{runno:04d}.h5"
if not os.path.exists(fn): if not os.path.exists(fn):
sopts = f"-N {num_nodes} -p {slurm_partition} --ntasks-per-node=1" sopts = f"-N {num_nodes} -p {slurm_partition} --ntasks-per-node=1"
mpiopts = ("--map-by NODES -x LD_LIBRARY_PATH -x PATH --bind-to none " mpiopts = ("--map-by NODES -x LD_LIBRARY_PATH -x PATH --bind-to none "
"--mca btl ^smcuda --mca rcache ^gpusm,rgpusm --mca accelerator ^cuda") "--mca btl ^smcuda --mca rcache ^gpusm,rgpusm --mca accelerator ^cuda")
powder_sum_args = ( powder_sum_args = (
f"-p {propno} -r {runno} -m {mask_fn} --legacy -l agipd1m SPB_DET_AGIPD1M-1" f"-p {propno} -r {runno} -R "
) )
!salloc {sopts} mpirun {mpiopts} geomtools-powder-sum {powder_sum_args} if mask_fn:
powder_sum_args += f" -m {mask_fn} "
if legacy:
powder_sum_args += " --legacy "
!salloc {sopts} mpirun {mpiopts} geomtools-powder-sum {powder_sum_args} {detector_id}
``` ```
%% Cell type:code id:e9c147f0-e011-46cb-bf0f-e0ef01a6d8a4 tags: %% Cell type:code id:e9c147f0-e011-46cb-bf0f-e0ef01a6d8a4 tags:
``` python ``` python
fn = f"powder_sum_p{propno:06d}_r{runno:04d}.h5"
with h5py.File(fn, "r") as f: with h5py.File(fn, "r") as f:
# conditions # conditions
clen = f["powderSum/conditions/cameraLen"][()] clen = f["powderSum/conditions/cameraLen"][()]
photon_en = f["powderSum/conditions/photonEnergy"][()] photon_en = f["powderSum/conditions/photonEnergy"][()]
lmd = f["powderSum/conditions/waveLength"][()] lmd = f["powderSum/conditions/waveLength"][()]
detector_id = f["powderSum/conditions/detectorId"][()].decode() detector_id = f["powderSum/conditions/detectorId"][()].decode()
# average image # average image
img_mean = f["powderSum/image/mean"][:] img_mean = f["powderSum/image/mean"][:]
img_std = f["powderSum/image/std"][:] img_std = f["powderSum/image/std"][:]
img_count = f["powderSum/image/count"][:] img_count = f["powderSum/image/count"][:]
num_frames = f["powderSum/image/numFrames"][:] num_frames = f["powderSum/image/numFrames"][:]
mask = f["powderSum/image/mask"][:].astype(bool) mask = f["powderSum/image/mask"][:].astype(bool)
modules = f["powderSum/image/modules"][:] modules = f["powderSum/image/modules"][:]
print("Photon energy", photon_en, "(keV)") print("Photon energy", photon_en, "(keV)")
print("Wave length", lmd, "(nm)") print("Wave length", lmd, "(nm)")
print("Camera length", clen, "(m)") print("Camera length", clen, "(m)")
img=img_mean
msk=mask
```
%% Cell type:code id:20f6615a-f0d9-4376-b914-57d624251c2d tags:
``` python
DETECTOR = {
"agipd1m": {
"geometry": AGIPD_1MGeometry,
"borders": agipd_borders,
"motors": AGIPD1MQuadrantMotors,
"tracker": AGIPD_1MMotors
},
"jf4m": {
"geometry": JUNGFRAUGeometry,
"borders": jungfrau_borders,
"motors": JF4MMotors,
"tracker": JF4MHalfMotors
}
}
def guess_detector_type(detector_id):
for detector_type in DETECTOR.keys():
if detector_type.upper() in detector_id:
return detector_type
raise ValueError("Unknown detector type")
detector_type = guess_detector_type(detector_id)
det = DETECTOR[detector_type]
``` ```
%% Cell type:code id:8fdf43d7-db13-4493-a030-3132337e1512 tags: %% Cell type:code id:5cbcd431-5145-4bc0-92a0-6a11f598171a tags:
``` python ``` python
# geometry # geometry
run = open_run(propno, runno, data="raw") run = open_run(propno, runno, data="raw")
ref_geom = AGIPD_1MGeometry.from_crystfel_geom(ref_geom_fn) ref_geom = det["geometry"].from_crystfel_geom(ref_geom_fn)
tracker = AGIPD_1MMotors(ref_geom) tracker = det["tracker"](ref_geom)
motors = AGIPD1MQuadrantMotors(run) motors = det["motors"](run)
geom = tracker.geom_at(motors.most_frequent_positions()) geom = tracker.geom_at(motors.most_frequent_positions())
``` ```
%% Cell type:code id:332a77f6-b3b7-4e13-b6e4-5838391949df tags: %% Cell type:code id:332a77f6-b3b7-4e13-b6e4-5838391949df tags:
``` python ``` python
img = np.copy(img_mean) img = np.copy(img_mean)
geom.plot_data(img, norm="log", interpolation="none") geom.plot_data(img, norm="log", interpolation="none", vmax=1, vmin=0.01)
plt.show() plt.show()
``` ```
%% Cell type:code id:83dd3cb1-00d5-4337-80c5-e83d9eeec0b1 tags: %% Cell type:code id:83dd3cb1-00d5-4337-80c5-e83d9eeec0b1 tags:
``` python ``` python
rings = get_calibrant("AgBh") from geomtools.sfx.lattice import spacing_monoclinic
borders = agipd_borders() from geomtools.powder.misc import reciprocal_to_dectector
pw = PowderDiffraction(geom, rings, clen, lmd, h, k, l = np.mgrid[:5, :5, 1:5:2]
#rings = np.sqrt(np.unique(spacing_monoclinic(h.ravel(), k.ravel(), l.ravel(), 0.505, 0.876, 0.968, 100/180*np.pi)))[1:]
rings = get_calibrant("LaB6")
borders = det["borders"]()
pw = PowderDiffraction(geom, rings, clen, lmd, sigma=1.5, snr=1.3,
border_mask=~borders, make_shadow_mask=True) border_mask=~borders, make_shadow_mask=True)
pw.fit(img_mean, mask, num_frames) pw.fit(img, mask, num_frames)
pw.refinement_info() pw.refinement_info()
pw.draw() pw.draw()
plt.show() plt.show()
``` ```
%% Cell type:code id:bf65ce37-306a-4b19-b5af-e204e8fea59e tags:
``` python
geom.plot_data(pw.labels)
```
%% Cell type:code id:1bd08493-2562-4f92-82aa-eb866d5af791 tags: %% Cell type:code id:1bd08493-2562-4f92-82aa-eb866d5af791 tags:
``` python ``` python
pw.draw_radial_profile(img_mean, mask) pw.draw_radial_profile(img_mean, mask)
plt.show() plt.show()
``` ```
%% Cell type:code id:efbf9f46-3462-499e-9e8f-03d530d3182b tags:
``` python
geom2 = pw.transform_geometry()
geom2.motor_positions = motors.most_frequent_positions()
```
%% Cell type:code id:a9a4d447-0554-49f5-80d5-9ba5e1b11e0a tags:
``` python
geom2.metadata
```
%% Cell type:code id:75ee257e-833e-4bb9-90d4-67f1d73b8ee2 tags:
``` python
geom2.compare(geom)
```
%% Cell type:code id:fbac9184-5382-497b-b5f8-c53e04b22bf4 tags:
``` python
out_geom_fn = "/gpfs/exfel/exp/SPB/202530/p900510/usr/Shared/geom/agipd_p007955_r0001.geom"
geom2.write_crystfel_geom(out_geom_fn, **geom2.metadata)
```
%% Cell type:code id:8b590618-fbc2-45a0-9d9a-25ff88803eb6 tags:
``` python
geom2.plot_data(pw.shadow)
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment