Skip to content

Commit

Permalink
Merge branch 'master' of github.com:cyriltasse/ddfacet
Browse files Browse the repository at this point in the history
  • Loading branch information
bennahugo committed Jan 4, 2022
2 parents 3dc85ae + 88d5713 commit 5b31ee7
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 37 deletions.
2 changes: 1 addition & 1 deletion DDFacet/DDF.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def main(OP=None, messages=[]):
print(ModColor.Str(" the original underlying error may be reported in the log [possibly far] above."), file=log)
report_error = True
except:
if sys.exc_info()[0] is not WorkerProcessError and Exceptions.is_pdb_enabled():
if sys.exc_info()[0]!=WorkerProcessError and Exceptions.is_pdb_enabled():
APP.terminate()
raise
else:
Expand Down
2 changes: 1 addition & 1 deletion DDFacet/Data/ClassFITSBeam.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def __init__ (self, ms, opts):
self.ms = ms
# filename is potentially a list (frequencies will be matched)
self.beamsets = opts["FITSFile"]
if type(self.beamsets) is not list:
if not isinstance(self.beamsets,list):
self.beamsets = self.beamsets.split(',')
self.pa_inc = opts["FITSParAngleIncDeg"]
self.time_inc = opts["DtBeamMin"]
Expand Down
2 changes: 1 addition & 1 deletion DDFacet/Data/ClassJones.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def InitDDESols(self, DATA, quiet=False):

self.HasKillMSSols = True

ApplyBeam=(GD["Beam"]["Model"] is not None) and (GD["Beam"]["Model"] is not "")
ApplyBeam=(GD["Beam"]["Model"] is not None) and (GD["Beam"]["Model"]!="")
if ApplyBeam:
self.ApplyCal = True
valid=False
Expand Down
4 changes: 2 additions & 2 deletions DDFacet/Data/ClassMS.py
Original file line number Diff line number Diff line change
Expand Up @@ -1679,9 +1679,9 @@ def expandMSList(MSName,defaultField=0,defaultDDID=0,defaultColumn="DATA"):
Ultimately, returns a list of (MSName, ddid, field) tuples, where MSName is a proper MS path, and ddid
and field are indices.
"""
if type(MSName) is list:
if isinstance(MSName,list):
print("multi-MS mode", file=log)
elif type(MSName) is not str:
elif not isinstance(MSName,str):
raise TypeError("MSName parameter must be a list or a filename")
elif MSName.endswith(".txt"):
MSName0 = MSName
Expand Down
2 changes: 1 addition & 1 deletion DDFacet/Data/ClassStokes.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def _extractStokesCombinationExpression(exp):
index of dependency (0 or 1) to be used as right-most value in the expressions listed above (compulsory
if binary operator was specified)
"""
if type(exp) is not list or len(exp) < 2:
if not isinstance(exp,list) or len(exp) < 2:
raise ValueError("Expected stokes dependency of the form x0 [x1 ... xN] expr")
deps = exp[0:len(exp)-1]
combExp = exp[len(exp)-1]
Expand Down
72 changes: 46 additions & 26 deletions DDFacet/Data/ClassVisServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def __init__(self, MSList, GD=None,
self.Super = GD["Weight"]["SuperUniform"]
self.VisWeights = None

self.HasPrintedTaperingSettings = False
self.EnableSigmoidTaper = GD["Weight"]["EnableSigmoidTaper"]
self.SigmoidInCut = GD["Weight"]["SigmoidTaperInnerCutoff"]
self.SigmoidOutCut = GD["Weight"]["SigmoidTaperOuterCutoff"]
Expand Down Expand Up @@ -145,7 +146,7 @@ def Init(self, PointingID=0):
self._chunk_shape = [0, 0, 0]

for msspec in self.MSList:
if type(msspec) is not str:
if not isinstance(msspec,str):
msname, ddid, field, column = msspec
else:
msname, ddid, field, column = msspec, self.DicoSelectOptions["DDID"], self.DicoSelectOptions["Field"], self.ColName
Expand Down Expand Up @@ -729,7 +730,7 @@ def __sigmoid(x):
__sigmoid(-uvdistlda * inner_taper_strength - inner_cut * inner_taper_strength)) - 2.0
visweights *= (y / y.max())

def _CalcSigmoidTaper(self):
def _CalcSigmoidTaper(self, ims, ms, ichunk):
"""
Sets up the UV crafter using Sigmoids to taper inner and outer
The cuts are specified in uvlambda and the rolloff tuning parameter will be clamped to
Expand All @@ -739,20 +740,20 @@ def _CalcSigmoidTaper(self):
to that form either way by the visreader)
"""
if self.EnableSigmoidTaper:
print("Tapering visibilities with the uv-crafter:", file=log)
print("\t Inner Cutoff {0:.2f} klda".format(self.SigmoidInCut * 1.0e-3), file=log)
print("\t Outer Cutoff {0:.2f} klda".format(self.SigmoidOutCut * 1.0e-3), file=log)
print("\t Inner Rolloff Strength {0:.2f}".format(self.SigmoidInRoll), file=log)
print("\t Outer Rolloff Strength {0:.2f}".format(self.SigmoidOutRoll), file=log)
for ims, ms in enumerate(self.ListMS):
for ichunk in range(len(ms.getChunkRow0Row1())):
# APP will handle any serialization if NCPU == 1
APP.runJob("SigmoidTaper:%d:%d" % (ims, ichunk), self._sigtaper,
args=(self._weight_dict[ims][ichunk].readwrite(),
ms.ChanFreq,
self.SigmoidInCut, self.SigmoidOutCut,
self.SigmoidOutRoll, self.SigmoidInRoll),
counter=self._weightjob_counter, collect_result=False)
if not self.HasPrintedTaperingSettings:
print("Tapering visibilities with the uv-crafter:", file=log)
print("\t Inner Cutoff {0:.2f} klda".format(self.SigmoidInCut * 1.0e-3), file=log)
print("\t Outer Cutoff {0:.2f} klda".format(self.SigmoidOutCut * 1.0e-3), file=log)
print("\t Inner Rolloff Strength {0:.2f}".format(self.SigmoidInRoll), file=log)
print("\t Outer Rolloff Strength {0:.2f}".format(self.SigmoidOutRoll), file=log)
self.HasPrintedTaperingSettings = True
# APP will handle any serialization if NCPU == 1
APP.runJob("SigmoidTaper:%d:%d" % (ims, ichunk), self._sigtaper,
args=(self._weight_dict[ims][ichunk].readwrite(),
ms.ChanFreq,
self.SigmoidInCut, self.SigmoidOutCut,
self.SigmoidOutRoll, self.SigmoidInRoll),
counter=self._weightjob_counter, collect_result=False)
APP.awaitJobCounter(self._weightjob_counter, progress="Sigmoid Tapering")

def _CalcWeights_handler(self):
Expand Down Expand Up @@ -818,9 +819,6 @@ def _CalcWeights_handler(self):
return
if not self._uvmax:
UserWarning("data appears to be fully flagged: can't compute imaging weights")

# compute and apply tapers
self._CalcSigmoidTaper()

# in natural mode, leave the weights as is. In other modes, setup grid for calculations
self._weight_grid = shared_dict.create("VisWeights.Grid")
Expand Down Expand Up @@ -869,6 +867,7 @@ def _CalcWeights_handler(self):
# launch jobs to finalize weights and save them to the cache
for ims, ms in enumerate(self.ListMS):
for ichunk in range(len(ms.getChunkRow0Row1())):
self._CalcSigmoidTaper(ims, ms, ichunk)
APP.runJob("FinalizeWeights:%d:%d" % (ims, ichunk), self._finalizeWeights_handler,
args=(self._weight_grid.readonly(),
self._weight_dict[ims][ichunk].readwrite(),
Expand Down Expand Up @@ -1135,6 +1134,15 @@ def _CalcWeights_serial(self):
npix = npixx * npixy
print("Calculating imaging weights on an [%i,%i]x%i grid with cellsize %g" % (npixx, npixy, nbands, cell), file=log)
self._weight_grid.addSharedArray("grid", (nbands, npix), np.float64)
else:
nbands = self.NFreqBands
nch, npol, npixIm, _ = self.FullImShape
FOV = self.CellSizeRad * npixIm
cell = 1. / (self.Super * FOV)
xymax = int(math.floor(self._uvmax / cell)) + 1
npixx = xymax * 2 + 1
npixy = xymax + 1
npix = npixx * npixy

# scan through MSs one by one
for ims, ms in enumerate(self.ListMS):
Expand All @@ -1154,16 +1162,16 @@ def _CalcWeights_serial(self):

# in Natural mode, we're done: dump weights out
if self.Weighting == "natural":
self._finalizeWeights_handler(None, msw, ims, ichunk, 0)
self._CalcSigmoidTaper(ims, ms, ichunk)
self._finalizeWeights_handler(None, msw,
ims, ichunk, ms.ChanFreq, cell,
npix, npixx, nbands, xymax)
# else accumulate onto uv grid
else:
self._accumulateWeights_handler(self._weight_grid, msw,
ims, ichunk, ms.ChanFreq, cell, npix, npixx, nbands, xymax)
# delete to save memory
for field in "weight", "uv", "flags", "index":
if field in msw:
msw.delete_item(field)

ims, ichunk, ms.ChanFreq, cell,
npix, npixx, nbands, xymax)

# save wmax to cache
cPickle.dump(wmax, open(wmax_path, "wb"))
self.maincache.saveCache("wmax")
Expand Down Expand Up @@ -1200,12 +1208,24 @@ def _CalcWeights_serial(self):
continue
print("reloading weights %d.%d" % (ims, ichunk), file=log)
self._loadWeights_handler(msw, ims, ichunk, self._ignore_vis_weights)
self._CalcSigmoidTaper(ims, ms, ichunk)
self._finalizeWeights_handler(self._weight_grid, msw,
ims, ichunk, ms.ChanFreq, cell, npix, npixx, nbands, xymax)

if self._weight_grid is not None:
self._weight_grid.delete()

# free memory
for ims, ms in enumerate(self.ListMS):
msweights = self._weight_dict[ims]
for ichunk in range(len(ms.getChunkRow0Row1())):
msw = msweights[ichunk]
# delete to save memory
if self.Weighting != "natural":
for field in "weight", "uv", "flags", "index":
if field in msw:
msw.delete_item(field)

# mark caches as valid
for ims, ms in enumerate(self.ListMS):
for ichunk, (row0, row1) in enumerate(ms.getChunkRow0Row1()):
Expand Down
6 changes: 3 additions & 3 deletions DDFacet/Imager/ClassDeconvMachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ def GiveDirty(self, psf=False, sparsify=0, last_cycle=False):
else:
print(ModColor.Str("============================ Loading cached dirty image ========================="), file=log)
print("found valid cached residual image in %s" % dirty_cachepath, file=log)
if type(self.GD["Cache"]["Dirty"]) is not str or not self.GD["Cache"]["Dirty"].startswith("force"):
if not isinstance(self.GD["Cache"]["Dirty"],str) or not self.GD["Cache"]["Dirty"].startswith("force"):
print(ModColor.Str("As near as we can tell, we can reuse this cache because it was produced"), file=log)
print(ModColor.Str("with the same set of relevant DDFacet settings. If you think this is in error,"), file=log)
print(ModColor.Str("or if your MS has changed, please remove the cache, or run with --Cache-Dirty reset."), file=log)
Expand Down Expand Up @@ -620,7 +620,7 @@ def GiveDirty(self, psf=False, sparsify=0, last_cycle=False):
if psf_valid:
print(ModColor.Str("============================ Loading cached PSF ================================="), file=log)
print("found valid cached PSF in %s" % psf_cachepath, file=log)
if type(self.GD["Cache"]["PSF"]) is not str or not self.GD["Cache"]["PSF"].startswith("force"):
if not isinstance(self.GD["Cache"]["PSF"],str) or not self.GD["Cache"]["PSF"].startswith("force"):
print(ModColor.Str("As near as we can tell, we can reuse this cache because it was produced"), file=log)
print(ModColor.Str("with the same set of relevant DDFacet settings. If you think this is in error,"), file=log)
print(ModColor.Str("or if your MS has changed, please remove the cache, or run with --Cache-PSF reset."), file=log)
Expand Down Expand Up @@ -896,7 +896,7 @@ def GivePredict(self, subtract=False, from_fits=True):

modelfile = self.GD["Predict"]["FromImage"]
# if model image is specified, we'll use that, rather than the ModelMachine
if modelfile is not None and modelfile is not "":
if modelfile:
print(ModColor.Str("Reading image file for the predict: %s" % modelfile), file=log)
FixedModelImage = ClassCasaImage.FileToArray(modelfile,True)
nch,npol,_,NPix=self.FacetMachine.OutImShape
Expand Down
2 changes: 1 addition & 1 deletion DDFacet/Imager/SSD/ClassArrayMethodSSD.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def SetDirtyArrays(self,Dirty):

def ToConvArray(self,V,OutMode="Data",Noise=False):
A=self.PM.GiveModelArray(V)
if Noise is not False:
if Noise:
A+=np.random.randn(*A.shape)*Noise
A=self.ConvMachine.Convolve(A,OutMode=OutMode)
return A
Expand Down
2 changes: 1 addition & 1 deletion SkyModel/MakeMask.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ def CreateMask(self):
self.MaskSelectedDS9()

ExternalAndMask=self.options.ExternalMask
if ExternalAndMask is not None and ExternalAndMask is not "":
if ExternalAndMask:
from DDFacet.Imager import ClassCasaImage
CleanMaskImageName=ExternalAndMask
print("Use mask image %s"%CleanMaskImageName, file=log)
Expand Down

0 comments on commit 5b31ee7

Please sign in to comment.