# Path: elfi/methods/utils.py
# def numpy_to_python_type(data):
#     """Convert numpy data types to python data type for json serialization.
# 
#     Parameters
#     ----------
#     data : dict, required
#         Stores collected data for json
# 
#     """
#     for key, val in data.items():
#         # in data there is keys as 'samples' which is actually a dictionary
#         if isinstance(val, dict):
#             for nested_key, nested_val in val.items():
#                 is_numpy = type(nested_val)
#                 data_type = str(is_numpy)
#                 # check whether the current value has numpy data type
#                 if is_numpy.__module__ == np.__name__:
#                     # it is enough to check that current value's name has one of these sub-strings
#                     # https://docs.scipy.org/doc/numpy-1.13.0/user/basics.types.html
#                     if 'array' in data_type:
#                         data[key][nested_key] = nested_val.tolist()
#                     elif 'int' in data_type:
#                         data[key][nested_key] = int(nested_val)
#                     elif 'float' in data_type:
#                         data[key][nested_key] = float(nested_val)
# 
#         is_numpy = type(val)
#         data_type = str(is_numpy)
#         if is_numpy.__module__ == np.__name__:
#             if 'array' in data_type:
#                 data[key] = val.tolist()
#             elif 'int' in data_type:
#                 data[key] = int(val)
#             elif 'float' in data_type:
#                 data[key] = float(val)
# 
# def sample_object_to_dict(data, elem, skip=''):
#     """Process data from self object to data dictionary to prepare for json serialization.
# 
#     Parameters
#     ----------
#     data : dict, required
#         Stores collected data for json
#     elem : dict, required
#         Default data from Sample object(s)
#     skip : str, optional
#         Some keys in the object should be skipped, such as `outputs` or `populations`. Latter
#         is skipped in case if it is already processed previously.
# 
#     """
#     for key, val in elem.__dict__.items():
#         # skip `outputs` because its values are in `samples` and in `discrepancies`
#         if key in ['outputs', skip]:
#             continue
#         if key == 'meta':
#             for meta_key, meta_val in elem.__dict__[key].items():
#                 data[meta_key] = meta_val
#             continue
#         data[key] = val


import io
import itertools
import logging
import os
import string
import sys
import numpy as np
import elfi.visualization.visualization as vis
        import csv
        import json
        import pickle
from collections import OrderedDict
from matplotlib import pyplot as plt
from elfi.methods.utils import numpy_to_python_type, sample_object_to_dict
        elif kind == 'json':
            with open(fname, 'w') as f:

                data = OrderedDict()

                data['n_samples'] = self.n_samples
                data['discrepancies'] = self.discrepancies
                data['dim'] = self.dim

                # populations key exists in SMC-ABC sampler and contains the history of all
                # inferences with different number of simulations and thresholds
                populations = 'populations'
                if populations in self.__dict__:
                    # setting populations in the following form:
                    # data = {'populations': {'A': dict(), 'B': dict()}, ...}
                    # this helps to save all kind of populations
                    pop_num = string.ascii_letters.upper()[:len(self.__dict__[populations])]
                    data[populations] = OrderedDict()
                    for n, elem in enumerate(self.__dict__[populations]):
                        data[populations][pop_num[n]] = OrderedDict()
                        sample_object_to_dict(data[populations][pop_num[n]], elem)

                    # convert numpy types into python types in populations key
                    for key, val in data[populations].items():
                        numpy_to_python_type(val)

                # skip populations because it was processed previously
                sample_object_to_dict(data, self, skip='populations')

                # convert numpy types into python types

# Path: elfi/methods/utils.py
# def numpy_to_python_type(data):
#     """Convert numpy data types to python data type for json serialization.
# 
#     Parameters
#     ----------
#     data : dict, required
#         Stores collected data for json
# 
#     """
#     for key, val in data.items():
#         # in data there is keys as 'samples' which is actually a dictionary
#         if isinstance(val, dict):
#             for nested_key, nested_val in val.items():
#                 is_numpy = type(nested_val)
#                 data_type = str(is_numpy)
#                 # check whether the current value has numpy data type
#                 if is_numpy.__module__ == np.__name__:
#                     # it is enough to check that current value's name has one of these sub-strings
#                     # https://docs.scipy.org/doc/numpy-1.13.0/user/basics.types.html
#                     if 'array' in data_type:
#                         data[key][nested_key] = nested_val.tolist()
#                     elif 'int' in data_type:
#                         data[key][nested_key] = int(nested_val)
#                     elif 'float' in data_type:
#                         data[key][nested_key] = float(nested_val)
# 
#         is_numpy = type(val)
#         data_type = str(is_numpy)
#         if is_numpy.__module__ == np.__name__:
#             if 'array' in data_type:
#                 data[key] = val.tolist()
#             elif 'int' in data_type:
#                 data[key] = int(val)
#             elif 'float' in data_type:
#                 data[key] = float(val)
# 
# def sample_object_to_dict(data, elem, skip=''):
#     """Process data from self object to data dictionary to prepare for json serialization.
# 
#     Parameters
#     ----------
#     data : dict, required
#         Stores collected data for json
#     elem : dict, required
#         Default data from Sample object(s)
#     skip : str, optional
#         Some keys in the object should be skipped, such as `outputs` or `populations`. Latter
#         is skipped in case if it is already processed previously.
# 
#     """
#     for key, val in elem.__dict__.items():
#         # skip `outputs` because its values are in `samples` and in `discrepancies`
#         if key in ['outputs', skip]:
#             continue
#         if key == 'meta':
#             for meta_key, meta_val in elem.__dict__[key].items():
#                 data[meta_key] = meta_val
#             continue
#         data[key] = val


# Path: elfi/methods/results.py
import io
import itertools
import logging
import os
import string
import sys
import numpy as np
import elfi.visualization.visualization as vis
        import csv
        import json
        import pickle
from collections import OrderedDict
from matplotlib import pyplot as plt
from elfi.methods.utils import numpy_to_python_type, sample_object_to_dict

        elif kind == 'json':
            with open(fname, 'w') as f:

                data = OrderedDict()

                data['n_samples'] = self.n_samples
                data['discrepancies'] = self.discrepancies
                data['dim'] = self.dim

                # populations key exists in SMC-ABC sampler and contains the history of all
                # inferences with different number of simulations and thresholds
                populations = 'populations'
                if populations in self.__dict__:
                    # setting populations in the following form:
                    # data = {'populations': {'A': dict(), 'B': dict()}, ...}
                    # this helps to save all kind of populations
                    pop_num = string.ascii_letters.upper()[:len(self.__dict__[populations])]
                    data[populations] = OrderedDict()
                    for n, elem in enumerate(self.__dict__[populations]):
                        data[populations][pop_num[n]] = OrderedDict()
                        sample_object_to_dict(data[populations][pop_num[n]], elem)

                    # convert numpy types into python types in populations key
                    for key, val in data[populations].items():
                        numpy_to_python_type(val)

                # skip populations because it was processed previously
                sample_object_to_dict(data, self, skip='populations')

                # convert numpy types into python types

                numpy_to_python_type(data)