Skip to content

Commit

Permalink
Responding to PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Cbameron12 committed Jan 28, 2025
1 parent ba6fea1 commit 9989815
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 37 deletions.
45 changes: 23 additions & 22 deletions api/endpoints/singlepoint_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

import logging
from pathlib import Path
from typing import Annotated, Any
from typing import Any

from fastapi import APIRouter, HTTPException, Query
from fastapi import APIRouter, HTTPException
from janus_core.helpers.janus_types import Architectures, Properties
from pydantic import BaseModel

from api.utils.singlepoint_helper import singlepoint

Expand All @@ -15,30 +17,28 @@
router = APIRouter(prefix="/singlepoint", tags=["calculations"])


@router.get("/")
def get_singlepoint(
struct: Annotated[str, Query()],
arch: Annotated[str | None, Query()] = "mace_mp",
properties: Annotated[list[str] | None, Query()] = None,
range_selector: Annotated[str | None, Query()] = ":",
) -> dict[str, Any]:
class SinglePointRequest(BaseModel):
"""Class validation for singlepoint requests."""

struct: str
arch: Architectures
properties: list[Properties]
range_selector: str


@router.post("/")
def get_singlepoint(request: SinglePointRequest) -> dict[str, Any]:
"""
Endpoint to perform single point calculations and return results.
Parameters
----------
struct : str
The filename of the dataset to perform calculations on.
arch : str
The name of the model to perform the calculations with.
properties : List[str]
The properties to calculate.
range_selector : str
The range of indicies to read from the data structure.
request : SinglePointRequest
The request body containing the parameters for the calculation.
Returns
-------
Dict[str, Any]
dict[str, Any]
Results of the single point calculations.
Raises
Expand All @@ -47,13 +47,14 @@ def get_singlepoint(
If there is an error during the call.
"""
base_dir = Path("data")
struct_path = base_dir / struct
struct_path = base_dir / request.struct
logger.info(request)
try:
results = singlepoint(
struct=struct_path,
arch=arch,
properties=properties,
range_selector=range_selector,
arch=request.arch,
properties=request.properties,
range_selector=request.range_selector,
)
if "error" in results:
raise HTTPException(status_code=500, detail=results["error"])
Expand Down
21 changes: 6 additions & 15 deletions api/utils/singlepoint_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import Any

from janus_core.calculations.single_point import SinglePoint
from janus_core.helpers.janus_types import Architectures, Properties
import numpy as np

logger = logging.getLogger(__name__)
Expand All @@ -26,6 +27,7 @@ def convert_ndarray_to_list(obj) -> dict:
dict
Dictionary of properties calculated.
"""
print(obj)
if isinstance(obj, np.ndarray):
return obj.tolist()
if isinstance(obj, dict):
Expand All @@ -37,8 +39,8 @@ def convert_ndarray_to_list(obj) -> dict:

def singlepoint(
struct: Path,
arch: str | None = "mace_mp",
properties: list[str] | None = None,
arch: Architectures | None = "mace_mp",
properties: list[Properties] | None = None,
range_selector: str | None = ":",
) -> dict[str, Any]:
"""
Expand All @@ -57,12 +59,10 @@ def singlepoint(
Returns
-------
Dict[str, Any]
dict[str, Any]
Results of the single point calculations.
"""
read_kwargs = {"index": range_selector}
if properties == "all properties":
properties = None

singlepoint_kwargs = {
"struct_path": struct,
Expand All @@ -71,19 +71,10 @@ def singlepoint(
"device": "cpu",
"read_kwargs": read_kwargs,
}
logger.info(singlepoint_kwargs)

s_point = SinglePoint(**singlepoint_kwargs)

s_point.run()
print(s_point.results)

return convert_ndarray_to_list(s_point.results)


if __name__ == "__main__":
results = singlepoint(
struct=Path("/home/ubuntu/janus-api/janus-web/data/input.data.2055.xyz"),
range_selector="0:2",
)
print(results)
print(type(results))

0 comments on commit 9989815

Please sign in to comment.