Skip to content

Commit d79b8b6

Browse files
committed
[feat] move files (to be merged) out from suffx0* dir
1 parent 46b4c73 commit d79b8b6

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

tools/recover_merged_files.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
PDM merge .ncs files when there are multiple sessions and save results in one file. This leads to NaNs if there is a gap
3+
between sessions. We plan to save sessions separately in nwbPipeline. This module find files that are merged (still
4+
saved separately in folder 'suffix0*'). Move files to the parent folder and (optional) remove the combined files to save
5+
storage space.
6+
"""
7+
8+
import shutil
9+
import csv
10+
from pathlib import Path
11+
from typing import List, Union
12+
13+
def list_files_with_suffix(directory: Union[str, Path], pattern: str, result_name: str) -> List[Path]:
14+
"""List all files matching the pattern 'suffix*'."""
15+
if isinstance(directory, str):
16+
directory = Path(directory)
17+
18+
res = [path for path in directory.rglob(f'{pattern}*')]
19+
# Writing the list to a CSV file
20+
with open(result_name, 'w', newline='') as file:
21+
writer = csv.writer(file)
22+
for item in res:
23+
writer.writerow([str(item)]) # Write each item as a row
24+
25+
return res
26+
27+
28+
def move_ncs_files_to_parent(file_paths: List[Path]) -> None:
29+
"""Move only .ncs files from the list to the parent directory."""
30+
for path in file_paths:
31+
parent_dir = path.parent
32+
for ncs_file in path.glob('*.ncs'): # Look for .ncs files in the directory
33+
try:
34+
shutil.move(str(ncs_file), str(parent_dir / ncs_file.name))
35+
print(f"Moved: {ncs_file} to {parent_dir}")
36+
except Exception as e:
37+
print(f"Error moving {ncs_file}: {e}")
38+
39+
40+
if __name__ == '__main__':
41+
directory = '/Volumes/DATA/NLData/'
42+
suffix_pattern = 'suffix0' # The pattern to match
43+
44+
# Step 1: List all files matching 'suffix*'
45+
matching_files: List[Path] = list_files_with_suffix(directory, suffix_pattern, 'merged_directories.csv')
46+
47+
# Step 2: Move only the .ncs files
48+
# move_ncs_files_to_parent(matching_files)

0 commit comments

Comments
 (0)