-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pro_Feat_Fit.js
executable file
·1684 lines (1432 loc) · 61.9 KB
/
Pro_Feat_Fit.js
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
// ProFeatFit script by Christophe Leterrier
// Profiles, Feature detection (begin/max/end), Curve fitting of the profile, Alignment of profiles
// 14-06-19 Adapted to new Fiji with call to classes
importClass(Packages.java.awt.Color);
importClass(Packages.ij.gui.Overlay);
importClass(Packages.java.awt.Polygon);
importClass(Packages.java.lang.Double);
importClass(Packages.ij.IJ);
importClass(Packages.ij.gui.GenericDialog);
importClass(Packages.ij.plugin.frame.RoiManager);
importClass(Packages.ij.plugin.Duplicator);
importClass(Packages.ij.gui.ProfilePlot);
importClass(Packages.java.lang.Double);
importClass(Packages.ij.measure.CurveFitter);
importClass(Packages.ij.gui.Plot);
importClass(Packages.ij.process.ByteProcessor);
importClass(Packages.ij.gui.Roi);
importClass(Packages.ij.process.ImageProcessor);
importClass(Packages.ij.plugin.filter.ThresholdToSelection);
importClass(Packages.ij.gui.PointRoi);
importClass(Packages.ij.ImageStack);
importClass(Packages.ij.ImagePlus);
importClass(Packages.ij.measure.ResultsTable);
importClass(Packages.ij.gui.PolygonRoi);
IJ.log("\n*****************************************************\nProFeatFit has started!\n*****************************************************");
//**************************
// Input Variables (SR)
//**************************
var ChooseTypeD = false; // Process specific categories?
var TypeNameD = "Axon"; // Name of categories to process
var ProfileWidthD = 50; // total width of the line trace for profile, 5 for AIS
var HalfWidthD = 0; // sliding window smoothing along the profile, 0 for no smoothing, usually 10
var ScalePlotsD = true; // present each plot scaled in Y?
var getFeatureD = false; // compute feature (with threshold-based begin/max/end)?
var beginThresholdD = 0.30; // threshold for begin, 0.30 for AIS
var endThresholdD = 0.30; // threshold for end, 0.30 for AIS
var FeatureTypeArray = new Array("small", "large");
var FeatureTypeD = "small"; // choose "small" or "large" relative to threshold (small = first time under from max, large = first time above from extremities)
var getFitD = true; // compute a fit of the profile?
var FitYSourceArray = new Array("PRawProfile", "PSmoothProfile"); // "PNormProfile", "PRawNormProfile" not supported yet (plotting problem)
var FitYSourceD = "PRawProfile" // which profile to fit: PRawProfile, PSmoothProfile, PNormProfile, PRawNormProfile
var FitEquationArray = new Array("GAUSSIAN_NOOFFSET", "GAUSSIAN", "GAMMA_VARIATE");
var FitEquationD = "GAUSSIAN"; // type of fit: GAUSSIAN_NOOFFSET, GAUSSIAN, GAMMA_VARIATE etc.
var getAlignmentD = true; // compute aligned profiles?
// var alignFitsD = true; // add fit curves to the alignments? needs some more work
var AlignOnArray = new Array("start", "begin", "max", "fitmax", "end");
var AlignOnD = "fitmax"; // choose to align on "start", "begin", "max", "fitmax", "end"
//**************************
// Output Variables (SR)
//**************************
var logProfilesD = true; // detailled log of the Profiles, Features and Fits?
var generateFeatureROID = false; // generates feature as ROI in the Roi Manager?
var outTypeNameD = "PFF"; // category of output ROIs
var displayOverD = false; // display overlays
var displayPlotsD = true; // display the plots stack
var displayResultsTableD = true; // output Results Table?
var displayProfilesTableD = true; // output Profiles Table?
var displayAlignedTableD = true; // output Aligned Profiles Tables?
var ProfileTypeArray = new Array("RawProfile", "SmoothProfile", "NormProfile", "RawNormProfile");
var ProfileTypeD = "RawProfile"; // choose type of profile to align: raw, smoothened, smoothened normalized, raw normalized
var SubBackgroundD = false;
/*
//**************************
// Input Variables (AIS)
//**************************
var ChooseTypeD = false; // Process specific categories?
var TypeNameD = "Axon"; // Name of categories to process
var ProfileWidthD = 5; // total width of the line trace for profile, 5 for AIS
var HalfWidthD = 5; // sliding window smoothing along the profile, 0 for no smoothing, usually 10
var ScalePlotsD = true; // present each plot scaled in Y?
//var getFeatureD = true; // compute feature (with threshold-based begin/max/end)?
var getFeatureD = false;
var beginThresholdD = 0.35; // threshold for begin, 0.35 for AIS
var endThresholdD = 0.35; // threshold for end, 0.35 for AIS
var FeatureTypeArray = new Array("small", "large");
var FeatureTypeD = "large"; // choose "small" or "large" relative to threshold (small = first time under from max, large = first time above from extremities)
// large for AIS
var getFitD = false; // compute a fit of the profile?
var FitYSourceArray = new Array("PRawProfile", "PSmoothProfile"); // "PNormProfile", "PRawNormProfile" not supported yet
var FitYSourceD = "PSmoothProfile" // which profile to fit: PRawProfile, PSmoothProfile, PNormProfile, PRawNormProfile
var FitEquationArray = new Array("GAUSSIAN_NOOFFSET", "GAUSSIAN", "GAMMA_VARIATE");
var FitEquationD = "GAUSSIAN_NOOFFSET"; // type of fit: GAUSSIAN_NOOFFSET, GAUSSIAN, GAMMA_VARIATE etc.
//var getAlignmentD = true; // compute aligned profiles?
var getAlignmentD = false;
var AlignOnArray = new Array("start", "begin", "max", "fitmax", "end");
var AlignOnD = "begin"; // choose to align on "start", "begin", "max", "fitmax", "end"
//**************************
// Output Variables (AIS)
//**************************
var logProfilesD = true; // detailled log of the Profiles, Features and Fits?
var generateFeatureROID = false; // generates feature as ROI in the Roi Manager?
var outTypeNameD = "PFF"; // category of output ROIs
var displayOverD = false; // display overlays
var displayPlotsD = true; // display the plots stack
var displayResultsTableD = true; // output Results Table?
var displayProfilesTableD = true; // output Profiles Table?
var displayAlignedTableD = false; // output Aligned Profiles Tables?
var ProfileTypeArray = new Array("RawProfile", "SmoothProfile", "NormProfile", "RawNormProfile");
var ProfileTypeD = "NormProfile"; // choose type of profile to align: raw, smoothened, smoothened normalized, raw normalized
var SubBackgroundD = true;
*/
//**************************
// Variables not in dialog
//**************************
var plotSizeX = 800; // width in px of the output plot
var plotSizeY = 512; // height in px of the output plot
var boxWidth = 10; // margin in px for the crop (unused yet)
var letters = new Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l");
//**************************
// Dialog
//**************************
var gd = new GenericDialog("ProFeatFit Options");
gd.addCheckbox("Process Category", ChooseTypeD);
gd.addStringField("Category Name", TypeNameD, 10);
gd.addMessage("");
gd.addNumericField("Profile width", ProfileWidthD, 0, 5, "px");
gd.addNumericField("Window half-width", HalfWidthD, 0, 5, "px");
gd.addCheckbox("Scale Plots", ScalePlotsD);
gd.addMessage("");
gd.addCheckbox("Process begin/max/end", getFeatureD);
gd.addNumericField("Begin threshold", beginThresholdD, 2, 5, "");
gd.addNumericField("End threshold", endThresholdD, 2, 5, "");
gd.addChoice("Type of detection", FeatureTypeArray, FeatureTypeD);
gd.addMessage("");
gd.addCheckbox("Fit profile", getFitD);
gd.addChoice("Source profile for fit", FitYSourceArray, FitYSourceD);
gd.addChoice("Fit equation", FitEquationArray, FitEquationD);
gd.addMessage("");
gd.addCheckbox("Make alignment", getAlignmentD);
// gd.addCheckbox("Align Fits", alignFitsD); needs more work
gd.addChoice("Align on", AlignOnArray, AlignOnD);
gd.addMessage("Output parameters");
gd.addCheckbox("Log profiles", logProfilesD);
gd.addCheckbox("Generate ROIs", generateFeatureROID);
gd.addStringField("Add to Category Name", outTypeNameD, 10);
gd.addCheckbox("Display overlays", displayOverD);
gd.addCheckbox("Display plots stack", displayPlotsD);
gd.addCheckbox("Display results table", displayResultsTableD);
gd.addCheckbox("Display profiles table", displayProfilesTableD);
gd.addCheckbox("Display aligned table", displayAlignedTableD);
gd.addChoice("Profile Type", ProfileTypeArray, ProfileTypeD);
gd.addCheckbox("Background-corrected profiles", SubBackgroundD);
gd.showDialog();
var ChooseType = gd.getNextBoolean();
var TypeName = gd.getNextString();
var ProfileWidth = gd.getNextNumber();
var HalfWidth = gd.getNextNumber();
var ScalePlots = gd.getNextBoolean();
var getFeature = gd.getNextBoolean();
var beginThreshold = gd.getNextNumber();
var endThreshold = gd.getNextNumber();
var FeatureType = gd.getNextChoice();
var getFit = gd.getNextBoolean();
var FitYSource = gd.getNextChoice();
var FitEquation = gd.getNextChoice();
var getAlignment = gd.getNextBoolean();
// var alignFits = gd.getNextBoolean(); needs more work
var AlignOn = gd.getNextChoice();
var logProfiles = gd.getNextBoolean();
var generateFeatureROI = gd.getNextBoolean();
var outTypeName = gd.getNextString();
var displayOver = gd.getNextBoolean();
var displayPlots = gd.getNextBoolean();
var displayResultsTable = gd.getNextBoolean();
var displayProfilesTable = gd.getNextBoolean();
var displayAlignedTable = gd.getNextBoolean();
var ProfileType = gd.getNextChoice();
var SubBackground = gd.getNextBoolean();
if (gd.wasOKed()) {
//**************************
// Get names, IDs, number of slices, scale, number of ROIs
//**************************
var imp = IJ.getImage();
var StackName = imp.getTitle();
var StackID = imp.getID();
var StackDim = imp.getDimensions();
var Scale = getScale(imp);
var PxSize = Scale[0];
var PxUnit = Scale[1];
var rm = RoiManager.getInstance();
var ra = rm.getRoisAsArray();
var nroi = rm.getCount();
//**************************
// for each roi: detect category
//**************************
if (ChooseType == true) {
var ri = 0;
var tArray = new Array();
for (var r=0; r<nroi; r++) {
var currRoi = rm.getRoi(r);
var currType = currRoi.getProperty("TracingType");
var currTName = currRoi.getProperty("TypeName");
if (currTName == TypeName || currTName == null) {
tArray[ri] = r;
ri++;
}
}
}
else {
var tArray = new Array();
for (var r=0; r<nroi; r++) {
tArray[r] = r;
}
}
nr = tArray.length;
// define the Profiles array
var AllProfiles = new Array(nr);
var AllFeatures = new Array(nr);
var AllFits = new Array(nr);
var AllAligns = new Array(nr);
//**************************
// for each roi: define Profile parameters, add it to the Profile array
//**************************
for (var ar=0; ar<nr; ar++) {
r = tArray[ar];
var Profile = new profile();
var Feature = new feature();
var Fit = new fit();
var Align = new align();
Profile.PStackName = StackName;
Profile.PStackID = StackID;
Profile.PPxSize = PxSize;
Profile.PPxUnit = PxUnit;
Profile.PRoiName = rm.getName(r);
Profile.PRoiIndex = r;
var RoiType = getRoiType(imp, rm, r);
Profile.PRoiLabel = RoiType[0];
Profile.PRoiType = RoiType[1];
Profile.PRoiColor = RoiType[2]
Profile.PRoiWidth = ProfileWidth;
var Slice = getSlice(imp, rm, r);
Profile.PSliceName = Slice[0];
Profile.PSliceNumber = Slice[1];
if (SubBackground == true) Profile.PBackground = getBackground(imp, rm, r);
Profile.PWidth = StackDim[0];
Profile.PHeight = StackDim[1];
Profile.PSourceROI = getROI(imp, rm, r);
Profile.PScaledLength = getScaledLength(imp, rm, r);
Profile.PRawProfile = getProfile(imp, rm, ra, r, ProfileWidth);
Profile.PRawLength = Profile.PRawProfile.length;
Profile.PLineCoor = getLineCoor(Profile.PRawLength, Profile.PPxSize);
var XYFullCoor = getFullCoordinates(imp,rm, r, Profile.PRawLength);
var XYCoor = getCoordinates(imp,rm, r);
Profile.PXCoor = XYCoor[0];
Profile.PYCoor = XYCoor[1];
Profile.PRawMin = getMin(Profile.PRawProfile);
Profile.PRawMax = getMax(Profile.PRawProfile);
Profile.PRawMeanInt = getMeanIntensity(Profile.PRawProfile);
Profile.PRawIntDens = getRawIntegratedDensity(Profile.PRawProfile);
Profile.PHalfWidth = HalfWidth;
Profile.PSmoothProfile = getSmoothProfile(Profile.PRawProfile, Profile.PHalfWidth);
Profile.PSmoothMin = getMin(Profile.PSmoothProfile);
Profile.PSmoothMax = getMax(Profile.PSmoothProfile);
Profile.PSmoothMaxIndex = getMaxIndex(Profile.PSmoothProfile);
Profile.PSmoothMaxIndexScaled = Profile.PLineCoor[Profile.PSmoothMaxIndex];
Profile.PNormProfile = getNormalizedProfile(Profile.PSmoothProfile, Profile.PSmoothMin, Profile.PSmoothMax);
Profile.PRawNormProfile = getNormalizedProfile(Profile.PRawProfile, Profile.PSmoothMin, Profile.PSmoothMax);
Profile.PRawNormMin = getMin(Profile.PRawNormProfile);
Profile.PRawNormMax = getMax(Profile.PRawNormProfile);
Profile.PHasFeature = false;
Profile.PHasFits = false;
Profile.hasAlignment = false;
// Compute Features on the Profile
if (getFeature == true) {
Profile.PHasFeature = true;
Feature.FProfileIndex = r;
var FBegin = getFBegin(Profile, beginThreshold);
if (FeatureType == "small") {
Feature.FBeginIndex = FBegin[0];
Feature.FScaledBegin = FBegin[1];
}
else if (FeatureType == "large") {
Feature.FBeginIndex = FBegin[2];
Feature.FScaledBegin = FBegin[3];
}
var FEnd = getFEnd(Profile, endThreshold);
if (FeatureType == "small") {
Feature.FEndIndex = FEnd[0];
Feature.FScaledEnd = FEnd[1];
}
else if (FeatureType == "large") {
Feature.FEndIndex = FEnd[2];
Feature.FScaledEnd = FEnd[3];
}
Feature.FRawLength = Feature.FEndIndex - Feature.FBeginIndex;
if (isNaN(Feature.FRawLength) == true) {
Profile.PHasFeature = false;
}
Feature.FScaledLength = Feature.FScaledEnd - Feature.FScaledBegin;
Feature.FMeanInt = getMeanSub(Profile.PRawProfile, Feature.FBeginIndex, Feature.FEndIndex);
Feature.FMiCor = Feature.FMeanInt - Profile.PBackground;
Feature.FxCoor = getSub(Profile.PXCoor, Feature.FBeginIndex, Feature.FEndIndex);
Feature.FyCoor = getSub(Profile.PYCoor, Feature.FBeginIndex, Feature.FEndIndex);
}
// Compute Fit of the Profile
if (getFit == true) {
Profile.PHasFits = true;
Fit.TProfileIndex = r;
Fit.TEqType = FitEquation;
Fit.TFit = doProfileFit(Profile, FitYSource, FitEquation);
Fit.TFitX = Fit.TFit.getXPoints();
Fit.TFitY = Fit.TFit.getYPoints();
Fit.TParameters = Fit.TFit.getParams();
Fit.TRSquared = Fit.TFit.getRSquared();
Fit.TCurve = new Array(Fit.TFitX.length);
for (var i = 0; i < Fit.TFitX.length; i++) Fit.TCurve[i] = Fit.TFit.f(Fit.TParameters, Fit.TFitX[i]);
Fit.TCurveMax = getMax(Fit.TCurve);
Fit.TCurveMaxIndex = getMaxIndex(Fit.TCurve);
Fit.TCurveMin = getMin(Fit.TCurve);
Fit.TNormCurve = getNormalizedProfile(Fit.TCurve, Fit.TCurveMin, Fit.TCurveMax);
}
AllProfiles[ar] = Profile;
AllFeatures[ar] = Feature;
AllFits[ar] = Fit;
AllAligns[ar] = Align;
if (logProfiles == true) {
var ProfileLog = printProfile(Profile, Feature, Fit);
IJ.log(ProfileLog);
}
}
//*************************************************************************************
// Compute Alignment of Profiles
//*************************************************************************************
if (getAlignment == true) {
MasterLength = getAllValues("PRawLength", AllProfiles);
if (AlignOn == "start") {
var MasterPos = new Array (AllProfiles.length);
for (var i = 0; i < AllProfiles.length; i++) MasterPos[i] = 0;
}
else if (AlignOn == "begin") {
var MasterPos = getAllValues("FBeginIndex", AllFeatures);
}
else if (AlignOn == "max") {
var MasterPos = getAllValues("PSmoothMaxIndex", AllProfiles);
}
else if (AlignOn == "fitmax") {
var MasterPos = getAllValues("TCurveMaxIndex", AllFits);
}
else if (AlignOn == "end") {
var MasterPos = getAllValues("FEndIndex", AllFeatures);
}
var MaxShiftLeft = getMax(MasterPos);
var MaxShiftRight = getMaxDiff(MasterLength, MasterPos);
var AlignLength = MaxShiftLeft + MaxShiftRight;
var AlignX = getLineCoor(AlignLength, 1);
for (var r = 0; r < AllProfiles.length; r++) {
var Profile = AllProfiles[r];
var Feature = AllFeatures[r];
var Fit = AllFits[r];
var Align = AllAligns[r];
Align.AProfileIndex = r;
Align.AMasterPos = MasterPos[r];
if (isNaN(Align.AMasterPos) == false) {
Profile.PHasAlignment = true;
Align.AShiftLeft = MaxShiftLeft - MasterPos[r];
Align.AShiftRight = MaxShiftRight - (Profile.PRawLength - MasterPos[r]);
Align.AXScaled = getLineShiftCoor(AlignLength, Profile.PPxSize, MaxShiftLeft);
Align.ARawProfile = getAlignY(Align.AShiftLeft, Align.AShiftRight, Profile.PRawProfile);
Align.ASmoothProfile = getAlignY(Align.AShiftLeft, Align.AShiftRight, Profile.PSmoothProfile);
Align.ANormProfile = getAlignY(Align.AShiftLeft, Align.AShiftRight, Profile.PNormProfile);
Align.ARawNormProfile = getAlignY(Align.AShiftLeft, Align.AShiftRight, Profile.PRawNormProfile);
if (Profile.hasFits == true) {
Align.ACurve = getAlignY(Align.AShiftLeft, Align.AShiftRight, Fit.TCurve);
Align.ANormCurve = getAlignY(Align.AShiftLeft, Align.AShiftRight, Fit.TNormCurve);
}
AllProfiles[r] = Profile;
AllAligns[r] = Align;
}
else {
Profile.PHasAlignment = false;
AllProfiles[r] = Profile;
}
if (logProfiles == true) {
var AlignLog = printAlign(Profile, Feature, Fit, Align);
IJ.log(AlignLog);
}
}
}
//**************************
// Output part
//**************************
var outName = StackName.replace(".tif", "") + "_PFF(w" + ProfileWidth + ",h" + HalfWidth;
if (getFeature == true) outName += ",b" + beginThreshold + ",e" + endThreshold + "," + FeatureType;
if (getAlignment == true) outName += ",a" + AlignOn;
outName += ")";
//*************************************************************************************
// Output 1: Generate Feature ROI
//*************************************************************************************
// For each Profile, if options are valid, generate the Feature ROI
if (generateFeatureROI == true) {
for (var r = 0; r < AllProfiles.length; r++) {
var Profile = AllProfiles[r];
var Feature = AllFeatures[r];
// Generate the feature roi
if (Profile.PHasFeature == true) {
var Froi = new PolygonRoi(convertArrayF(Feature.FxCoor), convertArrayF(Feature.FyCoor), Feature.FxCoor.length, Roi.POLYLINE);
imp.setSlice(Profile.PSliceNumber);
Froi.setProperty("TracingType", Profile.PRoiLabel);
Froi.setProperty("TypeName", Profile.PRoiType + "_" + outTypeName);
Froi.setStrokeColor(Profile.PRoiColor);
Froi.setStrokeWidth(Profile.PRoiWidth);
rm.addRoi(Froi);
// Delete source ROI
var sourceIndex = Profile.PRoiIndex;
rm.select(sourceIndex - r);
rm.runCommand("Delete");
// Rename output ROI
rm.select(rm.getCount()-1);
var sourceName = Profile.PRoiName;
var sourceParts = sourceName.split("-");
var destName = sourceParts[0] + "-" + sourceParts[1] + "-" + sourceParts[2] + "-" + Profile.PRoiLabel + "-" + Profile.PRoiType + "_" + outTypeName;
rm.runCommand("Rename", destName);
var rmrois = rm.getSelectedRoisAsArray();
Froi = rmrois[0];
// Store feature ROI
Feature.FRoi = Froi;
}
else {
// Select source ROI
var sourceIndex = Profile.PRoiIndex;
rm.select(sourceIndex - r);
var rmrois = rm.getSelectedRoisAsArray();
Froi = rmrois[0];
Froi.setProperty("TracingType", Profile.PRoiLabel);
Froi.setProperty("TypeName", Profile.PRoiType + "_" + outTypeName);
Froi.setStrokeColor(Profile.PRoiColor);
Froi.setStrokeWidth(Profile.PRoiWidth);
rm.addRoi(Froi);
// Delete source ROI
var sourceIndex = Profile.PRoiIndex;
rm.select(sourceIndex - r);
rm.runCommand("Delete");
// Rename with "noFeat" to indicate failure to find full Feature
rm.select(rm.getCount()-1);
var sourceName = Profile.PRoiName;
var sourceParts = sourceName.split("-");
var destName = sourceParts[0] + "-" + sourceParts[1] + "-" + sourceParts[2] + "-" + Profile.PRoiLabel + "-" + Profile.PRoiType + "_" + "noFeat";
rm.runCommand("Rename", destName);
}
}
rm.runCommand("Sort");
}
//*************************************************************************************
// Output 2: Plots of Profile, Feature & Fit
//*************************************************************************************
if (displayPlots == true) {
var AllProfilePlots = new Array(nroi);
var plotStacks = new ImageStack(plotSizeX, plotSizeY);
// Looks for max and min of all plots to unify plot scale accross all features
var plotMaxX = getMaxValue("PScaledLength", AllProfiles);
var plotMinY = getMinValue("PRawMin", AllProfiles);
var plotMaxY = getMaxValue("PRawMax", AllProfiles);
// Log the plots limits
// IJ.log("plotMaxX=" + plotMaxX + ", plotMinY=" + plotMinY + ", plotMaxY=" + plotMaxY);
// For each Profile, generate the profile plot, and add a slice to the Profiles image stack
for (var r = 0; r < AllProfiles.length; r++) {
var Profile = AllProfiles[r];
var Feature = AllFeatures[r];
var Fit = AllFits[r];
// generate the profile plot: add the profile and the feature points, get the ip
if (ScalePlots == true) plotMaxY = Profile.PRawMax * 1.1;
else plotMaxY = plotMaxY * 1.2;
var prfPlot = new Plot("Profiles", Profile.PPxUnit, "intensity", convertArrayD(Profile.PLineCoor), Profile.PRawProfile);
prfPlot.setSize(plotSizeX, plotSizeY);
prfPlot.setLimits(0, plotMaxX, plotMinY, plotMaxY);
prfPlot = addProfilePlot(Profile, prfPlot);
if (getFeature == true) prfPlot = addFPlot(Profile, Feature, prfPlot)
if (getFit == true) prfPlot = addFitPlot(Profile, Feature, Fit, prfPlot);
prfPlot.draw;
AllProfilePlots[r] = prfPlot;
var PlotP = prfPlot.getProcessor();
plotStacks.addSlice(Profile.PSliceName.replace(",","-") + " " + Profile.PRoiName, PlotP);
}
// i+ from the Profiles image stack
var plotImp = new ImagePlus(outName + "_Plots", plotStacks);
// show the plots
plotImp.show();
}
//*************************************************************************************
// Output 3: Overlay of features
//*************************************************************************************
if (displayOver == true) {
// Initialize the overlay
var over = new Overlay();
// For each Profile, generate the profile plot, and add a slice to the Profiles image stack
for (var r = 0; r < AllProfiles.length; r++) {
var Profile = AllProfiles[r];
var Feature = AllFeatures[r];
var Fit = AllFits[r];
// IJ.log(Profile.PRoiName);
// generate the overlay: snake around the line trace and feature points
over = addOutlineToOverlay(Profile, over, ProfileWidth, "stack");
if (getFeature == true) over = addFToOverlay(Profile, Feature, over, "stack");
}
imp.setOverlay(over);
}
//*************************************************************************************
// Output 4: Results Tables
//*************************************************************************************
if (displayResultsTable == true) {
// Initialize the Results Table
var rt = new ResultsTable();
var row = -1;
for (var r = 0; r < AllProfiles.length; r++) {
var Profile = AllProfiles[r];
var Feature = AllFeatures[r];
var Fit = AllFits[r];
//log to Results Table
rt.incrementCounter();
row++;
var fullName = Profile.PStackName + " " + Profile.PRoiName+ " " + Profile.PSliceName;
rt.setValue("Stack", row, Profile.PStackName);
rt.setValue("Slice #", row, "" + Profile.PSliceNumber);
rt.setValue("Slice", row, Profile.PSliceName);
rt.setValue("Roi #", row, "" + Profile.PRoiIndex);
rt.setValue("Roi", row, Profile.PRoiName);
rt.setValue("Length", row, Profile.PRawLength);
rt.setValue("Max", row, Profile.PRawMax);
if (getFeature == true) {
rt.setValue("F Begin", row, Feature.FScaledBegin);
rt.setValue("F Max", row, Profile.PSmoothMaxIndexScaled);
rt.setValue("F End", row, Feature.FScaledEnd);
rt.setValue("F length", row, Feature.FScaledLength);
if (getFit == true) {
for (var i = 0; i < Fit.TParameters.length; i++) {
rt.setValue("Fit " + letters[i], row, Fit.TParameters[i]);
}
rt.setValue("Fit R2", row, Fit.TRSquared);
}
}
// show the Results Table
rt.show(outName + "_Results");
}
}
//*************************************************************************************
// Output 5: Profile Tables
//*************************************************************************************
if (displayProfilesTable == true) {
// Initialize the Profiles Table
var pt = new ResultsTable();
var maxRawLength = getMaxValue("PRawLength", AllProfiles);
var Profile = AllProfiles[0];
var scaleX = Profile.PPxSize;
for (var p = 0; p < maxRawLength; p++) {
pt.setValue("Scaled X", p, p * scaleX);
}
for (var r = 0; r < AllProfiles.length; r++) {
var Profile = AllProfiles[r];
var Feature = AllFeatures[r];
var Fit = AllFits[r];
for (p = 0; p < Profile.PNormProfile.length; p++) {
var stringValue = "Profile.P" + ProfileType + "[p]"; // uses ProfileType to choose which profile to output
var pValue = eval(stringValue);
if (SubBackground == true) {
if (ProfileType == "RawProfile" || ProfileType == "SmoothProfile") pValue = pValue - Profile.PBackground;
}
pt.setValue(Profile.PSliceName.replace(",","-") + " " + Profile.PRoiName, p, pValue);
}
for (p = Profile.PNormProfile.length; p < maxRawLength; p++) {
pt.setValue(Profile.PSliceName.replace(",","-") + " " + Profile.PRoiName, p, Number.NaN);
}
}
// show the Profiles Table
pt.show(outName + "_" + ProfileType + "s");
}
//*************************************************************************************
// Output 6: Alignment Tables
//*************************************************************************************
if (getAlignment == true && displayAlignedTable == true) {
// Initialize the Alignment Table
var at = new ResultsTable();
// make a column with scaled X
makeAlignXScaled(AllAligns, at);
// make a column with each alignment (type chosen by ProfileType, background-subtracted as an option)
for (var r = 0; r < AllAligns.length; r++) {
var Profile = AllProfiles[r];
if (Profile.PHasAlignment == true) {
var Align = AllAligns[r];
for (var p = 0; p < AlignLength; p++) {
var stringValue = "Align.A" + ProfileType + "[p]";
var pValue = eval(stringValue);
if (SubBackground == true) {
if (ProfileType == "RawProfile" || ProfileType == "SmoothProfile") pValue = pValue - Profile.PBackground;
}
at.setValue(Profile.PSliceName.replace(",","-") + " " + Profile.PRoiName, p, pValue);
}
}
else {
for (var p = 0; p < AlignLength; p++) {
at.setValue(Profile.PSliceName.replace(",","-") + " " + Profile.PRoiName, p, Double.NaN);
}
}
}
// show the Alignment Table
at.show(outName + "_" + ProfileType + "s_Alignments");
}
}
//*************************************************************************************
// End
//*************************************************************************************
IJ.log("\n*****************************************************\nProFeatFit has finished!\n*****************************************************");
//*************************************************************************************
// Utilities funtions
//*************************************************************************************
// Utility to convert a javascript array into a java double array
function convertArrayD(arr) {
var jArr = java.lang.reflect.Array.newInstance(java.lang.Double.TYPE, arr.length);
for (var i = 0; i < arr.length; i++) {
jArr[i] = arr[i];
}
return jArr;
}
// Utility to convert a javascript array into a java float array
function convertArrayF(arr) {
var jArr = java.lang.reflect.Array.newInstance(java.lang.Float.TYPE, arr.length);
for (var i = 0; i < arr.length; i++) {
jArr[i] = arr[i];
}
return jArr;
}
//*************************************************************************************
// Utility to log Profile parameters
//*************************************************************************************
function printProfile(Profile, Feature, Fit) {
var logstring = "\n*** Profile data ***\n";
logstring += "Stack Name: " + Profile.PStackName + "\n";
logstring += "Stack ID: " + Profile.PStackID + "\n";
logstring += "Stack Pixel Size: " + Profile.PPxSize + " " + Profile.PPxUnit + "\n";
logstring += "Roi Name: " + Profile.PRoiName + "\n";
logstring += "Roi Index: " + Profile.PRoiIndex + "\n";
logstring += "Roi Label: " + Profile.PRoiLabel + "\n";
logstring += "Roi Type: " + Profile.PRoiType + "\n";
logstring += "Roi Color: " + Profile.PRoiColor + "\n";
logstring += "Slice Name: " + Profile.PSliceName + "\n";
logstring += "Slice Number: " + Profile.PSliceNumber + "\n";
logstring += "Image background: " + Profile.PBackground + "\n";
logstring += "xCoor: " + printArraySample(Profile.PXCoor) + "\n";
logstring += "yCoor: " + printArraySample(Profile.PYCoor) + "\n";
logstring += "Scaled Length: " + Profile.PScaledLength + " "+ Profile.PPxUnit+"\n";
logstring += "Raw Length: " + Profile.PRawLength + " pixels\n";
logstring += "Raw Profile: " + printArraySample(Profile.PRawProfile) + "\n";
logstring += "Line Coordinates: " + printArraySample(Profile.PLineCoor) + "\n";
logstring += "Raw Min: " + Profile.PRawMin + "\n";
logstring += "Raw Max: " + Profile.PRawMax + "\n";
logstring += "Raw Mean Intensity: " + Profile.PRawMeanInt + "\n";
logstring += "Raw Integrated Density: " + Profile.PRawIntDens + "\n";
logstring += "Sliding Window Half-Width: " + Profile.PHalfWidth + "\n";
logstring += "Smoothened Profile: " + printArraySample(Profile.PSmoothProfile) + "\n";
logstring += "Smooth Min: " + Profile.PSmoothMin + "\n";
logstring += "Smooth Max: " + Profile.PSmoothMax + "\n";
logstring += "Smooth Max Index: " + Profile.PSmoothMaxIndex + "\n";
logstring += "Scaled Smooth Max: " + Profile.PSmoothMaxIndexScaled + "\n";
logstring += "Normalized Profile: " + printArraySample(Profile.PNormProfile) + "\n";
logstring += "Raw Normalized Profile: " + printArraySample(Profile.PRawNormProfile) + "\n";
logstring += "Raw Norm Min: " + Profile.PRawNormMin + "\n";
logstring += "Raw Norm Max: " + Profile.PRawNormMax + "\n";
logstring += "\nhas Features: " + Profile.PHasFeature + "\n";
if (Profile.PHasFeature == true) {
logstring += "Feature Begin Index: " + Feature.FBeginIndex + "\n";
logstring += "Feature Scaled Begin: " + Feature.FScaledBegin + "\n";
logstring += "Feature End Index: " + Feature.FEndIndex + "\n";
logstring += "Feature Scaled End: " + Feature.FScaledEnd + "\n";
logstring += "Feature Raw Length: " + Feature.FRawLength + "\n";
logstring += "Feature Scaled Length: " + Feature.FScaledLength + "\n";
logstring += "Feature Mean Intensity: " + Feature.FMeanInt + "\n";
logstring += "Feature Corrected Mean Intensity: " + Feature.FMiCor + "\n";
// logstring += "Feature xCoor: " + printArraySample(Feature.FxCoor) + "\n";
// logstring += "Feature yCoor: " + printArraySample(Feature.FyCoor) + "\n";
}
logstring += "\nhas Fits: " + Profile.PHasFits + "\n";
if (Profile.PHasFits == true) {
logstring += "Fit Equation: " + FitEquation + "\n";
for (var i = 0; i < Fit.TParameters.length; i++) {
logstring += "Fit_" + letters[i] + ": " + Fit.TParameters[i] + "\n";
}
logstring += "Fit R2: " + Fit.TRSquared + "\n";
}
return logstring;
}
//*************************************************************************************
// Utility to log Alignment parameters
//*************************************************************************************
function printAlign(Profile, Feature, Fit, Align) {
var logstring = "\n*** Alignment data ***\n";
logstring += "Stack Name: " + Profile.PStackName + "\n";
logstring += "Slice Number: " + Profile.PSliceNumber + "\n";
logstring += "Slice Name: " + Profile.PSliceName + "\n";
logstring += "Roi Name: " + Profile.PRoiName + "\n";
logstring += "Has Alignment: " + Profile.PHasAlignment + "\n";
logstring += "Aligned on: " + AlignOn + " at " + Align.AMasterPos + " pixels \n";
return logstring;
}
//*************************************************************************************
// Utility to print a few elements from an array + its length: takes an array, returns a string
//*************************************************************************************
function printArraySample(Array) {
if (Array.length < 2) return "*too small*";
var string = "[ " + Array[0] + ", " + Array[1] + ", ... , " + Array[Array.length-2] + ", " + Array[Array.length-1] + " ] (length "+ Array.length + ")";
return string;
}
//*************************************************************************************
// Utility to print all elements of an array: takes an array, returns a string
//*************************************************************************************
function printArrayFull(Array) {
var string = "[ ";
for (var i = 0; i < Array.length-1; i++) {
string += Array[i] + "\n";
}
string += Array[Array.length-1] + " ]";
return string;
}
//*************************************************************************************
// Function that generate Profile parameters
//*************************************************************************************
//*************************************************************************************
// get the scale of the image: takes an i+, returns an array with (size of a pixel, unit)
//*************************************************************************************
function getScale(imp){
var cal=imp.getCalibration();
var scale=cal.getX(1);
var unit=cal.getXUnit()+"s";
return [scale , unit];
}
//*************************************************************************************
// takes roimanager, index, returns the label of the ROI as an integer (second to last number from NDF to ROI macro)
//*************************************************************************************
function getRoiType(imp, rm, r){
rm.select(imp, r);
var roi = imp.getRoi();
var rti = roi.getProperty("TracingType");
var rtn = roi.getProperty("TypeName");
var rtc = roi.getStrokeColor();
return [rti, rtn, rtc];
}
//*************************************************************************************
// takes i+, roimanager, index, returns an array (slice label, slice number) for a given ROI
//*************************************************************************************
function getSlice(imp, rm, r) {
if (imp.getImageStackSize() > 1) {
var snumber = rm.getSliceNumber(rm.getName(r));
var stk = imp.getImageStack();
var sname = stk.getSliceLabel(snumber);
}
else {
var snumber = 1;
var sname = imp.getTitle();
}
return [sname , snumber];
}
//*************************************************************************************
// takes i+, roimanager, index, returns the corresponding ROI
//*************************************************************************************
function getROI(imp, rm, r){
rm.select(imp, r);
var roi = imp.getRoi();
return roi;
}
//*************************************************************************************
// takes i+, roimanager, index, returns the scaled length of the ROI
//*************************************************************************************
function getScaledLength(imp, rm, r){
rm.select(imp, r);
var roi = imp.getRoi();
var length = roi.getLength();
return length;
}
//*************************************************************************************
// takes i+, roimanager, index, returns the profile along the ROI with a w width (array)
//*************************************************************************************
function getProfile(imp, rm, ra, r, w){
// gets the roi to have the initial stroke width
var roi = ra[r];
var roiT = roi.getTypeAsString();
if (roiT == "Straight Line") {
var iw = roi.getStrokeWidth();
roi.setStrokeWidth(w);
roi.setImage(imp);
var prf = roi.getPixels();
roi.setStrokeWidth(iw);
}
else {
var iw = roi.getStrokeWidth();
rm.select(imp, r);
rm.runCommand("Set Line Width", w);
var profPlot = new ProfilePlot(imp);
var prf = profPlot.getProfile();
rm.runCommand("Set Line Width", iw);
}
return prf;
}
//*************************************************************************************
// takes array, returns min of the array
//*************************************************************************************
function getMin(prf){
var min = 1000000000;
for (var i = 0; i <prf.length; i++) {
if (prf[i] < min) min = prf[i];
}
return min;
}
//*************************************************************************************
// takes array, returns max of the array
//*************************************************************************************
function getMax(prf){
var max = 0;