Skip to content

Commit b9284c4

Browse files
author
Martin Journois
committed
Apply pep8 corrections, etc
1 parent 373364e commit b9284c4

1 file changed

Lines changed: 70 additions & 76 deletions

File tree

folium/colormap.py

Lines changed: 70 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -229,23 +229,25 @@
229229
def _parse_color(x):
230230
"""
231231
"""
232-
if isinstance(x, tuple) or isinstance(x, list):
232+
if isinstance(x, (tuple,list)):
233233
color_tuple = tuple(x)[:4]
234-
elif isinstance(x, text_type) or isinstance(x, binary_type):
235-
if x[0]=="#" and len(x)==7:
236-
# Color of the for #RRGGBB
237-
color_tuple = (int(x[1:3],16), int(x[3:5],16), int(x[5:7],16))
234+
elif isinstance(x, (text_type, binary_type)):
235+
if x.startswith('#') and len(x)==7:
236+
# Color of the form #RRGGBB
237+
color_tuple = (int(x[1:3], 16),
238+
int(x[3:5], 16),
239+
int(x[5:7],16))
238240
else:
239241
color_code = _cnames.get(x.lower(),None)
240242
if color_code is None:
241-
raise ValueError('Unknown color.')
242-
color_tuple = (int(color_code[1:3],16), int(color_code[3:5],16), int(color_code[5:7],16))
243+
raise ValueError('Unknown color {!r}.'.format(x))
244+
color_tuple = (int(color_code[1:3], 16),
245+
int(color_code[3:5], 16),
246+
int(color_code[5:7],16))
243247
if max(color_tuple)>1.:
244248
color_tuple = tuple(u/255. for u in color_tuple)
245249
return tuple(map(float,(color_tuple+(1.,))[:4]))
246250

247-
_round = round
248-
249251
def _base(x):
250252
if x > 0:
251253
base = pow(10, math.floor(math.log10(x)))
@@ -256,49 +258,40 @@ def _base(x):
256258
class ColorMap(MacroElement):
257259
"""A generic class for creating colormaps."""
258260

259-
def __init__(self, min=0., max=1., caption=""):
261+
def __init__(self, vmin=0., vmax=1., caption=""):
260262
"""
261263
"""
262264
super(ColorMap, self).__init__()
263265
self._name = 'ColorMap'
264266

265-
self.min = min
266-
self.max = max
267+
self.vmin = vmin
268+
self.vmax = vmax
267269
self.caption = caption
268-
self.index = [min, max]
270+
self.index = [vmin, vmax]
269271

270272
self._template = self._env.get_template('color_scale.js')
271273

272274
def render(self, **kwargs):
273-
self.color_domain = [self.min + (self.max-self.min)*i/499. for i in range(500)]
275+
self.color_domain = [self.vmin + (self.vmax-self.vmin)*i/499. for i in range(500)]
274276
self.color_range = [self.__call__(x) for x in self.color_domain]
275277
self.tick_labels = legend_scaler(self.index)
276-
self.caption = ""
277278

278279
super(ColorMap, self).render(**kwargs)
279280

280281
figure = self.get_root()
281282
assert isinstance(figure, Figure), ("You cannot render this Element "
282283
"if it's not in a Figure.")
283284

284-
figure.header.add_children(
285-
JavascriptLink("https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"), # noqa
286-
name='d3')
285+
figure.header.add_children(JavascriptLink("https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"), name='d3') # noqa
287286

288-
def _rgba_floats_tuple(self, x):
287+
def rgba_floats_tuple(self, x):
289288
"""This class has to be implemented for each class inheriting from Colormap.
290289
This has to be a function of the form float -> (float,float,float,float)
291290
Describing for each input float x, the output color in RGBA format ;
292291
each output value being between 0 and 1.
293292
"""
294293
raise NotImplementedError
295294

296-
def rgba_floats_tuple(self, x):
297-
"""Provides the color corresponding to value `x` in the
298-
form of a tuple (R,G,B,A) with float values between 0. and 1.
299-
"""
300-
return self._rgba_floats_tuple(x)
301-
302295
def rgba_bytes_tuple(self, x):
303296
"""Provides the color corresponding to value `x` in the
304297
form of a tuple (R,G,B,A) with int values between 0 and 255.
@@ -332,15 +325,15 @@ def _repr_html_(self):
332325
'y2="20" style="stroke:{color};stroke-width:3;" />'
333326
).format(
334327
i=i*1,
335-
color = self.rgb_hex_str(self.min + (self.max-self.min)*i/499.))
328+
color = self.rgb_hex_str(self.vmin + (self.vmax-self.vmin)*i/499.))
336329
for i in range(500)
337330
])
338-
+ '<text x="0" y="35">{}</text>'.format(self.min)
339-
+ '<text x="500" y="35" style="text-anchor:end;">{}</text>'.format(self.max)
331+
+ '<text x="0" y="35">{}</text>'.format(self.vmin)
332+
+ '<text x="500" y="35" style="text-anchor:end;">{}</text>'.format(self.vmax)
340333
+ '</svg>')
341334

342335
class LinearColormap(ColorMap):
343-
def __init__(self, colors, index=None, min=0., max=1., caption=""):
336+
def __init__(self, colors, index=None, vmin=0., vmax=1., caption=""):
344337
"""Creates a ColorMap based on linear interpolation of a set of colors
345338
over a given index.
346339
@@ -356,32 +349,32 @@ def __init__(self, colors, index=None, min=0., max=1., caption=""):
356349
index : list of floats, default None
357350
The values corresponding to each color.
358351
It has to be sorted, and have the same length as `colors`.
359-
If None, a regular grid between `min` and `max` is created.
360-
min : float, default 0.
352+
If None, a regular grid between `vmin` and `vmax` is created.
353+
vmin : float, default 0.
361354
The minimal value for the colormap.
362-
Values lower than `min` will be bound directly to `colors[0]`.
363-
max : float, default 1.
355+
Values lower than `vmin` will be bound directly to `colors[0]`.
356+
vmax : float, default 1.
364357
The maximal value for the colormap.
365-
Values higher than `max` will be bound directly to `colors[-1]`.
358+
Values higher than `vmax` will be bound directly to `colors[-1]`.
366359
"""
367-
super(LinearColormap, self).__init__(min=min, max=max, caption=caption)
360+
super(LinearColormap, self).__init__(vmin=vmin, vmax=vmax, caption=caption)
368361

369362
n = len(colors)
370-
if n<2:
363+
if n < 2:
371364
raise ValueError('You must provide at least 2 colors.')
372365
if index is None:
373-
self.index = [min + (max-min)*i*1./(n-1) for i in range(n)]
366+
self.index = [vmin + (vmax-vmin)*i*1./(n-1) for i in range(n)]
374367
else:
375368
self.index = list(index).copy()
376369
self.colors = [_parse_color(x) for x in colors]
377370

378-
def _rgba_floats_tuple(self, x):
371+
def rgba_floats_tuple(self, x):
379372
"""Provides the color corresponding to value `x` in the
380373
form of a tuple (R,G,B,A) with float values between 0. and 1.
381374
"""
382-
if x<= self.index[0]:
375+
if x <= self.index[0]:
383376
return self.colors[0]
384-
if x>= self.index[-1]:
377+
if x >= self.index[-1]:
385378
return self.colors[-1]
386379

387380
i = len([u for u in self.index if u<x]) # 0 < i < n
@@ -395,7 +388,7 @@ def _rgba_floats_tuple(self, x):
395388
return tuple((1.-p) * self.colors[i-1][j] + p*self.colors[i][j] for j in range(4))
396389

397390
def to_step(self, n=None, index=None, data=None, method=None, quantiles=None,
398-
round=False, base_round=False):
391+
round_method=None):
399392
"""Splits the LinearColormap into a StepColormap.
400393
401394
Parameters
@@ -406,7 +399,7 @@ def to_step(self, n=None, index=None, data=None, method=None, quantiles=None,
406399
index : list of floats, default None
407400
The values corresponding to each color bounds.
408401
It has to be sorted.
409-
If None, a regular grid between `min` and `max` is created.
402+
If None, a regular grid between `vmin` and `vmax` is created.
410403
data : list of floats, default None
411404
A sample of data to adapt the color map to.
412405
method : str, default 'linear'
@@ -416,11 +409,11 @@ def to_step(self, n=None, index=None, data=None, method=None, quantiles=None,
416409
quantiles : list of floats, default None
417410
Alternatively, you can provide explicitely the quantiles you
418411
want to use in the scale.
419-
round : bool, default False
420-
If True, all values will be rounded.
421-
base_round : bool, default False
422-
If True, all values will be rounded to the nearest order-of-magnitude
423-
integer. For example, 2100 is rounded to 2000, 2790 to 3000.
412+
round_method : str, default None
413+
The method used to round thresholds.
414+
* If 'int', all values will be rounded to the nearest integer.
415+
* If 'log10', all values will be rounded to the nearest order-of-magnitude
416+
integer. For example, 2100 is rounded to 2000, 2790 to 3000.
424417
425418
Return
426419
------
@@ -434,19 +427,19 @@ def to_step(self, n=None, index=None, data=None, method=None, quantiles=None,
434427
>> lc.to_step(data=some_list, n=12, method='log')
435428
>> lc.to_step(data=some_list, n=12, method='quantiles')
436429
>> lc.to_step(data=some_list, quantiles=[0,0.3,0.7,1])
437-
>> lc.to_step(data=some_list, quantiles=[0,0.3,0.7,1], base_round=True)
430+
>> lc.to_step(data=some_list, quantiles=[0,0.3,0.7,1], round_method='log10')
438431
"""
439432
if index is None:
440433
if data is None:
441434
if n is None:
442435
raise ValueError('You must specify either `index`,`data` or `n`')
443436
else:
444-
index = [self.min + (self.max-self.min)*i*1./n for i in range(1+n)]
437+
index = [self.vmin + (self.vmax-self.vmin)*i*1./n for i in range(1+n)]
445438
scaled_cm = self
446439
else:
447440
max_ = max(data)
448441
min_ = min(data)
449-
scaled_cm = self.scale(min=min_, max=max_)
442+
scaled_cm = self.scale(vmin=min_, vmax=max_)
450443
method = ('quantiles' if quantiles is not None
451444
else method if method is not None
452445
else 'linear'
@@ -475,33 +468,33 @@ def to_step(self, n=None, index=None, data=None, method=None, quantiles=None,
475468
else:
476469
raise ValueError('Unknown method {}'.format(method))
477470
else:
478-
scaled_cm = self.scale(min=min(index), max=max(index))
471+
scaled_cm = self.scale(vmin=min(index), vmax=max(index))
479472

480473
n = len(index)-1
481474

482-
if round:
483-
index = [_round(x) for x in index]
475+
if round_method=='int':
476+
index = [round(x) for x in index]
484477

485-
if base_round:
478+
if round_method=='log10':
486479
index = [_base(x) for x in index]
487480

488-
colors = [scaled_cm._rgba_floats_tuple(index[i]*(1.-i/(n-1.))+index[i+1]*i/(n-1.)) for i in range(n)]
481+
colors = [scaled_cm.rgba_floats_tuple(index[i]*(1.-i/(n-1.))+index[i+1]*i/(n-1.)) for i in range(n)]
489482

490-
return StepColormap(colors, index=index, min=index[0], max=index[-1])
483+
return StepColormap(colors, index=index, vmin=index[0], vmax=index[-1])
491484

492-
def scale(self, min=0., max=1.):
485+
def scale(self, vmin=0., vmax=1.):
493486
"""Transforms the colorscale so that the minimal and maximal values
494487
fit the given parameters.
495488
"""
496489
return LinearColormap(
497490
self.colors,
498-
index = [min + (max-min)*(x-self.min)*1./(self.max-self.min) for x in self.index],
499-
min = min,
500-
max = max,
491+
index = [vmin + (vmax-vmin)*(x-self.vmin)*1./(self.vmax-self.vmin) for x in self.index],
492+
vmin = vmin,
493+
vmax = vmax,
501494
)
502495

503496
class StepColormap(ColorMap):
504-
def __init__(self, colors, index=None, min=0., max=1., caption=""):
497+
def __init__(self, colors, index=None, vmin=0., vmax=1., caption=""):
505498
"""Creates a ColorMap based on stepwise constant colorfunction.
506499
507500
Parameters
@@ -516,26 +509,26 @@ def __init__(self, colors, index=None, min=0., max=1., caption=""):
516509
index : list of floats, default None
517510
The values corresponding to each color.
518511
It has to be sorted, and its length must be equal to `len(colors)+1`.
519-
If None, a regular grid between `min` and `max` is created.
520-
min : float, default 0.
512+
If None, a regular grid between `vmin` and `vmax` is created.
513+
vmin : float, default 0.
521514
The minimal value for the colormap.
522-
Values lower than `min` will be bound directly to `colors[0]`.
523-
max : float, default 1.
515+
Values lower than `vmin` will be bound directly to `colors[0]`.
516+
vmax : float, default 1.
524517
The maximal value for the colormap.
525-
Values higher than `max` will be bound directly to `colors[-1]`.
518+
Values higher than `vmax` will be bound directly to `colors[-1]`.
526519
"""
527-
super(StepColormap, self).__init__(min=min, max=max, caption=caption)
520+
super(StepColormap, self).__init__(vmin=vmin, vmax=vmax, caption=caption)
528521

529522
n = len(colors)
530523
if n<1:
531524
raise ValueError('You must provide at least 1 colors.')
532525
if index is None:
533-
self.index = [min + (max-min)*i*1./n for i in range(n+1)]
526+
self.index = [vmin + (vmax-vmin)*i*1./n for i in range(n+1)]
534527
else:
535528
self.index = list(index).copy()
536529
self.colors = [_parse_color(x) for x in colors]
537530

538-
def _rgba_floats_tuple(self, x):
531+
def rgba_floats_tuple(self, x):
539532
"""Provides the color corresponding to value `x` in the
540533
form of a tuple (R,G,B,A) with float values between 0. and 1.
541534
"""
@@ -555,24 +548,24 @@ def to_linear(self, index=None):
555548
index : list of floats, default None
556549
The values corresponding to each color in the output colormap.
557550
It has to be sorted.
558-
If None, a regular grid between `min` and `max` is created.
551+
If None, a regular grid between `vmin` and `vmax` is created.
559552
"""
560553
if index is None:
561554
n = len(self.index)-1
562555
index = [self.index[i]*(1.-i/(n-1.))+self.index[i+1]*i/(n-1.) for i in range(n)]
563556

564-
colors = [self._rgba_floats_tuple(x) for x in index]
565-
return LinearColormap(colors, index=index, min=self.min, max=self.max)
557+
colors = [self.rgba_floats_tuple(x) for x in index]
558+
return LinearColormap(colors, index=index, vmin=self.vmin, vmax=self.vmax)
566559

567-
def scale(self, min=0., max=1.):
560+
def scale(self, vmin=0., vmax=1.):
568561
"""Transforms the colorscale so that the minimal and maximal values
569562
fit the given parameters.
570563
"""
571564
return StepColormap(
572565
self.colors,
573-
index = [min + (max-min)*(x-self.min)*1./(self.max-self.min) for x in self.index],
574-
min = min,
575-
max = max,
566+
index = [vmin + (vmax-vmin)*(x-self.vmin)*1./(self.vmax-self.vmin) for x in self.index],
567+
vmin = vmin,
568+
vmax = vmax,
576569
)
577570

578571
class _LinearColormaps(object):
@@ -583,6 +576,7 @@ def __init__(self):
583576
self._colormaps = {key : LinearColormap(val) for key,val in _schemes.items()}
584577
for key,val in _schemes.items():
585578
setattr(self,key, LinearColormap(val))
579+
586580
def _repr_html_(self):
587581
return Template("""
588582
<table>

0 commit comments

Comments
 (0)