[9e8054]: / aggmap / utils / vismap.py

Download this file

209 lines (156 with data), 8.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from scipy.cluster.hierarchy import dendrogram, linkage, to_tree
from scipy.spatial.distance import squareform
import seaborn as sns
from highcharts import Highchart
import pandas as pd
import numpy as np
import os
from aggmap.utils.logtools import print_info
def plot_scatter(mp, htmlpath = './', htmlname = None, radius = 2, enabled_data_labels = False):
'''
mp: the object of mp
htmlpath: the figure path, not include the prefix of 'html'
htmlname: the name
radius: int, defaut:3, the radius of scatter dot
'''
title = '2D emmbedding of %s based on %s method' % (mp.ftype, mp.emb_method)
subtitle = 'number of %s: %s, metric method: %s' % (mp.ftype, len(mp.flist), mp.metric)
name = '%s_%s_%s_%s_%s' % (mp.ftype,len(mp.flist), mp.metric, mp.emb_method, 'scatter')
if not os.path.exists(htmlpath):
os.makedirs(htmlpath)
if htmlname:
name = htmlname + '_' + name
filename = os.path.join(htmlpath, name)
print_info('generate file: %s' % filename)
colormaps = mp.colormaps
df = mp.df_scatter
H = Highchart(width=1000, height=850)
H.set_options('chart', {'type': 'scatter', 'zoomType': 'xy'})
H.set_options('title', {'text': title})
H.set_options('subtitle', {'text': subtitle})
H.set_options('xAxis', {'title': {'enabled': True,'text': 'X', 'style':{'fontSize':20}},
'labels':{'style':{'fontSize':20}},
'gridLineWidth': 1,
'startOnTick': True,
'endOnTick': True,
'showLastLabel': True})
H.set_options('yAxis', {'title': {'text': 'Y', 'style':{'fontSize':20}},
'labels':{'style':{'fontSize':20}},
'gridLineWidth': 1,})
# H.set_options('legend', {'layout': 'horizontal','verticalAlign': 'top','align':'right','floating': False,
# 'backgroundColor': "(Highcharts.theme && Highcharts.theme.legendBackgroundColor) || '#FFFFFF'",
# 'borderWidth': 1})
H.set_options('legend', {'align': 'right', 'layout': 'vertical',
'margin': 1, 'verticalAlign': 'top', 'y':40,
'symbolHeight': 12, 'floating': False,})
H.set_options('plotOptions', {'scatter': {'marker': {'radius': radius,
'states': {'hover': {'enabled': True,
'lineColor': 'rgb(100,100,100)'}}},
'states': {'hover': {'marker': {'enabled': False} }},
'tooltip': {'headerFormat': '<b>{series.name}</b><br>',
'pointFormat': '{point.IDs}'}},
'series': {'turboThreshold': 5000,
'dataLabels': {'enabled': enabled_data_labels, 'format': '{point.IDs}'}}
})
for subtype, color in colormaps.items():
dfi = df[df['Subtypes'] == subtype]
if len(dfi) == 0:
continue
data = dfi.to_dict('records')
H.add_data_set(data, 'scatter', subtype, color=color)
H.save_file(filename)
print_info('save html file to %s' % filename)
return H
def plot_grid(mp, htmlpath = './', htmlname = None, enabled_data_labels = False):
'''
mp: the object of mp
htmlpath: the figure path
'''
if not os.path.exists(htmlpath):
os.makedirs(htmlpath)
title = 'Assignment of %s by %s emmbedding result' % (mp.ftype, mp.emb_method)
subtitle = 'number of %s: %s, metric method: %s' % (mp.ftype, len(mp.flist), mp.metric)
name = '%s_%s_%s_%s_%s' % (mp.ftype,len(mp.flist), mp.metric, mp.emb_method, 'mp')
if htmlname:
name = name = htmlname + '_' + name
filename = os.path.join(htmlpath, name)
print_info('generate file: %s' % filename)
colormaps = mp.colormaps
df = mp.df_grid
H = Highchart(width=1000, height=850)
H.set_options('chart', {'type': 'heatmap', 'zoomType': 'xy'})
H.set_options('title', {'text': title})
H.set_options('subtitle', {'text': subtitle})
# H.set_options('xAxis', {'title': '',
# 'min': 0, 'max': mp._S.fmap_shape[1]-1,
# 'allowDecimals':False,
# 'labels':{'style':{'fontSize':20}}})
# H.set_options('yAxis', {'title': '', 'tickPosition': 'inside',
# 'min': 0, 'max': mp._S.fmap_shape[0]-1,
# 'reversed': True,
# 'allowDecimals':False,
# 'labels':{'style':{'fontSize':20}}})
H.set_options('xAxis', {'title': None,
'min': 0, 'max': mp.fmap_shape[1]-1,
'startOnTick': False,
'endOnTick': False,
'allowDecimals':False,
'labels':{'style':{'fontSize':20}}})
H.set_options('yAxis', {'title': {'text': ' ', 'style':{'fontSize':20}},
'startOnTick': False,
'endOnTick': False,
'gridLineWidth': 0,
'reversed': True,
'min': 0, 'max': mp.fmap_shape[0]-1,
'allowDecimals':False,
'labels':{'style':{'fontSize':20}}})
H.set_options('legend', {'align': 'right', 'layout': 'vertical',
'margin': 1, 'verticalAlign': 'top',
'y': 60, 'symbolHeight': 12, 'floating': False,})
H.set_options('tooltip', {'headerFormat': '<b>{series.name}</b><br>',
'pointFormat': '{point.v}'})
H.set_options('plotOptions', {'series': {'turboThreshold': 5000,
'dataLabels': {'enabled': enabled_data_labels,
'format': '{point.v}',
'style': {'textOutline':False, 'color': 'black'},
}
}
}
)
for subtype, color in colormaps.items():
dfi = df[df['Subtypes'] == subtype]
if len(dfi) == 0:
continue
H.add_data_set(dfi.to_dict('records'), 'heatmap',
name = subtype,
#dataLabels = {'enabled': True, 'color': color}
color = color,
)
H.save_file(filename)
print_info('save html file to %s' % filename)
return H
def _getNewick(node, newick, parentdist, leaf_names):
if node.is_leaf():
return "%s:%.2f%s" % (leaf_names[node.id], parentdist - node.dist, newick)
else:
if len(newick) > 0:
newick = "):%.2f%s" % (parentdist - node.dist, newick)
else:
newick = ");"
newick = _getNewick(node.get_left(), newick, node.dist, leaf_names)
newick = _getNewick(node.get_right(), ",%s" % (newick), node.dist, leaf_names)
newick = "(%s" % (newick)
return newick
def _mp2newick(mp, treefile = 'mytree'):
dist_matrix = mp.dist_matrix
leaf_names = mp.flist
df = mp.df_embedding[['colors','Subtypes']]
dists = squareform(dist_matrix)
linkage_matrix = linkage(dists, 'complete')
tree = to_tree(linkage_matrix, rd=False)
newick = getNewick(tree, "", tree.dist, leaf_names = leaf_names)
with open(treefile + '.nwk', 'w') as f:
f.write(newick)
df.to_excel(treefile + '.xlsx')
def plot_tree(mp, htmlpath = './', htmlname = None):
pass