Skip to content

Allow callables directly in the simulation model#254

Open
gilrrei wants to merge 1 commit intoqueens-py:mainfrom
gilrrei:allow_function_in_simulation_model
Open

Allow callables directly in the simulation model#254
gilrrei wants to merge 1 commit intoqueens-py:mainfrom
gilrrei:allow_function_in_simulation_model

Conversation

@gilrrei
Copy link
Member

@gilrrei gilrrei commented Nov 25, 2025

Description and Context:
What and Why?

This PR enables the direct use of functions in the simulation model, provided they have a specific signature. It follows from discussion #244 and the related meeting.

(As always, better naming welcome)

Related Issues and Pull Requests

Interested Parties

Note: More information on the merge request procedure in QUEENS can be found in the Submit a pull request section in the CONTRIBUTING.md file.

self.scheduler.copy_files_to_experiment_dir(self.driver.files_to_copy)
self.function: SchedulerCallableSignature
if isinstance(driver, Driver):
self.function = driver.run_from_parameters
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this, I reverted the __call__ method, since we need to check if it is a driver either way. This way, we can then see the function that is being called directly.

Result and potentially the gradient
"""
return self.run(sample, job_id, num_procs, experiment_dir, experiment_name)
def run_from_parameters(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the discussion of #244 and #245, I split up the functionality to allow the use of the driver without parameters. Once more, naming can be changed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add a default to the parameters like parameters=None in the init of the driver to indicate that parameters are not necessarily needed anymore? If we decide to do that, this function here needs to check that the parameters are not None before calling sample_as_dict.

@gilrrei gilrrei force-pushed the allow_function_in_simulation_model branch 2 times, most recently from 04ff736 to 055d99c Compare November 25, 2025 16:02
@gilrrei gilrrei force-pushed the allow_function_in_simulation_model branch from 055d99c to cc96d3b Compare November 26, 2025 08:42
@rjoussen
Copy link
Contributor

Looks good! I think this would also close #233, since the only use case for a dict_as_sample method was to pass dictionaries to the driver instead of the sample array, which can now be achieved by using a function (e.g. jobscript.run() instead of the full driver instance for the Simulation model creation.

@gilrrei gilrrei requested a review from maxdinkel November 28, 2025 07:54
Comment on lines +38 to +43
self.function: SchedulerCallableSignature
if isinstance(driver, Driver):
self.function = driver.run_from_parameters
self.scheduler.copy_files_to_experiment_dir(driver.files_to_copy)
else:
self.function = driver
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned to you in person my personal preference would be this version and keeping the __call__ method.

Suggested change
self.function: SchedulerCallableSignature
if isinstance(driver, Driver):
self.function = driver.run_from_parameters
self.scheduler.copy_files_to_experiment_dir(driver.files_to_copy)
else:
self.function = driver
self.function = driver
if isinstance(driver, Driver):
self.scheduler.copy_files_to_experiment_dir(driver.files_to_copy)

Copy link
Member

@leahaeusel leahaeusel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes look good to me, thank you for taking care of this! 🚀 Just two small comments from my side.

scheduler (Scheduler): Scheduler for the simulations
driver (Driver): Driver for the simulations
"""
"""Simulation model class."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Simulation model class."""
"""Simulation model class.
Attributes:
function: Callable for the simulations
"""

Result and potentially the gradient
"""
return self.run(sample, job_id, num_procs, experiment_dir, experiment_name)
def run_from_parameters(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add a default to the parameters like parameters=None in the init of the driver to indicate that parameters are not necessarily needed anymore? If we decide to do that, this function here needs to check that the parameters are not None before calling sample_as_dict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants