-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexp12_nwb_to_tiff.py
More file actions
53 lines (38 loc) · 1.68 KB
/
exp12_nwb_to_tiff.py
File metadata and controls
53 lines (38 loc) · 1.68 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
#!/usr/bin/env python3
"""Export frames from a TwoPhotonSeries in an NWB file to uncompressed TIFF files."""
import sys
from pathlib import Path
import pynwb
import tifffile
from tqdm import tqdm
def main() -> None:
if len(sys.argv) != 4:
print("Usage: python exp12_nwb_to_tiff.py <nwb_file> <series_name> <output_dir>")
print("Example: python exp12_nwb_to_tiff.py data.nwb TwoPhotonSeries ./tiffs")
sys.exit(1)
nwb_filepath = Path(sys.argv[1])
series_name = sys.argv[2]
output_dir = Path(sys.argv[3])
if not nwb_filepath.exists():
print(f"Error: NWB file not found: {nwb_filepath}")
sys.exit(1)
output_dir.mkdir(parents=True, exist_ok=True)
with pynwb.NWBHDF5IO(nwb_filepath, "r") as io:
nwbfile = io.read()
if series_name not in nwbfile.acquisition:
print(f"Error: '{series_name}' not found in acquisition group")
print(f"Available: {list(nwbfile.acquisition.keys())}")
sys.exit(1)
series = nwbfile.acquisition[series_name]
data = series.data
n_frames = data.shape[0]
print(f"Exporting {n_frames} frames from {series_name}")
print(f"Frame shape: {data.shape[1]}x{data.shape[2]} (width x height)")
print(f"Data type: {data.dtype}")
for frame_idx in tqdm(range(n_frames), desc="Exporting", unit="frame"):
frame = data[frame_idx, :, :].T # transpose from (width, height) to (height, width)
output_path = output_dir / f"frame_{frame_idx:06d}.tiff"
tifffile.imwrite(output_path, frame, compression=None)
print(f"Done. TIFF files saved to {output_dir}")
if __name__ == "__main__":
main()