IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
pir2data.m
Go to the documentation of this file.
1 %>@ingroup ioio
2 %>@file
3 %> @brief Merges several "Pirouette .dat" 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] = pir2data(wild, trimdot, flag_image, height)
21 
22 if ~exist('trimdot', 'var')
23  trimdot = 2;
24 end;
25 
26 [filenames, groupcodes] = resolve_dir(wild, trimdot, flag_image);
27 
28 no_files = length(filenames);
29 
30 if flag_image
31  if no_files/height ~= floor(no_files/height)
32  irerror('Pir2Data: Invalid image height!');
33  end;
34 end;
35 
36 
37 idxs = find(wild == '/');
38 if isempty(idxs)
39  path_ = '';
40 else
41  path_ = wild(1:idxs(end));
42 end;
43 data = irdata();
44 
45 flag_first = 1;
46 ipro = progress2_open('PIR2DATA', [], 0, no_files);
47 cnt_error = 0;
48 errors = {};
49 ii = 0;
50 for i = 1:no_files
51  filename = fullfile(path_, filenames{i});
52  flag_open = 0;
53  try
54  h = fopen(filename, 'r');
55  if h < 0
56  irerror(sprintf('Pir2Data: Could not open file ''%s''!', filename));
57  end;
58  flag_open = 1;
59 
60  fprintf('%d/%d: %s ...\n', i, no_files, filenames{i});
61 
62  flag_wants_wns = 1;
63  flag_wants_point = 0;
64 
65  x = [];
66  cnt_point = 0;
67  while 1
68  s = fgets(h);
69  if isnumeric(s) && s == -1 % EOF
70  break;
71  end;
72 
73  flag_wns = 0;
74  flag_point = 0;
75 
76  if s(1) == '#'
77  if s(2) == 'c'
78  flag_wns = 1;
79  end;
80  else
81  flag_point = 1;
82  end;
83 
84  if flag_wns && ~flag_wants_wns
85  irerror(sprintf('Pir2Data: Found wavenumbers specification but expecting something else in file ''%s''!', filenames{i}));
86  end;
87  if flag_point && ~flag_wants_point
88  irerror(sprintf('Pir2Data: Found y-value but expecting something else in file ''%s''!', filenames{i}));
89  end;
90 
91  if flag_wns
92  if flag_first
93  wns = eval(['[' s(4:end) ']']);
94  wns_global = wns;
95  nf = length(wns_global);
96  end;
97 
98  flag_wants_point = 1;
99  flag_wants_wns = 0;
100  elseif flag_point
101  cnt_point = cnt_point+1;
102  x(cnt_point) = eval(s);
103  end;
104  end;
105  fclose(h);
106  flag_open = 0;
107 
108  if flag_wants_wns
109  irerror('Pir2Data: Wavenumbers specification not found in file ''%s''!', filenames{i});
110  end;
111 
112  if nf == cnt_point
113  if flag_first
114  % Initializes dataset if first row
115  data.X = zeros(no_files, nf);
116  data.classes = zeros(no_files, 1);
117  data.obsnames = cell(no_files, 1);
118  data.groupcodes = cell(no_files, 1);
119  data.fea_x = wns;
120  data.classlabels = {'Class 0'};
121  flag_first = 0;
122  end;
123 
124  ii = ii+1;
125  data.X(ii, :) = x;
126  data.classes(ii) = 0;
127  data.obsnames{ii} = filenames{i};
128  data.groupcodes{ii} = groupcodes{i};
129  else
130  irerror('Pir2Data: Wrong number of data points in file ''%s''!', filenames{i});
131  end;
132  catch ME
133  if flag_open
134  fclose(h);
135  end;
136 
137  %irverbose(['ERROR: ', ME.message]);
138  irverbose(ME.getReport());
139  cnt_error = cnt_error+1;
140  errors{end+1} = filename;
141  end;
142  ipro = progress2_change(ipro, [], [], i);
143 end;
144 progress2_close(ipro);
145 
146 flag_error = 0;
147 if cnt_error > 0
148  irverbose(sprintf('NOTICE: only %d/%d files were successfully read. Import failed on following files:', no_files-cnt_error, no_files));
149  for i = 1:cnt_error
150  irverbose(errors{i});
151  end;
152  flag_error = cnt_error;
153 
154  data.X = data.X(1:ii, :);
155  data.classes = data.classes(1:ii, :);
156  data.obsnames = data.obsnames(1:ii, :);
157  data.groupcodes = data.groupcodes(1:ii, :);
158 end;
159 
160 
161 if flag_image
162  data.height = height;
163 end;
164 
function irverbose(in s, in level)
function progress2_change(in prgrss, in title, in perc, in i, in n)
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 pir2data(in wild, in trimdot, in flag_image, in height)
function progress2_close(in prgrss)