Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
self.user_params = {
'width': 1,
'height': 1,
'key1': UserSettableParameter('number', "Test Parameter", 101),
'key2': UserSettableParameter('slider', "Test Parameter", 200, 0, 300, 10)
}
self.viz_elements = [
CanvasGrid(self.portrayal, 10, 10, 20, 20),
TextElement(),
# ChartModule([{"Label": "Wolves", "Color": "#AA0000"}, # Todo - test chart module
# {"Label": "Sheep", "Color": "#666666"}])
]
self.server = ModularServer(MockModel, self.viz_elements, "Test Model", model_params=self.user_params)
def test_user_params(self):
print(self.server.user_params)
assert self.server.user_params == {
'key1': UserSettableParameter('number', "Test Parameter", 101).json,
'key2': UserSettableParameter('slider', "Test Parameter", 200, 0, 300, 10).json
}
class MyTextElement(TextElement):
def render(self, model):
ratio = model.resistant_susceptible_ratio()
ratio_text = '∞' if ratio is math.inf else '{0:.2f}'.format(ratio)
infected_text = str(number_infected(model))
return "Resistant/Susceptible Ratio: {}<br>Infected Remaining: {}".format(ratio_text, infected_text)
model_params = {
'num_nodes': UserSettableParameter('slider', 'Number of agents', 10, 10, 100, 1,
description='Choose how many agents to include in the model'),
'avg_node_degree': UserSettableParameter('slider', 'Avg Node Degree', 3, 3, 8, 1,
description='Avg Node Degree'),
'initial_outbreak_size': UserSettableParameter('slider', 'Initial Outbreak Size', 1, 1, 10, 1,
description='Initial Outbreak Size'),
'virus_spread_chance': UserSettableParameter('slider', 'Virus Spread Chance', 0.4, 0.0, 1.0, 0.1,
description='Probability that susceptible neighbor will be infected'),
'virus_check_frequency': UserSettableParameter('slider', 'Virus Check Frequency', 0.4, 0.0, 1.0, 0.1,
description='Frequency the nodes check whether they are infected by '
'a virus'),
'recovery_chance': UserSettableParameter('slider', 'Recovery Chance', 0.3, 0.0, 1.0, 0.1,
description='Probability that the virus will be removed'),
'gain_resistance_chance': UserSettableParameter('slider', 'Gain Resistance Chance', 0.5, 0.0, 1.0, 0.1,
description='Probability that a recovered agent will become '
'resistant to this virus in the future'),
}
server = ModularServer(VirusOnNetwork, [network, MyTextElement(), chart], 'Virus Model', model_params)
server.port = 8521
chart2 = ChartModule([
{"Label": "Gini", "Color": "#56bfdf"}],
data_collector_name='datacollector',
canvas_height=300, canvas_width=300
)
server = ModularServer(
CourtModel,
[chart1,chart2],
name="CourtModel",
model_params={
"juror_count": UserSettableParameter('slider', "Number of jurors", 20, 10, 100, 1,
description="Choose how many Juror agents to include in the model"),
"token_count": UserSettableParameter('slider', "Number of tokens", 40, 10, 400, 1,
description="Choose how many tokens in supply, tokens are split evenly among jurors at initialization."),
"threshold": UserSettableParameter('slider', "Belief Threshold", 1, 0.25, 5, 0.25,
description="Agents must sample within this many standard deviations from the true value to be coherent"),
"penalty_pct": UserSettableParameter('slider', "Dispensation Percentage", 0.1, 0.01, 1, 0.01,
description="Percentage of activated tokens which are redistributed from incoherent jurors to coherent")
})
server.port = 8521
server.launch()
portrayal["stroke_color"] = "#00FF00"
else:
portrayal["Color"] = ["#0000FF", "#9999FF"]
portrayal["stroke_color"] = "#000000"
return portrayal
happy_element = HappyElement()
canvas_element = CanvasGrid(schelling_draw, 20, 20, 500, 500)
happy_chart = ChartModule([{"Label": "happy", "Color": "Black"}])
model_params = {
"height": 20,
"width": 20,
"density": UserSettableParameter("slider", "Agent density", 0.8, 0.1, 1.0, 0.1),
"minority_pc": UserSettableParameter("slider", "Fraction minority", 0.2, 0.00, 1.0, 0.05),
"homophily": UserSettableParameter("slider", "Homophily", 3, 0, 8, 1)
}
server = ModularServer(Schelling,
[canvas_element, happy_element, happy_chart],
"Schelling", model_params)
server.launch()
canvas_height=300, canvas_width=300
)
chart2 = ChartModule([
{"Label": "Gini", "Color": "#56bfdf"}],
data_collector_name='datacollector',
canvas_height=300, canvas_width=300
)
server = ModularServer(
CourtModel,
[chart1,chart2],
name="CourtModel",
model_params={
"juror_count": UserSettableParameter('slider', "Number of jurors", 20, 10, 100, 1,
description="Choose how many Juror agents to include in the model"),
"token_count": UserSettableParameter('slider', "Number of tokens", 40, 10, 400, 1,
description="Choose how many tokens in supply, tokens are split evenly among jurors at initialization."),
"threshold": UserSettableParameter('slider', "Belief Threshold", 1, 0.25, 5, 0.25,
description="Agents must sample within this many standard deviations from the true value to be coherent"),
"penalty_pct": UserSettableParameter('slider', "Dispensation Percentage", 0.1, 0.01, 1, 0.01,
description="Percentage of activated tokens which are redistributed from incoherent jurors to coherent")
})
server.port = 8521
server.launch()
portrayal["Shape"] = "rect"
portrayal["Filled"] = "true"
portrayal["Layer"] = 0
portrayal["w"] = 1
portrayal["h"] = 1
return portrayal
canvas_element = CanvasGrid(wolf_sheep_portrayal, 20, 20, 500, 500)
chart_element = ChartModule([{"Label": "Wolves", "Color": "#AA0000"},
{"Label": "Sheep", "Color": "#666666"}])
model_params = {"grass": UserSettableParameter('checkbox', 'Grass Enabled', True),
"grass_regrowth_time": UserSettableParameter('slider', 'Grass Regrowth Time', 20, 1, 50),
"initial_sheep": UserSettableParameter('slider', 'Initial Sheep Population', 100, 10, 300),
"sheep_reproduce": UserSettableParameter('slider', 'Sheep Reproduction Rate', 0.04, 0.01, 1.0,
0.01),
"initial_wolves": UserSettableParameter('slider', 'Initial Wolf Population', 50, 10, 300),
"wolf_reproduce": UserSettableParameter('slider', 'Wolf Reproduction Rate', 0.05, 0.01, 1.0,
0.01,
description="The rate at which wolf agents reproduce."),
"wolf_gain_from_food": UserSettableParameter('slider', 'Wolf Gain From Food Rate', 20, 1, 50),
"sheep_gain_from_food": UserSettableParameter('slider', 'Sheep Gain From Food', 4, 1, 10)}
server = ModularServer(WolfSheep, [canvas_element, chart_element], "Wolf Sheep Predation", model_params)
server.port = 8521
portrayal["Color"] = ["#FF0000", "#FF9999"]
portrayal["stroke_color"] = "#00FF00"
else:
portrayal["Color"] = ["#0000FF", "#9999FF"]
portrayal["stroke_color"] = "#000000"
return portrayal
happy_element = HappyElement()
canvas_element = CanvasGrid(schelling_draw, 20, 20, 500, 500)
happy_chart = ChartModule([{"Label": "happy", "Color": "Black"}])
model_params = {
"height": 20,
"width": 20,
"density": UserSettableParameter("slider", "Agent density", 0.8, 0.1, 1.0, 0.1),
"minority_pc": UserSettableParameter("slider", "Fraction minority", 0.2, 0.00, 1.0, 0.05),
"homophily": UserSettableParameter("slider", "Homophily", 3, 0, 8, 1)
}
server = ModularServer(Schelling,
[canvas_element, happy_element, happy_chart],
"Schelling", model_params)
server.launch()
portrayal["Color"] = ["#84e184", "#adebad", "#d6f5d6"]
portrayal["Shape"] = "rect"
portrayal["Filled"] = "true"
portrayal["Layer"] = 0
portrayal["w"] = 1
portrayal["h"] = 1
return portrayal
canvas_element = CanvasGrid(wolf_sheep_portrayal, 20, 20, 500, 500)
chart_element = ChartModule([{"Label": "Wolves", "Color": "#AA0000"},
{"Label": "Sheep", "Color": "#666666"}])
model_params = {"grass": UserSettableParameter('checkbox', 'Grass Enabled', True),
"grass_regrowth_time": UserSettableParameter('slider', 'Grass Regrowth Time', 20, 1, 50),
"initial_sheep": UserSettableParameter('slider', 'Initial Sheep Population', 100, 10, 300),
"sheep_reproduce": UserSettableParameter('slider', 'Sheep Reproduction Rate', 0.04, 0.01, 1.0,
0.01),
"initial_wolves": UserSettableParameter('slider', 'Initial Wolf Population', 50, 10, 300),
"wolf_reproduce": UserSettableParameter('slider', 'Wolf Reproduction Rate', 0.05, 0.01, 1.0,
0.01,
description="The rate at which wolf agents reproduce."),
"wolf_gain_from_food": UserSettableParameter('slider', 'Wolf Gain From Food Rate', 20, 1, 50),
"sheep_gain_from_food": UserSettableParameter('slider', 'Sheep Gain From Food', 4, 1, 10)}
server = ModularServer(WolfSheep, [canvas_element, chart_element], "Wolf Sheep Predation", model_params)
server.port = 8521
portrayal["w"] = 1
portrayal["h"] = 1
return portrayal
canvas_element = CanvasGrid(wolf_sheep_portrayal, 20, 20, 500, 500)
chart_element = ChartModule([{"Label": "Wolves", "Color": "#AA0000"},
{"Label": "Sheep", "Color": "#666666"}])
model_params = {"grass": UserSettableParameter('checkbox', 'Grass Enabled', True),
"grass_regrowth_time": UserSettableParameter('slider', 'Grass Regrowth Time', 20, 1, 50),
"initial_sheep": UserSettableParameter('slider', 'Initial Sheep Population', 100, 10, 300),
"sheep_reproduce": UserSettableParameter('slider', 'Sheep Reproduction Rate', 0.04, 0.01, 1.0,
0.01),
"initial_wolves": UserSettableParameter('slider', 'Initial Wolf Population', 50, 10, 300),
"wolf_reproduce": UserSettableParameter('slider', 'Wolf Reproduction Rate', 0.05, 0.01, 1.0,
0.01,
description="The rate at which wolf agents reproduce."),
"wolf_gain_from_food": UserSettableParameter('slider', 'Wolf Gain From Food Rate', 20, 1, 50),
"sheep_gain_from_food": UserSettableParameter('slider', 'Sheep Gain From Food', 4, 1, 10)}
server = ModularServer(WolfSheep, [canvas_element, chart_element], "Wolf Sheep Predation", model_params)
server.port = 8521