Skip to content
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
387 changes: 266 additions & 121 deletions Pipfile.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ install_requires =
diffcalc-core
fastapi
uvicorn
pymongo
motor

[options.extras_require]
# For development tests/docs
Expand Down
9 changes: 3 additions & 6 deletions src/diffcalc_API/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from . import config, server
from . import config, database, server
from ._version_git import __version__

# __all__ defines the public API for the package.
# Each module also defines its own __all__.
__all__ = [
"__version__",
"server",
"config",
]

__all__ = ["__version__", "server", "config", "database"]
5 changes: 2 additions & 3 deletions src/diffcalc_API/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# this file is for defining constants

SAVE_PICKLES_FOLDER = "/dls/tmp/ton99817/diffcalc_pickles"
SAVE_PICKLES_FOLDER = "/"
VECTOR_PROPERTIES = ["n_hkl", "n_phi", "surf_nhkl", "surf_nphi"]
CONSTRAINTS_WITH_NO_VALUE = {"a_eq_b", "bin_eq_bout", "mu_is_gam", "bisect"}


ALL_CONSTRAINTS = {
"delta",
"gam" "qaz",
Expand Down
5 changes: 5 additions & 0 deletions src/diffcalc_API/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import motor.motor_asyncio
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorDatabase

client: AsyncIOMotorClient = motor.motor_asyncio.AsyncIOMotorClient()
database: AsyncIOMotorDatabase = client.test_db
2 changes: 1 addition & 1 deletion src/diffcalc_API/errors/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Codes(ErrorCodes):
CHECK_CONSTRAINT_EXISTS = 400


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


def check_constraint_exists(constraint: str) -> None:
Expand Down
15 changes: 6 additions & 9 deletions src/diffcalc_API/errors/definitions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Dict, Union
from enum import IntEnum
from typing import Any, Dict, List, Union

from diffcalc.util import DiffcalcException
from pydantic import BaseModel
Expand All @@ -25,14 +26,10 @@ class DiffcalcExceptionModel(BaseModel):
detail: str


class ErrorCodes:
def all_codes(self):
attributes = [
attr
for attr in dir(self)
if (not attr.startswith("__")) and (not attr == "all_codes")
]
return [getattr(self, attr) for attr in attributes]
class ErrorCodes(IntEnum):
@classmethod
def all_codes(cls) -> List[int]:
return [val.value for val in cls]


#######################################################################################
Expand Down
2 changes: 1 addition & 1 deletion src/diffcalc_API/errors/hkl.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Codes(ErrorCodes):
CALCULATE_UB_MATRIX = 400


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


def check_valid_miller_indices(miller_indices: Tuple[float, float, float]) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/diffcalc_API/errors/ub.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Codes(ErrorCodes):
CHECK_PROPERTY_IS_VALID = 400


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


def check_params_not_empty(params: SetLatticeParams) -> None:
Expand Down
52 changes: 36 additions & 16 deletions src/diffcalc_API/routes/constraints.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,72 @@
from typing import Dict, Union
from typing import Dict, Optional, Union

from fastapi import APIRouter, Body, Depends, Response
from fastapi import APIRouter, Body, Depends, Query, Response

from diffcalc_API.services import constraints as service
from diffcalc_API.stores.pickling import get_store
from diffcalc_API.stores.protocol import HklCalcStore
from diffcalc_API.stores.protocol import HklCalcStore, get_store

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


@router.get("/{name}")
async def get_constraints(name: str, store: HklCalcStore = Depends(get_store)):
content = await service.get_constraints(name, store)

async def get_constraints(
name: str,
store: HklCalcStore = Depends(get_store),
collection: Optional[str] = Query(default=None, example="B07"),
):
content = await service.get_constraints(name, store, collection)
return Response(content=content, media_type="application/text")


@router.put("/{name}/set")
@router.post("/{name}")
async def set_constraints(
name: str,
constraints: Dict[str, Union[float, bool]] = Body(
example={"qaz": 0, "alpha": 0, "eta": 0}
),
store: HklCalcStore = Depends(get_store),
collection: Optional[str] = Query(default=None, example="B07"),
):
await service.set_constraints(name, constraints, store)
await service.set_constraints(name, constraints, store, collection)

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


@router.patch("/{name}/unconstrain/{property}")
@router.delete("/{name}/{property}")
async def remove_constraint(
name: str,
property: str,
store: HklCalcStore = Depends(get_store),
collection: Optional[str] = Query(default=None, example="B07"),
):
await service.remove_constraint(name, property, store)
await service.remove_constraint(name, property, store, collection)

return {"message": f"unconstrained {property} for crystal {name}. "}
return {
"message": (
f"unconstrained {property} for crystal {name} in "
+ f"collection {collection}. "
)
}


@router.patch("/{name}/constrain/{property}")
@router.patch("/{name}/{property}")
async def set_constraint(
name: str,
property: str,
value: Union[float, bool] = Body(...),
store: HklCalcStore = Depends(get_store),
collection: Optional[str] = Query(default=None, example="B07"),
):
await service.set_constraint(name, property, value, store)
await service.set_constraint(name, property, value, store, collection)

return {"message": f"constrained {property} for crystal {name}. "}
return {
"message": (
f"constrained {property} for crystal {name} in collection "
+ f"{collection}. "
)
}
29 changes: 20 additions & 9 deletions src/diffcalc_API/routes/hkl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
from fastapi import APIRouter, Depends, Query, Response

from diffcalc_API.services import hkl as service
from diffcalc_API.stores.pickling import get_store
from diffcalc_API.stores.protocol import HklCalcStore
from diffcalc_API.stores.protocol import HklCalcStore, get_store

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


SingleConstraint = Union[Tuple[str, float], str]
Expand All @@ -19,8 +18,9 @@ async def calculate_ub(
first_tag: Optional[Union[int, str]] = Query(default=None, example="refl1"),
second_tag: Optional[Union[int, str]] = Query(default=None, example="plane"),
store: HklCalcStore = Depends(get_store),
collection: Optional[str] = Query(default=None, example="B07"),
):
content = await service.calculate_ub(name, first_tag, second_tag, store)
content = await service.calculate_ub(name, first_tag, second_tag, store, collection)
return Response(content=content, media_type="application/text")


Expand All @@ -30,9 +30,10 @@ async def lab_position_from_miller_indices(
miller_indices: Tuple[float, float, float] = Query(example=[0, 0, 1]),
wavelength: float = Query(..., example=1.0),
store: HklCalcStore = Depends(get_store),
collection: Optional[str] = Query(default=None, example="B07"),
):
positions = await service.lab_position_from_miller_indices(
name, miller_indices, wavelength, store
name, miller_indices, wavelength, store, collection
)

return {"payload": positions}
Expand All @@ -46,8 +47,11 @@ async def miller_indices_from_lab_position(
),
wavelength: float = Query(..., example=1.0),
store: HklCalcStore = Depends(get_store),
collection: Optional[str] = Query(default=None, example="B07"),
):
hkl = await service.miller_indices_from_lab_position(name, pos, wavelength, store)
hkl = await service.miller_indices_from_lab_position(
name, pos, wavelength, store, collection
)
return {"payload": hkl}


Expand All @@ -59,8 +63,11 @@ async def scan_hkl(
inc: PositionType = 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"),
):
scan_results = await service.scan_hkl(name, start, stop, inc, wavelength, store)
scan_results = await service.scan_hkl(
name, start, stop, inc, wavelength, store, collection
)
return {"payload": scan_results}


Expand All @@ -72,8 +79,11 @@ async def scan_wavelength(
inc: float = Query(..., example=0.2),
hkl: PositionType = Query(..., example=(1, 0, 1)),
store: HklCalcStore = Depends(get_store),
collection: Optional[str] = Query(default=None, example="B07"),
):
scan_results = await service.scan_wavelength(name, start, stop, inc, hkl, store)
scan_results = await service.scan_wavelength(
name, start, stop, inc, hkl, store, collection
)
return {"payload": scan_results}


Expand All @@ -87,9 +97,10 @@ async def scan_constraint(
hkl: PositionType = Query(..., example=(1, 0, 1)),
wavelength: float = Query(..., example=1.0),
store: HklCalcStore = Depends(get_store),
collection: Optional[str] = Query(default=None, example="B07"),
):
scan_results = await service.scan_constraint(
name, constraint, start, stop, inc, hkl, wavelength, store
name, constraint, start, stop, inc, hkl, wavelength, store, collection
)

return {"payload": scan_results}
Loading