Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run black on the codebase #599

Merged
merged 7 commits into from
Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 1 addition & 7 deletions chaco/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,4 @@
"""
from ._version import full_version as __version__ # noqa

__requires__ = [
'traits',
'traitsui',
'pyface',
'numpy',
'enable'
]
__requires__ = ["traits", "traitsui", "pyface", "numpy", "enable"]
113 changes: 68 additions & 45 deletions chaco/_speedups_fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,27 @@
_speedups extension module.
"""

from numpy import clip, invert, isnan, isinf, array, transpose, zeros, \
compress, where, take, float32, ones_like
from numpy import (
clip,
invert,
isnan,
isinf,
array,
transpose,
zeros,
compress,
where,
take,
float32,
ones_like,
)
import numpy as np

import operator


def array_combine(a, b, op=operator.and_, func=lambda x: x):
""" Returns op(func(a), func(b)) if a and b are both not None;
"""Returns op(func(a), func(b)) if a and b are both not None;
if one is None, then returns func() on the non-None array;
if both are None, then returns None.
"""
Expand All @@ -24,10 +37,20 @@ def array_combine(a, b, op=operator.and_, func=lambda x: x):
return None


def scatterplot_gather_points(index, index_low, index_high,
value, value_low, value_high,
index_mask=None, index_sel=None, index_sel_mask=None,
value_mask=None, value_sel=None, value_sel_mask=None):
def scatterplot_gather_points(
index,
index_low,
index_high,
value,
value_low,
value_high,
index_mask=None,
index_sel=None,
index_sel_mask=None,
value_mask=None,
value_sel=None,
value_sel_mask=None,
):
"""
Takes index and value arrays, masks, and optional selection arrays,
and returns the list of points and corresponding selection mask for
Expand Down Expand Up @@ -74,8 +97,9 @@ def scatterplot_gather_points(index, index_low, index_high,
index_range_mask = (index_low < index) & (index < index_high)
value_range_mask = (value_low < value) & (value < value_high)

nan_mask = array_combine(index_mask, value_mask,
func = lambda x: invert(isnan(x)) & x)
nan_mask = array_combine(
index_mask, value_mask, func=lambda x: invert(isnan(x)) & x
)

if nan_mask is not None:
point_mask = nan_mask & index_range_mask & value_range_mask
Expand Down Expand Up @@ -112,9 +136,8 @@ def scatterplot_gather_points(index, index_low, index_high,
return points, selections



def apply_selection_fade(mapped_image, mask, fade_alpha, fade_background):
'''Apply a selection fade to a colormapped image.
"""Apply a selection fade to a colormapped image.

Parameters
----------
Expand All @@ -127,22 +150,23 @@ def apply_selection_fade(mapped_image, mask, fade_alpha, fade_background):
fade_background : rgb888 tuple
The fade background

'''
"""
imask = invert(mask)
if fade_alpha == 0:
mapped_image[imask,0:3] = fade_background
mapped_image[imask, 0:3] = fade_background
else:
ialpha = (1.0 - fade_alpha)
ialpha = 1.0 - fade_alpha
background = tuple(ialpha * x for x in fade_background)
image_region = mapped_image[imask,0:3]
image_region = mapped_image[imask, 0:3]
image_region *= fade_alpha
image_region += background
mapped_image[imask,0:3] = image_region
mapped_image[imask, 0:3] = image_region


def map_colors(data_array, steps, low, high, red_lut, green_lut, blue_lut,
alpha_lut):
'''Map colors from color lookup tables to a data array.
def map_colors(
data_array, steps, low, high, red_lut, green_lut, blue_lut, alpha_lut
):
"""Map colors from color lookup tables to a data array.

This is used in ColorMapper.map_screen

Expand All @@ -164,37 +188,38 @@ def map_colors(data_array, steps, low, high, red_lut, green_lut, blue_lut,
The blue channel lookup table
alpha_lut : ndarray of float32
The alpha channel lookup table

Returns
-------
rgba: ndarray of float32
The rgba values of data_array according to the lookup tables. The shape
of this array is equal to data_array.shape + (4,).

'''
"""
range_diff = high - low

if range_diff == 0.0 or isinf(range_diff):
# Handle null range, or infinite range (which can happen during
# Handle null range, or infinite range (which can happen during
# initialization before range is connected to a data source).
norm_data = 0.5*ones_like(data_array)
norm_data = 0.5 * ones_like(data_array)
else:
norm_data = clip((data_array - low) / range_diff, 0.0, 1.0)


nanmask = isnan(norm_data)
norm_data = where(nanmask, 0, (norm_data * (steps-1)).astype(int))
rgba = zeros(norm_data.shape+(4,), float32)
rgba[...,0] = where(nanmask, 0, take(red_lut, norm_data))
rgba[...,1] = where(nanmask, 0, take(green_lut, norm_data))
rgba[...,2] = where(nanmask, 0, take(blue_lut, norm_data))
rgba[...,3] = where(nanmask, 0, take(alpha_lut, norm_data))
norm_data = where(nanmask, 0, (norm_data * (steps - 1)).astype(int))
rgba = zeros(norm_data.shape + (4,), float32)
rgba[..., 0] = where(nanmask, 0, take(red_lut, norm_data))
rgba[..., 1] = where(nanmask, 0, take(green_lut, norm_data))
rgba[..., 2] = where(nanmask, 0, take(blue_lut, norm_data))
rgba[..., 3] = where(nanmask, 0, take(alpha_lut, norm_data))

return rgba

def map_colors_uint8(data_array, steps, low, high, red_lut, green_lut, blue_lut,
alpha_lut):
'''Map colors from color lookup tables to a data array.

def map_colors_uint8(
data_array, steps, low, high, red_lut, green_lut, blue_lut, alpha_lut
):
"""Map colors from color lookup tables to a data array.

This is used in ColorMapper.map_screen

Expand All @@ -216,31 +241,29 @@ def map_colors_uint8(data_array, steps, low, high, red_lut, green_lut, blue_lut,
The blue channel lookup table
alpha_lut : ndarray of uint8
The alpha channel lookup table

Returns
-------
rgba: ndarray of uint8
The rgba values of data_array according to the lookup tables. The shape
of this array is equal to data_array.shape + (4,).

'''
"""
range_diff = high - low

if range_diff == 0.0 or isinf(range_diff):
# Handle null range, or infinite range (which can happen during
# Handle null range, or infinite range (which can happen during
# initialization before range is connected to a data source).
norm_data = 0.5*ones_like(data_array)
norm_data = 0.5 * ones_like(data_array)
else:
norm_data = clip((data_array - low) / range_diff, 0.0, 1.0)


nanmask = isnan(norm_data)
norm_data = where(nanmask, 0, (norm_data * (steps-1)).astype('uint8'))
rgba = zeros(norm_data.shape+(4,), dtype='uint8')
rgba[...,0] = where(nanmask, 0, take(red_lut, norm_data))
rgba[...,1] = where(nanmask, 0, take(green_lut, norm_data))
rgba[...,2] = where(nanmask, 0, take(blue_lut, norm_data))
rgba[...,3] = where(nanmask, 0, take(alpha_lut, norm_data))
norm_data = where(nanmask, 0, (norm_data * (steps - 1)).astype("uint8"))
rgba = zeros(norm_data.shape + (4,), dtype="uint8")
rgba[..., 0] = where(nanmask, 0, take(red_lut, norm_data))
rgba[..., 1] = where(nanmask, 0, take(green_lut, norm_data))
rgba[..., 2] = where(nanmask, 0, take(blue_lut, norm_data))
rgba[..., 3] = where(nanmask, 0, take(alpha_lut, norm_data))

return rgba

5 changes: 3 additions & 2 deletions chaco/abstract_colormap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from .data_range_1d import DataRange1D


class AbstractColormap(HasTraits):
"""
Abstract class for color maps, which map from scalar values to color values.
Expand All @@ -13,7 +14,7 @@ class AbstractColormap(HasTraits):
range = Instance(DataRange1D)

# The color depth of the colors to use.
color_depth = Enum('rgba', 'rgb')
color_depth = Enum("rgba", "rgb")

# A generic "update" event that generally means that anything that relies
# on this mapper for visual output should do a redraw or repaint.
Expand Down Expand Up @@ -65,4 +66,4 @@ def map_uint8(self, val):
**color_depth** setting.
"""
# default implementation (not efficient)
return (self.map_screen(val)*255.0).astype('uint8')
return (self.map_screen(val) * 255.0).astype("uint8")
2 changes: 1 addition & 1 deletion chaco/abstract_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, component, *args, **kw):
super(AbstractController, self).__init__(*args, **kw)

def deactivate(self, component):
""" This method is called by the component when this controller is no
"""This method is called by the component when this controller is no
longer the active tool.
"""
pass
36 changes: 18 additions & 18 deletions chaco/abstract_data_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@


class AbstractDataRange(HasTraits):
""" Abstract class for ranges that represent sub-regions of data space.
"""Abstract class for ranges that represent sub-regions of data space.

They support "autoscaling" by querying their associated data sources.
"""
Expand All @@ -33,42 +33,43 @@ class AbstractDataRange(HasTraits):
high = Float(1.0)

#: Setting for the lower bound of this range.
low_setting = Trait('auto', 'auto', Float)
low_setting = Trait("auto", "auto", Float)
#: Setting for the upper bound of this range.
high_setting = Trait('auto', 'auto', Float)
high_setting = Trait("auto", "auto", Float)

#: Event that is fired when the actual bounds values change; the value
#: of the event is a tuple (low_bound, high_bound)
updated = Event

#------------------------------------------------------------------------
# ------------------------------------------------------------------------
# Concrete methods
#------------------------------------------------------------------------
# ------------------------------------------------------------------------

def __init__(self, *sources, **kwargs):
if len(sources) > 0:
if 'sources' in kwargs:
raise RuntimeError("Datasources for data range provided as "
"both positional and keyword arguments.")
if "sources" in kwargs:
raise RuntimeError(
"Datasources for data range provided as "
"both positional and keyword arguments."
)
else:
kwargs['sources'] = list(sources)
kwargs["sources"] = list(sources)
super(AbstractDataRange, self).__init__(**kwargs)


#------------------------------------------------------------------------
# ------------------------------------------------------------------------
# Abstract methods that subclasses must implement
#------------------------------------------------------------------------
# ------------------------------------------------------------------------

def clip_data(self, data):
""" Returns a list of data values that are within the range.
"""Returns a list of data values that are within the range.

Given an array of data values of the same dimensionality as the range,
returns a list of data values that are inside the range.
"""
raise NotImplementedError

def mask_data(self, data):
""" Returns a mask array, indicating whether values in the given array
"""Returns a mask array, indicating whether values in the given array
are inside the range.

Given an array of data values of the same dimensionality as the range,
Expand All @@ -79,7 +80,7 @@ def mask_data(self, data):
raise NotImplementedError

def bound_data(self, data):
""" Returns a tuple of indices for the start and end of the first run
"""Returns a tuple of indices for the start and end of the first run
of data that falls within the range.

Given an array of data values of the same dimensionality as the range,
Expand All @@ -93,7 +94,7 @@ def bound_data(self, data):
raise NotImplementedError

def set_bounds(self, *new_bounds):
""" Sets all the bounds of the range simultaneously.
"""Sets all the bounds of the range simultaneously.

Because each bounds change probably fires an event, this method allows
tools to set all range elements in a single, atomic step.
Expand All @@ -112,10 +113,9 @@ def set_bounds(self, *new_bounds):
raise NotImplementedError

def _refresh_bounds(self):
""" Resets the values of the bounds depending on the data sources
"""Resets the values of the bounds depending on the data sources
referenced by the range.

This method is called only if one of the bounds settings is "auto".
"""
raise NotImplementedError

14 changes: 7 additions & 7 deletions chaco/abstract_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
# Local relative imports
from .base import DimensionTrait


class AbstractDataSource(HasTraits):
""" This abstract interface must be implemented by any class supplying data
"""This abstract interface must be implemented by any class supplying data
to Chaco.

Chaco does not have a notion of a "data format". For the most part, a data
Expand Down Expand Up @@ -52,9 +53,9 @@ class AbstractDataSource(HasTraits):
#: the datasource is serialized?
persist_data = Bool(True)

#------------------------------------------------------------------------
# ------------------------------------------------------------------------
# Abstract methods
#------------------------------------------------------------------------
# ------------------------------------------------------------------------

def get_data(self):
"""get_data() -> data_array
Expand Down Expand Up @@ -113,17 +114,16 @@ def get_bounds(self):
"""
raise NotImplementedError


### Persistence ###########################################################

def _metadata_default(self):
return {"selections":[], "annotations":[]}
return {"selections": [], "annotations": []}

def __getstate__(self):
state = super(AbstractDataSource,self).__getstate__()
state = super(AbstractDataSource, self).__getstate__()

# everything but 'metadata'
for key in ['value_dimension', 'index_dimension', 'persist_data']:
for key in ["value_dimension", "index_dimension", "persist_data"]:
if key in state:
del state[key]

Expand Down
Loading