Skip to content
This repository was archived by the owner on May 7, 2026. It is now read-only.
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
25 changes: 23 additions & 2 deletions src/diffcalc_API/errors/ub.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Union
from typing import Optional, Union

import numpy as np

Expand All @@ -14,19 +14,40 @@ class ErrorCodes(ErrorCodesBase):
INVALID_SET_LATTICE_PARAMS = 400
REFERENCE_RETRIEVAL_ERROR = 403
INVALID_PROPERTY = 400
NO_TAG_OR_IDX_PROVIDED = 400
BOTH_TAG_OR_IDX_PROVIDED = 400


responses = {code: ALL_RESPONSES[code] for code in np.unique(ErrorCodes.all_codes())}


class NoTagOrIdxProvidedError(DiffcalcAPIException):
def __init__(self):
self.detail = (
"One of the following must be provided as a query parameter:"
+ " tag (string), index (integer)"
)
self.status_code = ErrorCodes.NO_TAG_OR_IDX_PROVIDED


class BothTagAndIdxProvidedError(DiffcalcAPIException):
def __init__(self):
self.detail = (
"both the tag and index have been provided. These are identifiers"
+ " for a specific orientation or reflection, and so both cannot be"
+ " used. Retry with just one tag or index query parameter."
)
self.status_code = ErrorCodes.BOTH_TAG_OR_IDX_PROVIDED


class InvalidSetLatticeParamsError(DiffcalcAPIException):
def __init__(self):
self.detail = ("please provide lattice parameters in request body",)
self.status_code = ErrorCodes.INVALID_SET_LATTICE_PARAMS


class ReferenceRetrievalError(DiffcalcAPIException):
def __init__(self, handle: Union[str, int], reference_type: str) -> None:
def __init__(self, handle: Optional[Union[str, int]], reference_type: str) -> None:
self.detail = f"cannot retrieve {reference_type} with tag or index {handle}"
self.status_code = ErrorCodes.REFERENCE_RETRIEVAL_ERROR

Expand Down
5 changes: 1 addition & 4 deletions src/diffcalc_API/examples/ub.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,23 @@
"hkl": HklModel(h=0, k=0, l=1),
"position": PositionModel(mu=7.31, delta=0.0, nu=10.62, eta=0, chi=0.0, phi=0),
"energy": 12.39842,
"tag": "refl1",
}
)

edit_reflection: EditReflectionParams = EditReflectionParams(
**{"energy": 12.45, "tag_or_idx": "refl1"}
**{"energy": 12.45, "set_tag": "refl2"}
)

add_orientation: AddOrientationParams = AddOrientationParams(
**{
"hkl": HklModel(h=0, k=1, l=0),
"xyz": XyzModel(x=0, y=1, z=0),
"tag": "plane",
}
)

edit_orientation: EditOrientationParams = EditOrientationParams(
**{
"hkl": HklModel(h=0, k=1, l=0),
"tag_or_idx": "plane",
}
)

Expand Down
14 changes: 6 additions & 8 deletions src/diffcalc_API/models/ub.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Union
from typing import Optional

from pydantic import BaseModel

Expand All @@ -25,7 +25,7 @@ class PositionModel(BaseModel):


class SetLatticeParams(BaseModel):
system: Optional[Union[str, float]] = None
system: Optional[str] = None
a: Optional[float] = None
b: Optional[float] = None
c: Optional[float] = None
Expand All @@ -38,29 +38,27 @@ class AddReflectionParams(BaseModel):
hkl: HklModel
position: PositionModel
energy: float
tag: Optional[str] = None


class AddOrientationParams(BaseModel):
hkl: HklModel
xyz: XyzModel
position: Optional[PositionModel] = None
tag: Optional[str] = None


class EditReflectionParams(BaseModel):
hkl: Optional[HklModel] = None
position: Optional[PositionModel] = None
energy: Optional[float] = None
tag_or_idx: Union[int, str]
set_tag: Optional[str] = None


class EditOrientationParams(BaseModel):
hkl: Optional[HklModel] = None
xyz: Optional[XyzModel] = None
position: Optional[PositionModel] = None
tag_or_idx: Union[int, str]
set_tag: Optional[str] = None


class DeleteParams(BaseModel):
tag_or_idx: Union[int, str]
def select_idx_or_tag_str(idx: Optional[int], tag: Optional[str]):
return f"index {idx}" if idx is not None else f"tag {tag}"
8 changes: 3 additions & 5 deletions src/diffcalc_API/routes/constraints.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Dict, Optional, Union
from typing import Dict, Optional

from fastapi import APIRouter, Body, Depends, Query

Expand All @@ -21,9 +21,7 @@ async def get_constraints(
@router.post("/{name}")
async def set_constraints(
name: str,
constraints: Dict[str, Union[float, bool]] = Body(
example={"qaz": 0, "alpha": 0, "eta": 0}
),
constraints: Dict[str, float] = Body(example={"qaz": 0, "alpha": 0, "eta": 0}),
store: HklCalcStore = Depends(get_store),
collection: Optional[str] = Query(default=None, example="B07"),
):
Expand Down Expand Up @@ -58,7 +56,7 @@ async def remove_constraint(
async def set_constraint(
name: str,
property: str,
value: Union[float, bool] = Body(...),
value: float = Body(...),
store: HklCalcStore = Depends(get_store),
collection: Optional[str] = Query(default=None, example="B07"),
):
Expand Down
22 changes: 10 additions & 12 deletions src/diffcalc_API/routes/hkl.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Optional, Union
from typing import List, Optional

from fastapi import APIRouter, Depends, Query

Expand All @@ -12,19 +12,22 @@
@router.get("/{name}/UB")
async def calculate_ub(
name: str,
first_tag: Optional[Union[int, str]] = Query(default=None, example="refl1"),
second_tag: Optional[Union[int, str]] = Query(default=None, example="plane"),
tag1: Optional[str] = Query(default=None, example="refl1"),
idx1: Optional[int] = Query(default=None),
tag2: Optional[str] = Query(default=None, example="plane"),
idx2: Optional[int] = Query(default=None),
store: HklCalcStore = Depends(get_store),
collection: Optional[str] = Query(default=None, example="B07"),
):
content = await service.calculate_ub(name, first_tag, second_tag, store, collection)
content = await service.calculate_ub(
name, store, collection, tag1, idx1, tag2, idx2
)
return {"payload": content}


@router.get("/{name}/position/lab")
async def lab_position_from_miller_indices(
name: str,
# miller_indices: List[float] = Query(example=[0, 0, 1]),
miller_indices: HklModel = Depends(),
wavelength: float = Query(..., example=1.0),
store: HklCalcStore = Depends(get_store),
Expand All @@ -40,10 +43,7 @@ async def lab_position_from_miller_indices(
@router.get("/{name}/position/hkl")
async def miller_indices_from_lab_position(
name: str,
pos: PositionModel = Depends(
# ..., example={"mu": 7.31, "delta": 0, "nu": 10.62,
# "eta": 0, "chi": 0, "phi": 0}
),
pos: PositionModel = Depends(),
wavelength: float = Query(..., example=1.0),
store: HklCalcStore = Depends(get_store),
collection: Optional[str] = Query(default=None, example="B07"),
Expand All @@ -59,7 +59,7 @@ async def scan_hkl(
name: str,
start: List[float] = Query(..., example=[1, 0, 1]),
stop: List[float] = Query(..., example=[2, 0, 2]),
inc: List[float] = Query(..., example=(0.1, 0, 0.1)),
inc: List[float] = Query(..., example=[0.1, 0, 0.1]),
wavelength: float = Query(..., example=1),
store: HklCalcStore = Depends(get_store),
collection: Optional[str] = Query(default=None, example="B07"),
Expand All @@ -76,7 +76,6 @@ async def scan_wavelength(
start: float = Query(..., example=1.0),
stop: float = Query(..., example=2.0),
inc: float = Query(..., example=0.2),
# hkl: PositionType = Query(..., example=(1, 0, 1)),
hkl: HklModel = Depends(),
store: HklCalcStore = Depends(get_store),
collection: Optional[str] = Query(default=None, example="B07"),
Expand All @@ -94,7 +93,6 @@ async def scan_constraint(
start: float = Query(..., example=1),
stop: float = Query(..., example=4),
inc: float = Query(..., example=1),
# hkl: PositionType = Query(..., example=(1, 0, 1)),
hkl: HklModel = Depends(),
wavelength: float = Query(..., example=1.0),
store: HklCalcStore = Depends(get_store),
Expand Down
Loading