ccm's picture
Update app.py
98a45af
raw
history blame
3.04 kB
import pandas
import numpy
import gradio
import matplotlib.pyplot
import math
import datasets
data = datasets.load_dataset("cmudrc/truss-design-study", data_files="1-s2.0-S2352340918302014-mmc2.csv")["train"].to_pandas()
def how_many_designs(team: int, participant: int):
n = len(numpy.unique(data[(data['Team'] == team) & (data['Participant'] == participant)]['Design']))
return gradio.Slider.update(1, n, step=1)
def print_design(team: int, participant: int, design: int):
matplotlib.pyplot.close()
fig, ax = matplotlib.pyplot.subplots(1)
s = 0.3
h = s*math.sqrt(3)/2
matplotlib.pyplot.fill([-5.0, -5.0-s*0.5, -5.0+s*0.5], [0.0, -h, -h], color="black")
matplotlib.pyplot.fill([1.0, 1.0-s*0.5, 1.0+s*0.5], [0.0, -h, -h], color="black")
matplotlib.pyplot.fill([5.0, 5.0-s*0.5, 5.0+s*0.5], [0.0, -h, -h], color="black")
matplotlib.pyplot.axis('equal')
matplotlib.pyplot.xlim([-6.0, 6.0])
matplotlib.pyplot.ylim([-3.0, 5.0])
ax.set_xticks(range(-6, 7))
ax.set_yticks(range(-3, 6))
ax.set_yticklabels([])
ax.set_xticklabels([])
matplotlib.pyplot.grid()
df = data[(data['Team'] == int(team)) & (data['Participant'] == int(participant)) & (data['Design'] == int(design))]
nodes = df[df['Component'] > 0]
edges = df[df['Component'] < 0]
e1 = [int(x) for x in edges['Var1'].values]
e2 = [int(x) for x in edges['Var2'].values]
all_info = [e1, e2, [0]*len(e1)]
idx = numpy.array(all_info).transpose().flatten()
x = [None if i == 0 else nodes[nodes['Component'] == i]['Var1'].values[0] for i in idx]
y = [None if i == 0 else nodes[nodes['Component'] == i]['Var2'].values[0] for i in idx]
matplotlib.pyplot.plot(x, y, color="grey")
matplotlib.pyplot.plot(nodes['Var1'].values, nodes['Var2'].values, linestyle='none', marker='o', label="", color="black")
return fig
with gradio.Blocks() as demo:
with gradio.Row():
with gradio.Column():
team = gradio.Dropdown(choices=[str(i) for i in range(1,17)], value="1", label="Team Number")
participant = gradio.Dropdown(choices=[str(i) for i in range(1,4)], value="1", label="Teammate Number")
design = gradio.Slider(1, 100, step=1, value=1, label="Design Number")
with gradio.Row():
btn1 = gradio.Button("< Reverse", variant="secondary")
btn1.click(fn=lambda x: gradio.Slider.update(value=x-1), inputs=[design], outputs=[design], show_progress=False)
btn2 = gradio.Button("Forward >", variant="primary")
btn2.click(fn=lambda x: gradio.Slider.update(value=x+1), inputs=[design], outputs=[design], show_progress=False)
with gradio.Column():
output = gradio.Plot(value=print_design(1, 1, 1))
participant.change(fn=print_design, inputs=[team, participant, design], outputs=[output])
team.change(fn=print_design, inputs=[team, participant, design], outputs=[output])
design.change(fn=print_design, inputs=[team, participant, design], outputs=[output], show_progress=False)
demo.launch(debug=True)