File size: 3,094 Bytes
6abed4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08c0adb
 
6abed4d
 
 
 
 
 
 
 
08c0adb
 
 
 
 
 
 
 
 
 
 
 
6abed4d
 
 
 
 
 
 
 
 
 
 
 
 
4205634
 
6abed4d
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
70
71
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]))
    # Get the color map by name:
    cm = matplotlib.pyplot.get_cmap('RdBu')
    measure = max(v.max(), -v.min())
    output = (v / measure)

    legend = "<h2>Strain</h2><table style=\"width:100%\"><tr>"
    for i in range(11):
        color = cm(i/10.0)[:3]
        value = -measure + i*2*measure/10
        print(sum(list(color)))
        hex = matplotlib.colors.to_hex(list(color))
        text_color = "black" if sum(list(color)) > 2.0 else "white"
        legend = legend + f"<td style=\"background-color: {hex}; color: {text_color}\">{value:+.2e}</td>"
    legend = legend + "</tr></table>"
    
    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), legend

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")
    ht = gradio.HTML(label="", value="")
    selection.change(fn=randomize, inputs=[selection], outputs=[mask, exx, eyy, exy, ht])
demo.launch(debug=True)