cubevis 0.5.18__py3-none-any.whl → 0.5.20__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of cubevis might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  ########################################################################
2
2
  #
3
- # Copyright (C) 2022,2023,2024
3
+ # Copyright (C) 2022,2023,2024,2025
4
4
  # Associated Universities, Inc. Washington DC, USA.
5
5
  #
6
6
  # This script is free software; you can redistribute it and/or modify it
@@ -28,49 +28,15 @@
28
28
  '''implementation of the ``InteractiveClean`` application for interactive control
29
29
  of tclean'''
30
30
 
31
- ###
32
- ### Useful for debugging
33
- ###
34
- ###from cubevis.bokeh.state import initialize_bokeh
35
- ###initialize_bokeh( bokehjs_subst=".../bokeh-3.2.2.js" )
36
- ###
37
- import os
38
- import copy
39
- import asyncio
40
- import shutil
41
- import websockets
42
- from os.path import basename, abspath, exists
43
- from uuid import uuid4
44
- from html import escape as html_escape
45
- from contextlib import asynccontextmanager
46
- from bokeh.models import Button, TextInput, Checkbox, Div, LinearAxis, CustomJS, Spacer, Span, HoverTool, DataRange1d, Step, InlineStyleSheet
47
- from bokeh.events import ModelEvent, MouseEnter
48
- from bokeh.models import TabPanel, Tabs
49
- from bokeh.plotting import ColumnDataSource, figure, show
50
- from bokeh.layouts import column, row, layout
51
- from bokeh.io import reset_output as reset_bokeh_output, output_notebook
52
- from bokeh.models.dom import HTML
53
-
54
- from bokeh.models.ui.tooltips import Tooltip
55
- from cubevis.bokeh.models import TipButton, Tip, EvTextInput
56
- from cubevis.utils import resource_manager, reset_resource_manager, is_notebook, find_pkg, load_pkg
57
- from cubevis.utils import ContextMgrChain as CMC
58
-
59
- # pylint: disable=no-name-in-module
60
- from casatasks.private.imagerhelpers.imager_return_dict import ImagingDict
31
+ from pprint import pprint
61
32
 
33
+ import sys
34
+ from os.path import exists
62
35
  from casatasks.private.imagerhelpers.input_parameters import ImagerParameters
63
- # pylint: enable=no-name-in-module
64
36
 
65
- from cubevis.utils import find_ws_address, convert_masks
66
- from cubevis.toolbox import CubeMask, AppContext
67
- from cubevis.bokeh.utils import svg_icon
68
- from cubevis.bokeh.sources import DataPipe
69
- from cubevis.utils import DocEnum
70
-
71
- from ._interactiveclean_wrappers import SharedWidgets
72
-
73
- USE_MULTIPLE_GCLEAN_HACK=False
37
+ from cubevis.utils import find_pkg, load_pkg
38
+ from cubevis.toolbox import InteractiveCleanUI
39
+ from cubevis import exe
74
40
 
75
41
  class InteractiveClean:
76
42
  '''InteractiveClean(...) implements interactive clean using Bokeh
@@ -1839,83 +1805,10 @@ class InteractiveClean:
1839
1805
 
1840
1806
 
1841
1807
  '''
1842
- def __stop( self, _=None ):
1843
- self.__result_future.set_result(self.__retrieve_result( ))
1844
-
1845
- def _abort_handler( self, err ):
1846
- self._error_result = err
1847
- self.__stop( )
1848
-
1849
- def __reset( self ):
1850
- if self.__pipes_initialized:
1851
- self._pipe = { 'control': None }
1852
- self._clean = { 'converge': { 'state': { } }, 'last-success': None }
1853
- reset_bokeh_output( )
1854
- reset_resource_manager( )
1855
-
1856
- ###
1857
- ### reset asyncio result future
1858
- ###
1859
- self.__result_future = None
1860
-
1861
- ###
1862
- ### used by data pipe (websocket) initialization function
1863
- ###
1864
- self.__pipes_initialized = False
1865
-
1866
- ###
1867
- ### error or exception result
1868
- ###
1869
- self._error_result = None
1870
-
1871
- '''
1872
- _gen_port_fwd_cmd()
1873
-
1874
- Create an SSH port-forwarding command to create the tunnels necessary for remote connection.
1875
- NOTE: This assumes that the same remote ports are also available locally - which may
1876
- NOT always be true.
1877
- '''
1878
- def _gen_port_fwd_cmd(self):
1879
- hostname = os.uname()[1]
1880
-
1881
- ###
1882
- ### need to add extra cube ports here for multifield imaging
1883
- ###
1884
- ports = [ self._pipe['control'].address[1], self._clean['converge']['pipe'].address[1] ]
1885
-
1886
- for imid, imdetails in self._clean_targets.items( ):
1887
- ports.append( imdetails['gui']['cube']._pipe['image'].address[1] )
1888
- ports.append( imdetails['gui']['cube']._pipe['control'].address[1] )
1889
-
1890
- # Also forward http port if serving webpage
1891
- if not self._is_notebook:
1892
- ports.append(self._http_port)
1893
-
1894
- cmd = 'ssh'
1895
- for port in ports:
1896
- cmd += (' -L ' + str(port) + ':localhost:' + str(port))
1897
-
1898
- cmd += ' ' + str(hostname)
1899
- return cmd
1900
-
1901
- def _residual_path( self, gclean, imid ):
1902
- if self._clean['gclean_paths'] is None:
1903
- raise RuntimeError( f'''gclean paths are not available for {imid}''' )
1904
- for p in self._clean['gclean_paths']:
1905
- if p['name'] == imid:
1906
- return f"{p['imagepath']}/{p['residualname']}"
1907
- raise RuntimeError( f'''gclean residual path not found for {imid}''' )
1908
-
1909
- def _mask_path( self, gclean, imid ):
1910
- if self._clean['gclean_paths'] is None:
1911
- raise RuntimeError( f'''gclean paths are not available for {imid}''' )
1912
- for p in self._clean['gclean_paths']:
1913
- if p['name'] == imid:
1914
- return f"{p['imagepath']}/{p['maskname']}"
1915
- raise RuntimeError( f'''gclean mask path not found for {imid}''' )
1916
1808
 
1917
1809
  def __init__( self, vis, imagename, selectdata=True, field='', spw='', timerange='', uvrange='', antenna='', scan='', observation='', intent='', datacolumn='corrected', imsize=[ int(100) ], cell=[ ], phasecenter='', stokes='I', projection='SIN', startmodel='', specmode='mfs', reffreq='', nchan=int(-1), start='', width='', outframe='LSRK', veltype='radio', restfreq=[ ], interpolation='linear', perchanweightdensity=True, gridder='standard', facets=int(1), psfphasecenter='', wprojplanes=int(1), vptable='', mosweight=True, aterm=True, psterm=False, wbawp=True, conjbeams=False, cfcache='', usepointing=False, computepastep=float(360.0), rotatepastep=float(360.0), pointingoffsetsigdev=[ ], pblimit=float(0.2), normtype='flatnoise', deconvolver='hogbom', scales=[ ], nterms=int(2), smallscalebias=float(0.0), fusedthreshold=float(0.0), largestscale=int(-1), restoration=True, restoringbeam=[ ], pbcor=False, outlierfile='', weighting='natural', robust=float(0.5), noise='1.0Jy', npixels=int(0), uvtaper=[ '' ], niter=int(0), gain=float(0.1), threshold=float(0.0), nsigma=float(0.0), cycleniter=int(-1), cyclefactor=float(1.0), minpsffraction=float(0.05), maxpsffraction=float(0.8), nmajor=int(-1), usemask='user', mask='', pbmask=float(0.0), sidelobethreshold=float(3.0), noisethreshold=float(5.0), lownoisethreshold=float(1.5), negativethreshold=float(0.0), smoothfactor=float(1.0), minbeamfrac=float(0.3), cutthreshold=float(0.01), growiterations=int(75), dogrowprune=True, minpercentchange=float(-1.0), verbose=False, fastnoise=True, restart=True, savemodel='none', calcres=True, calcpsf=True, psfcutoff=float(0.35), parallel=False, iclean_backend="PROD" ):
1918
1810
 
1811
+
1919
1812
  ###
1920
1813
  ### iclean_backend can be used to select alternate backends for interactive clean. This could be used
1921
1814
  ### to enable a backend with extended features or it could be used to select a stub backend designed
@@ -1933,815 +1826,14 @@ class InteractiveClean:
1933
1826
  else:
1934
1827
  raise ImportError(f"Could not locate {iclean_backend} kind of iclean backend")
1935
1828
 
1936
- ###
1937
- ### With Bokeh 3.2.2, the spectrum and convergence plots extend beyond the edge of the
1938
- ### browser window (requiring scrolling) if a width is not specified. It could be that
1939
- ### this should be computed from the width of the tabbed control area at the right of
1940
- ### the image display.
1941
- ###
1942
- self._conv_spect_plot_width = 450
1943
- ###
1944
- ### Create application context (which includes a temporary directory).
1945
- ### This sets the title of the plot.
1946
- ###
1947
- self._app_state = AppContext( 'Interactive Clean' )
1948
-
1949
- ###
1950
- ### Whether or not the Interactive Clean session is running remotely
1951
- ###
1952
- #self._is_remote = remote
1953
- self._is_remote = False
1954
-
1955
- ###
1956
- ### whether or not the session is being run from a jupyter notebook or script
1957
- ###
1958
- self._is_notebook = is_notebook()
1959
-
1960
- ##
1961
- ## the http port for serving GUI in webpage if not running in script
1962
- ##
1963
- self._http_port = None
1964
-
1965
- ###
1966
- ### the asyncio future that is used to transmit the result from interactive clean
1967
- ###
1968
- self.__result_future = None
1969
-
1970
- ###
1971
- ### This is used to tell whether the websockets have been initialized, but also to
1972
- ### indicate if __call__ is being called multiple times to allow for resetting Bokeh
1973
- ###
1974
- self.__pipes_initialized = False
1975
-
1976
- ###
1977
- ### State required to manage iteration control
1978
- ###
1979
- self._control = { 'iteration': { } }
1980
-
1981
- ###
1982
- ### color specs
1983
- ###
1984
- self._converge_color = { 'residual': 'black',
1985
- 'flux': 'forestgreen' }
1986
-
1987
- ###
1988
- ### widgets shared across image tabs (multifield imaging)
1989
- ###
1990
- self._cube_palette = None
1991
- self._image_bitmask_controls = None
1992
-
1993
- ###
1994
- ### String which indicates the changes applied to the mask to indicte when
1995
- ### the mask has changed... however, THIS IS NO LONGER USED
1996
- ### It could be removed, but it adds minor overhead and would be
1997
- ### DIFFICULT to add back in the future
1998
- ###
1999
- self._last_mask_breadcrumbs = ''
2000
-
2001
- ###
2002
- ### Set up dictionary of javascript code snippets
2003
- ###
2004
- self._initialize_javascript( )
2005
-
2006
- # Create folder for the generated html webpage - needs its own folder to not name conflict (must be 'index.html')
2007
- webpage_dirname = imagename + '_webpage'
2008
- ### Directory is created when an HTTP server is running
2009
- ### (MAX)
2010
- # if not os.path.isdir(webpage_dirname):
2011
- # os.makedirs(webpage_dirname)
2012
- self._webpage_path = os.path.abspath(webpage_dirname)
2013
-
2014
- self._pipe = { 'control': None }
2015
- self._clean = { 'converge': { 'state': { } }, 'last-success': None }
2016
-
2017
- self._clean_targets = { imagename: { 'args': {'vis': vis, 'selectdata': selectdata, 'field': field, 'spw': spw, 'timerange': timerange, 'uvrange': uvrange, 'antenna': antenna, 'scan': scan, 'observation': observation, 'intent': intent, 'datacolumn': datacolumn, 'imagename': imagename, 'imsize': imsize, 'cell': cell, 'phasecenter': phasecenter, 'stokes': stokes, 'projection': projection, 'startmodel': startmodel, 'specmode': specmode, 'reffreq': reffreq, 'nchan': nchan, 'start': start, 'width': width, 'outframe': outframe, 'veltype': veltype, 'restfreq': restfreq, 'interpolation': interpolation, 'perchanweightdensity': perchanweightdensity, 'gridder': gridder, 'facets': facets, 'psfphasecenter': psfphasecenter, 'wprojplanes': wprojplanes, 'vptable': vptable, 'mosweight': mosweight, 'aterm': aterm, 'psterm': psterm, 'wbawp': wbawp, 'conjbeams': conjbeams, 'cfcache': cfcache, 'usepointing': usepointing, 'computepastep': computepastep, 'rotatepastep': rotatepastep, 'pointingoffsetsigdev': pointingoffsetsigdev, 'pblimit': pblimit, 'normtype': normtype, 'deconvolver': deconvolver, 'scales': scales, 'nterms': nterms, 'smallscalebias': smallscalebias, 'fusedthreshold': fusedthreshold, 'largestscale': largestscale, 'restoration': restoration, 'restoringbeam': restoringbeam, 'pbcor': pbcor, 'outlierfile': outlierfile, 'weighting': weighting, 'robust': robust, 'noise': noise, 'npixels': npixels, 'uvtaper': uvtaper, 'niter': niter, 'gain': gain, 'threshold': threshold, 'nsigma': nsigma, 'cycleniter': cycleniter, 'cyclefactor': cyclefactor, 'minpsffraction': minpsffraction, 'maxpsffraction': maxpsffraction, 'nmajor': nmajor, 'usemask': usemask, 'mask': mask, 'pbmask': pbmask, 'sidelobethreshold': sidelobethreshold, 'noisethreshold': noisethreshold, 'lownoisethreshold': lownoisethreshold, 'negativethreshold': negativethreshold, 'smoothfactor': smoothfactor, 'minbeamfrac': minbeamfrac, 'cutthreshold': cutthreshold, 'growiterations': growiterations, 'dogrowprune': dogrowprune, 'minpercentchange': minpercentchange, 'verbose': verbose, 'fastnoise': fastnoise, 'restart': restart, 'savemodel': savemodel, 'calcres': calcres, 'calcpsf': calcpsf, 'psfcutoff': psfcutoff, 'parallel': parallel} } }
2018
- self._initial_clean_params = { }
2019
-
2020
- self._parameters = ImagerParameters( **{ k:v for k,v in self._clean_targets[imagename]['args'].items( )
2021
- if k in ImagerParameters.__init__.__code__.co_varnames[1:] } )
2022
-
2023
- ###
2024
- ### For 6.6.5, outliers are not yet completely implemented so the
2025
- ### 'outlierfile' parameter may not be present.
2026
- ###
2027
- if 'outlierfile' in locals( ) and outlierfile and exists(outlierfile):
2028
- outliers, err = self._parameters.parseOutlierFile(outlierfile)
2029
- if err: raise RuntimeError( f'''outlierfile error: {err}''' )
2030
- for image in outliers:
2031
- if 'impars' in image and 'imagename' in image['impars']:
2032
- name = image['impars']['imagename']
2033
- self._clean_targets[name] = { }
2034
- args = {'vis': vis, 'selectdata': selectdata, 'field': field, 'spw': spw, 'timerange': timerange, 'uvrange': uvrange, 'antenna': antenna, 'scan': scan, 'observation': observation, 'intent': intent, 'datacolumn': datacolumn, 'imagename': imagename, 'imsize': imsize, 'cell': cell, 'phasecenter': phasecenter, 'stokes': stokes, 'projection': projection, 'startmodel': startmodel, 'specmode': specmode, 'reffreq': reffreq, 'nchan': nchan, 'start': start, 'width': width, 'outframe': outframe, 'veltype': veltype, 'restfreq': restfreq, 'interpolation': interpolation, 'perchanweightdensity': perchanweightdensity, 'gridder': gridder, 'facets': facets, 'psfphasecenter': psfphasecenter, 'wprojplanes': wprojplanes, 'vptable': vptable, 'mosweight': mosweight, 'aterm': aterm, 'psterm': psterm, 'wbawp': wbawp, 'conjbeams': conjbeams, 'cfcache': cfcache, 'usepointing': usepointing, 'computepastep': computepastep, 'rotatepastep': rotatepastep, 'pointingoffsetsigdev': pointingoffsetsigdev, 'pblimit': pblimit, 'normtype': normtype, 'deconvolver': deconvolver, 'scales': scales, 'nterms': nterms, 'smallscalebias': smallscalebias, 'fusedthreshold': fusedthreshold, 'largestscale': largestscale, 'restoration': restoration, 'restoringbeam': restoringbeam, 'pbcor': pbcor, 'outlierfile': outlierfile, 'weighting': weighting, 'robust': robust, 'noise': noise, 'npixels': npixels, 'uvtaper': uvtaper, 'niter': niter, 'gain': gain, 'threshold': threshold, 'nsigma': nsigma, 'cycleniter': cycleniter, 'cyclefactor': cyclefactor, 'minpsffraction': minpsffraction, 'maxpsffraction': maxpsffraction, 'nmajor': nmajor, 'usemask': usemask, 'mask': mask, 'pbmask': pbmask, 'sidelobethreshold': sidelobethreshold, 'noisethreshold': noisethreshold, 'lownoisethreshold': lownoisethreshold, 'negativethreshold': negativethreshold, 'smoothfactor': smoothfactor, 'minbeamfrac': minbeamfrac, 'cutthreshold': cutthreshold, 'growiterations': growiterations, 'dogrowprune': dogrowprune, 'minpercentchange': minpercentchange, 'verbose': verbose, 'fastnoise': fastnoise, 'restart': restart, 'savemodel': savemodel, 'calcres': calcres, 'calcpsf': calcpsf, 'psfcutoff': psfcutoff, 'parallel': parallel}
2035
- args['outlierfile'] = '' ### >>HERE>> outlierfile TEMPORARILY handled in _interactiveclean.py
2036
- for _,arg_mods in image.items( ):
2037
- for k,v in arg_mods.items( ):
2038
- if k in args:
2039
- args[k] = v
2040
- self._clean_targets[name]['args'] = args
2041
-
2042
- ###
2043
- ### create clean interface -- final version will have only one gclean object
2044
- ###
2045
- self._init_values = { "deconvolver": deconvolver, ### used by _residual_path( )
2046
- "cycleniter": cycleniter, ### used by _setup( )
2047
- "threshold": threshold, ### used by _setup( )
2048
- "cyclefactor": cyclefactor, ### used by _setup( )
2049
- "gain": gain, ### used by _setup( )
2050
- "nsigma": nsigma, ### used by _setup( )
2051
- "convergence_state": { 'convergence': {}, ### shares state between
2052
- 'cyclethreshold': {} } } ### __init__( ) and _setup( )
2053
-
2054
- self._clean['gclean'] = None
2055
- self._clean['gclean_paths'] = None
2056
- self._clean['imid'] = [ ]
2057
- self._clean['gclean_rest'] = [ ]
2058
- for imid, imdetails in self._clean_targets.items( ):
2059
- self._clean['imid'].append(imid)
2060
-
2061
- ###
2062
- ### Residual path...
2063
- ###
2064
- if 'path' not in imdetails: imdetails['path'] = { }
2065
- if self._clean['gclean'] is None:
2066
-
2067
- self._clean['gclean'] = self._gclean_module.gclean( **imdetails['args'] )
2068
- self._clean['gclean_paths'] = self._clean['gclean'].image_products( )
2069
-
2070
- imdetails['path']['residual'] = self._residual_path(self._clean['gclean'],imid)
2071
- imdetails['path']['mask'] = self._mask_path(self._clean['gclean'],imid)
2072
-
2073
- else:
2074
-
2075
- imdetails['path']['residual'] = self._residual_path(self._clean['gclean'],imid)
2076
- imdetails['path']['mask'] = self._mask_path(self._clean['gclean'],imid)
2077
-
2078
- for idx, (imid, imdetails) in enumerate(self._clean_targets.items( )):
2079
- imdetails['gui'] = { }
2080
-
2081
- imdetails['gui'] = { 'params': { 'iteration': { }, 'automask': { } },
2082
- 'image': { },
2083
- 'image-adjust': { } }
2084
-
2085
- ###
2086
- ### Only the first image should initialize the initial convergence state
2087
- ###
2088
- imdetails['gui']['cube'] = CubeMask( imdetails['path']['residual'], mask=imdetails['path']['mask'], abort=self._abort_handler,
2089
- init_script=CustomJS( args=dict( initial_convergence_state=self._init_values["convergence_state"],
2090
- name=imid ),
2091
- code='''document._casa_convergence_data = initial_convergence_state''' )
2092
- if idx == 0 else None )
2093
-
2094
- ###
2095
- ### Auto Masking Parameters
2096
- ###
2097
- imdetails['params'] = { }
2098
- imdetails['params']['am'] = { }
2099
- imdetails['params']['am']['usemask'] = usemask
2100
- imdetails['params']['am']['noisethreshold'] = noisethreshold
2101
- imdetails['params']['am']['sidelobethreshold'] = sidelobethreshold
2102
- imdetails['params']['am']['lownoisethreshold'] = lownoisethreshold
2103
- imdetails['params']['am']['minbeamfrac'] = minbeamfrac
2104
- imdetails['params']['am']['negativethreshold'] = negativethreshold
2105
- imdetails['params']['am']['dogrowprune'] = dogrowprune
2106
- imdetails['params']['am']['fastnoise'] = fastnoise
2107
-
2108
- def _init_pipes( self ):
2109
- if not self.__pipes_initialized:
2110
- self.__pipes_initialized = True
2111
- self._pipe['control'] = DataPipe( address=find_ws_address( ), abort=self._abort_handler )
2112
- ###
2113
- ### One pipe for updating the convergence plots.
2114
- ###
2115
- self._clean['converge'] = { 'state': None }
2116
- self._clean['converge']['pipe'] = DataPipe( address=find_ws_address( ), abort=self._abort_handler )
2117
- self._clean['converge']['id'] = str(uuid4( ))
2118
-
2119
-
2120
-
2121
- # Get port for serving HTTP server if running in script
2122
- self._http_port = find_ws_address("")[1]
2123
- for imid, imdetails in self._clean_targets.items( ):
2124
- imdetails['gui']['cube']._init_pipes( )
2125
-
2126
- def _create_convergence_gui( self, imdetails, orient='horizontal', sizing_mode='stretch_width', **kw ):
2127
- TOOLTIPS='''<div>
2128
- <div>
2129
- <span style="font-weight: bold;">@type</span>
2130
- <span>@values</span>
2131
- </div>
2132
- <div>
2133
- <span style="font-weight: bold; font-size: 10px">cycle threshold</span>
2134
- <span>@cyclethreshold</span>
2135
- </div>
2136
- <div>
2137
- <span style="font-weight: bold; font-size: 10px">stop</span>
2138
- <span>@stopDesc</span>
2139
- </div>
2140
- </div>'''
2141
-
2142
- hover = HoverTool( tooltips=TOOLTIPS )
2143
- imdetails['gui']['convergence'] = figure( sizing_mode=sizing_mode, y_axis_location="right",
2144
- tools=[ hover ], toolbar_location=None, **kw )
2145
-
2146
- if orient == 'vertical':
2147
- imdetails['gui']['convergence'].yaxis.axis_label='Iteration (cycle threshold dotted red)'
2148
- imdetails['gui']['convergence'].xaxis.axis_label='Peak Residual'
2149
- imdetails['gui']['convergence'].extra_x_ranges = { 'residual_range': DataRange1d( follow='end' ),
2150
- 'flux_range': DataRange1d( follow='end' ) }
2151
-
2152
- imdetails['gui']['convergence'].step( 'values', 'iterations', source=imdetails['converge-data']['cyclethreshold'],
2153
- line_color='red', x_range_name='residual_range', line_dash='dotted', line_width=2 )
2154
- imdetails['gui']['convergence'].line( 'values', 'iterations', source=imdetails['converge-data']['residual'],
2155
- line_color=self._converge_color['residual'], x_range_name='residual_range' )
2156
- imdetails['gui']['convergence'].scatter( 'values', 'iterations', source=imdetails['converge-data']['residual'],
2157
- color=self._converge_color['residual'], x_range_name='residual_range',size=10 )
2158
- imdetails['gui']['convergence'].line( 'values', 'iterations', source=imdetails['converge-data']['flux'],
2159
- line_color=self._converge_color['flux'], x_range_name='flux_range' )
2160
- imdetails['gui']['convergence'].scatter( 'values', 'iterations', source=imdetails['converge-data']['flux'],
2161
- color=self._converge_color['flux'], x_range_name='flux_range', size=10 )
2162
-
2163
- imdetails['gui']['convergence'].add_layout( LinearAxis( x_range_name='flux_range', axis_label='Total Flux',
2164
- axis_line_color=self._converge_color['flux'],
2165
- major_label_text_color=self._converge_color['flux'],
2166
- axis_label_text_color=self._converge_color['flux'],
2167
- major_tick_line_color=self._converge_color['flux'],
2168
- minor_tick_line_color=self._converge_color['flux'] ), 'above')
2169
-
2170
- else:
2171
- imdetails['gui']['convergence'].xaxis.axis_label='Iteration (cycle threshold dotted red)'
2172
- imdetails['gui']['convergence'].yaxis.axis_label='Peak Residual'
2173
- imdetails['gui']['convergence'].extra_y_ranges = { 'residual_range': DataRange1d( follow='end' ),
2174
- 'flux_range': DataRange1d( follow='end' ) }
2175
-
2176
- imdetails['gui']['convergence'].step( 'iterations', 'values', source=imdetails['converge-data']['cyclethreshold'],
2177
- line_color='red', y_range_name='residual_range', line_dash='dotted', line_width=2 )
2178
- imdetails['gui']['convergence'].line( 'iterations', 'values', source=imdetails['converge-data']['residual'],
2179
- line_color=self._converge_color['residual'], y_range_name='residual_range' )
2180
- imdetails['gui']['convergence'].scatter( 'iterations', 'values', source=imdetails['converge-data']['residual'],
2181
- color=self._converge_color['residual'], y_range_name='residual_range',size=10 )
2182
- imdetails['gui']['convergence'].line( 'iterations', 'values', source=imdetails['converge-data']['flux'],
2183
- line_color=self._converge_color['flux'], y_range_name='flux_range' )
2184
- imdetails['gui']['convergence'].scatter( 'iterations', 'values', source=imdetails['converge-data']['flux'],
2185
- color=self._converge_color['flux'], y_range_name='flux_range', size=10 )
2186
-
2187
- imdetails['gui']['convergence'].add_layout( LinearAxis( y_range_name='flux_range', axis_label='Total Flux',
2188
- axis_line_color=self._converge_color['flux'],
2189
- major_label_text_color=self._converge_color['flux'],
2190
- axis_label_text_color=self._converge_color['flux'],
2191
- major_tick_line_color=self._converge_color['flux'],
2192
- minor_tick_line_color=self._converge_color['flux'] ), 'right')
2193
-
2194
-
2195
- def _launch_gui( self ):
2196
- '''create and show GUI
2197
- '''
2198
- ###
2199
- ### Will contain the top level GUI
2200
- ###
2201
- self._fig = { }
2202
-
2203
- ###
2204
- ### Python-side handler for events from the interactive clean control buttons
2205
- ###
2206
- async def clean_handler( msg, self=self ):
2207
- if msg['action'] == 'next' or msg['action'] == 'finish':
2208
-
2209
- if 'mask' in msg['value']:
2210
- ###
2211
- ### >>HERE>> breadcrumbs must be specific to the field they are related to...
2212
- ###
2213
- if 'breadcrumbs' in msg['value'] and msg['value']['breadcrumbs'] is not None and msg['value']['breadcrumbs'] != self._last_mask_breadcrumbs:
2214
- self._last_mask_breadcrumbs = msg['value']['breadcrumbs']
2215
- mask_dir = "%s.mask" % self._imagename
2216
- shutil.rmtree(mask_dir)
2217
- new_mask = imdetails['gui']['cube'].jsmask_to_raw(msg['value']['mask'])
2218
- self._mask_history.append(new_mask)
2219
-
2220
- msg['value']['mask'] = convert_masks(masks=new_mask, coord='pixel', cdesc=imdetails['gui']['cube'].coorddesc())
2221
-
2222
- else:
2223
- ##### seemingly the mask path used to be spliced in?
2224
- #msg['value']['mask'] = self._mask_path
2225
- pass
2226
- else:
2227
- ##### seemingly the mask path used to be spliced in?
2228
- #msg['value']['mask'] = self._mask_path
2229
- pass
2230
-
2231
- ###
2232
- ### In the final implementation, there will only be one gclean object...
2233
- ###
2234
- convergence_state={ 'convergence': {}, 'cyclethreshold': {} }
2235
- err,errmsg = self._clean['gclean'].update( dict( **msg['value']['iteration'],
2236
- **msg['value']['automask'] ) )
2237
-
2238
- iteration_limit = int(msg['value']['iteration']['niter'])
2239
- stopdesc, stopcode, majordone, majorleft, iterleft, self._convergence_data = await self._clean['gclean'].__anext__( )
2240
-
2241
- clean_cmds = self._clean['gclean']._log( )
2242
-
2243
- for key, value in self._convergence_data.items( ):
2244
-
2245
- if len(value['chan']) == 0 or stopcode[0] == -1:
2246
- ### stopcode[0] == -1 indicates an error condition within gclean
2247
- return dict( result='error', stopcode=stopcode, cmd=clean_cmds,
2248
- convergence=None, majordone=majordone,
2249
- majorleft=majorleft, iterleft=iterleft, stopdesc=stopdesc )
2250
-
2251
- convergence_state['convergence'][key] = value['chan']
2252
- convergence_state['cyclethreshold'][key] = value['major']['cyclethreshold']
2253
-
2254
- ### stopcode[0] != 0 indicates that some stopping criteria has been reached
2255
- ### this will also catch errors as well as convergence
2256
- ### (so 'converged' isn't quite right...)
2257
- self._clean['last-success'] = dict( result='converged' if stopcode[0] else 'update', stopcode=stopcode, cmd=clean_cmds,
2258
- convergence=convergence_state['convergence'],
2259
- iterdone=iteration_limit - iterleft, iterleft=iterleft,
2260
- majordone=majordone, majorleft=majorleft, cyclethreshold=convergence_state['cyclethreshold'], stopdesc=stopdesc )
2261
- return self._clean['last-success']
2262
-
2263
- elif msg['action'] == 'stop':
2264
- self.__stop( )
2265
- return dict( result='stopped', update=dict( ) )
2266
- elif msg['action'] == 'status':
2267
- return dict( result="ok", update=dict( ) )
2268
- else:
2269
- print( "got something else: '%s'" % msg['action'] )
2270
-
2271
- ###
2272
- ### set up websockets which will be used for control and convergence updates
2273
- ###
2274
- self._init_pipes( )
2275
-
2276
- ###
2277
- ### Setup id that will be used for messages from each button
2278
- ###
2279
- self._clean_ids = { }
2280
- for btn in "continue", 'finish', 'stop':
2281
- self._clean_ids[btn] = str(uuid4( ))
2282
- #print("%s: %s" % ( btn, self._clean_ids[btn] ) )
2283
- self._pipe['control'].register( self._clean_ids[btn], clean_handler )
2284
-
2285
-
2286
- ###
2287
- ### There is one set of tclean controls for all images/outlier/etc. because
2288
- ### in the final version gclean will handle the iterations for all fields...
2289
- ###
2290
- cwidth = 64
2291
- cheight = 40
2292
- self._control['iteration'] = { }
2293
- self._control['iteration']['continue'] = TipButton( max_width=cwidth, max_height=cheight, name='continue',
2294
- icon=svg_icon(icon_name="iclean-continue", size=18),
2295
- tooltip=Tooltip( content=HTML( '''Stop after <b>one major cycle</b> or when any stopping criteria is met.''' ), position='left') )
2296
- self._control['iteration']['finish'] = TipButton( max_width=cwidth, max_height=cheight, name='finish',
2297
- icon=svg_icon(icon_name="iclean-finish", size=18),
2298
- tooltip=Tooltip( content=HTML( '''<b>Continue</b> until some stopping criteria is met.''' ), position='left') )
2299
- self._control['iteration']['stop'] = TipButton( button_type="danger", max_width=cwidth, max_height=cheight, name='stop',
2300
- icon=svg_icon(icon_name="iclean-stop", size=18),
2301
- tooltip=Tooltip( content=HTML( '''<p>Clicking a <font color="red">red</font> stop button will cause this tab to close and control will return to Python.<p>Clicking an <font color="orange">orange</font> stop button will cause <tt>tclean</tt> to stop after the current major cycle.''' ), position='left' ) )
2302
-
2303
- ###
2304
- ### The single SHARED help button will be supplied by the first CubeMask...
2305
- ###
2306
- help_button = None
2307
- ###
2308
- ### First status line will be reused...
2309
- ###
2310
- status_line = None
2311
-
2312
- ###
2313
- ### Manage the widgets which are shared between tabs...
2314
- ###
2315
- icw = SharedWidgets( )
2316
- toolbars = [ ]
2317
- for imid, imdetails in self._clean_targets.items( ):
2318
- imdetails['gui']['stats'] = imdetails['gui']['cube'].statistics( )
2319
- imdetails['image-channels'] = imdetails['gui']['cube'].shape( )[3]
2320
-
2321
- status_line = imdetails['gui']['stopcode'] = imdetails['gui']['cube'].status_text( "<p>initial residual image</p>" if imdetails['image-channels'] > 1 else "<p>initial <b>single-channel</b> residual image</p>", width=230, reuse=status_line )
2322
-
2323
- ###
2324
- ### Retrieve convergence information
2325
- ###
2326
- def convergence_handler( msg, self=self, imid=imid ):
2327
- if msg['action'] == 'retrieve':
2328
- return { 'result': self._clean['last-success'] }
2329
- else:
2330
- return { 'result': None, 'error': 'unrecognized action' }
2331
-
2332
- self._clean['converge']['pipe'].register( self._clean['converge']['id'], convergence_handler )
2333
-
2334
- ###
2335
- ### Data source that will be used for updating the convergence plot
2336
- ###
2337
- stokes = 0
2338
- convergence = imdetails['converge']['chan'][0][stokes]
2339
- imdetails['converge-data'] = { }
2340
- imdetails['converge-data']['flux'] = ColumnDataSource( data=dict( values=convergence['modelFlux'], iterations=convergence['iterations'],
2341
- cyclethreshold=convergence['cycleThresh'],
2342
- stopDesc=list( map( ImagingDict.get_summaryminor_stopdesc, convergence['stopCode'] ) ),
2343
- type=['flux'] * len(convergence['iterations']) ) )
2344
- imdetails['converge-data']['residual'] = ColumnDataSource( data=dict( values=convergence['peakRes'], iterations=convergence['iterations'],
2345
- cyclethreshold=convergence['cycleThresh'],
2346
- stopDesc=list( map( ImagingDict.get_summaryminor_stopdesc, convergence['stopCode'] ) ),
2347
- type=['residual'] * len(convergence['iterations'])) )
2348
- imdetails['converge-data']['cyclethreshold'] = ColumnDataSource( data=dict( values=convergence['cycleThresh'], iterations=convergence['iterations'] ) )
2349
-
2350
-
2351
- ###
2352
- ### help page for cube interactions
2353
- ###
2354
- if help_button is None:
2355
- help_button = imdetails['gui']['cube'].help( rows=[ '<tr><td><i><b>red</b> stop button</i></td><td>clicking the stop button (when red) will close the dialog and control to python</td></tr>',
2356
- '<tr><td><i><b>orange</b> stop button</i></td><td>clicking the stop button (when orange) will return control to the GUI after the currently executing tclean run completes</td></tr>' ], position='right' )
2357
-
2358
- self._create_convergence_gui( imdetails, orient='horizontal', sizing_mode='stretch_height', width=self._conv_spect_plot_width )
2359
-
2360
- imdetails['gui']['params']['iteration']['nmajor'] = icw.nmajor( title='nmajor', value="%s" % self._initial_clean_params['nmajor'], width=90 )
2361
- imdetails['gui']['params']['iteration']['niter'] = icw.niter( title='niter', value="%s" % self._initial_clean_params['niter'], width=90 )
2362
- imdetails['gui']['params']['iteration']['cycleniter'] = icw.cycleniter( title="cycleniter", value="%s" % self._initial_clean_params['cycleniter'], width=90 )
2363
- imdetails['gui']['params']['iteration']['threshold'] = icw.threshold( title="threshold", value="%s" % self._initial_clean_params['threshold'], width=90 )
2364
- imdetails['gui']['params']['iteration']['cyclefactor'] = icw.cyclefactor( value="%s" % self._initial_clean_params['cyclefactor'], title="cyclefactor", width=90 )
2365
- imdetails['gui']['params']['iteration']['gain'] = icw.gain( title='gain', value="%s" % self._initial_clean_params['gain'], width=90 )
2366
- imdetails['gui']['params']['iteration']['nsigma'] = icw.nsigma( title='nsigma', value="%s" % self._initial_clean_params['nsigma'], width=90 )
2367
-
2368
- if imdetails['params']['am']['usemask'] == 'auto-multithresh':
2369
- ###
2370
- ### Currently automasking tab is only available when the user selects 'auto-multithresh'
2371
- ###
2372
- imdetails['gui']['params']['automask']['active'] = True
2373
- imdetails['gui']['params']['automask']['noisethreshold'] = icw.noisethreshold( title='noisethreshold', value="%s" % imdetails['params']['am']['noisethreshold'], margin=( 5, 25, 5, 5 ), width=90 )
2374
- imdetails['gui']['params']['automask']['sidelobethreshold'] = icw.sidelobethreshold( title='sidelobethreshold', value="%s" % imdetails['params']['am']['sidelobethreshold'], margin=( 5, 25, 5, 5 ), width=90 )
2375
- imdetails['gui']['params']['automask']['lownoisethreshold'] = icw.lownoisethreshold( title='lownoisethreshold', value="%s" % imdetails['params']['am']['lownoisethreshold'], margin=( 5, 25, 5, 5 ), width=90 )
2376
- imdetails['gui']['params']['automask']['minbeamfrac'] = icw.minbeamfrac( title='minbeamfrac', value="%s" % imdetails['params']['am']['minbeamfrac'], width=90 )
2377
- imdetails['gui']['params']['automask']['negativethreshold'] = icw.negativethreshold( title='negativethreshold', value="%s" % imdetails['params']['am']['negativethreshold'], margin=( 5, 25, 5, 5 ), width=90 )
2378
- imdetails['gui']['params']['automask']['dogrowprune'] = icw.dogrowprune( label='dogrowprune', active=imdetails['params']['am']['dogrowprune'], margin=( 15, 25, 5, 5 ) )
2379
- imdetails['gui']['params']['automask']['fastnoise'] = icw.fastnoise( label='fastnoise', active=imdetails['params']['am']['fastnoise'], margin=( 15, 25, 5, 5 ) )
2380
-
2381
-
2382
- imdetails['gui']['image']['src'] = imdetails['gui']['cube'].js_obj( )
2383
- imdetails['gui']['image']['fig'] = imdetails['gui']['cube'].image( grid=False, height_policy='max', width_policy='max',
2384
- channelcb=CustomJS( args=dict( img_state={ 'src': imdetails['gui']['image']['src'],
2385
- 'flux': imdetails['converge-data']['flux'],
2386
- 'residual': imdetails['converge-data']['residual'],
2387
- 'cyclethreshold': imdetails['converge-data']['cyclethreshold'] },
2388
- imid=imid,
2389
- ctrl={ 'converge': self._clean['converge'] },
2390
- stopdescmap=ImagingDict.get_summaryminor_stopdesc( ) ),
2391
- code=self._js['update-converge'] +
2392
- '''update_convergence_single( img_state, document._casa_convergence_data.convergence[imid] )''' ) )
2393
-
2394
- ###
2395
- ### collect toolbars for syncing selection
2396
- ###
2397
- toolbars.append(imdetails['gui']['image']['fig'].toolbar)
2398
-
2399
- ###
2400
- ### spectrum plot must be disabled during iteration due to "tap to change channel" functionality
2401
- ###
2402
- if imdetails['image-channels'] > 1:
2403
- imdetails['gui']['spectrum'] = imdetails['gui']['cube'].spectrum( orient='vertical', sizing_mode='stretch_height', width=self._conv_spect_plot_width )
2404
- imdetails['gui']['slider'] = imdetails['gui']['cube'].slider( show_value=False, title='', margin=(14,5,5,5), sizing_mode="scale_width" )
2405
- imdetails['gui']['goto'] = imdetails['gui']['cube'].goto( )
2406
- else:
2407
- imdetails['gui']['spectrum'] = None
2408
- imdetails['gui']['slider'] = None
2409
- imdetails['gui']['goto'] = None
2410
-
2411
- imdetails['gui']['channel-ctrl'] = imdetails['gui']['cube'].channel_ctrl( )
2412
-
2413
- imdetails['gui']['cursor-pixel-text'] = imdetails['gui']['cube'].pixel_tracking_text( margin=(-3, 5, 3, 30) )
2414
-
2415
- self._image_bitmask_controls = imdetails['gui']['cube'].bitmask_ctrl( reuse=self._image_bitmask_controls, button_type='light' )
2416
-
2417
- if imdetails['params']['am']['usemask'] == 'auto-multithresh':
2418
- imdetails['gui']['auto-masking-panel'] = [ TabPanel( child=column( row( Tip( imdetails['gui']['params']['automask']['noisethreshold'],
2419
- tooltip=Tooltip( content=HTML( 'sets the signal-to-noise threshold above which significant emission is masked during the initial round of mask creation' ),
2420
- position='bottom' ) ),
2421
- Tip( imdetails['gui']['params']['automask']['sidelobethreshold'],
2422
- tooltip=Tooltip( content=HTML( 'sets a threshold based on the sidelobe level above which significant emission is masked during the initial round of mask creation' ),
2423
- position='bottom' ) ),
2424
- Tip( imdetails['gui']['params']['automask']['minbeamfrac'],
2425
- tooltip=Tooltip( content=HTML( 'sets the minimum size a region must be to be retained in the mask' ),
2426
- position='bottom' ) ) ),
2427
- row( Tip( imdetails['gui']['params']['automask']['lownoisethreshold'],
2428
- tooltip=Tooltip( content=HTML( 'sets the threshold into which the initial mask (which is determined by either noisethreshold or sidelobethreshold) is expanded in order to include low signal-to-noise regions in the mask' ),
2429
- position='bottom' ) ),
2430
- Tip( imdetails['gui']['params']['automask']['negativethreshold'],
2431
- tooltip=Tooltip( content=HTML( 'sets the signal-to-noise threshold for absorption features to be masked' ),
2432
- position='bottom' ) ) ),
2433
- row( Tip( imdetails['gui']['params']['automask']['dogrowprune'],
2434
- tooltip=Tooltip( content=HTML( 'allows you to turn off the pruning of the low signal-to-noise mask, which speeds up masking for images and cubes with complex low signal-to-noise emission' ),
2435
- position='bottom' ) ),
2436
- Tip( imdetails['gui']['params']['automask']['fastnoise'],
2437
- tooltip=Tooltip( content=HTML( 'When set to True, a simpler but faster noise calucation is used' ),
2438
- position='bottom' ) ) ) ),
2439
- title='Automask' ) ]
2440
- else:
2441
- imdetails['gui']['auto-masking-panel'] = [ ]
1829
+ self._args = {'vis': vis, 'selectdata': selectdata, 'field': field, 'spw': spw, 'timerange': timerange, 'uvrange': uvrange, 'antenna': antenna, 'scan': scan, 'observation': observation, 'intent': intent, 'datacolumn': datacolumn, 'imagename': imagename, 'imsize': imsize, 'cell': cell, 'phasecenter': phasecenter, 'stokes': stokes, 'projection': projection, 'startmodel': startmodel, 'specmode': specmode, 'reffreq': reffreq, 'nchan': nchan, 'start': start, 'width': width, 'outframe': outframe, 'veltype': veltype, 'restfreq': restfreq, 'interpolation': interpolation, 'perchanweightdensity': perchanweightdensity, 'gridder': gridder, 'facets': facets, 'psfphasecenter': psfphasecenter, 'wprojplanes': wprojplanes, 'vptable': vptable, 'mosweight': mosweight, 'aterm': aterm, 'psterm': psterm, 'wbawp': wbawp, 'conjbeams': conjbeams, 'cfcache': cfcache, 'usepointing': usepointing, 'computepastep': computepastep, 'rotatepastep': rotatepastep, 'pointingoffsetsigdev': pointingoffsetsigdev, 'pblimit': pblimit, 'normtype': normtype, 'deconvolver': deconvolver, 'scales': scales, 'nterms': nterms, 'smallscalebias': smallscalebias, 'fusedthreshold': fusedthreshold, 'largestscale': largestscale, 'restoration': restoration, 'restoringbeam': restoringbeam, 'pbcor': pbcor, 'outlierfile': outlierfile, 'weighting': weighting, 'robust': robust, 'noise': noise, 'npixels': npixels, 'uvtaper': uvtaper, 'niter': niter, 'gain': gain, 'threshold': threshold, 'nsigma': nsigma, 'cycleniter': cycleniter, 'cyclefactor': cyclefactor, 'minpsffraction': minpsffraction, 'maxpsffraction': maxpsffraction, 'nmajor': nmajor, 'usemask': usemask, 'mask': mask, 'pbmask': pbmask, 'sidelobethreshold': sidelobethreshold, 'noisethreshold': noisethreshold, 'lownoisethreshold': lownoisethreshold, 'negativethreshold': negativethreshold, 'smoothfactor': smoothfactor, 'minbeamfrac': minbeamfrac, 'cutthreshold': cutthreshold, 'growiterations': growiterations, 'dogrowprune': dogrowprune, 'minpercentchange': minpercentchange, 'verbose': verbose, 'fastnoise': fastnoise, 'restart': restart, 'savemodel': savemodel, 'calcres': calcres, 'calcpsf': calcpsf, 'psfcutoff': psfcutoff, 'parallel': parallel}
1830
+ self._gclean = self._gclean_module.gclean( **self._args )
1831
+ self._gclean_paths = self._gclean.image_products( )
1832
+ #self._residual_path = self._residual_path(self._clean['gclean'],imid)
1833
+ #self._mask_path = self._mask_path(self._clean['gclean'],imid)
2442
1834
 
1835
+ self._ui = InteractiveCleanUI(self._gclean, self._args)
2443
1836
 
2444
- ###
2445
- ### synchronize toolbar selections among figures
2446
- ###
2447
- if toolbars:
2448
- for tb in toolbars:
2449
- tb.js_on_change( 'active_changed',
2450
- ###
2451
- ### toolbars must filter out 'tb' to avoid circular references
2452
- ###
2453
- CustomJS( args=dict(toolbars=[t for t in toolbars if t.id != tb.id]),
2454
- code='''casalib.map( (gest,tool) => {
2455
- if ( tool.active ) {
2456
- // a tool which belongs to the toolbar that signaled a change
2457
- // is active for this gesture...
2458
- toolbars.forEach( (other_tb) => {
2459
- let new_active = other_tb.gestures[gest].tools.find(
2460
- (t) => t.name == tool.active.name )
2461
- if ( ! other_tb.gestures[gest].active ) {
2462
- if ( new_active ) {
2463
- other_tb.gestures[gest].active = new_active
2464
- new_active.active = true
2465
- }
2466
- } else if ( other_tb.gestures[gest].active.name != tool.active.name ) {
2467
- if ( new_active ) {
2468
- other_tb.gestures[gest].active.active = false
2469
- new_active.active = true
2470
- other_tb.gestures[gest].active = new_active
2471
- }
2472
- }
2473
- } )
2474
- } else {
2475
- // a tool which belongs to the toolbar that signaled a change
2476
- // is NOT active for this gesture...
2477
- toolbars.forEach( (other_tb) => {
2478
- if ( other_tb.gestures[gest] && other_tb.gestures[gest].active ) {
2479
- other_tb.gestures[gest].active.active = false
2480
- other_tb.gestures[gest].active = null
2481
- }
2482
- } )
2483
- }
2484
- }, cb_obj.gestures )''' ) )
2485
-
2486
- ###
2487
- ### button to display the tclean log -- in the final implmentation, outliers and other multifield imaging should be handled by gclean
2488
- ###
2489
- self._log_button = TipButton( max_width=help_button.width, max_height=help_button.height, name='log',
2490
- icon=svg_icon(icon_name="bp-application-sm", size=25),
2491
- tooltip=Tooltip( content=HTML('''click here to see the <pre>tclean</pre> execution log'''), position="right" ),
2492
- margin=(-1, 0, -10, 0), button_type='light',
2493
- stylesheets=[ InlineStyleSheet( css='''.bk-btn { border: 0px solid #ccc; padding: 0 var(--padding-vertical) var(--padding-horizontal); margin-top: 3px; }''' ) ] )
2494
-
2495
- self._control['iteration']['cb'] = CustomJS( args=dict( images_state={ k: { 'status': v['gui']['stopcode'],
2496
- 'automask': v['gui']['params']['automask'],
2497
- 'iteration': v['gui']['params']['iteration'],
2498
- 'img': v['gui']['image']['fig'],
2499
- 'src': v['gui']['cube'].js_obj( ),
2500
- 'spectrum': v['gui']['spectrum'],
2501
- 'src': v['gui']['image']['src'],
2502
- 'flux': v['converge-data']['flux'],
2503
- 'cyclethreshold': v['converge-data']['cyclethreshold'],
2504
- 'residual': v['converge-data']['residual'],
2505
- 'navi': { 'slider': v['gui']['slider'],
2506
- 'goto': v['gui']['goto'],
2507
- ## it doesn't seem like pixel tracking must be disabled
2508
- ##'tracking': v['gui']['cursor-pixel-text'],
2509
- 'stokes': v['gui']['channel-ctrl'][1] } }
2510
- for k,v in self._clean_targets.items( ) },
2511
- ctrl={ 'converge': self._clean['converge'] },
2512
- clean_ctrl=self._control['iteration'],
2513
- state=dict( mode='interactive', stopped=False, awaiting_stop=False, mask="" ),
2514
- ctrl_pipe=self._pipe['control'],
2515
- ids=self._clean_ids,
2516
- logbutton=self._log_button,
2517
- stopdescmap=ImagingDict.get_summaryminor_stopdesc( )
2518
- ),
2519
- code=self._js['update-converge'] + self._js['clean-refresh'] + self._js['clean-disable'] +
2520
- self._js['clean-enable'] + self._js['clean-status-update'] +
2521
- self._js['iter-gui-update'] + self._js['clean-wait'] +
2522
- '''function invalid_niter( s ) {
2523
- let v = parseInt( s )
2524
- if ( v > 0 ) return ''
2525
- if ( v == 0 ) return 'niter is zero'
2526
- if ( v < 0 ) return 'niter cannot be negative'
2527
- if ( isNaN(v) ) return 'niter must be an integer'
2528
- }
2529
- const itobj = Object.entries(images_state)[0][1].iteration
2530
- if ( ! state.stopped && cb_obj.origin.name == 'finish' ) {
2531
- let invalid = invalid_niter(itobj.niter.value)
2532
- if ( invalid ) update_status( invalid )
2533
- else {
2534
- state.mode = 'continuous'
2535
- update_status( 'Running multiple iterations' )
2536
- disable( false )
2537
- clean_ctrl.stop.button_type = "warning"
2538
- const thevalue = get_update_dictionary( )
2539
- ctrl_pipe.send( ids[cb_obj.origin.name],
2540
- { action: 'finish',
2541
- value: thevalue },
2542
- update_gui )
2543
- }
2544
- }
2545
- if ( ! state.stopped && state.mode === 'interactive' &&
2546
- cb_obj.origin.name === 'continue' ) {
2547
- let invalid = invalid_niter(itobj.niter.value)
2548
- if ( invalid ) update_status( invalid )
2549
- else {
2550
- update_status( 'Running one set of deconvolution iterations' )
2551
- disable( true )
2552
- // only send message for button that was pressed
2553
- // it's unclear whether 'this.origin.' or 'cb_obj.origin.' should be used
2554
- // (or even if 'XXX.origin.' is public)...
2555
- ctrl_pipe.send( ids[cb_obj.origin.name],
2556
- { action: 'next',
2557
- value: get_update_dictionary( ) },
2558
- update_gui )
2559
- }
2560
- }
2561
- if ( state.mode === 'interactive' && cb_obj.origin.name === 'stop' ) {
2562
- if ( confirm( "Are you sure you want to end this interactive clean session and close the GUI?" ) ) {
2563
- disable( true )
2564
- //ctrl_pipe.send( ids[cb_obj.origin.name],
2565
- // { action: 'stop',
2566
- // value: { } },
2567
- // update_gui )
2568
- document._casa_window_closed = true
2569
- /*** this will close the tab >>>>---------+ ***/
2570
- /*** | ***/
2571
- /*** vvvvv----------------------------+ ***/
2572
- document._cube_done( Object.entries(images_state).reduce((acc,[k,v]) => ({ ...acc, [k]: v.src.masks( ) }),{ } ) )
2573
- }
2574
- } else if ( state.mode === 'continuous' &&
2575
- cb_obj.origin.name === 'stop' &&
2576
- ! state.awaiting_stop ) {
2577
- disable( true )
2578
- state.awaiting_stop = true
2579
- ctrl_pipe.send( ids[cb_obj.origin.name],
2580
- { action: 'status',
2581
- value: { } },
2582
- wait_for_tclean_stop )
2583
- }''' )
2584
-
2585
-
2586
- self._control['iteration']['continue'].js_on_click( self._control['iteration']['cb'] )
2587
- self._control['iteration']['finish'].js_on_click( self._control['iteration']['cb'] )
2588
- self._control['iteration']['stop'].js_on_click( self._control['iteration']['cb'] )
2589
-
2590
- self._log_button.js_on_click( CustomJS( args=dict( logbutton=self._log_button ),
2591
- code='''function format_log( elem ) {
2592
- return `<html>
2593
- <head>
2594
- <style type="text/css">
2595
- body {
2596
- counter-reset: section;
2597
- }
2598
- p:not([no-num]):before {
2599
- font-weight: bold;
2600
- counter-increment: section;
2601
- content: "" counter(section) ": ";
2602
- }
2603
- </style>
2604
- </head>
2605
- <body>
2606
- <h1>Interactive Clean History</h1>
2607
- ` + elem.map((x) => x.startsWith('#') ? `<p no-num>${x}</p>` : `<p>${x}</p>`).join('\\n') + '</body>\\n</html>'
2608
- }
2609
- let b = cb_obj.origin
2610
- if ( ! b._window || b._window.closed ) {
2611
- b._window = window.open("about:blank","Interactive Clean Log")
2612
- b._window.document.write(format_log(b._log))
2613
- b._window.document.close( )
2614
- }''' ) )
2615
-
2616
- ###
2617
- ### Setup script that will be called when the user closes the
2618
- ### browser tab that is running interactive clean
2619
- ###
2620
- initial_log = self._clean['gclean']._log( )
2621
-
2622
- self._pipe['control'].init_script=CustomJS( args=dict( ctrl_pipe=self._pipe['control'],
2623
- ids=self._clean_ids,
2624
- logbutton=self._log_button,
2625
- log=initial_log,
2626
- initial_image=list(self._clean_targets.items( ))[0][0]
2627
- ),
2628
- code=self._js['initialize'] +
2629
- '''if ( ! logbutton._log ) {
2630
- /*** store log list with log button for access in other callbacks ***/
2631
- logbutton._log = log
2632
- }''' )
2633
-
2634
- tab_panels = list( map( self._create_image_panel, self._clean_targets.items( ) ) )
2635
-
2636
- for imid, imdetails in self._clean_targets.items( ):
2637
- imdetails['gui']['cube'].connect( )
2638
-
2639
- image_tabs = Tabs( tabs=tab_panels, tabs_location='below', height_policy='max', width_policy='max' )
2640
-
2641
- self._fig['layout'] = column(
2642
- row( help_button,
2643
- self._log_button,
2644
- Spacer( height=self._control['iteration']['stop'].height, sizing_mode="scale_width" ),
2645
- Div( text="<div><b>status:</b></div>" ),
2646
- status_line,
2647
- self._control['iteration']['stop'], self._control['iteration']['continue'], self._control['iteration']['finish'], sizing_mode="scale_width" ),
2648
- row( image_tabs, height_policy='max', width_policy='max' ),
2649
- height_policy='max', width_policy='max' )
2650
-
2651
- ###
2652
- ### Keep track of which image is currently active in document._casa_image_name (which is
2653
- ### initialized in self._js['initialize']). Also, update the current control sub-tab
2654
- ### when the field main-tab is changed. An attempt to manage this all within the
2655
- ### control sub-tabs using a reference to self._image_control_tab_groups from
2656
- ### each control sub-tab failed with:
2657
- ###
2658
- ### bokeh.core.serialization.SerializationError: circular reference
2659
- ###
2660
- image_tabs.js_on_change( 'active', CustomJS( args=dict( names=[ t[0] for t in self._clean_targets.items( ) ],
2661
- itergroups=self._image_control_tab_groups ),
2662
- code='''if ( ! hasprop(document,'_casa_last_control_tab') ) {
2663
- document._casa_last_control_tab = 0
2664
- }
2665
- document._casa_image_name = names[cb_obj.active]
2666
- itergroups[document._casa_image_name].active = document._casa_last_control_tab''' ) )
2667
-
2668
- # Change display type depending on runtime environment
2669
- if self._is_notebook:
2670
- output_notebook()
2671
- else:
2672
- ### Directory is created when an HTTP server is running
2673
- ### (MAX)
2674
- ### output_file(self._imagename+'_webpage/index.html')
2675
- pass
2676
-
2677
- show(self._fig['layout'])
2678
-
2679
- def _create_colormap_adjust( self, imdetails ):
2680
- palette = imdetails['gui']['cube'].palette( reuse=self._cube_palette )
2681
- return column( row( Div(text="<div><b>Colormap:</b></div>",margin=(5,2,5,25)), palette ),
2682
- imdetails['gui']['cube'].colormap_adjust( ), sizing_mode='stretch_both' )
2683
-
2684
-
2685
- def _create_control_image_tab( self, imid, imdetails ):
2686
- result = Tabs( tabs=[ TabPanel(child=column( row( Tip( imdetails['gui']['params']['iteration']['nmajor'],
2687
- tooltip=Tooltip( content=HTML( 'maximum number of major cycles to run before stopping'),
2688
- position='bottom' ) ),
2689
- Tip( imdetails['gui']['params']['iteration']['niter'],
2690
- tooltip=Tooltip( content=HTML( 'number of clean iterations to run' ),
2691
- position='bottom' ) ),
2692
- Tip( imdetails['gui']['params']['iteration']['threshold'],
2693
- tooltip=Tooltip( content=HTML( 'stopping threshold' ),
2694
- position='bottom' ) ) ),
2695
- row( Tip( imdetails['gui']['params']['iteration']['nsigma'],
2696
- tooltip=Tooltip( content=HTML( 'multiplicative factor for rms-based threshold stopping'),
2697
- position='bottom' ) ),
2698
- Tip( imdetails['gui']['params']['iteration']['gain'],
2699
- tooltip=Tooltip( content=HTML( 'fraction of the source flux to subtract out of the residual image'),
2700
- position='bottom' ) ) ),
2701
- row( Tip( imdetails['gui']['params']['iteration']['cycleniter'],
2702
- tooltip=Tooltip( content=HTML( 'maximum number of <b>minor-cycle</b> iterations' ),
2703
- position='bottom' ) ),
2704
- Tip( imdetails['gui']['params']['iteration']['cyclefactor'],
2705
- tooltip=Tooltip( content=HTML( 'scaling on PSF sidelobe level to compute the minor-cycle stopping threshold' ),
2706
- position='bottom_left' ) ), background="lightgray" ),
2707
- imdetails['gui']['convergence'], sizing_mode='stretch_height' ),
2708
- title='Iteration' ) ] +
2709
- ( [ TabPanel( child=imdetails['gui']['spectrum'],
2710
- title='Spectrum' ) ] if imdetails['image-channels'] > 1 else [ ] ) +
2711
- [ TabPanel( child=self._create_colormap_adjust(imdetails),
2712
- title='Colormap' ),
2713
- TabPanel( child=column( *imdetails['gui']['stats'] ),
2714
- title='Statistics' ) ] + imdetails['gui']['auto-masking-panel'],
2715
- width=500, sizing_mode='stretch_height', tabs_location='below' )
2716
-
2717
- if not hasattr(self,'_image_control_tab_groups'):
2718
- self._image_control_tab_groups = { }
2719
-
2720
- self._image_control_tab_groups[imid] = result
2721
- result.js_on_change( 'active', CustomJS( args=dict( ),
2722
- code='''document._casa_last_control_tab = cb_obj.active''' ) )
2723
- return result
2724
-
2725
- def _create_image_panel( self, imagetuple ):
2726
- imid, imdetails = imagetuple
2727
-
2728
-
2729
-
2730
- return TabPanel( child=column( row( *imdetails['gui']['channel-ctrl'], imdetails['gui']['cube'].coord_ctrl( ),
2731
- *self._image_bitmask_controls,
2732
- #Spacer( height=5, height_policy="fixed", sizing_mode="scale_width" ),
2733
- imdetails['gui']['cursor-pixel-text'],
2734
- row( Spacer( sizing_mode='stretch_width' ),
2735
- imdetails['gui']['cube'].tapedeck( size='20px' ) if imdetails['image-channels'] > 1 else Div( ),
2736
- Spacer( height=5, width=350 ), width_policy='max' ),
2737
- width_policy='max' ),
2738
- row( imdetails['gui']['image']['fig'],
2739
- column( row( imdetails['gui']['goto'],
2740
- imdetails['gui']['slider'],
2741
- width_policy='max' ) if imdetails['image-channels'] > 1 else Div( ),
2742
- self._create_control_image_tab(imid, imdetails), height_policy='max' ),
2743
- height_policy='max', width_policy='max' ),
2744
- height_policy='max', width_policy='max' ), title=imid )
2745
1837
 
2746
1838
  def __call__( self ):
2747
1839
  '''Display GUI and process events until the user stops the application.
@@ -2754,507 +1846,6 @@ class InteractiveClean:
2754
1846
  cell='12.0arcsec', specmode='cube',
2755
1847
  interpolation='nearest', ... )( ) )
2756
1848
  '''
2757
-
2758
- self._setup()
2759
-
2760
- # If Interactive Clean is being run remotely, print helper info for port tunneling
2761
- if self._is_remote:
2762
- # Tunnel ports for Jupyter kernel connection
2763
- print("\nImportant: Copy the following line and run in your local terminal to establish port forwarding.\
2764
- You may need to change the last argument to align with your ssh config.\n")
2765
- print(self._gen_port_fwd_cmd())
2766
-
2767
- # TODO: Include?
2768
- # VSCode will auto-forward ports that appear in well-formatted addresses.
2769
- # Printing this line will cause VSCode to autoforward the ports
2770
- # print("Cmd: " + str(repr(self.auto_fwd_ports_vscode())))
2771
- input("\nPress enter when port forwarding is setup...")
2772
-
2773
- async def _run_( ):
2774
- async with self.serve( ) as s:
2775
- await s
2776
-
2777
- if self._is_notebook:
2778
- ic_task = asyncio.create_task(_run_())
2779
- else:
2780
- asyncio.run(_run_( ))
2781
- return self.result( )
2782
-
2783
- def _setup( self ):
2784
- self.__reset( )
2785
-
2786
- def initialize_tclean( gclean ):
2787
-
2788
- stopdesc, stopcode, majordone, majorleft, iterleft, all_converge = next(gclean)
2789
-
2790
- for imid, converge in all_converge.items( ):
2791
- #######################################################################################################
2792
- ### gclean seems to return its internal state making it succeptable to modification... so we'll at ###
2793
- ### least start out with unique dictionaries. ###
2794
- #######################################################################################################
2795
- converge = copy.deepcopy(converge)
2796
-
2797
- imdetails = self._clean_targets[imid]
2798
- imdetails['converge'] = converge
2799
-
2800
- if imdetails['converge'] is None or len(imdetails['converge'].keys()) == 0 or \
2801
- imdetails['converge']['major'] is None or imdetails['converge']['chan'] is None:
2802
- ###
2803
- ### gclean should provide argument checking (https://github.com/casangi/casagui/issues/33)
2804
- ### but currently gclean can be initialized with bad arguments and it is not known until
2805
- ### the initial calls to tclean/deconvolve
2806
- ###
2807
- raise RuntimeError(f'''gclean failure "%s" not returned: {imdetails["converge"]}''' % ('major' if imdetails['converge']['major'] is None else 'chan'))
2808
-
2809
- self._clean['cmds'].extend(self._clean['gclean']._log( ))
2810
-
2811
- self._initial_clean_params['nmajor'] = majorleft
2812
- self._initial_clean_params['niter'] = iterleft
2813
- self._initial_clean_params['cycleniter'] = self._init_values["cycleniter"]
2814
- self._initial_clean_params['threshold'] = self._init_values["threshold"]
2815
- self._initial_clean_params['cyclefactor'] = self._init_values["cyclefactor"]
2816
- self._initial_clean_params['gain'] = self._init_values["gain"]
2817
- self._initial_clean_params['nsigma'] = self._init_values["nsigma"]
2818
-
2819
- self._init_values["convergence_state"]['convergence'][imid] = imdetails['converge']['chan']
2820
- self._init_values["convergence_state"]['cyclethreshold'][imid] = imdetails['converge']['major']['cyclethreshold']
2821
-
2822
- return (stopdesc, stopcode, majordone, majorleft, iterleft)
2823
-
2824
-
2825
- self._clean['cmds'] = []
2826
-
2827
- stopdesc, stopcode, majordone, majorleft, iterleft = initialize_tclean(self._clean['gclean'])
2828
-
2829
-
2830
- self._clean['last-success'] = dict( result='converged' if stopcode[0] else 'update', stopcode=stopcode, cmd=self._clean['cmds'],
2831
- convergence=self._init_values["convergence_state"]['convergence'],
2832
- iterdone=0, iterleft=iterleft,
2833
- majordone=majordone, majorleft=majorleft,
2834
- cyclethreshold=self._init_values["convergence_state"]['cyclethreshold'],
2835
- stopdesc=stopdesc )
2836
-
2837
- ### Must occur AFTER initial "next" call to gclean(s)
2838
- self._init_pipes()
2839
-
2840
- @asynccontextmanager
2841
- async def serve( self ):
2842
- '''This function is intended for developers who would like to embed interactive
2843
- clean as a part of a larger GUI. This embedded use of interactive clean is not
2844
- currently supported and would require the addition of parameters to this function
2845
- as well as changes to the interactive clean implementation. However, this function
2846
- does expose the ``asyncio.Future`` that is used to signal completion of the
2847
- interactive cleaning operation, and it provides the coroutines which must be
2848
- managed by asyncio to make the interactive clean GUI responsive.
2849
-
2850
- Example:
2851
- Create ``iclean`` object, process events and retrieve result::
2852
-
2853
- ic = iclean( vis='refim_point_withline.ms', imagename='test', imsize=512,
2854
- cell='12.0arcsec', specmode='cube', interpolation='nearest', ... )
2855
- async def process_events( ):
2856
- async with ic.serve( ) as state:
2857
- await state[0]
2858
-
2859
- asyncio.run(process_events( ))
2860
- print( "Result:", ic.result( ) )
2861
-
2862
-
2863
- Returns
2864
- -------
2865
- (asyncio.Future, dictionary of coroutines)
2866
- '''
2867
- def start_http_server():
2868
- import http.server
2869
- import socketserver
2870
- PORT = self._http_port
2871
- DIRECTORY=self._webpage_path
2872
-
2873
- class Handler(http.server.SimpleHTTPRequestHandler):
2874
- def __init__(self, *args, **kwargs):
2875
- super().__init__(*args, directory=DIRECTORY, **kwargs)
2876
-
2877
- with socketserver.TCPServer(("", PORT), Handler) as httpd:
2878
- print("\nServing Interactive Clean webpage from local directory: ", DIRECTORY)
2879
- print("Use Control-C to stop Interactive clean.\n")
2880
- print("Copy and paste one of the below URLs into your browser (Chrome or Firefox) to view:")
2881
- print("http://localhost:"+str(PORT))
2882
- print("http://127.0.0.1:"+str(PORT))
2883
-
2884
- httpd.serve_forever()
2885
-
2886
- self._launch_gui( )
2887
-
2888
- async with CMC( *( [ ctx for img in self._clean_targets.keys( ) for ctx in
2889
- [
2890
- self._clean_targets[img]['gui']['cube'].serve(self.__stop),
2891
- ]
2892
- ] + [ websockets.serve( self._pipe['control'].process_messages,
2893
- self._pipe['control'].address[0],
2894
- self._pipe['control'].address[1] ),
2895
- websockets.serve( self._clean['converge']['pipe'].process_messages,
2896
- self._clean['converge']['pipe'].address[0],
2897
- self._clean['converge']['pipe'].address[1] ) ]
2898
- ) ):
2899
- self.__result_future = asyncio.Future( )
2900
- yield self.__result_future
2901
-
2902
- def __retrieve_result( self ):
2903
- '''If InteractiveClean had a return value, it would be filled in as part of the
2904
- GUI dialog between Python and JavaScript and this function would return it'''
2905
- if isinstance(self._error_result,Exception):
2906
- raise self._error_result
2907
- elif self._error_result is not None:
2908
- return self._error_result
2909
- return { k: v['converge'] for k,v in self._clean_targets.items( ) }
2910
-
2911
- def result( self ):
2912
- '''If InteractiveClean had a return value, it would be filled in as part of the
2913
- GUI dialog between Python and JavaScript and this function would return it'''
2914
- if self.__result_future is None:
2915
- raise RuntimeError( 'no interactive clean result is available' )
2916
-
2917
- self._clean['gclean'].restore( )
2918
-
2919
- return self.__result_future.result( )
2920
-
2921
- def masks( self ):
2922
- '''Retrieves the masks which were used with interactive clean.
2923
-
2924
- Returns
2925
- -------
2926
- The standard ``cubevis`` cube region dictionary which contains two elements
2927
- ``masks`` and ``polys``.
2928
-
2929
- The value of the ``masks`` element is a dictionary that is indexed by
2930
- tuples of ``(stokes,chan)`` and the value of each element is a list
2931
- whose elements describe the polygons drawn on the channel represented
2932
- by ``(stokes,chan)``. Each polygon description in this list has a
2933
- polygon index (``p``) and a x/y translation (``d``).
2934
-
2935
- The value of the ``polys`` element is a dictionary that is indexed by
2936
- polygon indexes. The value of each polygon index is a dictionary containing
2937
- ``type`` (whose value is either ``'rect'`` or ``'poly``) and ``geometry``
2938
- (whose value is a dictionary containing ``'xs'`` and ``'ys'`` (which are
2939
- the x and y coordinates that define the polygon).
2940
-
2941
- This can be converted to other formats with ``cubevis.utils.convert_masks``.
2942
- '''
2943
- return copy.deepcopy(self._mask_history) ## don't allow users to change history
2944
-
2945
- def history( self ):
2946
- '''Retrieves the commands used during the interactive clean session.
2947
-
2948
- Returns
2949
- -------
2950
- list[str] tclean calls made during the interactive clean session.
2951
- '''
2952
- return self._clean['gclean']._log( True )
2953
-
2954
- def _initialize_javascript( self ):
2955
- self._js = { ### initialize state
2956
- ### --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
2957
- ### -- document is used storing state --
2958
- ### --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
2959
- 'initialize': '''if ( ! document._casa_initialized ) {
2960
- document._casa_image_name = initial_image
2961
- document._casa_initialized = true
2962
- document._casa_window_closed = false
2963
- window.addEventListener( 'beforeunload',
2964
- function (e) {
2965
- // if the window is already closed this message is never
2966
- // delivered (unless interactive clean is called again then
2967
- // the event shows up in the newly created control pipe
2968
- if ( document._casa_window_closed == false ) {
2969
- ctrl_pipe.send( ids['stop'],
2970
- { action: 'stop', value: { } },
2971
- undefined ) } } )
2972
- }''',
2973
-
2974
- ### --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
2975
- ### -- flux_src._convergence_data is used to store the complete --
2976
- ### -- --
2977
- ### -- The "Insert here ..." code seems to be called when when the stokes plane is changed --
2978
- ### -- but there have been no tclean iterations yet... --
2979
- ### --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
2980
- 'update-converge': '''function update_convergence_single( target, data ) {
2981
- const pos = target.src.cur_chan
2982
- const imdata = data.get(pos[1]).get(pos[0])
2983
- // chan----------------^^^^^^ ^^^^^^----stokes
2984
- const iterations = imdata.iterations
2985
- const peakRes = imdata.peakRes
2986
- const cyclethreshold = imdata.cycleThresh
2987
- const modelFlux = imdata.modelFlux
2988
- const stopCode = imdata.stopCode
2989
- const stopDesc = imdata.stopCode.map( code => stopdescmap.has(code) ? stopdescmap.get(code): "" )
2990
- target.residual.data = { iterations, cyclethreshold, stopDesc, values: peakRes, type: Array(iterations.length).fill('residual') }
2991
- target.flux.data = { iterations, cyclethreshold, stopDesc, values: modelFlux, type: Array(iterations.length).fill('flux') }
2992
- target.cyclethreshold.data = { iterations, values: cyclethreshold }
2993
- }
2994
-
2995
- function update_convergence( recurse=false ) {
2996
- let convdata
2997
- if ( hasprop(document,'_casa_convergence_data') ) {
2998
- convdata = document._casa_convergence_data
2999
- } else {
3000
- if ( ! recurse ) {
3001
- ctrl.converge.pipe.send( ctrl.converge.id, { action: 'retrieve' },
3002
- (msg) => { if ( hasprop( msg.result, 'convergence' ) ) {
3003
- document._casa_convergence_data = { convergence: msg.result.convergence,
3004
- cyclethreshold: msg.result.cyclethreshold }
3005
- update_convergence(true)
3006
- } } )
3007
- } else { console.log( 'INTERNAL ERROR: fetching convergence data failed' ) }
3008
- return
3009
- }
3010
-
3011
- Object.entries(images_state).map(
3012
- ([k,v],i) => { update_convergence_single(v,convdata.convergence[k]) } )
3013
- }''',
3014
-
3015
- 'clean-refresh': '''function refresh( clean_msg ) {
3016
- const itobj = Object.entries(images_state)[0][1].iteration
3017
- let stokes = 0 // later we will receive the polarity
3018
- // from some widget mechanism...
3019
- if ( clean_msg !== undefined ) {
3020
- if ( 'iterleft' in clean_msg ) {
3021
- itobj.niter.value = '' + clean_msg['iterleft']
3022
- } else if ( clean_msg !== undefined && 'iterdone' in clean_msg ) {
3023
- const remaining = parseInt(itobj.niter.value) - parseInt(clean_msg['iterdone'])
3024
- itobj.niter.value = '' + (remaining < 0 ? 0 : remaining)
3025
- }
3026
-
3027
- if ( 'majorleft' in clean_msg ) {
3028
- itobj.nmajor.value = '' + clean_msg['majorleft']
3029
- } else if ( 'majordone' in clean_msg ) {
3030
- const nm = parseInt(itobj.nmajor.value)
3031
- if ( nm != -1 ) {
3032
- const remaining = nm - parseInt(clean_msg['majordone'])
3033
- itobj.nmajor.value = '' + (remaining < 0 ? 0 : remaining)
3034
- } else itobj.nmajor.value = '' + nm // nmajor == -1 implies do not consider nmajor in stop decision
3035
- }
3036
-
3037
- if ( hasprop(clean_msg,'convergence') && clean_msg.convergence != null ) {
3038
- document._casa_convergence_data = { convergence: clean_msg.convergence,
3039
- cyclethreshold: clean_msg.cyclethreshold }
3040
- }
3041
- }
3042
-
3043
- // All images must be updated... without this no images are updated
3044
- casalib.map( (im,state) => state.src.refresh( msg => {
3045
- if ( 'stats' in msg ) state.src.update_statistics( msg.stats )
3046
- } ), images_state )
3047
- // Update convergence plot...
3048
- update_convergence( )
3049
- }''',
3050
-
3051
- ###
3052
- ### enabling/disabling tools in imdetails['gui']['image']['fig'].toolbar.tools does not seem to not work
3053
- ### imdetails['gui']['image']['fig'].toolbar.tools.tool_name (e.g. "Box Select", "Lasso Select")
3054
- ###
3055
- ### By design, images_state[*].automask.*/images_state[*].iteration.* are singletons which only need
3056
- ### to be disabled once...
3057
- ###
3058
- 'clean-disable': '''function disable( with_stop ) {
3059
- const amobj = Object.entries(images_state)[0][1].automask
3060
- Object.entries(amobj).map(
3061
- ([k,v],i) => { if ( hasprop(v,'disabled') ) v.disabled = true } )
3062
- const itobj = Object.entries(images_state)[0][1].iteration
3063
- Object.entries(itobj).map(
3064
- ([k,v],i) => { if ( hasprop(v,'disabled') ) v.disabled = true } )
3065
- Object.entries(images_state).map(
3066
- ([k,v],i) => {
3067
- v.img.disabled = true
3068
- if ( v.spectrum ) v.spectrum.disabled = true
3069
- v.src.disable_masking( )
3070
- v.src.disable_pixel_update( )
3071
- Object.entries(v.navi).map(
3072
- ([k1,v1],i1) => { if ( hasprop(v1,'disabled') ) v1.disabled = true }
3073
- )
3074
- }
3075
- )
3076
- clean_ctrl.continue.disabled = true
3077
- clean_ctrl.finish.disabled = true
3078
- clean_ctrl.stop.disabled = with_stop
3079
- }''',
3080
-
3081
- 'clean-enable': '''function enable( only_stop ) {
3082
- const amobj = Object.entries(images_state)[0][1].automask
3083
- Object.entries(amobj).map(
3084
- ([k,v],i) => { if ( hasprop(v,'disabled') ) v.disabled = false } )
3085
- const itobj = Object.entries(images_state)[0][1].iteration
3086
- Object.entries(itobj).map(
3087
- ([k,v],i) => { if ( hasprop(v,'disabled') ) v.disabled = false } )
3088
- Object.entries(images_state).map(
3089
- ([k,v],i) => {
3090
- v.img.disabled = false
3091
- if ( v.spectrum ) v.spectrum.disabled = false
3092
- v.src.enable_masking( )
3093
- v.src.enable_pixel_update( )
3094
- Object.entries(v.navi).map(
3095
- ([k1,v1],i) => { if ( hasprop(v1,'disabled') ) v1.disabled = false } )
3096
- } )
3097
-
3098
- clean_ctrl.stop.disabled = false
3099
- if ( ! only_stop ) {
3100
- clean_ctrl.continue.disabled = false
3101
- clean_ctrl.finish.disabled = false
3102
- }
3103
- }''',
3104
-
3105
-
3106
- 'clean-status-update': '''function update_status( status ) {
3107
- const stopstr = [ 'Zero stop code',
3108
- 'Iteration limit hit',
3109
- 'Force stop',
3110
- 'No change in peak residual across two major cycles',
3111
- 'Peak residual increased by 3x from last major cycle',
3112
- 'Peak residual increased by 3x from the minimum',
3113
- 'Zero mask found',
3114
- 'No mask found',
3115
- 'N-sigma or other valid exit criterion',
3116
- 'Stopping criteria encountered',
3117
- 'Unrecognized stop code' ]
3118
- if ( typeof status === 'number' ) {
3119
- images_state[document._casa_image_name]['status'].text =
3120
- '<p>' +
3121
- stopstr[ status < 0 || status >= stopstr.length ?
3122
- stopstr.length - 1 : status ] +
3123
- '</p>'
3124
- } else {
3125
- images_state[document._casa_image_name]['status'].text = `<p>${status}</p>`
3126
- }
3127
- }''',
3128
-
3129
- 'iter-gui-update': '''function get_update_dictionary( ) {
3130
- //const amste = images_state[document._casa_image_name]['automask']
3131
- //const clste = images_state[document._casa_image_name]['iteration']
3132
- // Assumption is that there is ONE set of iteration and automask updates
3133
- // for ALL imaging fields...
3134
- const amobj = Object.entries(images_state)[0][1].automask
3135
- const automask = amobj.active ?
3136
- Object.entries(amobj).reduce(
3137
- (acc,[k1,v1]) => { if ( hasprop(v1,'value') ) acc[k1] = v1.value; return acc },
3138
- { dogrowprune: amobj.dogrowprune.active,
3139
- fastnoise: amobj.fastnoise.active,
3140
- active: true }
3141
- ) : { }
3142
- const itobj = Object.entries(images_state)[0][1].iteration
3143
- const iteration = Object.entries(itobj).reduce(
3144
- (acc,[k1,v1]) => { if ( hasprop(v1,'value') ) acc[k1] = v1.value; return acc },
3145
- { }
3146
- )
3147
-
3148
- const masks = Object.entries(images_state).reduce( (acc,[k,v]) => { acc[k] = v.src.masks( ); return acc }, { } )
3149
- const breadcrumbs = Object.entries(images_state).reduce( (acc,[k,v]) => { acc[k] = v.src.breadcrumbs( ); return acc }, { } )
3150
- return { iteration, automask, masks, breadcrumbs, current_image: document._casa_image_name }
3151
- }
3152
- function update_log( log_lines ) {
3153
- let b = logbutton
3154
- b._log = b._log.concat( log_lines )
3155
- if ( b._window && ! b._window.closed ) {
3156
- for ( const line of log_lines ) {
3157
- const p = b._window.document.createElement('p')
3158
- p.appendChild( b._window.document.createTextNode(line) )
3159
- b._window.document.body.appendChild(p)
3160
- }
3161
- }
3162
- }
3163
- function update_gui( msg ) {
3164
- const itobj = Object.entries(images_state)[0][1].iteration
3165
- if ( msg.result === 'error' ) {
3166
- // ************************************************************************************
3167
- // ******** error occurs and is signaled by _gclean, e.g. exception in gclean ********
3168
- // ************************************************************************************
3169
- state.mode = 'interactive'
3170
- clean_ctrl.stop.button_type = "danger"
3171
- enable(false)
3172
- state.stopped = false
3173
- update_status( msg.stopdesc ? msg.stopdesc : 'An internal error has occurred' )
3174
- if ( 'cmd' in msg ) {
3175
- update_log( msg.cmd )
3176
- }
3177
- } else if ( msg.result === 'no-action' ) {
3178
- update_status( msg.stopdesc ? msg.stopdesc : 'nothing done' )
3179
- enable( false )
3180
- if ( 'cmd' in msg ) {
3181
- update_log( msg.cmd )
3182
- }
3183
- } else if ( msg.result == 'converged' ) {
3184
- state.mode = 'interactive'
3185
- clean_ctrl.stop.button_type = "danger"
3186
- enable(false)
3187
- state.stopped = false
3188
- update_status( msg.stopdesc ? msg.stopdesc : 'stopping criteria reached' )
3189
- if ( 'cmd' in msg ) {
3190
- update_log( msg.cmd )
3191
- }
3192
- refresh( msg )
3193
- } else if ( msg.result === 'update' ) {
3194
- if ( 'cmd' in msg ) {
3195
- update_log( msg.cmd )
3196
- }
3197
- refresh( msg )
3198
- // stopcode[0] == 1: iteration limit hit
3199
- // stopcode[0] == 9: major cycle limit hit
3200
- // *******************************************************************************************
3201
- // ******** perhaps the user should not be locked into exiting after the limit is hit ********
3202
- // *******************************************************************************************
3203
- //state.stopped = state.stopped || (msg.stopcode[0] > 1 && msg.stopcode[0] < 9) || msg.stopcode[0] == 0
3204
- state.stopped = false
3205
- if ( state.mode === 'interactive' && ! state.awaiting_stop ) {
3206
- clean_ctrl.stop.button_type = "danger"
3207
- update_status( msg.stopdesc ? msg.stopdesc : 'stopcode' in msg ? msg.stopcode[0] : -1 )
3208
- if ( ! state.stopped ) {
3209
- enable( false )
3210
- } else {
3211
- disable( false )
3212
- }
3213
- } else if ( state.mode === 'continuous' && ! state.awaiting_stop ) {
3214
- if ( ! state.stopped && itobj.niter.value > 0 && (itobj.nmajor.value > 0 || itobj.nmajor.value == -1) ) {
3215
- // *******************************************************************************************
3216
- // ******** 'niter.value > 0 so continue with one more iteration ********
3217
- // ******** 'nmajor.value' == -1 implies do not consider nmajor in stop consideration ********
3218
- // *******************************************************************************************
3219
- ctrl_pipe.send( ids[cb_obj.origin.name],
3220
- { action: 'finish',
3221
- value: get_update_dictionary( ) },
3222
- update_gui )
3223
- } else if ( ! state.stopped ) {
3224
- // *******************************************************************************************
3225
- // ******** 'niter.value <= 0 so iteration should stop ********
3226
- // *******************************************************************************************
3227
- state.mode = 'interactive'
3228
- clean_ctrl.stop.button_type = "danger"
3229
- enable(false)
3230
- state.stopped = false
3231
- update_status( msg.stopdesc ? msg.stopdesc : 'stopping criteria reached' )
3232
- } else {
3233
- state.mode = 'interactive'
3234
- clean_ctrl.stop.button_type = "danger"
3235
- enable(false)
3236
- state.stopped = false
3237
- update_status( msg.stopdesc ? msg.stopdesc : 'stopcode' in msg ? msg.stopcode[0] : -1 )
3238
- }
3239
- }
3240
- } else if ( msg.result === 'error' ) {
3241
- img_src.drop_breadcrumb('E')
3242
- if ( 'cmd' in msg ) {
3243
- update_log( msg.cmd )
3244
- }
3245
- state.mode = 'interactive'
3246
- clean_ctrl.stop.button_type = "danger"
3247
- state.stopped = false
3248
- update_status( 'stopcode' in msg ? msg.stopcode[0] : -1 )
3249
- enable( false )
3250
- }
3251
- }''',
3252
-
3253
- 'clean-wait': '''function wait_for_tclean_stop( msg ) {
3254
- state.mode = 'interactive'
3255
- clean_ctrl.stop.button_type = "danger"
3256
- enable( false )
3257
- state.awaiting_stop = false
3258
- update_status( 'user requested stop' )
3259
- }''',
3260
- }
1849
+ context = exe.Context( exe.Mode.SYNC )
1850
+ exec_task = self._ui( exe.Setting.CLI, context, "interactive-clean" )
1851
+ return context.execute( exec_task, "interactive-clean" )