-
Notifications
You must be signed in to change notification settings - Fork 916
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
Agent size change based on size of figure #1820
Changes from 4 commits
452cca1
b8e6c09
5ff171a
fd5e9a5
0e1b2de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,6 +51,23 @@ def make_model(): | |
set_current_step(0) | ||
return model | ||
|
||
def calculate_space_size(): | ||
if model_parameters["N"] > 50: | ||
fixed_size = int(model_parameters["N"] / 5) | ||
model_parameters.update({"width": fixed_size, "height": fixed_size}) | ||
plt.rcParams["figure.figsize"] = (fixed_size, fixed_size) | ||
|
||
def calculate_agent_size(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Matplotlib marker size shouldn't depend on the number of agents. It should depend only on the grid size and the computer's screen size. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be more abstract, like screen width, because in a Jupyter notebook, the width should be even smaller. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, let me think how this can be implemented. Currently the size of each agent is parsed via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would also be important to show all agents in a smaller window size, hence the implementation can work there. I try to come up with some scaling to fit the screen width. |
||
aspect_ratio_high = ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Definition of aspect ratio is width/height: https://en.wikipedia.org/wiki/Aspect_ratio_(image). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, makes more sense, thanks! |
||
0.5 # maintain this aspect ration b/w agent size and space size | ||
) | ||
aspect_ratio_low = 0.1 # | ||
print() | ||
size_of_agent = aspect_ratio_high * ( | ||
model_parameters["width"] * model_parameters["height"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Must be GitHub hiccup. I remembered already commenting about this) the size is length^1, but the width * height is length^2, so it scales quadratically instead. It makes more sense to scale linearly over There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, the idea is to have a constant ratio b/w size of agents (for e.g. area of a circle) and size of the figure (area of a square). Hence the scaling is still linear i.e. the size of agent = aspect_ratio * size of figure. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just realized that the |
||
) | ||
return size_of_agent | ||
|
||
reset_counter = solara.use_reactive(0) | ||
model = solara.use_memo( | ||
make_model, dependencies=[*list(model_parameters.values()), reset_counter.value] | ||
|
@@ -61,20 +78,28 @@ def handle_change_model_params(name: str, value: any): | |
|
||
# 3. Set up UI | ||
solara.Markdown(name) | ||
|
||
# 4. Calculate space size and scale agents | ||
|
||
calculate_space_size() | ||
ag_size = calculate_agent_size() | ||
print(ag_size) | ||
|
||
UserInputs(user_params, on_change=handle_change_model_params) | ||
ModelController(model, play_interval, current_step, set_current_step, reset_counter) | ||
|
||
with solara.GridFixed(columns=2): | ||
# 4. Space | ||
with solara.Row(): | ||
# 5. Space | ||
if space_drawer == "default": | ||
# draw with the default implementation | ||
make_space(model, agent_portrayal) | ||
make_space(model, agent_portrayal, ag_size=ag_size) | ||
elif space_drawer: | ||
# if specified, draw agent space with an alternate renderer | ||
space_drawer(model, agent_portrayal) | ||
# otherwise, do nothing (do not draw space) | ||
|
||
# 5. Plots | ||
# 6. Plots | ||
with solara.Row(): | ||
for measure in measures: | ||
if callable(measure): | ||
# Is a custom object | ||
|
@@ -234,7 +259,7 @@ def change_handler(value, name=name): | |
raise ValueError(f"{input_type} is not a supported input type") | ||
|
||
|
||
def make_space(model, agent_portrayal): | ||
def make_space(model, agent_portrayal, ag_size=50): | ||
def portray(g): | ||
x = [] | ||
y = [] | ||
|
@@ -253,7 +278,8 @@ def portray(g): | |
x.append(i) | ||
y.append(j) | ||
if "size" in data: | ||
s.append(data["size"]) | ||
print(ag_size) | ||
s.append(ag_size) | ||
if "color" in data: | ||
c.append(data["color"]) | ||
out = {"x": x, "y": y} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once again, the grid size is independent of the number of agents. Having few agents and having many agents should result in the same marker size and figure size.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, agents less than 50 the figure size of default used by matplotlib. I start to scale the it more num of agents.
Ideal would be scale the image size based on screen size, but this doesn't seem feasible. Any other ideas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Number of agents shouldn't have effect on image size for the grid.