-
Notifications
You must be signed in to change notification settings - Fork 1
/
Calibration_code.py
218 lines (178 loc) · 8.98 KB
/
Calibration_code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 30 23:58:04 2021
@author: Mc Zie
"""
##############################
# Capacitive Soil Moisture
# Sensor Calibration Analysis
##############################
#
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
container_mass = 19.67 # measured mass of container [g]
soil_mass_dry = 183 # mass of dry soil [g]
soil_vol = 153 # volume of soil sample [ml]
rho_s = (soil_mass_dry/1000.0)/(soil_vol*np.power(10.0,-6.0)) # bulk density of soil [kg/m^3]
rho_w = 997.0 # density of water [kg/m^3]
###############################################
# Data inputs
#
soil_masses = np.subtract([116.45,137.68,151.69,170.22,183.56,
191.14,193.31,194.48,197.19],container_mass) # mass measurements [g]
cap_sensor_readings = np.array([1.63,1.43,1.36,1.32,1.29,1.26,1.25,1.20,1.19]) # cap sensor readings [V]
###############################################
# calculating volumetric water content [%]
#
theta_g = (soil_masses - soil_mass_dry)/soil_mass_dry # water proportion
theta_v = ((theta_g*rho_s)/rho_w) # volumetric soil content [g/ml / g/ml]
###############################################
# Fitting 1/sensor readings with measurements
#
x_for_training = 1.0/cap_sensor_readings # 1/sensor readings
slope, intercept, r_value, p_value, std_err = stats.linregress(x_for_training, theta_v) # linear fit
theta_predict = (slope*(x_for_training))+intercept # prediction of theta_v with sensor
###############################################
# Plot the results
#
plt.style.use('ggplot')
fig,axs = plt.subplots(2,1,figsize=(12,9))
# plotting the sensor to theta_v
ax = axs[0]
fig.suptitle('Capacitive soil moisture sensor calibration for sandy soil', fontsize=16)
ax.plot(x_for_training,theta_v,label='Data',linestyle='',marker='o',color=plt.cm.Set1(0),
markersize=10,zorder=999)
ax.plot(x_for_training,theta_predict,label='Fit ({0:2.2f}$\cdot$(1/V) {1:+2.2f})'.format(slope,intercept),
color=plt.cm.Set1(1),linewidth=4)
ax.set_xlabel(r'Inverse of Capacitive Sensor Voltage [V$^{-1}$]',fontsize=18)
ax.set_ylabel(r'$\theta_v$ [cm$^{3}$/cm$^3$]',fontsize=18)
ax.legend(fontsize=16)
rmse = np.sqrt(np.mean(np.power(np.subtract(theta_predict,theta_v),2.0))) # value error
mape = np.mean(np.divide(np.subtract(theta_predict,theta_v),theta_v)*100) # % error
# plotting the comparison between fit and data
ax2 = axs[1]
ax2.plot(theta_predict,theta_v,label='Capacitive (RMSE: {0:2.3f}, MAPE: {1:2.0f}%)'.format(rmse,mape),
linestyle='',marker='o',color=plt.cm.Set1(2),markersize=10,zorder=999)
ax2.plot(theta_v,theta_v,label='Gravimetric',color=plt.cm.Set1(3),linewidth=4)
ax2.set_xlabel(r'$\theta_{v,cap}$ [cm$^{3}$/cm$^3$]',fontsize=18)
ax2.set_ylabel(r'$\theta_{v,grav}$ [cm$^{3}$/cm$^3$]',fontsize=18)
ax2.legend(fontsize=16)
fig.savefig('soil_moisture_calibration_results_for_sandy_soil.png',dpi=300,bbox_inches='tight',facecolor='#FCFCFC')
plt.show()
#################################################################################################################################"
##############################
# Capacitive Soil Moisture
# Sensor Calibration Analysis
##############################
#
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
container_mass = 19.67 # measured mass of container [g]
soil_mass_dry = 183 # mass of dry soil [g]
soil_vol = 153 # volume of soil sample [ml]
rho_s = (soil_mass_dry/1000.0)/(soil_vol*np.power(10.0,-6.0)) # bulk density of soil [kg/m^3]
rho_w = 997.0 # density of water [kg/m^3]
###############################################
# Data inputs
#
soil_masses = np.subtract([102.23,112.92,133.81,139.65,145.12,
148.80,156.69,162.61,190.81],container_mass) # mass measurements [g]
cap_sensor_readings = np.array([1.47,1.40,1.32,1.27,1.25,1.24,1.22,1.21,1.20]) # cap sensor readings [V]
###############################################
# calculating volumetric water content [%]
#
theta_g = (soil_masses - soil_mass_dry)/soil_mass_dry # water proportion
theta_v = ((theta_g*rho_s)/rho_w) # volumetric soil content [g/ml / g/ml]
###############################################
# Fitting 1/sensor readings with measurements
#
x_for_training = 1.0/cap_sensor_readings # 1/sensor readings
slope, intercept, r_value, p_value, std_err = stats.linregress(x_for_training, theta_v) # linear fit
theta_predict = (slope*(x_for_training))+intercept # prediction of theta_v with sensor
###############################################
# Plot the results
#
plt.style.use('ggplot')
fig,axs = plt.subplots(2,1,figsize=(12,9))
# plotting the sensor to theta_v
ax = axs[0]
fig.suptitle('Capacitive soil moisture sensor calibration for clayey soil', fontsize=16)
ax.plot(x_for_training,theta_v,label='Data',linestyle='',marker='o',color=plt.cm.Set1(0),
markersize=10,zorder=999)
ax.plot(x_for_training,theta_predict,label='Fit ({0:2.2f}$\cdot$(1/V) {1:+2.2f})'.format(slope,intercept),
color=plt.cm.Set1(1),linewidth=4)
ax.set_xlabel(r'Inverse of Capacitive Sensor Voltage [V$^{-1}$]',fontsize=18)
ax.set_ylabel(r'$\theta_v$ [cm$^{3}$/cm$^3$]',fontsize=18)
ax.legend(fontsize=16)
rmse = np.sqrt(np.mean(np.power(np.subtract(theta_predict,theta_v),2.0))) # value error
mape = np.mean(np.divide(np.subtract(theta_predict,theta_v),theta_v)*100) # % error
# plotting the comparison between fit and data
ax2 = axs[1]
ax2.plot(theta_predict,theta_v,label='Capacitive (RMSE: {0:2.3f}, MAPE: {1:2.0f}%)'.format(rmse,mape),
linestyle='',marker='o',color=plt.cm.Set1(2),markersize=10,zorder=999)
ax2.plot(theta_v,theta_v,label='Gravimetric',color=plt.cm.Set1(3),linewidth=4)
ax2.set_xlabel(r'$\theta_{v,cap}$ [cm$^{3}$/cm$^3$]',fontsize=18)
ax2.set_ylabel(r'$\theta_{v,grav}$ [cm$^{3}$/cm$^3$]',fontsize=18)
ax2.legend(fontsize=16)
fig.savefig('soil_moisture_calibration_results_for_clayey_soil.png',dpi=300,bbox_inches='tight',facecolor='#FCFCFC')
plt.show()
########################################################################################################################
##############################
# Capacitive Soil Moisture
# Sensor Calibration Analysis
##############################
#
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
container_mass = 19.67 # measured mass of container [g]
soil_mass_dry = 183 # mass of dry soil [g]
soil_vol = 153 # volume of soil sample [ml]
rho_s = (soil_mass_dry/1000.0)/(soil_vol*np.power(10.0,-6.0)) # bulk density of soil [kg/m^3]
rho_w = 997.0 # density of water [kg/m^3]
###############################################
# Data inputs
#
soil_masses = np.subtract([61.30,88.71,109.00,123.92,131.80,
136.34,139.32,149.99,156.34],container_mass) # mass measurements [g]
cap_sensor_readings = np.array([2.17,1.99,1.53,1.39,1.33,1.30,1.26,1.24,1.21]) # cap sensor readings [V]
###############################################
# calculating volumetric water content [%]
#
theta_g = (soil_masses - soil_mass_dry)/soil_mass_dry # water proportion
theta_v = ((theta_g*rho_s)/rho_w) # volumetric soil content [g/ml / g/ml]
###############################################
# Fitting 1/sensor readings with measurements
#
x_for_training = 1.0/cap_sensor_readings # 1/sensor readings
slope, intercept, r_value, p_value, std_err = stats.linregress(x_for_training, theta_v) # linear fit
theta_predict = (slope*(x_for_training))+intercept # prediction of theta_v with sensor
###############################################
# Plot the results
#
plt.style.use('ggplot')
fig,axs = plt.subplots(2,1,figsize=(12,9))
# plotting the sensor to theta_v
ax = axs[0]
fig.suptitle('Capacitive soil moisture sensor calibration for silty soil', fontsize=16)
ax.plot(x_for_training,theta_v,label='Data',linestyle='',marker='o',color=plt.cm.Set1(0),
markersize=10,zorder=999)
ax.plot(x_for_training,theta_predict,label='Fit ({0:2.2f}$\cdot$(1/V) {1:+2.2f})'.format(slope,intercept),
color=plt.cm.Set1(1),linewidth=4)
ax.set_xlabel(r'Inverse of Capacitive Sensor Voltage [V$^{-1}$]',fontsize=18)
ax.set_ylabel(r'$\theta_v$ [cm$^{3}$/cm$^3$]',fontsize=18)
ax.legend(fontsize=16)
rmse = np.sqrt(np.mean(np.power(np.subtract(theta_predict,theta_v),2.0))) # value error
mape = np.mean(np.divide(np.subtract(theta_predict,theta_v),theta_v)*100) # % error
# plotting the comparison between fit and data
ax2 = axs[1]
ax2.plot(theta_predict,theta_v,label='Capacitive (RMSE: {0:2.3f}, MAPE: {1:2.0f}%)'.format(rmse,mape),
linestyle='',marker='o',color=plt.cm.Set1(2),markersize=10,zorder=999)
ax2.plot(theta_v,theta_v,label='Gravimetric',color=plt.cm.Set1(3),linewidth=4)
ax2.set_xlabel(r'$\theta_{v,cap}$ [cm$^{3}$/cm$^3$]',fontsize=18)
ax2.set_ylabel(r'$\theta_{v,grav}$ [cm$^{3}$/cm$^3$]',fontsize=18)
ax2.legend(fontsize=16)
fig.savefig('soil_moisture_calibration_results_for_silty_soil.png',dpi=300,bbox_inches='tight',facecolor='#FCFCFC')
plt.show()