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

Agent size change based on size of figure #1820

Closed
wants to merge 5 commits into from

Conversation

ankitk50
Copy link
Contributor

If the number of agents in greater than 50, I use a linearly decreasing function to calculate the size of the agent. The lower limit for the agent size is 5.

def calculate_space_size():
pass

def calculate_agent_size():
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 agent_portrayal and hence it's easier(also less compute expensive) to manipulate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@Corvince
Copy link
Contributor

Thank you for starting to work on this 👍

@ankitk50
Copy link
Contributor Author

ankitk50 commented Sep 26, 2023

I have restructured the UI layout using some of the features by Solara. The sidebar can be minimised to expand the user view:

Screenshot 2023-09-26 at 15 57 57

@rht
Copy link
Contributor

rht commented Sep 26, 2023

That looks nice. We could adaptively change the layout by detecting whether the Solara component is run in a Jupyter notebook or not: https://stackoverflow.com/questions/15411967/how-can-i-check-if-code-is-executed-in-the-ipython-notebook. But for this PR, you should focus on solving the grid render for #1806. The layout restyling should be in a separate PR.

@ankitk50
Copy link
Contributor Author

ankitk50 commented Sep 26, 2023

I have now updated to change the size of the figure based on number of agents. This is possibly the easiest way. This avoids agent markers to come close as the number of users increase.

Changing the size of the markers itself does not look neat, although its possible.

Sure I can move future layout modifications to another PR.

@rht
Copy link
Contributor

rht commented Sep 26, 2023

To put it concisely:

  • The marker size depends mainly on figure width and grid width, and nothing else. cell_width = figure_width / grid_width. Marker size should be adaptively scaled such that it is x% of cell width
  • The figure size depends on screen width, and it is actually harder to detect screen width because it is a client-side information.

@ankitk50
Copy link
Contributor Author

Thanks for the explaination, I can try this out. Meanwhile UI related changes is moved to PR #1825.

@ankitk50 ankitk50 changed the title Agent size change based on Number of Users Agent size change based on size of figure Oct 3, 2023
@ankitk50
Copy link
Contributor Author

ankitk50 commented Oct 3, 2023

The change maintains the size of the agents based on size of the figure.

aspect_ratio_low = 0.1 #
print()
size_of_agent = aspect_ratio_high * (
model_parameters["width"] * model_parameters["height"]
Copy link
Contributor

Choose a reason for hiding this comment

The 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 min(width, height).

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that the s param in Matplotlib scatter is length^2: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.scatter.html. It's markersize**2 without a multiplication by pi. We should document that size refers to Matplotlib size, and it is an area.

plt.rcParams["figure.figsize"] = (fixed_size, fixed_size)

def calculate_agent_size():
aspect_ratio_high = (
Copy link
Contributor

Choose a reason for hiding this comment

The 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). fill_fraction is a more descriptive term.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, makes more sense, thanks!

@@ -51,6 +51,23 @@ def make_model():
set_current_step(0)
return model

def calculate_space_size():
if model_parameters["N"] > 50:
Copy link
Contributor

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.

Copy link
Contributor Author

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I start to scale the it more num of agents.

Number of agents shouldn't have effect on image size for the grid.

@rht rht mentioned this pull request Oct 7, 2023
@EwoutH
Copy link
Member

EwoutH commented Oct 23, 2023

Really like this idea! What's needed to move this forward?

@rht
Copy link
Contributor

rht commented Oct 23, 2023

  1. Remove number of agents from the formula to calculate size. I have said several times that grid cell size has nothing to do with the number of agents. You can have a huge grid with sparse agents, and yet the agent marker still needs to be not bigger than the grid cell size
  2. UI layout #1825 (comment). Given that we haven't figured out a way to get screen size info to be sent to the Solara server, auto-scaling figure size needs to be removed for now.
  3. Print functions haven't been removed

@rht
Copy link
Contributor

rht commented Oct 23, 2023

Secondary reason for #1825 (comment) is that it is not thread safe. Anything that is global operation via plt.* is not thread safe.

@EwoutH
Copy link
Member

EwoutH commented Jan 21, 2024

@ankitk50 can we help you move this forward?

@rht
Copy link
Contributor

rht commented Feb 22, 2024

Superseded by #2049.

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

Successfully merging this pull request may close these issues.

4 participants