|
1
|
|
|
# import functions and modules |
|
2
|
|
|
import file_read |
|
3
|
|
|
import baseline |
|
4
|
|
|
import numpy as np |
|
5
|
|
|
import pandas as pd |
|
6
|
|
|
|
|
7
|
|
|
#Test functions: |
|
8
|
|
|
def test_split(): |
|
9
|
|
|
""" |
|
10
|
|
|
This function tests the split function. |
|
11
|
|
|
The output of the function has to be np.array. |
|
12
|
|
|
Split function splits the length of input vector |
|
13
|
|
|
in two. So, len of output should equal to half len |
|
14
|
|
|
of input. |
|
15
|
|
|
""" |
|
16
|
|
|
dict_1,n_cycle = file_read.read_file('../../data/10mM_2,7-AQDS_1M_KOH_25mVs_0.5step_2.txt') |
|
17
|
|
|
df = file_read.data_frame(dict_1, 1) |
|
18
|
|
|
x = df.Potential |
|
19
|
|
|
a,b = baseline.split(x) |
|
20
|
|
|
assert type(a) == np.ndarray, "The output type is incorrect." |
|
21
|
|
|
assert type(b) == np.ndarray, "The output type is incorrect." |
|
22
|
|
|
#assert len(a) int(len(x)/2), "The output should be " |
|
23
|
|
|
np.testing.assert_almost_equal(len(a),(len(x)/2), decimal=0), "Output length is incorrect" |
|
24
|
|
|
np.testing.assert_almost_equal(len(b),(len(x)/2), decimal=0), "Output length is incorrect" |
|
25
|
|
|
return "Test of split function passed!" |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
def test_critical_idx(): |
|
29
|
|
|
""" |
|
30
|
|
|
Critical_idx returns idx of the index of the intercepts of different moving average curves. |
|
31
|
|
|
Test the output if it is a single index. |
|
32
|
|
|
Test if the output is integer. |
|
33
|
|
|
Test if the index exist in original input. |
|
34
|
|
|
""" |
|
35
|
|
|
dict_1,n_cycle = file_read.read_file('../../data/10mM_2,7-AQDS_1M_KOH_25mVs_0.5step_2.txt') |
|
36
|
|
|
df = file_read.data_frame(dict_1, 1) |
|
37
|
|
|
x1,x2 = baseline.split(df.Potential) |
|
38
|
|
|
y1,y2 = baseline.split(df.Current) |
|
39
|
|
|
idx = baseline.critical_idx(x1,y1) |
|
40
|
|
|
assert type(idx) == np.int64, ("Output should be integer, but" |
|
41
|
|
|
"Function is returning {}".format(type(idx))) |
|
42
|
|
|
assert idx.shape == (), "This function should return single idx" |
|
43
|
|
|
assert 0 < idx <len(x1), "Output index is out of order" |
|
44
|
|
|
return "Test of critical_idx function passed!" |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
def test_sum_mean(): |
|
48
|
|
|
""" |
|
49
|
|
|
Target function returns the mean and sum of the given vector. |
|
50
|
|
|
Expect output to be a list, with length 2. |
|
51
|
|
|
Can also test if the mean is correctly calculated. |
|
52
|
|
|
""" |
|
53
|
|
|
dict_1,n_cycle = file_read.read_file('../../data/10mM_2,7-AQDS_1M_KOH_25mVs_0.5step_2.txt') |
|
54
|
|
|
df = file_read.data_frame(dict_1, 1) |
|
55
|
|
|
x1,x2 = baseline.split(df.Potential) |
|
56
|
|
|
y1,y2 = baseline.split(df.Current) |
|
57
|
|
|
a = baseline.sum_mean(x1) |
|
58
|
|
|
assert type(a) == list, ("Output should be list object," |
|
59
|
|
|
" but fuction is returning{}".format(type(a))) |
|
60
|
|
|
assert len(a) == 2, ("length of output should be 2," |
|
61
|
|
|
"but, function is returning a list with length{}".format(len(a))) |
|
62
|
|
|
np.testing.assert_almost_equal(a[1],np.mean(x1), decimal=3), "Mean is calculated incorrectly" |
|
63
|
|
|
return "Test of sum_mean function passed!" |
|
64
|
|
|
|
|
65
|
|
|
def test_multiplica(): |
|
66
|
|
|
""" |
|
67
|
|
|
Target function returns the sum of the multilica of two given vector. |
|
68
|
|
|
Expect output as np.float object. |
|
69
|
|
|
""" |
|
70
|
|
|
dict_1,n_cycle = file_read.read_file('../../data/10mM_2,7-AQDS_1M_KOH_25mVs_0.5step_2.txt') |
|
71
|
|
|
df = file_read.data_frame(dict_1, 1) |
|
72
|
|
|
x1,x2 = baseline.split(df.Potential) |
|
73
|
|
|
y1,y2 = baseline.split(df.Current) |
|
74
|
|
|
a = baseline.multiplica(x1,y1) |
|
75
|
|
|
assert type(a) == np.float64, ("Output should be float object," |
|
76
|
|
|
" but fuction is returning{}".format(type(a))) |
|
77
|
|
|
b = np.multiply(x1,y1).sum() |
|
78
|
|
|
(np.testing.assert_almost_equal(a,b, decimal=3), |
|
79
|
|
|
"Calculation is incorrect") |
|
80
|
|
|
return "Test Passed for multiplica function!" |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
def test_linear_coeff(): |
|
84
|
|
|
""" |
|
85
|
|
|
Target function returns the inclination coeffecient |
|
86
|
|
|
and y axis interception coeffecient m and b. |
|
87
|
|
|
T |
|
88
|
|
|
""" |
|
89
|
|
|
x = np.array([1,2,3,4,5,6,7,8,9]) |
|
90
|
|
|
y = np.array([1,2,3,4,5,6,7,8,9]) |
|
91
|
|
|
m,b = baseline.linear_coeff(x,y) |
|
92
|
|
|
assert m == 1, "Inclination coeffecient is incorrect" |
|
93
|
|
|
assert b == 0, "Interception is incorrect" |
|
94
|
|
|
return "Test passed for linear_coeff function!" |
|
95
|
|
|
|
|
96
|
|
|
def test_y_fitted_line(): |
|
97
|
|
|
""" |
|
98
|
|
|
Target function returns the fitted baseline y. |
|
99
|
|
|
Should exam if the output is correct shape, |
|
100
|
|
|
correct type, and correct value. |
|
101
|
|
|
""" |
|
102
|
|
|
x = np.array([1,2,3,4,5,6,7,8,9]) |
|
103
|
|
|
m = 1 |
|
104
|
|
|
b = 0 |
|
105
|
|
|
y = baseline.y_fitted_line(m,b,x) |
|
106
|
|
|
if len(y) != len(x): |
|
107
|
|
|
raise ValueError("Output must have same length as input x," |
|
108
|
|
|
"but have lenth {}".format(len(y))) |
|
109
|
|
|
assert type(y) == list, "Output should be list object" |
|
110
|
|
|
if np.all(y != x): |
|
111
|
|
|
raise ValueError("Fitted line y values are calculated incorrectly") |
|
112
|
|
|
return "Test passed for y_fitted_line function!" |
|
113
|
|
|
|
|
114
|
|
|
def test_linear_background(): |
|
115
|
|
|
""" |
|
116
|
|
|
Target function is wrapping function which returns |
|
117
|
|
|
linear fitted line.Should exam if the output is |
|
118
|
|
|
correct shape, correct type, and correct value. |
|
119
|
|
|
""" |
|
120
|
|
|
dict_1,n_cycle = file_read.read_file('../../data/10mM_2,7-AQDS_1M_KOH_25mVs_0.5step_2.txt') |
|
121
|
|
|
df = file_read.data_frame(dict_1, 1) |
|
122
|
|
|
x1,x2 = baseline.split(df.Potential) |
|
123
|
|
|
y1,y2 = baseline.split(df.Current) |
|
124
|
|
|
y_fit = baseline.linear_background(x1,y1) |
|
125
|
|
|
assert type(y_fit) == list, "Output should be list object" |
|
126
|
|
|
if len(y_fit) != len(x1): |
|
127
|
|
|
raise ValueError("Output must have same length as input x," |
|
128
|
|
|
"but have lenth {}".format(len(y_fit))) |
|
129
|
|
|
if len(y_fit) != len(y1): |
|
130
|
|
|
raise ValueError("Output must have same length as input y," |
|
131
|
|
|
"but have lenth {}".format(len(y_fit))) |
|
132
|
|
|
return "Test passed for linear_background function!" |