-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug_pkl.py
More file actions
executable file
·297 lines (251 loc) · 9.53 KB
/
debug_pkl.py
File metadata and controls
executable file
·297 lines (251 loc) · 9.53 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python3
"""
Debug script for inspecting pickle files.
Usage:
python debug_pkl.py <path_to_pkl_file>
python debug_pkl.py wham_output.pkl
python debug_pkl.py STS1_optimized.pkl
"""
import argparse
import gzip
import io
import pickle
import sys
import zipfile
from pathlib import Path
from typing import Any, Dict
import joblib
import numpy as np
def _decode_pickle(contents: bytes) -> Any:
"""Decode pickle using multiple methods (plain, joblib, gzip, zip)."""
buffer = io.BytesIO(contents)
# 1) Plain pickle
try:
return pickle.load(buffer)
except Exception as e:
print(f" Plain pickle failed: {e}")
buffer.seek(0)
# 2) Joblib (handles joblib/zlib wrapped pickles)
try:
return joblib.load(buffer)
except Exception as e:
print(f" Joblib pickle failed: {e}")
buffer.seek(0)
# 3) gzip-compressed pickle
try:
with gzip.GzipFile(fileobj=buffer) as gz:
return pickle.load(gz)
except Exception as e:
print(f" Gzip pickle failed: {e}")
buffer.seek(0)
# 4) Zip archive containing a pickle
try:
with zipfile.ZipFile(buffer) as zf:
for name in zf.namelist():
with zf.open(name) as inner:
data = inner.read()
try:
return pickle.loads(data)
except Exception:
continue
except Exception as e:
print(f" Zip pickle failed: {e}")
raise ValueError("Unable to decode pickle: unknown format")
def _inspect_value(value: Any, indent: int = 0, max_depth: int = 3, current_depth: int = 0) -> None:
"""Recursively inspect a value and print its structure."""
prefix = " " * indent
if current_depth >= max_depth:
print(f"{prefix}... (max depth reached)")
return
if isinstance(value, dict):
print(f"{prefix}Dict with {len(value)} keys:")
for key in sorted(value.keys())[:20]: # Show first 20 keys
print(f"{prefix} '{key}': ", end="")
_inspect_value(value[key], indent + 1, max_depth, current_depth + 1)
if len(value) > 20:
print(f"{prefix} ... ({len(value) - 20} more keys)")
elif isinstance(value, (list, tuple)):
print(f"{prefix}{type(value).__name__} with {len(value)} items")
if len(value) > 0:
print(f"{prefix} First item: ", end="")
_inspect_value(value[0], indent + 1, max_depth, current_depth + 1)
if len(value) > 1:
print(f"{prefix} ... ({len(value) - 1} more items)")
elif isinstance(value, np.ndarray):
print(f"{prefix}np.ndarray: shape={value.shape}, dtype={value.dtype}")
if value.size > 0 and value.size <= 10:
print(f"{prefix} Values: {value}")
elif value.size > 0:
print(f"{prefix} Min: {np.min(value)}, Max: {np.max(value)}, Mean: {np.mean(value)}")
elif isinstance(value, (str, int, float, bool, type(None))):
if isinstance(value, str) and len(value) > 100:
print(f"{prefix}{type(value).__name__}: '{value[:100]}...' (length: {len(value)})")
else:
print(f"{prefix}{type(value).__name__}: {value}")
else:
print(f"{prefix}{type(value).__name__}: {repr(value)[:100]}")
def debug_pickle(file_path: Path) -> None:
"""Debug a pickle file and print detailed information."""
print(f"=" * 80)
print(f"Debugging pickle file: {file_path}")
print(f"=" * 80)
if not file_path.exists():
print(f"ERROR: File not found: {file_path}")
sys.exit(1)
file_size = file_path.stat().st_size
print(f"File size: {file_size:,} bytes ({file_size / 1024 / 1024:.2f} MB)")
print()
# Read file
print("Attempting to decode pickle...")
try:
with open(file_path, "rb") as f:
contents = f.read()
data = _decode_pickle(contents)
print("✓ Successfully decoded pickle!")
except Exception as e:
print(f"✗ Failed to decode pickle: {e}")
sys.exit(1)
print()
print("=" * 80)
print("STRUCTURE INSPECTION")
print("=" * 80)
print()
# Inspect top-level structure
print(f"Top-level type: {type(data).__name__}")
print()
if isinstance(data, dict):
print(f"Dictionary with {len(data)} keys:")
print()
# Show all keys first
keys = list(data.keys())
print("Keys:")
for i, key in enumerate(keys, 1):
print(f" {i}. {key!r} ({type(data[key]).__name__})")
print()
# Detailed inspection of each key
print("Detailed information:")
print()
for key in keys:
print(f"Key: {key!r}")
value = data[key]
if isinstance(value, np.ndarray):
print(f" Type: numpy.ndarray")
print(f" Shape: {value.shape}")
print(f" Dtype: {value.dtype}")
print(f" Size: {value.size:,} elements")
if value.size > 0:
print(f" Min: {np.min(value)}")
print(f" Max: {np.max(value)}")
print(f" Mean: {np.mean(value)}")
print(f" Std: {np.std(value)}")
if value.size <= 10:
print(f" Values:\n{value}")
print()
elif isinstance(value, dict):
print(f" Type: dict")
print(f" Number of keys: {len(value)}")
if len(value) <= 10:
print(f" Sub-keys: {list(value.keys())}")
else:
print(f" Sub-keys: {list(value.keys())[:10]} ... ({len(value) - 10} more)")
print()
elif isinstance(value, (list, tuple)):
print(f" Type: {type(value).__name__}")
print(f" Length: {len(value)}")
if len(value) > 0:
print(f" First item type: {type(value[0]).__name__}")
if isinstance(value[0], np.ndarray):
print(f" First item shape: {value[0].shape}")
print()
else:
print(f" Type: {type(value).__name__}")
if isinstance(value, (str, int, float, bool)):
print(f" Value: {value}")
elif isinstance(value, str) and len(value) > 100:
print(f" Value: {value[:100]}... (truncated, length: {len(value)})")
else:
print(f" Value: {repr(value)[:200]}")
print()
elif isinstance(data, (list, tuple)):
print(f"{type(data).__name__} with {len(data)} items")
print()
if len(data) > 0:
print("First item:")
_inspect_value(data[0], indent=1)
print()
elif isinstance(data, np.ndarray):
print(f"numpy.ndarray")
print(f" Shape: {data.shape}")
print(f" Dtype: {data.dtype}")
print(f" Size: {data.size:,} elements")
if data.size > 0:
print(f" Min: {np.min(data)}")
print(f" Max: {np.max(data)}")
print(f" Mean: {np.mean(data)}")
print(f" Std: {np.std(data)}")
print()
else:
print(f"Type: {type(data).__name__}")
print(f"Value: {repr(data)[:500]}")
print()
# Memory usage estimate
print("=" * 80)
print("MEMORY ESTIMATES")
print("=" * 80)
print()
def estimate_size(obj):
"""Rough estimate of object size in bytes."""
if isinstance(obj, np.ndarray):
return obj.nbytes
elif isinstance(obj, dict):
return sum(estimate_size(v) for v in obj.values())
elif isinstance(obj, (list, tuple)):
return sum(estimate_size(item) for item in obj)
elif isinstance(obj, (str, bytes)):
return len(obj)
else:
return sys.getsizeof(obj)
try:
total_size = estimate_size(data)
print(f"Estimated total size: {total_size:,} bytes ({total_size / 1024 / 1024:.2f} MB)")
except Exception as e:
print(f"Could not estimate size: {e}")
print()
print("=" * 80)
print("Debugging complete!")
print("=" * 80)
def main():
parser = argparse.ArgumentParser(
description="Debug and inspect pickle files",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python debug_pkl.py # defaults to wham_output.pkl
python debug_pkl.py STS1_optimized.pkl
python debug_pkl.py path/to/file.pkl
"""
)
parser.add_argument(
"file",
nargs="?",
default=None,
type=str,
help="Path to the pickle file to debug (defaults to wham_output.pkl)"
)
args = parser.parse_args()
# Default to wham_output.pkl in the project root when no argument is provided
if args.file is None:
project_root = Path(__file__).resolve().parent
default_candidates = [
# project_root / "test/test.pkl",
project_root / "test/STS1_optimized.pkl",
project_root / "wham_output.pkl",
]
chosen = next((p for p in default_candidates if p.exists()), default_candidates[0])
print(f"No file provided. Defaulting to: {chosen}")
file_path = chosen
else:
file_path = Path(args.file)
debug_pickle(file_path)
if __name__ == "__main__":
main()