Skip to content

Commit

Permalink
Handle exceptions from the initialize() function call from wrapped fu…
Browse files Browse the repository at this point in the history
…nctions
  • Loading branch information
IshaanDesai committed May 28, 2024
1 parent f50f748 commit e32066a
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions micro_manager/micro_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,15 +471,35 @@ def initialize(self) -> None:
self._micro_sims_init = True

# Check if the initialize() method of the micro simulation has any arguments
argspec = inspect.getfullargspec(micro_problem.initialize)
if len(argspec.args) == 1:
is_initial_data_required = False
elif len(argspec.args) == 2:
is_initial_data_required = True
else:
raise Exception(
"The initialize() method of the Micro simulation has an incorrect number of arguments."
try: # Try to get the signature of the initialize() method, if it is written in Python
argspec = inspect.getfullargspec(micro_problem.initialize)
if len(argspec.args) == 1:
is_initial_data_required = False
elif len(argspec.args) == 2:
is_initial_data_required = True
else:
raise Exception(
"The initialize() method of the Micro simulation has an incorrect number of arguments."
)
except TypeError:
self._logger.info(
"The signature of initialize() method of the micro simulation cannot be determined. Trying to determine the signature by calling the method."
)
# Try to call the initialize() method of the micro simulation without arguments. This is necessary if the function is not written in Python.
try:
self._micro_sims[0].initialize()
is_initial_data_required = False
except TypeError:
self._logger.info(
"The initialize() method of the micro simulation has arguments. Attempting to call it again with initial data."
)
try:
self._micro_sims[0].initialize(initial_data[0])
is_initial_data_required = True
except TypeError:
raise Exception(
"The initialize() method of the Micro simulation has an incorrect number of arguments."
)

if is_initial_data_required and not is_initial_data_available:
raise Exception(
Expand Down

0 comments on commit e32066a

Please sign in to comment.