Spaces:
Runtime error
Runtime error
removed problematic trial code
Browse files
app.py
CHANGED
@@ -569,63 +569,63 @@ class PoliticalModel(Model):
|
|
569 |
|
570 |
# # Example usage
|
571 |
|
572 |
-
radius=.09
|
573 |
-
physical_graph_points = np.random.rand(100, 2)
|
574 |
-
physical_graph = graph_from_coordinates(physical_graph_points, radius)
|
575 |
-
physical_graph = nx.convert_node_labels_to_integers(ensure_neighbors(physical_graph))
|
576 |
|
577 |
-
# unconnected nodes: link or drop?
|
578 |
-
networks = {
|
579 |
-
|
580 |
|
581 |
|
582 |
-
model = PoliticalModel(100, networks, .5, .5,.5, half_life=20, print_agents=False, print_frequency=50, agent_reporters=True, intervention_list=[])
|
583 |
|
584 |
|
585 |
-
for _ in tqdm.tqdm_notebook(range(40)): # Run for specified number of steps
|
586 |
-
|
587 |
|
588 |
-
import matplotlib.pyplot as plt
|
589 |
-
import pandas as pd
|
590 |
|
591 |
-
# Assuming 'model' is defined and has a datacollector with the necessary data
|
592 |
-
agent_df = model.datacollector.get_agent_vars_dataframe().reset_index()
|
593 |
|
594 |
-
# Pivot the dataframe for Estimation
|
595 |
-
agent_df_pivot = agent_df.pivot(index='Step', columns='AgentID', values='Estimation')
|
596 |
|
597 |
-
# Create the result plot
|
598 |
-
run_plot, ax = plt.subplots(figsize=(12, 8))
|
599 |
|
600 |
-
# Define colors for Dissident and Supporter
|
601 |
-
colors = {1: '#d6a44b', 0: '#1b4968'} # 1 for Dissident, 0 for Supporter
|
602 |
-
labels = {1: 'Dissident', 0: 'Supporter'}
|
603 |
-
legend_handles = []
|
604 |
|
605 |
-
# Plot each agent's data
|
606 |
-
for agent_id in agent_df_pivot.columns:
|
607 |
-
|
608 |
-
|
609 |
|
610 |
-
|
611 |
-
|
612 |
|
613 |
|
614 |
-
# Compute and plot the mean estimation for each group
|
615 |
-
for agent_type, color in colors.items():
|
616 |
-
|
617 |
-
|
618 |
|
619 |
-
# Set the plot title and labels
|
620 |
-
plt.title('Agent Estimation Over Time', loc='right')
|
621 |
-
plt.xlabel('Time step')
|
622 |
-
plt.ylabel('Estimation')
|
623 |
|
624 |
-
# Add legend
|
625 |
-
plt.legend(loc='lower right')
|
626 |
|
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',
|
@@ -635,3 +635,303 @@ def run_and_plot_simulation(separate_agent_types=False,n_agents=300, share_regim
|
|
635 |
|
636 |
print(physical_network_type)
|
637 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
569 |
|
570 |
# # Example usage
|
571 |
|
572 |
+
# radius=.09
|
573 |
+
# physical_graph_points = np.random.rand(100, 2)
|
574 |
+
# physical_graph = graph_from_coordinates(physical_graph_points, radius)
|
575 |
+
# physical_graph = nx.convert_node_labels_to_integers(ensure_neighbors(physical_graph))
|
576 |
|
577 |
+
# # unconnected nodes: link or drop?
|
578 |
+
# networks = {
|
579 |
+
# "physical": {"network": physical_graph, "type": "physical", "positions": physical_graph_points, 'network_data_to_keep':{'radius':radius},'homophily':0. }}
|
580 |
|
581 |
|
582 |
+
# model = PoliticalModel(100, networks, .5, .5,.5, half_life=20, print_agents=False, print_frequency=50, agent_reporters=True, intervention_list=[])
|
583 |
|
584 |
|
585 |
+
# for _ in tqdm.tqdm_notebook(range(40)): # Run for specified number of steps
|
586 |
+
# model.step()
|
587 |
|
588 |
+
# import matplotlib.pyplot as plt
|
589 |
+
# import pandas as pd
|
590 |
|
591 |
+
# # Assuming 'model' is defined and has a datacollector with the necessary data
|
592 |
+
# agent_df = model.datacollector.get_agent_vars_dataframe().reset_index()
|
593 |
|
594 |
+
# # Pivot the dataframe for Estimation
|
595 |
+
# agent_df_pivot = agent_df.pivot(index='Step', columns='AgentID', values='Estimation')
|
596 |
|
597 |
+
# # Create the result plot
|
598 |
+
# run_plot, ax = plt.subplots(figsize=(12, 8))
|
599 |
|
600 |
+
# # Define colors for Dissident and Supporter
|
601 |
+
# colors = {1: '#d6a44b', 0: '#1b4968'} # 1 for Dissident, 0 for Supporter
|
602 |
+
# labels = {1: 'Dissident', 0: 'Supporter'}
|
603 |
+
# legend_handles = []
|
604 |
|
605 |
+
# # Plot each agent's data
|
606 |
+
# for agent_id in agent_df_pivot.columns:
|
607 |
+
# # Get the agent type (Dissident or Supporter)
|
608 |
+
# agent_type = agent_df[agent_df['AgentID'] == agent_id]['Dissident'].iloc[0]
|
609 |
|
610 |
+
# # Plot
|
611 |
+
# line, = plt.plot(agent_df_pivot.index, agent_df_pivot[agent_id], color=colors[agent_type], alpha=0.1)
|
612 |
|
613 |
|
614 |
+
# # Compute and plot the mean estimation for each group
|
615 |
+
# for agent_type, color in colors.items():
|
616 |
+
# mean_estimation = agent_df_pivot.loc[:, agent_df[agent_df['Dissident'] == agent_type]['AgentID']].mean(axis=1)
|
617 |
+
# plt.plot(mean_estimation.index, mean_estimation, color=color, linewidth=2, label=f'{labels[agent_type]}')
|
618 |
|
619 |
+
# # Set the plot title and labels
|
620 |
+
# plt.title('Agent Estimation Over Time', loc='right')
|
621 |
+
# plt.xlabel('Time step')
|
622 |
+
# plt.ylabel('Estimation')
|
623 |
|
624 |
+
# # Add legend
|
625 |
+
# plt.legend(loc='lower right')
|
626 |
|
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',
|
|
|
635 |
|
636 |
print(physical_network_type)
|
637 |
|
638 |
+
networks = {}
|
639 |
+
|
640 |
+
# Set up physical network:
|
641 |
+
if physical_network_type == 'Fully Connected':
|
642 |
+
G = nx.complete_graph(n_agents)
|
643 |
+
networks['physical'] = {"network": G, "type": "physical", "positions": nx.circular_layout(G)}
|
644 |
+
|
645 |
+
elif physical_network_type == "Powerlaw":
|
646 |
+
s = nx.utils.powerlaw_sequence(n_agents, powerlaw_exponent) #100 nodes, power-law exponent 2.5
|
647 |
+
G = nx.expected_degree_graph(s, selfloops=False)
|
648 |
+
G = nx.convert_node_labels_to_integers(ensure_neighbors(G))
|
649 |
+
networks['physical'] = {"network": G, "type": "physical", "positions": nx.kamada_kawai_layout(G)}
|
650 |
+
|
651 |
+
elif physical_network_type == "Random Geometric":
|
652 |
+
physical_graph_points = np.random.rand(n_agents, 2)
|
653 |
+
G = graph_from_coordinates(physical_graph_points, phys_network_radius)
|
654 |
+
G = nx.convert_node_labels_to_integers(ensure_neighbors(G))
|
655 |
+
networks['physical'] = {"network": G, "type": "physical", "positions": physical_graph_points}
|
656 |
+
|
657 |
+
if introduce_physical_homophily_true_false:
|
658 |
+
networks['physical']['homophily'] = physical_homophily
|
659 |
+
networks['physical']['network_data_to_keep'] = {}
|
660 |
+
|
661 |
+
|
662 |
+
# Set up social media network:
|
663 |
+
|
664 |
+
if use_social_media_network:
|
665 |
+
if social_media_network_type == 'Fully Connected':
|
666 |
+
G = nx.complete_graph(n_agents)
|
667 |
+
networks['social_media'] = {"network": G, "type": "social_media", "positions": nx.circular_layout(G)}
|
668 |
+
|
669 |
+
elif social_media_network_type == "Powerlaw":
|
670 |
+
s = nx.utils.powerlaw_sequence(n_agents, social_media_network_type_powerlaw_exponent) # 100 nodes, power-law exponent adjusted for social media
|
671 |
+
G = nx.expected_degree_graph(s, selfloops=False)
|
672 |
+
G = nx.convert_node_labels_to_integers(ensure_neighbors(G))
|
673 |
+
networks['social_media'] = {"network": G, "type": "social_media", "positions": nx.kamada_kawai_layout(G)}
|
674 |
+
|
675 |
+
elif social_media_network_type == "Random Geometric":
|
676 |
+
social_media_graph_points = np.random.rand(n_agents, 2)
|
677 |
+
G = graph_from_coordinates(social_media_graph_points, social_media_network_type_random_geometric_radius)
|
678 |
+
G = nx.convert_node_labels_to_integers(ensure_neighbors(G))
|
679 |
+
networks['social_media'] = {"network": G, "type": "social_media", "positions": social_media_graph_points}
|
680 |
+
|
681 |
+
if introduce_social_media_homophily_true_false:
|
682 |
+
networks['social_media']['homophily'] = social_media_homophily
|
683 |
+
networks['social_media']['network_data_to_keep'] = {}
|
684 |
+
|
685 |
+
|
686 |
+
|
687 |
+
intervention_list = [ ]
|
688 |
+
|
689 |
+
# Initialize the model
|
690 |
+
model = PoliticalModel(n_agents, networks, share_regime_supporters, threshold,
|
691 |
+
social_learning_factor, half_life=half_life, print_agents=False, print_frequency=50, agent_reporters=True, intervention_list=intervention_list)
|
692 |
+
|
693 |
+
# Run the model
|
694 |
+
for _ in tqdm.tqdm_notebook(range(simulation_steps)): # Run for specified number of steps
|
695 |
+
model.step()
|
696 |
+
|
697 |
+
|
698 |
+
|
699 |
+
agent_df = model.datacollector.get_agent_vars_dataframe().reset_index()
|
700 |
+
|
701 |
+
# Pivot the dataframe
|
702 |
+
agent_df_pivot = agent_df.pivot(index='Step', columns='AgentID', values='Estimation')
|
703 |
+
|
704 |
+
|
705 |
+
# Create the esult-plot
|
706 |
+
run_plot, ax = plt.subplots(figsize=(12, 8))
|
707 |
+
if not separate_agent_types:
|
708 |
+
for column in agent_df_pivot.columns:
|
709 |
+
plt.plot(agent_df_pivot.index, agent_df_pivot[column], color='gray', alpha=0.1)
|
710 |
+
|
711 |
+
# Compute and plot the mean estimation
|
712 |
+
mean_estimation = agent_df_pivot.mean(axis=1)
|
713 |
+
plt.plot(mean_estimation.index, mean_estimation, color='black', linewidth=2)
|
714 |
+
|
715 |
+
|
716 |
+
|
717 |
+
else:
|
718 |
+
# Define colors for Dissident and Supporter
|
719 |
+
colors = {1: '#d6a44b', 0: '#1b4968'} # 1 for Dissident, 0 for Supporter
|
720 |
+
labels = {1: 'Dissident', 0: 'Supporter'}
|
721 |
+
legend_handles = []
|
722 |
+
|
723 |
+
# Plot each agent's data
|
724 |
+
for agent_id in agent_df_pivot.columns:
|
725 |
+
# Get the agent type (Dissident or Supporter)
|
726 |
+
agent_type = agent_df[agent_df['AgentID'] == agent_id]['Dissident'].iloc[0]
|
727 |
+
|
728 |
+
# Plot
|
729 |
+
line, = plt.plot(agent_df_pivot.index, agent_df_pivot[agent_id], color=colors[agent_type], alpha=0.1)
|
730 |
+
|
731 |
+
|
732 |
+
# Compute and plot the mean estimation for each group
|
733 |
+
for agent_type, color in colors.items():
|
734 |
+
mean_estimation = agent_df_pivot.loc[:, agent_df[agent_df['Dissident'] == agent_type]['AgentID']].mean(axis=1)
|
735 |
+
plt.plot(mean_estimation.index, mean_estimation, color=color, linewidth=2, label=f'{labels[agent_type]}')
|
736 |
+
plt.legend(loc='lower right')
|
737 |
+
|
738 |
+
|
739 |
+
|
740 |
+
# Set the plot title and labels
|
741 |
+
plt.title('Agent Estimation Over Time', loc='right')
|
742 |
+
plt.xlabel('Time step')
|
743 |
+
plt.ylabel('Estimation')
|
744 |
+
|
745 |
+
|
746 |
+
|
747 |
+
|
748 |
+
# Create the network-plot
|
749 |
+
n_networks = len(networks)
|
750 |
+
network_plot, axs = plt.subplots(1, n_networks, figsize=( 9.5 * n_networks,8))
|
751 |
+
|
752 |
+
if n_networks == 1:
|
753 |
+
axs = [axs]
|
754 |
+
estimations = {}
|
755 |
+
for agent in model.schedule.agents:
|
756 |
+
estimations[agent.unique_id] = agent.estimation
|
757 |
+
for idx, (network_id, network_dict) in enumerate(networks.items()):
|
758 |
+
network = network_dict['network']
|
759 |
+
# Collect estimations and set the node attributes
|
760 |
+
|
761 |
+
|
762 |
+
nx.set_node_attributes(network, estimations, 'estimation')
|
763 |
+
|
764 |
+
# Use the positions provided in the network dict if available
|
765 |
+
if 'positions' in network_dict:
|
766 |
+
pos = network_dict['positions']
|
767 |
+
else:
|
768 |
+
pos = nx.kamada_kawai_layout(network)
|
769 |
+
|
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 |
+
|
784 |
+
|
785 |
+
# run_and_plot_simulation(n_agents=300, share_regime_supporters=0.4, threshold=0.5, social_learning_factor=1, simulation_steps=40, half_life=20)
|
786 |
+
|
787 |
+
import gradio as gr
|
788 |
+
import matplotlib.pyplot as plt
|
789 |
+
|
790 |
+
|
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():
|
799 |
+
|
800 |
+
with gr.Group():
|
801 |
+
separate_agent_types = gr.Checkbox(value=False, label="Separate agent types in plot")
|
802 |
+
|
803 |
+
# Sliders for each parameter
|
804 |
+
n_agents_slider = gr.Slider(minimum=100, maximum=500, step=10, label="Number of Agents", value=150)
|
805 |
+
share_regime_slider = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Share of Regime Supporters", value=0.4)
|
806 |
+
threshold_slider = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Threshold", value=0.5)
|
807 |
+
social_learning_slider = gr.Slider(minimum=0.0, maximum=2.0, step=0.1, label="Social Learning Factor", value=1.0)
|
808 |
+
steps_slider = gr.Slider(minimum=10, maximum=100, step=5, label="Simulation Steps", value=40)
|
809 |
+
half_life_slider = gr.Slider(minimum=5, maximum=50, step=5, label="Half-Life", value=20)
|
810 |
+
|
811 |
+
|
812 |
+
# physical network settings
|
813 |
+
with gr.Group():
|
814 |
+
# with gr.Group():
|
815 |
+
gr.Markdown("""**Physical Network Settings:**""")
|
816 |
+
# Define the checkbox
|
817 |
+
introduce_physical_homophily_true_false = gr.Checkbox(value=False, label="Stipulate Homophily")
|
818 |
+
|
819 |
+
# Define a group to hold the slider
|
820 |
+
with gr.Group(visible=False) as homophily_group:
|
821 |
+
physical_homophily = gr.Slider(0, 1, label="Homophily", info='How much homophily to stipulate.')
|
822 |
+
|
823 |
+
# Function to update the visibility of the group based on the checkbox
|
824 |
+
def update_homophily_group_visibility(checkbox_state):
|
825 |
+
return {
|
826 |
+
homophily_group: gr.Group(visible=checkbox_state) # The group visibility depends on the checkbox
|
827 |
+
}
|
828 |
+
|
829 |
+
# Bind the function to the checkbox
|
830 |
+
introduce_physical_homophily_true_false.change(
|
831 |
+
update_homophily_group_visibility,
|
832 |
+
inputs=introduce_physical_homophily_true_false,
|
833 |
+
outputs=homophily_group
|
834 |
+
)
|
835 |
+
|
836 |
+
|
837 |
+
physical_network_type = gr.Dropdown(label="Physical Network Type", value="Fully Connected",choices=["Fully Connected", "Random Geometric","Powerlaw"])#value ="Fully Connected"
|
838 |
+
|
839 |
+
|
840 |
+
with gr.Group(visible=True) as physical_network_type_fully_connected_group:
|
841 |
+
gr.Markdown("""""")
|
842 |
+
|
843 |
+
with gr.Group(visible=False) as physical_network_type_random_geometric_group:
|
844 |
+
physical_network_type_random_geometric_radius = gr.Slider(minimum=.0, maximum=.5,label="Radius")
|
845 |
+
|
846 |
+
with gr.Group(visible=False) as physical_network_type_powerlaw_group:
|
847 |
+
physical_network_type_random_geometric_powerlaw_exponent = gr.Slider(minimum=.0, maximum=5.2,label="Powerlaw Exponent")
|
848 |
+
|
849 |
+
def update_sliders(option):
|
850 |
+
return {
|
851 |
+
physical_network_type_fully_connected_group: gr.Group(visible=option == "Fully Connected"),
|
852 |
+
physical_network_type_random_geometric_group: gr.Group(visible=option == "Random Geometric"),
|
853 |
+
physical_network_type_powerlaw_group: gr.Group(visible=option == "Powerlaw") }
|
854 |
+
|
855 |
+
|
856 |
+
physical_network_type.change(update_sliders, inputs=physical_network_type, outputs=[physical_network_type_fully_connected_group,
|
857 |
+
physical_network_type_random_geometric_group,
|
858 |
+
physical_network_type_powerlaw_group])
|
859 |
+
|
860 |
+
# social media settings:
|
861 |
+
use_social_media_network = gr.Checkbox(value=False, label="Use social media network")
|
862 |
+
with gr.Group(visible=False) as social_media_group:
|
863 |
+
gr.Markdown("""**Social Media Network Settings:**""")
|
864 |
+
|
865 |
+
# Define the checkbox for social media network
|
866 |
+
social_media_factor = gr.Slider(0, 2, label="Social Media Factor", info='How strongly to weigh the social media network against learning in the real world.')
|
867 |
+
introduce_social_media_homophily_true_false = gr.Checkbox(value=False, label="Stipulate Homophily")
|
868 |
+
|
869 |
+
# Define a group to hold the slider for social media network
|
870 |
+
with gr.Group(visible=False) as social_media_homophily_group:
|
871 |
+
social_media_homophily = gr.Slider(0, 1, label="Homophily", info='How much homophily to stipulate in social media network.')
|
872 |
+
|
873 |
+
# Function to update the visibility of the group based on the checkbox for social media network
|
874 |
+
def update_social_media_homophily_group_visibility(checkbox_state):
|
875 |
+
return {
|
876 |
+
social_media_homophily_group: gr.Group(visible=checkbox_state) # The group visibility depends on the checkbox for social media network
|
877 |
+
}
|
878 |
+
|
879 |
+
# Bind the function to the checkbox for social media network
|
880 |
+
introduce_social_media_homophily_true_false.change(
|
881 |
+
update_social_media_homophily_group_visibility,
|
882 |
+
inputs=introduce_social_media_homophily_true_false,
|
883 |
+
outputs=social_media_homophily_group
|
884 |
+
)
|
885 |
+
|
886 |
+
social_media_network_type = gr.Dropdown(label="Social Media Network Type", value="Fully Connected", choices=["Fully Connected", "Random Geometric", "Powerlaw"])
|
887 |
+
|
888 |
+
with gr.Group(visible=True) as social_media_network_type_fully_connected_group:
|
889 |
+
gr.Markdown("""""")
|
890 |
+
|
891 |
+
with gr.Group(visible=False) as social_media_network_type_random_geometric_group:
|
892 |
+
social_media_network_type_random_geometric_radius = gr.Slider(minimum=0.0, maximum=0.5, label="Radius")
|
893 |
+
|
894 |
+
with gr.Group(visible=False) as social_media_network_type_powerlaw_group:
|
895 |
+
social_media_network_type_powerlaw_exponent = gr.Slider(minimum=0.0, maximum=5.2, label="Powerlaw Exponent")
|
896 |
+
|
897 |
+
def update_social_media_network_sliders(option):
|
898 |
+
return {
|
899 |
+
social_media_network_type_fully_connected_group: gr.Group(visible=option == "Fully Connected"),
|
900 |
+
social_media_network_type_random_geometric_group: gr.Group(visible=option == "Random Geometric"),
|
901 |
+
social_media_network_type_powerlaw_group: gr.Group(visible=option == "Powerlaw")
|
902 |
+
}
|
903 |
+
|
904 |
+
social_media_network_type.change(update_social_media_network_sliders, inputs=social_media_network_type, outputs=[social_media_network_type_fully_connected_group,
|
905 |
+
social_media_network_type_random_geometric_group,
|
906 |
+
social_media_network_type_powerlaw_group])
|
907 |
+
def update_social_media_group_visibility(checkbox_state):
|
908 |
+
return {social_media_group: gr.Group(visible=checkbox_state) }
|
909 |
+
use_social_media_network.change(update_social_media_group_visibility,inputs=use_social_media_network,outputs=social_media_group)
|
910 |
+
|
911 |
+
|
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 |
+
|