Validations¶
The _Validator
class is used internally for validating parameters of transformation classes.
This interface is only necessary to use if you are creating your own custom transformations.
-
class
sigment.internals.validator.
_Validator
[source]¶ Performs internal validations on various input types.
-
integer
(self, item, desc)[source]¶ Validates an integer.
- Parameters
- item: int
The item to validate.
- desc: str
A description of the item being validated.
- Returns
- item: int
The original input item if valid.
- Raises
- TypeError
If the value is not an
int
.
Examples
>>> validator = _Validator() >>> n_crops = 3 >>> validator.integer(n_crops, 'n_crops (number of sections to crop)')
-
real
(self, item, desc)[source]¶ Validates a real number.
- Parameters
- item: float
The item to validate.
- desc: str
A description of the item being validated.
- Returns
- item: float
The original input item if valid.
- Raises
- TypeError
If the value cannot be converted into a
float
.
Examples
>>> validator = _Validator() >>> var = 5.0 >>> validator.real(var, 'var (variance)')
-
string
(self, item, desc)[source]¶ Validates a string.
- Parameters
- item: str
The item to validate.
- desc: str
A description of the item being validated.
- Returns
- item: str
The original input item if valid.
- Raises
- TypeError
If the value is not a
str
.
Examples
>>> validator = _Validator() >>> method = 'median' >>> method = validator.string(method, 'method (filter type)')
-
boolean
(self, item, desc)[source]¶ Validates a boolean.
- Parameters
- item: bool
The item to validate.
- desc: str
A description of the item being validated.
- Returns
- item: bool
The original input item if valid.
- Raises
- TypeError
If the value is not
True
orFalse
.
Examples
>>> validator = _Validator() >>> indep = True >>> indep = validator.boolean(indep, 'indep (whether to independently normalize channels)')
-
one_of
(self, item, desc, items)[source]¶ Validates that an item is one of some permitted values.
- Parameters
- item: Any
The item to validate.
- desc: str
A description of the item being validated.
- items: Iterable[Any]
The collection of permitted values to check against.
- Returns
- item: Any
The original input item if valid.
- Raises
- ValueError
If the value is not one of the specified permitted values.
Examples
>>> validator = _Validator() >>> method = 'median' >>> method = validator.one_of(method, 'method (filter type)', ['median', 'mean'])
-
restricted_integer
(self, item, desc, condition, expected)[source]¶ Validates an integer and checks that it satisfies some condition.
- Parameters
- item: int
The item to validate.
- desc: str
A description of the item being validated.
- condition: lambda
A condition to check the item against.
- expected: str
A description of the condition, or expected value.
- Returns
- item: int
The original input item if valid.
- Raises
- ValueError
If the
int
value does not satisfy the provided condition.
Examples
>>> validator = _Validator() >>> n_crops = 3 >>> n_crops = validator.restricted_integer( >>> n_crops, 'n_crops (number of sections to crop)', >>> lambda x: 0 <= x <= 5, 'between zero and five')
-
restricted_float
(self, item, desc, condition, expected)[source]¶ Validates a float and checks that it satisfies some condition.
- Parameters
- item: float
The item to validate.
- desc: str
A description of the item being validated.
- condition: lambda
A condition to check the item against.
- expected: str
A description of the condition, or expected value.
- Returns
- item: float
The original input item if valid.
- Raises
- ValueError
If the
float
value does not satisfy the provided condition.
Examples
>>> validator = _Validator() >>> var = 5.0 >>> validator.restricted_float(var, 'var (variance)', lambda x: x > 0, 'positive')
-
integer_value
(self, item, desc, condition, expected)[source]¶ Validates an integer value or value range and checks that it satisfies some condition.
- Parameters
- item: int
The item to validate.
- desc: str
A description of the item being validated.
- condition: lambda
A condition to check the item against.
- expected: str
A description of the condition, or expected value.
- Returns
- item: tuple(int, int)
The value range if the input item is valid.
- Raises
- TypeError
If the value is not a
int
or(int, int)
.- ValueError
If the value is a tuple with more than two elements (sanity check).
Examples
>>> validator = _Validator() >>> validator.integer_value(5, 'window_size (window size)', lambda a, b: 0 < a <= b, 'positive') >>> #=> (5, 5) >>> validator.integer_value((5, 10), 'window_size (window size)', lambda a, b: 0 < a <= b, 'positive') >>> #=> (5, 10)
-
float_value
(self, item, desc, condition, expected)[source]¶ Validates a float value or value range and checks that it satisfies some condition.
- Parameters
- item: float
The item to validate.
- desc: str
A description of the item being validated.
- condition: lambda
A condition to check the item against.
- expected: str
A description of the condition, or expected value.
- Returns
- item: tuple(float, float)
The value range if the input item is valid.
- Raises
- TypeError
If the value is not a
float
or(float, float)
.- ValueError
If the value is a tuple with more than two elements (sanity check).
Examples
>>> validator = _Validator() >>> validator.float_value(5.0, 'var (variance)', lambda a, b: 0 < a <= b, 'positive') >>> #=> (5.0, 5.0) >>> validator.float_value((5, 10), 'var (variance)', lambda a, b: 0 < a <= b, 'positive') >>> #=> (5.0, 10.0)
-
signal
(self, signal)[source]¶ Validates a WAV audio signal.
- Parameters
- signal: numpy.ndarray [shape (T,) or (1xT) for mono, (2xT) for stereo]
The input signal to validate.
- Returns
- signal: numpy.ndarray [shape (T,) for mono, (2xT) for stereo]
The original signal if it is valid.
- Raises
- TypeError
If the signal is of the wrong type.
If the signal is not a floating point
numpy.ndarray
.If the signal is a floating point
numpy.ndarray
with samples outside \([-1, 1]\).
- ValueError
If the signal is a
numpy.ndarray
with more than two dimensions.If the signal has more than two channels.
-