Spaces:
Runtime error
Runtime error
Commit
·
6b0ebb3
1
Parent(s):
331e426
Upload app.py
Browse files
app.py
CHANGED
@@ -1,68 +1,58 @@
|
|
1 |
-
import numpy as np
|
2 |
-
import gradio as gr
|
3 |
-
import tensorflow as tf
|
4 |
-
import matplotlib.pyplot as plt
|
5 |
-
from matplotlib import cm
|
6 |
-
from PIL import Image
|
7 |
-
import pandas as pd
|
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 |
-
return pred, df, im
|
59 |
-
|
60 |
-
iface = gr.Interface(
|
61 |
-
fn=predict,
|
62 |
-
inputs=[gr.inputs.Radio(["Cell #1", "Cell #2", "Cell #3"]), gr.inputs.Slider(1, 1000, step=1), gr.inputs.Radio(["10", "50", "100", "200", "400", "1000"]), "checkbox"],
|
63 |
-
title="LFP degradation diagnosis",
|
64 |
-
description="Enter cell number, duty cycle and cycle number to predict the percentage of LLI, LAMPE and LAMNE",
|
65 |
-
outputs=[gr.outputs.Label(label="Prediction"), gr.outputs.Timeseries(x=" ", y=["pristine", "degraded"]), gr.outputs.Image(type='pil', label="DTW image")],
|
66 |
-
allow_screenshot=False,
|
67 |
-
layout="unaligned")
|
68 |
iface.launch(share=True)
|
|
|
1 |
+
import numpy as np
|
2 |
+
import gradio as gr
|
3 |
+
import tensorflow as tf
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
from matplotlib import cm
|
6 |
+
from PIL import Image
|
7 |
+
import pandas as pd
|
8 |
+
from dtaidistance import dtw
|
9 |
+
|
10 |
+
def getDTWImage(IC_reference, sample, size):
|
11 |
+
d, paths = dtw.warping_paths(IC_reference, sample, window=int(size/2), psi=2)
|
12 |
+
x = np.array(paths)
|
13 |
+
# mask values that are not filled
|
14 |
+
x = np.where(x == np.inf, -99, x)
|
15 |
+
# negative values are replaced by 0
|
16 |
+
x = np.where(x < 0, 0, x)
|
17 |
+
# normalise values
|
18 |
+
x = x/np.max(x)
|
19 |
+
# reshape the array
|
20 |
+
x = np.expand_dims(x, -1).astype("float32")
|
21 |
+
return x
|
22 |
+
|
23 |
+
data = np.load('./data_LFP.npy')
|
24 |
+
model = tf.keras.models.load_model('./models/model-bestLFP_V2.h5',compile = False)
|
25 |
+
|
26 |
+
def predict(Cell_number, Duty_Cycle, Cycle_number):
|
27 |
+
# ------------------------ Prediction ------------------------
|
28 |
+
# select cell data
|
29 |
+
# data = x_test_1 #if Cell_number == '1' else x_test_2 if Cell_number == '2' else x_test_3
|
30 |
+
# data_DTW = x_test_DTW_1 #if Cell_number == '1' else x_test_DTW_2 if Cell_number == '2' else x_test_DTW_3
|
31 |
+
# select cycle number
|
32 |
+
cycle = 0 if Cycle_number == '10' else 1 if Cycle_number == '50' else 2 if Cycle_number == '100' else 3 if Cycle_number == '200'else 4 if Cycle_number == '400' else 5
|
33 |
+
|
34 |
+
IC_reference = data[0][0]
|
35 |
+
sample = data[Duty_Cycle-1][cycle]
|
36 |
+
sample_DTW = getDTWImage(IC_reference, sample, size)
|
37 |
+
prediction = model.predict(np.expand_dims(sample_DTW, axis=0))
|
38 |
+
pred = {"LLI ": str(prediction[0][0]), "LAMPE ": str(prediction[0][1]), "LAMNE ": str(prediction[0][2])}
|
39 |
+
|
40 |
+
# --------------------------- ICA + image----------------------------
|
41 |
+
d = {' ': np.linspace(1, len(ICA_reference), len(ICA_reference)), 'pristine': ICA_reference, 'degraded': sample}
|
42 |
+
df = pd.DataFrame(data=d)
|
43 |
+
|
44 |
+
image_array=sample_DTW.reshape(sample_DTW.shape[0], sample_DTW.shape[1])
|
45 |
+
image_array = normalise_data(image_array, np.min(image_array), np.max(image_array))
|
46 |
+
im = Image.fromarray(np.uint8(cm.inferno(image_array)*255))
|
47 |
+
|
48 |
+
return pred, df, im
|
49 |
+
|
50 |
+
iface = gr.Interface(
|
51 |
+
fn=predict,
|
52 |
+
inputs=[gr.inputs.Radio(["Cell #1", "Cell #2", "Cell #3"]), gr.inputs.Slider(1, 1000, step=1), gr.inputs.Radio(["10", "50", "100", "200", "400", "1000"]), "checkbox"],
|
53 |
+
title="LFP degradation diagnosis",
|
54 |
+
description="Enter cell number, duty cycle and cycle number to predict the percentage of LLI, LAMPE and LAMNE",
|
55 |
+
outputs=[gr.outputs.Label(label="Prediction"), gr.outputs.Timeseries(x=" ", y=["pristine", "degraded"]), gr.outputs.Image(type='pil', label="DTW image")],
|
56 |
+
allow_screenshot=False,
|
57 |
+
layout="unaligned")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
iface.launch(share=True)
|