Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to add Histogram to the visualization! #13

Open
PreetamKulkarni opened this issue Apr 25, 2023 · 2 comments
Open

Unable to add Histogram to the visualization! #13

PreetamKulkarni opened this issue Apr 25, 2023 · 2 comments

Comments

@PreetamKulkarni
Copy link

I am trying the "MoneyModel" from the tutorial and it demonstrates how to add a "Histogram" to the visualization window. I followed the instructions and I get the following error:

module 'mesa.flat.visualization' has no attribute 'HistogramModule'

I made sure I saved the "HistogramModule.py" and "HistogramModule.js" in the same folder. It still gives me the same error.

@rht
Copy link
Contributor

rht commented Apr 25, 2023

HistogramModule is a custom class defined in the tutorial, not imported from the mesa library. You should have done either a relative or absolute import from HistogramModule.py instead.

@PreetamKulkarni
Copy link
Author

Thank you for the help. I fixed the code and it looks like this:

from money_model import *
import mesa
from mesa.visualization.ModularVisualization import VisualizationElement, CHART_JS_FILE
import numpy as np

class HistogramModule(VisualizationElement):
package_includes = [CHART_JS_FILE]
local_includes = ["HistogramModule.js"]

def __init__(self, bins, canvas_height, canvas_width):
    self.canvas_height = canvas_height
    self.canvas_width = canvas_width
    self.bins = bins
    new_element = "new HistogramModule({}, {}, {})"
    new_element = new_element.format(bins,
                                     canvas_width,
                                     canvas_height)
    self.js_code = "elements.push(" + new_element + ");"
    
def render(self, model):
    wealth_vals = [agent.wealth for agent in model.schedule.agents]
    hist = np.histogram(wealth_vals, bins=self.bins)[0]
    return [int(x) for x in hist]

def agent_portrayal(agent):
portrayal = {"Shape": "circle",
"Filled": "true",
"r": 0.5}

if agent.wealth > 0:
    portrayal["Color"] = "red"
    portrayal["Layer"] = 0
else:
    portrayal["Color"] = "grey"
    portrayal["Layer"] = 1
    portrayal["r"] = 0.2
return portrayal

grid = mesa.visualization.CanvasGrid(agent_portrayal, 10, 10, 500, 500)

chart = mesa.visualization.ChartModule([{"Label": "Gini",
"Color": "Black"}],
data_collector_name='datacollector')

histogram = HistogramModule(list(range(10)), 200, 500)

server = mesa.visualization.ModularServer(MoneyModel,
[grid, histogram, chart],
"Money Model",
{"N":100, "width":10, "height":10})

server.port = 8521 # The default
server.launch()

However, I get the following error:
RuntimeError: This event loop is already running
I do see the histogram on the visualization window but it doesn't update.
When I press start, it doesn't advance. When I use step, it does advance but in either case, the chart and the histogram don't update.

I used the following fix on HistogramModule.js as well but it doesn't solve the problem:
projectmesa/mesa#609

@rht rht transferred this issue from projectmesa/mesa Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants