Spaces:
Runtime error
Runtime error
File size: 3,043 Bytes
1b1f8bd 31b5130 c4a1eee 1b1f8bd b9c59dd 98a45af 1b1f8bd 45770ea 1b1f8bd e20b343 31b5130 598d19d 1b1f8bd 31b5130 1b1f8bd 3b0b0de 8e6c03a 3f8872c 8e6c03a 1b1f8bd 2ff0f36 1b1f8bd 56830a2 3f8872c 1b1f8bd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
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) |