-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_check.py
100 lines (82 loc) · 4.04 KB
/
error_check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
error_checks
------------
List of helper functions to check for errors in input or in other aspects
of the model set up
Summary
-------
Contains all methods associated with error checking.
"""
import numpy as np
import warnings
distribution_list = ['Normal','Beta','Gamma','Poisson','Binomial']
def check_training_output_values(outputs,distributions,params):
"""
Checks output values (y) match up with number of defined distributions
and values are not too small if using beta or gamma mixture models (
small values can lead to nans in training as numbers appear outside of
support since gamma and beta defined on $(0,\infty)$.
).
"""
def check_output_support(output,distribution,eps=1e-3,i=None):
"""
Check is any output values below eps if using gamma or beta. Raise
a warning that this may lead to nans in training.
"""
if np.any(output<eps) and distribution in ['Gamma','Beta']:
warnings.warn("{}% of values for output {} below {}. As using the {} distribution for this output, this may lead to nans in training.".\
format(np.mean(output<eps),i,eps,distribution)
)
if np.any((output<=0)) and distribution in ['Gamma']:
raise NameError('Can\'t have output less than or equal to zero with Gamma distribution.')
if np.any((output>1) | (output<0)) and distribution in ['Beta']:
raise NameError('Can\'t have output less than zero or greater than one for output with Beta distribution.')
if np.any((output<0)) and distribution in ['Poisson','Binomial']:
raise NameError('Can\'t have output less than zero with {} distribution.'.format(distribution))
if np.any(output>1-eps) and distribution in ['Beta']:
raise UserWarning('{}% of values for output {} between {} and {}. As using the Beta distribution for this output, this may lead to nans in training.'.\
format(np.mean(output>1-eps),i,1.-eps,1.)
)
if np.any(output > params['binomial_n']) and distribution in ['Binomial']:
raise NameError('Can\'t have output greater than binomial_n with {} distribution.'.format(distribution))
if isinstance(outputs, list):
if len(outputs) != len(distributions):
raise NameError('size of outputs does not match size of named distributions')
else:
for i,(output,distribution) in enumerate(zip(outputs,distributions)):
check_output_support(output,distribution,i=i)
else:
check_output_support(outputs,distributions[0])
def check_distribution(distribution):
"""
Check if distribution matches one on list.
"""
err = 'Output needs to be of type: {}'.format(distribution_list)
if distribution not in distribution_list:
raise NameError(err)
def check_output_distributions(output_distributions):
"""
Check if output distribution list contains only those in distribution_list
"""
err = 'Output needs to be of type: {}'.format(distribution_list)
err_list = [True if o not in distribution_list else False for o in output_distributions ]
if np.any(err_list):
raise NameError(err)
def check_output_distributions_equals_output_size(output_size,output_distributions):
"""
Check output distribution length same as output size
"""
if output_size != len(output_distributions):
raise NameError('output size needs to be same as length of output_distributions')
def check_binomial_n_defined_if_binomial(params,distributions):
"""
Check that binomial_n is defined in params if using binomial
"""
if 'Binomial' in distributions:
if isinstance(params, dict):
if 'binomial_n' not in params:
raise NameError('binomial_n needs to be defined in params')
elif not isinstance(params['binomial_n'], float):
raise NameError('binomial_n must be float')
else:
raise NameError('params needs to be defined as a dictionary')