-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpatch_pycbc_numpy2.py
More file actions
96 lines (77 loc) · 3.04 KB
/
patch_pycbc_numpy2.py
File metadata and controls
96 lines (77 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
Patch pycbc for numpy 2.x compatibility.
pycbc <= 2.10.0 has two numpy 2.x incompatibilities:
1. events/threshold_cpu.py: np.array(..., copy=False) raises ValueError in
numpy 2.x when a copy is required; numpy.asarray() preserves the original
numpy 1.x semantics (copy only if necessary, silently).
2. filter/matchedfilter.py: numpy.real(x) / numpy.imag(x) dispatch via
x.real / x.imag. On pycbc Array objects these are plain methods, not
properties, so numpy.real() returns the bound method instead of the data.
Replacing with numpy.asarray(x).real / .imag forces conversion via
Array.__array__ first, which is safe and localized to the affected calls.
Run once after installing pycbc:
python scripts/patch_pycbc_numpy2.py
"""
import inspect
import re
import shutil
import sys
from pathlib import Path
def _clear_cache(path):
cache_dir = path.parent / "__pycache__"
if cache_dir.exists():
shutil.rmtree(cache_dir)
print(f" Cleared __pycache__ in {cache_dir.parent.name}/")
def patch_threshold_cpu():
try:
import pycbc.events.threshold_cpu as mod
except ImportError:
print("pycbc not found – nothing to patch.", file=sys.stderr)
sys.exit(1)
path = Path(inspect.getfile(mod))
src = path.read_text()
original = src
src = src.replace(
"numpy.array(series.data, copy=False, dtype=numpy.complex64)",
"numpy.asarray(series.data, dtype=numpy.complex64)",
)
src = re.sub(
r"numpy\.array\(series\.data,\s*copy=False,\s*\n(\s*)dtype=numpy\.complex64\)",
r"numpy.asarray(series.data,\n\1dtype=numpy.complex64)",
src,
)
if src == original:
print("threshold_cpu.py: already patched or pattern not found, skipping.")
else:
path.write_text(src)
_clear_cache(path)
print("threshold_cpu.py: patched (copy=False → asarray).")
def patch_matchedfilter():
try:
import pycbc.filter.matchedfilter as mod
except ImportError:
print("pycbc not found – nothing to patch.", file=sys.stderr)
sys.exit(1)
path = Path(inspect.getfile(mod))
src = path.read_text()
original = src
replacements = [
("numpy.real(hplus)", "numpy.asarray(hplus).real"),
("numpy.real(hcross)", "numpy.asarray(hcross).real"),
("numpy.imag(hplus)", "numpy.asarray(hplus).imag"),
("numpy.imag(hcross)", "numpy.asarray(hcross).imag"),
("numpy.real(hphccorr)", "numpy.asarray(hphccorr).real"),
("numpy.real(hplus_cross_corr)", "numpy.asarray(hplus_cross_corr).real"),
]
for old, new in replacements:
src = src.replace(old, new)
if src == original:
print("matchedfilter.py: already patched or pattern not found, skipping.")
else:
path.write_text(src)
_clear_cache(path)
n = sum(1 for a, b in zip(original.splitlines(), src.splitlines()) if a != b)
print(f"matchedfilter.py: patched ({n} lines changed).")
if __name__ == "__main__":
patch_threshold_cpu()
patch_matchedfilter()