IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
opus2data.m
Go to the documentation of this file.
1 %>@ingroup ioio
2 %>@file
3 %> @brief Merges several OPUS binary files into a dataset
4 %>
5 %> @c data.groupcodes is made from file name. File name is trimmed at the trimdot-th dot counted from right to left. E.g.,
6 %> This allows one to trim "sample.0.dat" at the penultimate dot (ignodedoctount=2) or "sample.dat" at the last dot
7 %> (trimdot=1)
8 %>
9 %> data.obsnames will contain the file names
10 %>
11 %> For reference on parameters, please check @ref mergetool.m
12 %>
13 %> @sa mergetool.m
14 
15 %> @param wild
16 %> @param trimdot See pirtool
17 %> @param flag_image
18 %> @param height
19 %> @return A dataset
20 function [data, flag_error] = opus2data(wild, trimdot, flag_image, height)
21 if ~exist('trimdot')
22  trimdot = 2;
23 end;
24 
25 [filenames, groupcodes] = resolve_dir(wild, trimdot, flag_image);
26 
27 no_files = length(filenames);
28 
29 if flag_image
30  if no_files/height ~= floor(no_files/height)
31  irerror('Opus2Data: Invalid image height!');
32  end;
33 end;
34 
35 
36 path_ = fileparts(wild);
37 
38 data = irdata();
39 
40 namestotry = {'Ratio', 'RatioChanged', 'RatioAbsorption'};
41 idxs = 1:numel(namestotry);
42 idxsnow = idxs;
43 
44 flag_first = 1;
45 ipro = progress2_open('OPUS2DATA', [], 0, no_files);
46 cnt_error = 0;
47 errors = {};
48 ii = 0;
49 for i = 1:no_files
50  filename = fullfile(path_, filenames{i});
51  flag_imported = 0; % whether file was imported successfully
52  lastmsg = '';
53  lastME = 0;
54  for j = 1:numel(namestotry)
55  flag_break = 0;
56  try
57  [Y, X] = ImportOpus(filename, namestotry{idxsnow(j)}); % Jake's code is currently not closing the handle if it throws an exception
58 
59  fclose('all');
60 
61  if isempty(Y)
62  lastmsg = 'Although the file opened, the import function returned empty';
63  flag_break = 1;
64  else
65  if j > 1
66  temp = idxsnow; % Makes priority to data block that was found in one file
67  temp(j) = [];
68  idxsnow = [idxsnow(j), temp];
69  end;
70 
71  flag_break = 1;
72  flag_imported = 1;
73  end;
74  catch ME
75  fclose('all');
76  lastmsg = ME.message;
77  lastME = ME;
78  end;
79 
80  if flag_break
81  break;
82  end;
83  end;
84 
85  if ~flag_imported
86  irverbose(sprintf('Not possible to read file %s. Last error message was ''%s''', filename, lastmsg), 3);
87  if isa(lastME, 'MException')
88  irverbose(lastME.getReport());
89  end;
90  cnt_error = cnt_error+1;
91  errors{end+1} = filename;
92  else
93  if length(Y) < numel(Y)
94  irverbose(sprintf('File "%s" has more than one spectrum', filename), 3);
95  cnt_error = cnt_error+1;
96  errors{end+1} = filename;
97  else
98  flag_put = 1;
99  if flag_first
100  % Initializes dataset if first row
101 
102  nf = length(X);
103 
104  data.fea_x = X;
105  data.X = zeros(no_files, nf);
106  data.classes = zeros(no_files, 1);
107  data.obsnames = cell(no_files, 1);
108  data.groupcodes = cell(no_files, 1);
109  data.classlabels = {'Class 0'};
110  flag_first = 0;
111 
112  else
113  if length(Y) ~= nf
114  irverbose(sprintf('Wrong number of data points (%d) in file "%s" (should be %d)!', length(Y), filenames{i}, nf), 3);
115  flag_put = 0;
116  end;
117  end;
118 
119  if flag_put
120  ii = ii+1;
121  data.X(ii, :) = Y;
122  data.classes(ii) = 0;
123  data.obsnames{ii} = filenames{i};
124  data.groupcodes{ii} = groupcodes{i};
125  end;
126  end;
127  end;
128 
129  ipro = progress2_change(ipro, [], [], i);
130 end;
131 progress2_close(ipro);
132 
133 flag_error = 0;
134 if cnt_error > 0
135  irverbose(sprintf('NOTICE: only %d/%d files were successfully read. Import failed on following files:', no_files-cnt_error, no_files));
136  for i = 1:cnt_error
137  irverbose(errors{i});
138  end;
139  flag_error = cnt_error;
140 
141 
142  data.X = data.X(1:ii, :);
143  data.classes = data.classes(1:ii, :);
144  data.obsnames = data.obsnames(1:ii, :);
145  data.groupcodes = data.groupcodes(1:ii, :);
146 end;
147 
148 if flag_image
149  data.height = height;
150 end;
151 
152 
153 
function irverbose(in s, in level)
function progress2_change(in prgrss, in title, in perc, in i, in n)
function opus2data(in wild, in trimdot, in flag_image, in height)
function progress2_open(in title, in perc, in i, in n)
function irerror(in s)
Dataset class.
Definition: irdata.m:30
function resolve_dir(in wild, in trimdot, in flag_image)
function progress2_close(in prgrss)