-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateParameterDict_mini.py
executable file
·194 lines (163 loc) · 5.83 KB
/
CreateParameterDict_mini.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
#!/usr/bin/env python3
##!/usr/bin/python3
import os
import glob
import ForceMetric as fm
from ForceMetric import ForceCurve, ParameterDict, IdentifyScanMode
from matplotlib import pyplot as plt
from pprint import pprint
import numpy as np
import seaborn as sb
plot_idx = True # if False plot indentation depth instead of qty vs index
media = '/media/jacob/MicroscopyData/Data/AFM/%s'
sb.set_style("whitegrid")
yes = ['y', 'Y', 'j', 'J', 'Yes', 'yes']
sample = 'Arabidopsis/131Y/0001/DEX/2019-01-15/Sample01_LG_7d24h/%s'
path = media % (sample % '*.ibw')
# The data of the images
data = np.sort(glob.glob(path))[:]
pprint(data)
# automatic recognition of Image and first next Force Curve
Modes = np.array([IdentifyScanMode(p) for p in data])
MaskScan = Modes == 'Imaging'
MaskForce = np.roll(MaskScan, 1)
img_files = data[MaskScan]
force_files = data[MaskForce]
sep = 'd'
img_no = np.array([
os.path.basename(n).split('.')[0]
for n in img_files
])
para_no = [
os.path.basename(n)
for n in force_files
]
path = media % (sample)
if os.path.exists(path % 'Parameters.npy'):
check = input('This file already exists do you want to change it? [y,n] ')
if check not in yes:
print('Quit program')
raise SystemExit
############################################################################
################### Calculation of contact parameters ######################
############################################################################
para_fc = [ForceCurve(path % (pn)) for pn in para_no]
img_files = [path % (n) for n in img_no]
pprint(img_files)
A1_free = []
A1_near = []
phi1_free = []
phi1_near = []
A2_free = []
A2_near = []
phi2_free = []
phi2_near = []
d0 = []
para = []
savepath = path % 'Parameters.npy'
#savepath = '/home/jacob/Desktop/Parameters.npy'
print(os.path.isfile(savepath))
dictionary = ParameterDict(savepath)
print(len(para_fc))
i = -1
for fc in para_fc:
i += 1
for fn in img_files:
print(fn, i)
fc.trace = True
aols = fc['AmpInvOLS']
dols = fc['InvOLS']
d0.append(fc.getData('Defl')[0]/dols)
fc.surfaceidx = 0
# this loop is if the force curves have a bad quality and therefore the
# contactpoint can't be determined. In this case the user can enter a
# number of pixels which should be skipped which usually is enough to
# determine the contact point correctly. Just change the -1 in the next
# line to a list of numbers where you want to aid the algorithm
if i in [-1]:
print('correction loop')
print(fc.force.Trace().shape)
# fc.contactidx = 0
if i == 5:
offset = 5910
fc.force.data[:offset] = fc.force.data[offset + 1]
elif i in [1, 2, 3]:
offset = 3000
fc.force.data[:offset] = fc.force.data[offset + 1]
fc.correct(method='fiv', stds=6)
else:
fc.correct(method='fiv', stds=6)
ind = fc.indentation.Trace()*1e6
f = fc.force.Trace()
p1 = fc.phase1.Trace()
a1 = fc.amp1.Trace()/aols*1e3
idx = fc.contactidx
# The offsets are for the far (off0) and near correction (off1) in
# pixels
off0 = 30
off1 = 100
# Surface correction for individual curves as above with the contact
# point
if i in [-1]:
off0 = 400
off1 = 300
A1_free.append(a1[off0])
A1_near.append(a1[idx-off1])
phi1_free.append(p1[off0])
phi1_near.append(p1[idx-off1])
try:
p2 = fc.phase2.Trace()
a2 = fc.amp2.Trace()/aols*1e3
A2_free.append(a2[off0])
A2_near.append(a2[idx-off1])
phi2_free.append(p2[off0])
phi2_near.append(p2[idx-off1])
except:
A2_free.append(np.nan)
A2_near.append(np.nan)
phi2_free.append(np.nan)
phi2_near.append(np.nan)
E0 = fc.Young('sneddon', fmin=150e-9, fmax=300e-9, constant='force')
para.append({'free A1': A1_free[-1], 'near A1': A1_near[-1],
'free A2': A2_free[-1], 'near A2': A2_near[-1],
'free Phi1': phi1_free[-1], 'near Phi1': phi1_near[-1],
'free Phi2': phi2_free[-1], 'near Phi2': phi2_near[-1],
'Defl offset': d0[-1], 'E0': E0})
dictionary.AddFileInfo(os.path.basename(fn).split('.')[0], para[-1])
key = list(dictionary.parameters.keys())[i]
print("%s \n" % key)
pprint(dictionary.parameters[key])
print("Phase: %.2f \t %.2f" % (p1[off0], p1[idx-off1]))
print("Amplitude: %.2f mV \t %.2f mV" % (a1[off0], a1[idx-off1]))
try:
print("Phase: %.2f \t %.2f" % (p1[off0], p2[idx-off1]))
print("Amplitude: %.2f mV \t %.2f mV" % (a2[off0], a2[idx-off1]))
except:
print('No parameters for 2nd eigenmode available')
print("d0: %.2f V" % d0[-1])
print("E0: %.2e Pa" % E0)
fig, ax = plt.subplots()
ix = fm.nearestPoint(ind, 0)
if plot_idx:
N = np.arange(len(ind))
ax.plot(N, f*1e9, N, p1, N, a1)
ax.plot(N[ix], p1[ix], 'bo')
else:
ax.plot(ind, f*1e9, ind, p1, ind, a1)
ax.set_title("Curve no %i" % i)
try:
plt.plot(ind, f*1e9, ind, p2, ind, a2)
except:
pass
ax.set_ylim(-1.5, 320)
fig.tight_layout()
# plt.grid()
plt.show()
############################################################################
#############End of Calculation of contact parameters ######################
############################################################################
dictionary.Write(savepath)
with open(path % 'filelist.txt', mode='w') as data:
for f in img_files:
data.write(f + '.ibw' + '\n')
# data.close()