failureDetectorSCU 4.0.1
Package used to run Failure Detection model for SCU-Boeing 787 components
Requires Python: >=3.8

# Failure Detector for Supplement Cooling Units - Boeing 787

## 1. Introduction

The failureDetectorSCU package (https://pypi.org/project/failureDetectorSCU) contains a set of methods that allows Technical IT Staff execute the failure detection model. It has been developed at the University of Coimbra under the Work Package 5 from ReMAP project.  

Detection a failure on whatever Supplement Cooling Unit (SCU) of Integrated Cooling System (ICS) form Boeing 787 Series is considered as a binary classification, where 0 refers to a Healthy State while 1 refers to a Failure State. The model requires at least 120 measurements (rows) to predict the health state of whatever SCU at the last 2 minutes. The algorithm does not have an upper threshold for number of measurements because it considers that the input measurements could correspond to two or more adjacent flights. However, it is recommended to provide a short period of time to avoid computation overhead.    

The Deep Learning failure detector model loaded through the package methods was developed in Python 3.5+ and Tensorflow2.2+ open-source library.  Therefore, using and configuring GPU (Graphical Processing Unit) is highly recommended to reduce inference times. More details about the installation packages are detailed at Section 2. The methods provided to Technical IT Staff as well as the input and output parameters are described in Section 3.

## 2.	Installation
The failureDetectorSCU package has been developed in Python 3.5+, but it is highly recommended use it under Python3.8+ environments.  A python module has been uploaded to PYPI (Python Package Index), which means that it is easy to install via pip (Python default package). 

Besides [Python](https://www.python.org/downloads/release/python-385/) and [pip](https://pypi.org/project/pip/), failureDetectorSCU requires the installation of some packages described in Section 2.1. Please, avoid dependence conflicts if other versions are previously installed. Note that it does not remain a problem if you are developing a docker container such in ReMAP platform runner environment.  

### 2.1.	Requirements
When the wheel (.whl file) was constructed, the following packages were defined in setup.py file: 
- Keras (2.6.0+)
  - Open-source software library that provides a Python interface for artificial neural networks. 
- Tensorflow (2.6+)
  - Open-source software library for machine learning and artificial intelligence.
- H5py (2.10.0+)
  - Pythonic interface to the HDF5 binary data format.
- Pandas (1.15.0+)
  - Fast, powerful, flexible and easy to use open-source data analysis and manipulation tool, built on top of the Python programming language.
### 2.2.	Recommended installation process
Considering that we have a myenv anaconda environment with Python3.8+, the following commands are provided to install failureDetectorSCU on linux distributions.
- Create and activate environment
  - $ conda create -n “myenv” python=3.8  
  - $ source activate myenv 
- Install pip
  - (myenv): apt install python3-pip --upgrade
- Install Tensorflow
  - (myenv): pip install tensorflow
  - Note: Now, Tensorflow=2.6 is installed under python3.8 environments, which also includes Keras=2.6.0
- Install h5py
  - (myenv): pip install h5py==2.10.0
- Install pandas
  - (myenv): pip install pandas
- Install the last version of failureDetectorSCU 
  - (myenv): pip install failureDetectorSCU –upgrade
- To test the installation, create a python file example.py and run it.
	- (myenv): nano example.py
- Import and call one of the methods described in Section 3.
 ```python
from failureDetectorSCU import loadModel
loadModel()
```   
    
- Run example.py
	- (myenv): python example.py

Note: In any error appears, failureDetectorSCU package is ready to be used. 

## 3.	Methods, Inputs and Outputs
The package’s methods require the names of the columns (6 SCU sensors and 3 informative measurements) which were defined when the metadata of Integrated Cooling System (ICS) was stored on ReMAP node.  Despite 9 column names are used as input to predict failures, only the columns which refer to SCU sensors are used to remove noisy data. Considering non anonymized names, the following lists are considered: 
-	All the column names given by reading ICS metadata
 ```python
metadata= ['flightPhase','altitude','airspeed','Liquid Level','Amplitude Peak',
          'Inlet Pressure','Inlet Temperature','Outlet Pressure','Max Frequency']
```  
-	The sensors per SCU which could contain noisy data
 ```python
noisy_sensors= ['Liquid Level','Amplitude Peak','Inlet Pressure',
              'Inlet Temperature','Outlet Pressure','Max Frequency']
```  
### 3.1.	Methods
It is assumed that data comes from ReMAP node by using ReMAP SDK package. The  ``` getDataset() ```  and ``` readMetadata() ```  methods of ReMAP SDK allow Thechnical IT Staff to obtain an initial DataFrame object, which is called data_table in this example.   
Note: ``` getDataset() ``` could obtain measurements from more than one flight, that does not remain a concern for failureDetectorSCU because only the measurements from the last two minutes are considered into account to obtain an output. 

-	``` preProcessingData(data_table,noisy_sensors) ```
    -	This method cleans the noisy measurements of the data by defining the sensor which could present wrong data. The data cleaning process is performed by applying a linear interpolation method and determined upper and lower thresholds. This data cleaning approach is also used for Infinite and NaN values.  
 After data cleaning, flight phases are aggregated. Then, a normalization per flight phase is also performed. This method returns a processed data_table which we will call data_pre_processed for illustrative purposes. 
- ```	processData(data_pre_processed,metadata) ```
    - This method reshapes the data preprocessed into three dimensions required to apply convolutional operations. It is important to mention that the reshaping process requires of the definition of the name of the   columns (metadata).
-	``` loadModel() ```
    - This method simply reconstructs the Deep Learning model and load its weights to return a Keras model ready to be used. 
-	``` predict(model,data_processed) ```
    - To obtain the health state of the last two minutes of a given period and the corresponding loss, this method requires of the loaded model and the reshaped data.

#### OUTPUTS

Considering a threshold of 0.5 for the binary classification, this method returns 0 for a Healthy State or 1 for a Failure State. Of course, the loss is also returned for illustrative purposes. 

Considering a dataset locally stored, a briew example could be:
 ```python
from failureDetectorSCU import preProcessingData
from failureDetectorSCU import processData
from failureDetectorSCU import loadModel
from failureDetectorSCU import predict
import pandas as pd

metadata= ['flightPhase','altitude','airspeed','Liquid Level','Amplitude Peak',
        'Inlet Pressure','Inlet Temperature','Outlet Pressure','Max Frequency']
noisy_sensors= metadata[3:8]

data_table = pd.read_csv('dataset.csv')

healthState, loss = predict(loadModel(),processData(
          preProcessingData(data_table,noisy_sensors),metadata))
print("Health State: ", healthState)
print("Loss: ", loss)
```