Skip to content

Commit

Permalink
Updated for alpha release 2
Browse files Browse the repository at this point in the history
  • Loading branch information
JonKing93 committed Sep 12, 2020
1 parent f5585ca commit 12fc8b1
Show file tree
Hide file tree
Showing 90 changed files with 3,919 additions and 262 deletions.
46 changes: 28 additions & 18 deletions @dash/assertPositiveIntegers.m
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
function[] = assertPositiveIntegers(input, allowNaN, allowInf, name)
function[] = assertPositiveIntegers(input, name, allowNaN, allowInf)
%% Checks that an input consists of positive integers. Optionally allows
% NaN and Inf values. Returns customized error messages.
%
% dash.assertPositiveIntegers(input, allowNaN, allowInf, name)
% dash.assertPositiveIntegers(input, name)
% Checks the input consists of positive integers. Does not allow NaN or Inf
%
% dash.assertPositiveIntegers(input, name, allowNaN, allowInf)
% Specify whether to allow NaN or Inf.
%
% ----- Inputs -----
%
% input: The input being checked
%
% allowNaN: A scalar logical. Whether to allow NaN values in the input.
% name: The name of the input. A string. Used for custom error messages.
%
% allowInf: A scalar logical. Whether to allow Inf values in the input.
% allowNaN: A scalar logical that indicates whether to allow NaN values in
% the input (true) or not (false -- default).
%
% name: The name of the input. Used for custom error messages.
% allowInf: A scalar logical that indicates whether to allow Inf values in
% the input (true) or not (false -- default).

% Process NaNs
if allowNaN
input(isnan(input)) = 1;
elseif any(isnan(input),'all')
error('%s may not contain NaN.', name);
% Defaults
if ~exist('allowNaN','var') || isempty(allowNaN)
allowNaN = false;
end
if ~exist('allowInf','var') || isempty(allowInf)
allowInf = false;
end

% Process Inf
if allowInf
input(isinf(input)) = 1;
elseif any(isinf(input),'all')
error('%s may not contain Inf.', name);
% Require numeric
if ~isnumeric(input)
error('%s must be numeric', name);
end

% Everything else
if ~isnumeric(input) || ~isreal(input) || any(input<1,'all') || any(mod(input,1)~=0,'all')
error('%s can only contain positive integers.', name);
% Process NaN and Inf
dash.assertRealDefined(input, name, allowNaN, allowInf);
input(isnan(input)) = 1;
input(isinf(input)) = 1;

% Check for positive integers
if any(input<1,'all') || any(mod(input,1)~=0,'all')
error('%s must only contain positive integers.', name);
end

end
46 changes: 46 additions & 0 deletions @dash/assertRealDefined.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function[] = assertRealDefined(input, name, allowNaN, allowInf, allowComplex)
%% Checks that an input is real, not NaN, and not Inf. Optionally allows
% NaN, Inf, or complex. Returns custom error messages.
%
% dash.assertRealDefined(input, name)
% Checks that an input is real, not NaN and not Inf.
%
% dash.assertRealDefined(input, name, allowNaN, allowInf, allowComplex)
% Optionally allow NaN, Inf, or complex.
%
% ----- Inputs -----
%
% input: The input being checked
%
% name: The name of the input. A string. Used for custom error messages.
%
% allowNaN: Scalar logical indicating whether to allow NaN (true) or not
% (false -- default)
%
% allowInf: Scalar logical indicating whether to allow Inf (true) or not
% (false -- default)
%
% allowComplex: Scalar logical indicating whether to allow complex values
% (true) or not (false -- default)

% Defaults
if ~exist('allowNaN','var') || isempty(allowNaN)
allowNaN = false;
end
if ~exist('allowInf','var') || isempty(allowInf)
allowInf = false;
end
if ~exist('allowComplex','var') || isempty(allowComplex)
allowComplex = false;
end

% Check input
if ~allowNaN && any(isnan(input), 'all')
error('%s may not contain NaN', name);
elseif ~allowInf && any(isinf(input), 'all')
error('%s may not contain Inf', name);
elseif ~allowComplex && ~isreal(input)
error('%s may not contain complex (imaginary) values', name);
end

end
17 changes: 17 additions & 0 deletions @dash/assertScalarLogical.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function[] = assertScalarLogical(input, name)
%% Checks that an input is a scalar logical. Throws a custom error message
% if not.
%
% dash.assertScalarLogical(input, name)
%
% ----- Inputs -----
%
% input: The input being checked
%
% name: The name of the input. A string. Used for error message.

if ~isscalar(input) || ~islogical(input)
error('%s must be a scalar logical.', name);
end

end
17 changes: 12 additions & 5 deletions @dash/assertStrFlag.m
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
function[] = assertStrFlag( input, name )
function[input] = assertStrFlag( input, name )
%% Checks that an input is a string flag. Returns a customized error
% message if not.
% message if not. Optionally returns input as a string data type.
%
% dash.assertStrFlags( input, name )
% dash.assertStrFlag( input, name )
%
% input = dash.assertStrFlag(input, name)
%
% ----- Inputs -----
%
% input: A variable being checked.
%
% names: The name of the variables to use in error messages. A string.
% name: The name of the variable to use in the error message. A string.
%
% ----- Outputs -----
%
% input: The input as a string data type.

if ~dash.isstrflag(input)
error('%s must be a string scalar or character row vector.',name);
end

input = string(input);

end
21 changes: 20 additions & 1 deletion @dash/assertStrList.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
function[] = assertStrList(input, name)
function[input] = assertStrList(input, name)
%% Checks that an input is a string list. Returns a customized error
% message if not. Optionally returns input as a string data type.
%
% dash.assertStrList(input, name)
%
% input = dash.assertStrList(input, name)
%
% ----- Inputs -----
%
% input: The input being checked
%
% name: The name of a variable being check. A string.
%
% ----- Outputs -----
%
% input: The input as a string data type.

if ~dash.isstrlist(input)
error('%s must be a string vector or cellstring vector.', name);
end
input = string(input);

end
32 changes: 32 additions & 0 deletions @dash/assertVectorTypeN.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function[] = assertVectorTypeN( input, type, N, name )
%% Checks that an input is a vector with length N. Optionally also checks
% the vector is a specific data type. Returns a customized error message if not.
%
% dash.assertVectorTypeN(input, [], N, name)
% Checks the input is a vector of length N.
%
% dash.assertVectorTypeN(input, type, [], name)
% Checks the input is a vector of a specific type.
%
% dash.assertVectorTypeN(input, type, N, name)
% Checks both type and length.
%
% ----- Inputs -----
%
% input: The input being checked.
%
% type: The required data type. Use [] to not check the type.
%
% N: The required length of the vector. Use [] to not check the length.
%
% name: The name of the input. Used for custom error message.

if ~isvector(input)
error('%s must be a vector.', name);
elseif ~isempty(N) && numel(input)~=N
error('%s must have %.f elements, but it has %.f elements instead.', name, N, numel(input));
elseif ~isempty(type) && ~isa(input, type)
error('%s must be a %s vector, but it is a %s vector instead.', name, type, class(input));
end

end
5 changes: 4 additions & 1 deletion @dash/checkFileExists.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
% Throw error if the file doesn't exist
if isempty(path)
if ~exist
error('Could not find file %s. It may be misspelled or not on the active path.', file);
error("DASH:missingFile",'Could not find file %s. It may be misspelled or not on the active path.', file);
end

% Get the path string if off the active path.
Expand All @@ -42,4 +42,7 @@
rmpath(fileparts(file));
end

% Use string internally
path = string(path);

end
52 changes: 52 additions & 0 deletions @dash/checkIndices.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function[indices] = checkIndices( indices, name, length, lengthName )
%% Checks that an input is a set of indices. Indices may be a logical
% vector the length of a dimension, or a vector of linear indices. Linear
% indices may not exceed the dimension length. Returns custom error
% messages. Converts logical indices to linear indices.
%
% indices = dash.checkIndices( indices, name, length, lengthName )
%
% ----- Inputs -----
%
% indices: The indices being checked.
%
% name: The name of the indices. Used for custom error messages.
%
% length: The length of the array dimension. This is the maximum value
% for linear indices and the required length of logical indices.
%
% lengthName: The name of the length of the array dimension. A string.
%
% ----- Outputs -----
%
% indices: Linear indices

% Allow empty call
if ~isequal(indices, [])

% Vector
if ~isvector(indices)
error('%s must be a vector.',name);
end

% Logical indices
if islogical(indices)
if numel(indices)~=length
error('%s is a logical vector, but it is not %s (%.f).', name, lengthName, length);
end
indices = find(indices);

% Numeric indices
elseif isnumeric(indices)
dash.assertPositiveIntegers(indices, name);
if max(indices) > length
error('%s has elements larger than %s (%.f).', name, lengthName, length);
end

% Other types are not allowed
else
error('%s must either be logical or numeric.');
end
end

end
35 changes: 35 additions & 0 deletions @dash/checkStrsInList.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function[k] = checkStrsInList(input, list, name, listName )
%% Checks that an input is a set of strings that are all members of a list.
% Throws a custom error message if not. Returns the indices of the strings
% in the list.
%
% k = dash.checkStrsInList(input, list, name, listMessage)
%
% ----- Inputs -----
%
% input: The input being checked
%
% list: A list of allowed strings. A string vector.
%
% name: The name of the input. A string
%
% listName: Name of the list. A string

% Check the input is a string list
dash.assertStrList(input, name);
input = string(input);

% Check all strings are allowed. Get their indices in the list.
[inList, k] = ismember(input, list);
if any(~inList)
bad = find(~inList,1);

% Informative error message
badName = name;
if numel(input)>1
badName = sprintf('Element %.f in %s (%s)', bad, name, input(bad));
end
error('%s is not a %s. Allowed values are %s.', badName, listName, dash.messageList(list));
end

end
21 changes: 21 additions & 0 deletions @dash/collectField.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function[values] = collectField(s, field)
%% Collects the values in a field of a structure vector.
%
% values = dash.collectField(s, field)
%
% ----- Inputs -----
%
% s: The structure vector
%
% field: The name of the field. A string scalar or character row vector.
%
% ----- Outputs -----
%
% values: The values in the field. A cell vector with one element per
% structure in s.

nEls = numel(s);
values = cell(nEls, 1);
[values{:}] = deal(s.(field));

end
Loading

0 comments on commit 12fc8b1

Please sign in to comment.