# Path: jr/utils.py
# def logcenter(center, x=None, inverse=False):
#     # Authors: Jean-Remi King, <jeanremi.king@gmail.com>
#     #          Clement Levrard <clement.levrard@gmail.com>
#     #
#     # License: BSD (3-clause)
#     """
#     Creates a logarithmic scale centered around center, and bounded between
#     [0., 1.] such that:
#         f(0, center) = 0
#         f(1, center) = 1
#         f(center, center) = .5
# 
#     Parameters
#     ----------
#         x : float | np.array | None
#             If float or np.array, 0. < x < 1.
#             If None, set to np.linspace(0., 1., 256).
#             Defaults to None.
#         center : float
#             0. < center < 1.
#     Returns
#     -------
#         y : float | np.array
#     """
# 
#     from numpy import exp, log
#     if x is None:
#         x = np.linspace(0., 1., 256)
#     if center >= 1. or center <= 0.:
#         raise ValueError('center must be between 0 and 1')
#     if center == .5:
#         y = x
#     else:
#         n = 1. / center
#         if inverse is False:
#             y = (exp(2 * log(n - 1) * x) - 1) / (n * (n - 2))
#         else:
#             y = log(x * (n * (n - 2)) + 1) / (2 * log(n - 1))
#     if center > .5:
#         y = 1. - y
#     return y
# 
# Path: jr/stats/base.py
# def median_abs_deviation(x, axis=None):
#     """median absolute deviation"""
#     x = np.asarray(x)
#     # transpose selected axis in front
#     shape = x.shape
#     n_dim = len(shape)
#     axis_ = None
#     if axis is not None:
#         dim_order = np.hstack((axis, np.delete(np.arange(n_dim), axis)))
#         x = np.transpose(x, dim_order)
#         axis_ = 0
# 
#     # compute median
#     center = np.median(x, axis=axis_, keepdims=False)
#     if len(center):
#         center = center[np.newaxis, ...]
# 
#     # compute median absolute deviation from median
#     mad = np.median(np.abs(x - center), axis=axis_)
# 
#     return mad


import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.colors as col
    import itertools
from matplotlib.colors import LinearSegmentedColormap
from ..utils import logcenter
from ..stats import median_abs_deviation
    elif isinstance(color, np.ndarray) and color.ndim == 1:
        color = [color] * len(x)
    means = np.nanmean(y, axis=0)
    sems = np.nanstd(y, axis=0) / np.sqrt(y.shape[0])
    if bin_width is None:
        bin_width = np.diff(x[:2])
    for mean, sem, bin_, this_color in zip(means, sems, x, color):
        options = dict(color=this_color, edgecolor='none', linewidth=0,
                       width=bin_width, bottom=bottom)
        ax.bar(bin_, mean + sem, alpha=aplha, **options)
        ax.bar(bin_, mean - sem, alpha=aplha, **options)
        ax.bar(bin_, mean, **options)
    pretty_plot(ax)
    return ax


class nonlinear_cmap(LinearSegmentedColormap):
    def __init__(self, cmap, center=.5, clim=[0, 1]):
        if isinstance(cmap, str):
            self.cmap = plt.get_cmap(cmap)
            self.clim = clim
            self.center = center
            for attr in self.cmap.__dict__.keys():
                setattr(self, attr, self.cmap.__dict__[attr])

    def __call__(self, value, alpha=1., **kwargs):
        center = (self.center - self.clim[0]) / np.diff(self.clim)
        value = (value - self.clim[0]) / np.diff(self.clim)
        value[value < 0] = 0.
        value[value > 1] = 1.

# Path: jr/utils.py
# def logcenter(center, x=None, inverse=False):
#     # Authors: Jean-Remi King, <jeanremi.king@gmail.com>
#     #          Clement Levrard <clement.levrard@gmail.com>
#     #
#     # License: BSD (3-clause)
#     """
#     Creates a logarithmic scale centered around center, and bounded between
#     [0., 1.] such that:
#         f(0, center) = 0
#         f(1, center) = 1
#         f(center, center) = .5
# 
#     Parameters
#     ----------
#         x : float | np.array | None
#             If float or np.array, 0. < x < 1.
#             If None, set to np.linspace(0., 1., 256).
#             Defaults to None.
#         center : float
#             0. < center < 1.
#     Returns
#     -------
#         y : float | np.array
#     """
# 
#     from numpy import exp, log
#     if x is None:
#         x = np.linspace(0., 1., 256)
#     if center >= 1. or center <= 0.:
#         raise ValueError('center must be between 0 and 1')
#     if center == .5:
#         y = x
#     else:
#         n = 1. / center
#         if inverse is False:
#             y = (exp(2 * log(n - 1) * x) - 1) / (n * (n - 2))
#         else:
#             y = log(x * (n * (n - 2)) + 1) / (2 * log(n - 1))
#     if center > .5:
#         y = 1. - y
#     return y
# 
# Path: jr/stats/base.py
# def median_abs_deviation(x, axis=None):
#     """median absolute deviation"""
#     x = np.asarray(x)
#     # transpose selected axis in front
#     shape = x.shape
#     n_dim = len(shape)
#     axis_ = None
#     if axis is not None:
#         dim_order = np.hstack((axis, np.delete(np.arange(n_dim), axis)))
#         x = np.transpose(x, dim_order)
#         axis_ = 0
# 
#     # compute median
#     center = np.median(x, axis=axis_, keepdims=False)
#     if len(center):
#         center = center[np.newaxis, ...]
# 
#     # compute median absolute deviation from median
#     mad = np.median(np.abs(x - center), axis=axis_)
# 
#     return mad


# Path: jr/plot/base.py
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.colors as col
    import itertools
from matplotlib.colors import LinearSegmentedColormap
from ..utils import logcenter
from ..stats import median_abs_deviation

    elif isinstance(color, np.ndarray) and color.ndim == 1:
        color = [color] * len(x)
    means = np.nanmean(y, axis=0)
    sems = np.nanstd(y, axis=0) / np.sqrt(y.shape[0])
    if bin_width is None:
        bin_width = np.diff(x[:2])
    for mean, sem, bin_, this_color in zip(means, sems, x, color):
        options = dict(color=this_color, edgecolor='none', linewidth=0,
                       width=bin_width, bottom=bottom)
        ax.bar(bin_, mean + sem, alpha=aplha, **options)
        ax.bar(bin_, mean - sem, alpha=aplha, **options)
        ax.bar(bin_, mean, **options)
    pretty_plot(ax)
    return ax


class nonlinear_cmap(LinearSegmentedColormap):
    def __init__(self, cmap, center=.5, clim=[0, 1]):
        if isinstance(cmap, str):
            self.cmap = plt.get_cmap(cmap)
            self.clim = clim
            self.center = center
            for attr in self.cmap.__dict__.keys():
                setattr(self, attr, self.cmap.__dict__[attr])

    def __call__(self, value, alpha=1., **kwargs):
        center = (self.center - self.clim[0]) / np.diff(self.clim)
        value = (value - self.clim[0]) / np.diff(self.clim)
        value[value < 0] = 0.
        value[value > 1] = 1.

        ilogval = logcenter(center, x=value, inverse=True)