|
1 | | -from scipy.spatial.distance import pdist, squareform |
2 | | -from plotly.tools import FigureFactory as FF |
3 | | -from plotly.graph_objs import * |
4 | | -import plotly.plotly as py |
5 | | -from glob import glob |
6 | | -import numpy as np |
7 | | -import pickle |
| 1 | +# A quick example of making a package tree with data derived from calculate_similarity.py |
8 | 2 |
|
9 | | -base = '/home/vanessa/Documents/Dropbox/Code/singularity/singularity-python' |
10 | | -analysis_directory = "%s/examples/package_tree" %(base) |
11 | | - |
12 | | - |
13 | | -# From https://plot.ly/python/dendrogram/, thanks plotly! :) |
| 3 | +from singularity.views.trees import make_package_tree |
14 | 4 |
|
15 | 5 | data = pickle.load(open('comparisons.pkl','rb'))['files.txt'] |
16 | | - |
17 | | -#lookup = {x:x.split("-")[0] for x in data.index.tolist()} |
18 | | -# labels = [lookup[x] for x in data.index.tolist()] |
19 | | - |
20 | | -labels = data.index.tolist() |
21 | | -# Initialize figure by creating upper dendrogram |
22 | | -figure = FF.create_dendrogram(data, orientation='bottom', labels=labels) |
23 | | -for i in range(len(figure['data'])): |
24 | | - figure['data'][i]['yaxis'] = 'y2' |
25 | | - |
26 | | -# Create Side Dendrogram |
27 | | -dendro_side = FF.create_dendrogram(data, orientation='right') |
28 | | -for i in range(len(dendro_side['data'])): |
29 | | - dendro_side['data'][i]['xaxis'] = 'x2' |
30 | | - |
31 | | -# Add Side Dendrogram Data to Figure |
32 | | -figure['data'].extend(dendro_side['data']) |
33 | | - |
34 | | -# Create Heatmap |
35 | | -dendro_leaves = dendro_side['layout']['yaxis']['ticktext'] |
36 | | -dendro_leaves = list(map(int, dendro_leaves)) |
37 | | -data_dist = pdist(data) |
38 | | -heat_data = squareform(data_dist) |
39 | | -heat_data = heat_data[dendro_leaves,:] |
40 | | -heat_data = heat_data[:,dendro_leaves] |
41 | | - |
42 | | -heatmap = Data([ |
43 | | - Heatmap( |
44 | | - x = dendro_leaves, |
45 | | - y = dendro_leaves, |
46 | | - z = heat_data, |
47 | | - colorscale = 'YIGnBu' |
48 | | - ) |
49 | | -]) |
50 | | - |
51 | | -heatmap[0]['x'] = figure['layout']['xaxis']['tickvals'] |
52 | | -heatmap[0]['y'] = dendro_side['layout']['yaxis']['tickvals'] |
53 | | - |
54 | | -# Add Heatmap Data to Figure |
55 | | -figure['data'].extend(Data(heatmap)) |
56 | | - |
57 | | - # Edit Layout |
58 | | - figure['layout'].update({'width':800, 'height':800, |
59 | | - 'showlegend':False, 'hovermode': 'closest', |
60 | | - }) |
61 | | - |
62 | | - # Edit xaxis |
63 | | - figure['layout']['xaxis'].update({'domain': [.15, 1], |
64 | | - 'mirror': False, |
65 | | - 'showgrid': False, |
66 | | - 'showline': False, |
67 | | - 'zeroline': False, |
68 | | - 'ticks':""}) |
69 | | - # Edit xaxis2 |
70 | | - figure['layout'].update({'xaxis2': {'domain': [0, .15], |
71 | | - 'mirror': False, |
72 | | - 'showgrid': False, |
73 | | - 'showline': False, |
74 | | - 'zeroline': False, |
75 | | - 'showticklabels': False, |
76 | | - 'ticks':""}}) |
77 | | - |
78 | | - # Edit yaxis |
79 | | - figure['layout']['yaxis'].update({'domain': [0, .85], |
80 | | - 'mirror': False, |
81 | | - 'showgrid': False, |
82 | | - 'showline': False, |
83 | | - 'zeroline': False, |
84 | | - 'showticklabels': False, |
85 | | - 'ticks': ""}) |
86 | | - # Edit yaxis2 |
87 | | - figure['layout'].update({'yaxis2':{'domain':[.825, .975], |
88 | | - 'mirror': False, |
89 | | - 'showgrid': False, |
90 | | - 'showline': False, |
91 | | - 'zeroline': False, |
92 | | - 'showticklabels': False, |
93 | | - 'ticks':""}}) |
94 | | - |
95 | | - # Plot! |
96 | | - py.iplot(figure, filename='dendrogram_with_heatmap') |
97 | | - |
98 | | - |
99 | | -# AND TREE |
100 | | -from matplotlib import pyplot as plt |
101 | | -from scipy.cluster.hierarchy import dendrogram, linkage |
102 | | -import numpy as np |
103 | | -Z = linkage(data, 'ward') |
104 | | - |
105 | | -from scipy.cluster.hierarchy import cophenet |
106 | | -from scipy.spatial.distance import pdist |
107 | | - |
108 | | -c, coph_dists = cophenet(Z, pdist(data)) |
109 | | -c |
110 | | - |
111 | | - |
112 | | -plt.figure(figsize=(10, 8)) |
113 | | -plt.scatter(Z[:,0], Z[:,1]) # plot all points |
| 6 | +plt = make_package_tree(matrix=data) |
114 | 7 | plt.show() |
115 | 8 |
|
116 | | -labels = [x.replace(':latest','') for x in data.index.tolist()] |
117 | | -plt.figure(figsize=(25, 10)) |
118 | | -plt.title('Docker Library Similarity') |
119 | | -plt.xlabel('image index') |
120 | | -plt.ylabel('distance') |
121 | | -dendrogram( |
122 | | - Z, |
123 | | - leaf_rotation=90., # rotates the x axis labels |
124 | | - leaf_font_size=8., # font size for the x axis labels |
125 | | - labels=labels |
126 | | -) |
127 | | -plt.show() |
| 9 | +# or save to file |
| 10 | +plt.savefig('examples/package_tree/docker-library.png') |
0 commit comments