Skip to content

Commit

Permalink
Forcing sum to 1 for all views #317 #318
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreagiovanni Reina committed Sep 27, 2019
1 parent 7c6a869 commit 890f53f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 34 deletions.
31 changes: 21 additions & 10 deletions mumot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,12 +805,14 @@ def integrate(self, showStateVars=None, initWidgets=None, **kwargs):
IntParams = {}
# read input parameters
IntParams['substitutedReactant'] = [[react for react in self._getAllReactants()[0] if react not in self._reactants][0] if self._systemSize is not None else None, True]
# the first item of extraParam2 for intial state is fixSumTo1, the second is the idle reactant
extraParam2initS = [True, IntParams['substitutedReactant'][0]]
IntParams['initialState'] = utils._format_advanced_option(
optionName='initialState',
inputValue=kwargs.get('initialState'),
initValues=initWidgets.get('initialState'),
extraParam=self._getAllReactants(),
extraParam2 = IntParams['substitutedReactant'][0] )
extraParam2 = extraParam2initS )
IntParams['maxTime'] = utils._format_advanced_option(
optionName='maxTime',
inputValue=kwargs.get('maxTime'),
Expand Down Expand Up @@ -921,12 +923,14 @@ def noiseCorrelations(self, initWidgets=None, **kwargs):
NCParams = {}
# read input parameters
NCParams['substitutedReactant'] = [[react for react in self._getAllReactants()[0] if react not in self._reactants][0] if self._systemSize is not None else None, True]
# the first item of extraParam2 for intial state is fixSumTo1, the second is the idle reactant
extraParam2initS = [True, NCParams['substitutedReactant'][0]]
NCParams['initialState'] = utils._format_advanced_option(
optionName='initialState',
inputValue=kwargs.get('initialState'),
initValues=initWidgets.get('initialState'),
extraParam=self._getAllReactants(),
extraParam2=NCParams['substitutedReactant'][0])
extraParam2=extraParam2initS)
NCParams['maxTime'] = utils._format_advanced_option(
optionName='maxTime',
inputValue=kwargs.get('maxTime'),
Expand Down Expand Up @@ -1373,12 +1377,14 @@ def bifurcation(self, bifurcationParameter, stateVariable1,
inputValue=kwargs.get('initBifParam'),
initValues=initWidgets.get('initBifParam'))
BfcParams['substitutedReactant'] = [[react for react in self._getAllReactants()[0] if react not in self._reactants][0] if self._systemSize is not None else None, True]
# the first item of extraParam2 for intial state is fixSumTo1, the second is the idle reactant
extraParam2initS = [True, BfcParams['substitutedReactant'][0]]
BfcParams['initialState'] = utils._format_advanced_option(
optionName='initialState',
inputValue=kwargs.get('initialState'),
initValues=initWidgets.get('initialState'),
extraParam=self._getAllReactants(),
extraParam2=BfcParams['substitutedReactant'][0])
extraParam2=extraParam2initS)
BfcParams['bifurcationParameter'] = [bifPar, True]
BfcParams['conserved'] = [conserved, True]

Expand Down Expand Up @@ -1468,15 +1474,17 @@ def multiagent(self, initWidgets=None, **kwargs):
MAParams = {}
# Read input parameters
MAParams['substitutedReactant'] = [[react for react in self._getAllReactants()[0] if react not in self._reactants][0] if self._systemSize is not None else None, True]
# next line forces the multiagent() view to have the sum of the initial states to 1. If this wants to be changed, remember to change also the callback function of the widgets _updateInitialStateWidgets in controllers.py
if MAParams['substitutedReactant'][0] is None:
# the first item of extraParam2 for intial state is fixSumTo1, the second is the idle reactant
# in the multiagent() view, the sum of the initial states is fixed to 1. If this wants to be changed, remember to change also the callback function of the widgets _updateInitialStateWidgets in controllers.py
if MAParams['substitutedReactant'][0] is None and len(self._getAllReactants()[0]) > 1:
MAParams['substitutedReactant'][0] = sorted(self._getAllReactants()[0], key=str)[0]
extraParam2initS = [True, MAParams['substitutedReactant'][0]]
MAParams['initialState'] = utils._format_advanced_option(
optionName='initialState',
inputValue=kwargs.get('initialState'),
initValues=initWidgets.get('initialState'),
extraParam=self._getAllReactants(),
extraParam2=MAParams['substitutedReactant'][0])
extraParam2=extraParam2initS)
MAParams['maxTime'] = utils._format_advanced_option(
optionName='maxTime',
inputValue=kwargs.get('maxTime'),
Expand Down Expand Up @@ -1630,15 +1638,17 @@ def SSA(self, initWidgets=None, **kwargs):
ssaParams = {}
# Read input parameters
ssaParams['substitutedReactant'] = [[react for react in self._getAllReactants()[0] if react not in self._reactants][0] if self._systemSize is not None else None, True]
# next line forces the SSA() view to have the sum of the initial states to 1. If this wants to be changed, remember to change also the callback function of the widgets _updateInitialStateWidgets in controllers.py
if ssaParams['substitutedReactant'][0] is None:
# the first item of extraParam2 for intial state is fixSumTo1, the second is the idle reactant
# in the SSA() view, the sum of the initial states is fixed to 1. If this wants to be changed, remember to change also the callback function of the widgets _updateInitialStateWidgets in controllers.py
if ssaParams['substitutedReactant'][0] is None and len(self._getAllReactants()[0]) > 1:
ssaParams['substitutedReactant'][0] = sorted(self._getAllReactants()[0], key=str)[0]
extraParam2initS = [True, ssaParams['substitutedReactant'][0]]
ssaParams['initialState'] = utils._format_advanced_option(
optionName='initialState',
inputValue=kwargs.get('initialState'),
initValues=initWidgets.get('initialState'),
extraParam=self._getAllReactants(),
extraParam2=ssaParams['substitutedReactant'][0])
extraParam2=extraParam2initS)
ssaParams['maxTime'] = utils._format_advanced_option(
optionName='maxTime',
inputValue=kwargs.get('maxTime'),
Expand Down Expand Up @@ -1793,7 +1803,8 @@ 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.'
raise exceptions.MuMoTWarning(warningMsg)
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
48 changes: 26 additions & 22 deletions mumot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def _format_advanced_option(optionName: str, inputValue, initValues, extraParam=
"""
if optionName == 'initialState':
(allReactants, _) = extraParam
fixSumTo1 = extraParam2 is not None
idleReactant = extraParam2 #if extraParam2 is not None else sorted(allReactants, key=str)[0]
fixSumTo1 = extraParam2[0]
idleReactant = extraParam2[1]
initialState = {}
# handle initialState dictionary (either convert or generate a default one)
if inputValue is not None:
Expand Down Expand Up @@ -143,17 +143,20 @@ def _format_advanced_option(optionName: str, inputValue, initValues, extraParam=
sumValues = sum([initialState[reactant][0] for reactant in allReactants])
minStep = min([initialState[reactant][3] for reactant in allReactants])

# first thing setting the values of the idleReactant
idleValue = initialState[idleReactant][0]
if idleValue > 1:
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
# the idleValue have range min-max reset to [0,1]
initialState[idleReactant][1] = 0
initialState[idleReactant][2] = 1
initialState[idleReactant][3] = minStep
# first thing setting the values of the idleReactant
if idleReactant is not None:
idleValue = initialState[idleReactant][0]
if idleValue > 1:
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
# the idleValue have range min-max reset to [0,1]
initialState[idleReactant][1] = 0
initialState[idleReactant][2] = 1
initialState[idleReactant][3] = minStep
else:
idleValue = 0
for reactant in sorted(allReactants, key=str):
if reactant not in allReactants:
error_msg = (f"Reactant '{reactant}' does not exist in this model.\n"
Expand All @@ -174,15 +177,16 @@ def _format_advanced_option(optionName: str, inputValue, initValues, extraParam=
sumValues += new_val
initialState[reactant][0] = new_val
# modify (if necessary) min-max
pop = initialState[reactant]
sumNorm = sumValues if sumValues <= 1 else 1
if pop[2] > (1 - sumNorm + pop[0] + idleValue): # max
if pop[1] > (1 - sumNorm + pop[0] + idleValue): # min
initialState[reactant][1] = (1 - sumNorm + pop[0] + idleValue)
initialState[reactant][2] = (1 - sumNorm + pop[0] + idleValue)
if pop[1] > (1 - sumNorm + pop[0]): # min
initialState[reactant][1] = (1 - sumNorm + pop[0])
# initialState[reactant][3] = minStep
if idleReactant is not None:
pop = initialState[reactant]
sumNorm = sumValues if sumValues <= 1 else 1
if pop[2] > (1 - sumNorm + pop[0] + idleValue): # max
if pop[1] > (1 - sumNorm + pop[0] + idleValue): # min
initialState[reactant][1] = (1 - sumNorm + pop[0] + idleValue)
initialState[reactant][2] = (1 - sumNorm + pop[0] + idleValue)
if pop[1] > (1 - sumNorm + pop[0]): # min
initialState[reactant][1] = (1 - sumNorm + pop[0])
# initialState[reactant][3] = minStep
if not _almostEqual(sumValues, 1):
new_val = 1 - sum([initialState[reactant][0]
for reactant in allReactants
Expand Down
7 changes: 5 additions & 2 deletions mumot/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3911,7 +3911,9 @@ 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:
raise exceptions.MuMoTWarning("Only Moving-Particle netType is available when rules contain the emptyset or constant reactants.")
wrnMsg = "Only Moving-Particle netType is available when rules contain the emptyset or constant reactants."
print(wrnMsg)
#raise exceptions.MuMoTWarning()
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 +3928,8 @@ 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)
raise exceptions.MuMoTWarning(wrnMsg)
print(wrnMsg)
#raise exceptions.MuMoTWarning(wrnMsg)

def _build_bookmark(self, includeParams=True) -> str:
log_str = "bookmark = " if not self._silent else ""
Expand Down

0 comments on commit 890f53f

Please sign in to comment.