Coverage for physioblocks / simulation / functions.py: 100%

14 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-09 16:40 +0100

1# SPDX-FileCopyrightText: Copyright INRIA 

2# 

3# SPDX-License-Identifier: LGPL-3.0-only 

4# 

5# Copyright INRIA 

6# 

7# This file is part of PhysioBlocks, a library mostly developed by the 

8# [Ananke project-team](https://team.inria.fr/ananke) at INRIA. 

9# 

10# Authors: 

11# - Colin Drieu 

12# - Dominique Chapelle 

13# - François Kimmig 

14# - Philippe Moireau 

15# 

16# PhysioBlocks is free software: you can redistribute it and/or modify it under the 

17# terms of the GNU Lesser General Public License as published by the Free Software 

18# Foundation, version 3 of the License. 

19# 

20# PhysioBlocks is distributed in the hope that it will be useful, but WITHOUT ANY 

21# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 

22# PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 

23# 

24# You should have received a copy of the GNU Lesser General Public License along with 

25# PhysioBlocks. If not, see <https://www.gnu.org/licenses/>. 

26 

27"""Declare base functions to write functions used during the simulation.""" 

28 

29from abc import ABC, abstractmethod 

30from inspect import signature 

31from typing import Any 

32 

33from physioblocks.simulation.state import STATE_NAME_ID 

34from physioblocks.simulation.time_manager import TIME_QUANTITY_ID 

35 

36 

37class AbstractFunction(ABC): 

38 """Base class for functions that update quantities during the simulation.""" 

39 

40 @abstractmethod 

41 def eval(self, *args: Any, **kwargs: Any) -> Any: 

42 """Child Functions have to overwrite this method to compute the 

43 function result.""" 

44 

45 

46def is_time_function(tested_function: AbstractFunction) -> bool: 

47 """Test if the simulation function needs simulation time parameter. 

48 

49 :param tested_function: the function 

50 :type tested_function: AbstractFunction 

51 

52 :return: True if the function needs time, False otherwise 

53 :rtype: bool 

54 """ 

55 

56 sig = signature(tested_function.eval) 

57 return TIME_QUANTITY_ID in sig.parameters 

58 

59 

60def is_state_function(tested_function: AbstractFunction) -> bool: 

61 """Test if the simulation function needs the state. 

62 

63 :param function: the simulation function 

64 :type function: AbstractFunction 

65 

66 :return: True if the function needs the state, False otherwise 

67 :rtype: bool 

68 """ 

69 

70 sig = signature(tested_function.eval) 

71 return STATE_NAME_ID in sig.parameters