Spaces:
Runtime error
Runtime error
| 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) |