Skip to content

Commit

Permalink
improving testing
Browse files Browse the repository at this point in the history
  • Loading branch information
tfm000 committed Dec 4, 2023
1 parent 39fb6a8 commit b5cf517
Show file tree
Hide file tree
Showing 15 changed files with 179 additions and 264 deletions.
11 changes: 0 additions & 11 deletions sklarpy/tests/copulas/test_fitted_dists.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def test_fitted_logpdf_pdf_cdf_mc_cdfs(all_mvt_data, copula_params_2d,
all_mdists_2d):
"""Testing the logpdf, pdf, cdf and mc-cdf functions of fitted copula
models."""
print("\nTesting logpdf, pdf, cdf and mc-cdf functions")
eps: float = 10 ** -5
num_generate: int = 10
cdf_num: int = 10
Expand Down Expand Up @@ -72,7 +71,6 @@ def test_fitted_copula_logpdf_pdf_cdf_mc_cdfs(all_mvt_uniform_data,
copula_params_2d, all_mdists_2d):
"""Testing the copula-logpdf, copula-pdf, copula-cdf and copula-mc-cdf
functions of fitted copula models."""
print("\nTesting copula logpdf, pdf, cdf and mc-cdf functions")
eps: float = 10 ** -5
num_generate: int = 10
cdf_num: int = 10
Expand Down Expand Up @@ -130,7 +128,6 @@ def test_fitted_copula_logpdf_pdf_cdf_mc_cdfs(all_mvt_uniform_data,

def test_fitted_rvs(all_mvt_data, copula_params_2d, all_mdists_2d):
"""Testing the rvs and copula-rvs functions of fitted copula models."""
print("\nTesting rvs and copula-rvs")
eps: float = 10 ** -5
dataset_name: str = 'mvt_mixed'
data: np.ndarray = all_mvt_data[dataset_name]
Expand Down Expand Up @@ -168,8 +165,6 @@ def test_fitted_rvs(all_mvt_data, copula_params_2d, all_mdists_2d):
def test_fitted_scalars(all_mvt_data, copula_params_2d, all_mdists_2d):
"""Testing the likelihood, loglikelihood, AIC and BIC functions of
fitted copula models."""
print("\nTesting scalars")

for dataset_name, data in all_mvt_data.items():
mdists = all_mdists_2d[dataset_name]
for name in distributions_map['all']:
Expand Down Expand Up @@ -215,8 +210,6 @@ def test_fitted_scalars(all_mvt_data, copula_params_2d, all_mdists_2d):
def test_fitted_integers(all_mvt_data, copula_params_2d, all_mdists_2d):
"""Testing the num_marginal_params, num_copula_params,
num_scalar_params and num_params functions of fitted copula models."""
print("\nTesting integers")

dataset_name: str = 'mvt_mixed'
data: np.ndarray = all_mvt_data[dataset_name]
mdists = all_mdists_2d[dataset_name]
Expand All @@ -239,8 +232,6 @@ def test_fitted_plots(all_mvt_data, copula_params_2d, copula_params_3d,
"""Testing the marginal_pairplot, pdf_plot, cdf_plot, mc_cdf_plot,
copula_pdf_plot, copula_cdf_plot and copula_mc_cdf_plot methods of
fitted copula models."""
print("\nTesting plots")

num_generate: int = 10
mc_num_generate: int = num_generate
num_points = 2
Expand Down Expand Up @@ -287,8 +278,6 @@ def test_fitted_plots(all_mvt_data, copula_params_2d, copula_params_3d,


def test_fitted_names(all_mvt_data, copula_params_2d, all_mdists_2d):
print("\nTesting name")

dataset_name: str = 'mvt_mixed'
data: np.ndarray = all_mvt_data[dataset_name]
mdists = all_mdists_2d[dataset_name]
Expand Down
3 changes: 0 additions & 3 deletions sklarpy/tests/copulas/test_marginal_fitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@

def test_init(all_mvt_data):
"""Testing whether MarginalFitter initialises without errors."""
print("\nTesting init")
for data in all_mvt_data.values():
mfitter = MarginalFitter(data)


def test_fit(all_mvt_data):
"""Testing the fit method for MarginalFitter"""
print("\nTesting fit")
for dataset_name, data in all_mvt_data.items():
# testing fit with default arguments
mfitter1: MarginalFitter = MarginalFitter(data)
Expand Down Expand Up @@ -46,7 +44,6 @@ def test_fit(all_mvt_data):

def test_pdfs_cdfs_ppfs_logpdf(all_mvt_data, all_mvt_uniform_data):
"""Testing the cdfs method for MarginalFitter"""
print("\nTesting pdfs, cdfs, ppfs and logpdfs")
for dataset_name in ('mvt_mixed', 'pd_mvt_mixed'):
data = all_mvt_data[dataset_name]
mfitter: MarginalFitter = MarginalFitter(data)
Expand Down
13 changes: 0 additions & 13 deletions sklarpy/tests/copulas/test_prefit_dists.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

def test_correct_type():
"""Testing copula distributions are all SklarPy objects."""
print("\nTesting correct type")
for name in distributions_map['all']:
copula = eval(name)
assert issubclass(type(copula), PreFitCopula), \
Expand All @@ -24,7 +23,6 @@ def test_correct_type():

def test_fit(all_mvt_data):
"""Testing we can fit copula distributions to data."""
print("\nTesting fit")
for data in all_mvt_data.values():
mfitter: MarginalFitter = MarginalFitter(data)
mfitter.fit()
Expand Down Expand Up @@ -92,7 +90,6 @@ def test_prefit_logpdf_pdf_cdf_mc_cdfs(all_mvt_data, copula_params_2d,
all_mdists_2d):
"""Testing the logpdf, pdf, cdf and mc-cdf functions of pre-fit copula
models."""
print("\nTesting logpdf, pdf, cdf and mc-cdf functions")
eps: float = 10 ** -5
num_generate: int = 10
cdf_num: int = 10
Expand Down Expand Up @@ -153,7 +150,6 @@ def test_prefit_copula_logpdf_pdf_cdf_mc_cdfs(all_mvt_uniform_data,
copula_params_2d, all_mdists_2d):
"""Testing the copula-logpdf, copula-pdf, copula-cdf and copula-mc-cdf
functions of pre-fit copula models."""
print("\nTesting copula logpdf, pdf, cdf and mc-cdf functions")
eps: float = 10 ** -5
num_generate: int = 10
cdf_num: int = 10
Expand Down Expand Up @@ -213,8 +209,6 @@ def test_prefit_copula_logpdf_pdf_cdf_mc_cdfs(all_mvt_uniform_data,

def test_prefit_rvs(all_mvt_data, copula_params_2d, all_mdists_2d):
"""Testing the rvs and copula-rvs functions of pre-fit copula models."""
print("\nTesting rvs and copula-rvs")

eps: float = 10 ** -5
dataset_name: str = 'mvt_mixed'
data: np.ndarray = all_mvt_data[dataset_name]
Expand Down Expand Up @@ -252,8 +246,6 @@ def test_prefit_rvs(all_mvt_data, copula_params_2d, all_mdists_2d):
def test_prefit_scalars(all_mvt_data, copula_params_2d, all_mdists_2d):
"""Testing the likelihood, loglikelihood, AIC and BIC functions of
pre-fit copula models."""
print("\nTesting scalars")

for dataset_name, data in all_mvt_data.items():
mdists = all_mdists_2d[dataset_name]
for name in distributions_map['all']:
Expand Down Expand Up @@ -294,8 +286,6 @@ def test_prefit_scalars(all_mvt_data, copula_params_2d, all_mdists_2d):
def test_prefit_integers(all_mvt_data, copula_params_2d, all_mdists_2d):
"""Testing the num_marginal_params, num_copula_params,
num_scalar_params and num_params functions of pre-fit copula models."""
print("\nTesting integers")

dataset_name: str = 'mvt_mixed'
data: np.ndarray = all_mvt_data[dataset_name]
mdists = all_mdists_2d[dataset_name]
Expand All @@ -318,8 +308,6 @@ def test_prefit_plots(all_mvt_data, copula_params_2d, copula_params_3d,
"""Testing the marginal_pairplot, pdf_plot, cdf_plot, mc_cdf_plot,
copula_pdf_plot, copula_cdf_plot and copula_mc_cdf_plot methods of
pre-fit copula models."""
print("\nTesting plots")

num_generate: int = 10
mc_num_generate: int = num_generate
num_points = 2
Expand Down Expand Up @@ -368,7 +356,6 @@ def test_prefit_plots(all_mvt_data, copula_params_2d, copula_params_3d,


def test_prefit_names():
print("\nTesting name")
for name in distributions_map['all']:
copula = eval(name)
assert isinstance(copula.name, str), f"name of {name} is not a string."
3 changes: 0 additions & 3 deletions sklarpy/tests/misc/test_correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

def test_implemented():
"""Testing CorrelationMatrix object has IMPLEMENTED class attribute."""
print("\nTesting implemented")
# checking class attribute exists
CorrelationMatrix.IMPLEMENTED

Expand All @@ -30,7 +29,6 @@ def test_implemented():
def test_corr(continuous_data, discrete_data, mvt_continuous_data,
mvt_discrete_data):
"""Testing correlation matrices generated by implemented methods."""
print("\nTesting corr")
for method in CorrelationMatrix.IMPLEMENTED:
for data in (mvt_continuous_data, mvt_discrete_data):
d: int = data.shape[1]
Expand Down Expand Up @@ -78,7 +76,6 @@ def test_corr(continuous_data, discrete_data, mvt_continuous_data,
def test_cov(continuous_data, discrete_data, mvt_continuous_data,
mvt_discrete_data):
"""Testing the covariance matrix generated by implemented methods."""
print("\nTesting cov")
for method in CorrelationMatrix.IMPLEMENTED:
for data in (mvt_continuous_data, mvt_discrete_data):
d: int = data.shape[1]
Expand Down
1 change: 0 additions & 1 deletion sklarpy/tests/misc/test_debye.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

def test_debye():
"""Testing debye function."""
print("\nTesting debye")
n_values = range(0, 10)
x_values: list = [0, 1.5, -0.5, 4.3]

Expand Down
1 change: 0 additions & 1 deletion sklarpy/tests/misc/test_gradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

def test_gradient_1d():
"""Testing gradient_1d function."""
print("\nTesting gradient-1d")
funcs: list = [XCubed, Exp, Log]
x_values = [0, 1.5, -0.5, 4.3, 9.7, 11, -8]
datatypes = [np.asarray, list, pd.DataFrame, pd.Series, set]
Expand Down
1 change: 0 additions & 1 deletion sklarpy/tests/misc/test_modified_bessel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

def test_kv():
"""Testing the Modified Bessel function of the 2nd kind."""
print("\nTesting kv")
v_values = [0, 0.1, 4.3, 9.7]
z_values = [0, *np.random.uniform(0, 200, 100)]

Expand Down
121 changes: 57 additions & 64 deletions sklarpy/tests/multivariate/test_fitted_dists.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,68 +10,67 @@
from sklarpy.utils._params import Params


# def test_fitted_logpdf_pdf_cdf_mc_cdf(
# mvt_continuous_data, mvt_discrete_data, pd_mvt_continuous_data,
# pd_mvt_discrete_data, mv_dists_to_test, params_2d):
# """Testing the logpdf, pdf, cdf and mc-cdf functions of fitted multivariate
# distributions"""
# print('\nTesting logpdf, pdf, cdf and mc-cdf functions')
# eps: float = 10 ** -5
# num_generate: int = 10
#
# for name in mv_dists_to_test:
# _, fitted, _ = get_dist(name, params_2d, mvt_continuous_data)
# for func_str in ('logpdf', 'pdf', 'mc_cdf'): #, 'cdf'):
# func: Callable = eval(f'fitted.{func_str}')
# cdf_num: int = 10
# datasets = (mvt_continuous_data[:cdf_num, :],
# mvt_discrete_data[:cdf_num, :],
# pd_mvt_continuous_data.iloc[:cdf_num, :],
# pd_mvt_discrete_data.iloc[:cdf_num, :]) \
# if func_str == 'cdf' else (mvt_continuous_data,
# mvt_discrete_data,
# pd_mvt_continuous_data,
# pd_mvt_discrete_data)
#
# for data in datasets:
# output = func(x=data, match_datatype=True,
# num_generate=num_generate)
#
# np_output = np.asarray(output)
# n, d = np.asarray(data).shape
#
# # checking same datatype
# assert isinstance(output, type(data)), \
# f"{func_str} values for {name} do not match the " \
# f"datatype: {type(data)}."
#
# # checking the correct size
# assert np_output.size == n, \
# f"{func_str} values for {name} are not the correct size."
#
# # checking for nan-values
# assert np.isnan(np_output).sum() == 0, \
# f'nans present in {name} {func_str} values.'
#
# # function specific tests
# if func_str == 'pdf':
# assert np.all(np_output >= -eps), \
# f"pdf values in {name} are negative."
# elif func_str in ('cdf', 'mc_cdf'):
# assert np.all((-eps <= np_output) & (output <= 1 + eps)), \
# f"{func_str} values in {name} outside [0, 1]."
#
# # checking error if wrong dimension
# new_dataset: np.ndarray = np.zeros((n, d + 1))
# with pytest.raises(
# ValueError, match="Dimensions implied by parameters do "
# "not match those of the dataset."):
# func(x=new_dataset, num_generate=num_generate)
@pytest.mark.test_local_only
def test_fitted_logpdf_pdf_cdf_mc_cdf(
mvt_continuous_data, mvt_discrete_data, pd_mvt_continuous_data,
pd_mvt_discrete_data, mv_dists_to_test, params_2d):
"""Testing the logpdf, pdf, cdf and mc-cdf functions of fitted multivariate
distributions"""
eps: float = 10 ** -5
num_generate: int = 10

for name in mv_dists_to_test:
_, fitted, _ = get_dist(name, params_2d, mvt_continuous_data)
for func_str in ('logpdf', 'pdf', 'mc_cdf'): #, 'cdf'):
func: Callable = eval(f'fitted.{func_str}')
cdf_num: int = 10
datasets = (mvt_continuous_data[:cdf_num, :],
mvt_discrete_data[:cdf_num, :],
pd_mvt_continuous_data.iloc[:cdf_num, :],
pd_mvt_discrete_data.iloc[:cdf_num, :]) \
if func_str == 'cdf' else (mvt_continuous_data,
mvt_discrete_data,
pd_mvt_continuous_data,
pd_mvt_discrete_data)

for data in datasets:
output = func(x=data, match_datatype=True,
num_generate=num_generate)

np_output = np.asarray(output)
n, d = np.asarray(data).shape

# checking same datatype
assert isinstance(output, type(data)), \
f"{func_str} values for {name} do not match the " \
f"datatype: {type(data)}."

# checking the correct size
assert np_output.size == n, \
f"{func_str} values for {name} are not the correct size."

# checking for nan-values
assert np.isnan(np_output).sum() == 0, \
f'nans present in {name} {func_str} values.'

# function specific tests
if func_str == 'pdf':
assert np.all(np_output >= -eps), \
f"pdf values in {name} are negative."
elif func_str in ('cdf', 'mc_cdf'):
assert np.all((-eps <= np_output) & (output <= 1 + eps)), \
f"{func_str} values in {name} outside [0, 1]."

# checking error if wrong dimension
new_dataset: np.ndarray = np.zeros((n, d + 1))
with pytest.raises(
ValueError, match="Dimensions implied by parameters do "
"not match those of the dataset."):
func(x=new_dataset, num_generate=num_generate)


def test_fitted_rvs(mv_dists_to_test, params_2d, mvt_continuous_data):
"""Testing the rvs functions of fitted multivariate distributions."""
print('\nTesting rvs')
for name in mv_dists_to_test:
_, fitted, _ = get_dist(name, params_2d, mvt_continuous_data)
for size in (1, 2, 5, 101):
Expand All @@ -93,7 +92,6 @@ def test_fitted_scalars(mvt_continuous_data, mvt_discrete_data,
mv_dists_to_test, params_2d):
"""Testing the likelihood, loglikelihood, AIC and BIC functions of
multivariate fitted distributions."""
print('\nTesting scalars')
for name in mv_dists_to_test:
_, fitted, _ = get_dist(name, params_2d, mvt_continuous_data)
for func_str in ('likelihood', 'loglikelihood', 'aic', 'bic'):
Expand Down Expand Up @@ -131,7 +129,6 @@ def test_fitted_scalars(mvt_continuous_data, mvt_discrete_data,
def test_fitted_plots(params_2d, params_3d, mvt_continuous_data):
"""Testing the marginal_pairplot, pdf_plot, cdf_plot and mc_cdf_plot
methods of fitted multivariate distributions."""
print('\nTesting plots')
mvt_continuous_data_3d: np.ndarray = scipy.stats.multivariate_normal.rvs(
size=(mvt_continuous_data.shape[0], 3))
kwargs: dict = {'num_points': 2, 'num_generate': 10, 'mc_num_generate': 10,
Expand Down Expand Up @@ -162,7 +159,6 @@ def test_fitted_plots(params_2d, params_3d, mvt_continuous_data):

def test_fitted_params(mv_dists_to_test, params_2d, mvt_continuous_data):
"""Testing the params attribute of fitted multivariate distributions."""
print('\nTesting params')
for name in mv_dists_to_test:
_, fitted, _ = get_dist(name, params_2d, mvt_continuous_data)

Expand All @@ -178,7 +174,6 @@ def test_fitted_params(mv_dists_to_test, params_2d, mvt_continuous_data):
def test_fitted_integers(mv_dists_to_test, params_2d, mvt_continuous_data):
"""Testing the num_params, num_scalar_params, num_variables and
fitted_num_data_points of fitted multivariate distributions."""
print('\nTesting integers')
for name in mv_dists_to_test:
_, fitted, params = get_dist(name, params_2d, mvt_continuous_data)

Expand All @@ -199,7 +194,6 @@ def test_fitted_integers(mv_dists_to_test, params_2d, mvt_continuous_data):

def test_fitted_converged(mv_dists_to_test, params_2d, mvt_continuous_data):
"""Testing converged attributes of fitted multivariate distributions."""
print('\nTesting converged')
for name in mv_dists_to_test:
_, fitted, _ = get_dist(name, params_2d, mvt_continuous_data)

Expand All @@ -214,7 +208,6 @@ def test_fitted_converged(mv_dists_to_test, params_2d, mvt_continuous_data):

def test_fitted_summaries(mv_dists_to_test, params_2d, mvt_continuous_data):
"""Testing the summaries of fitted multivariate distributions."""
print('\nTesting summaries')
for name in mv_dists_to_test:
_, fitted, _ = get_dist(name, params_2d, mvt_continuous_data)

Expand Down
Loading

0 comments on commit b5cf517

Please sign in to comment.