-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExportModule.m
1335 lines (1066 loc) · 49.2 KB
/
ExportModule.m
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
classdef ExportModule < handle
% EXPORTMODULE is a data-centric module for exporting data into multiple
% standard and commonly used formats. This module can handle masks, point
% clouds, and closed curves computed from images. The cell array of results
% enables flexibility and allows for future development to explore other
% data formats. It even allows the the array to hold different types of
% results but this will then limit the number of viable format options for
% export.
%
% Masks are only saved by default in .mat, .xml, and .json if the results
% are masks. Other formats will require the user to enable the
% "outputmasks" property in this class.
%
% By default, this module only exports:
% - the settings used to compute the results
% - points into .mat, .json, .xml, .csv, .xlsx
% - masks into .mat, .json, .xml
% - region properties IF the results are masks or closed curves
%
% If export to masks in .tif or .png files is desired, user need to set the
% outmasks property of the class to true before calling the ExportData
% method.
%
% William A. Ramos, Kumar Lab @ MBL, Woods Hole, April 2024
%% Class Properties
properties (Access = public)
% Visuals
Progdlg % Progress bar dialog box to show the percent export done
dmsg % Dialog box message to display during export
% Parent figure
Parent % Upon construction, the class will grab the handle for a GUI figure if the class is instantiated from a callback
% Toggles masks' export
outputmasks = false % Logical determines whether masks need to be computed / determined for point clouds
end
properties (GetAccess = public, SetAccess = private)
% Metadata
Date % Date of export
Time % Time of export
% Mask Stack Export Settings
maskfilepath % File path for exported mask(s)
maskfilename % File name for exported mask(s)
maskfileext % File extension for exported mask(s)
% Mask properties (Parameter settings/Stats) Export Settings
outputpath % File path for parameter / stats file export
outputname % File name for mask properties file
outputext % File extension for the properties file
Requestedprops % List of statistics to be pulled from region props
% Results output
Allinfo % Results and the settings used to achieve the results
Results % The ZxT cell array that holds segmentation/detection results
slices % Z indices (volume slices) of segmented 2D planes
timepoints % T indices (timeseries frame) of segmented 2D planes
end
properties (Access = private)
% Image/Results Metadata
mrows % y dimension of image - Number of rows needed for proper poly2mask computation in case of contour curve with masks requested
ncols % x dimension of image - Number of columns needed for proper poly2mask computation in case of contour curve with masks requested
% Requested properties
bboxreq % Logical whether bounding box is requested
aspectratioreq % Logical whether aspect ratio is requested
% Additional tools
TiffWriter % Fast tiff writer - would be good for a fully segmented stack
end
properties (Dependent)
% Dependent properties - can be more dynamic
saveallowed % checks if user is at risk at overwriting and enables saving to target location if true
numslices % checks number of z slices in dataset
numframes % checks number of time frames in dataset
isvolume % checks to see if data has a third spatial dimension, i.e. > 1 z slice (first dimension of results cell array)
istimeseries % checks to see if data has a temporal dimension, i.e. > 1 frame (second dimension of results cell array)
rtypemismatch % checks results for number of different types of result formats. Helpful for file formats that are ideally suited to one result type.
masksinresults % checks results for any masks in the results cell array. This format is incompatible with .csv and .xlsx so users have to enable mask writing to either a .tiff or .png for those export formats.
maskfid % filepath for masks export
outputfid % filepath for output of settings and results
end
%% Constructor and Set/Get methods
methods
function obj = ExportModule(f)
% Contructor
if nargin < 1 || isempty(f)
f = gcbf;
end
if ~isempty(f)
obj.Parent = f;
end
% Init fast tiff writer in case of fully segmented stack case
obj.TiffWriter = FWTiff;
end
function cansave = get.saveallowed(obj)
% checks to see if using the currently set filepath would lead
% to an overwrite and then prompts user to confirm the
% overwrite, otherwise, the user can write the file.
% Currently set filepath
fid = [obj.outputpath filesep obj.outputname obj.outputext];
if exist(fid, "file") == 2
% Overwrite might occur, ask user to overwrite results
prompt = 'File already exists. Proceed and overwrite?';
if ~isempty(obj.Parent)
% GUI dialog
Answer = uiconfirm(obj.Parent, prompt, 'File Overwrite');
cansave = strcmp(Answer, 'OK');
else
% CLI dialog
prompt = [prompt ' Enter y/n.'];
x = input(prompt);
cansave = isempty(x) || strcmp(x, 'y');
end
else
% User can save if the file does not exist yet
cansave = true;
end
end
function nslices = get.numslices(obj)
% checks number of slices present in volume.
nslices = size(obj.Results, 1);
end
function nframes = get.numframes(obj)
% checks number of frames present in time series.
nframes = size(obj.Results, 2);
end
function hasframes = get.istimeseries(obj)
% checks second dimension of cell array as it represents time
hasframes = obj.numframes > 1;
end
function hasslices = get.isvolume(obj)
% checks first dimension of cell array as it represents z
hasslices = obj.numslices > 1;
end
function mismatch = get.rtypemismatch(obj)
% rtypemismatch returns true if there is a mismatch in the
% types of results present. This might disable certain export
% format options or other functionality.
ntypes = obj.ResultsFormatInfo;
mismatch = ntypes>1;
end
function masksfound = get.masksinresults(obj)
% masksinresults returns true if there are any masks in the
% results array
[~, typelist] = obj.ResultsFormatInfo;
masksfound = any(strcmp(typelist, 'mask'));
end
function fid = get.maskfid(obj)
% maskfid gets full fid for the mask file(s) to be saved
fid = [obj.maskfilepath filesep obj.maskfilename obj.maskfileext];
end
function fid = get.outputfid(obj)
% outputfid gets full fid for the output file
fid = [obj.outputpath filesep obj.outputname obj.outputext];
end
function [ntypes, typeslist] = ResultsFormatInfo(obj)
% RESULTSINFO will return a list with the different result
% types found in the results array and how many different types
% were found.
typeslist = cellfun(@obj.CheckResultType, obj.Results(:), 'UniformOutput', false);
notype = cellfun(@isempty, typeslist);
typeslist = typeslist(~notype);
ntypes = numel(unique(typeslist));
end
function SetImageBounds(obj, yxsz)
% SETIMAGBOUNDS will set the y and x dimensions of the image to
% enable proper mask production when masks are requested from
% contour curves.
obj.mrows = yxsz(1);
obj.ncols = yxsz(2);
end
function R = GetPoints(obj)
% GETPOINTS will reorganize the computed results into an array
% that represents point coordinates.
R = obj.Results;
[r, c] = find(~cellfun(@isempty, obj.Results));
nresults = numel(r);
% If a volume, z slice index is appended
for i = 1:nresults
s = r(i);
t = c(i);
np = size(R{s, t}, 1);
R{s, t} = [R{s, t} repmat(s, [np, 1]) repmat(t, [np, 1])];
end
% Concatenate as array of points with up to 4 dimensions
R = vertcat(R{:});
% Eliminating singleton higher dimensions
if ~obj.istimeseries
% Drops 4th dim
R = R(:, 1:3);
end
if ~obj.isvolume
% Drops 3rd dim
if obj.istimeseries
R = R(:, [1:2, 4]);
else
R = R(:,1:2);
end
end
end
function pointsonly = OnlyHasPoints(obj)
% ONLYHASPOINTS returns true when the results array only has
% point detections or contours and no other result types.
% Check that data is only composed of point detections
for i = 1:numel(obj.Results)
[~, typeslist] = obj.ResultsFormatInfo;
ispoints = contains(typeslist, {'pointcloud', 'contour'}) & ~contains(typeslist, 'mask');
pointsonly = all(ispoints);
end
end
function T = PointsAsTable(obj)
% Checks that there are only points in the results
if ~obj.OnlyHasPoints
T = [];
return
end
% Points and the variable names
P = obj.GetPoints;
varnames = {'X', 'Y'};
% Consider z dimension
if obj.isvolume
varnames = [varnames {'Z'}];
end
% Consider t dimension
if obj.istimeseries
varnames = [varnames {'T'}];
end
T = array2table(P, 'VariableNames', varnames);
end
end
%% Public methods
methods (Access = public)
function loaded = LoadResults(obj, fid, pdlg)
% LOADRESULTS will allow the user to load up previous
% segmentation results and then navigate them in the GUI. This
% is a capability available to .mat, .json, and .xml.
%
%
% Loaded flag set to false by default
loaded = false;
if nargin < 3 || isempty(pdlg)
obj.Progdlg = pdlg;
end
if nargin < 2 || isempty(fid)
% Asking user to select a file to load in.
ftypes = {'*.mat; *.json; *.xml', 'Segmentation Files (*.mat, *.json, *.xml)'};
[fname, fpath] = uigetfile(ftypes, 'Load segmentation result', []);
% Window closing cancels results loading
if fname == 0
return
end
% File ID
fid = fullfile(fpath, fname);
end
% Converting files to structs
loadedData = obj.ImportResults2Struct(fid);
% Progress update
msg = 'Extracting segmentation information and data.';
if ~isempty(pdlg)
pdlg.Title = 'Importing Data';
pdlg.Message = msg;
else
disp(msg)
end
% Will access the struct and load in results
obj.ParseImportedData(loadedData)
% Setting loaded flag to true after success
loaded = true;
% Progress update
msg = 'Import successful';
if ~isempty(pdlg)
pdlg.Title = 'Success';
pdlg.Message = msg;
else
disp(msg)
end
end
function ParseImportedData(obj, loadedData)
% PARSEIMPORTEDDATA will parse out results into this class and
% then try to pass results into a GUI class.
% In case of sparse annotations - grab D3 and D4 indices
S = [loadedData.Slice]';
T = [loadedData.Timepoint]';
Data = {loadedData.Results};
numresults = numel(loadedData);
% Ensures data is set to correct indices
for i = 1:numresults
s = S(i);
t = T(i);
obj.UpdateData(Data{i}, s, t)
end
% Will try to load results and settings back into GUI
obj.PassImportedSettings2GUI(loadedData)
end
% PassImportedSettings2GUI function might change to better
% encapsulate the classes. Also, not fond of the Segdata struct
% name, especially considering this should probably generalize a
% little better... data info struct ...
% --> DataInfo? ResultsInfo? ComputeInfo? ProcessingInfo?
function PassImportedSettings2GUI(obj, loadedData)
% If linked to a GUI, settings are passed back to GUI class
% Might alter to instead return a struct of some sort that the
% GUI can then manipulate, i.e. the loop below is flexible
if ~isempty(obj.Parent)
% Checks for app running and Segdata (might change)
app = obj.Parent.RunningAppInstance;
validFields = fieldnames(app.Segsettings);
% In case of sparse annotations - grab D3 and D4 indices
S = [loadedData.Slice];
T = [loadedData.Timepoint];
numresults = numel(loadedData);
% Checks fields that match with GUI's struct
dataFields = fieldnames(loadedData);
idx = cellfun(@(x) any(strcmp(x, validFields)), dataFields, 'UniformOutput', false);
fields2drop = dataFields(~[idx{:}]);
loadedData = rmfield(loadedData, fields2drop);
% In case of sparsely indexed results
loadedData = obj.AppendEmptyFields(app.Segsettings, loadedData);
for i = 1:numresults
s = S(i);
t = T(i);
app.Segsettings(s, t) = loadedData(i);
end
obj.Allinfo = app.Segsettings;
end
end
function InitData(obj, z, t)
% INITDATA will initialize a cell array have z number of rows
% and t number of columns. These dimensions represent
if nargin < 2 || isempty(z)
z = 1;
end
if nargin < 3 || isempty(t)
t = 1;
end
% Initializes the data property as empty cell array
obj.Results = cell(z, t);
end
function IndexedData = GetPlaneData(obj, slice_idx, time_idx)
% GETSLICEDATA will retrieve the mask and points for a given
% slice.
IndexedData = obj.Results{slice_idx, time_idx};
end
function ResetData(obj)
% RESETDATA can be called when dealing with results for a new
% image. Slice and time indices have to be reset to empty as
% these properties represent the indices of image that have
% been segmented.
obj.Results = [];
obj.Allinfo = [];
obj.slices = [];
obj.timepoints = [];
end
function SetExportSettings(obj, Exportsettings, Propertylist)
% SETEXPORTSETTINGS sets the filepaths from an exportsettings
% struct for both the segmentation info and masks. Segmentation
% info includes settings used to perform segmentations or
% detections, any computed region properties from masks or ROI
% curves, and the ROI curve points or pointclouds from
% detections.
% Parses filepaths from settings struct
if nargin >= 2 && ~isempty(Exportsettings)
obj.SetFilePaths(Exportsettings)
end
% Sets the stats/properties to be computed
if nargin == 3 && ~isempty(Propertylist)
obj.SetRequestedStats(Propertylist)
end
end
function SetFilePaths(obj, Exportsettings)
% SETFILEPATH will parse the desired export location for the
% segmentation information (settings and any stats) and for the
% mask data.
% Results and Settings export location
obj.outputpath = Exportsettings.InfoPath;
obj.outputname = Exportsettings.InfoName;
obj.outputext = Exportsettings.InfoExt;
% Mask(s) export location - if masks are requested
obj.maskfilepath = Exportsettings.MaskPath;
obj.maskfilename = Exportsettings.MaskName;
obj.maskfileext = Exportsettings.MaskExt;
end
function SetRequestedStats(obj, Propertylist)
% SETREQUESTEDSTATS will set the stats that have been requested
% based on a tree with checkbox nodes (ui component) or a cell
% array with character arrays in each cell.
% Checkbox tree from GUI or cell array list as input
if isa(Propertylist, 'matlab.ui.container.CheckBoxTree')
if ~isempty(Propertylist.CheckedNodes)
% Ignores the top most node 'MaskProperties' from GUI
obj.Requestedprops = {Propertylist.CheckedNodes.Text};
idx = strcmp(obj.Requestedprops, 'Mask Properties');
obj.Requestedprops = obj.Requestedprops(~idx);
obj.Requestedprops = cellfun(@(x) AddRmStrSpace(x, false), obj.Requestedprops, 'UniformOutput', false);
else
obj.Requestedprops = [];
end
elseif iscell(Propertylist)
obj.Requestedprops = Propertylist;
obj.Requestedprops = cellfun(@(x) AddRmStrSpace(x, false), obj.Requestedprops, 'UniformOutput', false);
end
% Case for aspect ratio w/o bounding box request
obj.bboxreq = any(strcmp(obj.Requestedprops, 'BoundingBox'));
obj.aspectratioreq = any(strcmp(obj.Requestedprops, 'AspectRatio'));
end
function ExportData(obj, fig, Segdata)
% EXPORTDATA will package the segmentation/detection results
% alongside the settings used in the GUI and export this in one
% file according to the selected format. If the user has
% enabled the mask export, those will also be saved according
% to the preferred format, either as a tiff stack or multiple
% png files representing 2D planes. In either case, they will
% be saved within a folder that will be in the same directory
% as the target location for the other data.
% Will not export if there are no results
if isempty(obj.Results)
warning('No results to export.')
return
end
% Init the progress bar
obj.Progdlg = uiprogressdlg(fig, 'Title', 'Export', 'Message', 'Preparing for export...', 'Value', 0);
% Mask stack export
try
% Check if file exists before overwriting
if ~obj.saveallowed && ~isempty(obj.Progdlg)
% User decides not to overwrite, save not allowed.
obj.Progdlg.Message = 'Export stopped. Please change export filename.';
pause(1)
obj.Progdlg.delete
return
end
% Mask export into image files
if obj.outputmasks
obj.ExportResultsAsImageMasks
% Terminal Update
disp('Segmented mask(s) saved')
end
% Terminal Update
disp('Gathering results...')
% Update progress bar value
if ~isempty(obj.Progdlg)
obj.Progdlg.Value = 0.5;
end
% Progress bar dialog box
if ~isempty(obj.Requestedprops) && ~isempty(obj.Progdlg)
obj.dmsg = ['Computing properties' ...
newline ...
'Please wait...'];
obj.Progdlg.Message = obj.dmsg;
end
% Mask property computation
obj.ConsolidateResults(Segdata)
% Results and the settings used to get them
obj.ExportResultsWithSettings
catch ME
if ~isempty(obj.Progdlg)
close(obj.Progdlg)
end
warning('Save was unsuccesful...')
rethrow(ME)
end
if ~isempty(obj.Progdlg)
close(obj.Progdlg)
end
end
function ExportResultsAsImageMasks(obj)
% MASKSTACKEXPORT is used to export masks that correpond to the
% results produced from segmentation. If the user had an
% unordered point cloud as their results, the masks will simply
% be a blank image with the nearest pixels to points turned on
% and this will lead to a loss of some subpixel accuracy.
% Check for timepoints
% Dialog update
if ~isempty(obj.Progdlg)
obj.Progdlg.Message = 'Validating results as masks';
end
% First, converts the results data into masks for any cases
% where masks are not present, e.g. point detections or
% contours
R = obj.Results;
for j = 1:size(obj.Results, 2)
for i = 1:size(obj.Results, 1)
r_ij = R{i,j};
if ~strcmp(obj.CheckResultType(r_ij), 'mask')
R{i,j} = obj.Points2Mask(r_ij);
end
end
end
% Dialog update
if ~isempty(obj.Progdlg)
obj.Progdlg.Message = 'Saving masks as image files...';
end
switch obj.maskfileext
case '.tif'
% Saves a tiff volume per timepoint
obj.ExportStackAsTiff(R)
case '.png'
% Saves png files with each being a 2D plane
obj.ExportStackAsPNGs(R)
case '.mat'
% Saves all data in a numerical array
end
end
function ExportStackAsTiff(obj, R)
% EXPORTSTACKASTIFF will export the stack of masks as a tiff
% volume. If there are multiple timepoints, those will be saved
% as separate tiff stacks with a frame identifying
if obj.istimeseries
for i = 1:obj.numframes
fid = [obj.maskfilepath filesep obj.maskfilename '_T' num2str(i) obj.maskfileext];
obj.WriteLogicalTiff(R{:,i}, fid)
end
else
fid = [obj.maskfilepath filesep obj.maskfilename obj.maskfileext];
obj.WriteLogicalTiff(R, fid)
end
end
function ExportStackAsPNGs(obj, R)
% EXPORTSTACKASPNGS will export individual 2D slices from the
% dataset into .png files with the name specifying where in the
% volume and/or timeseries they came from
% Name adjusted to consider time
if obj.istimeseries
for i = 1:obj.numframes
fid = [obj.maskfilepath filesep obj.maskfilename '_T' num2str(i) obj.maskfileext];
obj.WriteImageSlices(R{:,i}, fid)
end
else
fid = [obj.maskfilepath filesep obj.maskfilename obj.maskfileext];
obj.WriteImageSlices(R, fid)
end
end
function ExportStackAsMData(obj, R)
% EXPORTSTACKASMDATA will write the masks to an mdata file
% with variable name "Masks".
fid = obj.maskfid;
Masks = R;
save(fid, Masks, '-v7.3', '-nocompression')
end
function Mask = Points2Mask(obj, R)
% POINTS2MASK will convert a contour curve around an ROI into a
% mask and an unordered point cloud into a binary image with
% the points' nearest pixels turned on.
% Result type determines the algorithm
rtype = obj.CheckResultType(R);
% Creating a size vector
sz = [obj.mrows obj.ncols];
if strcmp(rtype, 'contour')
% Boundary curve
Mask = obj.Contour2Mask(R, sz);
elseif strcmp(rtype, 'pointcloud')
% Unordered pointcloud
Mask = obj.Pointcloud2Mask(R, sz);
elseif isempty(rtype)
% Creates an empty 2D image
Mask = false(sz);
end
end
function ConsolidateResults(obj, Segdata)
% CONSOLIDATERESULTS computes region properties for masks in a
% mask stack from the segmentation GUI and then proceeds to
% extract parameter values, parametric method used, and other
% settings used to produce the AllInfo struct as a proprty of
% the class. This has all information consolidated into one
% variable/property.
% Clearing previous region props prior to appending new info
obj.Allinfo = [];
% Drops aspect ratio since region props cannot compute it
if ~isempty(obj.Requestedprops) && any(contains(obj.Requestedprops, 'AspectRatio'))
idx = strcmp(obj.Requestedprops, 'AspectRatio');
obj.Requestedprops = obj.Requestedprops(~idx);
end
% Number of timepoints and slices that were segmented. These
% two lists can be sparse and noncontinuous.
N = numel(obj.timepoints);
M = numel(obj.slices);
for idxdim4 = 1:N
% Initialize struct to hold stats of a given timepoint
StackSegInfo = [];
% Index of segmented frame (timepoint)
t = obj.timepoints(idxdim4);
for jdxdim3 = 1:M
% Grabs index of segmented z slice
z = obj.slices(jdxdim3);
Resultsdata = obj.Results{z, t};
% Aspect ratio calculation requires bounding box
if obj.aspectratioreq && ~obj.bboxreq
obj.Requestedprops{end+1} = 'BoundingBox';
end
% Will determine region props if possible
Regionprops = obj.DetermineRegionProps(Resultsdata);
% Settings used for segmentation extracted one 2D image
% at a time
Temp = Segdata(z, t);
% Adds z and t index information to prevent ambiguity
Temp.Slice = z; % slice index
Temp.Timepoint = t; % time index
% Regionprops column entry for temporary struct
if ~isempty(Regionprops)
Temp.RegionProps = Regionprops;
else
Temp.RegionProps = 'NA';
end
% Concatenate results to segmentation settings info so
% that all information regarding a given z,t index is
% in one place
Temp.Results = Resultsdata;
StackSegInfo = vertcat(StackSegInfo, Temp);
% Progress update for user
Progress = ((idxdim4/N)*(jdxdim3/M))*0.5;
obj.Progdlg.Value = 0.5 + Progress;
obj.Progdlg.Message = [obj.dmsg newline ...
'Slice ' num2str(jdxdim3) '/' num2str(M) newline ...
'Timepoint: ' num2str(idxdim4) '/' num2str(N)];
end
% Nests the structs to take time into consideration
if ~isfield(obj.Allinfo, 'SegmentationInfo')
obj.Allinfo(1).SegmentationInfo = StackSegInfo;
else
obj.Allinfo(end+1).SegmentationInfo = StackSegInfo;
end
end
% Finalizes the struct with all
obj.SetDateTime
obj.Allinfo(1).Date = obj.Date;
obj.Allinfo(1).Time = obj.Time;
end
function ExportResultsWithSettings(obj)
% EXPORTRESULTSWITHSETTINGS exports the results and settings
% from the GUI in a single file.
if isempty(obj.Allinfo)
% Terminal Update
disp('No results to save')
if ~isempty(obj.Progdlg)
close(obj.Progdlg)
end
return
end
% UI Progress update
if ~isempty(obj.Progdlg)
obj.Progdlg.Message = 'Writing output file...';
end
% Savings results and settings
switch obj.outputext
case '.mat'
obj.WriteMAT(obj.outputfid)
case '.json'
obj.WriteJSON(obj.outputfid)
case '.xml'
obj.WriteXML(obj.outputfid)
case '.csv'
obj.WriteCSV(obj.outputfid)
case '.xlsx'
obj.WriteXLSX(obj.outputfid)
end
end
function Regionprops = DetermineRegionProps(obj, Resultsdata)
% DETERMINEREGIONSPROPS will get region props if the data is a
% mask, otherwise, it will leave the data alone.
if isempty(obj.Requestedprops)
Regionprops = [];
return
end
% Point clouds cannot produce region props but a contour can
% get converted into a masked ROI
rtype = obj.CheckResultType(Resultsdata);
if strcmp(rtype, 'pointcloud')
Regionprops = [];
return
elseif strcmp(rtype, 'contour')
sz = [obj.mrows obj.ncols];
Resultsdata = obj.Contour2Mask(Resultsdata, sz);
end
% Computes the requested region properties
Regionprops = regionprops(Resultsdata, obj.Requestedprops);
% Computes aspect ratio if requested
if obj.aspectratioreq
Regionprops = obj.AppendAspectRatio(Regionprops);
end
end
function Regionprops = AppendAspectRatio(obj, Regionprops)
% RPASPECTRATIO will modify region props to include the aspect
% ratio calculation. It will remove the bounding box field if
% the bounding box was not requested as well.
% Empty Regionprops suggests an empty mask image (no CC)
if ~isempty(Regionprops) && isfield(Regionprops, 'BoundingBox')
% Loop over connnected components (CC) for AR computation
for c = 1:numel(Regionprops)
% x:y aspect ratio computed from [L B W H] from BB
BB = Regionprops(c).BoundingBox;
Regionprops(c).AspectRatio = BB(3)/BB(4);
end
else
% Enables a blank entry
clear Regionprops
Regionprops.BoundingBox = [];
Regionprops.AspectRatio = [];
end
% Bounding box removed if user did not request it despite
% requesting the aspect ratio
if ~obj.bboxreq
Regionprops = rmfield(Regionprops, 'BoundingBox');
end
end
function UpdateData(obj, Data, z, t)
% UPDATEDATA will update the data struct that holds masks and
% contour points as well as settings used to produce the
% results
z = max(z, 1);
t = max(t, 1);
obj.Results{z, t} = Data;
% Updating arrays containing indices of segmented images
obj.slices = [obj.slices z];
obj.timepoints = [obj.timepoints t];
obj.slices = unique(obj.slices);
obj.timepoints = unique(obj.timepoints);
end
end
methods (Access = private)
%% Export Related
function SetDateTime(obj)
% Time and data info for the file being written
obj.Date = char(datetime('now', 'Format', 'MM-dd-yyyy'));
obj.Time = char(datetime('now', 'Format', 'hh:mm:ss'));
end
function WriteLogicalTiff(obj, I, fid)
% WRITELOGICALTIFF will write the logical image stack from the
% cell array of images, I by leveraging the fast tiff writer
% class.
% Opening the tiff object to be written
% obj.TiffWriter.Open(fid)
% Regular tiff object
tiffObj = Tiff(fid,'w');
Im = I{1, 1};
% Bit Depth (Bits per sample)
if isa(Im, 'uint8')
BPS = 8;
elseif isa(Im, 'uint16')
BPS = 16;
elseif isa(Im, 'logical')
BPS = 1;
end
% Generate tag structure
tags.BitsPerSample = BPS;
tags.Compression = Tiff.Compression.None;
tags.ImageLength = size(Im, 1);
tags.ImageWidth = size(Im, 2);
tags.Photometric = Tiff.Photometric.MinIsBlack;
tags.PlanarConfiguration = Tiff.PlanarConfiguration.Chunky;
tags.SampleFormat = Tiff.SampleFormat.UInt;
tags.SamplesPerPixel = 1;
% tags.SubFileType = Tiff.SubFileType.Mask; % Requires Photometric = Photometric.Mask.
% Only grabs non empty slices
check = ~cellfun(@isempty, I);
J = I(check);
for j = 1:numel(J)
tiffObj.setTag(tags);
write(tiffObj, J{j});
writeDirectory(tiffObj);
end
tiffObj.close();
end
function WriteMAT(obj, fid)
% WRITEMAT will write the segmentation settings and results as
% a struct in addition to a table for the points
% Segmentation settings
SegmentationResults = obj.Allinfo;
% Points
PointDetections = obj.PointsAsTable;
if exist(fid, 'file') == 2
save(fid, 'SegmentationResults', 'PointDetections', '-mat', '-nocompression', '-append')
else
save(fid, 'SegmentationResults', 'PointDetections', '-mat', '-v7.3', '-nocompression')
end
end
function WriteJSON(obj, fid)
% WRITEJSON will encode the results struct into a JSON format
% in pretty print and then replace single backslashes with
% double backslashed to ensure proper encoding upon writing
S = obj.Allinfo;
J = jsonencode(S, "PrettyPrint", true);
J = replace(J, '\', '\\');
fileID = fopen(fid, 'w');
fprintf(fileID, J);
fclose(fileID);
end
function WriteXML(obj, fid)
% WRITEXML will adjust the struct format for proper xml export
% and then write the file
% Segmentation results and settings
S = obj.Allinfo;
% Reorganize results for XML export
P = {S.SegmentationInfo.Results};
nslices = size(P, 2);
for i = 1:nslices
% Reorganization depends on result type