TheChymera / chr-helpers

The Christian Helpers
GNU General Public License v3.0
1 stars 1 forks source link

remappedColorMap results in values above 1 #3

Open blaiseli opened 8 years ago

blaiseli commented 8 years ago

I'm not sure my diagnositc is correct and I'm not at all familiar with the intarnals of matplotlib, but what I observe on a particular case is the following:

When I use the plt.cm.RdYlBu_r colormap, the figure can be saved. When I instead run this colormap through remappedColorMap, plt.savefig fails with the following error: "ValueError: data mapping points must have x in increasing order"

After some investigation using the pdb debugger (which I use for the first time), it seems that the problem occurs at x = self.cmap(x, alpha=alpha, bytes=bytes) at the end of ScalarMappable.to_rgba.

See the following pdb excerpt:

  File "/usr/lib/python2.7/dist-packages/matplotlib/collections.py", line 256, in draw
    self.update_scalarmappable()
  File "/usr/lib/python2.7/dist-packages/matplotlib/collections.py", line 657, in update_scalarmappable
    self._facecolors = self.to_rgba(self._A, self._alpha)
  File "/usr/lib/python2.7/dist-packages/matplotlib/cm.py", line 263, in to_rgba
    x = self.cmap(x, alpha=alpha, bytes=bytes)
  File "/usr/lib/python2.7/dist-packages/matplotlib/colors.py", line 553, in __call__
    self._init()
  File "/usr/lib/python2.7/dist-packages/matplotlib/colors.py", line 732, in _init
    self.N, self._segmentdata['red'], self._gamma)
  File "/usr/lib/python2.7/dist-packages/matplotlib/colors.py", line 472, in makeMappingArray
    "data mapping points must have x in increasing order")
ValueError: data mapping points must have x in increasing order
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /usr/lib/python2.7/dist-packages/matplotlib/colors.py(472)makeMappingArray()
-> "data mapping points must have x in increasing order")
 (Pdb) u
> /usr/lib/python2.7/dist-packages/matplotlib/colors.py(732)_init()
-> self.N, self._segmentdata['red'], self._gamma)
(Pdb) u
> /usr/lib/python2.7/dist-packages/matplotlib/colors.py(553)__call__()
-> self._init()
(Pdb) [d[0] for d in self._segmentdata['red']]
[0.0, 0.0080002988562317655, 0.016000597712463531, 0.024000896568695296, 0.032001195424927062, 0.040001494281158831, 0.048001793137390593, 0.056002091993622355, 0.064002390849854124, 0.072002689706085893, 0.080002988562317662, 0.088003287418549417, 0.096003586274781186, 0.10400388513101295, 0.11200418398724471, 0.12000448284347648, 0.12800478169970825, 0.13600508055594002, 0.14400537941217179, 0.15200567826840355, 0.16000597712463532, 0.16800627598086706, 0.17600657483709883, 0.1840068736933306, 0.19200717254956237, 0.20000747140579414, 0.20800777026202591, 0.21600806911825768, 0.22400836797448942, 0.23200866683072119, 0.24000896568695296, 0.24800926454318473, 0.2560095633994165, 0.26400986225564826, 0.27201016111188003, 0.2800104599681118, 0.28801075882434357, 0.29601105768057534, 0.30401135653680711, 0.31201165539303888, 0.32001195424927065, 0.32801225310550236, 0.33601255196173413, 0.3440128508179659, 0.35201314967419767, 0.36001344853042944, 0.36801374738666121, 0.37601404624289297, 0.38401434509912474, 0.39201464395535651, 0.40001494281158828, 0.40801524166782005, 0.41601554052405182, 0.42401583938028359, 0.43201613823651536, 0.44001643709274713, 0.44801673594897884, 0.45601703480521061, 0.46401733366144238, 0.47201763251767415, 0.48001793137390592, 0.48801823023013768, 0.49601852908636945, 0.50401882794260122, 0.51201912679883299, 0.52001942565506476, 0.52801972451129653, 0.5360200233675283, 0.54402032222376007, 0.55202062107999184, 0.5600209199362236, 0.56802121879245537, 0.57602151764868714, 0.58402181650491891, 0.59202211536115068, 0.60002241421738245, 0.60802271307361422, 0.61602301192984599, 0.62402331078607776, 0.63202360964230953, 0.64002390849854129, 0.64802420735477295, 0.65602450621100472, 0.66402480506723649, 0.67202510392346826, 0.68002540277970003, 0.6880257016359318, 0.69602600049216357, 0.70402629934839533, 0.7120265982046271, 0.72002689706085887, 0.72802719591709064, 0.73602749477332241, 0.74402779362955418, 0.75202809248578595, 0.76002839134201772, 0.76802869019824949, 0.77602898905448126, 0.78402928791071302, 0.79202958676694479, 0.80002988562317656, 0.80803018447940833, 0.8160304833356401, 0.82403078219187187, 0.83203108104810364, 0.84003137990433541, 0.84803167876056718, 0.85603197761679894, 0.86403227647303071, 0.87203257532926248, 0.88003287418549425, 0.88803317304172602, 0.89603347189795768, 0.90403377075418945, 0.91203406961042122, 0.92003436846665299, 0.92803466732288475, 0.93603496617911652, 0.94403526503534829, 0.95203556389158006, 0.96003586274781183, 0.9680361616040436, 0.97603646046027537, 0.98403675931650714, 0.99203705817273891, 1.0000373570289707, 1.0080376558852024, 1.0160379547414342, 1.024038253597666, 1.0238504547414342, 1.0236626558852024, 1.0234748570289707, 1.0232870581727389, 1.0230992593165071, 1.0229114604602754, 1.0227236616040436, 1.0225358627478118, 1.0223480638915801, 1.0221602650353483, 1.0219724661791165, 1.0217846673228848, 1.021596868466653, 1.0214090696104212, 1.0212212707541894, 1.0210334718979577, 1.0208456730417259, 1.0206578741854941, 1.0204700753292624, 1.0202822764730306, 1.0200944776167988, 1.0199066787605671, 1.0197188799043353, 1.0195310810481035, 1.0193432821918718, 1.01915548333564, 1.0189676844794082, 1.0187798856231765, 1.0185920867669447, 1.0184042879107129, 1.0182164890544811, 1.0180286901982494, 1.0178408913420178, 1.0176530924857861, 1.0174652936295543, 1.0172774947733225, 1.0170896959170908, 1.016901897060859, 1.0167140982046272, 1.0165262993483954, 1.0163385004921637, 1.0161507016359319, 1.0159629027797001, 1.0157751039234684, 1.0155873050672366, 1.0153995062110048, 1.0152117073547731, 1.0150239084985413, 1.0148361096423095, 1.0146483107860778, 1.014460511929846, 1.0142727130736142, 1.0140849142173824, 1.0138971153611507, 1.0137093165049189, 1.0135215176486871, 1.0133337187924554, 1.0131459199362236, 1.0129581210799918, 1.0127703222237601, 1.0125825233675283, 1.0123947245112965, 1.0122069256550648, 1.012019126798833, 1.0118313279426012, 1.0116435290863695, 1.0114557302301377, 1.0112679313739059, 1.0110801325176741, 1.0108923336614424, 1.0107045348052106, 1.0105167359489788, 1.0103289370927471, 1.0101411382365153, 1.0099533393802835, 1.0097655405240518, 1.00957774166782, 1.0093899428115882, 1.0092021439553565, 1.0090143450991247, 1.0088265462428929, 1.0086387473866611, 1.0084509485304294, 1.0082631496741976, 1.0080753508179658, 1.0078875519617341, 1.0076997531055023, 1.0075119542492705, 1.0073241553930388, 1.007136356536807, 1.0069485576805752, 1.0067607588243435, 1.0065729599681117, 1.0063851611118799, 1.0061973622556482, 1.0060095633994166, 1.0058217645431848, 1.0056339656869531, 1.0054461668307213, 1.0052583679744895, 1.0050705691182578, 1.004882770262026, 1.0046949714057942, 1.0045071725495625, 1.0043193736933307, 1.0041315748370989, 1.0039437759808671, 1.0037559771246354, 1.0035681782684036, 1.0033803794121718, 1.0031925805559401, 1.0030047816997083, 1.0028169828434765, 1.0026291839872448, 1.002441385131013, 1.0022535862747812, 1.0020657874185495, 1.0018779885623177, 1.0016901897060859, 1.0015023908498542, 1.0013145919936224, 1.0011267931373906, 1.0009389942811588, 1.0007511954249271, 1.0005633965686953, 1.0003755977124635, 1.0001877988562318, 1.0]
(Pdb) u
> /usr/lib/python2.7/dist-packages/matplotlib/cm.py(263)to_rgba()
-> x = self.cmap(x, alpha=alpha, bytes=bytes)
(Pdb) a
self = <matplotlib.collections.QuadMesh object at 0x7f3a7b166350>
x = [ 0.00195313  0.00585937  0.00976562  0.01367188  0.01757812  0.02148438
  0.02539063  0.02929687  0.03320313  0.03710937  0.04101563  0.04492187
  0.04882812  0.05273438  0.05664062  0.06054688  0.06445313  0.06835937
  0.07226563  0.07617187  0.08007812  0.08398438  0.08789062  0.09179688
  0.09570313  0.09960937  0.10351563  0.10742187  0.11132813  0.11523437
  0.11914062  0.12304688  0.12695312  0.13085938  0.13476563  0.13867187
  0.14257813  0.14648438  0.15039062  0.15429688  0.15820312  0.16210938
  0.16601562  0.16992187  0.17382813  0.17773437  0.18164063  0.18554687
  0.18945312  0.19335938  0.19726563  0.20117188  0.20507812  0.20898438
  0.21289062  0.21679688  0.22070312  0.22460938  0.22851562  0.23242187
  0.23632812  0.24023437  0.24414063  0.24804687  0.25195312  0.25585938
  0.25976563  0.26367188  0.26757812  0.27148438  0.27539062  0.27929688
  0.28320312  0.28710938  0.29101562  0.29492188  0.29882812  0.30273437
  0.30664062  0.31054687  0.31445312  0.31835938  0.32226562  0.32617188
  0.33007813  0.33398438  0.33789062  0.34179688  0.34570312  0.34960938
  0.35351562  0.35742188  0.36132812  0.36523438  0.36914062  0.37304688
  0.37695312  0.38085937  0.38476562  0.38867187  0.39257812  0.39648438
  0.40039063  0.40429688  0.40820313  0.41210938  0.41601562  0.41992188
  0.42382813  0.42773438  0.43164062  0.43554687  0.43945312  0.44335938
  0.44726562  0.45117187  0.45507812  0.45898438  0.46289062  0.46679688
  0.47070312  0.47460938  0.47851563  0.48242188  0.48632812  0.49023438
  0.49414063  0.49804688  0.50195312  0.50585937  0.50976562  0.51367188
  0.51757812  0.52148438  0.52539062  0.52929688  0.53320312  0.53710938
  0.54101562  0.54492188  0.54882812  0.55273438  0.55664062  0.56054688
  0.56445313  0.56835938  0.57226562  0.57617187  0.58007812  0.58398438
  0.58789062  0.59179688  0.59570312  0.59960938  0.60351562  0.60742188
  0.61132812  0.61523438  0.61914062  0.62304688  0.62695312  0.63085938
  0.63476563  0.63867188  0.64257812  0.64648437  0.65039062  0.65429688
  0.65820312  0.66210938  0.66601562  0.66992188  0.67382812  0.67773438
  0.68164062  0.68554688  0.68945312  0.69335938  0.69726562  0.70117188
  0.70507813  0.70898438  0.71289062  0.71679687  0.72070312  0.72460938
  0.72851562  0.73242188  0.73632812  0.74023438  0.74414062  0.74804688
  0.75195312  0.75585938  0.75976562  0.76367188  0.76757812  0.77148438
  0.77539063  0.77929688  0.78320312  0.78710938  0.79101562  0.79492188
  0.79882812  0.80273438  0.80664062  0.81054688  0.81445312  0.81835938
  0.82226562  0.82617188  0.83007812  0.83398438  0.83789062  0.84179688
  0.84570312  0.84960938  0.85351563  0.85742187  0.86132813  0.86523437
  0.86914062  0.87304688  0.87695312  0.88085938  0.88476562  0.88867188
  0.89257813  0.89648437  0.90039062  0.90429688  0.90820312  0.91210938
  0.91601562  0.91992188  0.92382813  0.92773437  0.93164063  0.93554687
  0.93945312  0.94335938  0.94726562  0.95117188  0.95507812  0.95898438
  0.96289063  0.96679687  0.97070312  0.97460938  0.97851562  0.98242188
  0.98632812  0.99023438  0.99414063  0.99804687]
alpha = None
bytes = False

Notice how [d[0] for d in self._segmentdata['red']] has values above 1, whereas the data on which cmap was called (x) seemed normal.

TheChymera commented 8 years ago

hi, and thanks for using my function. Could you provide a self-contained example (including sample data) where this error occurs?

blaiseli commented 8 years ago

Hi,

Here is an example:

import numpy as np
from chr_matplotlib import remappedColorMap as remap_cmap
import matplotlib.pyplot as plt

points = [('7.68987928239', '0.280729370315'),
          ('0.403355125994', '0.257420349721'),
          ('0.319425497413', '0.150782099669'),
          ('3.19054207099', '0.0952249289337'),
          ('0.534078916888', '0.15178186293'),
          ('0.736596226609', '0.0010929162768'),
          ('0.899056652338', '0.000934496816481'),
          ('0.309946976912', '0.00459535101184'),
          ('0.638361946925', '0.00146612167208'),
          ('0.370177086819', '0.150245134832'),
          ('0.121657100203', '0.00215972597587'),
          ('0.122506499189', '0.0658473156801'),
          ('0.276003191681', '0.000727424188575'),
          ('0.259600782477', '0.000716744224958'),
          ('0.164641836761', '0.00740714809965'),
          ('2.10582095699', '1.79222961447'),
          ('2.1971828267', '0.155913822187'),
          ('0.554992149494', '0.00697698289841'),
          ('0.191095467298', '0.00409991936627'),
          ('0.963385755836', '0.024997641508'),
          ('0.0571799953669', '0.00961730723704'),
          ('0.917563254485', '0.521041011654'),
          ('0.644680960593', '0.521535849968'),
          ('0.594070937685', '0.023056261455'),
          ('0.113169545185', '0.00091966353368'),
          ('1.75169236313', '0.0480105897773'),
          ('1.53780468971', '0.00776937686566'),
          ('0.196526472935', '0.00210988614565'),
          ('0.567520784536', '0.199039515272'),
          ('0.466345782605', '0.00287884352607'),
          ('0.0481004349952', '0.000857363745914'),
          ('5.29981853749', '0.586445108844'),
          ('5.47841110911', '0.630035973677'),
          ('0.290957761705', '0.00241011178955'),
          ('0.318942884353', '0.00154622139921'),
          ('0.254504388561', '0.0973271017723'),
          ('0.440027283725', '0.00925537513669'),
          ('1.79219325114', '1.26906388339'),
          ('0.441050423413', '0.0022451656848'),
          ('0.223327584876', '0.000964756713395'),
          ('0.917389513783', '0.0293918532051'),
          ('1.13829116368', '0.00476801042364'),
          ('0.0146199583022', '0.0120303856832'),
          ('0.519471828267', '0.231622304273'),
          ('1.49606831227', '0.00961078059261'),
          ('0.167717690664', '0.139188999163'),
          ('0.409841445523', '0.000597484631236'),
          ('1.36145144269', '0.191990145954'),
          ('0.995115955831', '0.001737867413'),
          ('0.177234820211', '0.000888216974141'),
          ('0.32598260019', '0.00212234610321'),
          ('0.0407582816401', '0.0141094186006'),
          ('0.697626830712', '0.293862758908'),
          ('1.13297598517', '0.847418919793'),
          ('1.06084141978', '0.00256378459937'),
          ('3.14861908316', '0.00746054791773'),
          ('0.321491081311', '0.034580535529'),
          ('2.41543010476', '0.0100332324868'),
          ('0.233623330159', '0.000641391148327'),
          ('0.957311266119', '0.0637825227142'),
          ('0.458179969628', '0.00331553537174'),
          ('2.05074515456', '0.0189059089272'),
          ('1.09970142339', '0.368945869791'),
          ('0.71470168078', '0.00377625713555')]

logfolds = np.log2([float(y) / float(x) for (x, y) in points])
column = np.reshape(logfolds, (-1, 1))
cmap = plt.cm.RdYlBu_r
remap = True
if remap:
    absvmin = abs(min(logfolds))
    vmax = max(logfolds)
    midpoint = absvmin / (vmax + absvmin)
    cmap = remap_cmap(cmap, midpoint=midpoint)
axis = plt.gca()
axis.set_ylim((0, len(logfolds)))
axis.set_yticks(np.arange(len(logfolds)) + 0.5, minor = False)
axis.set_yticklabels(np.arange(len(logfolds)))
axis.set_xticks([])
heatmap = axis.pcolor(column, cmap=cmap)
cbar = plt.colorbar(
    mappable=heatmap,
    orientation="vertical",
    fraction=0.1)
axis.tick_params(
    axis="y", left="off", right="off",
    labelsize=5)

axis.set_aspect('equal')
plt.savefig("heatmap.pdf", dpi=300, bbox_inches="tight")

Setting remap = False avoids the error.