IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
dataio_txt_basic.m
Go to the documentation of this file.
1 %> @brief Basic TXT loader/saver
2 %>
3 %> Description of the "basic" file type (Figure 1):
4 %> <ul>
5 %> <li>Table of data with no header and last column containing the class index.</li>
6 %> <li>Delimiters such as ';', ' ', or '\t' (Tab) are supported.</li>
7 %> </ul>
8 %>
9 %> Because class labels and x-axis are not stored in the file,
10 %> <ul>
11 %> <li>Class labels are made up as "Class 0", "Class 1" etc</li>
12 %> <li>Unless the x-axis range is passed to load(), the default value [1801.47, 898.81] (Bruker Tensor27-aquired spectra, then cut to
13 %> 1800-900 region in OPUS)</li>
14 %>
15 %> @image html dataformat_basic.png
16 %> <center>Figure 1 - basic TXT file type</center>
17 %> </ul>
19  properties
20  %> 2-element vector specifying the wavenumber range.
21  %> This is necessary because this file format does not have this information.
22  %> However, the parameter is optional (the default data fea_x will be
23  %> [1, 2, 3, 4, ...])
24  range = [];
25 
26  %> (optional) Image height. If specified, will assign it the loaded dataset
27  height = [];
28  %> ='hor'. Whether the pixels are taken horizontally ('hor') or vertically ('ver') to form the image.
29  %> It was found that OPUS numbers the point in the image map left-right, bottom-up, hence 'hor'.
30  %> Same as irdata.direction (although irdata.direction has a different default).
31  direction = 'hor';
32  end;
33 
34  methods
35  function o = dataio_txt_basic()
36  o.flag_xaxis = 0;
37  o.flag_params = 1;
38  end;
39 
40 
41 
42  %> Loader
43  function data = load(o)
44  [no_cols, delimiter] = get_no_cols_deli(o.filename);
45 
46  data = irdata();
47 
48  if isempty(o.range)
49  data.fea_x = 1:no_cols-1;
50  else
51  data.fea_x = linspace(o.range(1), o.range(2), no_cols-1);
52  end;
53 
54 
55 
56  % Mounts mask
57  mask_data = [repmat('%f', 1, no_cols-1) '%q'];
58 
59  % Opens for the second time in order to actually read the file
60  fid = fopen(o.filename);
61 
62  c_data = textscan(fid, mask_data, 'Delimiter', delimiter);
63  fclose(fid);
64 
65  % Resolves easy one
66  data.X = cell2mat(c_data(1:end-1));
67 
68  % Resolves classes
69  clalpha = c_data{end};
70  if ~iscell(clalpha)
71  clalpha = cellfun(@num2str, num2cell(clalpha), 'UniformOutput', 0);
72  end;
73  data.classlabels = unique_appear(clalpha');
74 
75  no_obs = size(data.X, 1);
76  data.classes(no_obs, 1) = -1; % pre-allocates
77  for i = 1:no_obs
78  data.classes(i) = find(strcmp(data.classlabels, clalpha{i}))-1;
79  end;
80 
81 
82  data.assert_not_nan();
83  data.filename = o.filename;
84  data.filetype = 'txt';
85  if ~isempty(o.height)
86  data.height = o.height;
87  data.direction = o.direction;
88  end;
89  end;
90 
91 
92 
93 
94 
95 
96 
97  %> Saver
98  function o = save(o, data)
99 
100  h = fopen(o.filename, 'w');
101  if h < 1
102  irerror(sprintf('Could not create file ''%s''!', o.filename));
103  end;
104 
105  labels = classes2labels(data.classes, data.classlabels);
106 
107  for i = 1:data.no
108  fwrite(h, [sprintf('%g\t', data.X(i, :)) labels{i} sprintf('\n')]);
109  end;
110 
111  fclose(h);
112 
113  irverbose(sprintf('Just saved file "%s"', o.filename), 2);
114  end;
115  end
116 end
function irverbose(in s, in level)
function irerror(in s)
Dataset class.
Definition: irdata.m:30
Pre-processing block base class.
Definition: pre.m:2
function classes2labels(in classes, in labels)
Basic TXT loader/saver.
Dataset loader/saver common class.
Definition: dataio.m:2
Analysis Session (AS) base class.
Definition: as.m:6
function unique_appear(in classlabels)
function get_no_cols_deli(in filename)