Commit a2619925b4cce4f113ab3a1f89dbfb2e82f2701d

Authored by Josh Cardenzana
1 parent b8df7f03

Add computation of pixel by pixel mean and stddev.

Add sources by pixel with maximum stddevs above its respective mean
Maximum is removed by setting pixel values to mean associated with peak pixel.
Showing 1 changed file with 29 additions and 34 deletions
cscripts/cssrcdetect.py
... ... @@ -72,13 +72,17 @@ class cssrcdetect(ctools.cscript):
72 72 self['exclrad'].real()
73 73 self['fit_pos'].boolean()
74 74 self['fit_shape'].boolean()
  75 +
  76 + # Query the smoothing parameters
75 77 self['smoothkernel'].string()
76   - self['smoothparam'].real()
  78 + if self['smoothkernel'].string().upper() != "NONE":
  79 + self['smoothparam'].real()
77 80  
78 81 # Query ahead output model filename
79 82 if self._read_ahead():
80 83 self['outmodel'].filename()
81 84 self['outds9file'].filename()
  85 + self["momentradius"].real()
82 86  
83 87 # Write input parameters into logger
84 88 self._log_parameters(gammalib.TERSE)
... ... @@ -102,20 +106,13 @@ class cssrcdetect(ctools.cscript):
102 106 self._log_header3(gammalib.NORMAL, 'Iteration '+str(i+1))
103 107  
104 108 # Get map moments
105   - mean, std = self._map_moments(counts)
106   -
107   - # Log map moments
108   - self._log_value(gammalib.NORMAL, 'Map mean', mean)
109   - self._log_value(gammalib.NORMAL, 'Map standard deviation', std)
  109 + mean, std = self._map_moments(counts, self["momentradius"].real())
110 110  
111 111 # Compute threshold
112   - threshold = mean + self['threshold'].real() * std
113   -
114   - # Log map threshold
115   - self._log_value(gammalib.NORMAL, 'Map threshold', threshold)
116   -
  112 + sigmap = (counts - mean)/std
  113 +
117 114 # Get maximum value and corresponding sky direction
118   - value, pos = self._find_maximum(counts, threshold=threshold)
  115 + value, pos = self._find_maximum(sigmap)
119 116  
120 117 # If maximum found then log maximum and add model
121 118 if pos is not None:
... ... @@ -131,7 +128,7 @@ class cssrcdetect(ctools.cscript):
131 128 self._add_model(pos, name)
132 129  
133 130 # Remove maximum from map
134   - counts = self._remove_maximum(counts, pos, mean,
  131 + counts = self._remove_maximum(counts, pos, mean(pos),
135 132 radius=self['exclrad'].real())
136 133  
137 134 # ... otherwise log that no maximum was found and break iterations
... ... @@ -147,7 +144,7 @@ class cssrcdetect(ctools.cscript):
147 144 # Return
148 145 return
149 146  
150   - def _find_maximum(self, skymap, threshold=0.0):
  147 + def _find_maximum(self, skymap):
151 148 """
152 149 Find maximum in a sky map
153 150  
... ... @@ -155,8 +152,6 @@ class cssrcdetect(ctools.cscript):
155 152 ----------
156 153 skymap : `~gammalib.GSkyMap()`
157 154 Sky map
158   - threshold : float, optional
159   - Threshold for maximum value
160 155  
161 156 Returns
162 157 -------
... ... @@ -164,7 +159,7 @@ class cssrcdetect(ctools.cscript):
164 159 Maximum sky map value and corresponding sky direction
165 160 """
166 161 # Initialise maximum pixel value and sky direction
167   - value = threshold
  162 + value = self['threshold'].real()
168 163 pos = None
169 164  
170 165 # Loop over all pixels and find maximum
... ... @@ -208,7 +203,7 @@ class cssrcdetect(ctools.cscript):
208 203 # Return copy of map
209 204 return skymap_copy
210 205  
211   - def _map_moments(self, skymap):
  206 + def _map_moments(self, skymap, radius):
212 207 """
213 208 Determine moments of sky map pixels
214 209  
... ... @@ -216,26 +211,26 @@ class cssrcdetect(ctools.cscript):
216 211 ----------
217 212 skymap : `~gammalib.GSkyMap()`
218 213 Sky map
  214 + radius : float
  215 + radius (deg) for pixel consideration
219 216  
220 217 Returns
221 218 -------
222   - mean, std : tuple of float
223   - Mean and standard deviation of pixel values
  219 + mean, std : tuple of GSkyMap
  220 + Mean and standard deviation of pixel values within a given radius
224 221 """
225   - # Initialise mean and standard deviation
226   - mean = 0.0
227   - std = 0.0
228   -
229   - # Loop over all pixels
230   - for i in range(skymap.npix()):
231   - mean += skymap[i]
232   - std += skymap[i]*skymap[i]
233   -
234   - # Compute mean and standard deviation
235   - mean /= float(skymap.npix())
236   - std /= float(skymap.npix())
237   - std -= mean * mean
238   - std = math.sqrt(std)
  222 + # Copy the input skymap
  223 + mean = gammalib.GSkyMap(skymap)
  224 + std = gammalib.GSkyMap(skymap)
  225 + std *= std
  226 +
  227 + # Convolve by disk to get bin-by-bin mean
  228 + mean.smooth("DISK", radius)
  229 + std.smooth("DISK", radius)
  230 +
  231 + # Compute the standard deviation for each pixel
  232 + std = std - (mean*mean)
  233 + std = std.sqrt()
239 234  
240 235 # Return mean and standard deviation
241 236 return mean, std
... ...