-
Notifications
You must be signed in to change notification settings - Fork 0
/
Multi_Fixed_SimRunner.py
420 lines (342 loc) · 23 KB
/
Multi_Fixed_SimRunner.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
from __future__ import print_function
import numpy
import os
import sys
import time
from functools import partial
import multiprocessing
from RadioTelescope import antenna_gain_creator
from RadioTelescope import baseline_converter
from RadioTelescope import redundant_baseline_finder
from RadioTelescope import xyz_position_creator
from RadioTelescope import antenna_table_loader
from GeneralTools import TrueSolutions_Organizer
from GeneralTools import save_to_hdf5
from GeneralTools import solution_mapper
from SkyModel import analytic_visibilities
from SkyModel import numerical_visibilities
from RedundantCalibration import Redundant_Calibrator
from RedundantCalibration import LogcalMatrixPopulator
def source_flux_and_position_offset_changer_FixedMP(telescope_param, calibration_channel, noise_param, sky_param,
beam_param,
calibration_scheme, peakflux_range, offset_range, n_iterations,
save_to_disk,
processes):
"""
"""
print("Simulating the Calibration of Arrays with Redundancy")
print("Changing Maximum Flux and Position offsets")
print("Fixed position offsets")
start_time = time.time()
if not os.path.exists(save_to_disk[1]):
print("")
print("!!!Warning: Creating output folder at output destination!")
os.makedirs(save_to_disk[1])
output_types = ["ideal_amp", "ideal_phase", "noisy_amp", "noisy_phase"]
for output in output_types:
os.makedirs(save_to_disk[1] + "threaded_" + output + "/")
minimum_position_offset = numpy.log10(offset_range[0])
maximum_position_offset = numpy.log10(offset_range[1])
position_step_number = offset_range[2]
position_offsets = numpy.logspace(minimum_position_offset, maximum_position_offset, position_step_number)
minimum_peakflux = numpy.log10(peakflux_range[0])
maximum_peakflux = numpy.log10(peakflux_range[1])
peakflux_step_number = peakflux_range[2]
peak_fluxes = numpy.logspace(minimum_peakflux, maximum_peakflux, peakflux_step_number)
iterations = numpy.arange(n_iterations)
# generate idealized telescope coordinates
if telescope_param[0] == 'square' \
or telescope_param[0] == 'hex' \
or telescope_param[0] == 'doublehex' \
or telescope_param[0] == 'doublesquare' \
or telescope_param[0] == 'linear':
xyz_positions = xyz_position_creator(telescope_param)
else:
xyz_positions = antenna_table_loader(telescope_param[0])
# generate antenna gains
frequency_range = numpy.array(calibration_channel)
gain_table = antenna_gain_creator(xyz_positions, frequency_range)
# Create an initial baseline tables to identify which parameters we're going to solve for.
baseline_table = baseline_converter(xyz_positions, gain_table, frequency_range)
red_baseline_table = redundant_baseline_finder(baseline_table, 'ALL', verbose=True)
amp_matrix, phase_matrix, red_tiles, red_groups = LogcalMatrixPopulator(red_baseline_table, xyz_positions)
xy_offsets = numpy.random.normal(0, 1, xyz_positions[:, 1:3].shape)
numpy.savetxt(save_to_disk[1]+"position_offsets.txt", xy_offsets)
file = open(save_to_disk[1] + "SFPO_simulation_parameters.log", "w")
file.write("Changing Source Flux and Position Offset simulation\n")
file.write("Fixed and Scaled Positions offsets\n")
file.write("Telescope Parameters: " + str(telescope_param) + "\n")
file.write("Calibration Channel: " + str(frequency_range / 1e6) + "MHz \n")
file.write("Noise Parameters: " + str(noise_param) + "\n")
file.write("Sky Model: " + str(sky_param) + "\n")
file.write("Beam Parameters: " + str(beam_param) + "\n")
file.write("Calibration scheme: " + str(calibration_scheme) + "\n")
file.write("Offset Range: " + str(offset_range) + "\n")
file.write("Peak Flux Range: " + str(peakflux_range) + "\n")
file.write("Iterations: " + str(n_iterations) + "\n")
file.close()
pool = multiprocessing.Pool(processes=processes)
iterator = partial(single_iteration_source_flux_position_offset_Fixed,
xyz_positions, gain_table, frequency_range, peak_fluxes, position_offsets, calibration_scheme,
sky_param, noise_param, beam_param, save_to_disk, red_tiles, red_groups, n_iterations, xy_offsets)
pool.map(iterator, iterations)
end_time = time.time()
runtime = end_time - start_time
print("Runtime", runtime)
file = open(save_to_disk[1] + "SFPO_simulation_parameters.log", "a")
file.write("Runtime: " + str(runtime) + "\n")
file.close()
return
def single_iteration_source_flux_position_offset_Fixed(xyz_positions, gain_table, frequency_range, peak_fluxes,
position_offsets, calibration_scheme, sky_param,
noise_param,
beam_param, save_to_disk, red_tiles, red_groups,
n_processes, xy_offsets, iteration):
parameters = numpy.concatenate((red_tiles, red_groups))
noisy_amp_solutions = numpy.zeros((len(parameters), len(position_offsets), len(peak_fluxes)))
noisy_phase_solutions = numpy.zeros((len(parameters), len(position_offsets), len(peak_fluxes)))
ideal_amp_solutions = numpy.zeros((len(parameters), len(position_offsets), len(peak_fluxes)))
ideal_phase_solutions = numpy.zeros((len(parameters), len(position_offsets), len(peak_fluxes)))
for offset_index in range(len(position_offsets)):
offset_positions = xyz_positions.copy()
offset_positions[:, 1:3] += xy_offsets * position_offsets[offset_index]
offset_baseline_table = baseline_converter(offset_positions, gain_table, frequency_range, verbose=False)
off_red_baseline_table = redundant_baseline_finder(offset_baseline_table, 'ALL')
if off_red_baseline_table.shape[0] == 0:
empty_results = numpy.zeros(noisy_amp_solutions[:, offset_index, :].shape)
empty_results[:] = numpy.nan
noisy_amp_solutions[:, offset_index, :] = empty_results
noisy_phase_solutions[:, offset_index, :] = empty_results
ideal_amp_solutions[:, offset_index, :] = empty_results
ideal_phase_solutions[:, offset_index, :] = empty_results
else:
if sky_param[0] == "point_and_background":
background_model = ['background']
obs_background, ideal_background, model_background = \
numerical_visibilities(off_red_baseline_table, frequency_range, noise_param, background_model,
beam_param, iteration)
for flux_index in range(len(peak_fluxes)):
if sky_param[0] == "point":
sky_model = [sky_param[0], peak_fluxes[flux_index], sky_param[2], sky_param[3]]
obs_visibilities, ideal_visibilities, model_visibilities = \
analytic_visibilities(off_red_baseline_table, frequency_range, noise_param, sky_model,
beam_param, iteration)
elif sky_param[0] == "point_and_background":
sky_model = ["point", peak_fluxes[flux_index], sky_param[2], sky_param[3]]
obs_point_source, ideal_point_source, model_point_source = \
analytic_visibilities(off_red_baseline_table, frequency_range, noise_param, sky_model,
beam_param, iteration)
obs_visibilities = obs_background + obs_point_source
ideal_visibilities = ideal_background + ideal_point_source
model_visibilities = model_background + model_point_source
amp_matrix, phase_matrix, off_red_tiles, off_red_groups = LogcalMatrixPopulator(off_red_baseline_table,
offset_positions)
offset_parameters = numpy.concatenate((off_red_tiles, off_red_groups))
if calibration_scheme == 'lincal':
true_solutions = TrueSolutions_Organizer(gain_table, model_visibilities, off_red_baseline_table,
off_red_tiles, off_red_groups)
calibration_param = ['lincal', true_solutions]
elif calibration_scheme == 'logcal' or calibration_scheme == 'full':
calibration_param = [calibration_scheme]
else:
sys.exit("INVALID PARAMETER -calibration_scheme: 'logcal','lincal' or 'full'")
# Pass the visibility data and calibration parameters along to the calibrator
noisy_amp_results, noisy_phase_results = \
Redundant_Calibrator(amp_matrix, phase_matrix, obs_visibilities, off_red_baseline_table,
off_red_tiles, off_red_groups, calibration_param)
ideal_amp_results, ideal_phase_results = \
Redundant_Calibrator(amp_matrix, phase_matrix, ideal_visibilities, off_red_baseline_table,
off_red_tiles, off_red_groups, calibration_param)
# map the solutions from the offset array to the ideally redundant array
noisy_amp_solutions[:, offset_index, flux_index] = solution_mapper(parameters, offset_parameters,
noisy_amp_results)
noisy_phase_solutions[:, offset_index, flux_index] = solution_mapper(parameters, offset_parameters,
noisy_phase_results)
ideal_amp_solutions[:, offset_index, flux_index] = solution_mapper(parameters, offset_parameters,
ideal_amp_results)
ideal_phase_solutions[:, offset_index, flux_index] = solution_mapper(parameters, offset_parameters,
ideal_phase_results)
# Subtract the point source
if sky_param[0] == "point_and_background":
obs_visibilities -= obs_point_source
ideal_visibilities -= ideal_point_source
model_visibilities -= model_point_source
axesdata = [parameters, position_offsets, peak_fluxes]
axeskeys = ['parameters', 'positions_uncertainty', 'peak_fluxes']
output_types = ["ideal_amp", "ideal_phase", "noisy_amp", "noisy_phase"]
prefix = str(0) * (len(str(n_processes)) - len(str(iteration))) + str(iteration)
save_to_hdf5(save_to_disk[1] + "threaded_" + output_types[0] + "/", prefix + "_SFPO_ideal_amp_solutions",
ideal_amp_solutions, axesdata, axeskeys)
save_to_hdf5(save_to_disk[1] + "threaded_" + output_types[1] + "/", prefix + "_SFPO_ideal_phase_solutions",
ideal_phase_solutions, axesdata, axeskeys)
save_to_hdf5(save_to_disk[1] + "threaded_" + output_types[2] + "/", prefix + "_SFPO_noisy_amp_solutions",
noisy_amp_solutions, axesdata, axeskeys)
save_to_hdf5(save_to_disk[1] + "threaded_" + output_types[3] + "/", prefix + "_SFPO_noisy_phase_solutions",
noisy_phase_solutions, axesdata, axeskeys)
return
def source_location_and_position_offset_changer_FixedMP(telescope_param, calibration_channel, noise_param, sky_param,
beam_param,
calibration_scheme, source_position_range, offset_range,
n_iterations,
save_to_disk,
processes):
"""
"""
print("Simulating the Calibration of Arrays with Redundancy")
print("Changing the source location and Position offsets")
print("Fixed position offsets")
start_time = time.time()
if not os.path.exists(save_to_disk[1]):
print("")
print("!!!Warning: Creating output folder at output destination!")
os.makedirs(save_to_disk[1])
output_types = ["ideal_amp", "ideal_phase", "noisy_amp", "noisy_phase"]
for output in output_types:
os.makedirs(save_to_disk[1] + "threaded_" + output + "/")
minimum_position_offset = numpy.log10(offset_range[0])
maximum_position_offset = numpy.log10(offset_range[1])
position_step_number = offset_range[2]
position_offsets = numpy.logspace(minimum_position_offset, maximum_position_offset, position_step_number)
iterations = numpy.arange(n_iterations)
# generate idealized telescope coordinates
if telescope_param[0] == 'square' \
or telescope_param[0] == 'hex' \
or telescope_param[0] == 'doublehex' \
or telescope_param[0] == 'doublesquare' \
or telescope_param[0] == 'linear':
xyz_positions = xyz_position_creator(telescope_param)
else:
xyz_positions = antenna_table_loader(telescope_param[0])
# generate antenna gains
frequency_range = numpy.array(calibration_channel)
gain_table = antenna_gain_creator(xyz_positions, frequency_range)
# Create an initial baseline tables to identify which parameters we're going to solve for.
baseline_table = baseline_converter(xyz_positions, gain_table, frequency_range)
red_baseline_table = redundant_baseline_finder(baseline_table, 'ALL', verbose=True)
amp_matrix, phase_matrix, red_tiles, red_groups = LogcalMatrixPopulator(red_baseline_table, xyz_positions)
print("current setting l_steps=:",source_position_range[2])
max_b = numpy.max(numpy.abs(baseline_table[:, 2:4, -1]))
min_l = 1. / max_b
delta_l = 0.1 * min_l
n_l_steps = int((source_position_range[1]-source_position_range[0]) / delta_l)
if n_l_steps >= source_position_range[2]:
source_position_range[2] = n_l_steps
print("Warning: source location was too low, increased to", n_l_steps)
elif n_l_steps >= 999:
source_position_range[2] = 999
source_locations = numpy.linspace(source_position_range[0], source_position_range[1], source_position_range[2])
file = open(save_to_disk[1] + "SLPO_simulation_parameters.log", "w")
file.write("Changing Source Location and Position Offset simulation\n")
file.write("Fixed and Scaled Positions offsets\n")
file.write("Telescope Parameters: " + str(telescope_param) + "\n")
file.write("Calibration Channel: " + str(frequency_range / 1e6) + "MHz \n")
file.write("Noise Parameters: " + str(noise_param) + "\n")
file.write("Sky Model: " + str(sky_param) + "\n")
file.write("Source location parameters: " + str(source_position_range) + "\n")
file.write("Calibration scheme: " + str(calibration_scheme) + "\n")
file.write("Offset Range: " + str(offset_range) + "\n")
file.write("Beam Parameters: " + str(beam_param) + "\n")
file.write("Iterations: " + str(n_iterations) + "\n")
file.close()
xy_offsets = numpy.random.normal(0, 1, xyz_positions[:, 1:3].shape)
numpy.savetxt(save_to_disk[1]+"position_offsets.txt", xy_offsets)
pool = multiprocessing.Pool(processes=processes)
iterator = partial(single_iteration_source_location_position_offset_Fixed,
xyz_positions, gain_table, frequency_range, source_locations, position_offsets,
calibration_scheme, sky_param, noise_param, beam_param, save_to_disk, red_tiles, red_groups,
n_iterations, xy_offsets)
pool.map(iterator, iterations)
end_time = time.time()
runtime = end_time - start_time
print("Runtime", runtime)
file = open(save_to_disk[1] + "SLPO_simulation_parameters.log", "a")
file.write("Runtime: " + str(runtime) + "\n")
file.close()
return
def single_iteration_source_location_position_offset_Fixed(xyz_positions, gain_table, frequency_range,
source_locations, position_offsets,
calibration_scheme, sky_param, noise_param,
beam_param, save_to_disk, red_tiles, red_groups, n_processes,
xy_offsets, iteration):
parameters = numpy.concatenate((red_tiles, red_groups))
noisy_amp_solutions = numpy.zeros((len(parameters), len(position_offsets), len(source_locations)))
noisy_phase_solutions = numpy.zeros((len(parameters), len(position_offsets), len(source_locations)))
ideal_amp_solutions = numpy.zeros((len(parameters), len(position_offsets), len(source_locations)))
ideal_phase_solutions = numpy.zeros((len(parameters), len(position_offsets), len(source_locations)))
for offset_index in range(len(position_offsets)):
offset_positions = xyz_positions.copy()
offset_positions[:, 1:3] += xy_offsets * position_offsets[offset_index]
offset_baseline_table = baseline_converter(offset_positions, gain_table, frequency_range, verbose=False)
off_red_baseline_table = redundant_baseline_finder(offset_baseline_table, 'ALL')
if off_red_baseline_table.shape[0] == 0:
empty_results = numpy.zeros(noisy_amp_solutions[:, offset_index, :].shape)
empty_results[:] = numpy.nan
noisy_amp_solutions[:, offset_index, :] = empty_results
noisy_phase_solutions[:, offset_index, :] = empty_results
ideal_amp_solutions[:, offset_index, :] = empty_results
ideal_phase_solutions[:, offset_index, :] = empty_results
else:
if sky_param[0] == "point_and_background":
background_model = ['background']
obs_background, ideal_background, model_background = \
numerical_visibilities(off_red_baseline_table, frequency_range, noise_param, background_model,
beam_param, iteration+1000)
for location_index in range(len(source_locations)):
if sky_param[0] == "point":
sky_model = ["point", sky_param[1], source_locations[location_index], sky_param[3]]
obs_visibilities, ideal_visibilities, model_visibilities = \
analytic_visibilities(off_red_baseline_table, frequency_range, noise_param, sky_model,
beam_param, iteration)
elif sky_param[0] == "point_and_background":
sky_model = ["point", sky_param[1], source_locations[location_index], sky_param[3]]
obs_point_source, ideal_point_source, model_point_source = \
analytic_visibilities(off_red_baseline_table, frequency_range, noise_param, sky_model,
beam_param, iteration)
obs_visibilities = obs_background + obs_point_source
ideal_visibilities = ideal_background + ideal_point_source
model_visibilities = model_background + model_point_source
amp_matrix, phase_matrix, off_red_tiles, off_red_groups = LogcalMatrixPopulator(off_red_baseline_table,
offset_positions)
offset_parameters = numpy.concatenate((off_red_tiles, off_red_groups))
if calibration_scheme == 'lincal':
true_solutions = TrueSolutions_Organizer(gain_table, model_visibilities, off_red_baseline_table,
off_red_tiles, off_red_groups)
calibration_param = ['lincal', true_solutions]
elif calibration_scheme == 'logcal' or calibration_scheme == 'full':
calibration_param = [calibration_scheme]
else:
sys.exit("INVALID PARAMETER -calibration_scheme: 'logcal','lincal' or 'full'")
# Pass the visibility data and calibration parameters along to the calibrator
noisy_amp_results, noisy_phase_results = \
Redundant_Calibrator(amp_matrix, phase_matrix, obs_visibilities, off_red_baseline_table,
off_red_tiles, off_red_groups, calibration_param)
ideal_amp_results, ideal_phase_results = \
Redundant_Calibrator(amp_matrix, phase_matrix, ideal_visibilities, off_red_baseline_table,
off_red_tiles, off_red_groups, calibration_param)
# map the solutions from the offset array to the ideally redundant array
noisy_amp_solutions[:, offset_index, location_index] = solution_mapper(parameters, offset_parameters,
noisy_amp_results)
noisy_phase_solutions[:, offset_index, location_index] = solution_mapper(parameters, offset_parameters,
noisy_phase_results)
ideal_amp_solutions[:, offset_index, location_index] = solution_mapper(parameters, offset_parameters,
ideal_amp_results)
ideal_phase_solutions[:, offset_index, location_index] = solution_mapper(parameters, offset_parameters,
ideal_phase_results)
# Subtract the point source
if sky_param[0] == "point_and_background":
obs_visibilities -= obs_point_source
ideal_visibilities -= ideal_point_source
model_visibilities -= model_point_source
axesdata = [parameters, position_offsets, source_locations]
axeskeys = ['parameters', 'positions_uncertainty', 'source_locations']
output_types = ["ideal_amp", "ideal_phase", "noisy_amp", "noisy_phase"]
prefix = str(0) * (len(str(n_processes)) - len(str(iteration))) + str(iteration)
save_to_hdf5(save_to_disk[1] + "threaded_" + output_types[0] + "/", prefix + "_SLPO_ideal_amp_solutions",
ideal_amp_solutions, axesdata, axeskeys)
save_to_hdf5(save_to_disk[1] + "threaded_" + output_types[1] + "/", prefix + "_SLPO_ideal_phase_solutions",
ideal_phase_solutions, axesdata, axeskeys)
save_to_hdf5(save_to_disk[1] + "threaded_" + output_types[2] + "/", prefix + "_SLPO_noisy_amp_solutions",
noisy_amp_solutions, axesdata, axeskeys)
save_to_hdf5(save_to_disk[1] + "threaded_" + output_types[3] + "/", prefix + "_SLPO_noisy_phase_solutions",
noisy_phase_solutions, axesdata, axeskeys)
return