-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils_ViscoIndent.py
290 lines (257 loc) · 11.8 KB
/
utils_ViscoIndent.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 14 23:29:54 2020
@author: Yuri
"""
import os.path # for check existance of the spm file
import numpy as np
import shelve
import pickle
from Pars_class import Pars_gen
from make_Results import make_Results
from import_AFM_data import Bruker_import
from force_curve_fit import locate_position, smoothM, HertzBEC, DMTBEC, npmax
from Ting_numerical import ting_numerical
import sys
if not sys.warnoptions:
import warnings
def check_Pars(Pars):
if not hasattr(Pars, 'adhesion_model'):
Pars.adhesion_model = 'none'
if not hasattr(Pars, 'adhesion_region'):
Pars.adhesion_region = 'retraction'
return Pars
def save_AFM_data_pickle(filename, Pars, Data, Results):
# saving complete dataset
# global Pars, Data, Results
# filename = 'D:/MEGAsync/My materials/python/Ting_code/temp_spy_data/test_pickle.dat'
Pars_dict = Pars.class2dict()
with open(filename, 'wb') as f:
dictAFM = {'Pars': Pars_dict, 'Data': Data, 'Results': Results}
pickle.dump(dictAFM, f)
def save_AFM_data_pickle_short(filename, Pars, Data, Results):
# saving only minimal set of Data
# global Pars, Data, Results
Data2 = np.copy(Data)
Data2[:, 1] = np.zeros([Data2.shape[0]]) # remove curves data
print('saving data')
print(filename)
Pars_dict = Pars.class2dict()
with open(filename, 'wb') as f:
dictAFM = {'Pars': Pars_dict, 'Data': Data2, 'Results': Results}
pickle.dump(dictAFM, f)
print('saved')
def load_AFM_data_pickle(filename):
# load from .dat file
with open(filename, 'rb') as f:
data2 = pickle.load(f)
Pars_dict = data2['Pars']
if isinstance(Pars_dict, dict):
Pars = Pars_gen()
Pars.dict2class(Pars_dict)
else:
Pars = Pars_dict
Data = data2['Data']
Results = data2['Results']
return Pars, Data, Results
def load_AFM_data_pickle_short(filename):
# load from .dat file (short)
spmfilename = filename[:-3] + 'spm'
print(spmfilename)
if not os.path.isfile(spmfilename): # search for truncated filename
spmfilename = filename[:-5] + '.spm'
if not os.path.isfile(spmfilename):
print('No corresponding .spm file was found in the same folder!')
Data = [0]
Data = np.asarray(Data, dtype=object)
Results = make_Results(np.shape(Data)[0])
Pars = Pars_gen()
else:
with open(filename, 'rb') as f:
data2 = pickle.load(f)
Pars_dict = data2['Pars']
if isinstance(Pars_dict, dict):
Pars = Pars_gen()
Pars.dict2class(Pars_dict)
else:
Pars = Pars_dict
Pars.filedir[0] = spmfilename
Data = data2['Data']
Results = data2['Results']
Bruker_data = Bruker_import(Pars)
Data2 = Bruker_data.Data
print('data loaded from file')
kk = 0
for ii in range(np.shape(Data2)[0]):
if Data2[ii][0] == Data[kk][0]:
Data[kk][1] = Data2[ii][1]
if kk < np.shape(Data)[0]-1:
kk = kk+1
else:
break
return Pars, Data, Results
def AFM_data_pickle_to_short(filename):
Pars, Data, Results = load_AFM_data_pickle(filename)
save_AFM_data_pickle_short(filename, Pars, Data, Results)
def curve_from_saved_pars(Pars, cData, cResults):
currentcurve3 = cData[1][:, 0:2]
DFL_corrs = cData[2]
cpHertz = cResults['cpHertz']
Height = cResults['Height']
EHertz = cResults['EHertz']
EHertzBEC = cResults['EHertzBEC']
E_DMT = cResults['E_adhesion']
adhesion = cResults['adhesion']
cpTing = cResults['cpTing']
E0 = cResults['E0']
alpha_tau = cResults['alpha_tau']
Einf = cResults['Einf']
E0BEC = cResults['E0BEC']
alpha_tauBEC = cResults['alpha_tauBEC']
EinfBEC = cResults['EinfBEC']
# Sens = Pars.InvOLS # sensetivity, nm/V
# Stiffness = Pars.k # cantilever spring constant, nN/nm
modelprobe = Pars.probe_shape
Radius = Pars.probe_dimension # radius (nm) or angle (degrees)
Poisson = Pars.Poisson # Poisson's ratio of the sample
dT = Pars.dT # Sampling time
modelting = Pars.viscomodel
adhesion_model = Pars.adhesion_model # none, DMT, JKR
adhesion_region = Pars.adhesion_region # 'approach' 'retraction' 'both'
adhesion_pars = [adhesion_model, adhesion_region, adhesion]
# hydrodragcorr = Pars.hydro.corr # hydrodinamic drag correction
# try:
# Fixedpars = Pars.fixed_values
# except:
# Fixedpars = np.array([[0, 0, 0], [0, 0, 0]], dtype=float)
if modelprobe == 'sphere':
K1 = 4*Radius**0.5/3
power = 1.5
elif modelprobe == 'cone' or modelprobe == 'pyramid':
power = 2
if modelprobe == 'cone':
K1 = 2/np.pi*np.tan(Radius*np.pi/180)
elif modelprobe == 'pyramid':
K1 = 1.406/2*np.tan(Radius*np.pi/180)
elif modelprobe == 'cylinder':
K1 = 2*Radius # before integral no speed
power = 1
K1 = K1/(1-Poisson**2)*1e-9
# try:
# HeightfromZ = Pars.HeightfromZ
# except:
# HeightfromZ = 0
# Height = Pars.height
rawZ = currentcurve3[:, 0]
rawDFL = currentcurve3[:, 1]
if rawZ[0] > rawZ[30]:
CurveDir = -1 # decreasing curve
else: # default case
CurveDir = 1 # increasing curve
Displ = CurveDir*(rawZ-rawZ[0])
DisplSpeed = np.diff(Displ)/dT
DisplSpeed = np.append(DisplSpeed, DisplSpeed[-1])
DisplSpeed = smoothM(DisplSpeed, 5)
DFLc = rawDFL - DFL_corrs[0]*Displ - DFL_corrs[1] - DFL_corrs[2]*DisplSpeed/Pars.InvOLS/Pars.k
Forcec = DFLc*Pars.k
# indentationfull = Displ - np.min(Displ) - cpHertz - DFLc*Pars.InvOLS
indentationfull = Displ - Displ[0] - cpHertz - DFLc*Pars.InvOLS
funHertzfit = lambda ind, a, b: HertzBEC([a, b], K1, Poisson,
Radius, power, 0, np.nan,
modelprobe, ind)
funDMTfit = lambda ind, a, b, c: DMTBEC([a, b, c], K1, Poisson,
Radius, power, 0, np.nan,
modelprobe, ind)
warnings.simplefilter("ignore")
if EHertz > 0:
FitAppr = funHertzfit(indentationfull, EHertz, 0)
FitDMTBEC = funDMTfit(indentationfull, E_DMT, 0, adhesion)
# plt.plot(indentationfull, DFLc)
# plt.plot(indentationfull, FitAppr)
# plt.plot(indentationfull, FitDMTBEC)
if EHertzBEC > 0:
funHertzfitBEC = lambda ind, a, b: HertzBEC([a, b], K1, Poisson,
Radius, power, Height,
np.nan, modelprobe, ind)
funDMTfit = lambda ind, a, b, c: DMTBEC([a, b, c], K1, Poisson,
Radius, power, 0, np.nan,
modelprobe, ind)
FitApprBEC = funHertzfitBEC(indentationfull, EHertzBEC, 0)
FitDMTBEC = funDMTfit(indentationfull, E_DMT, 0, adhesion)
# plt.plot(Displ, rawDFL)
# plt.plot(indentationfull)
# plt.plot(indentationfull, Forcec)
# plt.plot(indentationfull, FitApprBEC)
# plt.plot(indentationfull, FitDMTBEC)
# plt.plot(currentcurve3[:, 2], currentcurve3[:, 4])
elif (EHertzBEC == 0 or np.isnan(EHertzBEC)) and EHertz > 0:
FitApprBEC = FitAppr
else:
FitApprBEC = np.nan*indentationfull
# locationcpHertz = locate_position(cpHertz, Displ)
# FitApprBEC[:locationcpHertz-1] = np.nan
FitApprBEC[indentationfull<0] = np.nan
# plt.plot(FitApprBEC)
currentcurve3 = np.c_[currentcurve3, indentationfull, Forcec, FitApprBEC, FitDMTBEC]
if E0 > 0:
locationcpTing = locate_position(cpTing, Displ)
[ApprLength, maxZ] = npmax(Displ)
MaxInd = int(ApprLength-locationcpTing) # points in Approach indentation
parf = [E0, alpha_tau, Einf]
indentationfullTing = Displ - cpTing - DFLc*Pars.InvOLS
FullLength = len(indentationfullTing)-1
indend = np.min([locationcpTing+int(MaxInd*2.15), len(indentationfullTing)-1])
for ala in range(ApprLength, FullLength): # alternative search for indend based on Z value
if Displ[ala] < Displ[locationcpTing]:
indend = ala
break
indentationTing = indentationfullTing[locationcpTing+1:indend]
indentationTing[indentationTing<0] = 0
Force_fitT = ting_numerical(parf, adhesion_pars, Poisson, Radius, dT, MaxInd, 0, modelting, modelprobe, indentationTing)[0]
# plt.plot(indentationTing, Force_fitT)
# plt.plot(indentationfullTing, Forcec)
if E0BEC > 0:
locationcpTing = locate_position(cpTing, Displ)
[ApprLength, maxZ] = npmax(Displ)
MaxInd = int(ApprLength-locationcpTing) # points in Approach indentation
parf = [E0BEC, alpha_tauBEC, EinfBEC]
indentationfullTing = Displ - cpTing - DFLc*Pars.InvOLS
FullLength = len(indentationfullTing)-1
indend = np.min([locationcpTing+int(MaxInd*2.15), len(indentationfullTing)-1])
for ala in range(ApprLength, FullLength): # alternative search for indend based on Z value
if Displ[ala] < Displ[locationcpTing]:
indend = ala
break
indentationTing = indentationfullTing[locationcpTing+1:indend]
indentationTing[indentationTing<0] = 0
Force_fitTBEC = ting_numerical(parf, adhesion_pars, Poisson, Radius, dT, MaxInd, Height, modelting, modelprobe, indentationTing)[0]
# plt.plot(indentationTing, Force_fitT)
# plt.plot(currentcurve3[:, 2]-(cpTing-cpHertz), currentcurve3[:, 5])
# plt.plot(indentationfullTing, Forcec)
elif (E0BEC == 0 or np.isnan(E0BEC)) and E0 > 0:
Force_fitTBEC = Force_fitT
Force_fitTBEC2 = np.full(np.shape(Forcec), np.nan)
Force_fitTBEC2[locationcpTing+1:locationcpTing+1+len(Force_fitTBEC)] = Force_fitTBEC
currentcurve3 = np.c_[currentcurve3, Force_fitTBEC2]
# plt.plot(Force_fitTBEC2)
# plt.plot(currentcurve3[:, 2], currentcurve3[:, 5])
# currentcurve3 = np.c_[currentcurve3, indentationfull, Forcec, FitApprBEC, Force_fitTBEC, FitAppr, Force_fitT]
warnings.simplefilter("default")
# print(currentcurve3)
return currentcurve3
if __name__ == '__main__':
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# filename = file_import_dialog_qt5_simplest(import_opt='single', window_title='select files')
# filename = 'D:/MEGAsync/My materials/python/Ting_code/examples/Bruker_forcevolume_cells.dat'
# Pars, Data, Results = load_AFM_data_pickle(filename)
# Pars, Data, Results = load_AFM_data_pickle_short(filename)
# Pars, Data, Results = save_AFM_data_pickle_short(filename, Pars, Data, Results)
# filename= 'D:/MailCloud/AFM_data/BrukerResolve/cytotoxicity/20211118_Ref52_ACR+NaOCL/control.0_000062.dat'
# Pars, Data, Results = load_AFM_data_pickle_short(filename)
kk=1
currentcurve3 = curve_from_saved_pars(Pars, Data[kk], Results.loc[kk, :])
plt.plot(currentcurve3[:, 2], currentcurve3[:, 3])
plt.plot(currentcurve3[:, 2], currentcurve3[:, 4])
plt.plot(currentcurve3[:, 2], currentcurve3[:, 5])