Skip to content

Commit 42e103c

Browse files
Add unit cell validator to convert input from ispyb
1 parent b2017c8 commit 42e103c

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/ffs/service.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from datetime import datetime
1313
from pathlib import Path
1414
from typing import Iterator, Optional
15+
import pydantic
1516

1617
import gemmi
1718
import numpy as np
@@ -43,7 +44,21 @@ class PiaRequest(BaseModel):
4344
detector_distance: float
4445
d_min: float | None = None
4546
d_max: float | None = None
46-
cell: tuple[float, float, float, float, float, float] | None = None
47+
unit_cell: tuple[float, float, float, float, float, float] | None = None
48+
49+
@pydantic.validator("unit_cell", pre=True)
50+
def check_unit_cell(cls, v):
51+
if not v:
52+
return None
53+
orig_v = v
54+
if isinstance(v, str):
55+
v = v.replace(",", " ").split()
56+
v = [float(v) for v in v]
57+
try:
58+
assert len(v) == 6
59+
except Exception:
60+
raise ValueError(f"Invalid unit_cell {orig_v}")
61+
return v
4762

4863

4964
class DetectorGeometry(BaseModel):
@@ -223,9 +238,9 @@ def gpu_per_image_analysis(
223238
f"Rejecting PIA request for {parameters.dcgid}/{parameters.message_index}({parameters.dcid}): Invalid detector parameters \n{e}"
224239
)
225240

226-
if self.indexer and parameters.cell and parameters.wavelength:
241+
if self.indexer and parameters.unit_cell and parameters.wavelength:
227242
## We have all we need to index, so make up to date models.
228-
cell = gemmi.UnitCell(*parameters.cell)
243+
cell = gemmi.UnitCell(*parameters.unit_cell)
229244
self.indexer.cell = np.reshape(
230245
np.array(cell.orth.mat, dtype="float32"), (3, 3)
231246
) ## Cell as an orthogonalisation matrix

0 commit comments

Comments
 (0)