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
43 changes: 42 additions & 1 deletion src/diffpy/apps/refinebase/refinement_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,39 @@ async def set_model_equation(
async def get_model_evaluation(
model_name: Annotated[str, "Name of the parametric model"],
data_path: Annotated[str, "Path to the data to retrieve"],
xmin: Annotated[
float | None,
(
"Minimum x value for the evaluation "
"if the profile is not set for the model"
),
] = None,
xmax: Annotated[
float | None,
(
"Maximum x value for the evaluation "
"if the profile is not set for the model"
),
] = None,
dx: Annotated[
float | None,
(
"Step size for the evaluation "
"if the profile is not set for the model"
),
] = None,
xname: Annotated[
str,
(
"Name of the x variable for the evaluation "
"if the profile is not set for the model"
),
] = "r",
) -> str:
"""Get the current evaluation of a parametric model."""
evaluation = session.get_model_evaluation(model_name=model_name).tolist()
evaluation = session.get_model_evaluation(
model_name=model_name, xmin=xmin, xmax=xmax, dx=dx, xname=xname
).tolist()
with open(data_path, "w") as f:
json.dump(evaluation, f)
return (
Expand All @@ -311,6 +341,17 @@ async def get_model_evaluation(
)


@mcp.tool()
@tool_errors
async def set_model_profile(
model_name: Annotated[str, "Name of the parametric model"],
profile_name: Annotated[str, "Name of the profile to set on the model"],
) -> str:
"""Set the profile for a parametric model in the refinement session."""
session.set_model_profile(model_name, profile_name)
return f"Profile {profile_name} set for model {model_name} successfully."


@mcp.tool()
@tool_errors
async def get_model_residual(
Expand Down
27 changes: 25 additions & 2 deletions src/diffpy/apps/refinebase/refinement_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,13 +247,36 @@ def get_model_residual(self, model_name: str):
)
return model.residual()

def get_model_evaluation(self, model_name: str):
def get_model_evaluation(
self, model_name: str, xmin=None, xmax=None, dx=None, xname="r"
):
model = self._get_model(model_name)
if not hasattr(model, "evaluate"):
raise ValueError(
f"Model '{model_name}' does not have an evaluate method."
)
return model.evaluate()
if not (
hasattr(model, "calc_obj")
and hasattr(model.calc_obj, "profile")
and model.calc_obj.profile is not None
):
profile = Profile()
if isinstance(model.calc_obj, FitContribution):
profile.set_observed_profile(
xobs=numpy.arange(xmin, xmax, dx),
yobs=numpy.zeros_like(numpy.arange(xmin, xmax, dx)),
xname=xname,
)
else:
# PDFGenerator uses profile.x, so it doesn't accept xname
profile.set_observed_profile(
xobs=numpy.arange(xmin, xmax, dx),
yobs=numpy.zeros_like(numpy.arange(xmin, xmax, dx)),
)
model.set_profile(profile)
ycalc = model.evaluate()
model.calc_obj.profile = None
return ycalc

def constrain_pdf_model_space_group_symmetry(
self, model_name, space_group=None
Expand Down
Loading