diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test/arduino_backend_test.py b/test/arduino_backend_test.py new file mode 100644 index 0000000..d8cdbc0 --- /dev/null +++ b/test/arduino_backend_test.py @@ -0,0 +1,88 @@ +import unittest +import serial +import numpy as np +import argparse +import sys +import tensorflow as tf + +class TestArduinoBackend(unittest.TestCase): + + inputs = [] + arduino = None + precision = 8 + modelPath = '../diabetes_model.h5' + + def __init__(self, testname, com, baud, model): + super(TestArduinoBackend, self).__init__(testname) + self.arduino = serial.Serial(com,baud, timeout=5) + self.modelPath = model + + def setUp(self): + self.inputs.append('6,148,72,35,0,33.6,0.627,50') #1 + self.inputs.append('10,168,74,0,0,38,0.537,34') #1 + self.inputs.append('0,101,65,28,0,24.6,0.237,22') #0 + self.inputs.append('4,97,60,23,0,28.2,0.443,22') #0 + self.inputs.append('7,125,86,0,0,37.6,0.304,51') #0 + self.inputs.append('11,120,80,37,150,42.3,0.785,48') #1 + return super().setUp() + + def tearDown(self): + return super().tearDown() + + def test_dense_results(self): + results_arduino = [] + + for input_values in self.inputs: + self.arduino.readline() + self.arduino.write(input_values.encode()) + self.arduino.readline() + result = self.arduino.readline() + self.arduino.readline() + self.arduino.readline() + results_arduino.append(result.decode().rstrip('\r\n')) + + results_framework = [] + model = tf.keras.models.load_model(self.modelPath) + + for input_values in self.inputs: + array = str(input_values).split(',') + array = np.array(array).reshape(1,len(array)) + results_framework.append(model.predict(array.astype(np.float))) + + for i in range(0, len(results_arduino)): + counter = 0 + dot_index = 0 + for digit in str(float(results_framework[i][0][0])): + if (digit=='.'): + dot_index=counter + if (digit==str(float(results_arduino[i]))[counter]): + counter=counter+1 + else: + break + + if ((counter-dot_index +1)