Version and change log Due Date : Friday 29th October 5pm AEST 1 Introduction Automatic detection of faults can be found in many engineering systems. There are systems to automatically diagnose faults...

1 answer below »
Python Programming Assignment


Version and change log Due Date : Friday 29th October 5pm AEST 1 Introduction Automatic detection of faults can be found in many engineering systems. There are systems to automatically diagnose faults in engines, chemical plants, photovoltaic plants, manufacturing processes and on on. This assignment is inspired by a fault detection system in a water treatment plant [1]. There are two main types of water treatment plants. One is to produce potable water (colloquially known as drinking water) and the other is to treat wastewater before discharging it into the natural environment. In both cases, water quality is an important issue. Therefore, there are instruments in the plants to constantly measure water quality and there are computers to process the collected data to check whether the plant is working correctly. If a fault has been developed, then the plant technicians should be alerted automatically so that they can fix the faults as soon as possible. In this assignment, you will write Python programs to perform fault detection. The aims of your program is to process data sequences to determine whether there are faults, the type of faults and when the fault is detected. Note that we chose the word inspired earlier because we have adapted the fault detection problem in [1] as a programming assignment by simplifying and liberally changing a few aspects of the original problem. In particular, we have made changes so that, in this assignment, you will have to use the various Python constructs that you have learnt. This means a few details of this assignment may not be realistic in engineering terms, but on the whole, you will still get a taste on how programming can be used to perform automatic fault detection. 1.1 Learning objectives By completing this assignment, you will learn: · To apply basic programming concepts of variable declaration, assignment, conditional, functions, loops and import. · To use the Python data types: list, float, int and Boolean · To translate an algorithm described in a natural language to a computer language. · To organize programs into modules by using functions · To use good program style including comments and documentation · To get a practice on software development, which includes incremental development, testing and debugging. 1.2 Prohibition For this assignment, the only external Python library that you are allowed to use is the math library. Any other libraries, such as numpy , scipy etc., are not allowed. This is an individual assignment, so no group work. 2 Intuition behind the fault detection algorithm The reference [1] uses a fault detection algorithm called CUSUM and you will be programming this algorithm in the assignment. We want to give you some intuition behind CUSUM. The name CUSUM is short for cumulative sum because the algorithm does the action of accumulating, as you will see in a moment. Figures 1a and 1b plot two signals (or data sequences) that you will be using in your testing. These two signals may look the same but they are actually slightly different. In fact, some of the data in one signal have slightly higher values because of a fault. Since the difference is small, it is not possible to use only one data point to decide whether there is a fault. However, each data point is important. Although a data point may only give us a tiny amount of evidence on whether there is a fault or not, what we can do is to accumulate the evidence from many data points. If in the end, the cumulative evidence that there is a fault is large, then we can be confident that a fault has in fact been developed. The blue curves in Figures 1c and 1d show the cumulative evidence of whether there is a fault in the signals in, respectively, Figures 1a and 1b. The red horizontal dashed lines can be thought of as the threshold of evidence that we need to decide whether there is a fault. If the cumulative evidence goes above the threshold, as in Figure 1c, then we say that a fault has occurred. If the cumulative evidence stays below the threshold, as in Figure 1d, then a fault has not been detected. The computer programs that you will write in this assignment will enable you to calculate the cumulative evidence. Fig 1a. The signal used in the test case 0 of the test file test_fault_detection_main_1.py    Fig 1b. The signal used in the test case 1 of the test file test_fault_detection_main_1.py   Fig 1c. The CUSUM of the signal in Fig. 1a. CUSUM can be thought of as cumulative evidence that a fault has occurred. Fig 1d. The CUSUM of the signal in Fig. 1b. CUSUM can be thought of as cumulative evidence that a fault has occurred. 3 Requirements for fault detection This section describes the requirements on the fault detection algorithm that you will be programming in this assignment. You should be able to implement these requirements by using only the Python skills that you have learnt in the first four weeks' of the lectures in this course. We begin with describing the data that the algorithm will operate on. We will use the following Python code as an example. In the following, we will refer to the following code as the sample code. Note that the data and parameter values in the sample code are for illustration only; your code should work with any valid input data and parameter values. # %% Test data and parameters # The conc signal conc_signal = [6.55, 6.40, 6.76, 6.45, 6.87, 6.92, 6.45, 7.36, 7.41, 6.67] # Parameters used to detect whether there is a fault in the conc_signal conc_range = [6.1, 6.9] conc_var = 0.5 conc_cusum_limit = 1.55 # The flow_signal flow_signal = [22.94, 23.07, 23.97, 23.9, 24.05, 23.65, 23.78, 23.78, 23.94, 23.91] # The parameters used to detect whether there is a fault in the flow_signal flow_range = [22.5, 23.5] flow_control_window = 3 # %% Call the fault detection function import detect_fault_main as df fault_type_your = df.detect_fault_main(conc_signal,conc_range,conc_var, conc_cusum_limit,flow_signal,flow_range,flow_control_window) In the sample code, there are two data series which contain, respectively, concentration and flow measurements. Both series are Python lists, and their variable names are conc_signal and flow_signal . In addition to the two data series, your programs will make use of five algorithmic parameters with the names conc_range , conc_var , conc_cusum_limit , flow_range , and flow_control_window . We will introduce these parameters when we describe the algorithm. We break the algorithm down into a number of steps. The first step is to compute what we called “a tiny amount of evidence” in Section 2. Formally speaking, “a tiny amount of evidence” is referring to log-probability ratio, which we will shorten to log-prob ratio. You do not need to know what log-prob ratio to complete this assignment but an explanation is in Section 12 which you can read at your own time later on. 3.1 Computing the log-prob ratio for one concentration measurement Let  be a concentration measurement. We calculate the log-prob ratio of  using the formula: where ,  and  are three parameters. We will use an example to illustrate how you can use this formula. This example uses values from the sample code. Let  be the first entry in conc_signal which means . The  and  values come from conc_range , we have  and . The value of  is  which comes from conc_var . Substituting these values into (1), the log-prob ratio is . You may wonder why we use the term log-prob ratio but you do not see any logarithm in the formula in (1), this is because the logarithm has been cancelled out when (1) is derived. 3.2 Computing the log-prob ratio for conc_signal Section 3.1 explains how the log-prob ratio for one measurement in conc_signal can be computed. The same procedure can be applied to each measurement in conc_signal to compute the log-prob ratios for all the entries in conc_signal . We will use the Python variable conc_lpr to refer to this result. For the sample code, the conc_lpr is expected to be: [0.08, -0.16, 0.416, -0.08, 0.592, 0.672, -0.08, 1.376, 1.456, 0.272] 3.3 Computing the CUSUM The CUSUM is computed after conc_lpr has been calculated. The CUSUM is a sequence of numbers (or list) which has the same number of entries as conc_lpr . We will use conc_cusum to denote this list. The first entry of conc_cusum is related to the first entry of conc_lpr by the following relationship: For entries in conc_cusum with indices k greater than or equal to 1, they are calculated using: For the sample code, we have calculated conc_lpr in Section 3.2. By using that conc_lpr , we can calculate conc_cusum should be: [0.08, 0, 0.416, 0.336, 0.928, 1.6, 1.52, 2.896, 4.352, 4.624] Note that the definition in (2) implies that if conc_lpr[0] is negative, then conc_cusum[0] will be zero. 3.4 Determining whether the concentration signal indicates a fault We will use conc_cusum (which can be thought of as the cumulative evidence that there is a fault) and the parameter conc_cusum_limit to determine whether there is a fault. We say the concentration measurements indicate that there is a fault if at least an entry in conc_cusum is greater than or equal to conc_cusum_limit . For the sample code, there is a fault. We are interested to determine two pieces of information from conc_cusum and conc_cusum_limit . First, we want to determine the time at which the fault is detected. We use the sample code to illustrate how this time is to be determines. In this example, the entries at indices 5, 7, 8 and 9 of conc_cusum are at or above conc_cusum_limit . The smallest of all these indices is 5 and we say that the fault is detected at time 5. Second, we want to determine the number of entries in conc_cusum that are at or above conc_cusum_limit . For the sample code, the answer is 4. The above example is for the case where there is a fault in conc_cusum . In the case where a fault is absent, we will assign math.inf (i.e. infinity in the math library) to the time at which fault is detected. The number of entries in conc_cusum that are at or above conc_cusum_limit should be zero. Note that Figures 1c and 1d in Section 2 plot the CUSUM. The red dashed lines in these figures indicate the level of conc_cusum_limit . 3.5 Classifying the type of fault If the concentration measurements indicate that there is a fault, we want to determine whether the fault is limited to the concentration only or whether there is also a fault in the flow. We will describe the procedure to determine whether there is a fault in the flow in two steps. In the first step, we compute the average of a number of consecutive entries in flow_signal . The number of entries to be used is specified by the parameter flow_control_window . Another requirement is that the last of these consecutive entries is at the index at which the concentration fault is detected. Let us use the sample code as an illustration. Since the value flow_control_window is 3, this means we will use 3 consecutive entries in flow_signal to compute the average. We know from Section 3.4 that the concentration fault is detected at index 5, so the three consecutive entries have indices 3, 4 and 5. Hence, we compute the average of 23.9, 24.05 and 23.65, which is 23.87. In the second step, we determine whether the average flow computed is within an acceptable range. We assume that the two numbers in flow_range form a closed interval, i.e. inclusive of the ends. If the average flow is inside the interval, then there are no faults in the flow; otherwise there is. In the sample
Answered 1 days AfterOct 23, 2021

Answer To: Version and change log Due Date : Friday 29th October 5pm AEST 1 Introduction Automatic detection of...

Dinesh answered on Oct 24 2021
129 Votes
document/assign1prelim-ytipxaae.zip
calc_log_prob_ratio.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ENGG1811 21T3 Assignment 1
Template file for calc_log_prob_ratio()
@author:
"""
def calc_log_prob_ratio(measurement, conc_range, conc_var):

__MACOSX/._calc_log_prob_ratio.py
calc_signal_log_prob_ratio.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ENGG1811 21T3 Assignment 1
Template file for calc_signal_log_prob_ratio()
@author:
"""
def calc_signal_log_prob_ratio(conc_signal,conc_range,conc_var):

import calc_log_prob_ratio as clpr


__MACOSX/._calc_signal_log_prob_ratio.py
classify_fault.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ENGG1811 21T3 Assignment 1
Template file for classify_fault()
@author:
"""
def classify_fault(conc_signal,conc_range,conc_var,conc_cusum_limit,
flow_signal,flow_range,flow_control_window):

import calc_signal_log_prob_ratio as cslpr
import cusum as cs
import detect_fault_in_conc as detect

__MACOSX/._classify_fault.py
conc_signal_0.txt
6.87
5.86
5.76
4.67
6.0
5.84
5.73
6.16
6.21
5.47
5.56
5.76
6.34
6.28
5.35
5.44
6.37
6.79
5.98
5.66
6.55
5.85
6.36
6.97
6.52
6.24
6.57
5.88
6.11
5.91
6.1
5.96
6.11
6.01
6.24
6.23
6.35
6.9
5.43
6.85
6.08
5.58
6.11
6.25
6.73
5.67
6.31
6.26
5.36
5.61
6.5
6.55
6.74
6.47
6.4
6.26
6.8
5.78
6.13
6.39
5.41
6.86
5.8
6.16
7.28
5.79
5.94
6.98
5.66
5.98
6.46
6.43
6.5
6.39
6.87
6.71
6.5
5.86
5.84
5.25
6.54
5.29
6.64
7.12
5.67
5.86
5.96
6.62
5.77
6.13
5.94
6.07
6.85
5.72
6.24
6.34
6.63
5.57
6.76
6.42
conc_signal_1.txt
6.87
5.86
5.76
4.67
6.0
5.84
5.73
6.16
6.21
5.47
5.56
5.76
6.34
6.28
5.35
5.44
6.37
6.79
5.98
5.66
6.55
5.85
6.36
6.77
6.32
6.04
6.37
5.68
5.91
5.71
5.9
5.76
5.91
5.81
6.04
6.03
6.15
6.7
5.23
6.65
5.88
5.38
5.91
6.05
6.53
5.47
6.11
6.06
5.16
5.41
6.3
6.35
6.54
6.27
6.2
6.06
6.6
5.58
5.93
6.19
5.21
6.66
5.6
5.96
7.08
5.59
5.74
6.78
5.46
5.78
6.26
6.23
6.3
6.19
6.67
6.51
6.3
5.66
5.64
5.05
6.34
5.09
6.44
6.92
5.47
5.66
5.76
6.42
5.57
5.93
5.74
5.87
6.65
5.52
6.04
6.14
6.43
5.37
6.56
6.22
conc_signal_2.txt
6.87
5.86
5.76
4.67
6.0
5.84
5.73
6.16
6.21
5.47
5.56
5.76
6.34
6.28
5.35
5.44
6.37
6.79
5.98
5.66
6.55
5.85
6.36
6.97
6.52
6.24
6.57
5.88
6.11
5.91
6.1
5.96
6.11
6.01
6.24
6.23
6.35
6.9
5.43
6.85
6.08
5.58
6.11
6.25
6.73
5.67
6.31
6.26
5.36
5.61
6.5
6.55
6.74
6.47
6.4
6.26
6.8
5.78
6.13
6.39
5.41
6.86
5.8
6.16
7.28
5.79
5.94
6.98
5.66
5.98
6.46
6.43
6.5
6.39
6.87
6.71
6.5
5.86
5.84
5.25
6.54
5.29
6.64
7.12
5.67
5.86
5.96
6.62
5.77
6
.13
5.94
6.07
6.85
5.72
6.24
6.34
6.63
5.57
6.76
6.42
conc_signal_cusum_0.txt
0.3479999999999988
0.29199999999999915
0.1959999999999994
0
0.0
0
0
0.06399999999999983
0.14799999999999952
0
0
0
0.13599999999999945
0.24799999999999917
0
0
0.14799999999999952
0.4639999999999984
0.45599999999999863
0.3199999999999992
0.5399999999999983
0.4799999999999983
0.6239999999999979
1.0119999999999965
1.2199999999999955
1.3159999999999952
1.5439999999999945
1.4959999999999947
1.5399999999999947
1.503999999999995
1.5439999999999947
1.5279999999999947
1.5719999999999947
1.5759999999999947
1.6719999999999944
1.7639999999999942
1.9039999999999937
2.2639999999999927
2.0359999999999934
2.375999999999992
2.407999999999992
2.2399999999999927
2.2839999999999927
2.3839999999999923
2.6759999999999913
2.5439999999999916
2.667999999999991
2.7719999999999905
2.5159999999999916
2.3599999999999923
2.5599999999999916
2.779999999999991
3.07599999999999
3.263999999999989
3.423999999999989
3.5279999999999885
3.8479999999999874
3.759999999999988
3.8119999999999874
3.9679999999999866
3.7319999999999878
4.075999999999986
3.9959999999999867
4.059999999999986
4.571999999999985
4.487999999999985
4.463999999999985
4.855999999999984
4.719999999999985
4.711999999999985
4.895999999999984
5.067999999999984
5.267999999999983
5.423999999999983
5.771999999999982
6.0559999999999805
6.25599999999998
6.19999999999998
6.13599999999998
5.835999999999981
6.05199999999998
5.767999999999981
6.0239999999999805
6.471999999999979
6.339999999999979
6.283999999999979
6.267999999999979
6.515999999999979
6.423999999999979
6.475999999999979
6.451999999999979
6.479999999999978
6.819999999999977
6.707999999999977
6.803999999999977
6.939999999999976
7.191999999999975
7.0199999999999765
7.323999999999975
7.491999999999974
conc_signal_cusum_1.txt
0.3479999999999988
0.29199999999999915
0.1959999999999994
0
0.0
0
0
0.06399999999999983
0.14799999999999952
0
0
0
0.13599999999999945
0.24799999999999917
0
0
0.14799999999999952
0.4639999999999984
0.45599999999999863
0.3199999999999992
0.5399999999999983
0.4799999999999983
0.6239999999999979
0.9319999999999966
1.0599999999999963
1.0759999999999963
1.2239999999999958
1.095999999999996
1.0599999999999963
0.9439999999999966
0.9039999999999969
0.8079999999999972
0.7719999999999974
0.6959999999999975
0.7119999999999975
0.7239999999999975
0.7839999999999975
1.0639999999999965
0.7559999999999978
1.015999999999997
0.9679999999999971
0.719999999999998
0.6839999999999982
0.7039999999999981
0.9159999999999975
0.7039999999999982
0.7479999999999981
0.7719999999999979
0.43599999999999917
0.20000000000000007
0.31999999999999956
0.4599999999999989
0.6759999999999982
0.7839999999999976
0.8639999999999974
0.8879999999999972
1.1279999999999961
0.9599999999999967
0.9319999999999967
1.0079999999999967
0.6919999999999977
0.9559999999999969
0.7959999999999973
0.7799999999999974
1.2119999999999957
1.0479999999999963
0.9439999999999967
1.2559999999999958
1.0399999999999965
0.9519999999999968
1.0559999999999965
1.1479999999999964
1.2679999999999958
1.3439999999999956
1.6119999999999948
1.815999999999994
1.9359999999999935
1.799999999999994
1.6559999999999944
1.2759999999999956
1.411999999999995
1.0479999999999963
1.2239999999999958
1.5919999999999943
1.379999999999995
1.2439999999999956
1.147999999999996
1.3159999999999954
1.1439999999999961
1.115999999999996
1.0119999999999965
0.9599999999999966
1.2199999999999958
1.0279999999999963
1.0439999999999963
1.0999999999999959
1.2719999999999951
1.019999999999996
1.243999999999995
1.3319999999999947
conc_signal_cusum_2.txt
0.3479999999999988
0.29199999999999915
0.1959999999999994
0
0.0
0
0
0.06399999999999983
0.14799999999999952
0
0
0
0.13599999999999945
0.24799999999999917
0
0
0.14799999999999952
0.4639999999999984
0.45599999999999863
0.3199999999999992
0.5399999999999983
0.4799999999999983
0.6239999999999979
1.0119999999999965
1.2199999999999955
1.3159999999999952
1.5439999999999945
1.4959999999999947
1.5399999999999947
1.503999999999995
1.5439999999999947
1.5279999999999947
1.5719999999999947
1.5759999999999947
1.6719999999999944
1.7639999999999942
1.9039999999999937
2.2639999999999927
2.0359999999999934
2.375999999999992
2.407999999999992
2.2399999999999927
2.2839999999999927
2.3839999999999923
2.6759999999999913
2.5439999999999916
2.667999999999991
2.7719999999999905
2.5159999999999916
2.3599999999999923
2.5599999999999916
2.779999999999991
3.07599999999999
3.263999999999989
3.423999999999989
3.5279999999999885
3.8479999999999874
3.759999999999988
3.8119999999999874
3.9679999999999866
3.7319999999999878
4.075999999999986
3.9959999999999867
4.059999999999986
4.571999999999985
4.487999999999985
4.463999999999985
4.855999999999984
4.719999999999985
4.711999999999985
4.895999999999984
5.067999999999984
5.267999999999983
5.423999999999983
5.771999999999982
6.0559999999999805
6.25599999999998
6.19999999999998
6.13599999999998
5.835999999999981
6.05199999999998
5.767999999999981
6.0239999999999805
6.471999999999979
6.339999999999979
6.283999999999979
6.267999999999979
6.515999999999979
6.423999999999979
6.475999999999979
6.451999999999979
6.479999999999978
6.819999999999977
6.707999999999977
6.803999999999977
6.939999999999976
7.191999999999975
7.0199999999999765
7.323999999999975
7.491999999999974
cusum.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ENGG1811 21T3 Assignment 1
Template file for cusum()
@author:
"""
def cusum(conc_lpr):


__MACOSX/._cusum.py
detect_fault_in_conc.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ENGG1811 21T3 Assignment 1
Template file for detect_fault_in_conc
@author:
"""
def detect_fault_in_conc(conc_cusum,conc_cusum_limit):

__MACOSX/._detect_fault_in_conc.py
detect_fault_main.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ENGG1811 21T3 Assignment 1
Template file for detect_fault_main
@author:
"""
def detect_fault_main(conc_signal,conc_range,conc_var,conc_cusum_limit,
flow_signal,flow_range,flow_control_window):

import classify_fault as classify

__MACOSX/._detect_fault_main.py
flow_signal_0.txt
22.94
23.07
22.97
22.9
23.05
22.65
22.78
22.78
22.94
22.91
22.96
23.07
23.14
23.08
22.89
23.12
22.97
23.32
23.14
23.0
22.98
22.8
23.02
22.77
23.07
22.63
22.77
23.28
23.3
23.26
22.64
22.7
22.71
22.66
23.05
22.9
23.0
22.9
22.84
23.41
23.12
22.8
23.24
22.87
22.95
23.28
22.84
22.95
22.94
23.35
23.4
23.04
23.38
22.73
22.96
23.26
22.95
23.07
23.04
23.14
23.05
22.7
22.92
22.85
23.12
22.65
22.83
23.24
23.07
23.49
22.99
22.46
23.2
23.04
22.93
22.73
22.96
23.12
23.07
23.02
23.12
23.03
22.79
23.06
23.1
23.39
23.01
22.79
23.23
23.15
22.89
22.76
23.31
22.66
22.87
23.06
22.79
22.96
23.1
22.92
flow_signal_1.txt
22.94
23.07
22.97
22.9
23.05
22.65
22.78
22.78
22.94
22.91
22.96
23.07
23.14
23.08
22.89
23.12
22.97
23.32
23.14
23.0
22.98
22.8
23.02
22.77
23.07
22.63
22.77
23.28
23.3
23.26
22.64
22.7
22.71
22.66
23.05
22.9
23.0
22.9
22.84
23.41
23.12
22.8
23.24
22.87
22.95
23.28
22.84
22.95
22.94
23.35
23.4
23.04
23.38
22.73
22.96
23.26
22.95
23.07
23.04
23.14
23.05
22.7
22.92
22.85
23.12
22.65
22.83
23.24
23.07
23.49
22.99
22.46
23.2
23.04
22.93
22.73
22.96
23.12
23.07
23.02
23.12
23.03
22.79
23.06
23.1
23.39
23.01
22.79
23.23
23.15
22.89
22.76
23.31
22.66
22.87
23.06
22.79
22.96
23.1
22.92
flow_signal_2.txt
22.94
23.07
22.97
22.9
23.05
22.65
22.78
22.78
22.94
22.91
22.96
23.07
23.14
23.08
22.89
23.12
22.97
23.32
23.14
23.0
22.98
22.8
23.02
22.83
23.57
22.47
22.82
24.11
24.15
24.04
22.49
22.65
22.67
22.55
23.51
23.16
23.4
23.15
23.0
24.42
23.7
22.9
24.01
23.07
23.26
24.11
23.01
23.27
23.25
24.26
24.39
23.51
24.35
22.72
23.3
24.04
23.28
23.57
23.51
23.74
23.53
22.66
23.21
23.03
23.69
22.53
22.99
23.99
23.58
24.62
23.38
22.04
23.9
23.5
23.22
22.73
23.31
23.7
23.57
23.44
23.7
23.48
22.88
23.54
23.65
24.37
23.42
22.87
23.98
23.77
23.12
22.79
24.17
22.55
23.08
23.54
22.88
23.31
23.65
23.2
test_calc_prob_ratio.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ENGG1811 Asssignment 1 (21T3)
You can use this file to test your calc_log_prob_ratio().
This file containing three test cases which you can choose by
adjusting the variable test_index in Line 18.
You can use this file to come out with additional tests.
"""
# %% import
import calc_log_prob_ratio as clpr
# %% Tests
test_index = 0 # Can be 0, 1 or 2
if test_index == 0:
measurement = 6
conc_range = [3, 7]
conc_var = 2
expected_answer = 2.0
elif test_index == 1:
measurement = 6.2
conc_range = [6.1, 6.8]
conc_var = 0.3
expected_answer = -0.58333
elif test_index == 2:
measurement = 6.9
conc_range = [6.0, 7.1]
conc_var = 0.4
expected_answer = 0.9625
# %% Run the function and check the answers

your_answer = clpr.calc_log_prob_ratio(measurement, conc_range, conc_var)
print('Your function returns',your_answer)
print('The expected answer is',expected_answer)
TOL = 1e-5
comparison_output = abs(your_answer-expected_answer) <= TOL
if comparison_output:
print('Your answer is correct')
else:
print('Your answer is NOT correct')

test_calc_signal_log_prob_ratio.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ENGG1811 Asssignment 1 (21T3)
You can use this file to test your calc_signal_log_prob_ratio().
This file containing three test cases which you can choose by
adjusting the variable test_index in Line 18.
You can use this file to come out with additional tests.
"""
# %% import
import calc_signal_log_prob_ratio as cslpr
# %% Tests
test_index = 0 # Can be 0, 1 or 2
if test_index == 0:
conc_signal = [6, 5, 4, 7]
conc_range = [3, 7]
conc_var = 2
expected_answer = [2.0, 0.0, -2.0, 4.0]
elif test_index == 1:
conc_signal = [15.62, 8.8, 11.69, 18.0, 16.14]
conc_range = [6.1, 6.8]
conc_var = 0.5
expected_answer = [12.838, 3.29, 7.336, 16.17, 13.566]
elif test_index == 2:
conc_signal = [5.55, 6.63, 0.39, 9.38, 2.51, 4.42, 7.11]
conc_range = [6.1, 6.8]
conc_var = 2.5
expected_answer = [-0.252, 0.0504, -1.6968, 0.8204, -1.1032, -0.5684, 0.1848]
# %% Run the function and check the answers

your_answer = cslpr.calc_signal_log_prob_ratio(conc_signal, conc_range, conc_var)
print('Your function returns',your_answer)
print('The expected answer is',expected_answer)
TOL = 1e-5
try:
if len(your_answer) == len(expected_answer):
comparison_output = [True if abs(y-e) <= TOL else False for
y,e in zip(your_answer,expected_answer)]

if all(comparison_output):
print('Your answer is correct')
else:
print('Your answer is NOT correct')
else:
print('The number of list entries in your answer is different from expected.')
except:
print('Your answer is NOT correct.')
print('Probably because your answer is not a list but can be other problems.')


__MACOSX/._test_calc_signal_log_prob_ratio.py
test_classify_fault.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ENGG1811 Asssignment 1 (21T3)
You can use this file to test your classify_fault().
This file containing three test cases which you can choose by
adjusting the variable test_index in Line 18.
You can use this file to come out with additional tests.
"""
# %% import
import classify_fault as classify
# %% Tests
test_index = 0 # Can be 0, 1 or 2
if test_index == 0:
conc_signal = [6.87, 7.06, 6.96, 5.87, 7.2, 7.04, 6.93, 7.36, 7.41, 6.67]
conc_range = [6.1, 6.8]
conc_var = 0.5
conc_cusum_limit = 3.1
flow_signal = [22.94, 23.07, 22.97, 22.9, 23.05, 22.65, 22.78, 22.78, 22.94, 22.91]
flow_range = [22.5, 23.5]
flow_control_window = 3
expected_answer = 'conc fault only'
elif test_index == 1:
conc_signal = [6.24, 5.66, 6.12, 5.15, 6.38, 6.43, 7.2, 7.14, 6.8, 8.64]
conc_range = [6.1, 6.9]
conc_var = 0.5
conc_cusum_limit = 2.5
flow_signal = [22.94, 23.07, 23.97, 23.9, 24.05, 23.65, 23.78, 23.78, 23.94, 23.91]
flow_range = [22.5, 23.5]
flow_control_window = 5
expected_answer = 'flow and conc faults'
elif test_index == 2:
conc_signal = [6.78, 6.04, 6.09, 5.96, 5.0, 6.07, 5.25, 6.11, 5.72, 6.54, 5.91, 6.01]
conc_range = [5.3, 6.8]
conc_var = 0.5
conc_cusum_limit = 3.7
flow_signal = [23.27, 23.14, 22.69, 23.0, 23.12, 22.86, 23.05, 23.02, 23.0, 22.97, 23.09, 23.24]
flow_range = [22.5, 23.5]
flow_control_window = 3
expected_answer = 'no faults'
# %% Run the function and check the answers

your_answer = classify.classify_fault(conc_signal,conc_range,conc_var,conc_cusum_limit,
flow_signal,flow_range,flow_control_window)
print('Your function returns:',your_answer)
print('The expected answer is:',expected_answer)
comparison_output = your_answer == expected_answer
if comparison_output:
print('Your answer is correct')
else:
print('Your answer is NOT correct')

test_cusum.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ENGG1811 Asssignment 1 (21T3)
You can use this file to test your cusum().
This file containing three test cases which you can choose by
adjusting the variable test_index in Line 18.
You can use this file to come out with additional tests.
"""
# %% import
import cusum as cs
# %% Tests
test_index = 0 # Can be 0, 1 or 2
if test_index == 0:
conc_lpr = [1, 3, 2, 4]
expected_answer = [1, 4, 6, 10]
elif test_index == 1:
conc_lpr = [2, -1, 3, -8, -1, -2, 3, 4]
expected_answer = [2, 1, 4, 0, 0, 0, 3, 7]
elif test_index == 2:
conc_lpr = [-1.6, -1.3, 2.5, -3.1, -1.7, 4.2, 3.6, -1.2, -0.4, 0.7]
expected_answer = [0, 0, 2.5, 0, 0, 4.2, 7.8, 6.6, 6.2, 6.9]
# %% Run the function and check the answers

your_answer = cs.cusum(conc_lpr)
print('Your function returns',your_answer)
print('The expected answer is',expected_answer)
TOL = 1e-5
try:
if len(your_answer) == len(expected_answer):
comparison_output = [True if abs(y-e) <= TOL else False for
y,e in zip(your_answer,expected_answer)]

if all(comparison_output):
print('Your answer is correct')
else:
print('Your answer is NOT correct')
else:
print('The number of list entries in your answer is different from expected.')
except:
print('Your answer is NOT correct.')
print('Probably because your answer is not a list but can be other problems.')


__MACOSX/._test_cusum.py
test_detect_fault_in_conc.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ENGG1811 Asssignment 1 (21T3)
You can use this file to test your detect_fault_in_conc().
This file containing four test cases which you can choose by
adjusting the variable test_index in Line 18.
You can use this file to come out with additional tests.
"""
# %% import
import detect_fault_in_conc as detect
import math
# %% Tests
test_index = 2 # Can be 0, 1, 2 or 3
if test_index == 0:
conc_cusum = [0.59, 1.44, 2.16, 1.34, 2.39, 3.22, 3.89, 5.17, 6.51, 6.82]
conc_cusum_limit = 3.1
time_fault_detected_expected = 5
total_fault_time_expected = 5
elif test_index == 1:
conc_cusum = [0.59, 1.44, 2.16, 1.34, 2.39, 3.22, 3.89, 5.17, 6.51, 6.82]
conc_cusum_limit = 1.9
time_fault_detected_expected = 2
total_fault_time_expected = 7
elif test_index == 2:
conc_cusum = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.12, 2.14, 2.62, 6.05]
conc_cusum_limit = 2.5
time_fault_detected_expected = 8
total_fault_time_expected = 2
elif test_index == 3:
conc_cusum = [2.19, 2.16, 2.28, 2.01, 0.0, 0.06, 0.0, 0.18, 0.0, 1.47]
conc_cusum_limit = 3.7
time_fault_detected_expected = math.inf
total_fault_time_expected = 0
# %% Plotting
#
# You can use the following line of code to plot what the cusim signal
# looks like respect to the threshold used for fault detection
#
# Comment the code out for plotting
#
# import matplotlib.pyplot as plt
# plt.plot(conc_cusum)
# plt.axhline(conc_cusum_limit,linestyle='--',color='r')
# plt.title('cusum for test case '+ str(test_index))
# plt.show()
# %% Run the function and check the answers

time_fault_detected_your, total_fault_time_your = \
detect.detect_fault_in_conc(conc_cusum, conc_cusum_limit)
print('For the computation of the time at which fault is detected:')
print('\tYour function returns:',time_fault_detected_your)
print('\tThe expected answer is:',time_fault_detected_expected)
comparison_time_fault_detected = time_fault_detected_your == time_fault_detected_expected
if comparison_time_fault_detected:
print('\tYour answer for the first output is correct')
else:
print('\tour answer for the first output is NOT correct')
print('\n') # Print a blank line
print('For the computation of the total fault time:')
print('\tYour function returns:',total_fault_time_your)
print('\tThe expected answer is:',total_fault_time_expected)
comparison_total_fault_time_expected = total_fault_time_your == total_fault_time_expected
if comparison_total_fault_time_expected:
print('\tYour answer for the second output is correct')
else:
print('\tYour answer for the second output is NOT correct')
__MACOSX/._test_detect_fault_in_conc.py
test_detect_fault_main_0.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ENGG1811 Asssignment 1 (21T3)
You can use this file to test your detect_fault_main().
This file contains only one test case which is the same as the sample
test code in the project specifications.
You can use this file to come out with additional tests.
"""
# %% Test data and parameters
# The conc signal
conc_signal = [6.55, 6.40, 6.76, 6.45, 6.87, 6.92, 6.45, 7.36, 7.41, 6.67]
# Parameters used to detect whether there is a fault in the conc_signal
conc_range = [6.1, 6.9]
conc_var = 0.5
conc_cusum_limit = 1.55
# The flow_signal
flow_signal = [22.94, 23.07, 23.97, 23.9, 24.05, 23.65, 23.78, 23.78, 23.94, 23.91]
# The parameters used to detect whether there is a fault in the flow_signal
flow_range = [22.5, 23.5]
flow_control_window = 3
# %% Call the fault detection function
import detect_fault_main as df
fault_type_your = df.detect_fault_main(conc_signal,conc_range,conc_var,
conc_cusum_limit,flow_signal,flow_range,flow_control_window)
# The fault type
fault_type_expected = 'flow and conc faults'
print('Your function returns:',fault_type_your)
print('The expected answer is:',fault_type_expected)
comparison_output = fault_type_your == fault_type_expected
if comparison_output:
print('Your answer is correct')
else:
print('Your answer is NOT correct')

__MACOSX/._test_detect_fault_main_0.py
test_detect_fault_main_1.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ENGG1811 Asssignment 1 (21T3)
You can use this file to test your detect_fault_main().
All the cases in this file have valid parameters and the function should
determine the fault type.
This file containing three test cases which you can choose by
adjusting the variable test_index in Line 20.
You can use this file to come out with additional tests.
"""
# %% import
import detect_fault_main as df
# %% Tests
test_index = 2 # Choose 0, 1 or 2
# Both conc_signal and flow_signal have 100 entries each.
# They will be loaded from files rather than listed here.
#
# load conc_signal and flow_signal from files
with open('conc_signal_'+str(test_index)+'.txt','r') as f:
conc_signal = f.readlines()
conc_signal = [float(d) for d in conc_signal]
with open('flow_signal_'+str(test_index)+'.txt','r') as f:
flow_signal = f.readlines()
flow_signal = [float(d) for d in flow_signal]
# The algorithm parameters - the same all tests
conc_range = [5.9, 6.1]
conc_var = 0.5
conc_cusum_limit = 3.1
flow_range = [22.5, 23.5]
flow_control_window = 10
# The expected answer
if test_index == 0:
expected_answer = 'conc fault only'
elif test_index == 1:
expected_answer = 'no faults'
elif test_index == 2:
expected_answer = 'flow and conc faults'

# %% Run the function and check the answers

your_answer = df.detect_fault_main(conc_signal,conc_range,conc_var,conc_cusum_limit,
flow_signal,flow_range,flow_control_window)
print('Your function returns:',your_answer)
print('The expected answer is:',expected_answer)
comparison_output = your_answer == expected_answer
if comparison_output:
print('Your answer is correct')
else:
print('Your answer is NOT correct')

# %% Code for plotting
"""
Since the signals are long, you can use the code below to visualise them.
The signals that you can plot are:
conc_signal
conc_signal_cusum
flow_signal
A point of interest is that if you look at the conc_singal when there is a
fault and there isn't, you don't see much difference. The fault in the
conc_signal is in fact very small and is not perceptible by eyes. However,
if you look at their corresponding conc_signal_cusum, you will see that
the cusum drifts upwards steadily later on. This is an indication of fault.
We have included this example to demonstrate how powerful it can be if you
can combine computing and maths to achieve a task which is beyond our
visual perception.
If you would like to see plot of the above signals, comment out the
appropriate lines in the code below.
"""
# import matplotlib.pyplot as plt
# # conc_signal
# plt.figure(0)
# plt.plot(conc_signal)
# plt.title('conc signal for test case'+str(test_index))
# # conc_signal_cusum (Need to load the pre-computed data from file)
# plt.figure(1)
# with open('conc_signal_cusum_'+str(test_index)+'.txt','r') as f:
# conc_signal_cusum = f.readlines()
# conc_signal_cusum = [float(d) for d in conc_signal_cusum]
# plt.plot(conc_signal_cusum)
# plt.axhline(conc_cusum_limit,linestyle='--',color='r')
# plt.title('conc cusum for test case'+str(test_index))
# # flow_signal
# plt.figure(2)
# plt.plot(flow_signal)
# plt.title('flow signal for test case'+str(test_index))
__MACOSX/._test_detect_fault_main_1.py
test_detect_fault_main_2.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ENGG1811 Asssignment 1 (21T3)
You can use this file to test your detect_fault_main().
All the cases in this file are expected to return the output 'invalid parameters'
This file containing three test cases which you can choose by
adjusting the variable test_index in Line 19.
You can use this file to come out with additional tests.
"""
# %% import
import detect_fault_main as df
# %% Tests
test_index = 0 # Can be 0, 1 or 2
if test_index == 0:
conc_signal = [6.87, 7.06, 6.96, 5.87, 7.2, 7.04, 6.93, 7.36, 7.41, 6.67]
conc_range = [6.8, 6.1] # invalid
conc_var = 0.5
conc_cusum_limit = 3.1
flow_signal = [22.94, 23.07, 22.97, 22.9, 23.05, 22.65, 22.78, 22.78, 22.94, 22.91]
flow_range = [22.5, 23.5]
flow_control_window = 3
expected_answer = 'invalid parameters'
elif test_index == 1:
conc_signal = [6.24, 5.66, 6.12, 5.15, 6.38, 6.43, 7.2, 7.14, 6.8, 8.64]
conc_range = [6.1, 6.9]
conc_var = 0.5
conc_cusum_limit = 2.5
flow_signal = [22.94, 23.07, 23.97, 23.9, 24.05, 23.65, 23.78, 23.78, 23.94, 23.91]
flow_range = [22.5, 23.5]
flow_control_window = 5.2 # invalid
expected_answer = 'invalid parameters'
elif test_index == 2:
conc_signal = [6.78, 6.04, 6.09, 5.96, 5.0, 6.07, 5.25, 6.11, 5.72, 6.54, 5.91, 6.01]
conc_range = [5.3, 6.8]
conc_var = 0.5
conc_cusum_limit = 2.6
flow_signal = [23.27, 23.14, 22.69, 23.0, 23.12, 22.86, 23.05, 23.02, 23.0, 22.97, 23.09, 23.24]
flow_range = [22.5, 23.5, 24.5] # invalid
flow_control_window = 3
expected_answer = 'invalid parameters'
# %% Run the function and check the answers

your_answer = df.detect_fault_main(conc_signal,conc_range,conc_var,conc_cusum_limit,
flow_signal,flow_range,flow_control_window)
print('Your function returns:',your_answer)
print('The expected answer is:',expected_answer)
comparison_output = your_answer == expected_answer
if comparison_output:
print('Your answer is correct')
else:
print('Your answer is NOT correct')

__MACOSX/._test_detect_fault_main_2.py
document/version-and-change-log-python-assignment-yeaewdip-1qlkhvwu.docx
Version and change log
Due Date : Friday 29th October 5pm AEST
1 Introduction
Automatic detection of faults can be found in many engineering systems. There are systems to automatically diagnose faults in engines, chemical plants,...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here