Skip to content

Commit

Permalink
Merge pull request #4 from analysiscenter/remove_samples
Browse files Browse the repository at this point in the history
Change processing of file samples
  • Loading branch information
SergeyTsimfer authored May 24, 2023
2 parents e11a40d + dced74f commit 575604f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
8 changes: 4 additions & 4 deletions examples/01_Basic_usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"data/A.sgy 2.0 500.0\n"
"data/A.sgy 2000\n"
]
}
],
Expand All @@ -141,7 +141,7 @@
"\n",
"# Context manager: recommended\n",
"with segfast.open(path=path) as file:\n",
" print(file.path, file.sample_interval, file.sample_rate)"
" print(file.path, file.sample_interval)"
]
},
{
Expand All @@ -163,7 +163,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"data/A.sgy 2.0 500.0\n"
"data/A.sgy 2000\n"
]
}
],
Expand All @@ -174,7 +174,7 @@
"with open('tester.pckl', 'rb') as file:\n",
" reloaded = pickle.load(file)\n",
"\n",
"print(reloaded.path, reloaded.sample_interval, reloaded.sample_rate)"
"print(reloaded.path, reloaded.sample_interval)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion segfast/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
from .memmap_loader import MemmapLoader
from .loader import Loader, File, open #pylint: disable=redefined-builtin

__version__ = '1.0.0'
__version__ = '1.0.1'
9 changes: 3 additions & 6 deletions segfast/memmap_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def _make_mmap_headers_dtype(headers, endian_symbol='>'):


# Data loading
def load_traces(self, indices, limits=None, buffer=None, return_samples=False):
def load_traces(self, indices, limits=None, buffer=None):
""" Load traces by their indices.
Under the hood, we use a pre-made memory mapping over the file, where trace data is viewed with a special dtype.
Regardless of the numerical dtype of SEG-Y file, we output IEEE float32:
Expand All @@ -237,8 +237,6 @@ def load_traces(self, indices, limits=None, buffer=None, return_samples=False):
Slice of the data along the depth axis.
buffer : np.ndarray, optional
Buffer to read the data into. If possible, avoids copies.
return_samples : bool
Whether to return samples of loaded traces in accordance to `limits`.
"""
limits = self.process_limits(limits)

Expand All @@ -251,11 +249,10 @@ def load_traces(self, indices, limits=None, buffer=None, return_samples=False):
traces = self._ibm_to_ieee(traces)

if buffer is None:
buffer = np.require(traces, dtype=self.dtype, requirements='C')
return buffer if return_samples is False else (buffer, self.samples[limits])
return np.require(traces, dtype=self.dtype, requirements='C')

buffer[:len(indices)] = traces
return buffer if return_samples is False else (buffer, self.samples[limits])
return buffer

def load_depth_slices(self, indices, buffer=None):
""" Load horizontal (depth) slices of the data.
Expand Down
29 changes: 13 additions & 16 deletions segfast/segyio_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,13 @@ def __init__(self, path, endian='big', strict=False, ignore_geometry=True):
self.n_traces = self.file_handler.trace.length
self.dtype = self.file_handler.dtype

# Sample interval, rate and delay
self.sample_interval = self._infer_sample_interval() # ms
self.sample_rate = 1000 / self.sample_interval # Hz
self.samples = np.arange(self.n_samples) * self.sample_interval
self.delay = self.file_handler.header[0].get(segyio.TraceField.DelayRecordingTime)

# Misc
self.metrics = self.file_handler.xfd.metrics()
self.text = [self.file_handler.text[i] for i in range(1 + self.file_handler.ext_headers)]


def _infer_sample_interval(self):
""" Get sample interval from file headers. """
@property
def sample_interval(self):
""" Sample interval of seismic traces. """
bin_sample_interval = self.file_handler.bin[segyio.BinField.Interval]
trace_sample_interval = self.file_handler.header[0][segyio.TraceField.TRACE_SAMPLE_INTERVAL]
# 0 means undefined sample interval, so it is removed from the set
Expand All @@ -87,7 +81,12 @@ def _infer_sample_interval(self):
"either both `Interval` (bytes 3217-3218 in the binary header) "
"and `TRACE_SAMPLE_INTERVAL` (bytes 117-118 in the header of the first trace) "
"are undefined or they have different values.")
return union_sample_interval.pop() / 1000 # convert from microseconds to milliseconds
return union_sample_interval.pop()

@property
def delay(self):
""" Delay recording time of seismic traces. """
return self.file_handler.header[0].get(segyio.TraceField.DelayRecordingTime)


# Headers
Expand Down Expand Up @@ -160,7 +159,7 @@ def make_tsf_header(self):


# Data loading: traces
def load_traces(self, indices, limits=None, buffer=None, return_samples=False):
def load_traces(self, indices, limits=None, buffer=None):
""" Load traces by their indices.
By pre-allocating memory for all of the requested traces, we significantly speed up the process.
Expand All @@ -172,19 +171,16 @@ def load_traces(self, indices, limits=None, buffer=None, return_samples=False):
Slice of the data along the depth axis.
buffer : np.ndarray, optional
Buffer to read the data into. If possible, avoids copies.
return_samples : bool
Whether to return samples of loaded traces in accordance to `limits`.
"""
limits = self.process_limits(limits)
samples = self.samples[limits]
n_samples = len(samples)
n_samples = len(range(*limits.indices(self.n_samples)))

if buffer is None:
buffer = np.empty((len(indices), n_samples), dtype=self.dtype)

for i, index in enumerate(indices):
self.load_trace(index=index, buffer=buffer[i], limits=limits)
return buffer if return_samples is False else (buffer, samples)
return buffer

def process_limits(self, limits):
""" Convert given `limits` to a `slice`. """
Expand All @@ -209,6 +205,7 @@ def load_trace(self, index, buffer, limits):
limits.start, limits.stop, limits.step,
buffer.size)


# Data loading: depth slices
def load_depth_slices(self, indices, buffer=None):
""" Load horizontal (depth) slices of the data.
Expand Down

0 comments on commit 575604f

Please sign in to comment.