Skip to content

Commit

Permalink
Merge pull request #285 from markspec/284_sparsity_check_override
Browse files Browse the repository at this point in the history
  • Loading branch information
tasansal authored Sep 28, 2023
2 parents 95bd831 + c116a3b commit c614a87
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
13 changes: 13 additions & 0 deletions src/mdio/converters/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@ def __init__(self, grid_traces, segy_traces):
)

super().__init__(self.message)


class GridTraceSparsityError(Exception):
"""Raised when mdio grid will be sparsely populated from SEG-Y traces."""

def __init__(self, shape, num_traces):
"""Initialize error."""
self.message = (
f"Grid shape: {shape} but SEG-Y tracecount: {num_traces}. "
"This grid is very sparse and most likely user error with indexing."
)

super().__init__(self.message)
20 changes: 12 additions & 8 deletions src/mdio/converters/segy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import annotations

import logging
import os
from datetime import datetime
from datetime import timezone
from importlib import metadata
Expand All @@ -16,6 +17,7 @@

from mdio.api.io_utils import process_url
from mdio.converters.exceptions import GridTraceCountError
from mdio.converters.exceptions import GridTraceSparsityError
from mdio.core import Grid
from mdio.core.utils_write import write_attribute
from mdio.segy import blocked_io
Expand Down Expand Up @@ -61,14 +63,16 @@ def grid_density_qc(grid: Grid, num_traces: int) -> None:
Basic qc of the grid to check density and provide warning/exception
when indexing is problematic to provide user with insights to the use.
If trace density on the specified grid is less than 50% a warning is
logged. If denisty is less than 1% an exception is raised.
logged. If density is less than 10% an exception is raised. To ignore
trace sparsity check set environment variable:
MDIO_IGNORE_CHECKS = True
Args:
grid: The grid instance to check.
num_traces: Expected number of traces.
Raises:
GridTraceCountError: When the grid is too sparse.
GridTraceSparsityError: When the grid is too sparse.
"""
grid_traces = np.prod(grid.shape[:-1], dtype=np.uint64) # Exclude sample
dims = {k: v for k, v in zip(grid.dim_names, grid.shape)} # noqa: B905
Expand All @@ -78,16 +82,16 @@ def grid_density_qc(grid: Grid, num_traces: int) -> None:

# Extreme case where the grid is very sparse (usually user error)
if grid_traces > 10 * num_traces:
logger.warning("WARNING: Sparse mdio grid detected!")
for dim_name in grid.dim_names:
dim_min = grid.get_min(dim_name)
dim_max = grid.get_max(dim_name)
logger.warning(f"{dim_name} min: {dim_min} max: {dim_max}")

msg = (
f"Grid shape: {grid.shape} but SEG-Y tracecount: {num_traces}. "
"This grid is very sparse and most likely user error with indexing."
)
raise GridTraceCountError(msg)
if os.getenv("MDIO_IGNORE_CHECKS", False):
# Do not raise an exception if MDIO_IGNORE_CHECK is False
pass
else:
raise GridTraceSparsityError(grid.shape, num_traces)

# Warning if we have above 50% sparsity.
if grid_traces > 2 * num_traces:
Expand Down

0 comments on commit c614a87

Please sign in to comment.