Comments for Helipad Codex https://helipad.dev Agent-based modeling in Python Thu, 04 Jul 2024 15:09:14 +0000 hourly 1 https://wordpress.org/?v=6.9.4 Comment on show by charwick https://helipad.dev/functions/params/show/#comment-1500 Thu, 04 Jul 2024 15:09:14 +0000 https://helipad.dev/?page_id=791#comment-1500 This code hides the 'rent' parameter depending on whether the 'city' parameter is checked.

def constrain(model, var, val):
	if var=='city':
		if val: model.params.hide('rent')
		else: model.params.show('rent')
heli.params.add('city', 'City', 'check', True, runtime=False, callback=constrain)
]]>
Comment on hide by charwick https://helipad.dev/functions/params/hide/#comment-1499 Thu, 04 Jul 2024 15:08:16 +0000 https://helipad.dev/?page_id=790#comment-1499 This code hides the 'rent' parameter depending on whether the 'city' parameter is checked.

def constrain(model, var, val):
	if var=='city':
		if val: model.params.hide('rent')
		else: model.params.show('rent')
heli.params.add('city', 'City', 'check', True, runtime=False, callback=constrain)
]]>
Comment on AgentsPlot by charwick https://helipad.dev/functions/agentsplot/#comment-600 Fri, 16 Jun 2023 18:37:53 +0000 https://helipad.dev/?page_id=596#comment-600 A single AgentsPlot object can support any or all of spatial, network, and scatterplot layouts, which can be rotated through with the L key. The network structure will be displayed over all layouts, including the spatial and scatterplots unless the value of plot.network does not correspond to a network name. This example makes all three layout types available.

import random
from helipad import Helipad
heli = Helipad()

@heli.hook
def modelPostSetup(model):
    model.agents.createNetwork(0.2, 'connections')

@heli.hook
def agentInit(agent, model):
    agent.p1 = random.uniform(0,100)
    agent.p2 = random.uniform(0,200)

#This function returns `AgentsPlot` and sets the default layout to `spatial`.
#We get the object this way rather than with `heli.visual.addPlot(type='agents')`
#because the latter will not make a spatial layout available.
plot = heli.spatial()

plot.network = 'connections'
plot.scatter = ('p1', 'p2')
]]>
Comment on PatchesGeo by charwick https://helipad.dev/functions/patchesgeo/#comment-547 Tue, 30 May 2023 02:25:55 +0000 https://helipad.dev/?page_id=772#comment-547 Patches can be imported from GeoJSON files, along with others, using the Geopandas package.

import geopandas,os

mapPlot = heli.spatial(corners=True, geometry='geo')
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
gdf = geopandas.read_file(os.path.join(__location__, 'NC_Counties.geojson'))
for i,county in gdf.iterrows():
    heli.patches.add(county['geometry'], county['CO_NAME'])
]]>
Comment on agentPosition by charwick https://helipad.dev/hooks/agentposition/#comment-539 Fri, 26 May 2023 06:05:11 +0000 https://helipad.dev/?post_type=hook&p=714#comment-539 If the spatial model has been initialized with offmap=False, this hook must return a position on a live patch, or a ValueError will be raised. This can be avoided by checking Patches.at(), which returns None if there is no patch under a coordinate.

@heli.hook
def agentPosition(agent, model):
    position = [0, random.uniform(0,patches.dim[1])]
    while not model.patches.at(*position):
        position[0] += 1
    return position
]]>
Comment on spatial by charwick https://helipad.dev/functions/model/spatial/#comment-511 Tue, 10 Jan 2023 02:03:54 +0000 https://helipad.dev/?page_id=517#comment-511 As of Helipad 1.5, the grid dimensions are no longer stored as global parameters, but as properties of the patchgrid object. They are thus no longer settable in the control panel by default. A grid dimension parameter can, however, be added with a callback to update the patchgrid object:

maxdim = 30
mapPlot = heli.spatial(dim=maxdim)

def setdim(model, var, val): model.patches.dim = (int(val), int(val))
heli.params.add('dim', 'Dimension', 'slider', maxdim, opts={'low': 4, 'high': maxdim, 'step': 1}, runtime=False, callback=setdim)
]]>
Comment on terminate by charwick https://helipad.dev/hooks/terminate/#comment-479 Tue, 12 Jul 2022 18:53:53 +0000 https://helipad.dev/?post_type=hook&p=339#comment-479 If you launch another Matplotlib window in post-analysis, you must make sure not to block the event loop; otherwise you cannot launch a new model until the window is closed. This can be done with the block=False argument on pyplot.show().

This example shows an impulse response function after each model run, without blocking further runs.

@heli.hook
def terminate(model, data):
	from statsmodels.tsa.api import VAR
	import matplotlib.pyplot as plt

	model = VAR(data)
	results = model.fit(50)
	irf = results.irf(50)
	irf.plot(orth=False, impulse='M0', response='ratio-jam-axe')
	plt.show(block=False)
]]>
Comment on Params by charwick https://helipad.dev/functions/params/#comment-472 Sat, 02 Jul 2022 04:36:12 +0000 https://helipad.dev/?page_id=678#comment-472 Properties of already-registered parameters can be accessed through this object. For example, to hide the default population slider:

heli.params['num_agent'].type='hidden'
]]>
Comment on cutStep by charwick https://helipad.dev/functions/model/cutstep/#comment-471 Fri, 01 Jul 2022 05:55:50 +0000 https://helipad.dev/?page_id=668#comment-471 In this model, all agents must succeed in order for the success condition to be met. If any agent fails, we can just skip to the next period.

@heli.hook
def agentStep(agent, model, stage):
	agent.checked = []

	#Choose the box with your own number, then choose the box with the number
	#you find inside, and so on until you find your own number or run out.
	agent.checked.append(model.boxes[agent.id-1])
	while agent.id not in agent.checked and len(agent.checked) < 50:
		agent.checked.append(model.boxes[agent.checked[-1]-1])

	if agent.id not in agent.checked:
		model.escaped = False
		model.cutStep() #Go ahead and skip everyone else if anyone fails
]]>
Comment on expandableFrame by charwick https://helipad.dev/functions/expandableframe/#comment-470 Tue, 28 Jun 2022 04:19:55 +0000 http://helipad-docs.nfshost.com/?page_id=77#comment-470 Action buttons can be added to either side of the expandableFrame as follows:

EF = expandableFrame(frame, text=param.title)
def efAction():
	#Do something here
	pass

EF.buttons['right'].setup('→', efAction)

This will place an arrow button on the right side of the expandable frame’s title that runs efAction().

]]>