def _init_solutions(self, label, load_from, interpolate, save_to, exportables): """ Internal helper implementation for init_solutions(): this initializes a pair of solution databases. Args: label (str): the Jones matrix label load_from (str): filename of solution DB to load from. Can be empty or None. interpolate (bool): True if solutions are allowed to be interpolated. save_to (str): filename of solution DB to save to. Can be empty or None. exportables (dict): Dictionary of {key: (empty_value, axis_list)} describing the solutions that will be saved. As returned by exportable_solutions() of the gain machine. """ # init solutions from database if load_from: print>>log(0, "blue"), "{} solutions will be initialized from {}".format(label, load_from) if "//" in load_from: filename, prefix = load_from.rsplit("//", 1) else: filename, prefix = load_from, label self._init_sols[label] = param_db.load(filename), prefix, interpolate # create database to save to if save_to: # define parameters in DB for sol_label, (empty_value, axes) in exportables.iteritems(): self.define_param(save_to, "{}:{}".format(label, sol_label), empty_value, axes) print>> log(0), "{} solutions will be saved to {}".format(label, save_to)
def extract_from_db(dbfile, name="G:gain"): """Extract from dbfile""" LOGGER.debug("Loading gains database {}".format(dbfile)) try: dbdata = db.load(dbfile) gains = dbdata[name] gainscube = gains.get_cube() data = gainscube.data[-1] # remove n_dir except Exception as e: # print(e) data = np.load(dbfile) data = data[-1] #,0] # assuming ntchunks is 1, remove n_dir return data
def __init__(self, gmfactory, ifrgain_opts, compute=True): """ Initializes the IFR-based gains machinery. Args: gmfactory: a GainMachine Factory is used to manage the solution databases ifrgain_opts: dict of options compute: if False, gains are not computed even if options ask them to """ from cubical.main import expand_templated_name self.gmfactory = gmfactory load_from = expand_templated_name(ifrgain_opts['load-from']) save_to = expand_templated_name(ifrgain_opts['save-to']) self._ifrgains_per_chan = ifrgain_opts['per-chan'] self._ifrgain = None self._nfreq = gmfactory.grid["freq"] nfreq, nant, ncorr = [ len(gmfactory.grid[axis]) for axis in ("freq", "ant", "corr") ] if load_from: filename = load_from print(ModColor.Str( "applying baseline-based corrections (BBCs) from {}".format( filename), col="green"), file=log(0)) if "//" in filename: filename, prefix = filename.rsplit("//", 1) else: filename, prefix = filename, "BBC" parm = param_db.load(filename).get(prefix) if parm is None: print(ModColor.Str(" no solutions for '{}' in {}".format( prefix, filename)), file=log(0)) else: self._ifrgain = parm.reinterpolate( freq=gmfactory.grid["freq"]).filled() if tuple(self._ifrgain.shape) != (nfreq, nant, nant, ncorr, ncorr): print(ModColor.Str( " invalid BBC shape {}, will ignore".format( self._ifrgain.shape)), file=log(0)) self._ifrgain = None else: print(" loaded per-channel BBCs of shape {}".format( filename, self._ifrgain.shape), file=log(0)) if not self._ifrgains_per_chan: print(" using one mean value across band", file=log(0)) self._ifrgain[np.newaxis, ...] = self._ifrgain.mean(axis=0) # reset off-diagonal values, if needed if ifrgain_opts["apply-2x2"]: print(ModColor.Str( " using full 2x2 BBCs. You'd better know what you're doing!", col="green"), file=log(0)) else: self._ifrgain[..., (0, 1), (1, 0)] = 1 print(" using parallel-hand BBCs only", file=log(0)) if save_to and compute: self._compute_2x2 = ifrgain_opts["compute-2x2"] # setup axes for IFR-based gains axes = ["freq", "ant1", "ant2", "corr1", "corr2"] # define the ifrgain parameter self._save_filename = save_to parm = gmfactory.define_param(self._save_filename, "BBC", 1 + 0j, axes, interpolation_axes=["freq"]) self._ifrgains_grid = { axis: parm.grid[i] for i, axis in enumerate(axes) } # initialize accumulators for M.D^H and D.D^H terms self._mdh_sum = np.ma.zeros(parm.shape, gmfactory.ctype, fill_value=0) self._ddh_sum = np.ma.zeros(parm.shape, gmfactory.ctype, fill_value=0) # print( "will compute & save suggested baseline-based corrections (BBCs) to {}" .format(self._save_filename), file=log(0)) print( " (these can optionally be applied in a subsequent CubiCal run)", file=log(0)) else: self._ifrgains_grid = None
def reload(self): """Reloads saved IFR gain solutions from database""" return param_db.load(self._save_filename)["BBC"].get_cube()