-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdata_classes.py
368 lines (272 loc) · 8.79 KB
/
data_classes.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
from abc import ABC, abstractmethod
from unittest import result
import segyio
import numpy as np
import random
# Class SeismicData
#
# Abstract class for handling 3D seismic data.
class SeismicData(ABC):
@abstractmethod
def __init__(self):
"""Initialize the object"""
pass
@abstractmethod
def __del__(self):
"""Cleanup the object"""
pass
@abstractmethod
def get_iline(self, indx):
"""Get the inline data of a given index
Args:
indx (int): index of the inline
Returns:
numpy.ndarray : inline data
"""
pass
@abstractmethod
def get_xline(self, indx):
"""Get the crossline data of a given index
Args:
indx (int): index of the crossline
Returns:
numpy.ndarray : crossline data
"""
pass
@abstractmethod
def get_zslice(self, indx):
"""Get the time slice data of a given index
Args:
indx (int): index of the time slice
Returns:
numpy.ndarray : time slice data
"""
pass
# get total number of ilines
@abstractmethod
def get_n_ilines(self):
"""Get total number of ilines
Returns:
int : number of ilines
"""
pass
# get total number of xlines
@abstractmethod
def get_n_xlines(self):
"""Get total number of xlines
Returns:
int : number of xlines
"""
pass
# get total number of zslices
@abstractmethod
def get_n_zslices(self):
"""Get total number of zslices
Returns:
int : number of zslices
"""
pass
@abstractmethod
def get_sample_rate(self):
"""Get the sample rate of the data
Returns:
float : sample rate
"""
pass
@abstractmethod
def get_vm(self):
"""Get the visualization threshold for the data
Returns:
float : visualization threshold
"""
pass
# Class SegyIO3D
#
# A concrete implementation of the SeismicData abstract class using segyio library.
class SegyIO3D(SeismicData):
def __init__(self, file_name, iline_byte=189, xline_byte=193):
super().__init__()
self._segyfile = segyio.open(file_name, iline=int(iline_byte), xline=int(xline_byte))
# get statistics for visualization
n_slices = 10
seis = [self.get_iline(random.randint(0, self.get_n_ilines()-1)) for i in range(n_slices)]
self.vm = np.percentile(seis, 95)
self.file_name = file_name
self.iline_byte = iline_byte
self.xline_byte = xline_byte
def __del__(self):
self._segyfile.close()
def get_iline(self, indx):
return self._segyfile.iline[self._segyfile.ilines[indx]]
def get_xline(self,indx):
return self._segyfile.xline[self._segyfile.xlines[indx]]
def get_zslice(self,indx):
return self._segyfile.depth_slice[indx]
# get total number of ilines
def get_n_ilines(self):
return len(self._segyfile.iline)
# get total number of xlines
def get_n_xlines(self):
return len(self._segyfile.xline)
# get total number of zslices
def get_n_zslices(self):
return self._segyfile.samples.size
def get_sample_rate(self):
return segyio.tools.dt(self._segyfile)
def get_vm(self):
return self.vm
def get_file_name(self):
return self.file_name
def cropped_numpy(self, min_il, max_il, min_xl, max_xl, min_z, max_z):
""" Reads cropped seismic and returns numpy array
Args:
min_il (_type_): min inline
max_il (_type_): max inline
min_xl (_type_): min crossline
max_xl (_type_): max crossline
min_z (_type_): min timeslice
max_z (_type_): max timeslice
"""
assert max_il>min_il, f"max_il must be greater than {min_il}, got: {max_il}"
assert max_xl>min_xl, f"max_il must be greater than {min_xl}, got: {max_xl}"
assert max_z>min_z, f"max_il must be greater than {min_z}, got: {max_z}"
return np.array([self.get_iline(i)[min_xl:max_xl, min_z:max_z] for i in range(min_il, max_il)])
def get_xline_byte(self):
return self.xline_byte
def get_iline_byte(self):
return self.iline_byte
def get_str_format(self):
return "SEGY"
def get_str_dim(self):
return "3D"
class SegyIO2D(SeismicData):
def __init__(self, file_name):
super().__init__()
seismic_type = "2D"
try:
with segyio.open(file_name, strict=True) as segyfile:
seismic_type = "3D"
raise ValueError("You can only use 2D seismic file with this mode")
except:
if seismic_type == "3D":
raise ValueError("You can only use 2D seismic file with this mode")
with segyio.open(file_name, strict=False) as segyfile:
self._data = np.stack(list((_.copy() for _ in segyfile.trace[:])))
self._dt = segyio.tools.dt(segyfile)
self.vm = np.percentile(self._data, 95)
self.file_name = file_name
self.iline_byte = 189
self.xline_byte = 193
def __del__(self):
pass
def get_file_name(self):
return self.file_name
def get_iline(self):
return self._data.T
def get_xline(self,):
pass
def get_zslice(self,):
pass
# get total number of ilines
def get_n_ilines(self):
pass
# get total number of xlines
def get_n_xlines(self):
return self._data.shape[0]
# get total number of zslices
def get_n_zslices(self):
return self._data.shape[1]
def get_sample_rate(self):
return self._dt
def get_vm(self):
return self.vm
def make_axis_devisable_by(self, factor):
xlim = self._data.shape[0]//int(factor)*int(factor)
ylim = self._data.shape[1]//int(factor)*int(factor)
self._data = self._data[:xlim, :ylim]
def get_xline_byte(self):
return self.xline_byte
def get_iline_byte(self):
return self.iline_byte
def get_str_format(self):
return "SEGY"
def get_str_dim(self):
return "2D"
class Numpy2D(SeismicData):
def __init__(self, data):
super().__init__()
if isinstance(data, str):
self._data = np.load(data)
elif isinstance(data, np.ndarray):
self._data = data
# get statistics for visualization
seis = self.get_iline()
self.vm = np.percentile(seis, 95)
def __del__(self):
pass
def get_iline(self):
return self._data
def get_xline(self):
pass
def get_zslice(self):
pass
def get_n_ilines(self):
pass
# get total number of xlines
def get_n_xlines(self):
return self._data.shape[0]
def get_n_zslices(self):
return self._data.shape[1]
def get_sample_rate(self):
return 1000
def get_vm(self):
return self.vm
def make_axis_devisable_by(self, factor):
xlim = self._data.shape[0]//int(factor)*int(factor)
ylim = self._data.shape[1]//int(factor)*int(factor)
self._data = self._data[:xlim, :ylim]
def get_str_format(self):
return "NUMPY"
def get_str_dim(self):
return "2D"
class Numpy3D(SeismicData):
def __init__(self, data):
super().__init__()
if isinstance(data, str):
self._data = np.load(data)
elif isinstance(data, np.ndarray):
self._data = data
# get statistics for visualization
if self._data is not None:
n_slices = 10
seis = [self.get_iline(random.randint(0, self.get_n_ilines()-1)) for i in range(n_slices)]
self.vm = np.percentile(seis, 95)
def __del__(self):
pass
def get_iline(self, indx):
return self._data[indx,:,:]
def get_xline(self, indx):
return self._data[:,indx,:]
def get_zslice(self, indx):
return self._data[:,:,indx]
def get_n_ilines(self):
return self._data.shape[0]
# get total number of xlines
def get_n_xlines(self):
return self._data.shape[1]
def get_n_zslices(self):
return self._data.shape[2]
def get_sample_rate(self):
return 1000
def get_vm(self):
return self.vm
def get_cube(self):
return self._data
def make_axis_devisable_by(self, factor):
xlim = self._data.shape[0]//int(factor)*int(factor)
ylim = self._data.shape[1]//int(factor)*int(factor)
self._data = self._data[:xlim, :ylim, :]
def get_str_format(self):
return "NUMPY"
def get_str_dim(self):
return "3D"