annotate toolboxes/MIRtoolbox1.3.2/MIRToolbox/mirexport.m @ 0:cc4b1211e677 tip

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