diff --git a/docs/source/Copulas.rst b/docs/source/Copulas.rst index 30f740d..aa8d901 100644 --- a/docs/source/Copulas.rst +++ b/docs/source/Copulas.rst @@ -203,7 +203,7 @@ Producing a pairplot of the marginals:: mfitter2.pairplot() -.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/mfitter_pairplot.png?raw=true +.. image:: https://github.com/tfm000/sklarpy/blob/main/media/mfitter_pairplot.png?raw=true :alt: MarginalFitter Pair-Plot :scale: 60% :align: center @@ -315,17 +315,17 @@ Plotting our fit:: fitted_copula.mc_cdf_plot(show=False) plt.show() -.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/PDF_Gh_PDF_Plot_Plot2.png?raw=true +.. image:: https://github.com/tfm000/sklarpy/blob/main/media/PDF_Gh_PDF_Plot_Plot2.png?raw=true :alt: Generalized Hyperbolic PDF :scale: 60% :align: center -.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/Copula_PDF_Gh_Copula_PDF_Plot_Plot2.png?raw=true +.. image:: https://github.com/tfm000/sklarpy/blob/main/media/Copula_PDF_Gh_Copula_PDF_Plot_Plot2.png?raw=true :alt: Generalized Hyperbolic Copula PDF :scale: 60% :align: center -.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/MC_CDF_Gh_MC_CDF_Plot_Plot2.png?raw=true +.. image:: https://github.com/tfm000/sklarpy/blob/main/media/MC_CDF_Gh_MC_CDF_Plot_Plot2.png?raw=true :alt: Generalized Hyperbolic CDF :scale: 60% :align: center diff --git a/docs/source/Multivariate.rst b/docs/source/Multivariate.rst index e091343..2e6e7e5 100644 --- a/docs/source/Multivariate.rst +++ b/docs/source/Multivariate.rst @@ -131,17 +131,17 @@ Plotting our fitted distribution:: fitted_msh.marginal_pairplot(show=False) plt.show() -.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/PDF_Mvt_Shyperbolic_PDF_Plot_Plot.png?raw=true +.. image:: https://github.com/tfm000/sklarpy/blob/main/media/PDF_Mvt_Shyperbolic_PDF_Plot_Plot.png?raw=true :alt: Symmetric Hyperbolic PDF :scale: 60% :align: center -.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/MC_CDF_Mvt_Shyperbolic_MC_CDF_Plot_Plot.png?raw=true +.. image:: https://github.com/tfm000/sklarpy/blob/main/media/MC_CDF_Mvt_Shyperbolic_MC_CDF_Plot_Plot.png?raw=true :alt: Symmetric Hyperbolic PDF :scale: 60% :align: center -.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/mvt_shyperbolic_marginal_pair_plot.png?raw=true +.. image:: https://github.com/tfm000/sklarpy/blob/main/media/mvt_shyperbolic_marginal_pair_plot.png?raw=true :alt: Symmetric Hyperbolic PDF :scale: 60% :align: center diff --git a/docs/source/Univariate.rst b/docs/source/Univariate.rst index 4d79d44..ec6f5c2 100644 --- a/docs/source/Univariate.rst +++ b/docs/source/Univariate.rst @@ -184,7 +184,7 @@ And plot our fitted distribution:: fitted_gamma.plot() -.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/univariate_continuous_example_figure1.png?raw=true +.. image:: https://github.com/tfm000/sklarpy/blob/main/media/univariate_continuous_example_figure1.png?raw=true :alt: gamma plot :align: center @@ -258,7 +258,7 @@ And plot our fitted distribution:: fitted_poisson.plot() -.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/univariate_discrete_example_figure1.png?raw=true +.. image:: https://github.com/tfm000/sklarpy/blob/main/media/univariate_discrete_example_figure1.png?raw=true :alt: poisson plot :align: center @@ -347,7 +347,7 @@ finding our best fit:: Sum of Squared Error 9.18869 #Fitted Data Points 1000 -.. image:: https://github.com/tfm000/sklarpy/blob/docs/readthedocs/media/univariate_fitter_example_figure1.png?raw=true +.. image:: https://github.com/tfm000/sklarpy/blob/main/media/univariate_fitter_example_figure1.png?raw=true :alt: poisson plot :align: center diff --git a/sklarpy/__init__.py b/sklarpy/__init__.py index 90cdb6b..d859f70 100644 --- a/sklarpy/__init__.py +++ b/sklarpy/__init__.py @@ -1,2 +1,2 @@ -from sklarpy._utils._serialize import load -from sklarpy._utils._object_printing import print_full +from sklarpy.utils._serialize import load +from sklarpy.utils._object_printing import print_full diff --git a/sklarpy/_plotting/__init__.py b/sklarpy/_plotting/__init__.py deleted file mode 100644 index 59b593b..0000000 --- a/sklarpy/_plotting/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -from sklarpy._plotting._pair_plot import pair_plot -from sklarpy._plotting._threeD_plot import threeD_plot diff --git a/sklarpy/_utils/__init__.py b/sklarpy/_utils/__init__.py deleted file mode 100644 index 47cf177..0000000 --- a/sklarpy/_utils/__init__.py +++ /dev/null @@ -1,12 +0,0 @@ -# Useful functions/objects and values used throughout SklarPy -from sklarpy._utils._errors import SignificanceError, DiscreteError, \ - FitError, SaveError, LoadError, DistributionError -from sklarpy._utils._input_handlers import univariate_num_to_array, \ - check_params, check_univariate_data, check_array_datatype, \ - check_multivariate_data, get_mask -from sklarpy._utils._type_keeper import TypeKeeper -from sklarpy._utils._iterator import get_iterator -from sklarpy._utils._copy import Copyable -from sklarpy._utils._not_implemented import NotImplementedBase -from sklarpy._utils._params import Params -from sklarpy._utils._serialize import Savable diff --git a/sklarpy/copulas/_distributions/_archimedean.py b/sklarpy/copulas/_distributions/_archimedean.py index 9d83408..2195c3a 100644 --- a/sklarpy/copulas/_distributions/_archimedean.py +++ b/sklarpy/copulas/_distributions/_archimedean.py @@ -5,7 +5,7 @@ from sklarpy.copulas._prefit_dists import PreFitCopula from sklarpy.copulas._fitted_dists import FittedCopula -from sklarpy._utils import Params +from sklarpy.utils._params import Params from sklarpy.copulas import MarginalFitter __all__ = ['clayton_copula_gen', 'gumbel_copula_gen', 'frank_copula_gen'] diff --git a/sklarpy/copulas/_distributions/_gaussian.py b/sklarpy/copulas/_distributions/_gaussian.py index 4ad47ba..34c15e7 100644 --- a/sklarpy/copulas/_distributions/_gaussian.py +++ b/sklarpy/copulas/_distributions/_gaussian.py @@ -6,7 +6,7 @@ from sklarpy.copulas._prefit_dists import PreFitCopula from sklarpy.copulas._fitted_dists import FittedCopula -from sklarpy._utils import Params +from sklarpy.utils._params import Params from sklarpy.copulas import MarginalFitter __all__ = ['gaussian_copula_gen'] diff --git a/sklarpy/copulas/_distributions/_gaussian_kde.py b/sklarpy/copulas/_distributions/_gaussian_kde.py index 6c4a787..0707bae 100644 --- a/sklarpy/copulas/_distributions/_gaussian_kde.py +++ b/sklarpy/copulas/_distributions/_gaussian_kde.py @@ -6,7 +6,7 @@ from sklarpy.copulas._prefit_dists import PreFitCopula from sklarpy.copulas._fitted_dists import FittedCopula from sklarpy.copulas import MarginalFitter -from sklarpy._utils import Params +from sklarpy.utils._params import Params __all__ = ['gaussian_kde_copula_gen'] diff --git a/sklarpy/copulas/_distributions/_generalized_hyperbolic.py b/sklarpy/copulas/_distributions/_generalized_hyperbolic.py index cad4d2d..9b29cce 100644 --- a/sklarpy/copulas/_distributions/_generalized_hyperbolic.py +++ b/sklarpy/copulas/_distributions/_generalized_hyperbolic.py @@ -5,7 +5,7 @@ from sklarpy.copulas._prefit_dists import PreFitCopula from sklarpy.copulas._fitted_dists import FittedCopula -from sklarpy._utils import Params +from sklarpy.utils._params import Params from sklarpy.copulas import MarginalFitter from sklarpy.univariate import gh diff --git a/sklarpy/copulas/_distributions/_hyperbolics.py b/sklarpy/copulas/_distributions/_hyperbolics.py index fb25918..dec44ab 100644 --- a/sklarpy/copulas/_distributions/_hyperbolics.py +++ b/sklarpy/copulas/_distributions/_hyperbolics.py @@ -4,7 +4,7 @@ from sklarpy.copulas._distributions._generalized_hyperbolic import \ gen_hyperbolic_copula_gen -from sklarpy._utils import Params +from sklarpy.utils._params import Params __all__ = ['marginal_hyperbolic_copula_gen', 'hyperbolic_copula_gen', 'nig_copula_gen'] diff --git a/sklarpy/copulas/_distributions/_skewed_t.py b/sklarpy/copulas/_distributions/_skewed_t.py index 97eec74..79c4820 100644 --- a/sklarpy/copulas/_distributions/_skewed_t.py +++ b/sklarpy/copulas/_distributions/_skewed_t.py @@ -4,7 +4,7 @@ from sklarpy.copulas._distributions._generalized_hyperbolic import \ gen_hyperbolic_copula_gen -from sklarpy._utils import Params +from sklarpy.utils._params import Params from sklarpy.univariate.distributions import skewed_t __all__ = ['skewed_t_copula_gen'] diff --git a/sklarpy/copulas/_distributions/_student_t.py b/sklarpy/copulas/_distributions/_student_t.py index 246e5a2..8b8f454 100644 --- a/sklarpy/copulas/_distributions/_student_t.py +++ b/sklarpy/copulas/_distributions/_student_t.py @@ -6,7 +6,7 @@ from sklarpy.copulas._prefit_dists import PreFitCopula from sklarpy.copulas._fitted_dists import FittedCopula -from sklarpy._utils import Params +from sklarpy.utils._params import Params from sklarpy.copulas import MarginalFitter __all__ = ['student_t_copula_gen'] diff --git a/sklarpy/copulas/_distributions/_symmetric_generalized_hyperbolic.py b/sklarpy/copulas/_distributions/_symmetric_generalized_hyperbolic.py index 38711a1..ad9f2f2 100644 --- a/sklarpy/copulas/_distributions/_symmetric_generalized_hyperbolic.py +++ b/sklarpy/copulas/_distributions/_symmetric_generalized_hyperbolic.py @@ -4,7 +4,7 @@ from sklarpy.copulas._distributions._generalized_hyperbolic import \ gen_hyperbolic_copula_gen -from sklarpy._utils import Params +from sklarpy.utils._params import Params __all__ = ['sym_gen_hyperbolic_copula_gen'] diff --git a/sklarpy/copulas/_fitted_dists.py b/sklarpy/copulas/_fitted_dists.py index 6852512..f8e847a 100644 --- a/sklarpy/copulas/_fitted_dists.py +++ b/sklarpy/copulas/_fitted_dists.py @@ -3,7 +3,10 @@ import pandas as pd from typing import Union, Iterable -from sklarpy._utils import TypeKeeper, Savable, Copyable, Params +from sklarpy.utils._type_keeper import TypeKeeper +from sklarpy.utils._serialize import Savable +from sklarpy.utils._copy import Copyable +from sklarpy.utils._params import Params __all__ = ['FittedCopula'] diff --git a/sklarpy/copulas/_prefit_dists.py b/sklarpy/copulas/_prefit_dists.py index 5614e2e..74bf06e 100644 --- a/sklarpy/copulas/_prefit_dists.py +++ b/sklarpy/copulas/_prefit_dists.py @@ -2,14 +2,18 @@ from typing import Union, Iterable, Callable, Dict, List import numpy as np import pandas as pd +from collections import deque from sklarpy.copulas import MarginalFitter -from sklarpy._utils import check_multivariate_data, TypeKeeper, Params, \ - NotImplementedBase, get_mask +from sklarpy.utils._input_handlers import check_multivariate_data, get_mask +from sklarpy.utils._type_keeper import TypeKeeper +from sklarpy.utils._params import Params +from sklarpy.utils._not_implemented import NotImplementedBase from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate, \ FittedContinuousMultivariate from sklarpy.univariate._fitted_dists import FittedUnivariateBase -from sklarpy._plotting import pair_plot, threeD_plot +from sklarpy.plotting._pair_plot import pair_plot +from sklarpy.plotting._threeD_plot import threeD_plot from sklarpy.copulas._fitted_dists import FittedCopula __all__ = ['PreFitCopula'] @@ -17,6 +21,8 @@ class PreFitCopula(NotImplementedBase): """A pre-fit copula model""" + __MAX_RVS_LOOPS: int = 100 + def __init__(self, name: str, mv_object: PreFitContinuousMultivariate): """A pre-fit copula model. @@ -796,17 +802,31 @@ def copula_rvs(self, size: int, copula_params: Union[Params, tuple], distribution. These correspond to randomly sampled cdf / pseudo-observation values of the univariate marginals. """ - # generating random variables from multivariate distribution - raw_mv_rvs: np.ndarray = self._mv_object.rvs(size, copula_params) - - # bounding these above and below - eps: float = 10 ** -5 - rvs_df: pd.DataFrame = pd.DataFrame(raw_mv_rvs) - rvs_df[rvs_df < 0] = eps - rvs_df[rvs_df > 1] = 1 - eps - mv_rvs: np.ndarray = rvs_df.to_numpy() - - return self._g_to_u(mv_rvs, copula_params) + num_loops: int = 0 + d: int = self._mv_object._get_dim( + self._mv_object._get_params(copula_params)) + valid_copula_rvs: deque = deque() + while size > 0: + # generating random variables from multivariate distribution + mv_rvs: np.ndarray = self._mv_object.rvs(size, copula_params) + + # converting to copula rvs + raw_copula_rvs: np.ndarray = self._g_to_u(mv_rvs, copula_params) + + # filtering out invalid copula rvs (not in [0, 1]^d) + mask: np.ndarray = ((raw_copula_rvs > 0) & (raw_copula_rvs < 1) + ).sum(axis=1) == d + copula_rvs = raw_copula_rvs[mask] + valid_copula_rvs.append(copula_rvs) + + # repeating until sample size reached + size -= copula_rvs.shape[0] + num_loops += 1 + if num_loops > self.__MAX_RVS_LOOPS: + raise ArithmeticError(f"Unable to generate valid copula rvs. " + f"Max number of retries reached: " + f"{self.__MAX_RVS_LOOPS}") + return np.concatenate(valid_copula_rvs, axis=0) def _get_components_summary(self, fitted_mv_object: FittedContinuousMultivariate, @@ -1008,11 +1028,20 @@ def fit(self, data: Union[pd.DataFrame, np.ndarray, None] = None, "params do not match.") # generating data to use when calculating statistics - data_array: np.ndarray = self.rvs( - size=10**3, copula_params=fitted_mv_object.params, - mdists=mdists_dict, ppf_approx=True - ) if data is None \ - else check_multivariate_data(data, allow_1d=True, allow_nans=True) + try: + data_array: np.ndarray = self.rvs( + size=10**3, copula_params=fitted_mv_object.params, + mdists=mdists_dict, ppf_approx=True) if data is None \ + else check_multivariate_data( + data, allow_1d=True, allow_nans=True) + + except ArithmeticError as e: + if str(e) != (f"Unable to generate valid copula rvs. Max number " + f"of retries reached: {self.__MAX_RVS_LOOPS}"): + raise + else: + data_array: np.ndarray = np.full((10**3, d), np.nan, + dtype=float) # fitting TypeKeeper object type_keeper: TypeKeeper = TypeKeeper(data_array) diff --git a/sklarpy/copulas/marginal_fitter.py b/sklarpy/copulas/marginal_fitter.py index 273757d..f536358 100644 --- a/sklarpy/copulas/marginal_fitter.py +++ b/sklarpy/copulas/marginal_fitter.py @@ -4,10 +4,13 @@ import pandas as pd from typing import Union, Iterable -from sklarpy._utils import FitError, TypeKeeper, check_multivariate_data, \ - get_iterator, Savable +from sklarpy.utils._errors import FitError +from sklarpy.utils._type_keeper import TypeKeeper +from sklarpy.utils._input_handlers import check_multivariate_data +from sklarpy.utils._iterator import get_iterator +from sklarpy.utils._serialize import Savable from sklarpy.univariate import UnivariateFitter -from sklarpy._plotting import pair_plot +from sklarpy.plotting._pair_plot import pair_plot __all__ = ['MarginalFitter'] diff --git a/sklarpy/misc/correlation.py b/sklarpy/misc/correlation.py index 0bb8f88..9c318d4 100644 --- a/sklarpy/misc/correlation.py +++ b/sklarpy/misc/correlation.py @@ -4,7 +4,7 @@ import warnings from typing import Tuple, Union -from sklarpy._utils import check_multivariate_data +from sklarpy.utils._input_handlers import check_multivariate_data __all__ = ['CorrelationMatrix'] diff --git a/sklarpy/misc/gradient.py b/sklarpy/misc/gradient.py index c32e725..fca93c7 100644 --- a/sklarpy/misc/gradient.py +++ b/sklarpy/misc/gradient.py @@ -4,7 +4,7 @@ from typing import Callable, Union, Iterable from collections import deque -from sklarpy._utils import check_univariate_data +from sklarpy.utils._input_handlers import check_univariate_data __all__ = ['gradient_1d'] diff --git a/sklarpy/multivariate/_distributions/_archimedean.py b/sklarpy/multivariate/_distributions/_archimedean.py index ddebcd4..1e9eb55 100644 --- a/sklarpy/multivariate/_distributions/_archimedean.py +++ b/sklarpy/multivariate/_distributions/_archimedean.py @@ -10,7 +10,9 @@ from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate from sklarpy.multivariate._fitted_dists import FittedContinuousMultivariate -from sklarpy._utils import get_iterator, FitError, Params +from sklarpy.utils._params import Params +from sklarpy.utils._iterator import get_iterator +from sklarpy.utils._errors import FitError from sklarpy.misc import debye __all__ = ['multivariate_clayton_gen', 'multivariate_gumbel_gen', diff --git a/sklarpy/multivariate/_distributions/_gaussian_kde.py b/sklarpy/multivariate/_distributions/_gaussian_kde.py index d05f317..c9f3653 100644 --- a/sklarpy/multivariate/_distributions/_gaussian_kde.py +++ b/sklarpy/multivariate/_distributions/_gaussian_kde.py @@ -6,7 +6,7 @@ from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate from sklarpy.multivariate._fitted_dists import FittedContinuousMultivariate -from sklarpy._utils import Params +from sklarpy.utils._params import Params __all__ = ['multivariate_gaussian_kde_gen'] diff --git a/sklarpy/multivariate/_distributions/_generalized_hyperbolic.py b/sklarpy/multivariate/_distributions/_generalized_hyperbolic.py index 36af31b..cecd9cc 100644 --- a/sklarpy/multivariate/_distributions/_generalized_hyperbolic.py +++ b/sklarpy/multivariate/_distributions/_generalized_hyperbolic.py @@ -10,7 +10,7 @@ from sklarpy.univariate import gig from sklarpy.univariate._distributions import _gh from sklarpy.misc import CorrelationMatrix, kv -from sklarpy._utils import Params +from sklarpy.utils._params import Params __all__ = ['multivariate_gen_hyperbolic_gen'] diff --git a/sklarpy/multivariate/_distributions/_hyperbolics.py b/sklarpy/multivariate/_distributions/_hyperbolics.py index 30bcd87..8d13a79 100644 --- a/sklarpy/multivariate/_distributions/_hyperbolics.py +++ b/sklarpy/multivariate/_distributions/_hyperbolics.py @@ -7,7 +7,7 @@ from sklarpy.multivariate._distributions._generalized_hyperbolic import \ multivariate_gen_hyperbolic_gen from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate -from sklarpy._utils import Params +from sklarpy.utils._params import Params __all__ = ['multivariate_marginal_hyperbolic_gen', 'multivariate_hyperbolic_gen', 'multivariate_nig_gen'] diff --git a/sklarpy/multivariate/_distributions/_normal.py b/sklarpy/multivariate/_distributions/_normal.py index 4874250..f218937 100644 --- a/sklarpy/multivariate/_distributions/_normal.py +++ b/sklarpy/multivariate/_distributions/_normal.py @@ -7,7 +7,7 @@ from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate from sklarpy.misc import CorrelationMatrix from sklarpy.multivariate._fitted_dists import FittedContinuousMultivariate -from sklarpy._utils import Params +from sklarpy.utils._params import Params __all__ = ['multivariate_normal_gen'] diff --git a/sklarpy/multivariate/_distributions/_skewed_t.py b/sklarpy/multivariate/_distributions/_skewed_t.py index 0697bf8..604b03a 100644 --- a/sklarpy/multivariate/_distributions/_skewed_t.py +++ b/sklarpy/multivariate/_distributions/_skewed_t.py @@ -9,7 +9,7 @@ multivariate_gen_hyperbolic_gen from sklarpy.multivariate._distributions._student_t import \ multivariate_student_t_gen -from sklarpy._utils import Params +from sklarpy.utils._params import Params from sklarpy.misc import kv from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate from sklarpy.univariate import ig diff --git a/sklarpy/multivariate/_distributions/_student_t.py b/sklarpy/multivariate/_distributions/_student_t.py index c2b366c..937485a 100644 --- a/sklarpy/multivariate/_distributions/_student_t.py +++ b/sklarpy/multivariate/_distributions/_student_t.py @@ -8,7 +8,7 @@ from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate from sklarpy.multivariate._fitted_dists import FittedContinuousMultivariate -from sklarpy._utils import Params +from sklarpy.utils._params import Params from sklarpy.misc import CorrelationMatrix __all__ = ['multivariate_student_t_gen'] diff --git a/sklarpy/multivariate/_distributions/_symmetric_generalized_hyperbolic.py b/sklarpy/multivariate/_distributions/_symmetric_generalized_hyperbolic.py index 731dcff..c92855a 100644 --- a/sklarpy/multivariate/_distributions/_symmetric_generalized_hyperbolic.py +++ b/sklarpy/multivariate/_distributions/_symmetric_generalized_hyperbolic.py @@ -5,7 +5,7 @@ from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate from sklarpy.multivariate._distributions._generalized_hyperbolic import \ multivariate_gen_hyperbolic_gen -from sklarpy._utils import Params +from sklarpy.utils._params import Params __all__ = ['multivariate_sym_gen_hyperbolic_gen'] diff --git a/sklarpy/multivariate/_distributions/_symmetric_hyperbolics.py b/sklarpy/multivariate/_distributions/_symmetric_hyperbolics.py index 00860e0..1f6e88d 100644 --- a/sklarpy/multivariate/_distributions/_symmetric_hyperbolics.py +++ b/sklarpy/multivariate/_distributions/_symmetric_hyperbolics.py @@ -5,7 +5,7 @@ from sklarpy.multivariate._distributions._hyperbolics import \ multivariate_hyperbolic_base_gen from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate -from sklarpy._utils import Params +from sklarpy.utils._params import Params __all__ = ['multivariate_sym_marginal_hyperbolic_gen', 'multivariate_sym_hyperbolic_base_gen', 'multivariate_sym_nig_gen'] diff --git a/sklarpy/multivariate/_fitted_dists.py b/sklarpy/multivariate/_fitted_dists.py index a6a4cfc..2d4e970 100644 --- a/sklarpy/multivariate/_fitted_dists.py +++ b/sklarpy/multivariate/_fitted_dists.py @@ -3,7 +3,10 @@ import pandas as pd from typing import Union, Iterable -from sklarpy._utils import TypeKeeper, Savable, Copyable, Params +from sklarpy.utils._params import Params +from sklarpy.utils._type_keeper import TypeKeeper +from sklarpy.utils._copy import Copyable +from sklarpy.utils._serialize import Savable __all__ = ['FittedContinuousMultivariate'] diff --git a/sklarpy/multivariate/_params/_archimedean.py b/sklarpy/multivariate/_params/_archimedean.py index d4a6012..c1b24f0 100644 --- a/sklarpy/multivariate/_params/_archimedean.py +++ b/sklarpy/multivariate/_params/_archimedean.py @@ -1,5 +1,5 @@ # Contains code for holding Archimedean copula parameters -from sklarpy._utils import Params +from sklarpy.utils._params import Params __all__ = ['MvtClaytonParams', 'MvtGumbelParams', 'BvtFrankParams'] diff --git a/sklarpy/multivariate/_params/_gaussian_kde.py b/sklarpy/multivariate/_params/_gaussian_kde.py index 3f038b1..44d5df2 100644 --- a/sklarpy/multivariate/_params/_gaussian_kde.py +++ b/sklarpy/multivariate/_params/_gaussian_kde.py @@ -1,5 +1,5 @@ # Contains code for holding Gaussian KDE parameters. -from sklarpy._utils import Params +from sklarpy.utils._params import Params __all__ = ['MvtGaussianKDEParams'] diff --git a/sklarpy/multivariate/_params/_normal.py b/sklarpy/multivariate/_params/_normal.py index 467d3ee..1b22198 100644 --- a/sklarpy/multivariate/_params/_normal.py +++ b/sklarpy/multivariate/_params/_normal.py @@ -1,7 +1,7 @@ # Contains code for holding Gaussian / Normal parameters import numpy as np -from sklarpy._utils import Params +from sklarpy.utils._params import Params __all__ = ['MvtNormalParams'] diff --git a/sklarpy/multivariate/_prefit_dists.py b/sklarpy/multivariate/_prefit_dists.py index 450160b..fe6a4b4 100644 --- a/sklarpy/multivariate/_prefit_dists.py +++ b/sklarpy/multivariate/_prefit_dists.py @@ -7,9 +7,14 @@ import scipy.integrate from scipy.optimize import differential_evolution -from sklarpy._utils import TypeKeeper, check_multivariate_data, get_iterator, \ - FitError, Params, NotImplementedBase -from sklarpy._plotting import pair_plot, threeD_plot +from sklarpy.utils._type_keeper import TypeKeeper +from sklarpy.utils._iterator import get_iterator +from sklarpy.utils._not_implemented import NotImplementedBase +from sklarpy.utils._params import Params +from sklarpy.utils._input_handlers import check_multivariate_data +from sklarpy.utils._errors import FitError +from sklarpy.plotting._pair_plot import pair_plot +from sklarpy.plotting._threeD_plot import threeD_plot from sklarpy.multivariate._fitted_dists import FittedContinuousMultivariate from sklarpy.misc import CorrelationMatrix diff --git a/sklarpy/_plotting/README.md b/sklarpy/plotting/README.md similarity index 100% rename from sklarpy/_plotting/README.md rename to sklarpy/plotting/README.md diff --git a/sklarpy/plotting/__init__.py b/sklarpy/plotting/__init__.py new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/sklarpy/plotting/__init__.py @@ -0,0 +1 @@ + diff --git a/sklarpy/_plotting/_pair_plot.py b/sklarpy/plotting/_pair_plot.py similarity index 85% rename from sklarpy/_plotting/_pair_plot.py rename to sklarpy/plotting/_pair_plot.py index fefa07e..c7a4fd6 100644 --- a/sklarpy/_plotting/_pair_plot.py +++ b/sklarpy/plotting/_pair_plot.py @@ -2,6 +2,8 @@ import matplotlib.pyplot as plt import pandas as pd import seaborn as sns +# from collections import deque +# import math __all__ = ['pair_plot'] @@ -64,6 +66,16 @@ def pair_plot(plot_df: pd.DataFrame, title: str, color: str = 'royalblue', raise TypeError("invalid argument type in pair_plot. check grid, " "plot_kde and show are boolean.") + # # getting plot limits + # lims: deque = deque() + # n, d = plot_df.shape + # + # lb: int = math.floor(n * 0.01) + # ub: int = math.ceil(n * 0.99) + # for i in range(d): + # ordered_i = plot_df.iloc[:, i].sort_values() + # lims.append((ordered_i.iloc[lb], ordered_i.iloc[ub])) + # plotting sns.set_style("whitegrid", {'axes.grid': grid}) g = sns.PairGrid(plot_df, corner=(not plot_kde)) @@ -74,5 +86,10 @@ def pair_plot(plot_df: pd.DataFrame, title: str, color: str = 'royalblue', g.fig.suptitle(title) plt.tight_layout() + # # setting limits + # for i in range(d): + # g.axes[i, i].set_xlim(lims[i]) + # g.axes[i, i].set_ylim(lims[i]) + if show: plt.show() diff --git a/sklarpy/_plotting/_threeD_plot.py b/sklarpy/plotting/_threeD_plot.py similarity index 99% rename from sklarpy/_plotting/_threeD_plot.py rename to sklarpy/plotting/_threeD_plot.py index c8b69cb..0ff0f31 100644 --- a/sklarpy/_plotting/_threeD_plot.py +++ b/sklarpy/plotting/_threeD_plot.py @@ -4,7 +4,7 @@ import matplotlib.pyplot as plt import warnings -from sklarpy._utils import get_iterator +from sklarpy.utils._iterator import get_iterator __all__ = ['threeD_plot'] diff --git a/sklarpy/tests/copulas/test_fitted_dists.py b/sklarpy/tests/copulas/test_fitted_dists.py index 3736e3e..3525d71 100644 --- a/sklarpy/tests/copulas/test_fitted_dists.py +++ b/sklarpy/tests/copulas/test_fitted_dists.py @@ -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 @@ -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 @@ -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] @@ -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']: @@ -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] @@ -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 @@ -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] diff --git a/sklarpy/tests/copulas/test_marginal_fitter.py b/sklarpy/tests/copulas/test_marginal_fitter.py index cef3d8c..180c066 100644 --- a/sklarpy/tests/copulas/test_marginal_fitter.py +++ b/sklarpy/tests/copulas/test_marginal_fitter.py @@ -4,19 +4,17 @@ import numpy as np from sklarpy.copulas import MarginalFitter -from sklarpy._utils import FitError +from sklarpy.utils._errors import FitError 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) @@ -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) diff --git a/sklarpy/tests/copulas/test_prefit_dists.py b/sklarpy/tests/copulas/test_prefit_dists.py index 2775f3b..a3e090d 100644 --- a/sklarpy/tests/copulas/test_prefit_dists.py +++ b/sklarpy/tests/copulas/test_prefit_dists.py @@ -8,13 +8,13 @@ from sklarpy.copulas import * from sklarpy.copulas._prefit_dists import PreFitCopula from sklarpy.copulas._fitted_dists import FittedCopula -from sklarpy._utils import Params, FitError +from sklarpy.utils._errors import FitError +from sklarpy.utils._params import Params from sklarpy.tests.copulas.helpers import get_dist 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), \ @@ -23,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() @@ -91,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 @@ -152,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 @@ -212,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] @@ -251,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']: @@ -293,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] @@ -317,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 @@ -367,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." diff --git a/sklarpy/tests/misc/test_correlation.py b/sklarpy/tests/misc/test_correlation.py index 73ed01e..7cc9f4d 100644 --- a/sklarpy/tests/misc/test_correlation.py +++ b/sklarpy/tests/misc/test_correlation.py @@ -7,7 +7,6 @@ def test_implemented(): """Testing CorrelationMatrix object has IMPLEMENTED class attribute.""" - print("\nTesting implemented") # checking class attribute exists CorrelationMatrix.IMPLEMENTED @@ -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] @@ -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] diff --git a/sklarpy/tests/misc/test_debye.py b/sklarpy/tests/misc/test_debye.py index 8a85b2b..b88b103 100644 --- a/sklarpy/tests/misc/test_debye.py +++ b/sklarpy/tests/misc/test_debye.py @@ -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] diff --git a/sklarpy/tests/misc/test_gradient.py b/sklarpy/tests/misc/test_gradient.py index 9276f4e..3dd278a 100644 --- a/sklarpy/tests/misc/test_gradient.py +++ b/sklarpy/tests/misc/test_gradient.py @@ -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] diff --git a/sklarpy/tests/misc/test_modified_bessel.py b/sklarpy/tests/misc/test_modified_bessel.py index 626aa0a..e08211a 100644 --- a/sklarpy/tests/misc/test_modified_bessel.py +++ b/sklarpy/tests/misc/test_modified_bessel.py @@ -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)] diff --git a/sklarpy/tests/multivariate/test_fitted_dists.py b/sklarpy/tests/multivariate/test_fitted_dists.py index 43eed94..2c254f0 100644 --- a/sklarpy/tests/multivariate/test_fitted_dists.py +++ b/sklarpy/tests/multivariate/test_fitted_dists.py @@ -7,71 +7,70 @@ import scipy.stats from sklarpy.tests.multivariate.helpers import get_dist -from sklarpy._utils 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) +from sklarpy.utils._params import Params + + +@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): @@ -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'): @@ -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, @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/sklarpy/tests/multivariate/test_prefit_dists.py b/sklarpy/tests/multivariate/test_prefit_dists.py index 45ed1a7..df476a3 100644 --- a/sklarpy/tests/multivariate/test_prefit_dists.py +++ b/sklarpy/tests/multivariate/test_prefit_dists.py @@ -8,12 +8,11 @@ from sklarpy.multivariate import * from sklarpy.multivariate._prefit_dists import PreFitContinuousMultivariate from sklarpy.multivariate._fitted_dists import FittedContinuousMultivariate -from sklarpy._utils import Params +from sklarpy.utils._params import Params from sklarpy.tests.multivariate.helpers import get_dist def test_correct_type(): - print('\nTesting correct type') """Testing multivariate distributions are all SklarPy objects.""" for name in distributions_map['all']: dist = eval(name) @@ -21,131 +20,131 @@ def test_correct_type(): f"{name} is not a child class of PreFitContinuousMultivariate" -# def test_fit_to_data(mvt_continuous_data, mvt_discrete_data, -# pd_mvt_continuous_data, pd_mvt_discrete_data, -# mv_dists_to_test): -# """Testing we can fit multivariate distributions to data.""" -# print('\nTesting fitting to data') -# for name in mv_dists_to_test: -# dist = eval(name) -# -# # fitting to both continuous and discrete data, -# # in both numpy and pandas format -# for method in dist._DATA_FIT_METHODS: -# for data in (mvt_continuous_data, pd_mvt_continuous_data, -# mvt_discrete_data, pd_mvt_discrete_data): -# if isinstance(data, np.ndarray): -# d: int = data.shape[1] -# else: -# d: int = len(data.columns) -# -# try: -# # fitting to data -# fitted = dist.fit(data=data, method=method) -# except RuntimeError: -# continue -# -# # testing fitted to correct type -# assert issubclass(type(fitted), FittedContinuousMultivariate -# ), f"{name} is not fitted to a child " \ -# f"class of FittedContinuousMultivariate." -# -# # testing parameters object -# params = fitted.params -# assert issubclass(type(params), Params), \ -# f"{name} fitted parameters are not a child class of " \ -# f"Params." -# assert params.name == name, \ -# f"{name} fitted parameters is not the correct type." -# assert len(params) > 0, \ -# f"{name} fitted parameter object is empty." -# -# vector_attributes: tuple = ('loc', 'mean', 'gamma') -# matrix_attributes: tuple = ('cov', 'corr') -# scale_attributes: tuple = ('dof', 'chi', 'psi', -# 'lamb', 'theta') -# to_obj_attributes: tuple = ('dict', 'tuple', 'list') -# -# for vect_str in vector_attributes: -# if vect_str in dir(params): -# vect = eval(f'params.{vect_str}') -# assert isinstance(vect, np.ndarray), \ -# f"{vect_str} fitted parameter is not an array " \ -# f"for {name}." -# assert vect.size == d, \ -# f"{vect_str} fitted parameter does not contain " \ -# f"the correct number of elements for {name}." -# assert vect.shape == (d, 1), \ -# f"{vect_str} fitted parameter is not of {(d, 1)}" \ -# f" shape for {name}." -# assert np.isnan(vect).sum() == 0, \ -# f"{vect_str} fitted parameter contains nan " \ -# f"values for {name}." -# -# for mat_str in matrix_attributes: -# if mat_str in dir(params): -# mat = eval(f'params.{mat_str}') -# assert isinstance(mat, np.ndarray), \ -# f"{mat_str} fitted parameter is not an array " \ -# f"for {name}." -# assert mat.shape == (d, d), \ -# f"{mat_str} fitted parameter is not of {(d, d)} " \ -# f"shape for {name}." -# assert np.isnan(mat).sum() == 0, \ -# f"{mat_str} fitted parameter contains nan " \ -# f"values for {name}." -# -# for scale_str in scale_attributes: -# if scale_str in dir(params): -# scale = eval(f'params.{scale_str}') -# assert (isinstance(scale, float) -# or isinstance(scale, int)), \ -# f"{scale_str} fitted parameter is not a scalar " \ -# f"value for {name}." -# assert not np.isnan(scale),\ -# f"{scale_str} fitted parameter is nan for {name}." -# -# for obj_str in to_obj_attributes: -# assert f'to_{obj_str}' in dir(params), \ -# f"to_{obj_str} attribute does not exist for {name}" -# obj_target_type = eval(obj_str) -# obj = eval(f'params.to_{obj_str}') -# assert isinstance(obj, obj_target_type), \ -# f"to_{obj_str} attribute does not return a " \ -# f"{obj_target_type} for {name}." -# assert len(obj) == len(params), \ -# f"to_{obj_str} attribute does not contain the " \ -# f"correct number of parameters for {name}." -# -# # testing we can fit distribution using parameters object. -# params_fitted = dist.fit(params=params) -# assert issubclass(type(params_fitted), -# FittedContinuousMultivariate), \ -# f"{name} is not fitted to a child class of " \ -# f"FittedContinuousMultivariate." -# -# # testing we can fit distribution using tuple object. -# tuple_fitted = dist.fit(params=params.to_tuple) -# assert issubclass(type(tuple_fitted), -# FittedContinuousMultivariate), \ -# f"{name} is not fitted to a child class of " \ -# f"FittedContinuousMultivariate." -# -# # testing for errors if incorrect params object provided -# with pytest.raises( -# TypeError, -# match=f"if params provided, must be a " -# f"{dist._params_obj} type or tuple of length " -# f"{dist.num_params}"): -# dist.fit(params=range(1000)) - +@pytest.mark.test_local_only +def test_fit_to_data(mvt_continuous_data, mvt_discrete_data, + pd_mvt_continuous_data, pd_mvt_discrete_data, + mv_dists_to_test): + """Testing we can fit multivariate distributions to data.""" + for name in mv_dists_to_test: + dist = eval(name) + # fitting to both continuous and discrete data, + # in both numpy and pandas format + for method in dist._DATA_FIT_METHODS: + for data in (mvt_continuous_data, pd_mvt_continuous_data, + mvt_discrete_data, pd_mvt_discrete_data): + if isinstance(data, np.ndarray): + d: int = data.shape[1] + else: + d: int = len(data.columns) + + try: + # fitting to data + fitted = dist.fit(data=data, method=method) + except RuntimeError: + continue + + # testing fitted to correct type + assert issubclass(type(fitted), FittedContinuousMultivariate + ), f"{name} is not fitted to a child " \ + f"class of FittedContinuousMultivariate." + + # testing parameters object + params = fitted.params + assert issubclass(type(params), Params), \ + f"{name} fitted parameters are not a child class of " \ + f"Params." + assert params.name == name, \ + f"{name} fitted parameters is not the correct type." + assert len(params) > 0, \ + f"{name} fitted parameter object is empty." + + vector_attributes: tuple = ('loc', 'mean', 'gamma') + matrix_attributes: tuple = ('cov', 'corr') + scale_attributes: tuple = ('dof', 'chi', 'psi', + 'lamb', 'theta') + to_obj_attributes: tuple = ('dict', 'tuple', 'list') + + for vect_str in vector_attributes: + if vect_str in dir(params): + vect = eval(f'params.{vect_str}') + assert isinstance(vect, np.ndarray), \ + f"{vect_str} fitted parameter is not an array " \ + f"for {name}." + assert vect.size == d, \ + f"{vect_str} fitted parameter does not contain " \ + f"the correct number of elements for {name}." + assert vect.shape == (d, 1), \ + f"{vect_str} fitted parameter is not of {(d, 1)}" \ + f" shape for {name}." + assert np.isnan(vect).sum() == 0, \ + f"{vect_str} fitted parameter contains nan " \ + f"values for {name}." + + for mat_str in matrix_attributes: + if mat_str in dir(params): + mat = eval(f'params.{mat_str}') + assert isinstance(mat, np.ndarray), \ + f"{mat_str} fitted parameter is not an array " \ + f"for {name}." + assert mat.shape == (d, d), \ + f"{mat_str} fitted parameter is not of {(d, d)} " \ + f"shape for {name}." + assert np.isnan(mat).sum() == 0, \ + f"{mat_str} fitted parameter contains nan " \ + f"values for {name}." + + for scale_str in scale_attributes: + if scale_str in dir(params): + scale = eval(f'params.{scale_str}') + assert (isinstance(scale, float) + or isinstance(scale, int)), \ + f"{scale_str} fitted parameter is not a scalar " \ + f"value for {name}." + assert not np.isnan(scale),\ + f"{scale_str} fitted parameter is nan for {name}." + + for obj_str in to_obj_attributes: + assert f'to_{obj_str}' in dir(params), \ + f"to_{obj_str} attribute does not exist for {name}" + obj_target_type = eval(obj_str) + obj = eval(f'params.to_{obj_str}') + assert isinstance(obj, obj_target_type), \ + f"to_{obj_str} attribute does not return a " \ + f"{obj_target_type} for {name}." + assert len(obj) == len(params), \ + f"to_{obj_str} attribute does not contain the " \ + f"correct number of parameters for {name}." + + # testing we can fit distribution using parameters object. + params_fitted = dist.fit(params=params) + assert issubclass(type(params_fitted), + FittedContinuousMultivariate), \ + f"{name} is not fitted to a child class of " \ + f"FittedContinuousMultivariate." + + # testing we can fit distribution using tuple object. + tuple_fitted = dist.fit(params=params.to_tuple) + assert issubclass(type(tuple_fitted), + FittedContinuousMultivariate), \ + f"{name} is not fitted to a child class of " \ + f"FittedContinuousMultivariate." + + # testing for errors if incorrect params object provided + with pytest.raises( + TypeError, + match=f"if params provided, must be a " + f"{dist._params_obj} type or tuple of length " + f"{dist.num_params}"): + dist.fit(params=range(1000)) + + +@pytest.mark.test_local_only def test_prefit_logpdf_pdf_cdf_mc_cdfs( 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 pre-fit multivariate distributions.""" - print("\nTesting logpdf, pdf, cdf and mc-cdf functions") eps: float = 10 ** -5 num_generate: int = 10 cdf_num: int = 10 @@ -201,7 +200,6 @@ def test_prefit_logpdf_pdf_cdf_mc_cdfs( def test_prefit_rvs(mv_dists_to_test, params_2d, mvt_continuous_data): """Testing the rvs functions of pre-fit multivariate distributions.""" - print("\nTesting the rvs") for name in mv_dists_to_test: dist, _, params = get_dist(name, params_2d, mvt_continuous_data) for size in (1, 2, 5, 101): @@ -226,7 +224,6 @@ def test_prefit_scalars(mvt_continuous_data, mvt_discrete_data, mv_dists_to_test, params_2d): """Testing the likelihood, loglikelihood, AIC and BIC functions of multivariate pre-fit distributions.""" - print("\nTesting scalars") for name in mv_dists_to_test: dist, _, params = get_dist(name, params_2d, mvt_continuous_data) for func_str in ('likelihood', 'loglikelihood', 'aic', 'bic'): @@ -264,7 +261,6 @@ def test_prefit_scalars(mvt_continuous_data, mvt_discrete_data, def test_prefit_plots(params_2d, params_3d, mvt_continuous_data): """Testing the marginal_pairplot, pdf_plot, cdf_plot and mc_cdf_plot methods of pre-fit 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, @@ -296,7 +292,6 @@ def test_prefit_plots(params_2d, params_3d, mvt_continuous_data): def test_prefit_names(mv_dists_to_test): """Testing that name of pre-fit multivariate distributions is a string.""" - print("\nTesting name") for name in mv_dists_to_test: dist = eval(name) assert isinstance(dist.name, str), f"name of {name} is not a string." @@ -305,7 +300,6 @@ def test_prefit_names(mv_dists_to_test): def test_prefit_integers(mv_dists_to_test, params_2d, mvt_continuous_data): """Testing the num_params and num_scalar_params of pre-fit multivariate distributions.""" - print("\nTesting integers") for name in mv_dists_to_test: dist, _, params = get_dist(name, params_2d, mvt_continuous_data) diff --git a/sklarpy/tests/univariate/test_fitted_dists.py b/sklarpy/tests/univariate/test_fitted_dists.py index dd6d606..ff2254e 100644 --- a/sklarpy/tests/univariate/test_fitted_dists.py +++ b/sklarpy/tests/univariate/test_fitted_dists.py @@ -7,14 +7,13 @@ import matplotlib.pyplot as plt from sklarpy.tests.univariate.helpers import get_data, get_fitted_dict -from sklarpy._utils import SaveError, FitError +from sklarpy.utils._errors import SaveError, FitError from sklarpy.univariate import distributions_map from sklarpy.univariate._fitted_dists import FittedContinuousUnivariate, \ FittedDiscreteUnivariate def test_fitted_pdfs(discrete_data, continuous_data, dists_to_test): """Testing the pdf functions of fitted univariate distributions.""" - print("\nTesting pdfs") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -46,7 +45,6 @@ def test_fitted_pdfs(discrete_data, continuous_data, dists_to_test): def test_fitted_cdfs(discrete_data, continuous_data, dists_to_test): """Testing the cdf functions of fitted univariate distributions.""" - print("\nTesting cdfs") eps: float = 10 ** -5 for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) @@ -104,7 +102,6 @@ def test_fitted_cdfs(discrete_data, continuous_data, dists_to_test): def test_fitted_ppfs(uniform_data, discrete_data, continuous_data, dists_to_test): """Testing the ppf functions of fitted univariate distributions.""" - print("\nTesting ppfs") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -136,7 +133,6 @@ def test_fitted_ppfs(uniform_data, discrete_data, continuous_data, def test_fitted_supports(discrete_data, continuous_data, dists_to_test): """Testing the support functions of fitted univariate distributions.""" - print("\nTesting supports") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -170,7 +166,6 @@ def test_fitted_supports(discrete_data, continuous_data, dists_to_test): def test_fitted_rvs(discrete_data, continuous_data, dists_to_test): """Testing the rvs functions of fitted univariate distributions.""" - print("\nTesting rvs") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -202,7 +197,6 @@ def test_fitted_rvs(discrete_data, continuous_data, dists_to_test): def test_fitted_logpdfs(discrete_data, continuous_data, dists_to_test): """Testing the log-pdf functions of all fitted univariate distributions.""" - print("\nTesting logpdfs") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -231,7 +225,6 @@ def test_fitted_logpdfs(discrete_data, continuous_data, dists_to_test): def test_fitted_scalars(discrete_data, continuous_data, dists_to_test): """Testing the likelihood, loglikelihood, AIC, BIC and SSE functions of fitted distributions.""" - print("\nTesting scalars") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -259,7 +252,6 @@ def test_fitted_scalars(discrete_data, continuous_data, dists_to_test): def test_fitted_gofs(discrete_data, continuous_data, dists_to_test): """Testing the gof functions of fitted univariate distributions.""" - print("\nTesting gofs") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -280,7 +272,6 @@ def test_fitted_gofs(discrete_data, continuous_data, dists_to_test): def test_fitted_plots(discrete_data, continuous_data, dists_to_test): """Testing the plot functions of fitted univariate distributions.""" - print("\nTesting plots") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -297,7 +288,6 @@ def test_fitted_plots(discrete_data, continuous_data, dists_to_test): def test_fitted_saves(discrete_data, continuous_data, dists_to_test): """Testing the save functions of all fitted univariate distributions.""" - print("\nTesting saves") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -319,7 +309,6 @@ def test_fitted_saves(discrete_data, continuous_data, dists_to_test): def test_fitted_names(discrete_data, continuous_data, dists_to_test): """Testing the names functions of fitted univariate distributions.""" - print("\nTesting names") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -338,7 +327,6 @@ def test_fitted_names(discrete_data, continuous_data, dists_to_test): def test_fitted_summaries(discrete_data, continuous_data, dists_to_test): """Testing the summary functions of fitted univariate distributions.""" - print("\nTesting summaries") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -362,7 +350,6 @@ def test_fitted_summaries(discrete_data, continuous_data, dists_to_test): def test_fitted_domains(discrete_data, continuous_data, dists_to_test): """Testing the fitted domain functions of fitted univariate distributions. """ - print("\nTesting domains") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -401,7 +388,6 @@ def test_fitted_domains(discrete_data, continuous_data, dists_to_test): def test_fitted_fitted_to_data(discrete_data, continuous_data, dists_to_test): """Testing the fitted to data functions of fitted univariate distributions. """ - print("\nTesting fitted to data") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -430,7 +416,6 @@ def test_fitted_integers(discrete_data, continuous_data, dists_to_test): """Testing the num_params and fitted_num_data_points functions of fitted univariate distributions. """ - print("\nTesting integers") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -469,7 +454,6 @@ def test_fitted_continuous_or_discretes(discrete_data, continuous_data, dists_to_test): """Testing the continuous_or_discrete functions of fitted univariate distributions.""" - print("\nTesting continuous or discrete") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: diff --git a/sklarpy/tests/univariate/test_prefit_dists.py b/sklarpy/tests/univariate/test_prefit_dists.py index b5e8b82..54c4688 100644 --- a/sklarpy/tests/univariate/test_prefit_dists.py +++ b/sklarpy/tests/univariate/test_prefit_dists.py @@ -12,7 +12,6 @@ def test_correct_type(): """Testing distributions are all SklarPy objects.""" - print("\nTesting correct type") for name in distributions_map['all']: dist = eval(name) assert issubclass(type(dist), PreFitUnivariateBase), \ @@ -21,7 +20,6 @@ def test_correct_type(): def test_fit_to_data(discrete_data, continuous_data, dists_to_test): """Testing we can fit distributions to data.""" - print("\nTesting fit to data") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) target_fit = get_target_fit(name, continuous_data, discrete_data) @@ -47,7 +45,6 @@ def test_fit_to_data(discrete_data, continuous_data, dists_to_test): def test_prefit_name(): """Testing the name of pre-fit distributions is a string.""" - print("\nTesting name") for name in distributions_map['all']: dist = eval(name) assert isinstance(dist.name, str), f"name of {name} is not a string." @@ -55,7 +52,6 @@ def test_prefit_name(): def test_prefit_continuous_or_parametric(): """Testing dists are continuous or discrete.""" - print("\nTesting continuous or discrete") for name in distributions_map['all']: dist = eval(name) s: str = dist.continuous_or_discrete @@ -68,7 +64,6 @@ def test_prefit_continuous_or_parametric(): def test_fit_to_params(discrete_data, continuous_data, dists_to_test): """Testing we can fit distributions to user specified parameters """ - print("\nTesting fit to params") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) target_fit = get_target_fit(name, continuous_data, discrete_data) @@ -94,7 +89,6 @@ def test_fit_to_params(discrete_data, continuous_data, dists_to_test): def test_prefit_pdfs(discrete_data, continuous_data, dists_to_test): """Testing the pdf functions of pre-fit distributions.""" - print("\nTesting pdfs") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -132,7 +126,6 @@ def test_prefit_pdfs(discrete_data, continuous_data, dists_to_test): def test_prefit_cdfs(discrete_data, continuous_data, dists_to_test): """Testing the cdf functions of pre-fit distributions.""" - print("\nTesting cdfs") eps: float = 10 ** -5 for name in dists_to_test: @@ -197,7 +190,6 @@ def test_prefit_cdfs(discrete_data, continuous_data, dists_to_test): def test_prefit_ppfs(uniform_data, discrete_data, continuous_data, dists_to_test): """Testing the ppf functions of pre-fit distributions.""" - print("\nTesting ppfs") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -237,7 +229,6 @@ def test_prefit_ppfs(uniform_data, discrete_data, continuous_data, def test_prefit_supports(discrete_data, continuous_data, dists_to_test): """Testing the support functions of pre-fit distributions.""" - print("\nTesting supports") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -275,7 +266,6 @@ def test_prefit_supports(discrete_data, continuous_data, dists_to_test): def test_prefit_rvs(discrete_data, continuous_data, dists_to_test): """Testing the rvs functions of pre-fit distributions.""" - print("\nTesting rvs") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -319,7 +309,6 @@ def test_prefit_rvs(discrete_data, continuous_data, dists_to_test): def test_prefit_logpdfs(discrete_data, continuous_data, dists_to_test): """Testing the log-pdf functions of pre-fit distributions.""" - print("\nTesting logpdfs") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -354,7 +343,6 @@ def test_prefit_logpdfs(discrete_data, continuous_data, def test_prefit_scalars(discrete_data, continuous_data, dists_to_test): """Testing the likelihood, loglikelihood, AIC, BIC and SSE functions of pre-fit distributions.""" - print("\nTesting scalars") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -388,7 +376,6 @@ def test_prefit_scalars(discrete_data, continuous_data, dists_to_test): def test_prefit_gofs(discrete_data, continuous_data, dists_to_test): """Testing the gof functions of pre-fit distributions""" - print("\nTesting gofs") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: @@ -416,7 +403,6 @@ def test_prefit_gofs(discrete_data, continuous_data, dists_to_test): def test_prefit_plots(discrete_data, continuous_data, dists_to_test): """Testing the plot functions of pre-fit distributions""" - print("\nTesting plots") for name in dists_to_test: data: np.ndarray = get_data(name, continuous_data, discrete_data) try: diff --git a/sklarpy/tests/univariate/test_univariate_fitter.py b/sklarpy/tests/univariate/test_univariate_fitter.py index fca5edd..1af12d8 100644 --- a/sklarpy/tests/univariate/test_univariate_fitter.py +++ b/sklarpy/tests/univariate/test_univariate_fitter.py @@ -6,20 +6,17 @@ from sklarpy.univariate import UnivariateFitter, distributions_map from sklarpy.univariate._fitted_dists import FittedContinuousUnivariate, \ FittedDiscreteUnivariate -from sklarpy._utils import FitError, SignificanceError +from sklarpy.utils._errors import FitError, SignificanceError def test_init(discrete_data, continuous_data): """Testing whether UnivariateFitter initialises without errors""" - print("\nTesting init") for data in (discrete_data, continuous_data): fitter = UnivariateFitter(data) def test_fit(discrete_data, continuous_data): """Testing the fit method of UnivariateFitter.""" - print("\nTesting fit") - for data in (discrete_data, continuous_data): # fitting with default args default_fitter = UnivariateFitter(data).fit() @@ -72,7 +69,6 @@ def test_fit(discrete_data, continuous_data): def test_get_summary(discrete_data, continuous_data): """Testing the get_summary method of UnivariateFitter""" - print("\nTesting get-summary") for ufitter in (UnivariateFitter(discrete_data), UnivariateFitter(continuous_data)): # testing not implemented when not fit @@ -106,7 +102,6 @@ def test_get_summary(discrete_data, continuous_data): def test_get_best(discrete_data, continuous_data): """Testing the get_best method of UnivariateFitter""" - print("\nTesting get-best") for dtype, ufitter in {'discrete': UnivariateFitter(discrete_data), 'continuous': UnivariateFitter(continuous_data) }.items(): @@ -118,7 +113,6 @@ def test_get_best(discrete_data, continuous_data): ufitter.fit() - # checking basic functionality works best = ufitter.get_best() target_dtype = eval(f'Fitted{dtype.title()}Univariate') @@ -135,7 +129,6 @@ def test_get_best(discrete_data, continuous_data): def test_plot(discrete_data, continuous_data): """Testing the plot method of UnivariateFitter""" - print("\nTesting plot") for dtype, ufitter in {'discrete': UnivariateFitter(discrete_data), 'continuous': UnivariateFitter(continuous_data) }.items(): @@ -153,7 +146,6 @@ def test_plot(discrete_data, continuous_data): def test_fitted_distributions(discrete_data, continuous_data): """Testing the fitted_distributions property of UnivariateFitter""" - print("\nTesting distributions") for dtype, ufitter in {'discrete': UnivariateFitter(discrete_data), 'continuous': UnivariateFitter(continuous_data) }.items(): diff --git a/sklarpy/tests/_utils/__init__.py b/sklarpy/tests/utils/__init__.py similarity index 100% rename from sklarpy/tests/_utils/__init__.py rename to sklarpy/tests/utils/__init__.py diff --git a/sklarpy/tests/_utils/conftest.py b/sklarpy/tests/utils/conftest.py similarity index 100% rename from sklarpy/tests/_utils/conftest.py rename to sklarpy/tests/utils/conftest.py diff --git a/sklarpy/tests/_utils/test_object_printing.py b/sklarpy/tests/utils/test_object_printing.py similarity index 92% rename from sklarpy/tests/_utils/test_object_printing.py rename to sklarpy/tests/utils/test_object_printing.py index c15cc81..2b62408 100644 --- a/sklarpy/tests/_utils/test_object_printing.py +++ b/sklarpy/tests/utils/test_object_printing.py @@ -7,7 +7,6 @@ def test_print_full(): """Testing print_full function.""" - print("\nTesting print full") num: int = 10 ** 5 arrA: np.ndarray = np.linspace(0, 1000, num) diff --git a/sklarpy/tests/_utils/test_serialize.py b/sklarpy/tests/utils/test_serialize.py similarity index 93% rename from sklarpy/tests/_utils/test_serialize.py rename to sklarpy/tests/utils/test_serialize.py index c6d78c7..ea9eae5 100644 --- a/sklarpy/tests/_utils/test_serialize.py +++ b/sklarpy/tests/utils/test_serialize.py @@ -5,12 +5,11 @@ from sklarpy import load from sklarpy.univariate import normal, poisson -from sklarpy._utils import LoadError +from sklarpy.utils._errors import LoadError def test_load(): """Testing SklarPy's load function.""" - print("\nTesting load") # Testing if loading a non-existent file raises an error with pytest.raises(LoadError): load(r'dont\put\a\file\here\test.pickle') @@ -19,7 +18,6 @@ def test_load(): def test_univariate_serialization(continuous_data, discrete_data): """Testing serialization and deserialization of a univariate distribution. """ - print("\nTesting univariate serialization") for dist, data in {normal: continuous_data, poisson: discrete_data }.items(): # fitting distribution diff --git a/sklarpy/univariate/_distributions/_base_gen.py b/sklarpy/univariate/_distributions/_base_gen.py index ad8d35a..7d9a7c5 100644 --- a/sklarpy/univariate/_distributions/_base_gen.py +++ b/sklarpy/univariate/_distributions/_base_gen.py @@ -3,7 +3,8 @@ import scipy.optimize import scipy.integrate -from sklarpy._utils import check_params, FitError +from sklarpy.utils._errors import FitError +from sklarpy.utils._input_handlers import check_params __all__ = ['base_gen'] diff --git a/sklarpy/univariate/_distributions/_discrete_empirical.py b/sklarpy/univariate/_distributions/_discrete_empirical.py index 01b9847..4c037d7 100644 --- a/sklarpy/univariate/_distributions/_discrete_empirical.py +++ b/sklarpy/univariate/_distributions/_discrete_empirical.py @@ -6,7 +6,7 @@ from sklarpy.univariate._distributions._numerical_wrappers import \ NumericalWrappers -from sklarpy._utils import DiscreteError +from sklarpy.utils._errors import DiscreteError __all__ = ['discrete_empirical_fit'] diff --git a/sklarpy/univariate/_fitted_dists.py b/sklarpy/univariate/_fitted_dists.py index 372bd93..d63b74c 100644 --- a/sklarpy/univariate/_fitted_dists.py +++ b/sklarpy/univariate/_fitted_dists.py @@ -5,7 +5,7 @@ import pandas as pd import matplotlib.pyplot as plt -from sklarpy._utils import Savable +from sklarpy.utils._serialize import Savable __all__ = ['FittedDiscreteUnivariate', 'FittedContinuousUnivariate'] diff --git a/sklarpy/univariate/_prefit_dists.py b/sklarpy/univariate/_prefit_dists.py index 29d4aad..e70369a 100644 --- a/sklarpy/univariate/_prefit_dists.py +++ b/sklarpy/univariate/_prefit_dists.py @@ -9,8 +9,9 @@ from sklarpy.univariate._goodness_of_fit import continuous_gof, discrete_gof from sklarpy.univariate._inverse_transform import inverse_transform -from sklarpy._utils import univariate_num_to_array, check_params, \ - check_univariate_data, FitError, check_array_datatype +from sklarpy.utils._input_handlers import univariate_num_to_array, \ + check_univariate_data, check_array_datatype, check_params +from sklarpy.utils._errors import FitError from sklarpy.univariate._fitted_dists import FittedContinuousUnivariate, \ FittedDiscreteUnivariate from sklarpy.univariate._distributions import discrete_empirical_fit, \ diff --git a/sklarpy/univariate/univariate_fitter.py b/sklarpy/univariate/univariate_fitter.py index 8f71608..c1ab865 100644 --- a/sklarpy/univariate/univariate_fitter.py +++ b/sklarpy/univariate/univariate_fitter.py @@ -13,8 +13,10 @@ from sklarpy.univariate.distributions import * from sklarpy.univariate.distributions_map import distributions_map -from sklarpy._utils import check_univariate_data, check_array_datatype, \ - FitError, SignificanceError, Savable +from sklarpy.utils._errors import SignificanceError, FitError +from sklarpy.utils._input_handlers import check_univariate_data, \ + check_array_datatype +from sklarpy.utils._serialize import Savable __all__ = ['UnivariateFitter'] diff --git a/sklarpy/_utils/README.md b/sklarpy/utils/README.md similarity index 100% rename from sklarpy/_utils/README.md rename to sklarpy/utils/README.md diff --git a/sklarpy/utils/__init__.py b/sklarpy/utils/__init__.py new file mode 100644 index 0000000..3e415fd --- /dev/null +++ b/sklarpy/utils/__init__.py @@ -0,0 +1,11 @@ +# Useful functions/objects and values used throughout SklarPy +from sklarpy.utils._errors import SignificanceError, DiscreteError, \ + FitError, SaveError, LoadError, DistributionError +from sklarpy.utils._input_handlers import univariate_num_to_array, \ + check_params, check_univariate_data, check_array_datatype, \ + check_multivariate_data, get_mask +from sklarpy.utils._type_keeper import TypeKeeper +from sklarpy.utils._iterator import get_iterator +from sklarpy.utils._copy import Copyable +from sklarpy.utils._not_implemented import NotImplementedBase +from sklarpy.utils._params import Params diff --git a/sklarpy/_utils/_copy.py b/sklarpy/utils/_copy.py similarity index 100% rename from sklarpy/_utils/_copy.py rename to sklarpy/utils/_copy.py diff --git a/sklarpy/_utils/_errors.py b/sklarpy/utils/_errors.py similarity index 100% rename from sklarpy/_utils/_errors.py rename to sklarpy/utils/_errors.py diff --git a/sklarpy/_utils/_input_handlers.py b/sklarpy/utils/_input_handlers.py similarity index 100% rename from sklarpy/_utils/_input_handlers.py rename to sklarpy/utils/_input_handlers.py diff --git a/sklarpy/_utils/_iterator.py b/sklarpy/utils/_iterator.py similarity index 100% rename from sklarpy/_utils/_iterator.py rename to sklarpy/utils/_iterator.py diff --git a/sklarpy/_utils/_not_implemented.py b/sklarpy/utils/_not_implemented.py similarity index 100% rename from sklarpy/_utils/_not_implemented.py rename to sklarpy/utils/_not_implemented.py diff --git a/sklarpy/_utils/_object_printing.py b/sklarpy/utils/_object_printing.py similarity index 100% rename from sklarpy/_utils/_object_printing.py rename to sklarpy/utils/_object_printing.py diff --git a/sklarpy/_utils/_params.py b/sklarpy/utils/_params.py similarity index 98% rename from sklarpy/_utils/_params.py rename to sklarpy/utils/_params.py index 45582d5..2c7d65b 100644 --- a/sklarpy/_utils/_params.py +++ b/sklarpy/utils/_params.py @@ -1,7 +1,7 @@ # Contains a base class for all distribution parameter objects from typing import Union -from sklarpy._utils._serialize import Savable +from sklarpy.utils._serialize import Savable __all__ = ['Params'] diff --git a/sklarpy/_utils/_serialize.py b/sklarpy/utils/_serialize.py similarity index 98% rename from sklarpy/_utils/_serialize.py rename to sklarpy/utils/_serialize.py index c0929d6..4e1142a 100644 --- a/sklarpy/_utils/_serialize.py +++ b/sklarpy/utils/_serialize.py @@ -3,7 +3,7 @@ import os from pathlib import Path -from sklarpy._utils._errors import LoadError, SaveError +from sklarpy.utils._errors import LoadError, SaveError __all__ = ['load', 'Savable'] diff --git a/sklarpy/_utils/_type_keeper.py b/sklarpy/utils/_type_keeper.py similarity index 100% rename from sklarpy/_utils/_type_keeper.py rename to sklarpy/utils/_type_keeper.py diff --git a/tox.ini b/tox.ini index da6c6d1..3ded63e 100644 --- a/tox.ini +++ b/tox.ini @@ -4,7 +4,9 @@ skip_missing_interpreters = true [testenv] deps=pytest -commands=pytest -s +commands= +; pytest -sv -m "test_local_only" + pytest -sv -m "not test_local_only" [gh-actions] python =