Skip to content

Commit

Permalink
Merge pull request #21 from bmcfee/exception-types
Browse files Browse the repository at this point in the history
custom exception types
  • Loading branch information
bmcfee committed Sep 13, 2016
2 parents c45cde1 + fe174ad commit 97a976b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
1 change: 1 addition & 0 deletions pumpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from .version import version
from .core import *
from .exceptions import *
from . import feature
from . import task
from .sampler import Sampler
10 changes: 8 additions & 2 deletions pumpp/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections import namedtuple, Iterable
import numpy as np

from .exceptions import *
__all__ = ['Tensor', 'Scope']

# This type is used for storing shape information
Expand Down Expand Up @@ -67,13 +68,18 @@ def register(self, field, shape, dtype):
dtype : type
The data type of the field
Raises
------
ParameterError
If dtype or shape are improperly specified
'''
if not isinstance(dtype, type):
raise TypeError('dtype={} must be a type'.format(dtype))
raise ParameterError('dtype={} must be a type'.format(dtype))

if not (isinstance(shape, Iterable) and
all([s is None or isinstance(s, int) for s in shape])):
raise ValueError('shape={} must be an iterable of integers'.format(shape))
raise ParameterError('shape={} must be an iterable of integers'.format(shape))

self.fields[self.scope(field)] = Tensor(tuple(shape), dtype)

Expand Down
18 changes: 18 additions & 0 deletions pumpp/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''Exception classes for pumpp'''


class PumppError(Exception):
'''The root pumpp exception class'''
pass


class DataError(PumppError):
'''Exceptions relating to data errors'''
pass


class ParameterError(PumppError):
'''Exceptions relating to function and method parameters'''
pass
9 changes: 5 additions & 4 deletions pumpp/task/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np

from .base import BaseTaskTransformer
from ..exceptions import *

__all__ = ['VectorTransformer']

Expand Down Expand Up @@ -76,13 +77,13 @@ def transform_annotation(self, ann, duration):
Raises
------
RuntimeError
DataError
If the input dimension does not match
'''
vector = np.asarray(ann.data.value.iloc[0], dtype=self.dtype)
if len(vector) != self.dimension:
raise RuntimeError('vector dimension({:0}) '
'!= self.dimension({:1})'
.format(len(vector), self.dimension))
raise DataError('vector dimension({:0}) '
'!= self.dimension({:1})'
.format(len(vector), self.dimension))

return {'vector': vector}
11 changes: 6 additions & 5 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

import pumpp

from pumpp import ParameterError

@pytest.mark.parametrize('dtype',
[int, np.int64,
pytest.mark.xfail('not a type',
raises=TypeError)])
raises=ParameterError)])
def test_scope_type(dtype):

scope = pumpp.base.Scope(None)
Expand All @@ -20,10 +21,10 @@ def test_scope_type(dtype):

@pytest.mark.parametrize('shape',
[[None], [1], [1, None],
pytest.mark.xfail(1, raises=ValueError),
pytest.mark.xfail(None, raises=ValueError),
pytest.mark.xfail(23.5, raises=ValueError),
pytest.mark.xfail('not a shape', raises=ValueError)])
pytest.mark.xfail(1, raises=ParameterError),
pytest.mark.xfail(None, raises=ParameterError),
pytest.mark.xfail(23.5, raises=ParameterError),
pytest.mark.xfail('not a shape', raises=ParameterError)])
def test_scope_badshape(shape):

scope = pumpp.base.Scope(None)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ def test_task_vector_absent(dimension, name):
@pytest.mark.parametrize('name', ['collab', 'vector'])
@pytest.mark.parametrize('target_dimension, data_dimension',
[(1, 1), (2, 2), (4, 4),
pytest.mark.xfail((2, 3), raises=RuntimeError)])
pytest.mark.xfail((2, 3), raises=pumpp.DataError)])
def test_task_vector_present(target_dimension, data_dimension, name):
var_name = '{:s}/vector'.format(name)
mask_name = '{:s}/_valid'.format(name)
Expand Down

0 comments on commit 97a976b

Please sign in to comment.