[134] | 1 | %PR_DOWNLOAD_UCI Load UCI data and convert to PRTools format
|
---|
| 2 | %
|
---|
| 3 | % [A,B, ...] = PR_DOWNLOAD_UCI(UCIDIR,DNAMS,OPTIONS)
|
---|
| 4 | %
|
---|
| 5 | % INPUT
|
---|
| 6 | % UCIDIR Name of desired UCI repository directory
|
---|
| 7 | % DNAMS Character cell array of UCI data files names to be downloaded
|
---|
| 8 | % or some full urls.
|
---|
| 9 | % OPTIONS Structure with options for parsing, see PR_DOWNLOAD
|
---|
| 10 | % OPTIONS may also be a cell array with options, one for every
|
---|
| 11 | % data file. Common fields may be defined in an additional
|
---|
| 12 | % element of the cell array.
|
---|
| 13 | %
|
---|
| 14 | % DESCRIPTION
|
---|
| 15 | % This routine loads data sets from the <a href="http://archive.ics.uci.edu/ml/datasets/">UCI Machine Learning Repository</a>.
|
---|
| 16 | % and converts them into PRTools datasets. The downloaded files are stored
|
---|
| 17 | % as .dat-files, the PRTools datasets as .mat-files in the directory of
|
---|
| 18 | % this routine. The file names of the calling routine are used. Various
|
---|
| 19 | % annotations are stored in the user-field of the PRTools datasets.
|
---|
| 20 | %
|
---|
| 21 | % This routine also accepts an old, undocumented format.
|
---|
| 22 | %
|
---|
| 23 | % EXAMPLE
|
---|
| 24 | % opt.nheadlines = 5;
|
---|
| 25 | % [a,b] = pr_download_uci('Image+Segmentation', ...
|
---|
| 26 | % {'segmentation.data','segmentation.test'},opt);
|
---|
| 27 | %
|
---|
| 28 | % SEE ALSO <a href="http://37steps.com/prtools">PRTools Guide</a>
|
---|
| 29 |
|
---|
| 30 | % Copyright: R.P.W. Duin, r.p.w.duin@37steps.com
|
---|
| 31 |
|
---|
| 32 | function varargout = pr_download_uci(name,varargin)
|
---|
| 33 | %%
|
---|
| 34 |
|
---|
| 35 | %% handle old format
|
---|
| 36 | if nargin > 3 || (nargin == 3 && ischar(varargin{2}))
|
---|
| 37 | %if ~(nargin == 3 && (iscell(varargin{2}) || isstruct(varargin{2})))
|
---|
| 38 | varargout = cell(1,nargout);
|
---|
| 39 | [varargout{:}] = pr_download_uci_old(name,varargin{:});
|
---|
| 40 | return
|
---|
| 41 | end
|
---|
| 42 |
|
---|
| 43 | %% get inputs: data files (ucinames) and parse options
|
---|
| 44 | [ucinames,opt] = setdefaults(varargin,{[lower(name) '.data']},[]);
|
---|
| 45 | if ~iscell(ucinames)
|
---|
| 46 | ucinames = {ucinames};
|
---|
| 47 | end
|
---|
| 48 |
|
---|
| 49 | if iscell(opt)
|
---|
| 50 | if numel(opt) == numel(ucinames)+1
|
---|
| 51 | % multiple structures to combine, opt{end} is common
|
---|
| 52 | for n=1:numel(opt)-1
|
---|
| 53 | % copy the common fields into the other stuctures
|
---|
| 54 | M = [fieldnames(opt{end})' fieldnames(opt{n})'; ...
|
---|
| 55 | struct2cell(opt{end})' struct2cell(opt{n})'];
|
---|
| 56 | opt{n} = struct(M{:});
|
---|
| 57 | end
|
---|
| 58 | elseif numel(opt) ~= numel(ucinames)
|
---|
| 59 | error('Number op option-structures is wrong')
|
---|
| 60 | end
|
---|
| 61 | else
|
---|
| 62 | opt = repmat({opt},1,numel(ucinames)+1);
|
---|
| 63 | end
|
---|
| 64 | % Now opt{n} should correspond to ucinames{n}
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | %% where to store: names of data file, mat file and directory
|
---|
| 68 | comname = callername; % filenames
|
---|
| 69 | datadir = callerdir; % directory
|
---|
| 70 | if isempty(comname)
|
---|
| 71 | % call from command line
|
---|
| 72 | comname = name;
|
---|
| 73 | datadir = pwd;
|
---|
| 74 | end
|
---|
| 75 | datadir = fullfile(datadir,'data');
|
---|
| 76 | varargout = cell(1,numel(ucinames));
|
---|
| 77 | % might be too large, will be corrected
|
---|
| 78 |
|
---|
| 79 | %% if matfiles available, use them
|
---|
| 80 | [varargout{:}] = loadmatfile(comname);
|
---|
| 81 | if ~isempty(varargout{1}), return; end
|
---|
| 82 |
|
---|
| 83 |
|
---|
| 84 | %% get UCI info
|
---|
| 85 | data = parselink(name);
|
---|
| 86 | url = data.url;
|
---|
| 87 |
|
---|
| 88 | %% Handle for all data files
|
---|
[135] | 89 | anynew = false;
|
---|
[134] | 90 | for j=1:numel(ucinames)
|
---|
| 91 | uciname = ucinames{j};
|
---|
| 92 | if strcmp(uciname(1:4),'http')
|
---|
| 93 | % full url given, use it
|
---|
| 94 | data.url = uciname;
|
---|
| 95 | else
|
---|
| 96 | % construct url from UCI info
|
---|
| 97 | data.url = [url uciname];
|
---|
| 98 | end
|
---|
| 99 | if numel(ucinames) > 1
|
---|
| 100 | dataname = [comname '_' num2str(j)];
|
---|
| 101 | else
|
---|
| 102 | dataname = comname;
|
---|
| 103 | end
|
---|
| 104 | opt{j}.dsetname = dataname;
|
---|
| 105 | savemat = ~isfield(opt{j},'matfile') || opt{j}.matfile;
|
---|
| 106 | opt{j}.matfile = false;
|
---|
| 107 | a = pr_download(data.url,fullfile(datadir,dataname),opt{j});
|
---|
| 108 | a = setuser(a,data,'user'); % store dataset info
|
---|
| 109 | a = setname(a,dataname); % set dataset name
|
---|
| 110 | if ~isfield(opt{j},'labfeat') || isempty(opt{j}.labfeat)
|
---|
| 111 | a = feat2lab(a,size(a,2));
|
---|
| 112 | end
|
---|
| 113 | if savemat
|
---|
| 114 | save(fullfile(datadir,dataname),'a');
|
---|
| 115 | end
|
---|
| 116 | varargout{j} = a;
|
---|
| 117 | end
|
---|
| 118 |
|
---|
| 119 | %% combine them
|
---|
| 120 | if numel(ucinames) > 1
|
---|
| 121 | % multiple datasets loaded, alignment might be needed
|
---|
| 122 | [varargout{:}] = dset_align(varargout{:});
|
---|
| 123 | a = vertcat(varargout{:});
|
---|
| 124 | a = setuser(a,data,'user'); % store dataset info
|
---|
| 125 | a = setname(a,comname); % set dataset name
|
---|
| 126 | if ~isfield(opt{end},'matfile') || opt{end}.matfile
|
---|
| 127 | save(fullfile(datadir,comname),'a');
|
---|
| 128 | end
|
---|
| 129 | if nargout == 1 % just combined set is requested
|
---|
| 130 | varargout{1} = a;
|
---|
| 131 | end
|
---|
| 132 | end
|
---|
| 133 |
|
---|
| 134 | function varargout = pr_download_uci_old(name,varargin)
|
---|
| 135 | %% take care of old definition
|
---|
| 136 | [ucinames,form,prname,siz,nhead,misvalchar,delchar,cfeat,nosave] = ...
|
---|
| 137 | setdefaults(varargin,{[lower(name) '.data']},[],[],[],[],'?',',',[],false);
|
---|
| 138 | nhead = setdefaults({nhead},zeros(1,numel(ucinames))); % headerlines to be skipped
|
---|
| 139 |
|
---|
| 140 | if ~iscell(ucinames)
|
---|
| 141 | ucinames = {ucinames};
|
---|
| 142 | end
|
---|
| 143 | if isempty(cfeat)
|
---|
| 144 | cfeat = repmat({[]},1,numel(ucinames));
|
---|
| 145 | end
|
---|
| 146 | if ~iscell(cfeat)
|
---|
| 147 | cfeat = repmat({cfeat},1,numel(ucinames));
|
---|
| 148 | end
|
---|
| 149 |
|
---|
| 150 | prname = setdefaults({prname},callername(1));
|
---|
| 151 | prname = setdefaults({prname},lower(name));
|
---|
| 152 | thisdir = fileparts(which(mfilename));
|
---|
| 153 |
|
---|
| 154 | if isempty(siz)
|
---|
| 155 | % no sizes given, make all 0
|
---|
| 156 | siz = zeros(1,numel(ucinames));
|
---|
| 157 | end
|
---|
| 158 |
|
---|
| 159 | varargout = cell(1,numel(ucinames));
|
---|
| 160 | anynew = false;
|
---|
| 161 | filenames = cell(1,numel(ucinames));
|
---|
| 162 | for j=1:numel(ucinames)
|
---|
| 163 | uciname = ucinames{j};
|
---|
| 164 | if numel(ucinames) > 1
|
---|
| 165 | dataname = [prname '_' num2str(j)];
|
---|
| 166 | else
|
---|
| 167 | dataname = prname;
|
---|
| 168 | end
|
---|
| 169 | filenames{j} = fullfile(thisdir,dataname);
|
---|
| 170 | if exist([filenames{j} '.mat'],'file') == 2
|
---|
| 171 | % if mat-file is available, use it
|
---|
| 172 | s = load([filenames{j} '.mat']);
|
---|
| 173 | f = fieldnames(s);
|
---|
| 174 | a = getfield(s,f{1});
|
---|
| 175 | else
|
---|
| 176 | if ~exist('data')
|
---|
| 177 | % get UCI info
|
---|
| 178 | data = parselink(name);
|
---|
| 179 | if ~data.misval % avoid checking missing values if not needed
|
---|
| 180 | misvalchar = [];
|
---|
| 181 | end
|
---|
| 182 | url = data.url;
|
---|
| 183 | end
|
---|
| 184 | if strcmp(uciname(1:4),'http')
|
---|
| 185 | % full url given, use it
|
---|
| 186 | data.url = uciname;
|
---|
| 187 | else
|
---|
| 188 | % construct url from UCI info
|
---|
| 189 | data.url = [url uciname];
|
---|
| 190 | end
|
---|
| 191 | % do the real work
|
---|
| 192 | a = pr_download(data.url,filenames{j},siz(j),nhead(j),form,misvalchar,delchar,true);
|
---|
| 193 | if ischar(cfeat{j})
|
---|
| 194 | labfile = [filenames{j} '_lab'];
|
---|
| 195 | % old call to pr_download
|
---|
| 196 | labs = +pr_download([url cfeat{j}],labfile,0,[],[],[],[],true);
|
---|
| 197 | if isempty(labs) || size(labs,1) ~= size(a,1)
|
---|
| 198 | warning(['No correct label file found: ' [url cfeat{j}]]);
|
---|
| 199 | else
|
---|
| 200 | delete([labfile '.dat']);
|
---|
| 201 | a = setlabels(a,+labs);
|
---|
| 202 | end
|
---|
| 203 | else
|
---|
| 204 | if isempty(cfeat{j}) % find labels and use them
|
---|
| 205 | a = feat2lab(a,size(a,2));
|
---|
| 206 | elseif cfeat{j} ~= 0
|
---|
| 207 | a = feat2lab(a,cfeat{j});
|
---|
| 208 | end
|
---|
| 209 | end
|
---|
| 210 | a = setuser(a,data,'user'); % store dataset info
|
---|
| 211 | a = setname(a,dataname); % set dataset name
|
---|
| 212 | save([filenames{j} '.mat'],'a'); % save it
|
---|
| 213 | anynew = true;
|
---|
| 214 | end
|
---|
| 215 | varargout{j} = a;
|
---|
| 216 | end
|
---|
| 217 |
|
---|
| 218 | if anynew && numel(ucinames) > 1
|
---|
| 219 | % multiple datasets loaded, alignment might be needed
|
---|
| 220 | [varargout{:}] = dset_align(varargout{:});
|
---|
| 221 | for j=1:numel(ucinames)
|
---|
| 222 | a = varargout{j};
|
---|
| 223 | if ~nosave
|
---|
| 224 | save(filenames{j},'a');
|
---|
| 225 | end
|
---|
| 226 | end
|
---|
| 227 | end
|
---|
| 228 |
|
---|
| 229 | function data = parselink(link)
|
---|
| 230 | %% Parse info from a particular UCI ML data set
|
---|
| 231 | % data.link : url of the particular data set pages
|
---|
| 232 | % data.info : url of the data set info page
|
---|
| 233 | % data.url : url of the data set data files (excluding the filename, as
|
---|
| 234 | % there might be more files and their names ar irregular)
|
---|
| 235 | % data.desc : the problem infor as given in the abstract;
|
---|
| 236 | % data.misval : true/false for missing values
|
---|
| 237 | % data.type : feature types (categorical / integer / real)
|
---|
| 238 |
|
---|
| 239 | link = ['http://archive.ics.uci.edu/ml/datasets/' link];
|
---|
| 240 | desc = urlread(link);
|
---|
| 241 | k = strfind(desc,'Download');
|
---|
| 242 | s = desc(k:k+250);
|
---|
| 243 | k = strfind(s,'"');
|
---|
| 244 | url = ['http://archive.ics.uci.edu/ml/' s(k(1)+4:k(2)-1)];
|
---|
| 245 | info = ['http://archive.ics.uci.edu/ml/' s(k(5)+3:k(6)-1)];
|
---|
| 246 |
|
---|
| 247 | k1 = strfind(desc,'Abstract</b>');
|
---|
| 248 | if ~isempty(k1)
|
---|
| 249 | k2 = strfind(desc(k1+12:end),'</p>');
|
---|
| 250 | descr = desc(k1+14:k1+k2+10);
|
---|
| 251 | end
|
---|
| 252 |
|
---|
| 253 | k = strfind(desc,'Attribute Characteristics:</b></p></td>');
|
---|
| 254 | type = desc(k+64:k+150);
|
---|
| 255 | k = strfind(type,'</p');
|
---|
| 256 | type = textscan(type(1:k-1),'%s','delimiter',',');
|
---|
| 257 | type = char(type{1});
|
---|
| 258 |
|
---|
| 259 | k1 = strfind(desc,'Missing Values');
|
---|
| 260 | if strcmp(desc(k1+53:k1+54),'No')
|
---|
| 261 | misval = false;
|
---|
| 262 | elseif strcmp(desc(k1+53:k1+55),'Yes')
|
---|
| 263 | misval = true;
|
---|
| 264 | else
|
---|
| 265 | misval = [];
|
---|
| 266 | end
|
---|
| 267 |
|
---|
| 268 | data.link = link;
|
---|
| 269 | data.info = info;
|
---|
| 270 | data.url = url;
|
---|
| 271 | data.desc = descr;
|
---|
| 272 | data.misval = misval;
|
---|
| 273 | data.type = type;
|
---|
| 274 |
|
---|
| 275 |
|
---|
| 276 | function name = callername(n)
|
---|
| 277 | %%
|
---|
| 278 | if nargin < 1, n=0; end
|
---|
| 279 | [ss,dummy] = dbstack;
|
---|
| 280 | if length(ss) < n+3
|
---|
| 281 | name = [];
|
---|
| 282 | else
|
---|
| 283 | name = ss(n+3).name;
|
---|
| 284 | end
|
---|
| 285 |
|
---|
| 286 | function dirname = callerdir(n)
|
---|
| 287 | %%
|
---|
| 288 | if nargin < 1, n=0; end
|
---|
| 289 | ss = dbstack;
|
---|
| 290 | if length(ss) < 3
|
---|
| 291 | % no caller, commandline call
|
---|
| 292 | dirname = pwd;
|
---|
| 293 | else
|
---|
| 294 | dirname = fileparts(which(ss(n+3).name));
|
---|
| 295 | end
|
---|