Skip to content

Commit

Permalink
ENH+RF: cleanup of wtf
Browse files Browse the repository at this point in the history
  • Loading branch information
nno committed Sep 16, 2017
1 parent e32ca19 commit 4e1da80
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 38 deletions.
110 changes: 72 additions & 38 deletions mvpa/cosmo_wtf.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,47 +36,83 @@
% # For CoSMoMVPA's copyright information and license terms, # % # For CoSMoMVPA's copyright information and license terms, #
% # see the COPYING file distributed with CoSMoMVPA. # % # see the COPYING file distributed with CoSMoMVPA. #


has_param=nargin>=1 && ~isempty(param); has_param=nargin>=1 && ~isempty(param);
if has_param if has_param
if strcmp(param,'is_matlab') s=get_single_param_value(param);
s=environment_is_matlab(); else
return s=get_all_param_values();
elseif strcmp(param,'is_octave') end
s=environment_is_octave();
return



function s=get_all_param_values()
param2func=get_param2func();
keys=fieldnames(param2func);
value_printer=@(key)as_string(helper_get_param_from_func_value(key));
printer=@(key) sprintf('%s: %s',...
key,value_printer(key));
s_cell=cellfun(printer,keys,'UniformOutput',false);
s=cosmo_strjoin(s_cell,'\n');

function s=as_string(v)
if ischar(v)
s=v;
elseif isnumeric(v)
s=sprintf('[ %s]',sprintf('%d ',v));
elseif iscellstr(v)
s=sprintf('\n %s',v{:});
else
assert(false,'unsupported type');
end

function s=get_single_param_value(param)
if ~ischar(param)
error('argument must be a string');
end end
end


params2func=struct(); switch param
params2func.computer=@computer_; case 'is_matlab'
params2func.environment=@environment; s=environment_is_matlab();
params2func.version=version_;
params2func.java=java_; case 'is_octave'
params2func.cosmo_externals=@cosmo_externals; s=environment_is_octave();
params2func.toolboxes=@toolboxes;
params2func.warnings=@warning_helper;
params2func.cosmo_config=@cosmo_config_helper;
params2func.version_number=@version_number_;
params2func.cosmo_files=@cosmo_files;


otherwise
s=helper_get_param_from_func_value(param);
end


function s=helper_get_param_from_func_value(key)
param2func=get_param2func();


has_param=nargin>=1 && ~isempty(param); if ~isfield(param2func,key)
if has_param;
if ~isfield(params2func,param)
error('Unsupported parameter ''%s''. Supported are: %s',... error('Unsupported parameter ''%s''. Supported are: %s',...
param, cosmo_strjoin(fieldnames(params2func),', ')); key, cosmo_strjoin(fieldnames(param2func),', '));
end end
f=params2func.(param); f=param2func.(key);
s=f(); s=f();
return
end


me=str2func(mfilename());
params=fieldnames(params2func); function param2func=get_param2func()
printer=@(param) sprintf('%s: %s',param,me(param)); persistent cached_param2func;
s_cell=cellfun(printer,params,'UniformOutput',false);
s=cosmo_strjoin(s_cell,'\n'); if ~isstruct(cached_param2func)
cached_param2func=struct();
cached_param2func.computer=@computer_;
cached_param2func.environment=@environment;
cached_param2func.version=version_;
cached_param2func.java=java_;
cached_param2func.cosmo_externals=@cosmo_externals;
cached_param2func.toolboxes=@toolboxes;
cached_param2func.warnings=@warning_helper;
cached_param2func.cosmo_config=@cosmo_config_helper;
cached_param2func.version_number=@version_number_;
cached_param2func.cosmo_files=@cosmo_files;
end

param2func=cached_param2func;





function s=computer_() function s=computer_()
Expand Down Expand Up @@ -127,7 +163,8 @@
s=dir2str(v,formatter); s=dir2str(v,formatter);


function s=cosmo_externals() function s=cosmo_externals()
s=cosmo_strjoin(cosmo_check_external('-list'),', '); s=cosmo_check_external('-list');



function s=cosmo_files() function s=cosmo_files()
pth=fileparts(which(mfilename())); % cosmo root directory pth=fileparts(which(mfilename())); % cosmo root directory
Expand All @@ -151,15 +188,12 @@
s=caught_error.message; s=caught_error.message;
end end


function s=warning_helper() function parts=warning_helper()
s=warning(); s=warning();
n=numel(s); n=numel(s);


s_parts=arrayfun(@(i)sprintf('%s: %s', s(i).identifier, s(i).state),... parts=arrayfun(@(i)sprintf('%s: %s', s(i).identifier, s(i).state),...
1:n,'UniformOutput',false); 1:n,'UniformOutput',false);
s=cosmo_strjoin(s_parts, ', ');




function s=dir2str(d, formatter) function s=dir2str(d, formatter)
% d is the result from 'dir' or 'cosmo_dir' % d is the result from 'dir' or 'cosmo_dir'
Expand Down
4 changes: 4 additions & 0 deletions tests/test_wtf.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ function test_wtf_version_number()
assertEqual(cosmo_wtf('is_matlab'),~is_octave); assertEqual(cosmo_wtf('is_matlab'),~is_octave);
assertEqual(cosmo_wtf('is_octave'),is_octave); assertEqual(cosmo_wtf('is_octave'),is_octave);


function test_wtf_cosmo_externals()
s=cosmo_wtf('cosmo_externals');
assert(iscellstr(s));



function assert_contains(haystack, needle) function assert_contains(haystack, needle)
assert_contains_helper(haystack, needle, true); assert_contains_helper(haystack, needle, true);
Expand Down

0 comments on commit 4e1da80

Please sign in to comment.