-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun_parallel.py
More file actions
98 lines (91 loc) · 4.19 KB
/
run_parallel.py
File metadata and controls
98 lines (91 loc) · 4.19 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
import subprocess
from concurrent.futures import ThreadPoolExecutor
import sys
import glob
import argparse
import os
def run(yaml_path):
"""Function to execute a script using subprocess."""
try:
# Ensure your script has the appropriate executable permissions
result = subprocess.run(['python3', 'run.py', yaml_path], check=True, text=True, capture_output=True)
print(f"{yaml_path} ran successfully:")
except subprocess.CalledProcessError as e:
error_message=e.stderr.decode()
print(f"Error running {yaml_path}: {error_message}:")
def run_analysis(yaml_path):
"""Function to execute a script using subprocess."""
try:
# Ensure your script has the appropriate executable permissions
result = subprocess.run(['python3', 'run_analysis.py', yaml_path], check=True, text=True, capture_output=True)
print(f"{yaml_path} ran successfully:")
except subprocess.CalledProcessError as e:
error_message=e.stderr.decode()
print(f"Error running {yaml_path}: {error_message}:")
def run_analysis_cfg(yaml_path):
"""Function to execute a script using subprocess."""
try:
# Ensure your script has the appropriate executable permissions
result = subprocess.run(['python3', 'run_analysis_cfg.py', yaml_path], check=True, text=True, capture_output=True)
print(f"{yaml_path} ran successfully:")
except subprocess.CalledProcessError as e:
error_message=e.stderr.decode()
print(f"Error running {yaml_path}: {error_message}:")
def run_analysis_regress(yaml_path):
"""Function to execute a script using subprocess."""
try:
# Ensure your script has the appropriate executable permissions
result = subprocess.run(['python3', 'run_analysis_regress.py', yaml_path], check=True, text=True, capture_output=True)
print(f"{yaml_path} ran successfully:")
except subprocess.CalledProcessError as e:
error_message=e.stderr.decode()
print(f"Error running {yaml_path}: {error_message}:")
def calc_mse_pix(yaml_path):
"""Function to execute a script using subprocess."""
try:
# Ensure your script has the appropriate executable permissions
result = subprocess.run(['python3', 'calc_mse_pix.py', yaml_path], check=True, text=True, capture_output=True)
print(f"{yaml_path} ran successfully:")
except subprocess.CalledProcessError as e:
error_message=e.stderr.decode()
print(f"Error running {yaml_path}: {error_message}:")
def parse_args():
parser = argparse.ArgumentParser(description="Run multiple scripts in parallel")
parser.add_argument("folfile", type=str, help="Folder/File containing the yamls")
parser.add_argument("--n-par", type=int, default=0, help="Number of parallel processes")
parser.add_argument("--exname", type=str,default="run", help="exname.py will be ran, default is \"run\" ")
args=parser.parse_args()
return args
if __name__ == "__main__":
# Parse arguments
args=parse_args()
#check if folfile is a file
if os.path.isfile(args.folfile):
with open(args.folfile,"r") as f:
yaml_paths = f.readlines()
yaml_paths = [x.strip() for x in yaml_paths]
else:
fol=args.folfile
yaml_paths = glob.glob(f"{fol}/*.yaml")
if args.n_par>0:
n_par=args.n_par
else:
n_par=len(yaml_paths)
print("Running:",yaml_paths)
# Use ThreadPoolExecutor to run scripts in parallel
with ThreadPoolExecutor(max_workers=n_par) as executor:
# Map each script to the executor
if args.exname=="run":
results = executor.map(run, yaml_paths)
elif args.exname=="run_analysis":
results = executor.map(run_analysis, yaml_paths)
elif args.exname=="run_analysis_cfg":
results = executor.map(run_analysis_cfg, yaml_paths)
results = executor.map(run_analysis_cfg2, yaml_paths)
elif args.exname=="run_analysis_regress":
results = executor.map(run_analysis_regress, yaml_paths)
elif args.exname=="calc_mse_pix":
results = executor.map(calc_mse_pix, yaml_paths)
else:
raise ValueError("exname not recognized")
print("Done")