function C = suppressWarnings(ToSuppress)
% C = suppressWarnings() suppress certain warnings we never want to see
% while running bayesopt, and return an onCleanup object that restores the
% warning state upon destruction.
%
% C = suppressWarnings(warningIDs) accepts a cell array of warning IDs to
% suppress instead of the default list.

%   Copyright 2020-2021 The MathWorks, Inc.

arguments
    ToSuppress cell = iGetDefaultWarningsToSuppress()
end

for whichID = 1:numel(ToSuppress)
  status(whichID) = warning('off', ToSuppress{whichID});
end
C = onCleanup(@() warning(status));
end

function ToSuppress = iGetDefaultWarningsToSuppress()
ToSuppress = {...
    'stats:classreg:learning:impl:GPImpl:GPImpl:OptimizerUnableToConverge',...
    'stats:classreg:learning:impl:GPImpl:GPImpl:SigmaMustBeGreaterThanSigmaLowerBound',...
    'MATLAB:contour:ConstantData',...
    'MATLAB:contour:NonFiniteData',...
    'MATLAB:nearlySingularMatrix',...
    'MATLAB:illConditionedMatrix',...
    'MATLAB:singularMatrix',...
    'MATLAB:rankDeficientMatrix',...
    };
end
