From e32066a3f8ab10cc672c47ea78585e47fa7e10dd Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Tue, 28 May 2024 17:42:49 +0200 Subject: [PATCH 1/2] Handle exceptions from the initialize() function call from wrapped functions --- micro_manager/micro_manager.py | 36 ++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/micro_manager/micro_manager.py b/micro_manager/micro_manager.py index 449f5d1..b420ce8 100644 --- a/micro_manager/micro_manager.py +++ b/micro_manager/micro_manager.py @@ -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( From 228a4bd2bd8a5890b67651c101ff38dc5299f076 Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Fri, 31 May 2024 12:16:25 +0200 Subject: [PATCH 2/2] Rework comments and add CHANGELOG entry --- CHANGELOG.md | 1 + micro_manager/micro_manager.py | 13 +++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ff9770c..cf2ba50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## latest +- Handle calling `initialize()` function of micro simulations written in languages other than Python https://github.com/precice/micro-manager/pull/110 - Check if initial data returned from the micro simulation is the data that the adaptivity computation requires https://github.com/precice/micro-manager/pull/109 - Use executable `micro-manager-precice` by default, and stop using the script `run_micro_manager.py` https://github.com/precice/micro-manager/pull/105 - Make `initialize()` method of the MicroManager class public https://github.com/precice/micro-manager/pull/105 diff --git a/micro_manager/micro_manager.py b/micro_manager/micro_manager.py index b420ce8..6472b5f 100644 --- a/micro_manager/micro_manager.py +++ b/micro_manager/micro_manager.py @@ -468,12 +468,13 @@ def initialize(self) -> None: if hasattr(micro_problem, "initialize") and callable( getattr(micro_problem, "initialize") ): - self._micro_sims_init = True + self._micro_sims_init = True # Starting value before setting - # Check if the initialize() method of the micro simulation has any 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: + if ( + len(argspec.args) == 1 + ): # The first argument in the signature is self is_initial_data_required = False elif len(argspec.args) == 2: is_initial_data_required = True @@ -485,15 +486,15 @@ def initialize(self) -> None: 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: + # Try to get the signature of the initialize() method, if it is not written in Python + try: # Try to call the initialize() method without initial data 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: + try: # Try to call the initialize() method with initial data self._micro_sims[0].initialize(initial_data[0]) is_initial_data_required = True except TypeError: