Skip to content

Commit

Permalink
add messages to assertion checks (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
trhallam authored Jul 30, 2024
1 parent 41514c8 commit fd27541
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
22 changes: 13 additions & 9 deletions segysak/_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,11 @@ def coordinate_df(
"""
cdp_x, cdp_y = ds.segysak.get_coords()

assert ds[cdp_x].dims == ds[cdp_y].dims
assert (
ds[cdp_x].dims == ds[cdp_y].dims
), f"{cdp_x} and {cdp_y} vars do not have the same dims"
dims = ds[cdp_x].dims
assert len(dims) == 2
assert len(dims) == 2, f"{cdp_x} and {cdp_y} are not 2D"

df = ds[[cdp_x, cdp_y]].to_dataframe().reset_index()
df_nona = df.dropna()
Expand Down Expand Up @@ -346,14 +348,16 @@ def set_dimensions(
"""
attrs = {}
for key, value in seisnc_dims.items():
assert key in DimKeyField
assert value in self._obj.dims
assert key in DimKeyField, f"{key} is not a valid dimension"
assert value in self._obj.dims, f"{value} is not in Dataset dims"

if seisnc_dims is not None:
attrs[AttrKeyField.dimensions] = seisnc_dims

if seisnc_vert_dim is not None:
assert seisnc_vert_dim in self._obj.dims
assert (
seisnc_vert_dim in self._obj.dims
), f"{seisnc_vert_dim} is not in Dataset dims"
attrs[DimKeyField.samples] = seisnc_vert_dim

self.store_attributes(**attrs)
Expand Down Expand Up @@ -410,8 +414,8 @@ def set_coords(self, seisnc_coords: Dict[Union[_CoordKeyField, str], str]):
"""
attrs = {}
for key, value in seisnc_coords.items():
assert key in CoordKeyField
assert value in self._obj.data_vars
assert key in CoordKeyField, f"{key} is not a valid Coordinate key"
assert value in self._obj.data_vars, f"{value} is not in Dataset variables"

self.store_attributes(**seisnc_coords)

Expand Down Expand Up @@ -494,9 +498,9 @@ def calc_corner_points(self) -> List[Tuple[str, str]]:
assert (
self._obj[cdp_x].segysak.get_dimensions()
== self._obj[cdp_y].segysak.get_dimensions()
)
), f"{cdp_x} and {cdp_y} do not have the same dimensions"
dims = self._obj[cdp_x].segysak.get_dimensions()
assert len(dims) <= 2
assert len(dims) <= 2, f"{cdp_x} and {cdp_y} are not 1D or 2D"

# 3d or 2d selection (polygon vs line)
if len(dims) == 2:
Expand Down
4 changes: 2 additions & 2 deletions segysak/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ def get_uniform_spacing(
Interpolated points, Interpolated extra vars: Uniform sampling using the bin_spacing hint.
"""
points = np.asarray(points)
assert len(points.shape) == 2
assert points.shape[1] == 2
assert len(points.shape) == 2, f"points must be array of shape [N, 2]"
assert points.shape[1] == 2, f"points must be array of shape [N, 2]"

segment_lengths = np.insert(np.linalg.norm(np.diff(points, axis=0), axis=1), 0, 0.0)
segments_cum_lengths = np.cumsum(segment_lengths)
Expand Down
8 changes: 5 additions & 3 deletions segysak/segy/_xarray_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ def _create_output_trace_headers(

# change numbering if dead traces
if dead_trace_da is not None:
assert dead_trace_da.dtype == bool
assert (
dead_trace_da.dtype == bool
), f"Dead trace map should be of type: bool"
n_dead = dead_trace_da.sum()
trace_numbers[:] = -1
# use the dead_trace_da as a mask to assign trace numbering (skipping dead traces)
Expand Down Expand Up @@ -245,7 +247,7 @@ def to_segy(
to the function.
"""
# test for bad args
assert data_var in ds
assert data_var in ds, f"Data variable '{data_var}' not found in Dataset"

# process dimension arguments
da_dims = tuple(ds[data_var].dims)
Expand Down Expand Up @@ -278,7 +280,7 @@ def to_segy(
if not trace_header_map:
trace_header_map = dict()
for th in trace_header_map:
assert th in _dse
assert th in _dse, f"Trace header variable {th} not found in Dataset"

header_vars = {
byte: _dse[var]
Expand Down

0 comments on commit fd27541

Please sign in to comment.