Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import datasets
|
| 2 |
+
import random
|
| 3 |
+
import numpy
|
| 4 |
+
import json
|
| 5 |
+
import gradio
|
| 6 |
+
import matplotlib.pyplot # for colormap
|
| 7 |
+
import matplotlib.colors # for color conversion
|
| 8 |
+
|
| 9 |
+
vr = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['variable_radius.zip'], split='train')
|
| 10 |
+
|
| 11 |
+
vn = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['variable_number.zip'], split='train')
|
| 12 |
+
|
| 13 |
+
circle = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['circle/circle_test.zip'], split="train[:10]")
|
| 14 |
+
|
| 15 |
+
crescent = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['crescent/crescent_test.zip'], split="train[:10]")
|
| 16 |
+
|
| 17 |
+
peanut = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['peanut/peanut_test.zip'], split="train[:10]")
|
| 18 |
+
|
| 19 |
+
ellipse = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['ellipse/ellipse_test.zip'], split="train[:10]")
|
| 20 |
+
|
| 21 |
+
triangle = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['triangle/triangle_test.zip'], split="train[:10]")
|
| 22 |
+
|
| 23 |
+
rectangle = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['rectangle/rectangle_test.zip'], split="train[:10]")
|
| 24 |
+
|
| 25 |
+
shapes = {
|
| 26 |
+
"circle": circle,
|
| 27 |
+
"crescent": crescent,
|
| 28 |
+
"ellipse": ellipse,
|
| 29 |
+
"peanut": peanut,
|
| 30 |
+
"triangle": triangle,
|
| 31 |
+
"rectangle": rectangle,
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
def randomize(selection):
|
| 35 |
+
index = random.randint(0, 9)
|
| 36 |
+
mask = 255*numpy.array(json.loads(shapes[selection]['Defects'][index]))
|
| 37 |
+
v = numpy.array(json.loads(shapes[selection]['Strain'][index]))
|
| 38 |
+
print(v)
|
| 39 |
+
# Get the color map by name:
|
| 40 |
+
cm = matplotlib.pyplot.get_cmap('RdBu')
|
| 41 |
+
measure = max(v.max(), -v.min())
|
| 42 |
+
print(measure)
|
| 43 |
+
output = (v / measure)
|
| 44 |
+
print(cm((numpy.multiply(output[:, :, 0], mask)+1.0)/2.0))
|
| 45 |
+
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)
|
| 46 |
+
|
| 47 |
+
with gradio.Blocks() as demo:
|
| 48 |
+
selection = gradio.Dropdown(["circle", "crescent", "ellipse", "peanut", "triangle", "rectangle"], label="Select defect shape")
|
| 49 |
+
with gradio.Row():
|
| 50 |
+
with gradio.Column(label="Defects"):
|
| 51 |
+
mask = gradio.Image()
|
| 52 |
+
with gradio.Column(label="ε-xx"):
|
| 53 |
+
exx = gradio.Image()
|
| 54 |
+
with gradio.Row():
|
| 55 |
+
with gradio.Column():
|
| 56 |
+
eyy = gradio.Image(label="ε-yy")
|
| 57 |
+
with gradio.Column():
|
| 58 |
+
exy = gradio.Image(label="ε-xy")
|
| 59 |
+
selection.change(fn=randomize, inputs=[selection], outputs=[mask, exx, eyy, exy])
|
| 60 |
+
demo.launch(debug=True)
|