forked from dariodematties/NANOPORE_TRANSLOCATIONS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dataset_Management.py
396 lines (295 loc) · 16.5 KB
/
Dataset_Management.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
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
import torch
import numpy as np
class Artificial_DataLoader:
def __init__(self, world_size, rank, device, File, sampling_rate, number_of_concentrations, number_of_durations, number_of_diameters, window, length, batch_size,
max_num_of_pulses_in_a_wind=75):
assert window < length
self.world_size = world_size
self.rank = rank
self.rank_id = rank # the rank id will change according to the epoch
self.device = device
self.File = File
self.sampling_rate = sampling_rate
self.number_of_concentrations = number_of_concentrations
self.number_of_durations = number_of_durations
self.number_of_diameters = number_of_diameters
self.windows_per_signal = int(length / window)
# this is the shape of the structure of windows in the dataset
self.shape = (number_of_concentrations, number_of_durations, number_of_diameters, int(length / window))
# this is the total number of windows in the dataset
self.total_number_of_windows = self.number_of_concentrations * \
self.number_of_durations * \
self.number_of_diameters * \
self.windows_per_signal
# this is the size of the fragment from the total number of windows that corresponds to this rank
self.shard_size = self.total_number_of_windows // world_size
# if there is residue in the distribution of windows among ranks
# all shard sizes have to be incremented in one
# since all shard sizes have to be equal
if self.total_number_of_windows % world_size != 0:
self.shard_size += 1
self.window = window
self.length = length
self.batch_size = batch_size
self.max_num_of_pulses_in_a_wind = max_num_of_pulses_in_a_wind
self.avail_winds = self.get_avail_winds(self.shard_size)
# unravel indices in advance to avoid computational cost during execution
auxiliary = [i for i in range(self.total_number_of_windows)]
self.unraveled_indices = np.unravel_index(auxiliary, self.shape)
self.samples_indices = []
self.number_of_avail_windows = self.get_number_of_avail_windows()
@staticmethod
def get_avail_winds(shard_size):
return torch.ones((shard_size), dtype=bool)
# determines the quota of any number of things among ranks including residues
# for instance if total is 100 and world_size is 3, then rank 0 will have a quota of 4
# rank 1 a quota of 3 and rank 2 a quota of 3 too.
def _get_quota(self, world_size, rank, total):
assert(total >= world_size)
quota = total // world_size
residue = total % world_size
if rank < residue:
quota += 1
return quota
# restart all the available windows as it is when the object is created
# it rotates the identity of ranks at each epoch in order to make each rank to "see" all the samples
def reset_avail_winds(self, epoch):
self.rank_id = (self.rank + epoch) % self.world_size
# this is the fragment from the total number of windows that corresponds to this rank
self.shard_size = self._get_quota(self.world_size, self.rank_id, self.total_number_of_windows)
self.avail_winds = torch.ones((self.shard_size), dtype=bool)
self.number_of_avail_windows = self.get_number_of_avail_windows()
# make 100 random windows available
def _reset_random_winds(self):
i = 0
while i < 100:
window = torch.randint(0, self.shard_size, (1,1))[0].item()
if self.avail_winds[window] == False:
self.avail_winds[window] = True
i += 1
self.number_of_avail_windows += 100
# returns the number of available windows in the object
def get_number_of_avail_windows(self):
return sum(self.avail_winds==True).item()
# map from the local available resource in the rank to the global available resource in the world
# For instance, global resource is:
# 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
# world_size = 3
#
# rank 0 rank 1 rank 2
# 0, 1, 2, 3 0, 1, 2 0, 1, 2
#
# the mapping formula is:
# (sample * world_size) + rank
#
# rank 0 rank 1 rank 2
# (0 * 3) + 0 = 0 (0 * 3) + 1 = 1 (0 * 3) + 2 = 2
# (1 * 3) + 0 = 3 (1 * 3) + 1 = 4 (1 * 3) + 2 = 5
# (2 * 3) + 0 = 6 (2 * 3) + 1 = 7 (2 * 3) + 2 = 8
# (3 * 3) + 0 = 9
#
# this situation is going to rotate in according to the epoch
def _map_from_rank_to_world(self, sample):
return (sample * self.world_size) + self.rank_id
# get a sample from the available windows and set the sample as unavailable
def _get_sample(self):
if (self.number_of_avail_windows == 0):
self._reset_random_winds()
availables = np.where(self.avail_winds)
idx = torch.randint(0, availables[0].size, (1,1))[0].item()
sampled_window = availables[0][idx]
# set window as unavailable
self.avail_winds[sampled_window] = False
# map the sample from the rank domain to the global resources
sampled_window = self._map_from_rank_to_world(sampled_window)
sampled_window = (self.unraveled_indices[0][sampled_window], \
self.unraveled_indices[1][sampled_window], \
self.unraveled_indices[2][sampled_window], \
self.unraveled_indices[3][sampled_window],)
#sampled_window = np.unravel_index(sampled_window, self.shape)
self.number_of_avail_windows -= 1
return sampled_window
def _get_labels(self, time_window, Cnp, Duration, Dnp):
"Returns classes and bboxes inside the signal window"
dset_p = self.File['Cnp_' + str(Cnp+1) + '/Duration_' + str(Duration+1) + '/Dnp_' + str(Dnp+1) + '/parameters']
pulses_inside_window = np.where((torch.from_numpy(dset_p[0,:]) > time_window[0].cpu()) & \
(torch.from_numpy(dset_p[0,:]) < time_window[-1].cpu()))[0]
pulses_inside_window = pulses_inside_window.tolist()
start_times = dset_p[0,pulses_inside_window]
#pulse_widths = dset_p[1,pulses_inside_window]
#pulse_categories = dset_p[2,pulses_inside_window]
pulse_widths = dset_p[2,pulses_inside_window]
pulse_amplitudes = dset_p[3,pulses_inside_window]
number_of_pulses = len(pulses_inside_window)
if number_of_pulses == 0:
average_width = 0.0
average_amplitude = 0.0
else:
average_width = np.average(pulse_widths)
average_amplitude = np.average(pulse_amplitudes)
starts = (torch.from_numpy(start_times) - time_window[0].cpu()) / self.window
widths = torch.from_numpy(pulse_widths) / self.window
amplitudes = torch.from_numpy(pulse_amplitudes)
starts = starts.tolist()
widths = widths.tolist()
amplitudes = amplitudes.tolist()
#categories = pulse_categories.tolist()
categories = np.zeros(len(pulses_inside_window)).tolist()
starts = (starts + [1.0]*(self.max_num_of_pulses_in_a_wind - len(starts)))
widths = (widths + [1.0]*(self.max_num_of_pulses_in_a_wind - len(widths)))
amplitudes = (amplitudes + [1.0]*(self.max_num_of_pulses_in_a_wind - len(amplitudes)))
categories = (categories + [1.0]*(self.max_num_of_pulses_in_a_wind - len(categories)))
starts = torch.FloatTensor(starts)
widths = torch.FloatTensor(widths)
amplitudes = torch.FloatTensor(amplitudes)
categories = torch.FloatTensor(categories)
return starts, widths, amplitudes, categories, number_of_pulses, average_width, average_amplitude
def _get_signal_window(self, with_labels=False):
if len(self.samples_indices) == 0: # bring 100 samples
for i in range(100):
self.samples_indices.append(self._get_sample())
sample = self.samples_indices.pop(0)
Cnp = sample[0]
Duration = sample[1]
Dnp = sample[2]
window_number = sample[3]
dset = self.File['Cnp_' + str(Cnp+1) + '/Duration_' + str(Duration+1) + '/Dnp_' + str(Dnp+1) + '/data']
#assert dset.shape[1] % self.length == 0
samples_per_second = int(dset.shape[1] / self.length)
samples_per_window = int(samples_per_second * self.window)
begin = window_number * samples_per_window
end = begin + samples_per_window
time_window = torch.Tensor(dset[0,begin:end]).to(self.device)
clean_signal = torch.Tensor(dset[1,begin:end]).to(self.device)
noisy_signal = torch.Tensor(dset[2,begin:end]).to(self.device)
if with_labels:
starts, widths, amplitudes, categories, number_of_pulses, average_width, average_amplitude = self._get_labels(time_window, Cnp, Duration, Dnp)
return time_window, clean_signal, noisy_signal, starts, widths, amplitudes, categories, number_of_pulses, average_width, average_amplitude
else:
return time_window, clean_signal, noisy_signal
def get_batch(self, descart_empty_windows=True):
#assert sum(self.avail_winds == True) > self.batch_size
noisy_signals = torch.Tensor(self.batch_size, int(self.window*self.sampling_rate)).to(self.device)
clean_signals = torch.Tensor(self.batch_size, int(self.window*self.sampling_rate)).to(self.device)
times = torch.Tensor(self.batch_size, int(self.window*self.sampling_rate)).to(self.device)
pulse_labels = torch.Tensor(self.batch_size, 4, self.max_num_of_pulses_in_a_wind).to(self.device)
average_labels = torch.Tensor(self.batch_size, 3).to(self.device)
for i in range(self.batch_size):
number_of_pulses = 0
if descart_empty_windows:
while(number_of_pulses==0):
Time, Clean_signal, Noisy_signal, starts, widths, amplitudes, categories,\
number_of_pulses, average_width, average_amplitude = self._get_signal_window(with_labels=True)
else:
Time, Clean_signal, Noisy_signal, starts, widths, amplitudes, categories,\
number_of_pulses, average_width, average_amplitude = self._get_signal_window(with_labels=True)
times[i] = Time
clean_signals[i] = Clean_signal
noisy_signals[i] = Noisy_signal
pulse_labels[i][0] = starts
pulse_labels[i][1] = widths
pulse_labels[i][2] = amplitudes
pulse_labels[i][3] = categories
average_labels[i][0] = number_of_pulses
average_labels[i][1] = average_width
average_labels[i][2] = average_amplitude
return times, noisy_signals, clean_signals, pulse_labels, average_labels
def get_signal_window(self, Cnp, Duration, Dnp, window_number):
dset = self.File['Cnp_' + str(Cnp+1) + '/Duration_' + str(Duration+1) + '/Dnp_' + str(Dnp+1) + '/data']
#assert dset.shape[1] % self.length == 0
samples_per_second = int(dset.shape[1] / self.length)
samples_per_window = int(samples_per_second * self.window)
begin = window_number * samples_per_window
end = begin + samples_per_window
time_window = torch.Tensor(dset[0,begin:end]).to(self.device)
clean_signal = torch.Tensor(dset[1,begin:end]).to(self.device)
noisy_signal = torch.Tensor(dset[2,begin:end]).to(self.device)
starts, widths, amplitudes, categories, number_of_pulses, average_width, average_amplitude = self._get_labels(time_window, Cnp, Duration, Dnp)
pulse_labels = torch.Tensor(4, self.max_num_of_pulses_in_a_wind).to(self.device)
average_labels = torch.Tensor(3).to(self.device)
pulse_labels[0] = starts
pulse_labels[1] = widths
pulse_labels[2] = amplitudes
pulse_labels[3] = categories
average_labels[0] = number_of_pulses
average_labels[1] = average_width
average_labels[2] = average_amplitude
return time_window, noisy_signal, clean_signal, pulse_labels, average_labels
class Unlabeled_Real_DataLoader:
def __init__(self, device, File, num_of_traces, window, length):
assert window < length
self.device = device
self.File = File
self.num_of_traces = num_of_traces
self.windows_per_trace = int(length / window)
# this is the shape of the structure of windows in the dataset
self.shape = (self.num_of_traces, int(length / window))
# this is the total number of windows in the dataset
self.total_number_of_windows = self.num_of_traces * self.windows_per_trace
self.window = window
self.length = length
def get_signal_window(self, trace_number, window_number):
dset = self.File['Volt_' + str(trace_number+1) + '/data']
#assert dset.shape[1] % self.length == 0
samples_per_second = int(dset.shape[1] / self.length)
samples_per_window = int(samples_per_second * self.window)
begin = window_number * samples_per_window
end = begin + samples_per_window
time_window = torch.Tensor(dset[0,begin:end]).to(self.device)
signal = torch.Tensor(dset[1,begin:end]).to(self.device)
return time_window, signal
class Labeled_Real_DataLoader:
def __init__(self, device, File, num_of_traces, window, length):
assert window < length
self.device = device
self.File = File
self.num_of_traces = num_of_traces
self.windows_per_trace = int(length / window)
# this is the shape of the structure of windows in the dataset
self.shape = (self.num_of_traces, int(length / window))
# this is the total number of windows in the dataset
self.total_number_of_windows = self.num_of_traces * self.windows_per_trace
self.window = window
self.length = length
def _get_labels(self, time_window, trace_number):
"Returns classes and bboxes inside the signal window"
dset_p = self.File['Volt_' + str(trace_number+1) + '/parameters']
pulses_inside_window = np.where((torch.from_numpy(dset_p[0,:]) > time_window[0].cpu()) & \
(torch.from_numpy(dset_p[0,:]) < time_window[-1].cpu()))[0]
pulses_inside_window = pulses_inside_window.tolist()
start_times = dset_p[0,pulses_inside_window]
#pulse_widths = dset_p[1,pulses_inside_window]
#pulse_categories = dset_p[2,pulses_inside_window]
pulse_widths = dset_p[2,pulses_inside_window]
pulse_amplitudes = dset_p[3,pulses_inside_window]
number_of_pulses = len(pulses_inside_window)
if number_of_pulses == 0:
average_width = 0.0
average_amplitude = 0.0
else:
average_width = np.average(pulse_widths)
average_amplitude = np.average(pulse_amplitudes)
starts = (torch.from_numpy(start_times) - time_window[0].cpu()) / self.window
widths = torch.from_numpy(pulse_widths) / self.window
amplitudes = torch.from_numpy(pulse_amplitudes)
return starts, widths, amplitudes, number_of_pulses, average_width, average_amplitude
def get_signal_window(self, trace_number, window_number):
dset = self.File['Volt_' + str(trace_number+1) + '/data']
#assert dset.shape[1] % self.length == 0
samples_per_second = int(dset.shape[1] / self.length)
samples_per_window = int(samples_per_second * self.window)
begin = window_number * samples_per_window
end = begin + samples_per_window
time_window = torch.Tensor(dset[0,begin:end]).to(self.device)
noisy_signal = torch.Tensor(dset[1,begin:end]).to(self.device)
starts, widths, amplitudes, number_of_pulses, average_width, average_amplitude = self._get_labels(time_window, trace_number)
num_of_pulses_in_the_wind = starts.shape[0]
pulse_labels = torch.Tensor(3, num_of_pulses_in_the_wind).to(self.device)
average_labels = torch.Tensor(3).to(self.device)
pulse_labels[0] = starts
pulse_labels[1] = widths
pulse_labels[2] = amplitudes
average_labels[0] = number_of_pulses
average_labels[1] = average_width
average_labels[2] = average_amplitude
return time_window, noisy_signal, pulse_labels, average_labels