We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Automatically loading colormaps when importing nilearn (or cm?)
The colormaps that are created in [cm.py](https://github.com/KirstieJane/nilearn/blob/40069cb14b43333a73137797eac895cfb054db29/nilearn/plotting/cm.py) are really nice!

Currently, if I want to use them in one of my wrapper scripts I have the following hack:

```
from nilearn.plotting import cm

cmap = 'cold_white_hot'

if hasattr(cm, cmap):
    cmap = getatt(cm, cmap)
```

Would it be possible to have these colormaps already loaded so they're available by name without this code?


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `nilearn/plotting/cm.py`
Content:
```
1 # emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
2 # vi: set ft=python sts=4 ts=4 sw=4 et:
3 """
4 Matplotlib colormaps useful for neuroimaging.
5 """
6 import numpy as _np
7 
8 from matplotlib import cm as _cm
9 from matplotlib import colors as _colors
10 
11 ################################################################################
12 # Custom colormaps for two-tailed symmetric statistics
13 ################################################################################
14 
15 ################################################################################
16 # Helper functions
17 
18 def _rotate_cmap(cmap, swap_order=('green', 'red', 'blue')):
19     """ Utility function to swap the colors of a colormap.
20     """
21     orig_cdict = cmap._segmentdata.copy()
22 
23     cdict = dict()
24     cdict['green'] = [(p, c1, c2)
25                         for (p, c1, c2) in orig_cdict[swap_order[0]]]
26     cdict['blue'] = [(p, c1, c2)
27                         for (p, c1, c2) in orig_cdict[swap_order[1]]]
28     cdict['red'] = [(p, c1, c2)
29                         for (p, c1, c2) in orig_cdict[swap_order[2]]]
30 
31     return cdict
32 
33 
34 def _pigtailed_cmap(cmap, swap_order=('green', 'red', 'blue')):
35     """ Utility function to make a new colormap by concatenating a
36         colormap with its reverse.
37     """
38     orig_cdict = cmap._segmentdata.copy()
39 
40     cdict = dict()
41     cdict['green'] = [(0.5*(1-p), c1, c2)
42                         for (p, c1, c2) in reversed(orig_cdict[swap_order[0]])]
43     cdict['blue'] = [(0.5*(1-p), c1, c2)
44                         for (p, c1, c2) in reversed(orig_cdict[swap_order[1]])]
45     cdict['red'] = [(0.5*(1-p), c1, c2)
46                         for (p, c1, c2) in reversed(orig_cdict[swap_order[2]])]
47 
48     for color in ('red', 'green', 'blue'):
49         cdict[color].extend([(0.5*(1+p), c1, c2) 
50                                     for (p, c1, c2) in orig_cdict[color]])
51 
52     return cdict
53 
54 
55 def _concat_cmap(cmap1, cmap2):
56     """ Utility function to make a new colormap by concatenating two
57         colormaps.
58     """
59     cdict = dict()
60 
61     cdict1 = cmap1._segmentdata.copy()
62     cdict2 = cmap2._segmentdata.copy()
63     if not hasattr(cdict1['red'], '__call__'):
64         for c in ['red', 'green', 'blue']:
65             cdict[c] = [(0.5*p, c1, c2) for (p, c1, c2) in cdict1[c]]
66     else:
67         for c in ['red', 'green', 'blue']:
68             cdict[c] = []
69         ps = _np.linspace(0, 1, 10)
70         colors = cmap1(ps)
71         for p, (r, g, b, a) in zip(ps, colors):
72             cdict['red'].append((.5*p, r, r))
73             cdict['green'].append((.5*p, g, g))
74             cdict['blue'].append((.5*p, b, b))
75     if not hasattr(cdict2['red'], '__call__'):
76         for c in ['red', 'green', 'blue']:
77             cdict[c].extend([(0.5*(1+p), c1, c2) for (p, c1, c2) in cdict2[c]])
78     else:
79         ps = _np.linspace(0, 1, 10)
80         colors = cmap2(ps)
81         for p, (r, g, b, a) in zip(ps, colors):
82             cdict['red'].append((.5*(1+p), r, r))
83             cdict['green'].append((.5*(1+p), g, g))
84             cdict['blue'].append((.5*(1+p), b, b))
85 
86     return cdict
87 
88 
89 def alpha_cmap(color, name='', alpha_min=0.5, alpha_max=1.):
90     """ Return a colormap with the given color, and alpha going from
91         zero to 1.
92 
93         Parameters
94         ----------
95         color: (r, g, b), or a string
96             A triplet of floats ranging from 0 to 1, or a matplotlib
97             color string
98     """
99     red, green, blue = _colors.colorConverter.to_rgb(color)
100     if name == '' and hasattr(color, 'startswith'):
101         name = color
102     cmapspec = [(red, green, blue, 1.),
103                 (red, green, blue, 1.),
104                ]
105     cmap = _colors.LinearSegmentedColormap.from_list(
106                                 '%s_transparent' % name, cmapspec, _cm.LUTSIZE)
107     cmap._init()
108     cmap._lut[:, -1] = _np.linspace(alpha_min, alpha_max, cmap._lut.shape[0])
109     cmap._lut[-1, -1] = 0
110     return cmap
111 
112 
113 
114 ################################################################################
115 # Our colormaps definition
116 _cmaps_data = dict(
117     cold_hot     = _pigtailed_cmap(_cm.hot),
118     cold_white_hot = _pigtailed_cmap(_cm.hot_r),
119     brown_blue   = _pigtailed_cmap(_cm.bone),
120     cyan_copper  = _pigtailed_cmap(_cm.copper),
121     cyan_orange  = _pigtailed_cmap(_cm.YlOrBr_r),
122     blue_red     = _pigtailed_cmap(_cm.Reds_r),
123     brown_cyan   = _pigtailed_cmap(_cm.Blues_r),
124     purple_green = _pigtailed_cmap(_cm.Greens_r,
125                     swap_order=('red', 'blue', 'green')),
126     purple_blue  = _pigtailed_cmap(_cm.Blues_r,
127                     swap_order=('red', 'blue', 'green')),
128     blue_orange  = _pigtailed_cmap(_cm.Oranges_r,
129                     swap_order=('green', 'red', 'blue')),
130     black_blue   = _rotate_cmap(_cm.hot),
131     black_purple = _rotate_cmap(_cm.hot,
132                                     swap_order=('blue', 'red', 'green')),
133     black_pink   = _rotate_cmap(_cm.hot,
134                             swap_order=('blue', 'green', 'red')),
135     black_green  = _rotate_cmap(_cm.hot,
136                             swap_order=('red', 'blue', 'green')),
137     black_red    = _cm.hot._segmentdata.copy(),
138 )
139 
140 if hasattr(_cm, 'ocean'):
141     # MPL 0.99 doesn't have Ocean
142     _cmaps_data['ocean_hot'] =  _concat_cmap(_cm.ocean, _cm.hot_r)
143 if hasattr(_cm, 'afmhot'): # or afmhot
144     _cmaps_data['hot_white_bone'] = _concat_cmap(_cm.afmhot, _cm.bone_r)
145     _cmaps_data['hot_black_bone'] = _concat_cmap(_cm.afmhot_r, _cm.bone)
146 
147 # Copied from matplotlib 1.2.0 for matplotlib 0.99 compatibility.
148 _bwr_data = ((0.0, 0.0, 1.0), (1.0, 1.0, 1.0), (1.0, 0.0, 0.0))
149 _cmaps_data['bwr'] = _colors.LinearSegmentedColormap.from_list(
150     'bwr', _bwr_data)._segmentdata.copy()
151 
152 ################################################################################
153 # Build colormaps and their reverse.
154 _cmap_d = dict()
155 
156 for _cmapname in list(_cmaps_data.keys()):  # needed as dict changes within loop
157     _cmapname_r = _cmapname + '_r'
158     _cmapspec = _cmaps_data[_cmapname]
159     _cmaps_data[_cmapname_r] = _cm.revcmap(_cmapspec)
160     _cmap_d[_cmapname] = _colors.LinearSegmentedColormap(
161                             _cmapname, _cmapspec, _cm.LUTSIZE)
162     _cmap_d[_cmapname_r] = _colors.LinearSegmentedColormap(
163                             _cmapname_r, _cmaps_data[_cmapname_r],
164                             _cm.LUTSIZE)
165 
166 ################################################################################
167 # A few transparent colormaps
168 for color, name in (((1, 0, 0), 'red'),
169                     ((0, 1, 0), 'blue'),
170                     ((0, 0, 1), 'green'),
171                     ):
172     _cmap_d['%s_transparent' % name] = alpha_cmap(color, name=name)
173     _cmap_d['%s_transparent_full_alpha_range' % name] = alpha_cmap(
174         color, alpha_min=0,
175         alpha_max=1, name=name)
176 
177 
178 locals().update(_cmap_d)
179 
180 
181 ################################################################################
182 # Utility to replace a colormap by another in an interval
183 ################################################################################
184 
185 def dim_cmap(cmap, factor=.3, to_white=True):
186     """ Dim a colormap to white, or to black.
187     """
188     assert factor >= 0 and factor <=1, ValueError(
189             'Dimming factor must be larger than 0 and smaller than 1, %s was passed.' 
190                                                         % factor)
191     if to_white:
192         dimmer = lambda c: 1 - factor*(1-c)
193     else:
194         dimmer = lambda c: factor*c
195     cdict = cmap._segmentdata.copy()
196     for c_index, color in enumerate(('red', 'green', 'blue')):
197         color_lst = list()
198         for value, c1, c2 in cdict[color]:
199             color_lst.append((value, dimmer(c1), dimmer(c2)))
200         cdict[color] = color_lst
201 
202     return _colors.LinearSegmentedColormap(
203                                 '%s_dimmed' % cmap.name,
204                                 cdict,
205                                 _cm.LUTSIZE)
206 
207 
208 def replace_inside(outer_cmap, inner_cmap, vmin, vmax):
209     """ Replace a colormap by another inside a pair of values.
210     """
211     assert vmin < vmax, ValueError('vmin must be smaller than vmax')
212     assert vmin >= 0,    ValueError('vmin must be larger than 0, %s was passed.' 
213                                         % vmin)
214     assert vmax <= 1,    ValueError('vmax must be smaller than 1, %s was passed.' 
215                                         % vmax)
216     outer_cdict = outer_cmap._segmentdata.copy()
217     inner_cdict = inner_cmap._segmentdata.copy()
218 
219     cdict = dict()
220     for this_cdict, cmap in [(outer_cdict, outer_cmap),
221                              (inner_cdict, inner_cmap)]:
222         if hasattr(this_cdict['red'], '__call__'):
223             ps = _np.linspace(0, 1, 25)
224             colors = cmap(ps)
225             this_cdict['red'] = list()
226             this_cdict['green'] = list()
227             this_cdict['blue'] = list()
228             for p, (r, g, b, a) in zip(ps, colors):
229                 this_cdict['red'].append((p, r, r))
230                 this_cdict['green'].append((p, g, g))
231                 this_cdict['blue'].append((p, b, b))
232 
233 
234     for c_index, color in enumerate(('red', 'green', 'blue')):
235         color_lst = list()
236 
237         for value, c1, c2 in outer_cdict[color]:
238             if value >= vmin:
239                 break
240             color_lst.append((value, c1, c2))
241 
242         color_lst.append((vmin, outer_cmap(vmin)[c_index], 
243                                 inner_cmap(vmin)[c_index]))
244 
245         for value, c1, c2 in inner_cdict[color]:
246             if value <= vmin:
247                 continue
248             if value >= vmax:
249                 break
250             color_lst.append((value, c1, c2))
251 
252         color_lst.append((vmax, inner_cmap(vmax)[c_index],
253                                 outer_cmap(vmax)[c_index]))
254 
255         for value, c1, c2 in outer_cdict[color]:
256             if value <= vmax:
257                 continue
258             color_lst.append((value, c1, c2))
259 
260         cdict[color] = color_lst
261 
262     return _colors.LinearSegmentedColormap(
263                                 '%s_inside_%s' % (inner_cmap.name, outer_cmap.name),
264                                 cdict,
265                                 _cm.LUTSIZE)
266 
267 
268
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/nilearn/plotting/cm.py b/nilearn/plotting/cm.py
--- a/nilearn/plotting/cm.py
+++ b/nilearn/plotting/cm.py
@@ -175,7 +175,11 @@
         alpha_max=1, name=name)
 
 
+# Save colormaps in the scope of the module
 locals().update(_cmap_d)
+# Register cmaps in matplotlib too
+for k, v in _cmap_d.items():
+    _cm.register_cmap(name=k, cmap=v)
 
 
 ################################################################################

{"golden_diff": "diff --git a/nilearn/plotting/cm.py b/nilearn/plotting/cm.py\n--- a/nilearn/plotting/cm.py\n+++ b/nilearn/plotting/cm.py\n@@ -175,7 +175,11 @@\n         alpha_max=1, name=name)\n \n \n+# Save colormaps in the scope of the module\n locals().update(_cmap_d)\n+# Register cmaps in matplotlib too\n+for k, v in _cmap_d.items():\n+    _cm.register_cmap(name=k, cmap=v)\n \n \n ################################################################################\n", "issue": "Automatically loading colormaps when importing nilearn (or cm?)\nThe colormaps that are created in [cm.py](https://github.com/KirstieJane/nilearn/blob/40069cb14b43333a73137797eac895cfb054db29/nilearn/plotting/cm.py) are really nice!\n\nCurrently, if I want to use them in one of my wrapper scripts I have the following hack:\n\n```\nfrom nilearn.plotting import cm\n\ncmap = 'cold_white_hot'\n\nif hasattr(cm, cmap):\n    cmap = getatt(cm, cmap)\n```\n\nWould it be possible to have these colormaps already loaded so they're available by name without this code?\n\n", "before_files": [{"content": "# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-\n# vi: set ft=python sts=4 ts=4 sw=4 et:\n\"\"\"\nMatplotlib colormaps useful for neuroimaging.\n\"\"\"\nimport numpy as _np\n\nfrom matplotlib import cm as _cm\nfrom matplotlib import colors as _colors\n\n################################################################################\n# Custom colormaps for two-tailed symmetric statistics\n################################################################################\n\n################################################################################\n# Helper functions\n\ndef _rotate_cmap(cmap, swap_order=('green', 'red', 'blue')):\n    \"\"\" Utility function to swap the colors of a colormap.\n    \"\"\"\n    orig_cdict = cmap._segmentdata.copy()\n\n    cdict = dict()\n    cdict['green'] = [(p, c1, c2)\n                        for (p, c1, c2) in orig_cdict[swap_order[0]]]\n    cdict['blue'] = [(p, c1, c2)\n                        for (p, c1, c2) in orig_cdict[swap_order[1]]]\n    cdict['red'] = [(p, c1, c2)\n                        for (p, c1, c2) in orig_cdict[swap_order[2]]]\n\n    return cdict\n\n\ndef _pigtailed_cmap(cmap, swap_order=('green', 'red', 'blue')):\n    \"\"\" Utility function to make a new colormap by concatenating a\n        colormap with its reverse.\n    \"\"\"\n    orig_cdict = cmap._segmentdata.copy()\n\n    cdict = dict()\n    cdict['green'] = [(0.5*(1-p), c1, c2)\n                        for (p, c1, c2) in reversed(orig_cdict[swap_order[0]])]\n    cdict['blue'] = [(0.5*(1-p), c1, c2)\n                        for (p, c1, c2) in reversed(orig_cdict[swap_order[1]])]\n    cdict['red'] = [(0.5*(1-p), c1, c2)\n                        for (p, c1, c2) in reversed(orig_cdict[swap_order[2]])]\n\n    for color in ('red', 'green', 'blue'):\n        cdict[color].extend([(0.5*(1+p), c1, c2) \n                                    for (p, c1, c2) in orig_cdict[color]])\n\n    return cdict\n\n\ndef _concat_cmap(cmap1, cmap2):\n    \"\"\" Utility function to make a new colormap by concatenating two\n        colormaps.\n    \"\"\"\n    cdict = dict()\n\n    cdict1 = cmap1._segmentdata.copy()\n    cdict2 = cmap2._segmentdata.copy()\n    if not hasattr(cdict1['red'], '__call__'):\n        for c in ['red', 'green', 'blue']:\n            cdict[c] = [(0.5*p, c1, c2) for (p, c1, c2) in cdict1[c]]\n    else:\n        for c in ['red', 'green', 'blue']:\n            cdict[c] = []\n        ps = _np.linspace(0, 1, 10)\n        colors = cmap1(ps)\n        for p, (r, g, b, a) in zip(ps, colors):\n            cdict['red'].append((.5*p, r, r))\n            cdict['green'].append((.5*p, g, g))\n            cdict['blue'].append((.5*p, b, b))\n    if not hasattr(cdict2['red'], '__call__'):\n        for c in ['red', 'green', 'blue']:\n            cdict[c].extend([(0.5*(1+p), c1, c2) for (p, c1, c2) in cdict2[c]])\n    else:\n        ps = _np.linspace(0, 1, 10)\n        colors = cmap2(ps)\n        for p, (r, g, b, a) in zip(ps, colors):\n            cdict['red'].append((.5*(1+p), r, r))\n            cdict['green'].append((.5*(1+p), g, g))\n            cdict['blue'].append((.5*(1+p), b, b))\n\n    return cdict\n\n\ndef alpha_cmap(color, name='', alpha_min=0.5, alpha_max=1.):\n    \"\"\" Return a colormap with the given color, and alpha going from\n        zero to 1.\n\n        Parameters\n        ----------\n        color: (r, g, b), or a string\n            A triplet of floats ranging from 0 to 1, or a matplotlib\n            color string\n    \"\"\"\n    red, green, blue = _colors.colorConverter.to_rgb(color)\n    if name == '' and hasattr(color, 'startswith'):\n        name = color\n    cmapspec = [(red, green, blue, 1.),\n                (red, green, blue, 1.),\n               ]\n    cmap = _colors.LinearSegmentedColormap.from_list(\n                                '%s_transparent' % name, cmapspec, _cm.LUTSIZE)\n    cmap._init()\n    cmap._lut[:, -1] = _np.linspace(alpha_min, alpha_max, cmap._lut.shape[0])\n    cmap._lut[-1, -1] = 0\n    return cmap\n\n\n\n################################################################################\n# Our colormaps definition\n_cmaps_data = dict(\n    cold_hot     = _pigtailed_cmap(_cm.hot),\n    cold_white_hot = _pigtailed_cmap(_cm.hot_r),\n    brown_blue   = _pigtailed_cmap(_cm.bone),\n    cyan_copper  = _pigtailed_cmap(_cm.copper),\n    cyan_orange  = _pigtailed_cmap(_cm.YlOrBr_r),\n    blue_red     = _pigtailed_cmap(_cm.Reds_r),\n    brown_cyan   = _pigtailed_cmap(_cm.Blues_r),\n    purple_green = _pigtailed_cmap(_cm.Greens_r,\n                    swap_order=('red', 'blue', 'green')),\n    purple_blue  = _pigtailed_cmap(_cm.Blues_r,\n                    swap_order=('red', 'blue', 'green')),\n    blue_orange  = _pigtailed_cmap(_cm.Oranges_r,\n                    swap_order=('green', 'red', 'blue')),\n    black_blue   = _rotate_cmap(_cm.hot),\n    black_purple = _rotate_cmap(_cm.hot,\n                                    swap_order=('blue', 'red', 'green')),\n    black_pink   = _rotate_cmap(_cm.hot,\n                            swap_order=('blue', 'green', 'red')),\n    black_green  = _rotate_cmap(_cm.hot,\n                            swap_order=('red', 'blue', 'green')),\n    black_red    = _cm.hot._segmentdata.copy(),\n)\n\nif hasattr(_cm, 'ocean'):\n    # MPL 0.99 doesn't have Ocean\n    _cmaps_data['ocean_hot'] =  _concat_cmap(_cm.ocean, _cm.hot_r)\nif hasattr(_cm, 'afmhot'): # or afmhot\n    _cmaps_data['hot_white_bone'] = _concat_cmap(_cm.afmhot, _cm.bone_r)\n    _cmaps_data['hot_black_bone'] = _concat_cmap(_cm.afmhot_r, _cm.bone)\n\n# Copied from matplotlib 1.2.0 for matplotlib 0.99 compatibility.\n_bwr_data = ((0.0, 0.0, 1.0), (1.0, 1.0, 1.0), (1.0, 0.0, 0.0))\n_cmaps_data['bwr'] = _colors.LinearSegmentedColormap.from_list(\n    'bwr', _bwr_data)._segmentdata.copy()\n\n################################################################################\n# Build colormaps and their reverse.\n_cmap_d = dict()\n\nfor _cmapname in list(_cmaps_data.keys()):  # needed as dict changes within loop\n    _cmapname_r = _cmapname + '_r'\n    _cmapspec = _cmaps_data[_cmapname]\n    _cmaps_data[_cmapname_r] = _cm.revcmap(_cmapspec)\n    _cmap_d[_cmapname] = _colors.LinearSegmentedColormap(\n                            _cmapname, _cmapspec, _cm.LUTSIZE)\n    _cmap_d[_cmapname_r] = _colors.LinearSegmentedColormap(\n                            _cmapname_r, _cmaps_data[_cmapname_r],\n                            _cm.LUTSIZE)\n\n################################################################################\n# A few transparent colormaps\nfor color, name in (((1, 0, 0), 'red'),\n                    ((0, 1, 0), 'blue'),\n                    ((0, 0, 1), 'green'),\n                    ):\n    _cmap_d['%s_transparent' % name] = alpha_cmap(color, name=name)\n    _cmap_d['%s_transparent_full_alpha_range' % name] = alpha_cmap(\n        color, alpha_min=0,\n        alpha_max=1, name=name)\n\n\nlocals().update(_cmap_d)\n\n\n################################################################################\n# Utility to replace a colormap by another in an interval\n################################################################################\n\ndef dim_cmap(cmap, factor=.3, to_white=True):\n    \"\"\" Dim a colormap to white, or to black.\n    \"\"\"\n    assert factor >= 0 and factor <=1, ValueError(\n            'Dimming factor must be larger than 0 and smaller than 1, %s was passed.' \n                                                        % factor)\n    if to_white:\n        dimmer = lambda c: 1 - factor*(1-c)\n    else:\n        dimmer = lambda c: factor*c\n    cdict = cmap._segmentdata.copy()\n    for c_index, color in enumerate(('red', 'green', 'blue')):\n        color_lst = list()\n        for value, c1, c2 in cdict[color]:\n            color_lst.append((value, dimmer(c1), dimmer(c2)))\n        cdict[color] = color_lst\n\n    return _colors.LinearSegmentedColormap(\n                                '%s_dimmed' % cmap.name,\n                                cdict,\n                                _cm.LUTSIZE)\n\n\ndef replace_inside(outer_cmap, inner_cmap, vmin, vmax):\n    \"\"\" Replace a colormap by another inside a pair of values.\n    \"\"\"\n    assert vmin < vmax, ValueError('vmin must be smaller than vmax')\n    assert vmin >= 0,    ValueError('vmin must be larger than 0, %s was passed.' \n                                        % vmin)\n    assert vmax <= 1,    ValueError('vmax must be smaller than 1, %s was passed.' \n                                        % vmax)\n    outer_cdict = outer_cmap._segmentdata.copy()\n    inner_cdict = inner_cmap._segmentdata.copy()\n\n    cdict = dict()\n    for this_cdict, cmap in [(outer_cdict, outer_cmap),\n                             (inner_cdict, inner_cmap)]:\n        if hasattr(this_cdict['red'], '__call__'):\n            ps = _np.linspace(0, 1, 25)\n            colors = cmap(ps)\n            this_cdict['red'] = list()\n            this_cdict['green'] = list()\n            this_cdict['blue'] = list()\n            for p, (r, g, b, a) in zip(ps, colors):\n                this_cdict['red'].append((p, r, r))\n                this_cdict['green'].append((p, g, g))\n                this_cdict['blue'].append((p, b, b))\n\n\n    for c_index, color in enumerate(('red', 'green', 'blue')):\n        color_lst = list()\n\n        for value, c1, c2 in outer_cdict[color]:\n            if value >= vmin:\n                break\n            color_lst.append((value, c1, c2))\n\n        color_lst.append((vmin, outer_cmap(vmin)[c_index], \n                                inner_cmap(vmin)[c_index]))\n\n        for value, c1, c2 in inner_cdict[color]:\n            if value <= vmin:\n                continue\n            if value >= vmax:\n                break\n            color_lst.append((value, c1, c2))\n\n        color_lst.append((vmax, inner_cmap(vmax)[c_index],\n                                outer_cmap(vmax)[c_index]))\n\n        for value, c1, c2 in outer_cdict[color]:\n            if value <= vmax:\n                continue\n            color_lst.append((value, c1, c2))\n\n        cdict[color] = color_lst\n\n    return _colors.LinearSegmentedColormap(\n                                '%s_inside_%s' % (inner_cmap.name, outer_cmap.name),\n                                cdict,\n                                _cm.LUTSIZE)\n\n\n", "path": "nilearn/plotting/cm.py"}], "after_files": [{"content": "# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-\n# vi: set ft=python sts=4 ts=4 sw=4 et:\n\"\"\"\nMatplotlib colormaps useful for neuroimaging.\n\"\"\"\nimport numpy as _np\n\nfrom matplotlib import cm as _cm\nfrom matplotlib import colors as _colors\n\n################################################################################\n# Custom colormaps for two-tailed symmetric statistics\n################################################################################\n\n################################################################################\n# Helper functions\n\ndef _rotate_cmap(cmap, swap_order=('green', 'red', 'blue')):\n    \"\"\" Utility function to swap the colors of a colormap.\n    \"\"\"\n    orig_cdict = cmap._segmentdata.copy()\n\n    cdict = dict()\n    cdict['green'] = [(p, c1, c2)\n                        for (p, c1, c2) in orig_cdict[swap_order[0]]]\n    cdict['blue'] = [(p, c1, c2)\n                        for (p, c1, c2) in orig_cdict[swap_order[1]]]\n    cdict['red'] = [(p, c1, c2)\n                        for (p, c1, c2) in orig_cdict[swap_order[2]]]\n\n    return cdict\n\n\ndef _pigtailed_cmap(cmap, swap_order=('green', 'red', 'blue')):\n    \"\"\" Utility function to make a new colormap by concatenating a\n        colormap with its reverse.\n    \"\"\"\n    orig_cdict = cmap._segmentdata.copy()\n\n    cdict = dict()\n    cdict['green'] = [(0.5*(1-p), c1, c2)\n                        for (p, c1, c2) in reversed(orig_cdict[swap_order[0]])]\n    cdict['blue'] = [(0.5*(1-p), c1, c2)\n                        for (p, c1, c2) in reversed(orig_cdict[swap_order[1]])]\n    cdict['red'] = [(0.5*(1-p), c1, c2)\n                        for (p, c1, c2) in reversed(orig_cdict[swap_order[2]])]\n\n    for color in ('red', 'green', 'blue'):\n        cdict[color].extend([(0.5*(1+p), c1, c2) \n                                    for (p, c1, c2) in orig_cdict[color]])\n\n    return cdict\n\n\ndef _concat_cmap(cmap1, cmap2):\n    \"\"\" Utility function to make a new colormap by concatenating two\n        colormaps.\n    \"\"\"\n    cdict = dict()\n\n    cdict1 = cmap1._segmentdata.copy()\n    cdict2 = cmap2._segmentdata.copy()\n    if not hasattr(cdict1['red'], '__call__'):\n        for c in ['red', 'green', 'blue']:\n            cdict[c] = [(0.5*p, c1, c2) for (p, c1, c2) in cdict1[c]]\n    else:\n        for c in ['red', 'green', 'blue']:\n            cdict[c] = []\n        ps = _np.linspace(0, 1, 10)\n        colors = cmap1(ps)\n        for p, (r, g, b, a) in zip(ps, colors):\n            cdict['red'].append((.5*p, r, r))\n            cdict['green'].append((.5*p, g, g))\n            cdict['blue'].append((.5*p, b, b))\n    if not hasattr(cdict2['red'], '__call__'):\n        for c in ['red', 'green', 'blue']:\n            cdict[c].extend([(0.5*(1+p), c1, c2) for (p, c1, c2) in cdict2[c]])\n    else:\n        ps = _np.linspace(0, 1, 10)\n        colors = cmap2(ps)\n        for p, (r, g, b, a) in zip(ps, colors):\n            cdict['red'].append((.5*(1+p), r, r))\n            cdict['green'].append((.5*(1+p), g, g))\n            cdict['blue'].append((.5*(1+p), b, b))\n\n    return cdict\n\n\ndef alpha_cmap(color, name='', alpha_min=0.5, alpha_max=1.):\n    \"\"\" Return a colormap with the given color, and alpha going from\n        zero to 1.\n\n        Parameters\n        ----------\n        color: (r, g, b), or a string\n            A triplet of floats ranging from 0 to 1, or a matplotlib\n            color string\n    \"\"\"\n    red, green, blue = _colors.colorConverter.to_rgb(color)\n    if name == '' and hasattr(color, 'startswith'):\n        name = color\n    cmapspec = [(red, green, blue, 1.),\n                (red, green, blue, 1.),\n               ]\n    cmap = _colors.LinearSegmentedColormap.from_list(\n                                '%s_transparent' % name, cmapspec, _cm.LUTSIZE)\n    cmap._init()\n    cmap._lut[:, -1] = _np.linspace(alpha_min, alpha_max, cmap._lut.shape[0])\n    cmap._lut[-1, -1] = 0\n    return cmap\n\n\n\n################################################################################\n# Our colormaps definition\n_cmaps_data = dict(\n    cold_hot     = _pigtailed_cmap(_cm.hot),\n    cold_white_hot = _pigtailed_cmap(_cm.hot_r),\n    brown_blue   = _pigtailed_cmap(_cm.bone),\n    cyan_copper  = _pigtailed_cmap(_cm.copper),\n    cyan_orange  = _pigtailed_cmap(_cm.YlOrBr_r),\n    blue_red     = _pigtailed_cmap(_cm.Reds_r),\n    brown_cyan   = _pigtailed_cmap(_cm.Blues_r),\n    purple_green = _pigtailed_cmap(_cm.Greens_r,\n                    swap_order=('red', 'blue', 'green')),\n    purple_blue  = _pigtailed_cmap(_cm.Blues_r,\n                    swap_order=('red', 'blue', 'green')),\n    blue_orange  = _pigtailed_cmap(_cm.Oranges_r,\n                    swap_order=('green', 'red', 'blue')),\n    black_blue   = _rotate_cmap(_cm.hot),\n    black_purple = _rotate_cmap(_cm.hot,\n                                    swap_order=('blue', 'red', 'green')),\n    black_pink   = _rotate_cmap(_cm.hot,\n                            swap_order=('blue', 'green', 'red')),\n    black_green  = _rotate_cmap(_cm.hot,\n                            swap_order=('red', 'blue', 'green')),\n    black_red    = _cm.hot._segmentdata.copy(),\n)\n\nif hasattr(_cm, 'ocean'):\n    # MPL 0.99 doesn't have Ocean\n    _cmaps_data['ocean_hot'] =  _concat_cmap(_cm.ocean, _cm.hot_r)\nif hasattr(_cm, 'afmhot'): # or afmhot\n    _cmaps_data['hot_white_bone'] = _concat_cmap(_cm.afmhot, _cm.bone_r)\n    _cmaps_data['hot_black_bone'] = _concat_cmap(_cm.afmhot_r, _cm.bone)\n\n# Copied from matplotlib 1.2.0 for matplotlib 0.99 compatibility.\n_bwr_data = ((0.0, 0.0, 1.0), (1.0, 1.0, 1.0), (1.0, 0.0, 0.0))\n_cmaps_data['bwr'] = _colors.LinearSegmentedColormap.from_list(\n    'bwr', _bwr_data)._segmentdata.copy()\n\n################################################################################\n# Build colormaps and their reverse.\n_cmap_d = dict()\n\nfor _cmapname in list(_cmaps_data.keys()):  # needed as dict changes within loop\n    _cmapname_r = _cmapname + '_r'\n    _cmapspec = _cmaps_data[_cmapname]\n    _cmaps_data[_cmapname_r] = _cm.revcmap(_cmapspec)\n    _cmap_d[_cmapname] = _colors.LinearSegmentedColormap(\n                            _cmapname, _cmapspec, _cm.LUTSIZE)\n    _cmap_d[_cmapname_r] = _colors.LinearSegmentedColormap(\n                            _cmapname_r, _cmaps_data[_cmapname_r],\n                            _cm.LUTSIZE)\n\n################################################################################\n# A few transparent colormaps\nfor color, name in (((1, 0, 0), 'red'),\n                    ((0, 1, 0), 'blue'),\n                    ((0, 0, 1), 'green'),\n                    ):\n    _cmap_d['%s_transparent' % name] = alpha_cmap(color, name=name)\n    _cmap_d['%s_transparent_full_alpha_range' % name] = alpha_cmap(\n        color, alpha_min=0,\n        alpha_max=1, name=name)\n\n\n# Save colormaps in the scope of the module\nlocals().update(_cmap_d)\n# Register cmaps in matplotlib too\nfor k, v in _cmap_d.items():\n    _cm.register_cmap(name=k, cmap=v)\n\n\n################################################################################\n# Utility to replace a colormap by another in an interval\n################################################################################\n\ndef dim_cmap(cmap, factor=.3, to_white=True):\n    \"\"\" Dim a colormap to white, or to black.\n    \"\"\"\n    assert factor >= 0 and factor <=1, ValueError(\n            'Dimming factor must be larger than 0 and smaller than 1, %s was passed.' \n                                                        % factor)\n    if to_white:\n        dimmer = lambda c: 1 - factor*(1-c)\n    else:\n        dimmer = lambda c: factor*c\n    cdict = cmap._segmentdata.copy()\n    for c_index, color in enumerate(('red', 'green', 'blue')):\n        color_lst = list()\n        for value, c1, c2 in cdict[color]:\n            color_lst.append((value, dimmer(c1), dimmer(c2)))\n        cdict[color] = color_lst\n\n    return _colors.LinearSegmentedColormap(\n                                '%s_dimmed' % cmap.name,\n                                cdict,\n                                _cm.LUTSIZE)\n\n\ndef replace_inside(outer_cmap, inner_cmap, vmin, vmax):\n    \"\"\" Replace a colormap by another inside a pair of values.\n    \"\"\"\n    assert vmin < vmax, ValueError('vmin must be smaller than vmax')\n    assert vmin >= 0,    ValueError('vmin must be larger than 0, %s was passed.' \n                                        % vmin)\n    assert vmax <= 1,    ValueError('vmax must be smaller than 1, %s was passed.' \n                                        % vmax)\n    outer_cdict = outer_cmap._segmentdata.copy()\n    inner_cdict = inner_cmap._segmentdata.copy()\n\n    cdict = dict()\n    for this_cdict, cmap in [(outer_cdict, outer_cmap),\n                             (inner_cdict, inner_cmap)]:\n        if hasattr(this_cdict['red'], '__call__'):\n            ps = _np.linspace(0, 1, 25)\n            colors = cmap(ps)\n            this_cdict['red'] = list()\n            this_cdict['green'] = list()\n            this_cdict['blue'] = list()\n            for p, (r, g, b, a) in zip(ps, colors):\n                this_cdict['red'].append((p, r, r))\n                this_cdict['green'].append((p, g, g))\n                this_cdict['blue'].append((p, b, b))\n\n\n    for c_index, color in enumerate(('red', 'green', 'blue')):\n        color_lst = list()\n\n        for value, c1, c2 in outer_cdict[color]:\n            if value >= vmin:\n                break\n            color_lst.append((value, c1, c2))\n\n        color_lst.append((vmin, outer_cmap(vmin)[c_index], \n                                inner_cmap(vmin)[c_index]))\n\n        for value, c1, c2 in inner_cdict[color]:\n            if value <= vmin:\n                continue\n            if value >= vmax:\n                break\n            color_lst.append((value, c1, c2))\n\n        color_lst.append((vmax, inner_cmap(vmax)[c_index],\n                                outer_cmap(vmax)[c_index]))\n\n        for value, c1, c2 in outer_cdict[color]:\n            if value <= vmax:\n                continue\n            color_lst.append((value, c1, c2))\n\n        cdict[color] = color_lst\n\n    return _colors.LinearSegmentedColormap(\n                                '%s_inside_%s' % (inner_cmap.name, outer_cmap.name),\n                                cdict,\n                                _cm.LUTSIZE)\n\n\n", "path": "nilearn/plotting/cm.py"}]}