-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathspectral.py
150 lines (123 loc) · 6.13 KB
/
spectral.py
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
import cluster
import argparse
import multiprocessing as mp
import numpy as np
import time
import pypeline_io as io
def get_arguments():
help_str = """
pypeline help string
"""
prog = 'pypeline'
max_cpu = mp.cpu_count()
# logger.debug("Getting commandline arguments.")
parser = argparse.ArgumentParser(description=help_str, prog=prog)
parser.add_argument("--cluster_config_file", "-c", dest="cluster_config",
action="store",default=None,
help="Points to the cluster config file")
parser.add_argument("--parallel", "-p", dest="parallel",
action="store_true", default=True,
help='Run in parallel (default False)')
parser.add_argument("--num_cpus", "-n", dest="num_cpus",
action="store", default=max_cpu, type=int,
help="Number of CPUs to use in the fitting process. "
"Default is max number of cpus available ({} for "
"this system.)".format(max_cpu))
parser.add_argument("--continue", dest='cont',
action='store_true', default=True,
help='Continue an unfinished run')
parser.add_argument("--resolution", "-r", dest='resolution',
action='store', default=2, type=int,
help='Generate a low, medium, or high resolution temperature map. Low = 1, Med = 2, High = 3. '
'High resolution is a fit for every pixel, medium pixels are 3x3, low pixels are 5x5.')
parser.add_argument("--dev-mtpc", dest='dev_mtpc', type=int, action="store",
default=0, help="Max Task Per Child sent to mp.Pool()")
args = parser.parse_args()
return args
def print_stage_tmap_prep(cluster: cluster.ClusterObj):
prep_str = """Now ready for spectral fitting.
If offloaded, copy the spectral fits file, {spectral_fits},
back to the local machine.
Next, run
python acb.py --temperature_map --resolution 2 --cluster_config_file /path/to/cluster/A115/A115_pypeline_config.ini
This will create the temperature map and allow for the creation of the pressure maps.""".format(
spectral_fits=cluster.spec_fits_file
)
print(prep_str)
def print_iteration_string(start_time, iteration, total):
i = iteration
num_region_lists = total
try:
elapsed = time.time() - start_time
time_elapsed_str = time.strftime("%H hours %M minutes %S seconds",
time.gmtime(elapsed))
time_per_iteration = elapsed/iteration
time_per_iteration_str = time.strftime("%H hours %M minutes %S seconds",
time.gmtime(time_per_iteration))
time_remaining = time_per_iteration * (total-iteration)
time_remaining_str = time.strftime("%H hours %M minutes %S seconds",
time.gmtime(time_remaining))
io.print_red("\n*********************************\n")
io.print_red("Iteration {} of {}".format(i + 1, num_region_lists))
io.print_red("Time elapsed: {elapsed}".format(elapsed=time_elapsed_str))
io.print_red("Approximate time per iteration: {}".format(time_per_iteration_str))
io.print_red("Approximate time remaining: {}".format(time_remaining_str))
io.print_red("\n*********************************\n")
except ZeroDivisionError:
io.print_red("\n*********************************\n")
io.print_red("Iteration {} of {}".format(i + 1, num_region_lists))
io.print_red("\n*********************************\n")
def calculate_spectral_fits(clstr: cluster.ClusterObj, num_cpus=mp.cpu_count()):
args = get_arguments()
if args.cont \
and io.file_exists(clstr.spec_fits_file) \
and io.file_exists(clstr.bad_fits_file) \
and io.file_exists(clstr.all_fits_file):
print("Continuing spectral fits")
regions = clstr.unfinished_regions_to_fit(args.resolution)
original = len(clstr.scale_map_regions_to_fit(args.resolution))
print('{reg} of {orig} regions left to fit.'.format(
reg=len(regions),
orig=original)
)
else:
clstr.initialize_best_fits_file()
clstr.initialize_worst_fits_file()
clstr.initialize_all_fits_file()
regions = clstr.scale_map_regions_to_fit(args.resolution)
print("Regions to fit: {reg}".format(reg=len(regions)))
start_time = time.time()
num_regions = len(regions)
if num_cpus:
args.num_cpus = num_cpus
if args.parallel and args.num_cpus > 1:
num_region_lists = (num_regions//args.num_cpus)+1
region_lists = np.array_split(regions, num_region_lists)
print("Starting {} iterations with ~{} fits per iteration.".format(len(region_lists), len(region_lists[0])))
for i, small_region_list in enumerate(region_lists):
print_iteration_string(start_time, i, num_region_lists)
processes = [mp.Process(target=clstr.fit_region_number, args=(region,)) for region in small_region_list]
for process in processes:
process.start()
for process in processes:
process.join()
else:
num_regions = len(regions)
counter = 0
for region in regions:
print("Fitting region number {region}".format(region=region))
clstr.fit_region_number(region)
counter += 1
if counter % 10 == 0 or counter == num_regions:
print("{} of {} regions complete".format(counter, num_regions))
time_elapsed = time.strftime("%H hours %M minutes %S seconds.",
time.gmtime(time.time() - start_time))
print("Time elapsed: {}.".format(time_elapsed))
print_stage_tmap_prep(clstr)
if __name__ == '__main__':
args = get_arguments()
if args.cluster_config is not None:
clstr = cluster.load_cluster(args.cluster_config)
calculate_spectral_fits(clstr)
else:
print("Check help string. spectral.py --help")