Skip to content

Speed up graph objs #678

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

Merged
merged 7 commits into from
Feb 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added
- `memoize` decorator added to `plotly.utils`

### Changed
- a `Grid` from `plotly.grid_objs` now accepts a `pandas.Dataframe` as its argument.
- computationally-intensive `graph_reference` functions are memoized.

## [2.0.0] - 2017-01-25

Expand Down
4 changes: 3 additions & 1 deletion plotly/graph_objs/graph_objs.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
from plotly import exceptions, graph_reference
from plotly.graph_objs import graph_objs_tools

_subplot_regex = re.compile(r'(?P<digits>\d+$)')


class PlotlyBase(object):
"""
Expand Down Expand Up @@ -506,7 +508,7 @@ def _get_subplot_attributes(self):

def _get_subplot_key(self, key):
"""Some keys can have appended integers, this handles that."""
match = re.search(r'(?P<digits>\d+$)', key)
match = _subplot_regex.search(key)
if match:
root_key = key[:match.start()]
if (root_key in self._get_subplot_attributes() and
Expand Down
65 changes: 43 additions & 22 deletions plotly/graph_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ def get_attributes_dicts(object_name, parent_object_names=()):
return attributes_dicts


def get_valid_attributes(object_name, parent_object_names=()):
@utils.memoize()
def _get_valid_attributes(object_name, parent_object_names):
attributes = get_attributes_dicts(object_name, parent_object_names)
# These are for documentation and quick lookups. They're just strings.
valid_attributes = set()
Expand All @@ -245,6 +246,11 @@ def get_valid_attributes(object_name, parent_object_names=()):
return valid_attributes


def get_valid_attributes(object_name, parent_object_names=()):
# Enforce that parent_object_names is hashable (a tuple).
return _get_valid_attributes(object_name, tuple(parent_object_names))


def get_deprecated_attributes(object_name, parent_object_names=()):
attributes = get_attributes_dicts(object_name, parent_object_names)
# These are for documentation and quick lookups. They're just strings.
Expand Down Expand Up @@ -340,21 +346,10 @@ def attribute_path_to_object_names(attribute_container_path):
return tuple(object_names)


def get_role(object_name, attribute, value=None, parent_object_names=()):
"""
Values have types associated with them based on graph_reference.

'data' type values are always kept
'style' values are kept if they're sequences (but not strings)

:param (str) object_name: The name of the object containing 'attribute'.
:param (str) attribute: The attribute we want the `role` of.
:param (*) value: If the value is an array, the return can be different.
:param parent_object_names: An iterable of obj names from graph reference.
:returns: (str) This will be 'data', 'style', or 'info'.

"""
if object_name in TRACE_NAMES and attribute == 'type':
@utils.memoize()
def _get_role(object_name, attribute, value_type, parent_object_names=()):
"""Private, more easily memoized version of get_role."""
if attribute == 'type' and object_name in TRACE_NAMES:
return 'info'
attributes_dicts = get_attributes_dicts(object_name, parent_object_names)
matches = []
Expand All @@ -372,12 +367,8 @@ def get_role(object_name, attribute, value=None, parent_object_names=()):
for match in matches:
role = match['role']
array_ok = match.get('arrayOk')
if value is not None and array_ok:
iterable = hasattr(value, '__iter__')
stringy = isinstance(value, six.string_types)
dicty = isinstance(value, dict)
if iterable and not stringy and not dicty:
role = 'data'
if array_ok and value_type == 'array':
role = 'data'
roles.append(role)

# TODO: this is ambiguous until the figure is in place...
Expand All @@ -388,6 +379,36 @@ def get_role(object_name, attribute, value=None, parent_object_names=()):
return role


def get_role(object_name, attribute, value=None, parent_object_names=()):
"""
Values have types associated with them based on graph_reference.

'data' type values are always kept
'style' values are kept if they're sequences (but not strings)

:param (str) object_name: The name of the object containing 'attribute'.
:param (str) attribute: The attribute we want the `role` of.
:param (*) value: If the value is an array, the return can be different.
:param parent_object_names: An iterable of obj names from graph reference.
:returns: (str) This will be 'data', 'style', or 'info'.

"""
if value is None:
value_type = 'none'
elif isinstance(value, dict):
value_type = 'dict'
elif isinstance(value, six.string_types):
value_type = 'string'
elif hasattr(value, '__iter__'):
value_type = 'array'
else:
value_type = 'unknown'

# Enforce that parent_object_names is hashable (a tuple).
return _get_role(object_name, attribute, value_type,
tuple(parent_object_names))


def _is_valid_sub_path(path, parent_paths):
"""
Check if a sub path is valid given an iterable of parent paths.
Expand Down
Loading