-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdatasets.py
More file actions
413 lines (328 loc) · 13.3 KB
/
Copy pathdatasets.py
File metadata and controls
413 lines (328 loc) · 13.3 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
"""
Copyright 2018 Novartis Institutes for BioMedical Research Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import h5py
import hashlib
import numpy as np
import os
import re
from contextlib import contextmanager, suppress
from scipy.spatial.distance import cdist
from server import chromsizes, utils
class Datasets:
def __init__(self):
self.datasets = []
self.datasets_by_id = {}
self.datasets_by_type = {}
self.custom_chromosomes = None
self.chromsizes = None
self.coords = None
self._cache_filename = None
self._total_len_windows = -1
self._total_len_encoded = -1
def __iter__(self):
return iter(self.datasets)
@property
def cache_filename(self):
return self._cache_filename
@property
def cache_filepath(self):
return self._cache_filepath
@property
def total_len_windows(self):
return self._total_len_windows
@property
def total_len_encoded(self):
return self._total_len_encoded
@property
def length(self):
return len(self.datasets)
@contextmanager
def cache(self):
cache = h5py.File(self.cache_filepath, "r")
try:
yield DatasetsCache(cache)
finally:
cache.close()
def add(self, dataset):
if self.chromsizes is None:
self.chromsizes = dataset.chromsizes
self.chromsizes_cum = np.cumsum(self.chromsizes) - self.chromsizes
if self.coords is None:
self.coords = dataset.coords
if dataset.custom_chromosomes is not None and self.custom_chromosomes is None:
self.custom_chromosomes = dataset.custom_chromosomes
if not chromsizes.equals(
self.chromsizes, dataset.chromsizes, self.coords, self.custom_chromosomes
):
raise ValueError(
"Incorrect coordinates: all datasets need to have the same coordinates."
)
self.datasets.append(dataset)
self.datasets_by_id[dataset.id] = dataset
try:
self.datasets_by_type[dataset.content_type].append(dataset)
except KeyError:
self.datasets_by_type[dataset.content_type] = [dataset]
def export(
self,
use_uuid: bool = False,
autoencodings: bool = False,
ignore_chromsizes: bool = False,
):
return [
dataset.export(
use_uuid=use_uuid,
autoencodings=autoencodings,
ignore_chromsizes=ignore_chromsizes,
)
for dataset in self.datasets
if not autoencodings or dataset.is_autoencoded
]
def get(self, dataset_id: str):
try:
return self.datasets_by_id[dataset_id]
except KeyError:
raise KeyError("No dataset with ID '{}' found".format(dataset_id))
def size(self):
return len(self.datasets)
def get_by_type(self, dtype: str):
if dtype in self.datasets_by_type:
return self.datasets_by_type[dtype]
def createCacheHash(self, encoders, config):
# Generate filename from the set of datasets and encoders
encoder_filenames = [encoder.encoder_filename for encoder in encoders]
dataset_filenames = [dataset.filename for dataset in self.datasets]
all_filenames = ":".join(encoder_filenames + dataset_filenames + config.chroms)
md5 = hashlib.md5()
md5.update(all_filenames.encode())
return md5.hexdigest()
def remove_cache(self):
for dataset in self.datasets:
dataset.remove_cache()
with suppress(FileNotFoundError):
os.remove(self.cache_filepath)
def compute_encodings_dist(
self,
target: np.ndarray,
dist_metric: str = "euclidean",
batch_size: int = 10000,
verbose: bool = False,
):
with h5py.File(self.cache_filepath, "r+") as f:
if verbose:
print(
"Compute distance of encoded windows to the encoded target",
end="",
flush=True,
)
encodings = f["encodings"][:]
N = encodings.shape[0]
target = target.reshape((1, -1))
dist = None
for batch_start in np.arange(0, N, batch_size):
if verbose:
print(".", end="", flush=True)
encodings_batch = encodings[batch_start : batch_start + batch_size]
try:
batch_dist = cdist(encodings_batch, target, dist_metric).flatten()
except ValueError:
batch_dist = cdist(encodings_batch, target).flatten()
if dist is None:
dist = batch_dist
else:
dist = np.concatenate((dist, batch_dist))
f["encodings_dist"][:] = dist
def get_encodable(self, encoders):
datasets = [self.get_by_type(encoder.content_type) for encoder in encoders]
datasets = [ds for ds in datasets if ds is not None]
return utils.flatten(datasets)
def prepare(
self,
encoders,
config,
clear: bool = False,
verbose: bool = False,
):
# Used for assertion checking
total_num_windows = None
chrom_num_windows = None
encodable_datasets = list(self.get_encodable(encoders))
if verbose:
print("Prepare all datasets just for you...", flush=True)
for encoder in encoders:
try:
for dataset in encodable_datasets:
if dataset.content_type != encoder.content_type:
continue
ds_total_num_windows, ds_chrom_num_windows = dataset.prepare(
config, encoder, clear=clear, verbose=verbose
)
if total_num_windows is None:
total_num_windows = ds_total_num_windows
if chrom_num_windows is None:
chrom_num_windows = ds_chrom_num_windows
if verbose:
print(
"Make sure that all windows are correctly prepared...",
flush=True,
)
# Check that all datasets have the same number of windows
assert (
total_num_windows == ds_total_num_windows
), "The total number of windows should be the same for all datasets"
# Check that all datasets have the same number of windows
assert ds_chrom_num_windows.equals(
chrom_num_windows
), "The number of windows per chromosome should be the same for all datasets"
except KeyError:
# If there's no data for the encoder we simply continue with our lives
# pass
raise
assert total_num_windows is not None, 'No windows extracted'
self._cache_filename = "{}.hdf5".format(self.createCacheHash(encoders, config))
self._cache_filepath = os.path.join(config.cache_dir, self.cache_filename)
if verbose:
print(f'Caching dataset under {self._cache_filename}')
self._total_len_windows = 0
self._total_len_encoded = 0
for dataset in encodable_datasets:
encoder = encoders.get(dataset.content_type)
self._total_len_encoded += encoder.latent_dim
self._total_len_windows += int(encoders.window_size // encoder.resolution)
# Concatenate data
mode = "w" if clear else "w-"
try:
with h5py.File(self.cache_filepath, mode) as f:
if verbose:
print("Concatenate and save windows...")
w = f.create_dataset(
"windows",
(total_num_windows, self.total_len_windows),
dtype=np.float32,
)
w_max = f.create_dataset(
"windows_max", (total_num_windows,), dtype=np.float32
)
w_sum = f.create_dataset(
"windows_sum", (total_num_windows,), dtype=np.float32
)
w_mean = f.create_dataset(
"windows_mean", (total_num_windows,), dtype=np.float32
)
e = f.create_dataset(
"encodings",
(total_num_windows, self.total_len_encoded),
dtype=np.float32,
)
f.create_dataset(
"encodings_dist", (total_num_windows,), dtype=np.float32
)
e_knn_density = f.create_dataset(
"encodings_knn_density", (total_num_windows,), dtype=np.float32
)
# Metadata
w.attrs["total_num_windows"] = total_num_windows
w.attrs["total_len_windows"] = self._total_len_windows
e.attrs["total_len_encoded"] = self._total_len_encoded
pos_window_from = 0
pos_window_to = 0
pos_encoded_from = 0
pos_encoded_to = 0
for dataset in encodable_datasets:
encoder = encoders.get(dataset.content_type)
pos_window_to = pos_window_from + encoder.window_num_bins
pos_encoded_to = pos_encoded_from + encoder.latent_dim
with dataset.cache() as dataset_cache:
w[:, pos_window_from:pos_window_to] = np.squeeze(
dataset_cache.windows
)
e[:, pos_encoded_from:pos_encoded_to] = np.squeeze(
dataset_cache.encodings
)
# Write to disk
f.flush()
pos_window_from = pos_window_to
pos_encoded_from = pos_encoded_to
# Compute simple stats to speed up online calculation down the road
pos_chrom_from = 0
pos_chrom_to = 0
if verbose:
print("Compute per-chromosome statistics...")
for i, chromosome in enumerate(config.chroms):
pos_chrom_to = pos_chrom_from + chrom_num_windows[i]
w_max[pos_chrom_from:pos_chrom_to] = np.nanmax(
w[pos_chrom_from:pos_chrom_to, :], axis=1
)
w_sum[pos_chrom_from:pos_chrom_to] = np.nansum(
w[pos_chrom_from:pos_chrom_to, :], axis=1
)
w_mean[pos_chrom_from:pos_chrom_to] = np.nanmean(
w[pos_chrom_from:pos_chrom_to, :], axis=1
)
pos_chrom_from = pos_chrom_to
# Write to disk
f.flush()
if verbose:
print("Compute the encoded windows' knn density...")
e_knn_density[:] = utils.knn_density(e[:])
except OSError as error:
# When `clear` is `False` and the data is already prepared then we expect to
# see error number 17 as we opened the file in `w-` mode.
if not clear:
# Stupid h5py doesn't populate `error.errno` so we have to parse it out
# manually
matches = re.search(r"errno = (\d+)", str(error))
if matches and int(matches.group(1)) == 17:
pass
else:
raise
else:
raise
if verbose:
print("All datasets have been prepared! Thanks for waiting.")
@contextmanager
def prepared_data(self):
if not self.cache_filepath:
raise ValueError("Data not prepared")
f = h5py.File(self.cache_filepath, "r")
try:
yield f
finally:
f.close()
class DatasetsCache:
def __init__(self, cache):
self.cache = cache
@property
def windows(self):
return self.cache["windows"]
@property
def windows_max(self):
return self.cache["windows_max"]
@property
def windows_sum(self):
return self.cache["windows_sum"]
@property
def windows_mean(self):
return self.cache["windows_mean"]
@property
def encodings(self):
return self.cache["encodings"]
@property
def encodings_dist(self):
return self.cache["encodings_dist"]
@property
def computed_dist_to_target(self):
return np.sum(self.cache["encodings_dist"]) > 0
@property
def encodings_knn_density(self):
return self.cache["encodings_knn_density"]