Skip to content

Commit bc97aea

Browse files
committed
Pre-commit
1 parent 3efa436 commit bc97aea

6 files changed

Lines changed: 30 additions & 28 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,4 @@ module-name = "mdio"
208208

209209
[build-system]
210210
requires = ["uv_build>=0.8.17,<0.9.0"]
211-
build-backend = "uv_build"
211+
build-backend = "uv_build"

src/mdio/converters/segy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,4 +612,4 @@ def segy_to_mdio( # noqa PLR0913
612612
"checksum_scope": "full_file",
613613
"checksum_library": "google-crc32c",
614614
}
615-
)
615+
)

src/mdio/segy/checksum.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,31 @@
1515
from __future__ import annotations
1616

1717
import logging
18+
import multiprocessing as mp
1819
import os
20+
from concurrent.futures import ProcessPoolExecutor
21+
from itertools import repeat
22+
from math import ceil
1923
from typing import TYPE_CHECKING
2024
from typing import Any
25+
2126
import numpy as np
27+
from psutil import cpu_count
2228
from segy import SegyFile
23-
from mdio.segy._raw_trace_wrapper import SegyFileRawTraceWrapper
24-
from math import ceil
25-
from concurrent.futures import ProcessPoolExecutor
26-
from itertools import repeat
29+
from segy.arrays import HeaderArray
2730
from tqdm.auto import tqdm
2831
from upath import UPath
29-
from psutil import cpu_count
30-
import multiprocessing as mp
3132

33+
from mdio.segy._raw_trace_wrapper import SegyFileRawTraceWrapper
3234

33-
from segy.arrays import HeaderArray
3435
if TYPE_CHECKING:
3536
from mdio.segy._workers import SegyFileArguments
3637

38+
try:
39+
from crc32c_dist_rs import DistributedCRC32C
40+
except ImportError:
41+
DistributedCRC32C = Any # type: ignore[assignment,misc]
42+
3743
default_cpus = cpu_count(logical=True)
3844

3945
logger = logging.getLogger(__name__)
@@ -58,7 +64,7 @@
5864

5965

6066
def header_scan_worker(
61-
segy_file_kwargs: "SegyFileArguments",
67+
segy_file_kwargs: SegyFileArguments,
6268
trace_range: tuple[int, int],
6369
subset: list[str] | None = None,
6470
) -> HeaderArray | tuple[HeaderArray, tuple[int, int, int]]:
@@ -103,7 +109,6 @@ def header_scan_worker(
103109
# (singleton) so we can concat and assign stuff later.
104110
trace_header = np.array(trace_header, dtype=new_dtype, ndmin=1)
105111

106-
107112
# Calculate checksum from the raw bytes ALREADY IN MEMORY (NO ADDITIONAL I/O!)
108113
raw_bytes = traces.trace_buffer_array.tobytes()
109114
partial_crc32c = calculate_bytes_crc32c(raw_bytes)
@@ -122,13 +127,13 @@ def header_scan_worker(
122127

123128
return HeaderArray(trace_header), checksum_info
124129

130+
125131
def parse_headers( # noqa: PLR0913
126132
segy_file_kwargs: SegyFileArguments,
127133
num_traces: int,
128134
subset: list[str] | None = None,
129135
block_size: int = 10000,
130136
progress_bar: bool = True,
131-
calculate_checksum: bool = True,
132137
) -> HeaderArray | tuple[HeaderArray, int]:
133138
"""Read and parse given `byte_locations` from SEG-Y file.
134139
@@ -200,9 +205,7 @@ def parse_headers( # noqa: PLR0913
200205
)
201206

202207
if progress_bar is True:
203-
desc = (
204-
"Scanning SEG-Y & calculating checksum"
205-
)
208+
desc = "Scanning SEG-Y & calculating checksum"
206209
lazy_work = tqdm(
207210
iterable=lazy_work,
208211
total=n_blocks,
@@ -215,6 +218,7 @@ def parse_headers( # noqa: PLR0913
215218

216219
return finalize_distributed_checksum(results, combiner)
217220

221+
218222
def is_checksum_available() -> bool:
219223
"""Check if checksum libraries are available.
220224
@@ -273,7 +277,7 @@ def calculate_bytes_crc32c(data: bytes) -> int:
273277
return int.from_bytes(crc.digest(), byteorder="big")
274278

275279

276-
def create_distributed_crc32c(initial_bytes: bytes, total_length: int) -> Any:
280+
def create_distributed_crc32c(initial_bytes: bytes, total_length: int) -> DistributedCRC32C:
277281
"""Create a distributed CRC32C combiner instance.
278282
279283
Args:
@@ -292,7 +296,7 @@ def create_distributed_crc32c(initial_bytes: bytes, total_length: int) -> Any:
292296

293297

294298
def finalize_distributed_checksum(
295-
results: list[tuple[HeaderArray, tuple[int, int, int]]], combiner: Any
299+
results: list[tuple[HeaderArray, tuple[int, int, int]]], combiner: DistributedCRC32C
296300
) -> tuple[HeaderArray, int]:
297301
"""Finalize a distributed CRC32C checksum from scan results.
298302
@@ -309,8 +313,6 @@ def finalize_distributed_checksum(
309313
"""
310314
require_checksum_libraries()
311315

312-
import numpy as np
313-
314316
headers: list[HeaderArray] = []
315317
for result in results:
316318
headers.append(result[0])
@@ -334,4 +336,3 @@ def finalize_distributed_checksum(
334336
"create_distributed_crc32c",
335337
"finalize_distributed_checksum",
336338
]
337-

src/mdio/segy/parsers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
default_cpus = cpu_count(logical=True)
2424

25+
2526
def parse_headers(
2627
segy_file_kwargs: SegyFileArguments,
2728
num_traces: int,
@@ -74,4 +75,4 @@ def parse_headers(
7475
headers: list[HeaderArray] = list(lazy_work)
7576

7677
# Merge blocks before return
77-
return np.concatenate(headers)
78+
return np.concatenate(headers)

src/mdio/segy/utilities.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
from dask.array.core import normalize_chunks
1212

1313
from mdio.core import Dimension
14-
from mdio.segy.geometry import GridOverrider
15-
16-
from mdio.segy.checksum import should_calculate_checksum
1714
from mdio.segy.checksum import is_checksum_available
15+
from mdio.segy.checksum import should_calculate_checksum
16+
from mdio.segy.geometry import GridOverrider
1817

1918
if TYPE_CHECKING:
2019
from numpy.typing import DTypeLike
@@ -61,10 +60,11 @@ def get_grid_plan( # noqa: C901, PLR0913
6160
"""
6261
calculate_checksum = False
6362
if should_calculate_checksum() and is_checksum_available():
64-
from mdio.segy.checksum import parse_headers
63+
from mdio.segy.checksum import parse_headers # noqa: PLC0415
64+
6565
calculate_checksum = True
6666
else:
67-
from mdio.segy.parsers import parse_headers
67+
from mdio.segy.parsers import parse_headers # noqa: PLC0415
6868

6969
if grid_overrides is None:
7070
grid_overrides = {}

tests/integration/test_crc32c_checksum.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def get_expected_crc32c(segy_path: Path) -> int:
4747
CRC32C checksum as integer
4848
"""
4949
# Import here to keep it test-only
50-
import google_crc32c
50+
import google_crc32c # noqa: PLC0415
5151

5252
crc = google_crc32c.Checksum()
5353

@@ -175,4 +175,4 @@ def test_corrupted_data_detects_changes(self, tmp_path: Path, monkeypatch: pytes
175175
assert mdio_crc == corrupted_crc, (
176176
f"MDIO checksum doesn't match corrupted file checksum. "
177177
f"MDIO: 0x{mdio_crc:08x}, Expected: 0x{corrupted_crc:08x}"
178-
)
178+
)

0 commit comments

Comments
 (0)