Skip to content

Commit 57e15bd

Browse files
committed
pep8 and docsrings
1 parent 4afc8c9 commit 57e15bd

File tree

1 file changed

+91
-52
lines changed

1 file changed

+91
-52
lines changed

plotly/tools.py

Lines changed: 91 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,9 +2285,15 @@ def create_candlestick(open, high, low, close,
22852285

22862286
@staticmethod
22872287
def create_dendrogram(X, orientation="bottom", labels=None,
2288-
colorscale=None, **kwargs):
2288+
colorscale=None):
22892289
"""
2290-
Returns a dendrogram Plotly figure object.
2290+
BETA function that returns a dendrogram Plotly figure object.
2291+
2292+
:param (ndarray) X: Matrix of observations as arrray of arrays
2293+
:param (str) orientation: 'top', 'right', 'bottom', or 'left'
2294+
:param (list) labels: List of axis category labels(observation labels)
2295+
:param (list) colorscale: Optional colorscale for dendrogram tree
2296+
clusters
22912297
22922298
X: Heatmap matrix as array of arrays
22932299
orientation: 'top', 'right', 'bottom', or 'left'
@@ -2302,7 +2308,7 @@ def create_dendrogram(X, orientation="bottom", labels=None,
23022308
23032309
import numpy as np
23042310
2305-
X = np.random.rand(5,5)
2311+
X = np.random.rand(5,5)
23062312
dendro_X = FF.create_dendrogram(X)
23072313
py.iplot(dendro_X, validate=False, height=300, width=1000)
23082314
@@ -2319,15 +2325,19 @@ def create_dendrogram(X, orientation="bottom", labels=None,
23192325
"""
23202326

23212327
if _scipy_imported is False:
2322-
raise ImportError("FigureFactory.create_dendrogram requires scipy, scipy.spatial and scipy.hierarchy")
2328+
raise ImportError("FigureFactory.create_dendrogram requires scipy,
2329+
scipy.spatial and scipy.hierarchy")
23232330

23242331
s = X.shape
23252332
if len(s) != 2:
23262333
exceptions.PlotlyError("X should be 2-dimensional array.")
23272334

23282335
dendrogram = _Dendrogram(X, orientation, labels, colorscale)
23292336

2330-
return {'layout': dendrogram.layout, 'data': dendrogram.data, 'labels': dendrogram.labels}
2337+
return {'layout': dendrogram.layout,
2338+
'data': dendrogram.data,
2339+
'labels': dendrogram.labels}
2340+
23312341

23322342
class _Quiver(FigureFactory):
23332343
"""
@@ -2925,34 +2935,34 @@ def get_candle_decrease(self):
29252935
stick_decrease_y, stick_decrease_x)
29262936

29272937
class _Dendrogram(FigureFactory):
2928-
''' Returns a Dendrogram figure object
2929-
Example usage:
2930-
D = Dendrogram( Z )
2931-
fig = { 'data':D.data, 'layout':D.layout }
2932-
py.iplot( fig, filename='Dendro', validate=False )'''
29332938

2939+
"""
2940+
Refer to FigureFactory.create_dendrogram() for docstring.
2941+
"""
2942+
29342943
def __init__(self, X, orientation='bottom', labels=None, colorscale=None, \
29352944
width="100%", height="100%", xaxis='xaxis', yaxis='yaxis' ):
2936-
''' Draw a 2d dendrogram tree
2937-
X: Heatmap matrix as array of arrays
2938-
orientation: 'top', 'right', 'bottom', or 'left'
2939-
labels: List of axis category labels
2940-
colorscale: Optional colorscale for dendrogram tree clusters
2941-
Returns a dendrogram Plotly figure object '''
2942-
29432945
self.orientation = orientation
29442946
self.labels = labels
29452947
self.xaxis = xaxis
29462948
self.yaxis = yaxis
29472949
self.data = []
29482950
self.leaves = []
2949-
self.sign = { self.xaxis:1, self.yaxis:1 }
2950-
self.layout = { self.xaxis:{}, self.yaxis:{} }
2951-
2952-
self.sign[self.xaxis] = 1 if self.orientation in ['left','bottom'] else -1
2953-
self.sign[self.yaxis] = 1 if self.orientation in ['right','bottom'] else -1
2951+
self.sign = {self.xaxis: 1, self.yaxis: 1}
2952+
self.layout = {self.xaxis: {}, self.yaxis: {}}
2953+
2954+
if self.orientation in ['left', 'bottom']:
2955+
self.sign[self.xaxis] = 1
2956+
else:
2957+
self.sign[self.xaxis] = -1
2958+
2959+
if self.orientation in ['right', 'bottom']:
2960+
self.sign[self.yaxis] = 1
2961+
else:
2962+
self.sign[self.yaxis] = -1
29542963

2955-
dd_traces, xvals, yvals, ordered_labels, leaves = self.get_dendrogram_traces( X, colorscale )
2964+
dd_traces, xvals, yvals,
2965+
ordered_labels, leaves = self.get_dendrogram_traces(X, colorscale)
29562966

29572967
self.labels = ordered_labels
29582968
self.leaves = leaves
@@ -2971,7 +2981,11 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None, \
29712981
self.data = Data(dd_traces)
29722982

29732983
def get_color_dict(self, colorscale):
2974-
''' Return colorscale used for dendrogram tree clusters '''
2984+
"""
2985+
Returns colorscale used for dendrogram tree clusters
2986+
:param (list) colorscale: colors to use for the plot,
2987+
in rgb format
2988+
"""
29752989

29762990
# These are the color codes returned for dendrograms
29772991
# We're replacing them with nicer colors
@@ -3004,9 +3018,11 @@ def get_color_dict(self, colorscale):
30043018
return default_colors
30053019

30063020
def set_axis_layout(self, axis_key):
3007-
''' Sets and returns default axis object for dendrogram figure
3008-
axis_key: "xaxis", "xaxis1", "yaxis", yaxis1", etc '''
3009-
3021+
"""
3022+
Sets and returns default axis object for dendrogram figure
3023+
:param (str) axis_key: "xaxis", "xaxis1", "yaxis", yaxis1", etc.
3024+
"""
3025+
30103026
axis_defaults = {
30113027
'type': 'linear',
30123028
'ticks': 'outside',
@@ -3024,7 +3040,8 @@ def set_axis_layout(self, axis_key):
30243040
axis_key_labels = self.yaxis
30253041
if axis_key_labels not in self.layout:
30263042
self.layout[axis_key_labels] = {}
3027-
self.layout[axis_key_labels]['tickvals'] = [ea*self.sign[axis_key] for ea in self.zero_vals]
3043+
self.layout[axis_key_labels]['tickvals'] = [ea*self.sign[axis_key]
3044+
for ea in self.zero_vals]
30283045
self.layout[axis_key_labels]['ticktext'] = self.labels
30293046
self.layout[axis_key_labels]['tickmode'] = 'array'
30303047

@@ -3033,11 +3050,13 @@ def set_axis_layout(self, axis_key):
30333050
return self.layout[axis_key]
30343051

30353052
def set_figure_layout(self, width, height):
3036-
''' Sets and returns default layout object for dendrogram figure '''
3053+
"""
3054+
Sets and returns default layout object for dendrogram figure
3055+
"""
30373056

30383057
self.layout.update({
30393058
'showlegend': False,
3040-
'autoscale': False,
3059+
'autoscale': False,
30413060
'hovermode': 'closest',
30423061
'width': width,
30433062
'width': height
@@ -3048,35 +3067,55 @@ def set_figure_layout(self, width, height):
30483067

30493068
return self.layout
30503069

3051-
def get_dendrogram_traces( self, X, colorscale ):
3052-
''' Returns a tuple with:
3053-
(a) List of Plotly trace objects for the dendrogram tree
3054-
(b) icoord: All X points of the dendogram tree as array of arrays with length 4
3055-
(c) dcoord: All Y points of the dendogram tree as array of arrays with length 4 '''
3070+
def get_dendrogram_traces(self, X, colorscale):
3071+
"""
3072+
Calculates all the elements needed for plotting a dendrogram
3073+
3074+
:rtype (tuple): Contains all the traces in the following order
3075+
(a) trace_list: List of Plotly trace objects for the dendrogram
3076+
tree
3077+
(b) icoord: All X points of the dendogram tree as array of arrays
3078+
with length 4
3079+
(c) dcoord: All Y points of the dendogram tree as array of arrays
3080+
with length 4
3081+
(d) ordered_labels: leaf labels in the order they are going to
3082+
appear on the plot
3083+
(e) P['leaves']: left-to-right traversal of the leaves
3084+
"""
30563085

30573086
d = scs.distance.pdist(X)
3058-
Z = sch.linkage(d, method='complete')
3059-
P = sch.dendrogram(Z, orientation=self.orientation,labels=self.labels, no_plot=True)
3060-
3061-
icoord = scp.array( P['icoord'] )
3062-
dcoord = scp.array( P['dcoord'] )
3063-
ordered_labels = scp.array( P['ivl'] )
3064-
color_list = scp.array( P['color_list'] )
3065-
colors = self.get_color_dict( colorscale )
3087+
Z = sch.linkage(d, method='complete')
3088+
P = sch.dendrogram(Z, orientation=self.orientation,
3089+
labels=self.labels, no_plot=True)
3090+
3091+
icoord = scp.array(P['icoord'])
3092+
dcoord = scp.array(P['dcoord'])
3093+
ordered_labels = scp.array(P['ivl'])
3094+
color_list = scp.array(P['color_list'])
3095+
colors = self.get_color_dict(colorscale)
30663096

30673097
trace_list = []
30683098

30693099
for i in range(len(icoord)):
3070-
# xs and ys are arrays of 4 points that make up the '∩' shapes of the dendrogram tree
3071-
xs = icoord[i] if self.orientation in ['top','bottom'] else dcoord[i]
3072-
ys = dcoord[i] if self.orientation in ['top','bottom'] else icoord[i]
3073-
color_key = color_list[i]
3074-
trace = Scatter(x=np.multiply(self.sign[self.xaxis],xs), \
3075-
y=np.multiply(self.sign[self.yaxis],ys), \
3076-
mode='lines', marker=Marker(color=colors[color_key]) )
3100+
# xs and ys are arrays of 4 points that make up the '∩' shapes
3101+
# of the dendrogram tree
3102+
if self.orientation in ['top', 'bottom']:
3103+
xs = icoord[i]
3104+
else:
3105+
xs = dcoord[i]
3106+
3107+
if self.orientation in ['top', 'bottom']:
3108+
ys = dcoord[i]
3109+
else:
3110+
ys = icoord[i]
3111+
color_key = color_list[i]
3112+
trace = Scatter(x=np.multiply(self.sign[self.xaxis], xs),
3113+
y=np.multiply(self.sign[self.yaxis], ys),
3114+
mode='lines',
3115+
marker=Marker(color=colors[color_key]))
30773116
trace['xaxis'] = 'x'+self.xaxis[-1]
30783117
trace['yaxis'] = 'y'+self.yaxis[-1]
3079-
trace_list.append( trace )
3080-
3118+
trace_list.append(trace)
3119+
30813120
return trace_list, icoord, dcoord, ordered_labels, P['leaves']
30823121

0 commit comments

Comments
 (0)