Line data Source code
1 : //# grpcInteractiveClean.h: perform interactive masking using grpc + casa viewer
2 : //# Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2003
3 : //# Associated Universities, Inc. Washington DC, USA.
4 : //#
5 : //# This library is free software; you can redistribute it and/or modify it
6 : //# under the terms of the GNU Library General Public License as published by
7 : //# the Free Software Foundation; either version 2 of the License, or (at your
8 : //# option) any later version.
9 : //#
10 : //# This library is distributed in the hope that it will be useful, but WITHOUT
11 : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 : //# License for more details.
14 : //#
15 : //# You should have received a copy of the GNU Library General Public License
16 : //# along with this library; if not, write to the Free Software Foundation,
17 : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 : //#
19 : //# Correspondence concerning AIPS++ should be addressed as follows:
20 : //# Internet email: aips2-request@nrao.edu.
21 : //# Postal address: AIPS++ Project Office
22 : //# National Radio Astronomy Observatory
23 : //# 520 Edgemont Road
24 : //#
25 : //# $Id$
26 :
27 : #ifndef SYNTHESIS_GRPCINTERACTIVEITERATION_H
28 : #define SYNTHESIS_GRPCINTERACTIVEITERATION_H
29 :
30 : #include <list>
31 : #include <mutex>
32 : #include <thread>
33 : #include <stdcasa/record.h>
34 : #include <casacore/casa/Containers/Record.h>
35 : #include "img.grpc.pb.h"
36 :
37 : namespace casa {
38 :
39 : struct grpcInteractiveCleanState {
40 : grpcInteractiveCleanState( );
41 : void reset( );
42 :
43 : int Niter;
44 : int CycleNiter;
45 : int InteractiveNiter;
46 :
47 : float Threshold;
48 : float CycleThreshold;
49 : float InteractiveThreshold;
50 :
51 : bool IsCycleThresholdAuto;
52 : bool IsThresholdAuto;
53 : // Determines if IsCycleThresholdAuto can ever be true.
54 : // Necessary because tclean implicitly updates IsCycleThresholdAuto during certain grpcInteractiveClean functions, but for deconvolve we don't want this value to change.
55 : bool IsCycleThresholdMutable;
56 :
57 : float CycleFactor;
58 : float LoopGain;
59 :
60 : bool StopFlag;
61 : bool PauseFlag;
62 : bool InteractiveMode;
63 : bool UpdatedModelFlag;
64 :
65 : int IterDone;
66 : int InteractiveIterDone;
67 : int MaxCycleIterDone;
68 : int MajorDone;
69 : int StopCode;
70 :
71 : //--- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
72 : float Nsigma;
73 : float MaxPsfSidelobe;
74 : float MinPsfFraction;
75 : float MaxPsfFraction;
76 : float PeakResidual;
77 :
78 : //--- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
79 : casacore::Array<double> SummaryMinor;
80 : casacore::Array<int> SummaryMajor;
81 : bool FullSummary;
82 : //--- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
83 : float MinorCyclePeakResidual;
84 : float PrevPeakResidual;
85 : float NsigmaThreshold;
86 :
87 : int PrevMajorCycleCount;
88 : float PeakResidualNoMask;
89 : float PrevPeakResidualNoMask;
90 : float MinPeakResidualNoMask;
91 : float MinPeakResidual;
92 : float MaskSum;
93 :
94 : //--- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
95 : float MadRMS;
96 :
97 : };
98 :
99 : class grpcInteractiveCleanGui {
100 : private:
101 : struct CleanState {
102 : int image_id;
103 : int prev_image_id;
104 : int mask_id;
105 : int prev_mask_id;
106 0 : CleanState( ) : image_id(0), prev_image_id(0),
107 0 : mask_id(0), prev_mask_id(0) { }
108 : };
109 :
110 : pid_t viewer_pid;
111 : bool viewer_started;
112 : std::string viewer_uri;
113 : std::unique_ptr<rpc::img::view::Stub> viewer_proxy;
114 : std::map<int,CleanState> clean_state;
115 :
116 : protected:
117 : std::string get_distro_data_path( );
118 : std::string get_python_path( );
119 : std::string get_viewer_path( );
120 : std::string get_fifo( );
121 :
122 : bool spawn_viewer( );
123 : bool stop_viewer( );
124 : bool launch( );
125 :
126 : void unload( int );
127 : static bool clone( const std::string &imageName, const std::string &newImageName );
128 : static float maskSum( const std::string &maskname );
129 :
130 : public:
131 : grpcInteractiveCleanGui( );
132 : ~grpcInteractiveCleanGui( );
133 :
134 : bool alive( );
135 : int open_panel( std::list<std::tuple<std::string,bool,bool,int>> images );
136 : void close_panel( int );
137 : int interactivemask( int panel, const std::string &image, const std::string &mask,
138 : int &niter, int &cycleniter, std::string &thresh,
139 : std::string &cyclethresh, const bool forceReload=false );
140 :
141 : };
142 :
143 : class grpcInteractiveCleanManager {
144 : private:
145 : grpcInteractiveCleanState state;
146 : std::mutex state_mtx;
147 : grpcInteractiveCleanGui gui;
148 :
149 : // filled at start of interaction... zero out after interactive clean completes...
150 : int clean_panel_id;
151 : // image name, multiterm, finished, exit code (if finished)
152 : std::list<std::tuple<std::string,bool,bool,int>> clean_images;
153 :
154 : protected:
155 15122 : template<typename T> T access( T data, std::function< T ( T, grpcInteractiveCleanState & )> csect ) {
156 28092 : std::lock_guard<std::mutex> state_lck(state_mtx);
157 30244 : return csect(data,state);
158 : }
159 :
160 : void pushDetails( );
161 : void updateCycleThreshold( grpcInteractiveCleanState & );
162 : void resetMinorCycleInitInfo( grpcInteractiveCleanState & );
163 : void mergeMinorCycleSummary( const casacore::Array<casacore::Double>&, grpcInteractiveCleanState &, casacore::Int immod );
164 :
165 : /**********
166 : casacore::Int interactivemask(const casacore::String& image, const casacore::String& mask,
167 : casacore::Int& niter, casacore::Int& cycleniter, casacore::String& thresh, casacore::String& cyclethresh,
168 : const casacore::Bool forceReload=false);
169 : ******/
170 : public:
171 1 : grpcInteractiveCleanManager( ) : clean_panel_id(-1) { }
172 :
173 : casacore::Record getMinorCycleControls( );
174 : casacore::Record getDetailsRecord( bool includeSummary=true );
175 : casacore::Record getSummaryRecord( ) { return getDetailsRecord(true); }
176 : int cleanComplete( bool lastcyclecheck=false, bool reachedMajorLimit=false );
177 : /* Note: Incrementing the Major cycle will reset the cycleIterDone */
178 : void incrementMajorCycleCount( );
179 : void resetMinorCycleInitInfo( );
180 : void addSummaryMajor( );
181 :
182 : void mergeCycleInitializationRecord( casacore::Record &initRecord );
183 : void mergeCycleExecutionRecord( casacore::Record&, casacore::Int immod );
184 :
185 : void changeStopFlag( bool stopEnabled );
186 :
187 : void setControlsFromRecord(const casac::record &);
188 : void setControls( int niter, int ncycle, float threshold );
189 :
190 : void setIterationDetails(const casac::record &);
191 : casacore::Record pauseForUserInteraction( );
192 : void closePanel( );
193 :
194 : };
195 :
196 : struct grpcInteractiveClean {
197 : static grpcInteractiveCleanManager &getManager( );
198 : };
199 :
200 : } //# NAMESPACE CASA - END
201 :
202 : #endif
203 :
|