exceptions.py 2.26 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright 2020 The PyMC Developers
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

15 16 17 18 19
__all__ = [
    "SamplingError",
    "IncorrectArgumentsError",
    "TraceDirectoryError",
    "ImputationWarning",
20
    "ShapeError"
21
]
22 23 24 25


class SamplingError(RuntimeError):
    pass
26 27 28 29


class IncorrectArgumentsError(ValueError):
    pass
rpgoldman's avatar
rpgoldman committed
30

31

rpgoldman's avatar
rpgoldman committed
32
class TraceDirectoryError(ValueError):
33 34 35 36 37 38 39 40
    """Error from trying to load a trace from an incorrectly-structured directory,"""

    pass


class ImputationWarning(UserWarning):
    """Warning that there are missing values that will be imputed."""

rpgoldman's avatar
rpgoldman committed
41
    pass
42 43 44 45 46


class ShapeError(Exception):
    """Error that the shape of a variable is incorrect."""
    def __init__(self, message, actual=None, expected=None):
47
        if actual is not None and expected is not None:
48
            super().__init__('{} (actual {} != expected {})'.format(message, actual, expected))
49 50 51 52
        elif actual is not None and expected is None:
            super().__init__('{} (actual {})'.format(message, actual))
        elif actual is None and expected is not None:
            super().__init__('{} (expected {})'.format(message, expected))
53
        else:
54 55 56 57 58 59
            super().__init__(message)


class DtypeError(TypeError):
    """Error that the dtype of a variable is incorrect."""
    def __init__(self, message, actual=None, expected=None):
60
        if actual is not None and expected is not None:
61
            super().__init__('{} (actual {} != expected {})'.format(message, actual, expected))
62 63 64 65
        elif actual is not None and expected is None:
            super().__init__('{} (actual {})'.format(message, actual))
        elif actual is None and expected is not None:
            super().__init__('{} (expected {})'.format(message, expected))
66 67
        else:
            super().__init__(message)