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
Expand Up @@ -36,47 +36,83 @@
% # For CoSMoMVPA's copyright information and license terms, #
% # see the COPYING file distributed with CoSMoMVPA. #

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





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

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

case 'is_octave'
s=environment_is_octave();

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 has_param;
if ~isfield(params2func,param)
if ~isfield(param2func,key)
error('Unsupported parameter ''%s''. Supported are: %s',...
param, cosmo_strjoin(fieldnames(params2func),', '));
key, cosmo_strjoin(fieldnames(param2func),', '));
end
f=params2func.(param);
f=param2func.(key);
s=f();
return
end

me=str2func(mfilename());
params=fieldnames(params2func);
printer=@(param) sprintf('%s: %s',param,me(param));
s_cell=cellfun(printer,params,'UniformOutput',false);
s=cosmo_strjoin(s_cell,'\n');

function param2func=get_param2func()
persistent cached_param2func;

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_()
Expand Down Expand Up @@ -127,7 +163,8 @@
s=dir2str(v,formatter);

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


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

function s=warning_helper()
function parts=warning_helper()
s=warning();
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);
s=cosmo_strjoin(s_parts, ', ');



function s=dir2str(d, formatter)
% d is the result from 'dir' or 'cosmo_dir'
Expand Down
4 changes: 4 additions & 0 deletions tests/test_wtf.m
Expand Up @@ -73,6 +73,10 @@ function test_wtf_version_number()
assertEqual(cosmo_wtf('is_matlab'),~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)
assert_contains_helper(haystack, needle, true);
Expand Down

0 comments on commit 4e1da80

Please sign in to comment.