Skip to content

Commit

Permalink
Fixed exceptions for stochastic analysis methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreagiovanni Reina committed Sep 27, 2019
1 parent 16ebb62 commit f1d090b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 24 deletions.
2 changes: 1 addition & 1 deletion mumot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 4 additions & 16 deletions mumot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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]
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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]
Expand Down Expand Up @@ -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:
Expand All @@ -456,20 +448,17 @@ 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]
else:
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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)


Expand All @@ -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')


Expand Down
11 changes: 4 additions & 7 deletions mumot/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 ""
Expand Down Expand Up @@ -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")
Expand All @@ -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 = []
Expand Down Expand Up @@ -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]))
Expand All @@ -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)

Expand Down

0 comments on commit f1d090b

Please sign in to comment.