CoolFace
Apppublic

cmudrc/microstructure-data-explorer

sourceHugging Facemitupdated 4y agoView on Hugging Face
2likes
app.py71 linesDownload Raw Back to root
1import datasets2import random3import numpy4import json5import gradio6import matplotlib.pyplot # for colormap7import matplotlib.colors # for color conversion8 9vr = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['variable_radius.zip'], split='train')10 11vn = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['variable_number.zip'], split='train')12 13circle = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['circle/circle_test.zip'], split="train[:10]")14 15crescent = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['crescent/crescent_test.zip'], split="train[:10]")16 17peanut = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['peanut/peanut_test.zip'], split="train[:10]")18 19ellipse = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['ellipse/ellipse_test.zip'], split="train[:10]")20 21triangle = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['triangle/triangle_test.zip'], split="train[:10]")22 23rectangle = datasets.load_dataset("cmudrc/porous-microstructure-strain-fields", data_files=['rectangle/rectangle_test.zip'], split="train[:10]")24 25shapes = {26    "circle": circle,27    "crescent": crescent,28    "ellipse": ellipse,29    "peanut": peanut,30    "triangle": triangle,31    "rectangle": rectangle,32}33 34 35 36def randomize(selection):37    index = random.randint(0, 9)38    mask = 255*numpy.array(json.loads(shapes[selection]['Defects'][index]))39    v = numpy.array(json.loads(shapes[selection]['Strain'][index]))40    # Get the color map by name:41    cm = matplotlib.pyplot.get_cmap('RdBu')42    measure = max(v.max(), -v.min())43    output = (v / measure)44 45    legend = "<h2>Strain</h2><table style=\"width:100%\"><tr>"46    for i in range(11):47        color = cm(i/10.0)[:3]48        value = -measure + i*2*measure/1049        print(sum(list(color)))50        hex = matplotlib.colors.to_hex(list(color))51        text_color = "black" if sum(list(color)) > 2.0 else "white"52        legend = legend + f"<td style=\"background-color: {hex}; color: {text_color}\">{value:+.2e}</td>"53    legend = legend + "</tr></table>"54    55    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), legend56 57with gradio.Blocks() as demo:58    selection = gradio.Dropdown(["circle", "crescent", "ellipse", "peanut", "triangle", "rectangle"], label="Select defect shape")59    with gradio.Row():60        with gradio.Column(label="Defects"):61            mask = gradio.Image()62        with gradio.Column(label="ε-xx"):63            exx = gradio.Image()64    with gradio.Row():65        with gradio.Column():66            eyy = gradio.Image(label="ε-yy")67        with gradio.Column():68            exy = gradio.Image(label="ε-xy")69    ht = gradio.HTML(label="", value="")70    selection.change(fn=randomize, inputs=[selection], outputs=[mask, exx, eyy, exy, ht])71demo.launch(debug=True)