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

Add spectrum_required=False to FormatXTC #674

Merged
merged 3 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions newsfragments/674.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Better handle spectra calibration for bad data in XTC format using new parameter: spectrum_required
31 changes: 26 additions & 5 deletions src/dxtbx/format/FormatXTC.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@
.help = Correction factor, needed during 2014
wavelength_offset = None
.type = float
.help = Optional constant shift to apply to each wavelength
.help = Optional constant shift to apply to each wavelength. Note, if \
spectrum_required = False and spectra calibration constants \
(spectrum_eV_per_pixel and spectrum_eV_offset) are provided, \
wavelength_offset can be used to apply a general correction \
for any events with a dropped spectrum. If the spectrum is \
present and calibration constants are provided, \
wavelength_offset is ignored.
spectrum_address = FEE-SPEC0
.type = str
.help = Address for incident beam spectrometer
Expand All @@ -82,6 +88,10 @@
spectrum_pedestal = None
.type = path
.help = Path to pickled pedestal file to subtract from the pedestal
spectrum_required = False
.type = bool
.help = Raise an exception for any event where the spectrum is not \
available.
filter {
evr_address = evr1
.type = str
Expand Down Expand Up @@ -113,7 +123,6 @@
@abstract
class FormatXTC(FormatMultiImage, FormatStill, Format):
def __init__(self, image_file, **kwargs):

if not self.understand(image_file):
raise IncorrectFormatError(self, image_file)
self.lazy = kwargs.get("lazy", True)
Expand Down Expand Up @@ -407,11 +416,11 @@
wavelength = serialtbx.detector.xtc.evt_wavelength(
evt, delta_k=self.params.wavelength_delta_k
)
if self.params.wavelength_offset is not None:
wavelength += self.params.wavelength_offset

Check warning on line 420 in src/dxtbx/format/FormatXTC.py

View check run for this annotation

Codecov / codecov/patch

src/dxtbx/format/FormatXTC.py#L420

Added line #L420 was not covered by tests
if wavelength is None:
self._beam_cache = None
else:
if self.params.wavelength_offset is not None:
wavelength += self.params.wavelength_offset
self._beam_cache = self._beam_factory.simple(wavelength)
s, nsec = evt.get(psana.EventId).time()
evttime = time.gmtime(s)
Expand All @@ -424,6 +433,14 @@
return self._beam_cache

def get_spectrum(self, index=None):
if index is None:
index = 0
spectrum = self._spectrum(index)

Check warning on line 438 in src/dxtbx/format/FormatXTC.py

View check run for this annotation

Codecov / codecov/patch

src/dxtbx/format/FormatXTC.py#L437-L438

Added lines #L437 - L438 were not covered by tests
if not spectrum and self.params.spectrum_required:
raise RuntimeError("No spectrum in shot %d" % index)
return spectrum

Check warning on line 441 in src/dxtbx/format/FormatXTC.py

View check run for this annotation

Codecov / codecov/patch

src/dxtbx/format/FormatXTC.py#L440-L441

Added lines #L440 - L441 were not covered by tests

def _spectrum(self, index=None):
if index is None:
index = 0
if self.params.spectrum_eV_per_pixel is None:
Expand Down Expand Up @@ -467,7 +484,11 @@
x = (
self.params.spectrum_eV_per_pixel * np.array(range(len(y)))
) + self.params.spectrum_eV_offset
return Spectrum(flex.double(x), flex.double(y))
try:
sp = Spectrum(flex.double(x), flex.double(y))
except RuntimeError:
return None
return sp

Check warning on line 491 in src/dxtbx/format/FormatXTC.py

View check run for this annotation

Codecov / codecov/patch

src/dxtbx/format/FormatXTC.py#L487-L491

Added lines #L487 - L491 were not covered by tests

def get_goniometer(self, index=None):
return None
Expand Down
Loading