Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _str_to_obj(self, beam_model, use_shared_mem=False):
# Convert beam strings to objects.
if isinstance(beam_model, (AnalyticBeam, UVBeam)):
return beam_model
if beam_model.startswith('analytic'):
bspl = beam_model.split('_')
model = bspl[1]
to_set = {}
for extra in bspl[2:]:
par, val = extra.split('=')
full = self._float_params[par]
to_set[full] = float(val)
return AnalyticBeam(model, **to_set)
path = beam_model # beam_model = path to beamfits
uvb = UVBeam()
if use_shared_mem and (mpi.world_comm is not None):
if mpi.rank == 0:
uvb.read_beamfits(path)
uvb.peak_normalize()
for key, attr in uvb.__dict__.items():
if not isinstance(attr, parameter.UVParameter):
continue
if key == '_data_array':
uvb.__dict__[key].value = mpi.shared_mem_bcast(attr.value, root=0)
else:
uvb.__dict__[key].value = mpi.world_comm.bcast(attr.value, root=0)
mpi.world_comm.Barrier()
else:
and (os.path.exists(beam_model)
or os.path.exists(os.path.join(SIM_DATA_PATH, beam_model)))):
if os.path.exists(beam_model):
beam_list.append(beam_model)
else:
beam_list.append(os.path.join(SIM_DATA_PATH, beam_model))
# Failing that, try to parse the beam string as an analytic beam.
else:
if isinstance(beam_model, str):
beam_type = beam_model
elif 'type' in beam_model:
beam_type = beam_model['type']
else:
raise ValueError("Beam model must have a 'type' field.")
if beam_type not in AnalyticBeam.supported_types:
raise ValueError("Undefined beam model type: {}".format(beam_type))
this_beam_opts = {}
if isinstance(beam_model, dict):
for key in beam_model:
if key != 'type':
this_beam_opts[key] = beam_model[key]
# Gaussian beam requires either diameter or sigma
# Airy beam requires diameter
# Default to None for diameter and sigma.
# Values in the "beam_paths" override globally-defined options.
beam_opts = {'diameter': None, 'sigma': None}
for opt in beam_opts.keys():
val = telconfig.get(opt, None)
def _obj_to_str(self, beam_model):
# Convert beam objects to strings that may generate them.
if isinstance(beam_model, str):
return beam_model
if isinstance(beam_model, AnalyticBeam):
btype = beam_model.type
bm_str = 'analytic_' + btype
for abbrv, full in self._float_params.items():
val = getattr(beam_model, full)
if val is not None:
bm_str += '_' + abbrv + '=' + str(val)
return bm_str
# If not AnalyticBeam, it's UVBeam.
try:
path = beam_model.extra_keywords['beam_path']
except KeyError:
raise ValueError("Need to set 'beam_path' key in extra_keywords for UVBeam objects.")
return path