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
Binary file removed dump.rdb
Binary file not shown.
3 changes: 1 addition & 2 deletions src/diffcalc_API/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from . import config, errorDefinitions, fileHandling, server
from . import config, errorDefinitions, server
from ._version_git import __version__

# __all__ defines the public API for the package.
# Each module also defines its own __all__.
__all__ = [
"__version__",
"server",
"fileHandling",
"errorDefinitions",
"config",
]
51 changes: 0 additions & 51 deletions src/diffcalc_API/fileHandling.py

This file was deleted.

45 changes: 16 additions & 29 deletions src/diffcalc_API/routes/Constraints.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
from pathlib import Path
from typing import Callable, Dict, Union
from typing import Dict, Union

from diffcalc.hkl.calc import HklCalculation
from fastapi import APIRouter, Body, Depends, Response

from diffcalc_API.fileHandling import supplyPersist, unpickleHkl
from diffcalc_API.services import Constraints as service
from diffcalc_API.stores.pickling import get_store
from diffcalc_API.stores.protocol import HklCalcStore

router = APIRouter(prefix="/constraints", tags=["constraints"])


@router.get("/{name}")
async def get_constraints_status(
name: str, hklCalc: HklCalculation = Depends(unpickleHkl)
):
return Response(content=str(hklCalc.constraints), media_type="application/text")
async def get_constraints(name: str, store: HklCalcStore = Depends(get_store)):
content = await service.get_constraints(name, store)

return Response(content=content, media_type="application/text")


@router.put("/{name}/set")
Expand All @@ -23,43 +22,31 @@ async def set_constraints(
constraintDict: Dict[str, Union[float, bool]] = Body(
example={"qaz": 0, "alpha": 0, "eta": 0}
),
hklCalc: HklCalculation = Depends(unpickleHkl),
persist: Callable[[HklCalculation, str], Path] = Depends(supplyPersist),
store: HklCalcStore = Depends(get_store),
):
service.set_constraints(name, constraintDict, hklCalc, persist)
await service.set_constraints(name, constraintDict, store)

return {"message": f"constraints updated (replaced) for crystal {name}"}


@router.patch("/{name}/unconstrain/{property}")
async def remove_constraint(
name: str,
property: str,
hklCalc: HklCalculation = Depends(unpickleHkl),
persist: Callable[[HklCalculation, str], Path] = Depends(supplyPersist),
store: HklCalcStore = Depends(get_store),
):
service.remove_constraint(name, property, hklCalc, persist)
await service.remove_constraint(name, property, store)

return {
"message": (
f"unconstrained {property} for crystal {name}. "
f"Constraints are now: {hklCalc.constraints.asdict}"
)
}
return {"message": f"unconstrained {property} for crystal {name}. "}


@router.patch("/{name}/constrain/{property}")
async def set_constraint(
name: str,
property: str,
value: Union[float, bool] = Body(...),
hklCalc: HklCalculation = Depends(unpickleHkl),
persist: Callable[[HklCalculation, str], Path] = Depends(supplyPersist),
store: HklCalcStore = Depends(get_store),
):
service.set_constraint(name, property, value, hklCalc, persist)
await service.set_constraint(name, property, value, store)

return {
"message": (
f"constrained {property} for crystal {name}. "
f"Constraints are now: {hklCalc.constraints.asdict}"
)
}
return {"message": f"constrained {property} for crystal {name}. "}
45 changes: 19 additions & 26 deletions src/diffcalc_API/routes/HklCalculation.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
from pathlib import Path
from typing import Callable, Optional, Tuple, Union
from typing import Optional, Tuple, Union

import numpy as np
from diffcalc.hkl.calc import HklCalculation
from fastapi import APIRouter, Depends, Query, Response

from diffcalc_API.fileHandling import supplyPersist, unpickleHkl
from diffcalc_API.services import HklCalculation as service
from diffcalc_API.stores.pickling import get_store
from diffcalc_API.stores.protocol import HklCalcStore

router = APIRouter(
prefix="/calculate", tags=["hkl"], dependencies=[Depends(unpickleHkl)]
)
router = APIRouter(prefix="/calculate", tags=["hkl"])


singleConstraintType = Union[Tuple[str, float], str]
Expand All @@ -22,24 +18,21 @@ async def calculate_UB(
name: str,
firstTag: Optional[Union[int, str]] = Query(default=None, example="refl1"),
secondTag: Optional[Union[int, str]] = Query(default=None, example="plane"),
hklCalc: HklCalculation = Depends(unpickleHkl),
persist: Callable[[HklCalculation, str], Path] = Depends(supplyPersist),
store: HklCalcStore = Depends(get_store),
):
service.calculate_UB(name, firstTag, secondTag, hklCalc, persist)
return Response(
content=str(np.round(hklCalc.ubcalc.UB, 6)), media_type="application/text"
)
content = await service.calculate_UB(name, firstTag, secondTag, store)
return Response(content=content, media_type="application/text")


@router.get("/{name}/position/lab")
async def lab_position_from_miller_indices(
name: str,
millerIndices: Tuple[float, float, float] = Query(example=[0, 0, 1]),
wavelength: float = Query(..., example=1.0),
hklCalc: HklCalculation = Depends(unpickleHkl),
store: HklCalcStore = Depends(get_store),
):
positions = service.lab_position_from_miller_indices(
millerIndices, wavelength, hklCalc
positions = await service.lab_position_from_miller_indices(
name, millerIndices, wavelength, store
)

return {"payload": positions}
Expand All @@ -52,9 +45,9 @@ async def miller_indices_from_lab_position(
..., example=[7.31, 0, 10.62, 0, 0, 0]
),
wavelength: float = Query(..., example=1.0),
hklCalc: HklCalculation = Depends(unpickleHkl),
store: HklCalcStore = Depends(get_store),
):
hkl = service.miller_indices_from_lab_position(pos, wavelength, hklCalc)
hkl = await service.miller_indices_from_lab_position(name, pos, wavelength, store)
return {"payload": hkl}


Expand All @@ -65,9 +58,9 @@ async def scan_hkl(
stop: positionType = Query(..., example=(2, 0, 2)),
inc: positionType = Query(..., example=(0.1, 0, 0.1)),
wavelength: float = Query(..., example=1),
hklCalc: HklCalculation = Depends(unpickleHkl),
store: HklCalcStore = Depends(get_store),
):
scanResults = service.scan_hkl(start, stop, inc, wavelength, hklCalc)
scanResults = await service.scan_hkl(name, start, stop, inc, wavelength, store)
return {"payload": scanResults}


Expand All @@ -78,9 +71,9 @@ async def scan_wavelength(
stop: float = Query(..., example=2.0),
inc: float = Query(..., example=0.2),
hkl: positionType = Query(..., example=(1, 0, 1)),
hklCalc: HklCalculation = Depends(unpickleHkl),
store: HklCalcStore = Depends(get_store),
):
scanResults = service.scan_wavelength(start, stop, inc, hkl, hklCalc)
scanResults = await service.scan_wavelength(name, start, stop, inc, hkl, store)
return {"payload": scanResults}


Expand All @@ -93,10 +86,10 @@ async def scan_constraint(
inc: float = Query(..., example=1),
hkl: positionType = Query(..., example=(1, 0, 1)),
wavelength: float = Query(..., example=1.0),
hklCalc: HklCalculation = Depends(unpickleHkl),
store: HklCalcStore = Depends(get_store),
):
scanResults = service.scan_constraint(
constraint, start, stop, inc, hkl, wavelength, hklCalc
scanResults = await service.scan_constraint(
name, constraint, start, stop, inc, hkl, wavelength, store
)

return {"payload": scanResults}
Loading