# Path: dipde/internals/utilities.py
# def discretize_if_needed(curr_input):
#     
#     if isinstance(curr_input, (sps._distn_infrastructure.rv_frozen,)):
#         vals, probs = descretize(curr_input)
#     elif isinstance(curr_input, (tuple, list)) and len(curr_input) == 2 and isinstance(curr_input[0], (sps._distn_infrastructure.rv_frozen,)) and isinstance(curr_input[1], (int,)):
#         vals, probs = descretize(curr_input[0], N=curr_input[1])
#     elif isinstance(curr_input, (float, int)):
#         vals, probs = np.array([float(curr_input)]), np.array([1])
#     elif isinstance(curr_input, (tuple, list)) and len(curr_input) == 2 and isinstance(curr_input[0], (tuple, list, np.ndarray)) and isinstance(curr_input[1], (tuple, list, np.ndarray)):
#         vals, probs = map(np.array, curr_input)
#     elif isinstance(curr_input, (sps._distn_infrastructure.rv_discrete, )):
#         return curr_input
#     elif isinstance(curr_input, (dict,)):
#         if curr_input['distribution'] == 'delta':
#             vals, probs = np.array([curr_input['weight']]), np.array([1.])
#         else:
#             raise NotImplementedError # pragma: no cover
#     elif isinstance(curr_input,str):
#         return discretize_if_needed(json.loads(curr_input))
#     
#     else:
#         
#         raise ValueError("Unrecognized curr_input format: input=%s" % (curr_input,)) # pragma: no cover
#         
#     # Double-check inputs with more helpful error messages:
#     try:
#         for val in probs:
#             assert val >= 0
#     except:                                                                         # pragma: no cover
#         raise ValueError("Probability values must be positive: probs=%s" % (probs,))# pragma: no cover
#     
#     assert_probability_mass_conserved(probs, 1e-15)
#     if len(vals) == len(probs):
#         pass
#     elif len(vals) == len(probs)+1:
#         vals = (np.array(vals[1:]) + np.array(vals[:-1]))/2
#     else:                                                                         # pragma: no cover
#         raise ValueError("Length of vals and probs not consistent with a probability distribution")
#     
#     return sps.rv_discrete(values=(vals, probs))


import json
import scipy.stats as sps
from dipde.internals.utilities import discretize_if_needed

def test_continuous_no_N():
    discretize_if_needed(sps.norm(loc=2, scale=1.5))
 
def test_continuous_N():
    discretize_if_needed((sps.norm(loc=2, scale=1.5),25))
    
def test_scalar():
    discretize_if_needed(.02)
 
def test_discrete_rv():
#     sps.rv_discrete(values=([.02],[1]), )
    discretize_if_needed(sps.rv_discrete(values=([.02],[1])))
 
def test_dist_specified():
    discretize_if_needed(((0,1,2,3),(.25,.25,.25,.25)))
    
def test_dist_xk_pk_off_by_one():
    discretize_if_needed(((0,1,2,3),(.5,.25,.25)))
    
def test_dist_dict_delta():
    discretize_if_needed({'distribution':'delta', 'weight':.5})
    
def test_dist_json_delta():

# Path: dipde/internals/utilities.py
# def discretize_if_needed(curr_input):
#     
#     if isinstance(curr_input, (sps._distn_infrastructure.rv_frozen,)):
#         vals, probs = descretize(curr_input)
#     elif isinstance(curr_input, (tuple, list)) and len(curr_input) == 2 and isinstance(curr_input[0], (sps._distn_infrastructure.rv_frozen,)) and isinstance(curr_input[1], (int,)):
#         vals, probs = descretize(curr_input[0], N=curr_input[1])
#     elif isinstance(curr_input, (float, int)):
#         vals, probs = np.array([float(curr_input)]), np.array([1])
#     elif isinstance(curr_input, (tuple, list)) and len(curr_input) == 2 and isinstance(curr_input[0], (tuple, list, np.ndarray)) and isinstance(curr_input[1], (tuple, list, np.ndarray)):
#         vals, probs = map(np.array, curr_input)
#     elif isinstance(curr_input, (sps._distn_infrastructure.rv_discrete, )):
#         return curr_input
#     elif isinstance(curr_input, (dict,)):
#         if curr_input['distribution'] == 'delta':
#             vals, probs = np.array([curr_input['weight']]), np.array([1.])
#         else:
#             raise NotImplementedError # pragma: no cover
#     elif isinstance(curr_input,str):
#         return discretize_if_needed(json.loads(curr_input))
#     
#     else:
#         
#         raise ValueError("Unrecognized curr_input format: input=%s" % (curr_input,)) # pragma: no cover
#         
#     # Double-check inputs with more helpful error messages:
#     try:
#         for val in probs:
#             assert val >= 0
#     except:                                                                         # pragma: no cover
#         raise ValueError("Probability values must be positive: probs=%s" % (probs,))# pragma: no cover
#     
#     assert_probability_mass_conserved(probs, 1e-15)
#     if len(vals) == len(probs):
#         pass
#     elif len(vals) == len(probs)+1:
#         vals = (np.array(vals[1:]) + np.array(vals[:-1]))/2
#     else:                                                                         # pragma: no cover
#         raise ValueError("Length of vals and probs not consistent with a probability distribution")
#     
#     return sps.rv_discrete(values=(vals, probs))


# Path: dipde/test/test_distribution_constuctor.py
import json
import scipy.stats as sps
from dipde.internals.utilities import discretize_if_needed


def test_continuous_no_N():
    discretize_if_needed(sps.norm(loc=2, scale=1.5))
 
def test_continuous_N():
    discretize_if_needed((sps.norm(loc=2, scale=1.5),25))
    
def test_scalar():
    discretize_if_needed(.02)
 
def test_discrete_rv():
#     sps.rv_discrete(values=([.02],[1]), )
    discretize_if_needed(sps.rv_discrete(values=([.02],[1])))
 
def test_dist_specified():
    discretize_if_needed(((0,1,2,3),(.25,.25,.25,.25)))
    
def test_dist_xk_pk_off_by_one():
    discretize_if_needed(((0,1,2,3),(.5,.25,.25)))
    
def test_dist_dict_delta():
    discretize_if_needed({'distribution':'delta', 'weight':.5})
    
def test_dist_json_delta():

    discretize_if_needed(json.dumps({'distribution':'delta', 'weight':.5}))