/*
* Imagimob Studio 5.1.1876.65534+49e6155340215ca3d06c12b18af3707d859c2860
* Copyright © 2023- Imagimob AB, All Rights Reserved.
* 
* Generated at 08/22/2024 14:28:19 UTC. Any changes will be lost.
* 
* Model ID  5ffb4d7f-db2a-4ff2-a357-fea63b96bc4d
* 
* Memory    Size                      Efficiency
* Buffers   92 bytes (RAM)            87 %
* State     220 bytes (RAM)           100 %
* Readonly  428 bytes (Flash)         100 %
* 
* Backend              tensorflow
* Keras Version        2.15.0
* Backend Model Type   Sequential
* 
* Class Index | Symbol Label
* 0           | unlabelled
* 1           | touch
* 
* Layer                          Shape           Type       Function
* Sliding Window (data points)   [3]             float      dequeue
*    window_shape = [3]
*    stride = 1
*    buffer_multiplier = 1
* Input Layer                    [3]             float      dequeue
*    shape = [3]
* Dense                          [10]            float      dequeue
*    units = 10
*    use_bias = True
*    activation = relu
*    trainable = True
*    weight = float[3,10]
*    bias = float[10]
* Dense                          [5]             float      dequeue
*    units = 5
*    use_bias = True
*    activation = relu
*    trainable = True
*    weight = float[10,5]
*    bias = float[5]
* Dense                          [2]             float      dequeue
*    units = 2
*    use_bias = True
*    activation = softmax
*    trainable = True
*    weight = float[5,2]
*    bias = float[2]
* 
* Exported functions:
* 
* int BTN_NN_dequeue(float *restrict data_out)
*    Description: Dequeue features. RET_SUCCESS (0) on success, RET_NODATA (-1) if no data is available, RET_NOMEM (-2) on internal memory error
*    Parameter data_out is Output of size float[2].
* 
* int BTN_NN_enqueue(const float *restrict data_in)
*    Description: Enqueue features. Returns SUCCESS (0) on success, else RET_NOMEM (-2) when low on memory.
*    Parameter data_in is Input of size float[1].
* 
* void BTN_NN_init(void)
*    Description: Initializes buffers to initial state. This function also works as a reset function.
* 
* 
* Disclaimer:
*   The generated code relies on the optimizations done by the C compiler.
*   For example many for-loops of length 1 must be removed by the optimizer.
*   This can only be done if the functions are inlined and simplified.
*   Check disassembly if unsure.
*   tl;dr Compile using gcc with -O3 or -Ofast
*/

#ifndef _BTN_NN_BUTTON_NN_H_
#define _BTN_NN_BUTTON_NN_H_
#ifdef _MSC_VER
#pragma once
#endif

#include <stdint.h>

typedef struct {    
    char *name;
    double TP; // True Positive or Correct Positive Prediction
    double FN; // False Negative or Incorrect Negative Prediction
    double FP; // False Positive or Incorrect Positive Prediction
    double TN; // True Negative or Correct Negative Prediction
    double TPR; // True Positive Rate or Sensitivity, Recall
    double TNR; // True Negative Rate or Specificity, Selectivity
    double PPV; // Positive Predictive Value or Precision
    double NPV; // Negative Predictive Value
    double FNR; // False Negative Rate or Miss Rate
    double FPR; // False Positive Rate or Fall-Out
    double FDR; // False Discovery Rate
    double FOR; // False Omission Rate
    double F1S; // F1 Score
} BTN_NN_stats;

/*
* Tensorflow Test Set
* 
* (ACC) Accuracy 99.335 %
* (F1S) F1 Score 99.337 %
* 
* Name of class                                            (unlabelled)            touch
* (TP) True Positive or Correct Positive Prediction               77066            31536
* (FN) False Negative or Incorrect Negative Prediction              685               42
* (FP) False Positive or Incorrect Positive Prediction               42              685
* (TN) True Negative or Correct Negative Prediction               31536            77066
* (TPR) True Positive Rate or Sensitivity, Recall               99.12 %          99.87 %
* (TNR) True Negative Rate or Specificity, Selectivity          99.87 %          99.12 %
* (PPV) Positive Predictive Value or Precision                  99.95 %          97.87 %
* (NPV) Negative Predictive Value                               97.87 %          99.95 %
* (FNR) False Negative Rate or Miss Rate                         0.88 %           0.13 %
* (FPR) False Positive Rate or Fall-Out                          0.13 %           0.88 %
* (FDR) False Discovery Rate                                     0.05 %           2.13 %
* (FOR) False Omission Rate                                      2.13 %           0.05 %
* (F1S) F1 Score                                                99.53 %          98.86 %
*/


#define BTN_NN_TEST_AVG_ACC 0.9933503462027459 // Accuracy
#define BTN_NN_TEST_AVG_F1S 0.9933700503360198 // F1 Score

#define BTN_NN_TEST_STATS { \
 {name: "(unlabelled)", TP: 77066, FN: 685, FP: 42, TN: 31536, TPR: 0.9911898239250, TNR: 0.9986699600988, PPV: 0.9994553094361, NPV: 0.9787405729182, FNR: 0.0088101760749, FPR: 0.0013300399011, FDR: 0.0005446905638, FOR: 0.0212594270817, F1S: 0.9953054068539, }, \
 {name: "touch", TP: 31536, FN: 42, FP: 685, TN: 77066, TPR: 0.9986699600988, TNR: 0.9911898239250, PPV: 0.9787405729182, NPV: 0.9994553094361, FNR: 0.0013300399011, FPR: 0.0088101760749, FDR: 0.0212594270817, FOR: 0.0005446905638, F1S: 0.9886048370664, }, \
}

#ifdef BTN_NN_STATS_ENABLED
static const BTN_NN_stats BTN_NN_test_stats[] = BTN_NN_TEST_STATS;
#endif

/*
* Tensorflow Train Set
* 
* (ACC) Accuracy 98.169 %
* (F1S) F1 Score 98.192 %
* 
* Name of class                                            (unlabelled)            touch
* (TP) True Positive or Correct Positive Prediction               36935            11372
* (FN) False Negative or Incorrect Negative Prediction              877               24
* (FP) False Positive or Incorrect Positive Prediction               24              877
* (TN) True Negative or Correct Negative Prediction               11372            36935
* (TPR) True Positive Rate or Sensitivity, Recall               97.68 %          99.79 %
* (TNR) True Negative Rate or Specificity, Selectivity          99.79 %          97.68 %
* (PPV) Positive Predictive Value or Precision                  99.94 %          92.84 %
* (NPV) Negative Predictive Value                               92.84 %          99.94 %
* (FNR) False Negative Rate or Miss Rate                         2.32 %           0.21 %
* (FPR) False Positive Rate or Fall-Out                          0.21 %           2.32 %
* (FDR) False Discovery Rate                                     0.06 %           7.16 %
* (FOR) False Omission Rate                                      7.16 %           0.06 %
* (F1S) F1 Score                                                98.79 %          96.19 %
*/


#define BTN_NN_TRAIN_AVG_ACC 0.9816899691107137 // Accuracy
#define BTN_NN_TRAIN_AVG_F1S 0.9819157969196116 // F1 Score

#define BTN_NN_TRAIN_STATS { \
 {name: "(unlabelled)", TP: 36935, FN: 877, FP: 24, TN: 11372, TPR: 0.9768063048767, TNR: 0.9978939978939, PPV: 0.9993506317811, NPV: 0.9284023185566, FNR: 0.0231936951232, FPR: 0.0021060021060, FDR: 0.0006493682188, FOR: 0.0715976814433, F1S: 0.9879498736141, }, \
 {name: "touch", TP: 11372, FN: 24, FP: 877, TN: 36935, TPR: 0.9978939978939, TNR: 0.9768063048767, PPV: 0.9284023185566, NPV: 0.9993506317811, FNR: 0.0021060021060, FPR: 0.0231936951232, FDR: 0.0715976814433, FOR: 0.0006493682188, F1S: 0.9618946923239, }, \
}

#ifdef BTN_NN_STATS_ENABLED
static const BTN_NN_stats BTN_NN_train_stats[] = BTN_NN_TRAIN_STATS;
#endif

/*
* Tensorflow Validation Set
* 
* (ACC) Accuracy 99.216 %
* (F1S) F1 Score 99.219 %
* 
* Name of class                                            (unlabelled)            touch
* (TP) True Positive or Correct Positive Prediction               40978            19800
* (FN) False Negative or Incorrect Negative Prediction              463               17
* (FP) False Positive or Incorrect Positive Prediction               17              463
* (TN) True Negative or Correct Negative Prediction               19800            40978
* (TPR) True Positive Rate or Sensitivity, Recall               98.88 %          99.91 %
* (TNR) True Negative Rate or Specificity, Selectivity          99.91 %          98.88 %
* (PPV) Positive Predictive Value or Precision                  99.96 %          97.72 %
* (NPV) Negative Predictive Value                               97.72 %          99.96 %
* (FNR) False Negative Rate or Miss Rate                         1.12 %           0.09 %
* (FPR) False Positive Rate or Fall-Out                          0.09 %           1.12 %
* (FDR) False Discovery Rate                                     0.04 %           2.28 %
* (FOR) False Omission Rate                                      2.28 %           0.04 %
* (F1S) F1 Score                                                99.42 %          98.80 %
*/


#define BTN_NN_VALIDATION_AVG_ACC 0.9921642887459597 // Accuracy
#define BTN_NN_VALIDATION_AVG_F1S 0.9921866890340132 // F1 Score

#define BTN_NN_VALIDATION_STATS { \
 {name: "(unlabelled)", TP: 40978, FN: 463, FP: 17, TN: 19800, TPR: 0.9888274896841, TNR: 0.9991421506787, PPV: 0.9995853152823, NPV: 0.9771504713023, FNR: 0.0111725103158, FPR: 0.0008578493212, FDR: 0.0004146847176, FOR: 0.0228495286976, F1S: 0.9941773011790, }, \
 {name: "touch", TP: 19800, FN: 17, FP: 463, TN: 40978, TPR: 0.9991421506787, TNR: 0.9888274896841, PPV: 0.9771504713023, NPV: 0.9995853152823, FNR: 0.0008578493212, FPR: 0.0111725103158, FDR: 0.0228495286976, FOR: 0.0004146847176, F1S: 0.9880239520958, }, \
}

#ifdef BTN_NN_STATS_ENABLED
static const BTN_NN_stats BTN_NN_validation_stats[] = BTN_NN_VALIDATION_STATS;
#endif

#define BTN_NN_API_QUEUE

// All symbols in order
#define BTN_NN_SYMBOL_MAP {"unlabelled", "touch"}

// Model GUID (16 bytes)
#define BTN_NN_MODEL_ID {0x7f, 0x4d, 0xfb, 0x5f, 0x2a, 0xdb, 0xf2, 0x4f, 0xa3, 0x57, 0xfe, 0xa6, 0x3b, 0x96, 0xbc, 0x4d}

// First nibble is bit encoding, second nibble is number of bytes
#define IMAGINET_TYPES_NONE	(0x0)
#define IMAGINET_TYPES_FLOAT32	(0x14)
#define IMAGINET_TYPES_FLOAT64	(0x18)
#define IMAGINET_TYPES_INT8	(0x21)
#define IMAGINET_TYPES_INT16	(0x22)
#define IMAGINET_TYPES_INT32	(0x24)
#define IMAGINET_TYPES_INT64	(0x28)
#define IMAGINET_TYPES_QDYN8	(0x31)
#define IMAGINET_TYPES_QDYN16	(0x32)
#define IMAGINET_TYPES_QDYN32	(0x34)

// data_in [1] (4 bytes)
#define BTN_NN_DATA_IN_COUNT (1)
#define BTN_NN_DATA_IN_TYPE float
#define BTN_NN_DATA_IN_TYPE_ID IMAGINET_TYPES_FLOAT32
#define BTN_NN_DATA_IN_SCALE (1)
#define BTN_NN_DATA_IN_OFFSET (0)
#define BTN_NN_DATA_IN_IS_QUANTIZED (0)

// data_out [2] (8 bytes)
#define BTN_NN_DATA_OUT_COUNT (2)
#define BTN_NN_DATA_OUT_TYPE float
#define BTN_NN_DATA_OUT_TYPE_ID IMAGINET_TYPES_FLOAT32
#define BTN_NN_DATA_OUT_SCALE (1)
#define BTN_NN_DATA_OUT_OFFSET (0)
#define BTN_NN_DATA_OUT_IS_QUANTIZED (0)

#define BTN_NN_KEY_MAX (18)



// Return codes
#define BTN_NN_RET_SUCCESS 0
#define BTN_NN_RET_NODATA -1
#define BTN_NN_RET_NOMEM -2

// Exported methods
int BTN_NN_dequeue(float *restrict data_out);
int BTN_NN_enqueue(const float *restrict data_in);
void BTN_NN_init(void);

#endif /* _BTN_NN_BUTTON_NN_H_ */
