m7n commited on
Commit
998a5b2
·
1 Parent(s): 57431fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -28
app.py CHANGED
@@ -627,6 +627,8 @@ class PoliticalModel(Model):
627
 
628
  # plt.show()
629
 
 
 
630
  def run_and_plot_simulation(separate_agent_types=False,n_agents=300, share_regime_supporters=0.4, threshold=0.5, social_learning_factor=1, simulation_steps=40, half_life=20,
631
  phys_network_radius=.06, powerlaw_exponent=3,physical_network_type='physical_network_type_fully_connected',
632
  introduce_physical_homophily_true_false=False,physical_homophily=.5,
@@ -743,7 +745,9 @@ def run_and_plot_simulation(separate_agent_types=False,n_agents=300, share_regim
743
  plt.ylabel('Estimation')
744
 
745
 
746
-
 
 
747
 
748
  # Create the network-plot
749
  n_networks = len(networks)
@@ -770,14 +774,27 @@ def run_and_plot_simulation(separate_agent_types=False,n_agents=300, share_regim
770
  # Draw the network with nodes colored by their estimation values
771
  node_colors = [estimations[node] for node in network.nodes]
772
  axs[idx].set_title(f'Network: {network_id}', loc='right')
773
- nx.draw(network, pos, node_size=50, node_color=node_colors,
774
- cmap=cmocean.tools.crop_by_percent(cmocean.cm.curl, 20, which='both', N=None),
775
- with_labels=False,vmin=0, vmax=1, ax=axs[idx])
 
 
 
 
 
 
 
 
 
776
  # Create a dummy ScalarMappable with the same colormap
777
  sm = mpl.cm.ScalarMappable(cmap=cmocean.tools.crop_by_percent(cmocean.cm.curl, 20, which='both', N=None),
778
  norm=plt.Normalize(vmin=0, vmax=1))
779
  sm.set_array([])
780
  network_plot.colorbar(sm, ax=axs[idx])
 
 
 
 
781
 
782
  return run_plot, network_plot
783
 
@@ -791,8 +808,8 @@ import matplotlib.pyplot as plt
791
  # Gradio interface
792
  with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
793
  with gr.Column():
794
- gr.Markdown("""# Simulate Revolutions
795
- Agents are placed on a fully connected graph. Vary the parameters below, and click 'Run Simulation' to run.
796
  """)
797
  with gr.Row():
798
  with gr.Column():
@@ -912,26 +929,9 @@ with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
912
  with gr.Column():
913
  # Button to trigger the simulation
914
  button = gr.Button("Run Simulation")
915
- plot_output = gr.Plot(label="Simulation Result")
916
- network_output = gr.Plot(label="Networks")
917
-
918
-
919
- # Function to call when button is clicked
920
- def run_simulation_and_plot(*args):
921
- fig = run_and_plot_simulation(*args)
922
- return fig
923
-
924
- # Setting up the button click event
925
- button.click(
926
- run_simulation_and_plot,
927
- inputs=[separate_agent_types,n_agents_slider, share_regime_slider, threshold_slider, social_learning_slider,
928
- steps_slider, half_life_slider, physical_network_type_random_geometric_radius,physical_network_type_random_geometric_powerlaw_exponent,physical_network_type,
929
- introduce_physical_homophily_true_false,physical_homophily,
930
- introduce_social_media_homophily_true_false,social_media_homophily,social_media_network_type_random_geometric_radius,social_media_network_type_powerlaw_exponent,social_media_network_type,use_social_media_network],
931
- outputs=[plot_output,network_output]
932
- )
933
-
934
- # Launch the interface
935
- if __name__ == "__main__":
936
- demo.launch(debug=True)
937
 
 
627
 
628
  # plt.show()
629
 
630
+ import PIL
631
+
632
  def run_and_plot_simulation(separate_agent_types=False,n_agents=300, share_regime_supporters=0.4, threshold=0.5, social_learning_factor=1, simulation_steps=40, half_life=20,
633
  phys_network_radius=.06, powerlaw_exponent=3,physical_network_type='physical_network_type_fully_connected',
634
  introduce_physical_homophily_true_false=False,physical_homophily=.5,
 
745
  plt.ylabel('Estimation')
746
 
747
 
748
+ plt.savefig('run_plot.png' ,bbox_inches='tight',
749
+ dpi =400, transparent=True)
750
+ run_plot = PIL.Image.open('run_plot.png').convert('RGBA')
751
 
752
  # Create the network-plot
753
  n_networks = len(networks)
 
774
  # Draw the network with nodes colored by their estimation values
775
  node_colors = [estimations[node] for node in network.nodes]
776
  axs[idx].set_title(f'Network: {network_id}', loc='right')
777
+ # nx.draw(network, pos, node_size=50, node_color=node_colors,
778
+ # cmap=cmocean.tools.crop_by_percent(cmocean.cm.curl, 20, which='both', N=None),
779
+ # with_labels=False,vmin=0, vmax=1, ax=axs[idx])
780
+ # Drawing nodes
781
+ nx.draw_networkx_nodes(network, pos, node_size=50, node_color=node_colors,
782
+ cmap=cmocean.tools.crop_by_percent(cmocean.cm.curl, 20, which='both', N=None),
783
+ vmin=0, vmax=1, ax=axs[idx])
784
+
785
+ # Drawing edges with semi-transparency
786
+ nx.draw_networkx_edges(network, pos, alpha=0.3, ax=axs[idx]) # alpha value for semi-transparency
787
+
788
+
789
  # Create a dummy ScalarMappable with the same colormap
790
  sm = mpl.cm.ScalarMappable(cmap=cmocean.tools.crop_by_percent(cmocean.cm.curl, 20, which='both', N=None),
791
  norm=plt.Normalize(vmin=0, vmax=1))
792
  sm.set_array([])
793
  network_plot.colorbar(sm, ax=axs[idx])
794
+ plt.savefig('network_plot.png' ,bbox_inches='tight',
795
+ dpi =400, transparent=True)
796
+
797
+ network_plot = PIL.Image.open('network_plot.png').convert('RGBA')
798
 
799
  return run_plot, network_plot
800
 
 
808
  # Gradio interface
809
  with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
810
  with gr.Column():
811
+ gr.Markdown("""# Simulate the emergence of social movements
812
+ Vary the parameters below, and click 'Run Simulation' to run.
813
  """)
814
  with gr.Row():
815
  with gr.Column():
 
929
  with gr.Column():
930
  # Button to trigger the simulation
931
  button = gr.Button("Run Simulation")
932
+ plot_output = gr.Image(label="Simulation Result")
933
+ network_output = gr.Image(label="Networks")
934
+ # gr.Button(value="Download Results",link="/file=network_plot.png")
935
+
936
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
937