Package: paramcheckup
paramcheckupThis package has a collection of functions that check whether the parameter received has a certain type, returningTrueif the input is as expected orraising an errorthat indicates what the problem is.Installpip install paramcheckupExample 1importnumpyasnpfromscipyimportstatsAssume a functiont_test()that applies one sample Student's t test to compare means (two sided). This function receives three parameters, which arex_data,muandalpha.deft_test(x_data,mu,alpha):tcalc=(x_data.mean()-mu)*np.sqrt(x_data.size)/(x_data.std(ddof=1))t_critical=stats.t.ppf(1-alpha/2,x_data.size-1)p_value=(1-stats.t.cdf(np.abs(tcalc),x_data.size-1))*2ifp_value<alpha:conclusion="Reject H0"else:conclusion="Fail to reject H0"returntcalc,t_critical,p_value,conclusionThet_testfunction strongly depends on thex_dataparameter being a one-dimensionalNumpyArray. Thetypes.is_numpy(value, param_name, func_name)function can checks whetherx_datais in fact aNumpyArray(True):fromparamcheckupimporttypesdeft_test(x_data,mu,alpha):types.is_numpy(value=x_data,param_name="x_data",kind="function",kind_name="t_test",stacklevel=4,error=True,)tcalc=(x_data.mean()-mu)*np.sqrt(x_data.size)/(x_data.std(ddof=1))t_critical=stats.t.ppf(1-alpha/2,x_data.size-1)p_value=(1-stats.t.cdf(np.abs(tcalc),x_data.size-1))*2ifp_value<alpha:conclusion="Reject H0"else:conclusion="Fail to reject H0"returntcalc,t_critical,p_value,conclusionIf the user passes aNumpyArrayas input forx_data, the result oftypes.is_numpywill beTrueand the calculation will be performed as expected:x=np.array([1.24,1.3,1.11])result=t_test(x,3,0.05)print(result)(-31.80244895786038,4.302652729911275,0.0009872686643235262,'Reject H0')However, if you use alistinstead ofNumpyArray, anTypeErrorwill be raised indicating what the error is:x=[1.24,1.3,1.11]result=t_test(x,3,0.05)Theparameter'x_data'infunction't_test'mustbeoftype*numpy.ndarray*,butitstypeis*list*.UserWarningatline28:Theparameter`x_data`infunction`t_test`mustbeoftype`numpy.ndarray`,butitstypeis`list`.The UserWarning informs the linewherethe error occurred,which parameteris wrong andhowthis parameter should be to be correct. By default, the error traceback is also showed to the user:Traceback (most recent call last):
  File "...\main.py", line 21, in <module>
    result = t_test(x, 3, 0.05)
  File "...\main.py", line 8, in t_test
    types.is_numpy(x_data, "x_data", "t_test")
  File "...\venv\lib\site-packages\paramcheckup\types.py", line 436, in is_numpy
    raise TypeError("NotNumPyError")
TypeError: NotNumPyErrorHowever, it is possible to silence the traceback through the error parameter:types.is_numpy(value=x_data,param_name="x_data",kind="function",kind_name="t_test",stacklevel=4,error=False,# <------)Note that the function also requires the array to have a single dimension. This could be checked using theparamcheckup.numpy_arrays.n_dimensions function().Example 2Thealphaparameter indicates the level of significance that should be adopted for the test. It is a value that varies between0and1. To limit the range of values, you can use thenumbers.is_between_a_and_b()function:fromparamcheckupimporttypes,numbersdeft_test(x_data,mu,alpha):types.is_numpy(value=x_data,param_name="x_data",kind="function",kind_name="t_test",stacklevel=4,error=False,)numbers.is_between_a_and_b(number=alpha,lower=0,upper=1,param_name="alpha",kind="function",kind_name="t_test",inclusive=False,stacklevel=4,error=False,)tcalc=(x_data.mean()-mu)*np.sqrt(x_data.size)/(x_data.std(ddof=1))t_critical=stats.t.ppf(1-alpha/2,x_data.size-1)p_value=(1-stats.t.cdf(np.abs(tcalc),x_data.size-1))*2ifp_value<alpha:conclusion="Reject H0"else:conclusion="Fail to reject H0"returntcalc,t_critical,p_value,conclusionx=np.array([1.24,1.3,1.11])alpha=1.05result=t_test(x,3,alpha)UserWarningatline39:Thevalueof`alpha`infunction`t_test`mustbewithintherangeof`0<alpha<1`,butitis`1.05`.Note that theinclusive=Falseparameter causes the limits to be open, which makes sense for the significance level. Ifinclusive=True, we would have obtained the following error:UserWarningatline39:Thevalueof`alpha`infunction`t_test`mustbewithintherangeof`0<=alpha<=1`,butitis`1.05`.Note thatalphamust be of numeric type. This could be checked using functionparamcheckup.numbers.is_float_or_int()beforechecking whether the parameter is within a numerical range.LicenseBSD 3-Clause License