Skip to content

Commit

Permalink
feat(refact): startup path handling & speed
Browse files Browse the repository at this point in the history
This increase speed and reduce the amount of
path entries in non-deployments toolbox sessions by
more than 10x, since now we ignore git folders and any
other symlink/data folder within the root path.
  • Loading branch information
ocehugo committed Aug 12, 2020
1 parent 98497d1 commit 6bd84d1
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 8 deletions.
93 changes: 93 additions & 0 deletions Util/CellUtils/inCellPartialMatchString.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
function [is_complete, indexes] = inCellPartialMatchString(xcell,ycell,first_match_only)
% function [indexes,is_complete] = inCellPartialMatchString(xcell, ycell,first_match_only)
%
% Check if each string entry in ycell is within any string entry in xcell.
%
% If all the content of a ycell{n} string
% is found within the string of xcell{m}
% the m index is stored.
%
% For simple partial string matching, use the contains function
%
% See examples for usage.
%
% Inputs:
%
% xcell - a cell with items as strings.
% ycell - a string or another cell with items as strings.
% first_match_only - a boolean to skip subsequent matches of ycell in xcell.
% - Default: false
%
% Output:
%
% is_complete - a boolean to indicate that all ycell items
% were matched.
% indexes - a cell of indexes where matches occur in xcell.
%
%
% Example:
% % complete match
% xnames = {'a','ab','abc','123'};
% ynames = {'a','3'};
% [is_complete, indexes] = inCellPartialMatchString(xnames,ynames);
% assert(is_complete)
% assert(isequal({1,2,3,4},indexes))
% [is_complete, indexes] = inCellPartialMatchString(xnames,ynames,true);
% assert(is_complete)
% assert(isequal({1,4},indexes))
%
% % incomplete match
% xnames = {'a','b','c','123'};
% ynames = {'x','y','z','3'};
% [is_complete, indexes] = inCellPartialMatchString(xnames,ynames);
% assert(~is_complete)
% assert(isequal({4},indexes))
%
% % no match
% xnames = {'x','y','z'};
% ynames = {'a','b','c'};
% [is_complete, indexes] = inCellPartialMatchString(xnames,ynames);
% assert(~is_complete)
% assert(isequal({},indexes))

%
% author: hugo.oliveira@utas.edu.au
%
narginchk(2, 3)
if nargin<3
first_match_only = false;
end

if ~iscell(ycell)
ycell = {ycell};
end

indexes = cell(1,numel(ycell)*numel(xcell));
ydetection = zeros(1,numel(ycell));

c = 0;
for k = 1:numel(ycell)
if ~ischar(ycell{k})
error('The second argument index `ycell{%d}` is not a string', k);
end
for kk = 1:length(xcell)
if ~ischar(xcell{kk})
error('The first argument index `xcell{%d}` is not a string', k);
end
if contains(xcell{kk},ycell{k})
c = c + 1;
indexes{c} = kk;
ydetection(k) = true;
if first_match_only
break
end
end
end
end

indexes = indexes(1:c);
if isempty(indexes)
indexes = {};
end
is_complete = all(ydetection);
end
25 changes: 17 additions & 8 deletions imosToolbox.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,25 @@ function imosToolbox(auto, varargin)
path = '';
if ~isdeployed
[path, ~, ~] = fileparts(which('imosToolbox.m'));

% set Matlab path for this session (add all recursive directories to Matlab
% path)
searchPath = textscan(genpath(path), '%s', 'Delimiter', pathsep);
searchPath = searchPath{1};
iPathToRemove = ~cellfun(@isempty, strfind(searchPath, [filesep '.']));
searchPath(iPathToRemove) = [];
searchPath = cellfun(@(x)([x pathsep]), searchPath, 'UniformOutput', false);
searchPath = [searchPath{:}];
addpath(searchPath);
addpath(fullfile(path,'Util/Path'));
addpath(fullfile(path,'Util/CellUtils'));
addpath(fullfile(path,'Util/Schema'));

[~,subfolders] = FilesInFolder(path);
ignored_subfolders = {'.git','.mypy_cache','imos-toolbox/snapshot','imos-toolbox/data','imos-toolbox/dist'};
[~,ignore_indexes] = inCellPartialMatchString(subfolders,ignored_subfolders);
valid_subfolders = popFromCell(subfolders,subfolders([ignore_indexes{:}]));

rmpath(fullfile(path,'Util/Path'));
rmpath(fullfile(path,'Util/CellUtils'));
rmpath(fullfile(path,'Util/Schema'));

cell_of_folders_strings = cellfun(@genpath,valid_subfolders,'UniformOutput',false);
all_folders_as_string= cat(2,cell_of_folders_strings{:});
addpath(path);
addpath(all_folders_as_string);
end
if isempty(path), path = pwd; end

Expand Down

0 comments on commit 6bd84d1

Please sign in to comment.