-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsne.py
263 lines (227 loc) · 7.93 KB
/
tsne.py
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import argparse
from io import (
BytesIO,
)
import json
import logging
import os
from pathlib import Path
import sys
from typing import (
Dict,
)
from urllib.error import (
HTTPError,
)
from urllib.request import (
urlopen,
)
import boto3
import colorcet
import matplotlib.pyplot as plt
from more_itertools import (
one,
)
import numpy as np
import pandas as pd
from util import (
generate_project_uuid,
get_target_project_dirs,
)
# Note that while this script does use a local cache to avoid downloading the
# same data from SCXA every time it's run, it is NOT idempotent since the
# potential gains from implementing idempotence are currently not compelling.
log = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout,
level=logging.INFO)
# Local output
output_dir = Path('tSNE')
cache_dir = output_dir / 'cache'
class TSNE:
base_url = 'https://www.ebi.ac.uk/gxa/sc/'
@property
def tsne_url(self) -> str:
return f'{self.base_url}/experiments/{self.ax_acc}/results/tsne'
@property
def clusters_url(self) -> str:
return f'{self.base_url}/experiment/{self.ax_acc}/' \
f'download?fileType=cluster&accessKey='
@property
def points_url(self) -> str:
return f'{self.base_url}/json/experiments/{self.ax_acc}/tsneplot/' \
f'{self.perplexity}/clusters/k/{self.k}'
def __init__(self, geo_acc: str, perplexity: int):
self.geo_acc = geo_acc
self.ax_acc = f'E-GEOD-{self.geo_acc[3:]}'
self.uuid = generate_project_uuid(self.geo_acc)
self.perplexity = perplexity
self.k = None
self.clusters = None
def load(self):
if self.k is None:
self.k = self._get_default_k()
if self.clusters is None:
self.clusters = self._get_clusters()
def _get_default_k(self) -> int:
"""
Obtain the number of clusters SCXA uses by default when coloring by
cluster.
"""
cluster_info = self._cache(
'cluster_info.tsv',
lambda f: pd.read_csv(f, sep='\t'),
lambda cluster_info_, f: cluster_info_.to_csv(f, sep='\t'),
lambda: pd.read_csv(BytesIO(urlopen(self.clusters_url).read()), sep='\t')
)
return int(one(cluster_info['K'][cluster_info['sel.K']]))
def _get_clusters(self) -> Dict[str, np.ndarray]:
"""
Cluster names mapped to 2xN arrays where each column is a point and the
rows are X- and Y- coordinates.
"""
points = self._cache(
'points.json',
json.load,
json.dump,
lambda: json.load(urlopen(self.points_url))
)
return {
cluster["name"]: np.array([
(point["x"], point["y"])
for point
in cluster["data"]
]).T
for cluster
in points["series"]
}
def make_image(self, colormap, save_format: str, dpi: int) -> None:
"""
Render tSNE using matplotlib.
"""
target = output_dir / f'{self.geo_acc}.{save_format}'
log.info(f'Rendering {target}')
fig = plt.figure(figsize=[6, 6])
plt.title('Clusters', fontweight='bold')
plt.margins(tight=True)
# Hide axes
ax = fig.gca()
ax.spines['bottom'].set_color('0.75')
for obj in [ax.get_xaxis(),
ax.get_yaxis(),
*[ax.spines[side] for side in ('top', 'left', 'right')]]:
obj.set_visible(False)
# Make point size shrink with more points
n_points = np.hstack(list(self.clusters.values())).shape[1]
marker_size = 225 / (n_points ** 0.65)
for color, (cluster_id, coords) in zip(colormap, self.clusters.items()):
ax.scatter(x=coords[0],
y=coords[1],
s=marker_size,
# Prevent matplotlib warning about ambiguous color format
c=np.array(color)[..., np.newaxis],
label=cluster_id)
legend = plt.legend(loc='upper center',
bbox_to_anchor=(0.5, 0),
frameon=False,
ncol=4,
columnspacing=2.0,
labelspacing=0.5)
# Make sure legend markers are always the same size regardless of how
# the plotted points scale with number of points.
for handle in legend.legendHandles:
handle.set_sizes([25])
fig.text(
x=0.5,
y=0.0025,
s=f'tSNE data imported from {self.tsne_url}',
ha='center',
va='bottom',
fontsize='x-small'
)
plt.tight_layout()
plt.savefig(target, dpi=dpi)
plt.close('all')
def upload(self, s3_client, bucket):
image_file = one(
str(p)
for p
in output_dir.iterdir()
if p.name.startswith(self.geo_acc)
)
image_format = image_file.rsplit('.', 1)[-1]
key = f'project-assets/project-stats/{self.uuid}/tsne.{image_format}'
log.info(f'Uploading {image_file} as {key}')
s3_client.upload_file(
Bucket=bucket,
Key=key,
Filename=image_file,
ExtraArgs={
'ACL': 'public-read',
'ContentDisposition': 'inline',
'ContentType': f'image/{image_format}'
}
)
def _cache(self, name, reader, writer, getter):
path = cache_dir / '_'.join([self.geo_acc, name])
try:
with open(path) as f:
value = reader(f)
except FileNotFoundError:
log.debug(f'Retrieving {name} (not found in cache)')
try:
value = getter()
with open(path, 'w') as f:
writer(value, f)
except Exception as e:
raise e from None
else:
log.debug(f'Loaded {name} from cache')
return value
def main(args):
do_render = args.render_only or not args.upload_only
do_upload = args.upload_only or not args.render_only
client = boto3.client('s3')
os.makedirs(output_dir, exist_ok=True)
os.makedirs(cache_dir, exist_ok=True)
for project_dir in get_target_project_dirs():
tsne = TSNE(project_dir.name, args.perplexity)
if do_render:
try:
tsne.load()
except HTTPError:
log.info(f'Failed to retrieve tSNE data from SCXA for project'
f' {project_dir.name}')
continue
tsne.make_image(args.colormap, args.image_format, args.dpi)
if do_upload:
try:
tsne.upload(client, args.bucket)
except ValueError:
log.info(f'Nothing to upload for project {project_dir.name}')
continue
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='generate tSNE plots from SCXA data and upload to S3',
add_help=True
)
# General behavior
parser.add_argument('-r', '--render-only', action='store_true')
parser.add_argument('-u', '--upload-only', action='store_true')
# Filter specific projects
parser.add_argument('projects', nargs='*')
# Image properties
parser.add_argument(
'--colormap',
type=colorcet.__getattribute__, # Nice "type system" you got there...
default='glasbey'
)
parser.add_argument('-f', '--image-format', default='png')
parser.add_argument('--dpi', type=int, default=100)
# tSNE parameter. Default used by SCXA. Used to generate URLs.
parser.add_argument('-p', '--perplexity', type=int, default=25)
# Upload properties
parser.add_argument(
'--bucket',
default='ux-dev.project-assets.data.humancellatlas.org'
)
main(parser.parse_args())