Skip to content

Commit 22eb078

Browse files
committed
feat: evaluate model with placeholder profile
1 parent f3caa41 commit 22eb078

2 files changed

Lines changed: 67 additions & 3 deletions

File tree

‎src/diffpy/apps/refinebase/refinement_server.py‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,39 @@ async def set_model_equation(
300300
async def get_model_evaluation(
301301
model_name: Annotated[str, "Name of the parametric model"],
302302
data_path: Annotated[str, "Path to the data to retrieve"],
303+
xmin: Annotated[
304+
float | None,
305+
(
306+
"Minimum x value for the evaluation "
307+
"if the profile is not set for the model"
308+
),
309+
] = None,
310+
xmax: Annotated[
311+
float | None,
312+
(
313+
"Maximum x value for the evaluation "
314+
"if the profile is not set for the model"
315+
),
316+
] = None,
317+
dx: Annotated[
318+
float | None,
319+
(
320+
"Step size for the evaluation "
321+
"if the profile is not set for the model"
322+
),
323+
] = None,
324+
xname: Annotated[
325+
str,
326+
(
327+
"Name of the x variable for the evaluation "
328+
"if the profile is not set for the model"
329+
),
330+
] = "r",
303331
) -> str:
304332
"""Get the current evaluation of a parametric model."""
305-
evaluation = session.get_model_evaluation(model_name=model_name).tolist()
333+
evaluation = session.get_model_evaluation(
334+
model_name=model_name, xmin=xmin, xmax=xmax, dx=dx, xname=xname
335+
).tolist()
306336
with open(data_path, "w") as f:
307337
json.dump(evaluation, f)
308338
return (
@@ -311,6 +341,17 @@ async def get_model_evaluation(
311341
)
312342

313343

344+
@mcp.tool()
345+
@tool_errors
346+
async def set_model_profile(
347+
model_name: Annotated[str, "Name of the parametric model"],
348+
profile_name: Annotated[str, "Name of the profile to set on the model"],
349+
) -> str:
350+
"""Set the profile for a parametric model in the refinement session."""
351+
session.set_model_profile(model_name, profile_name)
352+
return f"Profile {profile_name} set for model {model_name} successfully."
353+
354+
314355
@mcp.tool()
315356
@tool_errors
316357
async def get_model_residual(

‎src/diffpy/apps/refinebase/refinement_session.py‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,36 @@ def get_model_residual(self, model_name: str):
247247
)
248248
return model.residual()
249249

250-
def get_model_evaluation(self, model_name: str):
250+
def get_model_evaluation(
251+
self, model_name: str, xmin=None, xmax=None, dx=None, xname="r"
252+
):
251253
model = self._get_model(model_name)
252254
if not hasattr(model, "evaluate"):
253255
raise ValueError(
254256
f"Model '{model_name}' does not have an evaluate method."
255257
)
256-
return model.evaluate()
258+
if not (
259+
hasattr(model, "calc_obj")
260+
and hasattr(model.calc_obj, "profile")
261+
and model.calc_obj.profile is not None
262+
):
263+
profile = Profile()
264+
if isinstance(model.calc_obj, FitContribution):
265+
profile.set_observed_profile(
266+
xobs=numpy.arange(xmin, xmax, dx),
267+
yobs=numpy.zeros_like(numpy.arange(xmin, xmax, dx)),
268+
xname=xname,
269+
)
270+
else:
271+
# PDFGenerator uses profile.x, so it doesn't accept xname
272+
profile.set_observed_profile(
273+
xobs=numpy.arange(xmin, xmax, dx),
274+
yobs=numpy.zeros_like(numpy.arange(xmin, xmax, dx)),
275+
)
276+
model.set_profile(profile)
277+
ycalc = model.evaluate()
278+
model.calc_obj.profile = None
279+
return ycalc
257280

258281
def constrain_pdf_model_space_group_symmetry(
259282
self, model_name, space_group=None

0 commit comments

Comments
 (0)