NahuelCosta commited on
Commit
6b0ebb3
·
1 Parent(s): 331e426

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -67
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
- def normalise_data(data, min_val, max_val, low=0, high=1):
10
- '''
11
- Normalises the data to the range [low, high]
12
-
13
- Parameters
14
- ----------
15
- data: numpy array, data to normalise
16
- low: float, minimum value of the range
17
- high: float, maximum value of the range
18
-
19
- Returns
20
- -------
21
- normalised_data: float, normalised data
22
- '''
23
- normalised_data = (data - min_val)/(max_val - min_val)
24
- normalised_data = (high - low)*normalised_data + low
25
- return normalised_data
26
-
27
- x_test_1 = np.load('./data/x_test_1_LFP.npy')
28
- x_test_1 = normalise_data(x_test_1, 0, 1, 0, 100)
29
- x_test_1 = x_test_1.reshape(-1, 6, x_test_1.shape[1])
30
-
31
- x_test_DTW_1 = np.load('./data/x_test_DTW_1_LFP.npy')
32
- x_test_DTW_1 = x_test_DTW_1.reshape(-1, 6, x_test_DTW_1.shape[1], x_test_DTW_1.shape[2], x_test_DTW_1.shape[3])
33
-
34
- model = tf.keras.models.load_model('./models/model-bestLFP_V2.h5',compile = False)
35
-
36
- def predict(Cell_number, Duty_Cycle, Cycle_number):
37
- # ------------------------ Prediction ------------------------
38
- # select cell data
39
- data = x_test_1 #if Cell_number == '1' else x_test_2 if Cell_number == '2' else x_test_3
40
- data_DTW = x_test_DTW_1 #if Cell_number == '1' else x_test_DTW_2 if Cell_number == '2' else x_test_DTW_3
41
- # select cycle number
42
- 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
43
-
44
- sample = data[Duty_Cycle-1][cycle]
45
- sample_DTW = data_DTW[Duty_Cycle-1][cycle]
46
- prediction = model.predict(np.expand_dims(sample_DTW, axis=0))
47
- pred = {"LLI ": str(prediction[0][0]), "LAMPE ": str(prediction[0][1]), "LAMNE ": str(prediction[0][2])}
48
- # --------------------------- ICA + image----------------------------
49
-
50
- ICA_reference = x_test_1[0][0]
51
- d = {' ': np.linspace(1, len(ICA_reference), len(ICA_reference)), 'pristine': ICA_reference, 'degraded': sample}
52
- df = pd.DataFrame(data=d)
53
-
54
- image_array=sample_DTW.reshape(sample_DTW.shape[0], sample_DTW.shape[1])
55
- image_array = normalise_data(image_array, np.min(image_array), np.max(image_array))
56
- im = Image.fromarray(np.uint8(cm.inferno(image_array)*255))
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)