ccm's picture
Create app.py
6abed4d
raw
history blame
2.67 kB
import datasets
import random
import numpy
import json
import gradio
import matplotlib.pyplot # for colormap
import matplotlib.colors # for color conversion
vr = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['variable_radius.zip'], split='train')
vn = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['variable_number.zip'], split='train')
circle = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['circle/circle_test.zip'], split="train[:10]")
crescent = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['crescent/crescent_test.zip'], split="train[:10]")
peanut = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['peanut/peanut_test.zip'], split="train[:10]")
ellipse = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['ellipse/ellipse_test.zip'], split="train[:10]")
triangle = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['triangle/triangle_test.zip'], split="train[:10]")
rectangle = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['rectangle/rectangle_test.zip'], split="train[:10]")
shapes = {
"circle": circle,
"crescent": crescent,
"ellipse": ellipse,
"peanut": peanut,
"triangle": triangle,
"rectangle": rectangle,
}
def randomize(selection):
index = random.randint(0, 9)
mask = 255*numpy.array(json.loads(shapes[selection]['Defects'][index]))
v = numpy.array(json.loads(shapes[selection]['Strain'][index]))
print(v)
# Get the color map by name:
cm = matplotlib.pyplot.get_cmap('RdBu')
measure = max(v.max(), -v.min())
print(measure)
output = (v / measure)
print(cm((numpy.multiply(output[:, :, 0], mask)+1.0)/2.0))
return mask, cm((numpy.multiply(output[:, :, 0], mask/255.0)+1.0)/2.0), cm((numpy.multiply(output[:, :, 1], mask/255.0)+1.0)/2.0), cm((numpy.multiply(output[:, :, 2], mask/255.0)+1.0)/2.0)
with gradio.Blocks() as demo:
selection = gradio.Dropdown(["circle", "crescent", "ellipse", "peanut", "triangle", "rectangle"], label="Select defect shape")
with gradio.Row():
with gradio.Column(label="Defects"):
mask = gradio.Image()
with gradio.Column(label="ε-xx"):
exx = gradio.Image()
with gradio.Row():
with gradio.Column():
eyy = gradio.Image(label="ε-yy")
with gradio.Column():
exy = gradio.Image(label="ε-xy")
selection.change(fn=randomize, inputs=[selection], outputs=[mask, exx, eyy, exy])
demo.launch(debug=True)