Skip to content

Commit f093cfc

Browse files
authored
added new refine_ub route (#188)
1 parent fec0fa9 commit f093cfc

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

src/diffcalc_api/models/ub.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ class AddReflectionParams(BaseModel):
5353
energy: float
5454

5555

56+
class RefineUbParams(BaseModel):
57+
"""Request body definition to refine the UB matrix."""
58+
59+
hkl: HklModel
60+
position: PositionModel
61+
wavelength: float
62+
63+
5664
class AddOrientationParams(BaseModel):
5765
"""Request body definition to add an orientation of the UB calculation."""
5866

src/diffcalc_api/routes/ub.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
HklModel,
3131
MiscutModel,
3232
PositionModel,
33+
RefineUbParams,
3334
SetLatticeParams,
3435
SphericalCoordinates,
3536
XyzModel,
@@ -597,6 +598,35 @@ async def get_u(
597598
return ArrayResponse(payload=content)
598599

599600

601+
@router.patch("/{name}/refine", response_model=InfoResponse)
602+
async def refine_ub(
603+
name: str,
604+
params: RefineUbParams = Body(...),
605+
refine_lattice: bool = Query(default=False),
606+
refine_u_matrix: bool = Query(default=False),
607+
store: HklCalcStore = Depends(get_store),
608+
collection: Optional[str] = Query(default=None, example="B07"),
609+
):
610+
"""Refine the UB matrix to match the diffractometer position to a given reflection.
611+
612+
Args:
613+
name: the name of the hkl object to access within the store
614+
params: the parameters to use to refine the UB matrix
615+
store: accessor to the hkl object
616+
collection: collection within which the hkl object resides
617+
618+
"""
619+
await service.refine_ub(
620+
name, params, refine_lattice, refine_u_matrix, store, collection
621+
)
622+
return InfoResponse(
623+
message=(
624+
f"lattice has been set for UB calculation of crystal {name} in "
625+
+ f"collection {collection}"
626+
)
627+
)
628+
629+
600630
#######################################################################################
601631
# Surface and Reference Vectors #
602632
#######################################################################################

src/diffcalc_api/services/ub.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
EditReflectionParams,
2121
HklModel,
2222
PositionModel,
23+
RefineUbParams,
2324
SetLatticeParams,
2425
XyzModel,
2526
)
@@ -614,6 +615,38 @@ async def set_ub(
614615
await store.save(name, hklcalc, collection)
615616

616617

618+
async def refine_ub(
619+
name: str,
620+
params: RefineUbParams,
621+
refine_lat: bool,
622+
refine_u: bool,
623+
store: HklCalcStore,
624+
collection: Optional[str],
625+
) -> None:
626+
"""Refine the UB matrix to match the reflection given in the parameters.
627+
628+
Args:
629+
name: the name of the hkl object to access within the store
630+
params: parameters for refining the UB matrix.
631+
store: accessor to the hkl object
632+
collection: collection within which the hkl object resides
633+
"""
634+
hklcalc = await store.load(name, collection)
635+
636+
ubcalc: UBCalculation = hklcalc.ubcalc
637+
hkl: Tuple[float, float, float] = params.hkl.h, params.hkl.k, params.hkl.l
638+
639+
ubcalc.refine_ub(
640+
hkl,
641+
Position(**params.position.dict()),
642+
params.wavelength,
643+
refine_lattice=refine_lat,
644+
refine_umatrix=refine_u,
645+
)
646+
647+
await store.save(name, hklcalc, collection)
648+
649+
617650
#######################################################################################
618651
# Surface and Reference Vectors #
619652
#######################################################################################

tests/test_ubcalc.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import ast
22
import math
33
from ast import literal_eval
4+
from math import radians
45
from typing import Dict
56

67
import numpy as np
@@ -573,6 +574,43 @@ def test_set_ub():
573574
)
574575

575576

577+
def test_refine_ub():
578+
ubcalc = UBCalculation()
579+
hkl = HklCalculation(ubcalc, Constraints())
580+
client = Client(hkl).client
581+
582+
ubcalc.set_lattice("LSMO_327", 3.78, 3.78, 20.1, 90, 90, 90)
583+
ubcalc.add_orientation([0, 0, 1], [1, 0, 0])
584+
ubcalc.add_orientation([0, 1, 0], [0, 1, 0])
585+
586+
ubcalc.calc_ub()
587+
588+
ubcalc.n_phi = [0.0, 1.0, 0.0]
589+
590+
ubcalc.set_miscut([1.0, 0.0, 0.0], radians(3))
591+
592+
ub_after_miscut = ubcalc.UB
593+
594+
client.patch(
595+
"/ub/test/refine?collection=B07",
596+
json={
597+
"hkl": {"h": 0, "k": 0, "l": 2},
598+
"position": {
599+
"mu": 100.0,
600+
"delta": 0.0,
601+
"nu": 82.9408,
602+
"eta": 0.0,
603+
"chi": 30.31,
604+
"phi": 0.0,
605+
},
606+
"wavelength": 13.3109,
607+
},
608+
params={"refine_lattice": 1, "refine_u_matrix": 1},
609+
)
610+
611+
assert np.any(ubcalc.UB != ub_after_miscut)
612+
613+
576614
@pytest.mark.parametrize(
577615
["url", "body", "property"],
578616
[

0 commit comments

Comments
 (0)