# Path: pyglm/utils/avg_dicts.py
# def average_list_of_dicts(smpls):
#     """ Average a list of dictionaries, e.g. a list of samples from
#         the MCMC loop. The dictionary elements may themselves be
#         dictionaries, so work recursively and only average the leaves.
#     """
#     N_smpls = len(smpls)
#     import copy
#     avg = copy.deepcopy(smpls[0])
# 
#     def inc_avg(avg, smpl):
#         """ Helper to recrusively add to the average
#         """
#         if isinstance(smpl, dict):
#             for (key,val) in smpl.items():
#                 if isinstance(val, dict) or \
#                   isinstance(val, list):
#                   # Recurse if the entry is another dict
#                     avg[key] = inc_avg(avg[key], val)
#                 elif isinstance(val, np.ndarray):
#                     # Otherwise increment the value
#                     avg[key] = avg[key].astype(np.float) + \
#                                val.astype(np.float)
#                 else:
#                     # Who knows if this will work without casting!
#                     avg[key] += val
#         
#         elif isinstance(smpl, list):
#             for (i,val) in enumerate(smpl):
#                 if isinstance(val, list) or \
#                    isinstance(val, dict):
#                     # Recurse if the entry is another iterable
#                     avg[i] = inc_avg(avg[i], val)
#                 elif isinstance(val, np.ndarray):
#                     avg[i] = avg[i].astype(np.float) + \
#                              val.astype(np.float)
#                 else:
#                     # Who knows if this will work without casting!
#                     avg[i] += float(val)
#         return avg
# 
#     for smpl in smpls[1:]:
#         avg = inc_avg(avg, smpl)
# 
#     def norm_avg(avg, N_smpls):
#         """ Normalize the average by dividing by N_smpls
#         """
#         if isinstance(avg, dict):
#             for (key,val) in avg.items():
#                 if isinstance(val, dict) or \
#                    isinstance(val, list):
#                     avg[key] = norm_avg(val, N_smpls)
#                 else:
#                     avg[key] /= float(N_smpls)
#         elif isinstance(avg, list):
#             for (i,val) in enumerate(avg):
#                 if isinstance(val, list) or \
#                    isinstance(val, dict):
#                     # Recurse if the entry is another iterable
#                     avg[i] = norm_avg(val, N_smpls)
#                 else:
#                     avg[i] /= float(N_smpls)
#                         
#         return avg
# 
#     avg = norm_avg(avg, N_smpls)
#     return avg
# 
# def std_list_of_dicts(smpls, avg=None):
#     """ Compute the std of a list of dictionaries, e.g. a list of samples from
#         the MCMC loop. The dictionary elements may themselves be
#         dictionaries, so work recursively and only average the leaves.
#     """
#     N_smpls = len(smpls)
#     var = variance_list_of_dicts(smpls, avg=avg)
# 
#     def sqrt_helper(smpl):
#         """ Helper to recursively take the square root 
#         """
#         if isinstance(smpl, dict):
#             for (key,val) in smpl.items():
#                 if isinstance(val, dict) or \
#                   isinstance(val, list):
#                   # Recurse if the entry is another dict
#                     smpl[key] = sqrt_helper(val)
#                 else:
#                     # Take the square root
#                     smpl[key] = np.sqrt(val)
#         
#         elif isinstance(smpl, list):
#             for (i,val) in enumerate(smpl):
#                 if isinstance(val, list) or \
#                    isinstance(val, dict):
#                     # Recurse if the entry is another iterable
#                     smpl[i] = sqrt_helper(val)
#                 else:
#                     smpl[i] = np.sqrt(val)
#         return smpl
# 
#     std = sqrt_helper(var)
#     return std


import os
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import cPickle
from matplotlib.colors import LinearSegmentedColormap
from pyglm.utils.avg_dicts import average_list_of_dicts, std_list_of_dicts
    from matplotlib.colorbar import ColorbarBase
    from matplotlib.patches import Rectangle
        from matplotlib.patches import Polygon
    from optparse import OptionParser
    from test.synth_harness import initialize_test_harness
                 resdir=None,
                 do_plot_connectivity=True,
                 do_plot_stim_resp=True,
                 do_plot_imp_responses=True,
                 do_plot_firing_rates=True,
                 do_plot_ks=True,
                 do_plot_logpr=True):
    """ Plot the inferred stimulus tuning curves and impulse responses
    """
    if not resdir:
        resdir = '.'

    true_given = x_true is not None and popn_true is not None
    
    # Make sure we have a list of x's
    if not isinstance(x_inf, list):
        x_inf = [x_inf]

    # Evaluate the state for each of the parameter settings
    N_samples = len(x_inf)
    s_inf = []
    for x in x_inf:
        s_inf.append(population.eval_state(x))
    
    s_true = None
    if true_given:
        s_true = popn_true.eval_state(x_true)

    # Average the inferred states
    s_avg = average_list_of_dicts(s_inf)

# Path: pyglm/utils/avg_dicts.py
# def average_list_of_dicts(smpls):
#     """ Average a list of dictionaries, e.g. a list of samples from
#         the MCMC loop. The dictionary elements may themselves be
#         dictionaries, so work recursively and only average the leaves.
#     """
#     N_smpls = len(smpls)
#     import copy
#     avg = copy.deepcopy(smpls[0])
# 
#     def inc_avg(avg, smpl):
#         """ Helper to recrusively add to the average
#         """
#         if isinstance(smpl, dict):
#             for (key,val) in smpl.items():
#                 if isinstance(val, dict) or \
#                   isinstance(val, list):
#                   # Recurse if the entry is another dict
#                     avg[key] = inc_avg(avg[key], val)
#                 elif isinstance(val, np.ndarray):
#                     # Otherwise increment the value
#                     avg[key] = avg[key].astype(np.float) + \
#                                val.astype(np.float)
#                 else:
#                     # Who knows if this will work without casting!
#                     avg[key] += val
#         
#         elif isinstance(smpl, list):
#             for (i,val) in enumerate(smpl):
#                 if isinstance(val, list) or \
#                    isinstance(val, dict):
#                     # Recurse if the entry is another iterable
#                     avg[i] = inc_avg(avg[i], val)
#                 elif isinstance(val, np.ndarray):
#                     avg[i] = avg[i].astype(np.float) + \
#                              val.astype(np.float)
#                 else:
#                     # Who knows if this will work without casting!
#                     avg[i] += float(val)
#         return avg
# 
#     for smpl in smpls[1:]:
#         avg = inc_avg(avg, smpl)
# 
#     def norm_avg(avg, N_smpls):
#         """ Normalize the average by dividing by N_smpls
#         """
#         if isinstance(avg, dict):
#             for (key,val) in avg.items():
#                 if isinstance(val, dict) or \
#                    isinstance(val, list):
#                     avg[key] = norm_avg(val, N_smpls)
#                 else:
#                     avg[key] /= float(N_smpls)
#         elif isinstance(avg, list):
#             for (i,val) in enumerate(avg):
#                 if isinstance(val, list) or \
#                    isinstance(val, dict):
#                     # Recurse if the entry is another iterable
#                     avg[i] = norm_avg(val, N_smpls)
#                 else:
#                     avg[i] /= float(N_smpls)
#                         
#         return avg
# 
#     avg = norm_avg(avg, N_smpls)
#     return avg
# 
# def std_list_of_dicts(smpls, avg=None):
#     """ Compute the std of a list of dictionaries, e.g. a list of samples from
#         the MCMC loop. The dictionary elements may themselves be
#         dictionaries, so work recursively and only average the leaves.
#     """
#     N_smpls = len(smpls)
#     var = variance_list_of_dicts(smpls, avg=avg)
# 
#     def sqrt_helper(smpl):
#         """ Helper to recursively take the square root 
#         """
#         if isinstance(smpl, dict):
#             for (key,val) in smpl.items():
#                 if isinstance(val, dict) or \
#                   isinstance(val, list):
#                   # Recurse if the entry is another dict
#                     smpl[key] = sqrt_helper(val)
#                 else:
#                     # Take the square root
#                     smpl[key] = np.sqrt(val)
#         
#         elif isinstance(smpl, list):
#             for (i,val) in enumerate(smpl):
#                 if isinstance(val, list) or \
#                    isinstance(val, dict):
#                     # Recurse if the entry is another iterable
#                     smpl[i] = sqrt_helper(val)
#                 else:
#                     smpl[i] = np.sqrt(val)
#         return smpl
# 
#     std = sqrt_helper(var)
#     return std


# Path: pyglm/plotting/plot_results.py
import os
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import cPickle
from matplotlib.colors import LinearSegmentedColormap
from pyglm.utils.avg_dicts import average_list_of_dicts, std_list_of_dicts
    from matplotlib.colorbar import ColorbarBase
    from matplotlib.patches import Rectangle
        from matplotlib.patches import Polygon
    from optparse import OptionParser
    from test.synth_harness import initialize_test_harness

                 resdir=None,
                 do_plot_connectivity=True,
                 do_plot_stim_resp=True,
                 do_plot_imp_responses=True,
                 do_plot_firing_rates=True,
                 do_plot_ks=True,
                 do_plot_logpr=True):
    """ Plot the inferred stimulus tuning curves and impulse responses
    """
    if not resdir:
        resdir = '.'

    true_given = x_true is not None and popn_true is not None
    
    # Make sure we have a list of x's
    if not isinstance(x_inf, list):
        x_inf = [x_inf]

    # Evaluate the state for each of the parameter settings
    N_samples = len(x_inf)
    s_inf = []
    for x in x_inf:
        s_inf.append(population.eval_state(x))
    
    s_true = None
    if true_given:
        s_true = popn_true.eval_state(x_true)

    # Average the inferred states
    s_avg = average_list_of_dicts(s_inf)

    s_std = std_list_of_dicts(s_inf, s_avg)