-
Notifications
You must be signed in to change notification settings - Fork 0
/
oxysoft2matlab.m
1841 lines (1655 loc) · 76.4 KB
/
oxysoft2matlab.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
function [nirs_data, events] = oxysoft2matlab(filename, target, savename, verbose, metaProj)
% This function reads in data exported from the Artinis software Oxysoft
% and returns a matlab-structure in a format that can be imported by other
% Matlab toolboxes, such as NIRS-SPM or Homer. The export can also be
% saved if desired.
%
% Use as:
% nirs_data = oxysoft2matlab(filename, target, savename, verbose, metaProj)
% where
% filename - string or cell, location of the file to be imported. The
% file has to originate from Oxysoft and must be an
% .oxy3 or oxy4-file, or an .oxyproj-file. If specifying an
% .oxyproj file, all .oxy3-files stored in there are imported.
% If using a cell-array and mixing oxy3/4 and oxyproj files,
% only the common oxy3/4-files are imported.
% target - string, the target toolbox. Can be 'rawOD', 'oxy/dxy',
% 'nirs-spm', 'nirs', or 'nap'.
% savename - string or cell, location where the export should be saved.
% Please specify without extension - it is automatically
% added. If empty, data will not be saved. If a cell, has
% to be of the same length as filename, which also has to
% be a cell-array then.
% verbose - boolean, true (default) means text output in command
% window.
% [metaProj - header of the project file]
%
% All input arguments are optional. UI dialogs will open and ask for the
% necessary information (what data to convert to which toolbox and where to
% save) when an input argument is missing.
% Data will be stored in the format of the toolbox, such that you can open
% the data from within the toolbox. The output argument of this function
% contains the data in the toolbox-specific format. If you specify 'raw'
% export format, a data structure with optical densities and positions of
% the system will be returned. You can transform the data to oxy- and
% deoxyvalues using the function transformoxy3od.m.
%
% An export of 'rawOD' will be returned as:
% nirs_data.OD - matrix NxM, where N=#samples and
% M=#measurements (transmitter wavelengths
% and receiver combinations). Contains the
% raw optical densities.
% An export of 'oxy/dxy' values will be returned as:
% nirs_data.oxyvals - matrix NxM, where N=#samples and
% M=#measurements (transmitter wavelengths
% and receiver combinations). Contains the
% relative change in oxygenated blood.
% nirs_data.dxyvals - matrix NxM, where N=#samples and
% M=#measurements (transmitter wavelengths
% and receiver combinations). Contains the
% relative change in deoxygenated blood.
% The export to 'oxy/dxy' can also contain additional fields for TSI data.
% These are absO2Hb, absHHb (absolute concentrations of (de-)oxygenated
% blood), TSI (tissue saturation index) and TSI_FF (TSI fit factor).
%
% Additional fields in the data structure for an export to rawOD or oxy/dxy
% data are:
% nirs_data.Fs - scalar, sampling frequency of the
% recording
% nirs_data.time - vector Nx1, , where N = #samples.
% General time axis in seconds, starting
% at 0s.%
% nirs_data.DPF - vector, differential path length per
% optode template
% nirs_data.wavelengths - matrix LxT, where L=#wavelengths and
% T=#transmitters. Contains wavelengths
% per transmitter.
% nirs_data.label - cell 1xC, where C=#channels. Unique
% channel names.
% nirs_data.RxLabel - vector 1xR, where R=#receivers. Unique
% receiver labels.
% nirs_data.TxLabel - vector 1xT, where T=#receivers. Unique
% transmitter labels.
% nirs_data.distance - vector 1xC, where C=#channels. Distance
% in cm between receiver and transmitter.
% nirs_data.chanPos - vector 2xC, where C=#channels. Position
% of the channels in cm relative in an
% arbitrary coordinate system.
% nirs_data.transPos - vector 2xT, where T=#transmitters.
% Position of the transmitters in cm
% relative in an arbitrary coordinate
% system.
% nirs_data.receiPos - vector 2xC, where R=#receivers. Position
% of the receivers in cm relative in an
% arbitrary coordinate system.
% nirs_data.Rx_TxId - vector, 2xM, where M=#measurements.
% Indexes which receivers (first row) and
% transmitters (second row) were used to
% measure the optical densities.
% nirs_data.ADvalues - matrix NxA, where N=#samples
% A=#additonal channels. All data of the
% additional channels that were attached
% to the system.
% nirs_data.metaInfo - struct. Meta and header information of
% the data.
%
% In case of an export to only ODs ('rawOD'), the structure will additionally
% have these fields:
% nirs_data.ODlabel - cell 1xC, where M=#measurements.
% Respective channel names and wavelength
% combination. Redundant with
% .wavelength, .label and .Rx_TxId
% nirs_data.ODchanPos - vector 2xM, where M=#measurement. Position
% of the measurement in cm relative in an
% arbitrary coordinate system. Redundant
% with .chanPos and .Rx_TxId.
%
% In case of an export to only ODs ('rawOD'), you should be able to use the
% transformoxy3od function in the private/ directory to convert to
% concentrations. use as
% transformoxy3od(nirs_data.OD, nirs_data.metaInfo, nirs_data.DPF)
% For more, see help of that function
%
%
% Version 1.67, copyright (c) by Artinis Medical Systems http://www.artinis.com
% Author Jörn M. Horschig, [email protected]
% License Creative Commons Attribution-ShareAlike 4.0 International License
%
% See also OXYSOFTMNIIMPORT
%%
% Version history
%
% v 1.67 enh - adding support for offline PM/PL files
% 05/29/20 (assuming default muscle/brain measurement parameters)
%
% v 1.66 fix - correct computation of channels for ld oxy3->oxy4 converted
% 11/20/19 data
% fix - storing with v7.3 on all formats
% fix - wavelength output for multiple devices and 'rawOD' and
% 'oxy/dxy' option
% fix - order of single- and multistack devices within a single
% measurement stays intact
% enh - allow conversion of data with two or more LSL streams
%
% v 1.65 fix - ignoring calibration data optode template
% 08/01/19
%
% v 1.64 enh - files incl. big variables (>2gig) will be stored with '-v7.3'
% 06/26/19
%
% v 1.63 enh - smarter way to determine bytes to be read for oxy4-files
% 06/24/19
%
% v 1.62 fix - reverting back to intensity mapping for Homer2
% 06/13/19 fix - correct extraction of DPF when first template is empty
%
% v 1.61 enh - very old oxy3-files supported (H2O)
% 05/14/19 enh - also extracting event description from oxyproj files
% fix - empty template (PortaSync) as first device supported
% fix - readout of laser mapping for versions with patch number
% (e.g. Oxysoft 3.2.51.4)
% fix - correced DPF readout for multi-system measurements
% (if necessary)
%
% v 1.60 fix - manual optodetemplate selection
% 04/15/19
%
% v 1.59 fix - reading out event names (letters) correctly again
% 04/11/19
%
% v 1.58 enh - adding support for EEG only data
% 04/04/19
%
% v 1.57 fix - fixing a compatibility issue with Oxysoft 3.2.27.2
% 03/29/19 fix - corrected reading in behaviour for long files
% enh - improved optodetemplate file finding routine
% enh - stratifying private functions with fieldtrip
%
% v 1.56 fix - several stability issues / crash preventions
% 02/01/19 fix - more robust single device multi channel TSI calculation
%
% v 1.55 fix - avoiding crash of reading in oxy4 created from oxy3-file
% 01/18/19
%
% v 1.54 fix - reading in last samples of oxy4-file always
% 12/21/18 enh - updated MNI import for NIRStorm
%
% v 1.53 fix - old oxy3-files supported again (<Oxysoft 3.2)
% 11/22/18
%
% v 1.52 fix - duplicate samples in oxy4-files deleted if present
% 11/16/18 enh - returning sample number in rawOD or oxy/dxy output
% when reading in oxy4-files
%
% v 1.51 fix - backwards support for older oxy3-files inc. with 1.50
% 11/01/18 fix - oxy4-PM/PL files can be read in again
% fix - critical bug for reading in oxy4 data (was looping samples)
%
% v 1.50 fix - AD label names correctly read in (or not)
% 10/31/18 fix - oxy3 can handle dataFormat 2 AD channels now
% enh - reading of oxy4 data in 10 bigger chunks (if possible)
%
% v 1.49 fix - corrected autodetect path for 64-bit Oxysoft
% 10/23/18 fix - corrected TSI rounding
% fix - AD channels in new data format correctly read in
% enh - improving EEG channel support (ADClabel field added/
% empty subtemplate supported)
%
% v 1.48 fix - circumvent missing endsample crash in oxy4-file
% 07/27/18
%
%
% v 1.47 enh - added support for Brite-offline files
% 07/23/18 enh - persistent storing of optodetemplate-file location
% enh - stratified TSI implementation (minimal effect here)
%
% v 1.46 fix - faulty copy of optodetemplateparameters circumvented
% 07/04/18
%
% v 1.45 fix - fixing crash with empty events
% 07/03/18
%
%
% v 1.44 enh - name of file to be saved predefined on input name
% 06/22/18 fix - corrected calculation of number of channels in oxy4
% fix - adjusted to new laser/A
%
% v 1.43 fix - oxy4-file read-in improved and fixed
% 04/26/18
%
% v 1.42 fix - multi-system-subtemplate measurements correctly supported
% 04/13/18
%
% v 1.41 enh - added error message for a common error (custom optodetemplate
% 03/29/18 definition is erroneous)
% fix - improved capability to read in long, multi-device oxy4-files
% fix - Better support for multi-template measurements
%
% v 1.40 enh - Added support for AtlasViewer in oxysoftMNIImport
% 01/15/18
%
% v 1.39 enh - Allowing oxy4-file reading
% 11/08/17 fix - error message when file not found corrected
% fix - critical bug prossibly overwriting oxy3-files
% enh - oxyproj optodetemplate precedence message adjusted
%
% v 1.38 fix - Rx/Tx numbers in optodetemplate can be in any order
% 10/13/17 fix - y-position offset for multiple templates corrected
%
% v 1.37 fix - homer/spm: lower wavelength always preceeding higher one
% 10/11/17
%
% v 1.36 fix - long event description supported
% 09/15/17 fix - various smaller bugfixed wrt event parsing
%
% v 1.35 fix - supporting multiple optodetemplates in .oxyproj
% 07/12/17
%
% v 1.34 fix - workaround if optodetemplate undefined
% 04/27/17
%
% v 1.33 fix - empty oxy3 header info handled properly
% 04/26/17 fix - optodetemplate parsing fixed
%
% v 1.32 fix - more robust optodetemplate-file selection
% 02/22/17
%
% v 1.31 fix - stable now if no optode template in oxy3
% 02/20/17
%
% v 1.30 fix - Homer2: added default condition name '1' if no conds
% 11/21/16 present
%
% v 1.29 fix - NIRS-SPM fix w/o oxyproj
% 11/21/16
%
% v 1.28 fix - works again without using an oxyproj-file
% 10/31/16 enh - add a message to send a mail for newest version
%
% v 1.27 enh - oxyproj files can point to a different directory than the
% 10/27/16 oxy3-file is in to enable working at another computer
%
% v 1.26 enh - also reading in optode parameters (DPF etc.) from project
% 10/10/16
%
% v 1.25 fix - more robust way of extracting event name and descriptions
% 09/29/16 fix - removing last sample if if was incompletely transferred
%
% v 1.24 fix - use event names if not all names have a description
% 09/13/16
%
% v 1.23 fix - corrected wavelengths counting in multi-Oxymon systems
% 09/12/16 fix - corrected single event handling nomenclature
% fix - improved behaviour when optodetemplate.xml not found
%
% v 1.22 enh - 3D positions correctly extracted and stored for NIRS-SPM
% 08/26/16 see also new function "oxysoftMNIImport"
%
% v 1.21 fix - corrected OD extraction when not all Tx but more than
% 08/24/16 one Rx was used
%
% v 1.20 fix - single oxyproj selection handled correctly again
% 08/08/16
%
% v 1.19 fix - Condition names properly exported to Homer2
% 07/18/16 fix - when optodetemplate not found in xml, ask for alternative
% enh - consecutive numbers in multi-system optodetemplates supported (?)
%
% v 1.18 enh - Condition names properly exported to Homer2
% 07/05/16
%
% v 1.17 fix - filename selection in UI dialog fixed
% 07/04/16 fix - mixup of triggers/events order removed
%
% v 1.16 fix - removed ~ references to increase backwards compatbility
% 06/20/16
%
% v 1.15 enh - multiple TSI system support
% 06/01/16
%
% v 1.14 fix - NAP single event bug
% 05/25/16 fix - projevents initialized
%
% v 1.13 fix - conditions when to ask for saving export
% 05/24/16 fix - project events return a double instead of a string
% fix - oxyproj read in
% fix - events export to Homer2
% enh - enabling 3D digitized position read-in
% enh - allowing NAP-export for all templates%
% doc - timestamp fix of v 1.12
%
% v 1.12 list of improvements and bugfixes:
% 05/20/16 fix - automatically adding oxy3 extension
% enh - allowing for batch processing of oxy3-files
% enh - enabling reading in of oxyproj files
% enh - allowing filename and savename to be a cell-array
%
% v 1.11 fixed a critical optodetemplate naming bug
% 05/03/16
%
% v 1.10 fixed a critical optodetemplate naming bug
% 04/25/16
%
% v 1.09 verbose documentation added, OD vals checked again
% 02/22/16
%
% v 1.08 multi-channel, raw OD channel labels corrects
% 04/08/15 timestamp of v1.07 corrected
%
% v 1.07 added information on used templates and optodetemplates.xml
% 06/07/15 also changed precedence of optodetemplates.xml
% and bugfixes (TSI+noTSI, wavelengths of diff. subtemplates)
%
% v 1.06 added SpatialUnit ('cm') to better support Homer2 v2.0
% 12/06/15 also fixed a bug for auxiliary channels (AD channels)
%
% v 1.05 improved memory handling and increased backwards compatibitliy
% 17/02/15 and also fixed channel label for raw transformation and structure for Homer
%
% v 1.04 fixed a bug pertaining to reading out of event names and in
% 12/22/14 case of having recorded multichannel TSI
%
% v 1.03 fixed a bug and inconsistency regarding reading out of event names
% 12/08/14
%
% v 1.02 fixed another crash during parsing of the header when Version
% 11/11/14 was not present in metaInfo and extended rawOD output with
% the (redundant) fields ODlabel and ODchanPos.
%
% v 1.01 removed the use of strjoin for backwards compatibility and fixed
% 11/04/14 a crash during parsing of the header when Version was not
% present in metaInfo
%
% v 1.0 initial release
% 10/30/14
%% input error handling
if nargin < 5
metaProj = [];
fprintf('[Artinis] Thank you for using oxysoft2matlab v1.67 (c) Artinis Medical Systems.\n');
if nargin < 4
verbose = true;
if nargin < 3
savename = '';
end
end
end
% check if all input arguments were specified, otherwise use a UI dialog
if nargin < 1 || isempty(filename)
w = true;
while w
[filename,pathname] = uigetfile({'*.oxyproj; *.oxy3; *.oxy4', 'Oxysoft Files (*.oxy3, *.oxy4, *.oxyproj)'}, ...
'Select one or several .oxy3/.oxy4/.oxyproj-file(s)', ...
'MultiSelect', 'on');
if isscalar(filename) && filename ==0
if verbose
fprintf('[Artinis] user aborted.\n');
end
return;
end
if ischar(filename)
filename = {filename};
end
for f=filename
if ~strcmp(f{1}(end-4:end), '.oxy3') && ~strcmp(f{1}(end-4:end), '.oxy4') && ~strcmp(f{1}(end-7:end), '.oxyproj')
errorbox('Please only select oxy3-, oxy4- or oxyproj-files.')
w = true;
break;
else
w = false;
end
end
end
for f=1:numel(filename)
filename{f} = fullfile(pathname, filename{f});
end
end
% check whether target toolbox is supported
% supported_targets = {'nirs-spm', 'nirs', 'nap', 'fosa', 'p3', 'potato'};
supported_targets = {'nirs-spm', 'nirs', 'nap', 'rawOD', 'oxy/dxy','snirf'};
if nargin < 2 || isempty(target )
target_idx = menu('Choose a target toolbox',supported_targets);
target = supported_targets{target_idx};
drawnow;
fprintf('[Artinis] Selected target toolbox %s.\n', target);
end
if ~ismember(target, supported_targets)
tgts = sprintf('''%s'', ', supported_targets{1:end-1});
tgts = sprintf('%s and ''%s''', tgts(1:end-2), supported_targets{end});
error('[Artinis] target toolbox cannot be identified. please choose among %s.', tgts);
end
supported_extensions = {'oxy3','oxy4', 'oxyproj'};
measurements = struct('name', '', ...
'file', '', ...
'optodeTemplateID', [], ...
'optodeParameters', struct('DPF', [], 'position', [], 'absK', [], 'absH', [], 'absH2O', [], 'useH2O', [], 'gradient', []), ...
'events', struct('value', -1, 'description', '', 'onset', inf), ...
'positions', struct('fid', struct('name', '', 'pos', []), 'rx', struct('name', '', 'pos', []), 'tx', struct('name', '', 'pos', [])), ...
'isoxy3', false);
if iscell(filename) && numel(filename)>1
% check whether each file has an extension, does exist and is supported
[filepath, filename, extension] = cellfun(@fileparts, filename, 'UniformOutput', false);
for f=1:numel(filename)
% assuming oxy3 extension if not provided
if isempty(extension{f})
if verbose
fprintf('[Artinis] No file extension provided, assuming .oxy3\n');
end
%filename = [filename '.oxy3'];
extension{f} = '.oxy3';
end
% check whether file exists
if ~exist(fullfile(filepath{f}, [filename{f} extension{f}]), 'file')
error('[Artinis] file ''%s'' cannot be located. please specify a valid filename.', fullfile(filepath{f}, [filename{f} extension{f}]));
end
% create a 'measurement'-list of files
switch extension{f}
case '.oxyproj'
% parse the content here
fprintf('[Artinis] Reading in oxyproj-file... please wait...')
measurements = [measurements parseoxyproj(fullfile(filepath{f}, [filename{f} extension{f}]))];
fprintf('done!\n')
case '.oxy3'
% handling the * wildcard here
if ismember('*', filename{f})
files = dir(fullfile(filepath{f}, [filename{f} extension{f}]));
for fl=files'
measurements(end+1).file = [fl.name extension{f}];
measurements(end).events = [];
measurements(end).isoxy3 = true;
end
else
measurements(end+1).file = fullfile(filepath{f}, [filename{f} extension{f}]);
measurements(end).events = [];
measurements(end).isoxy3 = true;
end
case '.oxy4'
% handling the * wildcard here
if ismember('*', filename{f})
files = dir(fullfile(filepath{f}, [filename{f} extension{f}]));
for fl=files'
measurements(end+1).file = [fl.name extension{f}];
measurements(end).events = [];
measurements(end).isoxy3 = true;
end
else
measurements(end+1).file = fullfile(filepath{f}, [filename{f} extension{f}]);
measurements(end).events = [];
measurements(end).isoxy3 = true;
end
otherwise
exts = sprintf('.%s-, ', supported_extensions{1:end-1});
exts = sprintf('%s or .%s-', exts(1:end-3), supported_extensions{end});
error('[Artinis] file extension not supported. please select a file in %sformat.', exts);
end
end
% check for each entry int he measurement-list if an 'isoxy3' does exist
m=1;
[dirs, names, exts] = cellfun(@fileparts, {measurements(:).file}, 'UniformOutput', false);
while m<=numel(measurements)
if ~measurements(m).isoxy3
% check if another measurement with the same name exists
midx = ismember(cellfun(@lower, names, 'UniformOutput', false), lower(names(m))) & [measurements(:).isoxy3];
midx = find(midx);
if isempty(midx)
% delete this measuement
measurements(m) = [];
names(m) = [];
else
% join the event of this measurement with the other one
for mi = midx
% oxy3-files do not have some parameters, they are meta-information of the project
measurements(mi).name = measurements(m).name;
measurements(mi).optodeTemplateID = measurements(m).optodeTemplateID;
measurements(mi).optodeParameters = measurements(m).optodeParameters;
% join events of the two (and positions)
measurements(mi).events = [measurements(mi).events measurements(m).events];
measurements(mi).positions = [measurements(mi).positions measurements(m).positions];
end
m = m+1;
end
else
m = m+1;
end
end
% and remove all those which are of non-oxy3 origin
midx = [measurements.isoxy3]==false;
measurements(midx) = [];
names(midx) = [];
else
if iscell(filename)
filename = filename{1};
end
% handle a single file here
[filepath, filename, extension] = fileparts(filename);
% assuming oxy3 extension if not provided
if isempty(extension)
if verbose
fprintf('[Artinis] No file extension provided, assuming .oxy3\n');
end
extension = '.oxy3';
end
% check whether file exists
if ~exist(fullfile(filepath, [filename extension]), 'file') && ~ismember('*', filename)
warning('[Artinis] file ''%s'' cannot be located. Please specify a valid filename.', fullfile(filepath, [filename extension]));
nirs_data = [];
events = [];
return;
end
measurements = [];
switch extension
case '.oxyproj'
% parse the content here
measurements = parseoxyproj(fullfile(filepath, [filename extension]));
case '.oxy3'
% handling the * wildcard here
if ismember('*', filename)
files = dir(fullfile(filepath, [filename extension]));
if isempty(files)
error('[Artinis] There are no oxy3-files in %s. Please specify a valid filename.', filepath);
end
for f=files'
measurements(end+1).file = fullfile(filepath, f.name);
measurements(end).events = [];
measurements(end).positions = [];
end
else
% all good, single file
end
case '.oxy4'
% handling the * wildcard here
if ismember('*', filename)
files = dir(fullfile(filepath, [filename extension]));
if isempty(files)
error('[Artinis] There are no oxy4-files in %s. Please specify a valid filename.', filepath);
end
for f=files'
measurements(end+1).file = fullfile(filepath, f.name);
measurements(end).events = [];
measurements(end).positions = [];
end
else
% all good, single file
end
otherwise
exts = sprintf('.%s-, ', supported_extensions{1:end-1});
exts = sprintf('%s or .%s-', exts(1:end-3), supported_extensions{end});
error('[Artinis] file extension not supported. please select a file in %sformat.', exts);
end
end
if ~isempty(measurements)
nirs_data = [];
events = [];
% recursively call this method for each file and save the output in a cell-array
nf = 0;
for f=measurements
[p, n, e] = fileparts(f.file);
nf = nf+1;
if ~iscell(savename)
tmp = savename;
savename = cell(1);
savename{1} = tmp;
end
if nargin < 2
savename = [];
savename{nf} = 'batch';
elseif nargin < 3
savename = [];
savename{nf} = '';
elseif numel(savename) < nf
if isempty(savename{1})
savename = [];
savename{nf} = [];
else
savename = [];
savename{nf} = [f.file '.mat'];
end
end
[nirs_data{end+1} events{end+1}] = oxysoft2matlab(fullfile(p, [n e]), target, savename{nf}, verbose, f);
end
return
end
% user interface asking for saving the exported file
if nargin < 2 || strcmp(savename, 'batch') % yes, <2 - I assume that a user does not know what he does if he does not specify all input arguments
[~, savename, ~] = fileparts(filename);
if strcmp(target,'snirf')
[savename,pathname] = uiputfile([savename '.snirf'], sprintf('Choose a destination for saving the export of %s to %s', filename, target));
if savename == 0
savename = []; % no saving desired
else
savename = savename(1:end-5); % will be added later
end
elseif strcmp(target, 'nirs')
[savename,pathname] = uiputfile([savename '.nirs'], sprintf('Choose a destination for saving the export of %s to %s', filename, target));
if savename == 0
savename = []; % no saving desired
else
savename = savename(1:end-5); % will be added later
end
else
[savename,pathname] = uiputfile([savename '.mat'], sprintf('Choose a destination for saving the export of %s to %s', filename, target));
if savename == 0
savename = []; % no saving desired
else
savename = savename(1:end-4); % will be added later
end
end
if ~isempty(savename)
savename = fullfile(pathname, savename);
else
if verbose
fprintf('[Artinis] Save dialog aborted - export will not be saved on disk.\n');
end
end
elseif nargin < 3
if verbose
fprintf('[Artinis] command-line input detected - assuming export should not be stored on disk.\n');
end
savename = [];
end
%% import input file
% here, we import the relevant pieces of information. these are:
% oxy- and deoxy-values, #channels, #transs, #detector, sampling rate,
% wavelengths, optode distance, DPF (and how it was obtained)
% and, if possible, we also extract event information:
% # conditions, condition names and condition onsets
if verbose
fprintf('[Artinis] Reading in data...');
end
switch(extension)
case '.oxy3'
% import to matlab function
[rawOD,metaInfo,ADvalues] = readoxy3file(fullfile(filepath, sprintf('%s%s', filename, extension)));
case '.oxy4'
% import to matlab function
[rawOD,metaInfo,ADvalues,sample] = readoxy4file(fullfile(filepath, sprintf('%s%s', filename, extension)));
otherwise
error('[Artinis] no support for %s-files, yet. if urgent, please send a mail to [email protected].', extension);
end
% overwrite metaInfo with metaProj
if ~isempty(metaProj)
if isfield(metaProj, 'optodeTemplateID') && ~isempty(metaProj.optodeTemplateID)
if isfield(metaInfo, 'optodeTemplateID') && ~isequal(metaProj.optodeTemplateID, metaInfo.optodeTemplateID)
warning('[Artinis] Preferring optode template found in oxyproj.');
end
if length(metaInfo.OptodeTemplateID) ~= length(metaProj.optodeTemplateID)
error('[Artinis] the number of optode templates in the .oxyproj-file does not coincide with the one in the measurement.');
end
metaInfo.OptodeTemplateID = metaProj.optodeTemplateID;
end
if isfield(metaProj, 'optodeParameters') && ~isempty(metaProj.optodeParameters)
fprintf('[Artinis] overwriting measurement parameters with settings from project.\n');
% do not check length here, as there can be more or less subtemplates than originally now!
metaInfo.DPF = metaProj.optodeParameters.DPF;
metaInfo.Position = metaProj.optodeParameters.position;
metaInfo.abs_K = metaProj.optodeParameters.absK;
metaInfo.abs_H2O = metaProj.optodeParameters.absH2O;
metaInfo.use_H2O = metaProj.optodeParameters.useH2O;
metaInfo.Gradient = metaProj.optodeParameters.gradient;
end
end
%% extract data
% get all relevant datachannels by the optode template file
if verbose
fprintf(' arranging optodes...\n')
end
[oxyOD2, metaInfo] = arrangeoxy3optodes(rawOD', metaInfo);
if verbose
if isfield(metaInfo, 'OptodeTemplatesFile')
fprintf('[Artinis] assuming desired optode templates in "%s...', metaInfo.OptodeTemplatesFile);
end
end
if isnumeric(oxyOD2) && oxyOD2==-1 % if no data was found
if verbose
if isfield(metaInfo, 'OptodeTemplatesFile')
fprintf(' but the appropriate template(s) were not found in there.\n');
end
end
% checking for a local version (in path) of optodetemplates.xml
metaInfo.checkLocal = true;
[oxyOD2, metaInfo] = arrangeoxy3optodes(rawOD', metaInfo);
if verbose
if isfield(metaInfo, 'OptodeTemplatesFile')
fprintf('[Artinis] assuming desired optode templates in "%s...', metaInfo.OptodeTemplatesFile);
end
end
if isnumeric(oxyOD2) && oxyOD2==-1 % if no data was found again
if verbose
if isfield(metaInfo, 'OptodeTemplatesFile')
fprintf(' but the appropriate template(s) were not found in there.\n');
end
end
% let the user decide manually
metaInfo.checkLocal = false;
fprintf('[Artinis] no appropriate optodetemplates.xml found - please choose one manually!\n');
[oxyOD2, metaInfo] = arrangeoxy3optodes(rawOD', metaInfo);
if verbose
if isfield(metaInfo, 'OptodeTemplatesFile')
fprintf('[Artinis] assuming desired optode templates in "%s...', metaInfo.OptodeTemplatesFile);
end
end
% okay, all previous attempts to select the correct optodetemplates.xml
% failed and the user did not choose the correct optodetemplates.xml, so error
if isnumeric(oxyOD2) && oxyOD2==-1
error('[Artinis] cannot find correct optode template in optodetemplate.xml');
end
end
end
if verbose
fprintf(' and I found the desired optode templates in there\n');
fprintf('[Artinis] %d template(s) are used\n', numel(metaInfo.OptodeTemplateID));
for i=1:numel(metaInfo.OptodeTemplateID)
if iscell(metaInfo.OptodeTemplateID)
fprintf('[Artinis] template#%d - ID:%d (name: %s)\n', i, metaInfo.OptodeTemplateID{i}, metaInfo.OptodeTemplateName{i});
else
fprintf('[Artinis] template#%d - ID:%d (name: %s)\n', i, metaInfo.OptodeTemplateID(i), metaInfo.OptodeTemplateName{i});
end
end
end
needsOnlyODs = any(ismember(target, {'nirs', 'rawOD'}));
% concatenate all data
OD = [];
oxyvals = [];
dxyvals = [];
ua = [];
absO2Hb = [];
absHHb = [];
TSI = [];
TSI_FF = [];
nTransmitters = 0;
nReceivers = 0;
label = {};
RxLabel = {};
TxLabel = {};
ADCLabel = {};
TSILabel = {};
wavelengths = [];
distance = [];
devices = [];
chanPos = [];
transPos = [];
receiPos = [];
transId = [];
receiId = [];
RxId = [];
TxId = [];
Rx_TxId = [];
Rx_TxwLengths = [];
RxSubTemplateId = [];
TxSubTemplateId = [];
DPF = [];
% note, we'll parse a lot here, in case future-me/-you needs more info
if isfield(oxyOD2, 'Sys')
for d=1:numel(oxyOD2.Sys)
% skip if empty (Portasync/AD box)
if isempty(oxyOD2.Sys(d).subtemplate)
continue;
end
OD = [OD oxyOD2.Sys(d).subtemplate(:).OD];
devices = [devices; metaInfo.Sys(d).nRx metaInfo.Sys(d).nTx 0];
nTransmitters = nTransmitters + metaInfo.Sys(d).nTx;
nReceivers = nReceivers+ metaInfo.Sys(d).nRx;
if numel(oxyOD2.Sys)>1
% concatenate System number here
label = [label strcat(sprintf('S%i-', d),[metaInfo.Sys(d).subtemplate(:).RxTx])];
RxLabel = [RxLabel strcat(sprintf('S%i-', d),metaInfo.Sys(d).Rx.names)];
TxLabel = [TxLabel strcat(sprintf('S%i-', d),metaInfo.Sys(d).Tx.names)];
else
label = [label [metaInfo.Sys(d).subtemplate(:).RxTx]];
RxLabel = [RxLabel metaInfo.Sys(d).Rx.names];
TxLabel = [TxLabel metaInfo.Sys(d).Tx.names];
end
wavelengths = [wavelengths metaInfo.Sys(d).Wavelength];
distance = [distance metaInfo.Sys(d).subtemplate(:).dist];
chanPos = [chanPos [metaInfo.Sys(d).subtemplate(:).pos]];
transPos = [transPos metaInfo.Sys(d).Tx.pos];
receiPos = [receiPos metaInfo.Sys(d).Rx.pos];
for s=1:numel(metaInfo.Sys(d).subtemplate)
DPF = [DPF [metaInfo.Sys(d).subtemplate(s).DPF{:}]];
end
% Below Ids are only used for giving unique numbers, thus correct for
% multiple systems
maxRx = max([receiId 0]);
maxTx = max([transId 0]);
transId = [transId maxTx+[metaInfo.Sys(d).Tx.unique_id{:}]];
receiId = [receiId maxRx+[metaInfo.Sys(d).Rx.unique_id{:}]];
maxRx = max([RxId 0]);
maxTx = max([TxId 0]);
RxId = [RxId cell2mat([metaInfo.Sys(d).subtemplate(:).iRx])+maxRx];
TxId = [TxId cell2mat([metaInfo.Sys(d).subtemplate(:).iTx])+maxTx];
RxSubTemplateId = [RxSubTemplateId metaInfo.Sys(d).Rx.subtemplate];
TxSubTemplateId = [TxSubTemplateId metaInfo.Sys(d).Tx.subtemplate];
Rx_TxId = [Rx_TxId [metaInfo.Sys(d).subtemplate(:).cmbs]+...
repmat([maxRx; maxTx], 1, size([metaInfo.Sys(d).subtemplate(:).cmbs], 2))];
wLengths = vertcat(cell2mat([metaInfo.Sys(d).subtemplate(:).Wavelength]));
Rx_TxwLengths = [Rx_TxwLengths; wLengths(:)];
if ~needsOnlyODs || isfield(metaInfo.Sys(d).subtemplate(:), 'TSI')
[toxyvals, tdxyvals,tTSI,tTSI_FF,tabsO2Hb,tabsHHb] = transformoxy3od([oxyOD2.Sys(d).subtemplate(:).OD],metaInfo.Sys(d),metaInfo.DPF);
oxyvals = [oxyvals toxyvals];
dxyvals = [dxyvals tdxyvals];
if ~isempty(tTSI) && strcmp(target, 'oxy/dxy')
absO2Hb = [absO2Hb tabsO2Hb'];
absHHb = [absHHb tabsHHb'];
TSI = [TSI tTSI'];
TSI_FF = [TSI_FF tTSI_FF'];
for t=1:numel(metaInfo.Sys(d).subtemplate)
if ~isempty( metaInfo.Sys(d).subtemplate(t).TSI)
if numel(oxyOD2.Sys)>1
% concatenate System number here
TSILabel{end+1} = strcat(sprintf('S%i-', d), [metaInfo.Sys(d).subtemplate(t).TSI.RxTx]);
else
TSILabel{end+1} = metaInfo.Sys(d).subtemplate(t).TSI.RxTx;
end
end
end
end
end
end
devices(end, 3) = metaInfo.nADC;
if isfield(metaInfo, 'ADCNames')
ADClabel = metaInfo.ADCNames;
else
ADClabel = [];
end
rawvals = OD;
advals = ADvalues'; % samples x channels as homer and nirs-spm
nSamples = size(rawvals, 1);
nChannels = numel(label); % channels == optode-pairs
nDevices = numel(metaInfo.Device);
fs = 1/metaInfo.SampleTime;
nWavelengths = numel(unique(wavelengths));
% differential path-length factor
DPF_correction = 'none';
if unique(length(transId))*2 ~= numel(wavelengths) && ~needsOnlyODs
error('[Artinis] more than two laser per transmitter unit cannot be transformed to oxy- and deoxyvalues, yet. If your system had 3 wavelengths. the output of this function will be wrong! Please contact the Artinis support.');
end
else
warning('No NIRS data found, only exporting AD channels');
if isfield(metaInfo, 'ADCNames')
ADClabel = metaInfo.ADCNames;
else
ADClabel = [];
end
rawvals = [];
oxyvals = [];
dxyvals = [];
advals = ADvalues'; % samples x channels as homer and nirs-spm
nSamples = size(advals, 1);
label = {};
nChannels = numel(label); % channels == optode-pairs
nDevices = numel(metaInfo.Device);
fs = 1/metaInfo.SampleTime;
nWavelengths = 0;
% differential path-length factor
DPF = 0;
DPF_correction = 'none';
Rx_TxwLengths = 0;
wLengths = [];
distance = 0;
RxLabel = {};
TxLabel = {};
Rx_TxId = [];
chanPos = [];
transPos = [];
receiPos = [];
RxId = [];
TxId = [];
end
%% create output structure
nirs_data = [];
switch(target)
case {'rawOD', 'oxy/dxy'}
if strcmp(target, 'rawOD')
nirs_data.OD = rawvals;
% also change labels and channel positions respectively
nirs_data.ODlabel = cell(1, size(rawvals, 2));
nirs_data.ODchanPos = zeros(2, size(rawvals, 2));
for i=1:numel(Rx_TxwLengths)/2 % TODO FIXME: hardcoded - assuming 2 wavelengths per Tx
nirs_data.ODlabel{2*i-1} = strcat(label{i}, '@', num2str(Rx_TxwLengths(2*i-1)), 'nm');
nirs_data.ODlabel{2*i} = strcat(label{i}, '@', num2str(Rx_TxwLengths(2*i)), 'nm');
nirs_data.ODchanPos(:, 2*i-1) = chanPos(:, i);
nirs_data.ODchanPos(:, 2*i) = chanPos(:, i);
end
else
nirs_data.oxyvals = oxyvals;
nirs_data.dxyvals = dxyvals;
if ~isempty(TSI) % only if TSI data is available in the measurement
nirs_data.absO2Hb = absO2Hb;
nirs_data.absHHb = absHHb;
nirs_data.TSI = TSI;
nirs_data.TSILabel = TSILabel;
nirs_data.TSI_FF = TSI_FF;
end
end
nirs_data.time = (0:nSamples-1)' ./ fs;
if exist('sample', 'var')
nirs_data.sampleNo = sample';
end
nirs_data.wavelengths = [Rx_TxwLengths(1:2:end) Rx_TxwLengths(2:2:end)]';
nirs_data.DPF = metaInfo.DPF;
nirs_data.distance = distance;
nirs_data.Fs = fs;
nirs_data.label = label;
nirs_data.RxLabel = RxLabel;
nirs_data.TxLabel = TxLabel;
nirs_data.Rx_TxId = Rx_TxId;
nirs_data.chanPos = chanPos;
nirs_data.transPos = transPos;
nirs_data.receiPos = receiPos;