Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
Marius Isken committed Mar 5, 2024
1 parent 015d059 commit ded369a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
49 changes: 28 additions & 21 deletions src/qseek/models/semblance.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def _populate_table(self, table: Table) -> None:

class Semblance:
_max_semblance: np.ndarray | None = None
_node_idx_max: np.ndarray | None = None
_node_max_idx: np.ndarray | None = None
_node_hashes: list[bytes]
_offset_samples: int = 0

Expand Down Expand Up @@ -199,7 +199,7 @@ async def apply_cache(self, cache: dict[bytes, np.ndarray]) -> None:
logger.debug("applying cache for %d nodes", mask.sum())
data = [cache[hash] for hash in self._node_hashes if hash in cache]

# This is a faster then
# memoryview is faster then
# self.semblance_unpadded[mask, :] = data
# for idx, copy in enumerate(mask):
# if copy:
Expand All @@ -214,31 +214,33 @@ def maximum_node_semblance(self) -> np.ndarray:

async def maxima_semblance(
self,
padded: bool = True,
nparallel: int = 12,
trim_padding: bool = True,
nthreads: int = 12,
) -> np.ndarray:
"""Maximum semblance over time, aggregated over all nodes.
Args:
trim_padding (bool, optional): Trim padded data in post-processing.
nparallel (int, optional): Number of threads for calculation. Defaults to 6.
Returns:
np.ndarray: Maximum semblance.
"""
if self._max_semblance is None:
node_idx = await self.maxima_node_idx(padded=False, nparallel=nparallel)
node_idx = await self.maxima_node_idx(trim_padding=False, nthreads=nthreads)
self._max_semblance = self.semblance_unpadded[
node_idx, np.arange(self.n_samples_unpadded)
]
self._max_semblance.setflags(write=False)

if padded:
if trim_padding:
return self._max_semblance[self.padding_samples : -self.padding_samples]
return self._max_semblance

async def maxima_node_idx(
self,
padded: bool = False,
nparallel: int = 12,
trim_padding: bool = True,
nthreads: int = 12,
) -> np.ndarray:
"""Indices of maximum semblance at any time step.
Expand All @@ -248,15 +250,16 @@ async def maxima_node_idx(
Returns:
np.ndarray: Node indices.
"""
if self._node_idx_max is None:
self._node_idx_max = await asyncio.to_thread(
if self._node_max_idx is None:
self._node_max_idx = await asyncio.to_thread(
parstack.argmax,
self.semblance_unpadded,
nparallel=nparallel,
nparallel=nthreads,
)
if padded:
return self._node_idx_max[self.padding_samples : -self.padding_samples]
return self._node_idx_max
self._node_max_idx.setflags(write=False)
if trim_padding:
return self._node_max_idx[self.padding_samples : -self.padding_samples]
return self._node_max_idx

def apply_exponent(self, exponent: float) -> None:
"""Apply exponent to the maximum semblance.
Expand All @@ -275,6 +278,7 @@ async def find_peaks(
prominence: float,
distance: float,
trim_padding: bool = True,
nthreads: int = 12,
) -> tuple[np.ndarray, np.ndarray]:
"""Find peaks in maximum semblance.
Expand All @@ -290,7 +294,9 @@ async def find_peaks(
Returns:
tuple[np.ndarray, np.ndarray]: Indices of peaks and peak values.
"""
max_semblance_unpadded = await self.maxima_semblance(padded=False)
max_semblance_unpadded = await self.maxima_semblance(
trim_padding=False, nthreads=nthreads
)

detection_idx, _ = await asyncio.to_thread(
signal.find_peaks,
Expand All @@ -300,12 +306,12 @@ async def find_peaks(
distance=distance,
)
if trim_padding:
max_semblance_padded = await self.maxima_semblance(padded=True)
max_semblance_trimmed = await self.maxima_semblance()

detection_idx -= self.padding_samples
detection_idx = detection_idx[detection_idx >= 0]
detection_idx = detection_idx[detection_idx < max_semblance_padded.size]
semblance = max_semblance_padded[detection_idx]
detection_idx = detection_idx[detection_idx < max_semblance_trimmed.size]
semblance = max_semblance_trimmed[detection_idx]
else:
semblance = max_semblance_unpadded[detection_idx]

Expand All @@ -318,10 +324,10 @@ async def get_trace(self, padded: bool = True) -> Trace:
Trace: Holding the semblance
"""
if padded:
data = await self.maxima_semblance(padded=True)
data = await self.maxima_semblance(trim_padding=True)
start_time = self.start_time
else:
data = await self.maxima_semblance(padded=False)
data = await self.maxima_semblance(trim_padding=False)
start_time = self.start_time - timedelta(
seconds=int(round(self.padding_samples * self.sampling_rate))
)
Expand All @@ -335,7 +341,7 @@ async def get_trace(self, padded: bool = True) -> Trace:
)

def _clear_cache(self) -> None:
self._node_idx_max = None
self._node_max_idx = None
self._max_semblance = None

async def calculate_semblance(
Expand Down Expand Up @@ -370,6 +376,7 @@ async def calculate_semblance(
offset_samples,
)
self._offset_samples = offset_samples
self._clear_cache()

def normalize(
self,
Expand Down
1 change: 1 addition & 0 deletions src/qseek/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@ async def search(
height=threshold,
prominence=threshold,
distance=round(parent.detection_blinding.total_seconds() * sampling_rate),
nthreads=parent.n_threads_argmax,
)

if detection_idx.size == 0:
Expand Down

0 comments on commit ded369a

Please sign in to comment.