-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmorphology.c
1455 lines (1186 loc) · 43.9 KB
/
morphology.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// morphology.c-- genodsp operators performing Minkowkski morphology operations
#include <stdlib.h>
#define true 1
#define false 0
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>
#include <float.h>
#include "utilities.h"
#include "genodsp_interface.h"
#include "morphology.h"
//----------
// [[-- a dsp operation function group, operating on a single chromosome --]]
//
// See genodsp_interface.h, "headers for dsp operator function groups" for
// function descriptions and argument details.
//
//----------
//
// op_close--
// Perform a "close" operation, in the sense of Minkowski/morphology set
// operations on covered intervals.
//
// The incoming signal is first binarized (conceptually), and the result is
// 1 or 0 depending depending on whether a position is within the closure or
// not.
//
//----------
// private dspop subtype
typedef struct dspop_close
{
dspop common; // common elements shared with all operators
u32 closingLength;
int haveThreshold;
char* thresholdVarName;
valtype threshold;
valtype oneVal;
valtype zeroVal;
int debug;
} dspop_close;
// op_close_short--
void op_close_short (char* name, int nameWidth, FILE* f, char* indent)
{
int nameFill = nameWidth-2 - strlen(name);
if (indent == NULL) indent = "";
if (nameFill > 0) fprintf (f, "%s%s:%*s", indent, name, nameFill+1, " ");
else fprintf (f, "%s%s: ", indent, name);
fprintf (f, "apply interval closure (fill small gaps between intervals)\n");
}
// op_close_usage--
void op_close_usage (char* name, FILE* f, char* indent)
{
if (indent == NULL) indent = "";
// 3456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789
fprintf (f, "%sCompute the morphological closure of the current set of intervals. This is\n", indent);
fprintf (f, "%sequivalent to \"filling\" short gaps between intervals.\n", indent);
fprintf (f, "%s\n", indent);
fprintf (f, "%sThe current signal is first binarized according to the given threshold; any\n", indent);
fprintf (f, "%slocations above the threshold are considered to be \"in\" the set. Closure is\n", indent);
fprintf (f, "%sthen performed, adding any short gaps into the set. The result is one for\n", indent);
fprintf (f, "%slocations in the resulting zet, zero otherwise. Note that short gaps at the\n", indent);
fprintf (f, "%send of chromosomes are NOT filled.\n", indent);
fprintf (f, "%s\n", indent);
fprintf (f, "%sStrictly speaking, the length parameter is twice the true closure length. For\n", indent);
fprintf (f, "%smore information on set morphology, see\n", indent);
fprintf (f, "%s en.wikipedia.org/wiki/Closing_(morphology).\n", indent);
fprintf (f, "%s\n", indent);
fprintf (f, "%susage: %s <length> [options]\n", indent, name);
fprintf (f, "%s <length> the \"length\" of closure; gaps of this length\n", indent);
fprintf (f, "%s or shorter will be filled\n", indent);
fprintf (f, "%s --threshold=<value> (T=) a binarization threshold; values above this\n", indent);
fprintf (f, "%s are considered to be \"in\" the incoming intervals\n", indent);
fprintf (f, "%s (default is 0.0)\n", indent);
fprintf (f, "%s <value> can be a named variable\n", indent);
fprintf (f, "%s --one=<value> (O=) value for locations in the closure\n", indent);
fprintf (f, "%s (default is 1.0)\n", indent);
fprintf (f, "%s --zero=<value> (Z=) value for locations not in the closure\n", indent);
fprintf (f, "%s (default is 0.0)\n", indent);
}
// op_close_parse--
dspop* op_close_parse (char* name, int _argc, char** _argv)
{
dspop_close* op;
int argc = _argc;
char** argv = _argv;
char* arg, *argVal;
valtype tempVal;
int haveLength;
// allocate and initialize our control record
op = (dspop_close*) malloc (sizeof(dspop_close));
if (op == NULL) goto cant_allocate;
op->common.atRandom = false;
op->closingLength = 0; // not used, user is required to set it
op->haveThreshold = false;
op->thresholdVarName = NULL;
op->threshold = 0.0;
op->oneVal = 1.0;
op->zeroVal = 0.0;
op->debug = false;
// parse arguments
haveLength = false;
while (argc > 0)
{
arg = argv[0];
argVal = strchr(arg,'=');
if (argVal != NULL) argVal++;
// --threshold=<variable> or T=<variable>
if ((strcmp_prefix (arg, "--threshold=") == 0)
|| (strcmp_prefix (arg, "T=") == 0)
|| (strcmp_prefix (arg, "--T=") == 0))
{
if (op->haveThreshold) goto more_than_one_threshold;
if (!try_string_to_valtype (argVal, &op->threshold))
op->thresholdVarName = copy_string (argVal);
else
op->threshold = string_to_valtype (argVal);
op->haveThreshold = true;
goto next_arg;
}
// --one=<value> or O=<value>
if ((strcmp_prefix (arg, "--one=") == 0)
|| (strcmp_prefix (arg, "O=") == 0)
|| (strcmp_prefix (arg, "--O=") == 0))
{
tempVal = string_to_valtype (argVal);
op->oneVal = tempVal;
goto next_arg;
}
// --zero=<value> or Z=<value>
if ((strcmp_prefix (arg, "--zero=") == 0)
|| (strcmp_prefix (arg, "Z=") == 0)
|| (strcmp_prefix (arg, "--Z=") == 0))
{
tempVal = string_to_valtype (argVal);
op->zeroVal = tempVal;
goto next_arg;
}
// --debug argument
if (strcmp (arg, "--debug") == 0)
{ op->debug = true; goto next_arg; }
// unknown -- argument
if (strcmp_prefix (arg, "--") == 0)
chastise ("[%s] Can't understand \"%s\"\n", name, arg);
// <length>
if (!haveLength)
{
op->closingLength = (u32) string_to_unitized_int (arg, /*thousands*/ true);
haveLength = true;
goto next_arg;
}
// unknown argument
chastise ("[%s] Can't understand \"%s\"\n", name, arg);
next_arg:
argv++; argc--;
continue;
}
if (!haveLength) goto length_missing;
return (dspop*) op;
cant_allocate:
fprintf (stderr, "[%s] failed to allocate control record (%d bytes)\n",
name, (int) sizeof(dspop_close));
exit(EXIT_FAILURE);
return NULL; // (never reaches here)
more_than_one_threshold:
fprintf (stderr, "[%s] threshold specified more than once (at \"%s\")\n",
name, arg);
exit(EXIT_FAILURE);
return NULL; // (never reaches here)
length_missing:
fprintf (stderr, "[%s] closing length was not provided\n", name);
exit(EXIT_FAILURE);
return NULL; // (never reaches here)
}
// op_close_free--
void op_close_free (dspop* _op)
{
dspop_close* op = (dspop_close*) _op;
if (op->thresholdVarName != NULL) free (op->thresholdVarName);
free (op);
}
// op_close_apply--
void op_close_apply
(arg_dont_complain(dspop* _op),
arg_dont_complain(char* vName),
arg_dont_complain(u32 vLen),
arg_dont_complain(valtype* v))
{
dspop_close* op = (dspop_close*) _op;
valtype closingLength = op->closingLength;
valtype cutoffThresh = op->threshold;
valtype oneVal = op->oneVal;
valtype zeroVal = op->zeroVal;
int ok;
int inGap;
u32 gapStartIx = 0; // placate compiler
u32 ix, scanIx;
// if the threshold is a named variable, fetch it now; note that we copy
// the value from the named variable, then destroy our reference to the
// named variable
if (op->thresholdVarName != NULL)
{
ok = named_global_exists (op->thresholdVarName, &cutoffThresh);
if (!ok) goto no_threshold;
op->threshold = cutoffThresh;
fprintf (stderr, "[%s] using %s = " valtypeFmt " as threshold\n",
_op->name, op->thresholdVarName, cutoffThresh);
free (op->thresholdVarName);
op->thresholdVarName = NULL;
}
// process the vector, filling short gaps (and binarizing); note that we
// never fill the first gap because its "true" length is infinite (it
// includes everything on the number line before the start of the
// chromosome)
if (v[0] <= cutoffThresh) { gapStartIx = 0; inGap = true; }
else { v[0] = oneVal; inGap = false; }
for (ix=1 ; ix<vLen ; ix++)
{
if (v[ix] <= cutoffThresh) // new location is in a gap
{
if (inGap) continue;
gapStartIx = ix;
inGap = true;
}
else // new location is in an interval
{
v[ix] = oneVal;
if (!inGap) continue;
if ((gapStartIx == 0) || (ix - gapStartIx > closingLength))
{
if (op->debug) fprintf (stderr, "clear %u bp gap from %u..%u\n",
ix-gapStartIx, gapStartIx, ix);
for (scanIx=gapStartIx ; scanIx<ix ; scanIx++) v[scanIx] = zeroVal;
}
else
{
if (op->debug) fprintf (stderr, "fill %u bp gap from %u..%u\n",
ix-gapStartIx, gapStartIx, ix);
for (scanIx=gapStartIx ; scanIx<ix ; scanIx++) v[scanIx] = oneVal;
}
inGap = false;
}
}
// clear the final gap; we never fill it because its "true" length is
// infinite (it includes everything on the number line beyond the end of
// the chromosome)
if (inGap)
{
if (op->debug) fprintf (stderr, "clear %u bp gap from %u..%u\n",
vLen-gapStartIx, gapStartIx, vLen);
for (scanIx=gapStartIx ; scanIx<vLen ; scanIx++) v[scanIx] = zeroVal;
}
// success
return;
// failure
no_threshold:
fprintf (stderr, "[%s] attempt to use %s as threshold failed (no such variable)\n",
_op->name, op->thresholdVarName);
exit(EXIT_FAILURE);
}
//----------
// [[-- a dsp operation function group, operating on a single chromosome --]]
//
// See genodsp_interface.h, "headers for dsp operator function groups" for
// function descriptions and argument details.
//
//----------
//
// op_open--
// Perform a "open" operation, in the sense of Minkowski/morphology set
// operations on covered intervals.
//
// The incoming signal is first binarized (conceptually), and the result is
// 1 or 0 depending depending on whether a position is within the opened set
// or not.
//
//----------
// private dspop subtype
typedef struct dspop_open
{
dspop common; // common elements shared with all operators
u32 openingLength;
int haveThreshold;
char* thresholdVarName;
valtype threshold;
valtype oneVal;
valtype zeroVal;
} dspop_open;
// op_open_short--
void op_open_short (char* name, int nameWidth, FILE* f, char* indent)
{
int nameFill = nameWidth-2 - strlen(name);
if (indent == NULL) indent = "";
if (nameFill > 0) fprintf (f, "%s%s:%*s", indent, name, nameFill+1, " ");
else fprintf (f, "%s%s: ", indent, name);
fprintf (f, "apply interval opening (remove small intervals)\n");
}
// op_open_usage--
void op_open_usage (char* name, FILE* f, char* indent)
{
if (indent == NULL) indent = "";
// 3456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789
fprintf (f, "%sCompute the morphological opening of the current set of intervals. This is\n", indent);
fprintf (f, "%sequivalent to \"removing\" short intervals.\n", indent);
fprintf (f, "%s\n", indent);
fprintf (f, "%sThe current signal is first binarized according to the given threshold; any\n", indent);
fprintf (f, "%slocations above the threshold are considered to be \"in\" the set. Opening is\n", indent);
fprintf (f, "%sthen performed, removing any short intervals from the set. The result is one\n", indent);
fprintf (f, "%sfor locations in the resulting zet, zero otherwise.\n", indent);
fprintf (f, "%s\n", indent);
fprintf (f, "%sStrictly speaking, the length parameter is twice the true opening length. For\n", indent);
fprintf (f, "%smore information on set morphology, see\n", indent);
fprintf (f, "%s en.wikipedia.org/wiki/Opening_(morphology).\n", indent);
fprintf (f, "%s\n", indent);
fprintf (f, "%susage: %s <length> [options]\n", indent, name);
fprintf (f, "%s <length> the \"length\" of opening; intervals of this\n", indent);
fprintf (f, "%s length or shorter will be removed\n", indent);
fprintf (f, "%s --threshold=<value> (T=) a binarization threshold; values above this\n", indent);
fprintf (f, "%s are considered to be \"in\" the incoming intervals\n", indent);
fprintf (f, "%s (default is 0.0)\n", indent);
fprintf (f, "%s <value> can be a named variable\n", indent);
fprintf (f, "%s --one=<value> (O=) value for locations in the opened set\n", indent);
fprintf (f, "%s (default is 1.0)\n", indent);
fprintf (f, "%s --zero=<value> (Z=) value for locations not in the opened set\n", indent);
fprintf (f, "%s (default is 0.0)\n", indent);
}
// op_open_parse--
dspop* op_open_parse (char* name, int _argc, char** _argv)
{
dspop_open* op;
int argc = _argc;
char** argv = _argv;
char* arg, *argVal;
valtype tempVal;
int haveLength;
// allocate and initialize our control record
op = (dspop_open*) malloc (sizeof(dspop_open));
if (op == NULL) goto cant_allocate;
op->common.atRandom = false;
op->openingLength = 0; // not used, user is required to set it
op->haveThreshold = false;
op->thresholdVarName = NULL;
op->threshold = 0.0;
op->oneVal = 1.0;
op->zeroVal = 0.0;
// parse arguments
haveLength = false;
while (argc > 0)
{
arg = argv[0];
argVal = strchr(arg,'=');
if (argVal != NULL) argVal++;
// --threshold=<variable> or T=<variable>
if ((strcmp_prefix (arg, "--threshold=") == 0)
|| (strcmp_prefix (arg, "T=") == 0)
|| (strcmp_prefix (arg, "--T=") == 0))
{
if (op->haveThreshold) goto more_than_one_threshold;
if (!try_string_to_valtype (argVal, &op->threshold))
op->thresholdVarName = copy_string (argVal);
else
op->threshold = string_to_valtype (argVal);
op->haveThreshold = true;
goto next_arg;
}
// --one=<value> or O=<value>
if ((strcmp_prefix (arg, "--one=") == 0)
|| (strcmp_prefix (arg, "O=") == 0)
|| (strcmp_prefix (arg, "--O=") == 0))
{
tempVal = string_to_valtype (argVal);
op->oneVal = tempVal;
goto next_arg;
}
// --zero=<value> or Z=<value>
if ((strcmp_prefix (arg, "--zero=") == 0)
|| (strcmp_prefix (arg, "Z=") == 0)
|| (strcmp_prefix (arg, "--Z=") == 0))
{
tempVal = string_to_valtype (argVal);
op->zeroVal = tempVal;
goto next_arg;
}
// unknown -- argument
if (strcmp_prefix (arg, "--") == 0)
chastise ("[%s] Can't understand \"%s\"\n", name, arg);
// <length>
if (!haveLength)
{
op->openingLength = (u32) string_to_unitized_int (arg, /*thousands*/ true);
haveLength = true;
goto next_arg;
}
// unknown argument
chastise ("[%s] Can't understand \"%s\"\n", name, arg);
next_arg:
argv++; argc--;
continue;
}
if (!haveLength) goto length_missing;
return (dspop*) op;
cant_allocate:
fprintf (stderr, "[%s] failed to allocate control record (%d bytes)\n",
name, (int) sizeof(dspop_open));
exit(EXIT_FAILURE);
return NULL; // (never reaches here)
more_than_one_threshold:
fprintf (stderr, "[%s] threshold specified more than once (at \"%s\")\n",
name, arg);
exit(EXIT_FAILURE);
return NULL; // (never reaches here)
length_missing:
fprintf (stderr, "[%s] opening length was not provided\n", name);
exit(EXIT_FAILURE);
return NULL; // (never reaches here)
}
// op_open_free--
void op_open_free (dspop* _op)
{
dspop_open* op = (dspop_open*) _op;
if (op->thresholdVarName != NULL) free (op->thresholdVarName);
free (op);
}
// op_open_apply--
void op_open_apply
(arg_dont_complain(dspop* _op),
arg_dont_complain(char* vName),
arg_dont_complain(u32 vLen),
arg_dont_complain(valtype* v))
{
dspop_open* op = (dspop_open*) _op;
valtype openingLength = op->openingLength;
valtype cutoffThresh = op->threshold;
valtype oneVal = op->oneVal;
valtype zeroVal = op->zeroVal;
int ok;
int inInterval;
u32 intervalStartIx = 0; // placate compiler
u32 ix, scanIx;
// if the threshold is a named variable, fetch it now; note that we copy
// the value from the named variable, then destroy our reference to the
// named variable
if (op->thresholdVarName != NULL)
{
ok = named_global_exists (op->thresholdVarName, &cutoffThresh);
if (!ok) goto no_threshold;
op->threshold = cutoffThresh;
fprintf (stderr, "[%s] using %s = " valtypeFmt " as threshold\n",
_op->name, op->thresholdVarName, cutoffThresh);
free (op->thresholdVarName);
op->thresholdVarName = NULL;
}
// process the vector, clearing short intervals (and binarizing)
if (v[0] > cutoffThresh) { intervalStartIx = 0; inInterval = true; }
else { v[0] = zeroVal; inInterval = false; }
for (ix=1 ; ix<vLen ; ix++)
{
if (v[ix] > cutoffThresh) // new location is in an interval
{
if (inInterval) continue;
intervalStartIx = ix;
inInterval = true;
}
else // new location is in a gap
{
v[ix] = zeroVal;
if (!inInterval) continue;
if (ix - intervalStartIx > openingLength)
{ for (scanIx=intervalStartIx ; scanIx<ix ; scanIx++) v[scanIx] = oneVal; }
else
{ for (scanIx=intervalStartIx ; scanIx<ix ; scanIx++) v[scanIx] = zeroVal; }
inInterval = false;
}
}
// fill or clear the final interval
if (inInterval)
{
if (vLen - intervalStartIx > openingLength)
{ for (scanIx=intervalStartIx ; scanIx<vLen ; scanIx++) v[scanIx] = oneVal; }
else
{ for (scanIx=intervalStartIx ; scanIx<vLen ; scanIx++) v[scanIx] = zeroVal; }
}
// success
return;
// failure
no_threshold:
fprintf (stderr, "[%s] attempt to use %s as threshold failed (no such variable)\n",
_op->name, op->thresholdVarName);
exit(EXIT_FAILURE);
}
//----------
// [[-- a dsp operation function group, operating on a single chromosome --]]
//
// See genodsp_interface.h, "headers for dsp operator function groups" for
// function descriptions and argument details.
//
//----------
//
// op_dilate--
// Perform a "dilation" operation, in the sense of Minkowski/morphology set
// operations on covered intervals.
//
// The incoming signal is first binarized (conceptually), and the result is
// 1 or 0 depending depending on whether a position is within the dilation set
// or not.
//
//----------
// private dspop subtype
typedef struct dspop_dilate
{
dspop common; // common elements shared with all operators
u32 dilationLength;
u32 leftDilation;
u32 rightDilation;
int haveThreshold;
char* thresholdVarName;
valtype threshold;
valtype oneVal;
valtype zeroVal;
int debug;
} dspop_dilate;
// op_dilate_short--
void op_dilate_short (char* name, int nameWidth, FILE* f, char* indent)
{
int nameFill = nameWidth-2 - strlen(name);
if (indent == NULL) indent = "";
if (nameFill > 0) fprintf (f, "%s%s:%*s", indent, name, nameFill+1, " ");
else fprintf (f, "%s%s: ", indent, name);
fprintf (f, "apply interval dilation (widen intervals)\n");
}
// op_dilate_usage--
void op_dilate_usage (char* name, FILE* f, char* indent)
{
if (indent == NULL) indent = "";
// 3456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789
fprintf (f, "%sCompute the morphological dilation of the current set of intervals. This is\n", indent);
fprintf (f, "%sequivalent to widening intervals.\n", indent);
fprintf (f, "%s\n", indent);
fprintf (f, "%sThe current signal is first binarized according to the given threshold; any\n", indent);
fprintf (f, "%slocations above the threshold are considered to be \"in\" the set. Dilation is\n", indent);
fprintf (f, "%sthen performed, widening any intervals in the set. The result is one for\n", indent);
fprintf (f, "%slocations in the resulting zet, zero otherwise.\n", indent);
fprintf (f, "%s\n", indent);
fprintf (f, "%sWe assume that the signal is equivalent to a zero beyond the left and right\n", indent);
fprintf (f, "%sends of the vector.\n", indent);
fprintf (f, "%s\n", indent);
fprintf (f, "%sStrictly speaking, the length parameter is twice the true dilation length.\n", indent);
fprintf (f, "%sFor more information on set morphology, see\n", indent);
fprintf (f, "%s en.wikipedia.org/wiki/Dilation_(morphology).\n", indent);
fprintf (f, "%s\n", indent);
fprintf (f, "%susage: %s <length> [options]\n", indent, name);
fprintf (f, "%s <length> the \"length\" of dilation; intervals will be\n", indent);
fprintf (f, "%s widened by this amount, half on each side\n", indent);
fprintf (f, "%s --threshold=<value> (T=) a binarization threshold; values above this\n", indent);
fprintf (f, "%s are considered to be \"in\" the incoming intervals\n", indent);
fprintf (f, "%s (default is 0.0)\n", indent);
fprintf (f, "%s <value> can be a named variable\n", indent);
fprintf (f, "%s --one=<value> (O=) value for locations in the dilation set\n", indent);
fprintf (f, "%s (default is 1.0)\n", indent);
fprintf (f, "%s --zero=<value> (Z=) value for locations not in the dilation set\n", indent);
fprintf (f, "%s (default is 0.0)\n", indent);
fprintf (f, "%s --left=<length> intervals will be widened by this amount, only on\n", indent);
fprintf (f, "%s the left side\n", indent);
fprintf (f, "%s --right=<length> intervals will be widened by this amount, only on\n", indent);
fprintf (f, "%s the right side\n", indent);
}
// op_dilate_parse--
dspop* op_dilate_parse (char* name, int _argc, char** _argv)
{
dspop_dilate* op;
int argc = _argc;
char** argv = _argv;
char* arg, *argVal;
valtype tempVal;
int haveLength;
// allocate and initialize our control record
op = (dspop_dilate*) malloc (sizeof(dspop_dilate));
if (op == NULL) goto cant_allocate;
op->common.atRandom = false;
op->dilationLength = 0; // not used, user is required to set it
op->leftDilation = 0;
op->rightDilation = 0;
op->haveThreshold = false;
op->thresholdVarName = NULL;
op->threshold = 0.0;
op->oneVal = 1.0;
op->zeroVal = 0.0;
op->debug = false;
// parse arguments
haveLength = false;
while (argc > 0)
{
arg = argv[0];
argVal = strchr(arg,'=');
if (argVal != NULL) argVal++;
// --threshold=<variable> or T=<variable>
if ((strcmp_prefix (arg, "--threshold=") == 0)
|| (strcmp_prefix (arg, "T=") == 0)
|| (strcmp_prefix (arg, "--T=") == 0))
{
if (op->haveThreshold) goto more_than_one_threshold;
if (!try_string_to_valtype (argVal, &op->threshold))
op->thresholdVarName = copy_string (argVal);
else
op->threshold = string_to_valtype (argVal);
op->haveThreshold = true;
goto next_arg;
}
// --one=<value> or O=<value>
if ((strcmp_prefix (arg, "--one=") == 0)
|| (strcmp_prefix (arg, "O=") == 0)
|| (strcmp_prefix (arg, "--O=") == 0))
{
tempVal = string_to_valtype (argVal);
op->oneVal = tempVal;
goto next_arg;
}
// --zero=<value> or Z=<value>
if ((strcmp_prefix (arg, "--zero=") == 0)
|| (strcmp_prefix (arg, "Z=") == 0)
|| (strcmp_prefix (arg, "--Z=") == 0))
{
tempVal = string_to_valtype (argVal);
op->zeroVal = tempVal;
goto next_arg;
}
// --left=<length> or --right=<length>
if (strcmp_prefix (arg, "--left=") == 0)
{
if (strcmp_suffix (argVal, "-1") == 0)
{
char* tempStr = copy_string (argVal);
tempStr[strlen(tempStr)-strlen("-1")] = 0;
op->leftDilation = string_to_unitized_int(tempStr,/*thousands*/true) - 1;
free(tempStr);
}
else
{
op->leftDilation = string_to_unitized_int(argVal,/*thousands*/true);
}
goto next_arg;
}
if (strcmp_prefix (arg, "--right=") == 0)
{
if (strcmp_suffix (argVal, "-1") == 0)
{
char* tempStr = copy_string (argVal);
tempStr[strlen(tempStr)-strlen("-1")] = 0;
op->rightDilation = string_to_unitized_int(tempStr,/*thousands*/true) - 1;
free(tempStr);
}
else
{
op->rightDilation = string_to_unitized_int(argVal,/*thousands*/true);
}
goto next_arg;
}
// --debug argument
if (strcmp (arg, "--debug") == 0)
{ op->debug = true; goto next_arg; }
// unknown -- argument
if (strcmp_prefix (arg, "--") == 0)
chastise ("[%s] Can't understand \"%s\"\n", name, arg);
// <length>
if (!haveLength)
{
op->dilationLength = (u32) string_to_unitized_int (arg, /*thousands*/ true);
haveLength = true;
goto next_arg;
}
// unknown argument
chastise ("[%s] Can't understand \"%s\"\n", name, arg);
next_arg:
argv++; argc--;
continue;
}
// user has to specify the length, OR specify a left-only or right-only
// length
if (haveLength)
{
if ((op->leftDilation != 0) || (op->rightDilation != 0)) goto more_than_one_length;
}
else // if (!haveLength)
{
if ((op->leftDilation == 0) && (op->rightDilation == 0)) goto length_missing;
}
return (dspop*) op;
cant_allocate:
fprintf (stderr, "[%s] failed to allocate control record (%d bytes)\n",
name, (int) sizeof(dspop_dilate));
exit(EXIT_FAILURE);
return NULL; // (never reaches here)
more_than_one_threshold:
fprintf (stderr, "[%s] threshold specified more than once (at \"%s\")\n",
name, arg);
exit(EXIT_FAILURE);
return NULL; // (never reaches here)
length_missing:
fprintf (stderr, "[%s] dilation length was not provided\n", name);
exit(EXIT_FAILURE);
return NULL; // (never reaches here)
more_than_one_length:
fprintf (stderr, "[%s] dilation length was provided in more than one way\n", name);
exit(EXIT_FAILURE);
return NULL; // (never reaches here)
}
// op_dilate_free--
void op_dilate_free (dspop* _op)
{
dspop_dilate* op = (dspop_dilate*) _op;
if (op->thresholdVarName != NULL) free (op->thresholdVarName);
free (op);
}
// op_dilate_apply--
void op_dilate_apply
(arg_dont_complain(dspop* _op),
arg_dont_complain(char* vName),
arg_dont_complain(u32 vLen),
arg_dont_complain(valtype* v))
{
dspop_dilate* op = (dspop_dilate*) _op;
valtype dilationLength = op->dilationLength;
valtype cutoffThresh = op->threshold;
valtype oneVal = op->oneVal;
valtype zeroVal = op->zeroVal;
int ok;
int inInterval;
u32 leftDilation, rightDilation;
u32 gapStartIx, leftIx, rightIx, ix, scanIx;
// if the threshold is a named variable, fetch it now; note that we copy
// the value from the named variable, then destroy our reference to the
// named variable
if (op->thresholdVarName != NULL)
{
ok = named_global_exists (op->thresholdVarName, &cutoffThresh);
if (!ok) goto no_threshold;
op->threshold = cutoffThresh;
fprintf (stderr, "[%s] using %s = " valtypeFmt " as threshold\n",
_op->name, op->thresholdVarName, cutoffThresh);
free (op->thresholdVarName);
op->thresholdVarName = NULL;
}
// process the vector, widening intervals (and binarizing); note that
// locations in intervals are binarized immediately; locations in gaps
// are not binarized until we have determined the start and end of the gap
if ((op->leftDilation == 0) && (op->rightDilation == 0))
{
leftDilation = dilationLength / 2;
rightDilation = dilationLength - leftDilation;
}
else
{
leftDilation = op->leftDilation;
rightDilation = op->rightDilation;
}
gapStartIx = 0;
inInterval = (v[0] > cutoffThresh);
if (inInterval) v[0] = oneVal;
for (ix=1 ; ix<vLen ; ix++)
{
if (v[ix] <= cutoffThresh) // new location is in a gap
{
if (!inInterval) continue;
// we've encountered the start of a new gap; record the start
gapStartIx = ix;
inInterval = false;
}
else // new location is in an interval
{
v[ix] = oneVal;
if (inInterval) continue;
// we've encountered the start of a new interval; set and/or clear
// items to narrow the gap between this interval and the last
if (op->debug) fprintf (stderr, "%u bp gap from %u..%u\n",
ix-gapStartIx, gapStartIx, ix);
if (gapStartIx == 0)
{
// gap is at left edge, so there's no fill on the gap's left
if (ix <= leftDilation)
{
// left-extension of new interval reaches left edge
if (op->debug)
{
fprintf (stderr, " narrowed gap is -%u..%u\n", leftDilation-ix, 0);
fprintf (stderr, " set %u..%u\n", 0, ix);
}
for (scanIx=0 ; scanIx<ix ; scanIx++) v[scanIx] = oneVal;
}
else
{
// left-extension of new interval leaves a gap at left edge
leftIx = ix - leftDilation;
if (op->debug)
{
fprintf (stderr, " narrowed gap is %u..%u\n", 0, leftIx);
fprintf (stderr, " clear %u..%u\n", 0, leftIx);
fprintf (stderr, " set %u..%u\n", leftIx, ix);
}
for (scanIx=0 ; scanIx<leftIx ; scanIx++) v[scanIx] = zeroVal;
for ( ; scanIx<ix ; scanIx++) v[scanIx] = oneVal;
}
}
else
{
rightIx = gapStartIx + rightDilation;
leftIx = (ix <= leftDilation)? 0 : ix - leftDilation;
if (op->debug) fprintf (stderr, " narrowed gap is %u..%u\n", rightIx, leftIx);
if (rightIx >= leftIx)
{
// right-extension of previous interval and left-extension
// of new interval overlap (or abut), so gap disappears
if (op->debug)
fprintf (stderr, " set %u..%u\n", gapStartIx, ix);
for (scanIx=gapStartIx ; scanIx<ix ; scanIx++) v[scanIx] = oneVal;
}
else
{