From f1d090ba1ef40357fddcd4114a26c66b9b3601b6 Mon Sep 17 00:00:00 2001 From: Andreagiovanni Reina Date: Fri, 27 Sep 2019 12:23:41 +0100 Subject: [PATCH] Fixed exceptions for stochastic analysis methods --- mumot/models.py | 2 +- mumot/utils.py | 20 ++++---------------- mumot/views.py | 11 ++++------- 3 files changed, 9 insertions(+), 24 deletions(-) diff --git a/mumot/models.py b/mumot/models.py index 0271865..469043c 100644 --- a/mumot/models.py +++ b/mumot/models.py @@ -1782,7 +1782,7 @@ def _getSingleAgentRules(self): if reactant in allConstantReactants: warningMsg = 'WARNING! Constant reactants appearing on the right-handside are ignored. Every constant reactant on the left-handside (implicitly) corresponds to the same constant reactant on the right-handside.\n'\ f'E.g., in rule ' + str(rule.lhsReactants) + ' -> ' + str(rule.rhsReactants) + ' constant reactants should not appear on the right-handside.' - print(warningMsg) + raise exceptions.MuMoTWarning(warningMsg) break # print maximum one warning # Add to the target of the first non-empty item the new born coming from empty-set or constant reactants diff --git a/mumot/utils.py b/mumot/utils.py index b4d77ab..6ca8197 100644 --- a/mumot/utils.py +++ b/mumot/utils.py @@ -143,7 +143,6 @@ def _format_advanced_option(optionName: str, inputValue, initValues, extraParam= if reactant not in allReactants: error_msg = (f"Reactant '{reactant}' does not exist in this model.\n" f"Valid reactants are {allReactants}. Please, correct the value and retry.") - print(error_msg) raise exceptions.MuMoTValueError(error_msg) pop = initialState[reactant] @@ -161,7 +160,7 @@ def _format_advanced_option(optionName: str, inputValue, initValues, extraParam= new_val = max(0, pop[0] + (1 - sumValues)) if not _almostEqual(pop[0], new_val): wrn_msg = f"WARNING! the initial value of reactant {reactant} has been changed to {new_val}\n" - print(wrn_msg) + raise exceptions.MuMoTWarning(wrn_msg) sumValues -= pop[0] sumValues += new_val initialState[reactant][0] = new_val @@ -180,7 +179,7 @@ def _format_advanced_option(optionName: str, inputValue, initValues, extraParam= for reactant in allReactants if reactant != idleReactant]) wrn_msg = f"WARNING! the initial value of reactant {idleReactant} has been changed to {new_val}\n" - print(wrn_msg) + raise exceptions.MuMoTWarning(wrn_msg) initialState[idleReactant][0] = new_val return [initialState, fixedBool] # print("Initial State is " + str(initialState)) @@ -228,7 +227,6 @@ def _format_advanced_option(optionName: str, inputValue, initValues, extraParam= if decodedNetType is None: # terminating the process if the input argument is wrong error_msg = (f"The specified value for netType ={inputValue} is not valid. \n" "Accepted values are: 'full', 'erdos-renyi', 'barabasi-albert', and 'dynamic'.") - print(error_msg) raise exceptions.MuMoTValueError(error_msg) return [inputValue, True] @@ -246,7 +244,6 @@ def _format_advanced_option(optionName: str, inputValue, initValues, extraParam= if (not netType[-1]) and inputValue is not None: error_msg = ("If netType is not fixed, netParam cannot be fixed. " "Either leave free to widget the 'netParam' or fix the 'netType'.") - print(error_msg) raise exceptions.MuMoTValueError(error_msg) # check if netParam range is valid or set the correct default range (systemSize is necessary) if utils._decodeNetworkTypeFromString(netType[0]) == consts.NetworkType.FULLY_CONNECTED: @@ -317,7 +314,6 @@ def _format_advanced_option(optionName: str, inputValue, initValues, extraParam= if inputValue not in validVisualisationTypes: # terminating the process if the input argument is wrong errorMsg = (f"The specified value for visualisationType = {inputValue} is not valid.\n" f"Valid values are: {validVisualisationTypes}. Please correct it and retry.") - print(errorMsg) raise exceptions.MuMoTValueError(errorMsg) return [inputValue, True] else: @@ -333,7 +329,6 @@ def _format_advanced_option(optionName: str, inputValue, initValues, extraParam= if inputValue not in reactants_str: error_msg = (f"The specified value for {optionName} = {inputValue} is not valid.\n" f"Valid values are the reactants: {reactants_str}. Please correct it and retry.") - print(error_msg) raise exceptions.MuMoTValueError(error_msg) else: return [inputValue, True] @@ -429,18 +424,15 @@ def _parse_input_keyword_for_numeric_widgets( if initValueRangeStep is not None and getattr(initValueRangeStep, "__getitem__", None) is None: error_msg = (f"initValueRangeStep value '{initValueRangeStep}' must be specified in the format [val,min,max,step].\n" "Please, correct the value and retry.") - print(error_msg) raise exceptions.MuMoTValueError(error_msg) if inputValue is not None: if not isinstance(inputValue, numbers.Number): error_msg = (f"Input value '{inputValue}' is not a numeric vaule and must be a number.\n" "Please, correct the value and retry.") - print(error_msg) raise exceptions.MuMoTValueError(error_msg) elif validRange and (inputValue < validRange[0] or inputValue > validRange[1]): error_msg = (f"Input value '{inputValue}' has raised out-of-range exception. Valid range is {validRange}\n" "Please, correct the value and retry.") - print(error_msg) raise exceptions.MuMoTValueError(error_msg) else: if onlyValue: @@ -456,7 +448,6 @@ def _parse_input_keyword_for_numeric_widgets( if validRange and (initValueRangeStep < validRange[0] or initValueRangeStep > validRange[1]): error_msg = (f"Invalid init value={initValueRangeStep} has raised out-of-range exception. Valid range is {validRange}\n" "Please, correct the value and retry.") - print(error_msg) raise exceptions.MuMoTValueError(error_msg) else: outputValues = [initValueRangeStep] @@ -464,12 +455,10 @@ def _parse_input_keyword_for_numeric_widgets( if initValueRangeStep[1] > initValueRangeStep[2] or initValueRangeStep[0] < initValueRangeStep[1] or initValueRangeStep[0] > initValueRangeStep[2]: error_msg = (f"Invalid init range [val,min,max,step]={initValueRangeStep}. Value must be within min and max values.\n" "Please, correct the value and retry.") - print(error_msg) raise exceptions.MuMoTValueError(error_msg) elif validRange and (initValueRangeStep[1] < validRange[0] or initValueRangeStep[2] > validRange[1]): error_msg = (f"Invalid init range [val,min,max,step]={initValueRangeStep} has raised out-of-range exception. Valid range is {validRange}\n" "Please, correct the value and retry.") - print(error_msg) raise exceptions.MuMoTValueError(error_msg) else: outputValues = initValueRangeStep @@ -502,7 +491,6 @@ def _parse_input_keyword_for_boolean_widgets(inputValue, defaultValue, initValue paramNameForErrorMsg = f"for {paramNameForErrorMsg} = " if paramNameForErrorMsg else "" errorMsg = (f"The specified value {paramNameForErrorMsg}'{inputValue}' is not valid. \n" "The value must be a boolean True/False.") - print(errorMsg) raise exceptions.MuMoTValueError(errorMsg) return [inputValue, True] else: @@ -540,7 +528,7 @@ def _decodeNetworkTypeFromString(netTypeStr: str) -> Optional[consts.NetworkType 'dynamic': consts.NetworkType.DYNAMIC} if netTypeStr not in admissibleNetTypes: - print(f"ERROR! Invalid network type argument! Valid strings are: {admissibleNetTypes}") + raise exceptions.MuMoTValueError(f"ERROR! Invalid network type argument! Valid strings are: {admissibleNetTypes}") return admissibleNetTypes.get(netTypeStr, None) @@ -552,7 +540,7 @@ def _encodeNetworkTypeToString(netType: consts.NetworkType) -> Optional[str]: consts.NetworkType.DYNAMIC: 'dynamic'} if netType not in netTypeEncoding: - print(f"ERROR! Invalid netTypeEncoding table! Tried to encode network type: {netType}") + raise exceptions.MuMoTValueError(f"ERROR! Invalid netTypeEncoding table! Tried to encode network type: {netType}") return netTypeEncoding.get(netType, 'none') diff --git a/mumot/views.py b/mumot/views.py index 3656023..ed1fc07 100644 --- a/mumot/views.py +++ b/mumot/views.py @@ -3911,7 +3911,7 @@ def _constructorSpecificParams(self, MAParams): # if (not self._controller) and (not self._netType == consts.NetworkType.DYNAMIC): # if the user has specified the network type, we notify him/her through error-message # self._errorMessage.value = "Only Moving-Particle netType is available when rules contain the emptyset." if not self._netType == consts.NetworkType.DYNAMIC: - print("Only Moving-Particle netType is available when rules contain the emptyset or constant reactants.") + raise exceptions.MuMoTWarning("Only Moving-Particle netType is available when rules contain the emptyset or constant reactants.") self._netType = consts.NetworkType.DYNAMIC if self._controller: # updating value and disabling widget if self._controller._widgetsExtraParams.get('netType') is not None: @@ -3926,7 +3926,7 @@ def _constructorSpecificParams(self, MAParams): wrnMsg = "WARNING! net-param value " + str(self._netParam) + " is invalid for Moving-Particles. Valid range is [0,1] indicating the particles' communication range. \n" self._netParam = 0.1 wrnMsg += "New default values is '_netParam'=" + str(self._netParam) - print(wrnMsg) + raise exceptions.MuMoTWarning(wrnMsg) def _build_bookmark(self, includeParams=True) -> str: log_str = "bookmark = " if not self._silent else "" @@ -4224,7 +4224,6 @@ def _initGraph(self): else: errorMsg = ("ERROR! Invalid network parameter (link probability) for E-R networks. " f"It must be between 0 and 1; input is {self._netParam}") - print(errorMsg) raise exceptions.MuMoTValueError(errorMsg) elif (self._netType == consts.NetworkType.BARABASI_ALBERT): # print("Generating Barabasi-Albert graph") @@ -4234,12 +4233,10 @@ def _initGraph(self): else: errorMsg = ("ERROR! Invalid network parameter (number of edges per new node) for B-A networks." f"It must be an integer between 1 and {numNodes}; input is {self._netParam}") - print(errorMsg) raise exceptions.MuMoTValueError(errorMsg) elif (self._netType == consts.NetworkType.SPACE): # @todo: implement network generate by placing points (with local communication range) randomly in 2D space errorMsg = "ERROR: Graphs of type SPACE are not implemented yet." - print(errorMsg) raise exceptions.MuMoTValueError(errorMsg) elif (self._netType == consts.NetworkType.DYNAMIC): self._positions = [] @@ -4697,7 +4694,7 @@ def _simulationStep(self) -> Tuple[float, object]: bottom += prob if reaction_id == -1: - print("ERROR! Transition not found. Error in the algorithm execution.") + raise exceptions.MuMoTError("ERROR! Transition not found. Error in the algorithm execution.") sys.exit() # print("current state: " + str(self._currentState)) # print("triggered change: " + str(self._mumotModel._stoichiometry[reaction_id])) @@ -4708,7 +4705,7 @@ def _simulationStep(self) -> Tuple[float, object]: continue self._currentState[reactant] += re_stoch[1] - re_stoch[0] if self._currentState[reactant] < 0: - print(f"ERROR! Population size became negative: {self._currentState}; Error in the algorithm execution.") + raise exceptions.MuMoTError(f"ERROR! Population size became negative: {self._currentState}; Error in the algorithm execution.") sys.exit() # print(self._currentState)