-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool_apopins.c
382 lines (305 loc) · 11.8 KB
/
tool_apopins.c
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include "tool_apopins.h"
#include "datatypes.h"
#include "niget-common.h"
#include <math.h>
#include <libprocess/gwyprocess.h>
void init_Apopindata(Apopindata *apopdata)
{
apopdata->nmove = 1;
apopdata->npopin = 0;
apopdata->thresh = THRESH_DEFAULT;
apopdata->thresh2 = THRESH2_DEFAULT;
apopdata->wpop = WPOP_DEFAULT;
apopdata->hpop = HPOP_DEFAULT;
apopdata->Fpopin = NULL;
apopdata->hpopin = NULL;
apopdata->dhpopin = NULL;
apopdata->ileft = NULL;
apopdata->iright = NULL;
apopdata->havg = NULL;
apopdata->dh = NULL;
apopdata->t = NULL;
apopdata->dth = NULL;
apopdata->dth2 = NULL;
}
gboolean apopin_has_results(const Apopindata *apopdata)
{
return (apopdata->npopin > 0);
}
void apopin_remove_results(Apopindata *apopdata)
{
// remove all results
if (apopdata->npopin > 0) {
g_free(apopdata->Fpopin);
g_free(apopdata->hpopin);
g_free(apopdata->dhpopin);
g_free(apopdata->ileft);
g_free(apopdata->iright);
apopdata->Fpopin = NULL;
apopdata->hpopin = NULL;
apopdata->dhpopin = NULL;
apopdata->ileft = NULL;
apopdata->iright = NULL;
}
apopdata->npopin = 0;
}
void apopin_create_aux_datalines(Apopindata *apopdata, const FDdata *fddata)
{
gint i;
// check if pseudo-time data have been already created, if not create them
//check and possibly create constant thresh, thresh2 datalines
if (fddata->hload) {
if (apopdata->t == NULL) {
apopdata->t = gwy_data_line_new_alike(fddata->hload, TRUE); /* LEAK */
for (i = 0; i < apopdata->t->res; i++) {
apopdata->t->data[i] = i;
}
}
else {
if (apopdata->t->res != fddata->hload->res) {
gwy_data_line_resample(apopdata->t, fddata->hload->res, GWY_INTERPOLATION_NONE);
for (i = 0; i < apopdata->t->res; i++) {
apopdata->t->data[i] = i;
}
}
}
if (apopdata->dth == NULL) {
apopdata->dth = gwy_data_line_new_alike(fddata->hload, TRUE); /* LEAK */
gwy_data_line_add(apopdata->dth, apopdata->thresh);
}
else {
gwy_data_line_resample(apopdata->dth, fddata->hload->res, GWY_INTERPOLATION_NONE);
gwy_data_line_clear(apopdata->dth);
gwy_data_line_add(apopdata->dth, apopdata->thresh);
}
if (apopdata->dth2 == NULL) {
apopdata->dth2 = gwy_data_line_new_alike(fddata->hload, TRUE); /* LEAK */
gwy_data_line_add(apopdata->dth2, apopdata->thresh2);
}
else {
gwy_data_line_resample(apopdata->dth2, fddata->hload->res, GWY_INTERPOLATION_NONE);
gwy_data_line_clear(apopdata->dth2);
gwy_data_line_add(apopdata->dth2, apopdata->thresh2);
}
}
}
void apopin_calc(Apopindata *apopdata, const FDdata *fddata)
{
GwyDataLine *x, *xavg;
GwyDataLine *dx;
gint n ;
gint i, j;
/* gdouble xm; */
gdouble thresh, upvalue;
gint prev;
gint ngr;
gint nind;
gint *indices = NULL;
gint **groups = NULL;
gint *ngrind = NULL;
gint wpop;
gdouble hpop;
gdouble thresh2;
gint ileft;
gint iright;
gint npopin;
x = fddata->hload;
n = x->res;
thresh = apopdata->thresh;
thresh2 = apopdata->thresh2;
wpop = apopdata->wpop;
hpop = apopdata->hpop;
// calculate sliding average of depth data
xavg = moving_average(x, apopdata->nmove);
//calculate derivative of smoothed depth
dx = gwy_data_line_new_alike(x, FALSE);
for (i = 0; i < n; i++) {
dx->data[i] = gwy_data_line_get_der(xavg, i);
}
/* xm = gwy_data_line_get_avg(dx); */
/* upvalue = xm + thresh; // works but would require plotting upvalue instead of thresh in apopdata->dth */
upvalue = thresh;
indices = (gint *)g_malloc(n * sizeof(gint));
nind = 0;
// find indices where the derivative is larger than a certain value (= average derivative + threshhold) or (=threshhold)
for (i = 0; i < n; i++) {
if (dx->data[i] > upvalue) {
indices[nind++] = i;
}
}
if (nind == 0) {
ngr = 0;
npopin = 0;
}
else {
indices = (gint *)g_realloc(indices, nind * sizeof(gint));
groups = (gint **) g_malloc(nind * sizeof(gint *));
prev = indices[0];
for (i = 0; i < nind; i++) {
groups[i] = (gint *) g_malloc(nind * sizeof(gint));
}
if (verbose) {
g_print(" nind %d \n", nind);
g_print("indices \n");
for (i = 1; i < nind; i++) {
g_print("indices[%d] %d \n", i, indices[i]);
}
}
ngrind = (gint *) g_malloc(nind * sizeof(gint));
// split indices into groups with consecutive indices, ngr = number of these groups, ngrind = number of indices in each group, groups[igr] = indices belonging to group[igr]
groups[0][0] = prev;
j = 1;
ngr = 0;
if (verbose) {
g_print("--------\n");
}
for (i = 1; i < nind; i++) {
if (indices[i] == prev + 1) {
groups[ngr][j] = indices[i];
prev++;
j++;
if (verbose) {
g_print("add indices[%d]=%d to group %d \n", i, indices[i], ngr);
}
}
else {
if (verbose) {
g_print("finish previous group at ngr %d \n", ngr);
}
ngrind[ngr] = j;
ngr++;
if (verbose) {
g_print("start new group at ngr %d \n", ngr);
}
prev = indices[i];
j = 0;
groups[ngr][j] = indices[i];
j++;
}
}
ngrind[ngr] = j;
if (verbose) {
g_print("finish previous group at ngr %d \n", ngr);
}
ngr++;
}
if (verbose) {
g_print(" ngr %d \n", ngr);
for (i = 0; i < ngr; i++) {
g_print("ngrind[%d] %d \n", i, ngrind[i]);
for (j = 0; j < ngrind[i]; j++) {
g_print(" groups[%d][%d] %d \n", i, j, groups[i][j]);
}
}
}
//copy local data to Data structure
apopdata->havg = gwy_data_line_duplicate(xavg);
apopdata->dh = gwy_data_line_duplicate(dx);
// clean up old data if necessary
if (apopdata->Fpopin != NULL) {
g_free(apopdata->Fpopin);
}
if (apopdata->hpopin != NULL) {
g_free(apopdata->hpopin);
}
if (apopdata->dhpopin != NULL) {
g_free(apopdata->dhpopin);
}
if (apopdata->ileft != NULL) {
g_free(apopdata->ileft);
}
if (apopdata->iright != NULL) {
g_free(apopdata->iright);
}
//allocate new data
apopdata->Fpopin = (gdouble *)g_malloc(ngr * sizeof(gdouble));
apopdata->hpopin = (gdouble *)g_malloc(ngr * sizeof(gdouble));
apopdata->dhpopin = (gdouble *)g_malloc(ngr * sizeof(gdouble));
apopdata->ileft = (gint *)g_malloc(ngr * sizeof(gdouble));
apopdata->iright = (gint *)g_malloc(ngr * sizeof(gdouble));
// starting from a group of consecutive indices, add more indices at both ends so that the derivative is everywhere above threshhold2
// if the width of this group is larger than wpop and the height jump associated with it larger than hpop, then this is a pop-in
npopin = 0;
for (i = 0; i < ngr; i++) {
ileft = groups[i][0];
while (ileft > 0 && dx->data[ileft] > thresh2 && dx->data[ileft] > dx->data[ileft - 1]) {
ileft--;
}
iright = groups[i][ngrind[i] - 1];
while (iright < n - 1 && dx->data[iright] > thresh2 && dx->data[iright] > dx->data[iright + 1]) {
iright++;
}
if ((iright - ileft > wpop) & (x->data[iright] - x->data[ileft] > hpop)) {
//it's a real pop-in, write the data to the Data structure
apopdata->Fpopin[npopin] = gwy_data_line_get_avg(gwy_data_line_part_extract(fddata->Fload, ileft, iright - ileft + 1));
apopdata->hpopin[npopin] = fddata->hload->data[ileft];
apopdata->dhpopin[npopin] = fddata->hload->data[iright] - fddata->hload->data[ileft];
apopdata->ileft[npopin] = ileft;
apopdata->iright[npopin] = iright;
npopin++;
}
}
//realloc data to match
apopdata->Fpopin = (gdouble *)g_realloc(apopdata->Fpopin, npopin * sizeof(gdouble));
apopdata->hpopin = (gdouble *)g_realloc(apopdata->hpopin, npopin * sizeof(gdouble));
apopdata->dhpopin = (gdouble *)g_realloc(apopdata->dhpopin, npopin * sizeof(gdouble));
apopdata->ileft = (gint *)g_realloc(apopdata->ileft, npopin * sizeof(gint));
apopdata->iright = (gint *)g_realloc(apopdata->iright, npopin * sizeof(gint));
apopdata->npopin = npopin;
g_object_unref(xavg);
g_object_unref(dx);
if (indices != NULL) {
g_free(indices);
}
if (ngrind != NULL) {
g_free(ngrind);
}
if (groups != NULL) {
for (i = 0 ; i < nind; i++) {
g_free(groups[i]);
}
g_free(groups);
}
}
gchar* apopin_export_data(const Apopindata *apopdata, const FDdata *fddata, enum ExportFormat exportformat)
{
gint i, j;
GString *buf;
buf = g_string_new(NULL);
g_string_append_printf(buf, "# %s \n", fddata->filename);
g_string_append_printf(buf, "#Popin detection method A \n");
g_string_append_printf(buf, "#Input parameters:\n");
g_string_append_printf(buf, "#moving average window width %d \n", apopdata->nmove);
g_string_append_printf(buf, "#threshhold of depth derivative for identification of popin %g \n", apopdata->thresh);
g_string_append_printf(buf, "#threshhold of depth derivative for determination of popin width/height %g \n", apopdata->thresh2);
g_string_append_printf(buf, "#minimum popin width %d \n", apopdata->wpop);
g_string_append_printf(buf, "#minimum popin height %g \n", apopdata->hpop);
g_string_append_printf(buf, "\n\n");
g_string_append_printf(buf, "\n#Popins found by method A : %d \n", apopdata->npopin);
if (apopdata->npopin) {
g_string_append_printf(buf, "# load/mN depth/mN depth difference/mN index left-end index right-end \n");
for (i = 0; i < apopdata->npopin; i++) {
g_string_append_printf(buf, "#%d %g %g %g %d %d \n", i + 1,
apopdata->Fpopin[i], apopdata->hpopin[i], apopdata->dhpopin[i], apopdata->ileft[i], apopdata->iright[i]);
}
g_string_append_printf(buf, "\n\n");
g_string_append_printf(buf, "#Popin depth data \n");
for (i = 0; i < apopdata->npopin; i++) {
g_string_append_printf(buf, "#Popin #%d\n", i + 1);
g_string_append_printf(buf, "# index i hload/nm havg/nm Fload/nm dh/di threshold height threshold width \n");
for (j = apopdata->ileft[i]; j <= apopdata->iright[i]; j++)
g_string_append_printf(buf, "%g %g %g %g %g %g %g\n",
apopdata->t->data[j], fddata->hload->data[j], apopdata->havg->data[j],
fddata->Fload->data[j], apopdata->dh->data[j], apopdata->thresh, apopdata->thresh2);
g_string_append_printf(buf, "\n\n");
}
g_string_append_printf(buf, "\n\n");
g_string_append_printf(buf, "\n\n");
g_string_append_printf(buf, "# index i hload/nm havg/nm Fload/nm dh/di threshold height threshold width \n");
for (j = 0; j < apopdata->havg->res; j++)
g_string_append_printf(buf, "%g %g %g %g %g %g %g\n",
apopdata->t->data[j], fddata->hload->data[j], apopdata->havg->data[j],
fddata->Fload->data[j], apopdata->dh->data[j], apopdata->thresh, apopdata->thresh2);
}
return g_string_free(buf, FALSE);
}