comparison toolboxes/MIRtoolbox1.3.2/MIRToolbox/mirexport.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function m = mirexport(f,varargin)
2 % mirexport(filename,...) exports statistical information related to
3 % diverse data into a text file called filename.
4 % mirexport('Workspace',...) instead directly output the statistical
5 % information in a structure array saved in the Matlab workspace.
6 % This structure contains three fields:
7 % filenames: the name of the original audio files,
8 % types: the name of the features,
9 % data: the data.
10 % The exported data should be related to the same initial audio file
11 % or the same ordered set of audio files.
12 % The data listed after the first arguments can be:
13 % - any feature computed in MIRtoolbox.
14 % What will be exported is the statistical description of the
15 % feature (using the mirstat function)
16 % - any structure array of such features.
17 % Such as the ouput of the mirstat function.
18 % - any cell array of such features.
19 % - the name of a text file.
20 % The text file is imported with the Matlab importdata command.
21 % Each line of the file should contains a fixed number of data
22 % delimited by tabulations. The first line, or 'header',
23 % indicates the name of each of these columns.
24 % The file format of the output can be either:
25 % - a text file.
26 % It follows the same text file representation as for the input
27 % text files. The first column of the matrix indicates the name
28 % of the audio files. The text file can be opened in Matlab,
29 % or in a spreadsheet program, such as Microsoft Excel, where the
30 % data matrix can be automatically reconstructed.
31 % - an attribute-relation file.
32 % It follows the ARFF standard, used in particular in the WEKA
33 % data mining environment.
34
35 stored.data = {};
36 stored.textdata = {};
37 stored.name = {};
38 narg = nargin;
39 if strcmpi(f,'Workspace')
40 format = 'Workspace';
41 elseif length(f)>4 && strcmpi(f(end-4:end),'.arff')
42 format = 'ARFF';
43 else
44 format = 'Matrix';
45 end
46 v = ver('MIRtoolbox');
47 title = ['MIRtoolbox' v.Version];
48 class = {};
49 if not(isempty(varargin)) && ischar(varargin{end}) && strcmp(varargin{end},'#add')
50 add = 1;
51 varargin(end) = [];
52 narg = narg-1;
53 else
54 add = 0;
55 end
56 for v = 2:narg
57 argv = varargin{v-1};
58 if isa(argv,'mirdesign')
59 mirerror('MIREXPORT','You can only export features that have been already evaluated (using mireval).');
60 end
61 if ischar(argv)
62 if strcmpi(argv,'Matrix')
63 format = 'Matrix';
64 elseif strcmpi(argv,'ARFF')
65 format = 'ARFF';
66 else
67 imported = importdata(argv,'\t',1);
68 imported.name = {};
69 [stored class] = integrate(stored,imported);
70 end
71 elseif isstruct(argv) && isfield(argv,'data')
72 new.data = argv.data;
73 new.textdata = argv.fields;
74 new.name = {};
75 [stored class] = integrate(stored,new);
76 else
77 new.data = argv;
78 new.textdata = '';
79 new.name = {};
80 [stored class] = integrate(stored,new);
81 end
82 end
83 switch format
84 case 'Matrix'
85 matrixformat(stored,f,title,add);
86 m = 1;
87 case 'ARFF'
88 classes = {};
89 for i = 1:length(class)
90 if isempty(strcmp(class{i},classes)) || not(max(strcmp(class{i},classes)))
91 classes{end+1} = class{i};
92 end
93 end
94 ARFFformat(stored,f,title,class,classes,add);
95 m = 1;
96 case 'Workspace'
97 m = variableformat(stored,f,title);
98 end
99
100
101
102 function [stored class] = integrate(stored,new,class)
103
104 if nargin<3
105 class = {};
106 end
107
108 % Input information
109 data = new.data;
110 textdata = new.textdata;
111 if isfield(new,'name')
112 name = new.name;
113 else
114 name = {};
115 end
116
117 % Input information after processing
118 newdata = {};
119 newtextdata = {};
120 newname = {};
121
122 if isstruct(data)
123 if isfield(data,'Class')
124 class = data.Class;
125 data = rmfield(data,'Class');
126 end
127
128 if isfield(data,'FileNames')
129 name = data.FileNames;
130 data = rmfield(data,'FileNames');
131 end
132
133 fields = fieldnames(data);
134 nfields = length(fields);
135
136 for w = 1:nfields
137 % Field information
138 field = fields{w};
139 newfield.data = data.(field);
140 if 1 %not(isnumeric(newfield.data) && all(all(isnan(newfield.data))))
141 if isempty(textdata)
142 newfield.textdata = field;
143 else
144 newfield.textdata = strcat(textdata,'_',field);
145 end
146
147 % Processing of the field
148 [n class] = integrate({},newfield,class);
149
150 % Concatenation of the results
151 newdata = {newdata{:} n.data{:}};
152 newtextdata = {newtextdata{:} n.textdata{:}};
153 newname = checkname(newname,name);
154 end
155 end
156 elseif isa(data,'mirdata')
157 newinput.data = mirstat(data);
158 if isfield(newinput.data,'FileNames')
159 newinput.data = rmfield(newinput.data,'FileNames');
160 end
161 title = get(data,'Title');
162 newinput.textdata = [textdata '_' title(find(not(isspace(title))))];
163 [n class] = integrate({},newinput,class);
164 newdata = n.data;
165 newtextdata = n.textdata;
166 newname = get(data,'Name');
167 elseif iscell(textdata)
168 % Input comes from importdata
169 nvar = size(data,2);
170 newdata = cell(1,nvar);
171 newtextdata = cell(1,nvar);
172 for i = 1:nvar
173 newdata{i} = data(:,i);
174 newtextdata{i} = textdata{i};
175 end
176 newname = {};
177 elseif iscell(data)
178 for i = 1:length(data)
179 if not(isempty(data{i}))
180 % Element information
181 newelement.data = data{i};
182 newelement.textdata = [textdata num2str(i)];
183
184 % Processing of the element
185 [n class] = integrate({},newelement,class);
186
187 % Concatenation of the results
188 newdata = {newdata{:} n.data{:}};
189 newtextdata = {newtextdata{:} n.textdata{:}};
190 newname = checkname(newname,n.name);
191 end
192 end
193 elseif size(data,4)>1
194 % Input is vector
195 for w = 1:size(data,4)
196 % Bin information
197 bin.data = data(:,:,:,w);
198 if isempty(textdata)
199 bin.textdata = num2str(w);
200 else
201 bin.textdata = strcat(textdata,'_',num2str(w));
202 end
203
204 % Processing of the bin
205 [n class] = integrate({},bin,class);
206
207 % Concatenation of the results
208 newdata = {newdata{:} n.data{:}};
209 newtextdata = {newtextdata{:} n.textdata{:}};
210 end
211 elseif size(data,3)>1
212 % Input is vector
213 for w = 1:size(data,3)
214 % Bin information
215 bin.data = data(:,:,w,:);
216 if isempty(textdata)
217 bin.textdata = num2str(w);
218 else
219 bin.textdata = strcat(textdata,'_',num2str(w));
220 end
221
222 % Processing of the bin
223 [n class] = integrate({},bin,class);
224
225 % Concatenation of the results
226 newdata = {newdata{:} n.data{:}};
227 newtextdata = {newtextdata{:} n.textdata{:}};
228 end
229 elseif size(data,1)>1 && size(data,1)<=50
230 % Input is vector
231 for w = 1:size(data,1)
232 % Bin information
233 bin.data = data(w,:,:,:);
234 if isempty(textdata)
235 bin.textdata = num2str(w);
236 else
237 bin.textdata = strcat(textdata,'_',num2str(w));
238 end
239
240 % Processing of the bin
241 [n class] = integrate({},bin,class);
242
243 % Concatenation of the results
244 newdata = {newdata{:} n.data{:}};
245 newtextdata = {newtextdata{:} n.textdata{:}};
246 end
247 else
248 if size(data,1)>1
249 data = mean(data);
250 end
251 newdata = {data};
252 newtextdata = {textdata};
253 newname = {};
254 end
255 if isempty(stored)
256 stored.data = newdata;
257 stored.textdata = newtextdata;
258 stored.name = newname;
259 else
260 stored.data = {stored.data{:} newdata{:}};
261 stored.textdata = {stored.textdata{:} newtextdata{:}};
262 if isempty(stored.name)
263 stored.name = newname;
264 else
265 stored.name = checkname(newname,stored.name);
266 end
267 end
268
269
270 function m = matrixformat(data,filename,title,add)
271 named = ~isempty(data.name);
272 if named
273 if not(add)
274 m(1,:) = {title,data.textdata{:}};
275 end
276 for i = 1:length(data.name)
277 m{i+~add,1} = data.name{i};
278 end
279 elseif not(add)
280 m(1,:) = {data.textdata{:}};
281 end
282 for i = 1:length(data.data)
283 m((1:length(data.data{i}))+~add,i+named) = num2cell(data.data{i});
284 end
285 if add
286 fid = fopen(filename,'at');
287 else
288 fid = fopen(filename,'wt');
289 end
290 for i = 1:size(m,1)
291 for j = 1:size(m,2)
292 if ischar(m{i,j})
293 fprintf(fid,'%s\t', m{i,j}(find(not(m{i,j} == ' '))));
294 else
295 if iscell(m{i,j}) % Problem with key strength pos to be solved
296 fprintf(fid,'%f\t', m{i,j}{1});
297 else
298 fprintf(fid,'%f\t', m{i,j});
299 end
300 end
301 end
302 %if i < size(m,1)
303 fprintf(fid,'\n');
304 %end
305 end
306 fclose(fid);
307 disp(['Data exported to file ',filename,'.']);
308
309
310 function ARFFformat(data,filename,title,class,classes,add)
311 if add
312 fid = fopen(filename,'at');
313 else
314 fid = fopen(filename,'wt');
315 fprintf(fid,['%% Attribution-Relation File automatically generated using ',title,'\n\n']);
316 fprintf(fid,'@RELATION %s\n\n',title);
317 for i = 1:length(data.textdata)
318 fprintf(fid,'@ATTRIBUTE %s NUMERIC\n',data.textdata{i});
319 end
320 if not(isempty(class))
321 fprintf(fid,'@ATTRIBUTE class {');
322 for i = 1:length(classes)
323 if i>1
324 fprintf(fid,',');
325 end
326 fprintf(fid,'%s',classes{i});
327 end
328 fprintf(fid,'}\n');
329 end
330 fprintf(fid,'\n@DATA\n');
331 fid2 = fopen([filename(1:end-5) '.filenames.txt'],'wt');
332 for i = 1:length(data.name)
333 fprintf(fid2,'%s\n',data.name{i});
334 end
335 fclose(fid2);
336 end
337
338 try
339 data = cell2mat(data.data(:))';
340 catch
341 error('ERROR IN MIREXPORT: Are you sure all the data to be exported relate to the same ordered list of audio files?');
342 end
343 for i = 1:size(data,1)
344 fprintf(fid,'%d ',data(i,:));
345 if not(isempty(class))
346 fprintf(fid,'%s',class{i});
347 end
348 fprintf(fid,'\n');
349 end
350 fclose(fid);
351 disp(['Data exported to file ',filename,'.']);
352
353
354 function m = variableformat(data,filename,title)
355 m.types = data.textdata;
356 m.filenames = data.name;
357 for i = 1:length(data.data)
358 m.data{i} = data.data{i};
359 end
360
361
362 function name = checkname(newname,name)
363 if not(isempty(newname)) && not(isempty(name))
364 if length(newname) == length(name)
365 for i = 1:length(name)
366 if not(strcmp(name{i},newname{i}))
367 error('ERROR IN MIREXPORT: All the input are not associated to the same audio files (or the same ordering of these files.');
368 end
369 end
370 else
371 error('ERROR IN MIREXPORT: All the input are not associated to the same audio files.');
372 end
373 elseif isempty(name)
374 name = newname;
375 end