Commits
38 38 | from casatasks import deconvolve, tclean, imstat |
39 39 | |
40 40 | ### |
41 41 | ### import check versions |
42 42 | ### |
43 43 | _GCV001 = True |
44 44 | _GCV002 = True |
45 45 | _GCV003 = True |
46 46 | _GCV004 = True |
47 47 | |
48 + | print("USING THIS GCLEAN") |
49 + | |
48 50 | |
49 51 | # from casatasks.private.imagerhelpers._gclean import gclean |
50 52 | class gclean: |
51 53 | '''gclean(...) creates a stream of convergence records which indicate |
52 54 | the convergence quaility of the tclean process. The initial record |
53 55 | describes the initial dirty image. |
54 56 | It is designed for use with the interactive clean GUI, but it could |
55 57 | be used independently. It can be used as a regular generator: |
56 58 | for rec in gclean( vis='refim_point_withline.ms', imagename='test', imsize=512, cell='12.0arcsec', |
57 59 | specmode='cube', interpolation='nearest', nchan=5, start='1.0GHz', width='0.2GHz', |
367 369 | return outrec |
368 370 | |
369 371 | def _update_peakres(self): |
370 372 | if self._deconvolver == 'mtmfs': |
371 373 | residname = self._imagename + '.residual.tt0' |
372 374 | else: |
373 375 | residname = self._imagename + '.residual' |
374 376 | |
375 377 | maskname = self._imagename + '.mask' |
376 378 | |
377 - | peakres = imstat(imagename=residname, mask=maskname)['max'][0] |
378 - | return peakres |
379 + | peakres = imstat(imagename=residname, mask=maskname)['max'] |
380 + | masksum = imstat(imagename=maskname)['sum'] |
381 + | |
382 + | if len(peakres) > 0: |
383 + | peakres = peakres[0] |
384 + | else: |
385 + | peakres = None |
386 + | |
387 + | if len(masksum) > 0: |
388 + | masksum = masksum[0] |
389 + | else: |
390 + | masksum = None |
391 + | |
392 + | return peakres, masksum |
379 393 | |
380 394 | def __next__( self ): |
381 395 | """ Runs tclean and returns the (stopcode, convergence result) when executed with the python builtin next() function. |
382 396 | |
383 397 | The returned convergence result is a nested dictionary: |
384 398 | { |
385 399 | channel id: { |
386 400 | stokes id: { |
387 401 | summary key: [values, one per minor cycle] |
388 402 | }, |
438 452 | |
439 453 | self.current_imdict.returndict['stopcode'] = self.hasit |
440 454 | self.current_imdict.returndict['stopDescription'] = self.stopdescription |
441 455 | self._major_done = 0 |
442 456 | else: |
443 457 | # Reset convergence every time, since we return control to the GUI after a single major cycle |
444 458 | self.current_imdict.returndict['iterdone'] = 0. |
445 459 | |
446 460 | # Mask can be updated here... |
447 461 | # Check for mask update - peakres + masksum |
448 - | _peakres = self._update_peakres() |
449 - | |
450 - | self.hasit, self.stopdescription = self.global_imdict.has_converged(self._niter, self._threshold, self._nmajor, peakres=_peakres) |
462 + | _peakres, _masksum = self._update_peakres() |
451 463 | |
452 - | #print("HASIT : ",self.hasit) |
453 - | #print("DESC : ",self.stopdescription) |
464 + | self.hasit, self.stopdescription = self.global_imdict.has_converged(self._niter, self._threshold, self._nmajor, masksum=_masksum, peakres=_peakres) |
454 465 | |
455 466 | #self.global_imdict.returndict['stopcode'] = self.hasit |
456 467 | #self.global_imdict.returndict['stopDescription'] = self.stopdescription |
457 468 | |
458 469 | #self.current_imdict.returndict['stopcode'] = self.hasit |
459 470 | #self.current_imdict.returndict['stopDescription'] = self.stopdescription |
460 471 | |
472 + | # Has not, i.e., not converged |
461 473 | if self.hasit ==0 : |
462 474 | use_cycleniter, cyclethreshold = self._calc_deconv_controls(self.current_imdict, self._niter, self._threshold, self._cycleniter) |
463 475 | |
476 + | print("No convergence, running deconvolve + tclean") |
464 477 | # Run the minor cycle |
465 478 | deconv_ret = self._deconvolve(imagename=self._imagename, startmodel=self._startmodel, |
466 479 | deconvolver=self._deconvolver, restoration=False, |
467 480 | threshold=cyclethreshold, niter=use_cycleniter, gain=self._gain, usemask=self._usemask, |
468 481 | nsigma=self._nsigma, fullsummary=True, fastnoise=self._fastnoise, noisethreshold=self._noisethreshold) |
469 482 | |
470 - | if deconv_ret['stopcode'] != 7: ## If zero mask, then deconvolution would have done zero iterations -> no need for major cycle |
471 - | |
472 - | # Run the major cycle |
473 - | tclean_ret = self._tclean( vis=self._vis, imagename=self._imagename, imsize=self._imsize, cell=self._cell, |
474 - | phasecenter=self._phasecenter, stokes=self._stokes, specmode=self._specmode, reffreq=self._reffreq, |
475 - | gridder=self._gridder, wprojplanes=self._wprojplanes, mosweight=self._mosweight, psterm=self._psterm, |
476 - | wbawp=self._wbawp, conjbeams=self._conjbeams, usepointing=self._usepointing, interpolation=self._interpolation, |
477 - | perchanweightdensity=self._perchanweightdensity, nchan=self._nchan, start=self._start, |
478 - | width=self._width, veltype=self._veltype, restfreq=self._restfreq, outframe=self._outframe, |
479 - | pointingoffsetsigdev=self._pointingoffsetsigdev, pblimit=self._pblimit, deconvolver=self._deconvolver, |
480 - | smallscalebias=self._smallscalebias, cyclefactor=self._cyclefactor, scales=self._scales, |
481 - | restoringbeam=self._restoringbeam, pbcor=self._pbcor, nterms=self._nterms, field=self._field, |
482 - | spw=self._spw, timerange=self._timerange, uvrange=self._uvrange, antenna=self._antenna, |
483 - | scan=self._scan, observation=self._observation, intent=self._intent, datacolumn=self._datacolumn, |
484 - | weighting=self._weighting, robust=self._robust, npixels=self._npixels, interactive=False, |
485 - | niter=0, restart=True, calcpsf=False, calcres=True, restoration=False, threshold=self._threshold, |
486 - | nsigma=self._nsigma, cycleniter=self._cycleniter, nmajor=1, gain=self._gain, |
487 - | sidelobethreshold=self._sidelobethreshold, noisethreshold=self._noisethreshold, |
488 - | lownoisethreshold=self._lownoisethreshold, negativethreshold=self._negativethreshold, |
489 - | minbeamfrac=self._minbeamfrac, growiterations=self._growiterations, dogrowprune=self._dogrowprune, |
490 - | minpercentchange=self._minpercentchange, fastnoise=self._fastnoise, savemodel=self._savemodel, |
491 - | maxpsffraction=self._maxpsffraction, |
492 - | minpsffraction=self._minpsffraction, parallel=self._parallel, fullsummary=True ) |
493 - | |
494 - | # Replace return dict with new return dict |
495 - | # The order of the dicts into merge is important. |
496 - | self.current_imdict.returndict = self.current_imdict.merge(tclean_ret, deconv_ret) |
497 - | |
498 - | # Append new return dict to global return dict |
499 - | self.global_imdict.returndict = self.global_imdict.concat(self.global_imdict.returndict, self.current_imdict.returndict) |
500 - | self._major_done = self.current_imdict.returndict['nmajordone'] |
501 - | |
502 - | ## Decrement count for the major cycle just done... |
503 - | self.__decrement_counts() |
504 - | |
505 - | #else: |
506 - | # print("NO DECONVOLUTION ITERATIONS because of zero mask. Skipped major cycle and return dictionary update.\n") |
507 - | |
483 + | # Run the major cycle |
484 + | tclean_ret = self._tclean( vis=self._vis, imagename=self._imagename, imsize=self._imsize, cell=self._cell, |
485 + | phasecenter=self._phasecenter, stokes=self._stokes, specmode=self._specmode, reffreq=self._reffreq, |
486 + | gridder=self._gridder, wprojplanes=self._wprojplanes, mosweight=self._mosweight, psterm=self._psterm, |
487 + | wbawp=self._wbawp, conjbeams=self._conjbeams, usepointing=self._usepointing, interpolation=self._interpolation, |
488 + | perchanweightdensity=self._perchanweightdensity, nchan=self._nchan, start=self._start, |
489 + | width=self._width, veltype=self._veltype, restfreq=self._restfreq, outframe=self._outframe, |
490 + | pointingoffsetsigdev=self._pointingoffsetsigdev, pblimit=self._pblimit, deconvolver=self._deconvolver, |
491 + | smallscalebias=self._smallscalebias, cyclefactor=self._cyclefactor, scales=self._scales, |
492 + | restoringbeam=self._restoringbeam, pbcor=self._pbcor, nterms=self._nterms, field=self._field, |
493 + | spw=self._spw, timerange=self._timerange, uvrange=self._uvrange, antenna=self._antenna, |
494 + | scan=self._scan, observation=self._observation, intent=self._intent, datacolumn=self._datacolumn, |
495 + | weighting=self._weighting, robust=self._robust, npixels=self._npixels, interactive=False, |
496 + | niter=0, restart=True, calcpsf=False, calcres=True, restoration=False, threshold=self._threshold, |
497 + | nsigma=self._nsigma, cycleniter=self._cycleniter, nmajor=1, gain=self._gain, |
498 + | sidelobethreshold=self._sidelobethreshold, noisethreshold=self._noisethreshold, |
499 + | lownoisethreshold=self._lownoisethreshold, negativethreshold=self._negativethreshold, |
500 + | minbeamfrac=self._minbeamfrac, growiterations=self._growiterations, dogrowprune=self._dogrowprune, |
501 + | minpercentchange=self._minpercentchange, fastnoise=self._fastnoise, savemodel=self._savemodel, |
502 + | maxpsffraction=self._maxpsffraction, |
503 + | minpsffraction=self._minpsffraction, parallel=self._parallel, fullsummary=True ) |
504 + | |
505 + | # Replace return dict with new return dict |
506 + | # The order of the dicts into merge is important. |
507 + | self.current_imdict.returndict = self.current_imdict.merge(tclean_ret, deconv_ret) |
508 + | |
509 + | # Append new return dict to global return dict |
510 + | self.global_imdict.returndict = self.global_imdict.concat(self.global_imdict.returndict, self.current_imdict.returndict) |
511 + | self._major_done = self.current_imdict.returndict['nmajordone'] |
512 + | |
513 + | ## Decrement count for the major cycle just done... |
514 + | self.__decrement_counts() |
508 515 | |
509 516 | # Use global imdict for convergence check |
510 517 | if deconv_ret['stopcode'] == 7: ## Tell the convergence checker that the mask is zero and iterations were skipped |
511 518 | self.hasit, self.stopdescription = self.global_imdict.has_converged(self._niter, self._threshold, self._nmajor, masksum=0) |
512 519 | else: |
513 520 | self.hasit, self.stopdescription = self.global_imdict.has_converged(self._niter, self._threshold, self._nmajor) |
514 521 | |
515 522 | |
516 523 | self.global_imdict.returndict['stopcode'] = self.hasit |
517 524 | self.global_imdict.returndict['stopDescription'] = self.stopdescription |
601 608 | self._finalized = False |
602 609 | self._convergence_result = ( None, |
603 610 | self._convergence_result[1], |
604 611 | self._major_done, |
605 612 | self._nmajor, |
606 613 | self._niter, |
607 614 | self._convergence_result[5] ) |
608 615 | |
609 616 | def restore(self): |
610 617 | """ Restores the final image, and returns a path to the restored image. """ |
611 - | deconv_ret = self._deconvolve(imagename=self._imagename, startmodel=self._startmodel, |
612 - | deconvolver=self._deconvolver, restoration=True, |
613 - | threshold=self._threshold, niter=0, gain=self._gain, |
614 - | nsigma=self._nsigma, fullsummary=True, fastnoise=self._fastnoise, usemask=self._usemask, |
615 - | noisethreshold=self._noisethreshold) |
618 + | deconv_ret = self._deconvolve(imagename=self._imagename, |
619 + | deconvolver=self._deconvolver, |
620 + | restoration=True, niter=0, |
621 + | fullsummary=True) |
616 622 | |
617 623 | return { "image": f"{self._imagename}.image" } |
618 624 | |
619 625 | def has_next(self): |
620 626 | return not self._finalized |