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

Compatibility with cfelpyutils 2.x & read/write bad regions from .geom file #114

Merged
merged 7 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion .github/dependabot/constraints.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cfelpyutils==1.0.1
cfelpyutils==2.0.4
cycler==0.10.0
future==0.18.2
h5py<3.5.0
Expand Down
18 changes: 10 additions & 8 deletions extra_geom/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from itertools import chain

import numpy as np
from cfelpyutils.crystfel_utils import load_crystfel_geometry
from cfelpyutils.geometry import load_crystfel_geometry

from .crystfel_fmt import write_crystfel_geom
from .snapped import GridGeometryFragment, SnappedGeometry
Expand Down Expand Up @@ -31,8 +31,8 @@ def from_panel_dict(cls, d):
corner_pos = np.array([d['cnx']/res, d['cny']/res, d['coffset']])
ss_vec = np.array([d['ssx'], d['ssy'], d['ssz']]) / res
fs_vec = np.array([d['fsx'], d['fsy'], d['fsz']]) / res
ss_pixels = d['max_ss'] - d['min_ss'] + 1
fs_pixels = d['max_fs'] - d['min_fs'] + 1
ss_pixels = d['orig_max_ss'] - d['orig_min_ss'] + 1
fs_pixels = d['orig_max_fs'] - d['orig_min_fs'] + 1
Copy link
Member Author

Choose a reason for hiding this comment

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

These fields appear to have been renamed in cfelpyutils. I couldn't work out why by looking at the code, and I didn't get any answer from CFEL when I asked. 🤷

return cls(corner_pos, ss_vec, fs_vec, ss_pixels, fs_pixels)

def corners(self):
Expand Down Expand Up @@ -244,15 +244,15 @@ def _cfel_panels_by_data_coord(cls, panels: dict):
if len(ix_dims) > 1:
raise ValueError(f"Too many index dimensions for {pname}: {dims}")

min_ss = info['min_ss']
min_ss = info['orig_min_ss']
if ix_dims:
# Geometry for 3D data, modules stacked along separate axis
modno = ix_dims[0]
else:
# Geometry for 2D data, modules concatenated along slow-scan axis
modno, min_ss = divmod(min_ss, cls.expected_data_shape[1])

res[(modno, min_ss, info['min_fs'])] = info
res[(modno, min_ss, info['orig_min_fs'])] = info

return res

Expand All @@ -262,8 +262,10 @@ def from_crystfel_geom(cls, filename):

Returns a new geometry object.
"""
geom_dict = load_crystfel_geometry(filename)
panels_by_data_coord = cls._cfel_panels_by_data_coord(geom_dict['panels'])
cfel_geom = load_crystfel_geometry(filename)
panels_by_data_coord = cls._cfel_panels_by_data_coord(
cfel_geom.detector['panels']
)
n_modules = cls.n_modules
if n_modules == 0:
# Detector type with varying number of modules (e.g. JUNGFRAU)
Expand All @@ -285,7 +287,7 @@ def from_crystfel_geom(cls, filename):
cfel_md_keys = ('data', 'mask', 'adu_per_eV', 'clen')
d1 = panels_by_data_coord[0, 0, 0]
metadata = {'crystfel': {k: d1.get(k) for k in cfel_md_keys}}
# TODO: photon_energy (not returned with cfelpyutils 1.0)
metadata['crystfel']['photon_energy'] = cfel_geom.beam['photon_energy']

return cls(modules, filename=filename, metadata=metadata)

Expand Down
22 changes: 11 additions & 11 deletions extra_geom/tests/test_agipd500k2g_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
import pytest
import xarray as xr
from cfelpyutils.crystfel_utils import load_crystfel_geometry
from cfelpyutils.geometry import load_crystfel_geometry
from extra_data.stacking import stack_detector_data
from matplotlib.axes import Axes

Expand Down Expand Up @@ -80,7 +80,7 @@ def test_write_read_crystfel_file(tmpdir):
np.testing.assert_allclose(loaded.modules[0][0].fs_vec, geom.modules[0][0].fs_vec)

# Load the geometry file with cfelpyutils and test the rigid groups
geom_dict = load_crystfel_geometry(path)
geom_dict = load_crystfel_geometry(path).detector
quad_gr0 = [ # quadrant: p0a0 ... p7a7
'p{}a{}'.format(p, a) for p, a in product(range(8), range(8))
]
Expand All @@ -89,10 +89,10 @@ def test_write_read_crystfel_file(tmpdir):
assert geom_dict['rigid_groups']['q0'] == quad_gr0
assert geom_dict['panels']['p0a0']['res'] == 5000 # 5000 pixels/metre
p3a7 = geom_dict['panels']['p3a7']
assert p3a7['min_ss'] == 448
assert p3a7['max_ss'] == 511
assert p3a7['min_fs'] == 0
assert p3a7['max_fs'] == 127
assert p3a7['orig_min_ss'] == 448
assert p3a7['orig_max_ss'] == 511
assert p3a7['orig_min_fs'] == 0
assert p3a7['orig_max_fs'] == 127


def test_write_read_crystfel_file_2d(tmpdir):
Expand All @@ -108,14 +108,14 @@ def test_write_read_crystfel_file_2d(tmpdir):
np.testing.assert_allclose(loaded.modules[0][0].fs_vec, geom.modules[0][0].fs_vec)

# Load the geometry file with cfelpyutils and check some values
geom_dict = load_crystfel_geometry(path)
geom_dict = load_crystfel_geometry(path).detector

p3a7 = geom_dict['panels']['p3a7']
assert p3a7['dim_structure'] == ['%', 'ss', 'fs']
assert p3a7['min_ss'] == (3 * 512) + 448
assert p3a7['max_ss'] == (3 * 512) + 511
assert p3a7['min_fs'] == 0
assert p3a7['max_fs'] == 127
assert p3a7['orig_min_ss'] == (3 * 512) + 448
assert p3a7['orig_max_ss'] == (3 * 512) + 511
assert p3a7['orig_min_fs'] == 0
assert p3a7['orig_max_fs'] == 127


def test_inspect():
Expand Down
22 changes: 11 additions & 11 deletions extra_geom/tests/test_agipd_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import numpy as np
import pytest
from cfelpyutils.crystfel_utils import load_crystfel_geometry
from cfelpyutils.geometry import load_crystfel_geometry
from matplotlib.axes import Axes

from extra_geom import AGIPD_1MGeometry, agipd_asic_seams
Expand Down Expand Up @@ -82,7 +82,7 @@ def test_write_read_crystfel_file(tmpdir):
assert_geom_close(loaded, geom)

# Load the geometry file with cfelpyutils and test the rigid groups
geom_dict = load_crystfel_geometry(path)
geom_dict = load_crystfel_geometry(path).detector
quad_gr0 = [ # 1st quadrant: p0a0 ... p3a7
'p{}a{}'.format(p, a) for p, a in product(range(4), range(8))
]
Expand All @@ -91,10 +91,10 @@ def test_write_read_crystfel_file(tmpdir):
assert geom_dict['rigid_groups']['q0'] == quad_gr0
assert geom_dict['panels']['p0a0']['res'] == 5000 # 5000 pixels/metre
p3a7 = geom_dict['panels']['p3a7']
assert p3a7['min_ss'] == 448
assert p3a7['max_ss'] == 511
assert p3a7['min_fs'] == 0
assert p3a7['max_fs'] == 127
assert p3a7['orig_min_ss'] == 448
assert p3a7['orig_max_ss'] == 511
assert p3a7['orig_min_fs'] == 0
assert p3a7['orig_max_fs'] == 127


def test_write_read_crystfel_file_2d(tmpdir):
Expand All @@ -112,14 +112,14 @@ def test_write_read_crystfel_file_2d(tmpdir):
np.testing.assert_allclose(loaded.modules[0][0].fs_vec, geom.modules[0][0].fs_vec)

# Load the geometry file with cfelpyutils and check some values
geom_dict = load_crystfel_geometry(path)
geom_dict = load_crystfel_geometry(path).detector

p3a7 = geom_dict['panels']['p3a7']
assert p3a7['dim_structure'] == ['%', 'ss', 'fs']
assert p3a7['min_ss'] == (3 * 512) + 448
assert p3a7['max_ss'] == (3 * 512) + 511
assert p3a7['min_fs'] == 0
assert p3a7['max_fs'] == 127
assert p3a7['orig_min_ss'] == (3 * 512) + 448
assert p3a7['orig_max_ss'] == (3 * 512) + 511
assert p3a7['orig_min_fs'] == 0
assert p3a7['orig_max_fs'] == 127


def test_quad_positions():
Expand Down
12 changes: 6 additions & 6 deletions extra_geom/tests/test_epix_geometry.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import matplotlib.pyplot as plt
import numpy as np
import pytest
from cfelpyutils.crystfel_utils import load_crystfel_geometry
from cfelpyutils.geometry import load_crystfel_geometry

import extra_geom
from extra_geom import Epix10KGeometry, Epix100Geometry
Expand Down Expand Up @@ -132,14 +132,14 @@ def test_write_read_crystfel_file(args, tmpdir):
assert_geom_close(loaded, epix)

# Load the geometry file with cfelpyutils and test the rigid groups
geom_dict = load_crystfel_geometry(path)
geom_dict = load_crystfel_geometry(path).detector
assert geom_dict['panels']['p0a0']['res'] == 1 / pxsz
assert len(geom_dict['panels']) == 4
p0a0 = geom_dict['panels']['p0a0']
assert p0a0['max_ss'] == nrow - 1
assert p0a0['min_ss'] == 0
assert p0a0['max_fs'] == ncol - 1
assert p0a0['min_fs'] == 0
assert p0a0['orig_max_ss'] == nrow - 1
assert p0a0['orig_min_ss'] == 0
assert p0a0['orig_max_fs'] == ncol - 1
assert p0a0['orig_min_fs'] == 0


@pytest.mark.parametrize('args', ['epix100'], indirect=True)
Expand Down
13 changes: 7 additions & 6 deletions extra_geom/tests/test_jungfrau_geometry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import numpy as np
from cfelpyutils.crystfel_utils import load_crystfel_geometry
from cfelpyutils.geometry import load_crystfel_geometry

from extra_geom import JUNGFRAUGeometry

Expand Down Expand Up @@ -33,15 +33,16 @@ def test_write_read_crystfel_file(tmpdir):
assert_geom_close(loaded, geom)
assert loaded.metadata['crystfel']['adu_per_eV'] == 0.0042
assert loaded.metadata['crystfel']['clen'] == 0.101
assert loaded.metadata['crystfel']['photon_energy'] == 9000

# Load the geometry file with cfelpyutils and test the rigid groups
geom_dict = load_crystfel_geometry(path)
geom_dict = load_crystfel_geometry(path).detector
assert geom_dict['panels']['p0a0']['res'] == 1 / 75e-6
p3a7 = geom_dict['panels']['p3a7']
assert p3a7['min_ss'] == 256
assert p3a7['max_ss'] == 511
assert p3a7['min_fs'] == 768
assert p3a7['max_fs'] == 1023
assert p3a7['orig_min_ss'] == 256
assert p3a7['orig_max_ss'] == 511
assert p3a7['orig_min_fs'] == 768
assert p3a7['orig_max_fs'] == 1023

# Check that metadata is written back to .geom file OK
path2 = str(tmpdir / 'test2.geom')
Expand Down
4 changes: 2 additions & 2 deletions extra_geom/tests/test_lpd_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import h5py
import numpy as np
import pytest
from cfelpyutils.crystfel_utils import load_crystfel_geometry
from cfelpyutils.geometry import load_crystfel_geometry
from matplotlib.axes import Axes
from testpath import assert_isfile

Expand Down Expand Up @@ -38,7 +38,7 @@ def test_write_read_crystfel_file(tmpdir):
np.testing.assert_allclose(loaded.modules[0][0].fs_vec, geom.modules[0][0].fs_vec)


geom_dict = load_crystfel_geometry(path)
geom_dict = load_crystfel_geometry(path).detector
quad_gr0 = ['p0a0', 'p0a1', 'p0a2', 'p0a3', 'p0a4', 'p0a5', 'p0a6', 'p0a7',
'p0a8', 'p0a9', 'p0a10','p0a11', 'p0a12', 'p0a13', 'p0a14',
'p0a15', 'p1a0', 'p1a1','p1a2', 'p1a3','p1a4','p1a5','p1a6',
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def find_version(*parts):
'extra_geom.tests': ['dssc_geo_june19.h5', 'lpd_mar_18.h5'],
},
install_requires=[
'cfelpyutils>=0.92, <2.0',
'cfelpyutils>=2.0, <3.0',
'h5py>=2.7.1',
'matplotlib',
'numpy',
Expand Down