Ejemplo n.º 1
0
    def redo(self):
        self.w.background_value.set_text(str(self._dummy_value))
        self.w.subtract.widget.setEnabled(False)

        if self.bgtype not in ('annulus', 'box'):
            return True

        self.w.x.set_text(str(self.xcen))
        self.w.y.set_text(str(self.ycen))
        self._debug_str = 'x={0}, y={1}'.format(self.xcen, self.ycen)

        image = self.fitsimage.get_image()
        depth = image.get_depth()
        if depth == 3:
            self.logger.error(
                'Background calculation for RGB image is not supported')
            return True

        header = image.get_header()
        extname = header.get(self._ext_key, self._no_keyword).upper()
        if extname != self._sci_extname:
            self.logger.debug(
                'Background calculation not possible for {0} extension in '
                '{1}'.format(extname, image.get('name')))
            return True

        try:
            obj = self.canvas.getObjectByTag(self.bgsubtag)
        except KeyError:
            return True
        if obj.kind != 'compound':
            return True
        bg_obj = obj.objects[0]

        if self.bgtype == 'annulus':
            self.w.r.set_text(str(self.radius))
            self._debug_str += ', r={0}, dannulus={1}'.format(
                self.radius, self.annulus_width)
        else:  # box
            self.w.box_w.set_text(str(self.boxwidth))
            self.w.box_h.set_text(str(self.boxheight))
            self._debug_str += ', w={0}, h={1}'.format(self.boxwidth,
                                                       self.boxheight)

        # Extract background data
        bg_masked = image.cutout_shape(bg_obj)
        bg_data = bg_masked[~bg_masked.mask]
        self.bgval = calc_stat(bg_data,
                               sigma=self.sigma,
                               niter=self.niter,
                               algorithm=self.algorithm)
        self._debug_str += (', bgval={0}, salgo={1}, sigma={2}, '
                            'niter={3}'.format(self.bgval, self.algorithm,
                                               self.sigma, self.niter))

        self.logger.debug(self._debug_str)
        self.w.background_value.set_text(str(self.bgval))

        if self.bgval != 0:
            self.w.subtract.widget.setEnabled(True)

        return True
Ejemplo n.º 2
0
    def redo(self):
        """This updates circle/point values from drawing.
        It also calculates fill value but does not apply it.

        """
        if not self.gui_up:
            return True

        self.w.fix_bad_pixels.set_enabled(False)
        self.w.x.set_text(str(self.xcen))
        self.w.y.set_text(str(self.ycen))

        if self.corrtype == 'circle':
            self.w.r.set_text(str(self.radius))

        if self.filltype != 'spline':
            self.fillval = self._dummy_value
            self.w.fill_value.set_text(str(self.fillval))

        image = self.fitsimage.get_image()
        if image is None:
            return True

        depth = image.get_depth()
        if depth == 3:
            self.logger.error(
                'Bad pixel correction for RGB image is not supported')
            return True

        header = image.get_header()
        extname = header.get(self._ext_key, self._no_keyword).upper()

        # Only calculate for science extension.
        # If EXTNAME does not exist, just assume user knows best.
        if extname not in (self._sci_extname, self._no_keyword):
            self.logger.debug(
                'Bad pixel correction for science data not possible for {0} '
                'extension in {1}'.format(extname, image.get('name')))
            return True

        # Nothing to do
        if self.filltype == 'constant':
            self.w.fix_bad_pixels.set_enabled(True)
            return True

        try:
            obj = self.canvas.get_object_by_tag(self.bpixcorrtag)
        except KeyError:
            return True
        if obj.kind != 'compound':
            return True
        sig_obj = obj.objects[1]

        # Just have to make sure signal region exists
        if self.filltype == 'spline':
            self.w.fix_bad_pixels.set_enabled(True)
            return True

        # Extract DQ info
        dqsrc = self.load_dq(image, header)

        sci_masked = image.cutout_shape(sig_obj)

        # Extract DQ mask
        if dqsrc is not False:
            dqsrc_masked = dqsrc.cutout_shape(sig_obj)
            mask = (~dqsrc_masked.mask) & (dqsrc_masked.data == 0)
        else:
            mask = ~sci_masked.mask

        # Extract annulus data
        try:
            sci_data = sci_masked[mask]
        except Exception as e:
            self.logger.error('{0}: {1}'.format(e.__class__.__name__, str(e)))
            return True

        # Calculate fill value from annulus
        self.fillval = utils.calc_stat(sci_data,
                                       sigma=self.sigma,
                                       niter=self.niter,
                                       algorithm=self.algorithm)
        self.w.fill_value.set_text(str(self.fillval))

        self.w.fix_bad_pixels.set_enabled(True)
        return True
Ejemplo n.º 3
0
    def redo(self):
        self.w.background_value.set_text(str(self._dummy_value))
        self.w.subtract.widget.setEnabled(False)

        if self.bgtype not in ('annulus', 'box'):
            return True

        self.w.x.set_text(str(self.xcen))
        self.w.y.set_text(str(self.ycen))
        self._debug_str = 'x={0}, y={1}'.format(self.xcen, self.ycen)

        image = self.fitsimage.get_image()
        if image is None:
            return True

        depth = image.get_depth()
        if depth == 3:
            self.logger.error(
                'Background calculation for RGB image is not supported')
            return True

        header = image.get_header()
        extname = header.get(self._ext_key, self._no_keyword).upper()

        # Only calculate for science extension.
        # If EXTNAME does not exist, just assume user knows best.
        if extname not in (self._sci_extname, self._no_keyword):
            self.logger.warn(
                'Background calculation not possible for {0} extension in '
                '{1}'.format(extname, image.get('name')))
            return True

        try:
            obj = self.canvas.getObjectByTag(self.bgsubtag)
        except KeyError:
            return True
        if obj.kind != 'compound':
            return True
        bg_obj = obj.objects[0]

        if self.bgtype == 'annulus':
            self.w.r.set_text(str(self.radius))
            self._debug_str += ', r={0}, dannulus={1}'.format(
                self.radius, self.annulus_width)
        else:  # box
            self.w.box_w.set_text(str(self.boxwidth))
            self.w.box_h.set_text(str(self.boxheight))
            self._debug_str += ', w={0}, h={1}'.format(
                self.boxwidth, self.boxheight)

        # Extract background data
        try:
            bg_masked = image.cutout_shape(bg_obj)
            bg_data = bg_masked[~bg_masked.mask]
        except Exception as e:
            self.logger.warn('{0}: {1}'.format(e.__class__.__name__, str(e)))
            self.bgval = self._dummy_value
        else:
            self.bgval = calc_stat(bg_data, sigma=self.sigma, niter=self.niter,
                                   algorithm=self.algorithm)

        self._debug_str += (', bgval={0}, salgo={1}, sigma={2}, '
                            'niter={3}'.format(
            self.bgval, self.algorithm, self.sigma, self.niter))
        self.logger.debug(self._debug_str)
        self.w.background_value.set_text(str(self.bgval))

        if self.bgval != 0:
            self.w.subtract.widget.setEnabled(True)

        return True
Ejemplo n.º 4
0
    def redo(self):
        if not self.gui_up:
            return True

        self.w.background_value.set_text(str(self._dummy_value))
        self.w.subtract.set_enabled(False)

        if self.bgtype not in ('annulus', 'box'):
            return True

        self.w.x.set_text(str(self.xcen))
        self.w.y.set_text(str(self.ycen))
        self._debug_str = f'x={self.xcen}, y={self.ycen}'

        image = self.fitsimage.get_image()
        if image is None:
            return True

        depth = image.get_depth()
        if depth == 3:
            self.logger.error(
                'Background calculation for RGB image is not supported')
            return True

        header = image.get_header()
        extname = header.get(self._ext_key, self._no_keyword).upper()

        # Only calculate for science extension.
        # If EXTNAME does not exist, just assume user knows best.
        if extname not in (self._sci_extname, self._no_keyword):
            self.logger.warn(
                f'Background calculation not possible for {extname} extension '
                f'in {image.get("name")}')
            return True

        try:
            obj = self.canvas.get_object_by_tag(self.bgsubtag)
        except KeyError:
            return True
        if obj.kind != 'compound':
            return True
        bg_obj = obj.objects[0]

        if self.bgtype == 'annulus':
            self.w.r.set_text(str(self.radius))
            self._debug_str += f', r={self.radius}, dannulus={self.annulus_width}'  # noqa: E501
        else:  # box
            self.w.box_w.set_text(str(self.boxwidth))
            self.w.box_h.set_text(str(self.boxheight))
            self._debug_str += f', w={self.boxwidth}, h={self.boxheight}'

        # Extract DQ info
        if self.ignore_badpix:
            dqsrc = self.load_dq(image, header)
        else:
            dqsrc = False

        bg_masked = image.cutout_shape(bg_obj)

        # Extract DQ mask
        if dqsrc is not False:
            dqsrc_masked = dqsrc.cutout_shape(bg_obj)
            mask = (~dqsrc_masked.mask) & (dqsrc_masked.data == 0)
        else:
            mask = ~bg_masked.mask

        # Extract background data
        try:
            bg_data = bg_masked.data[mask]
        except Exception as e:
            self.logger.warn(f'{e.__class__.__name__}: {repr(e)}')
            self.bgval = self._dummy_value
        else:
            self.bgval = utils.calc_stat(bg_data,
                                         sigma=self.sigma,
                                         niter=self.niter,
                                         algorithm=self.algorithm)

        self._debug_str += (f', bgval={self.bgval}, salgo={self.algorithm}, '
                            f'sigma={self.sigma}, niter={self.niter}, '
                            f'ignore_badpix={self.ignore_badpix}')
        self.logger.debug(self._debug_str)
        self.w.background_value.set_text(str(self.bgval))

        if self.bgval != 0:
            self.w.subtract.set_enabled(True)

        return True
Ejemplo n.º 5
0
    def redo(self):
        """Calculate SBR and SNR."""
        if not self.gui_up:
            return True

        self._clear_results()

        self.w.x.set_text(str(self.xcen))
        self.w.y.set_text(str(self.ycen))
        self._debug_str = f'x={self.xcen}, y={self.ycen}'

        image = self.fitsimage.get_image()
        if image is None:
            return True

        depth = image.get_depth()
        if depth == 3:
            self.logger.error(
                'SNR/SBR calculation for RGB image is not supported')
            return True

        header = image.get_header()
        extname = header.get(self._ext_key, self._no_keyword).upper()

        # Only calculate for science extension.
        # If EXTNAME does not exist, just assume user knows best.
        if extname not in (self._sci_extname, self._no_keyword):
            self.logger.warn(
                f'SNR/SBR calculations not possible for {extname} extension in'
                f' {image.get("name")}')
            return True

        try:
            obj = self.canvas.get_object_by_tag(self.sbrtag)
        except KeyError:
            return True
        if obj.kind != 'compound':
            return True
        sig_obj = obj.objects[0]
        bg_obj = obj.objects[1]

        if self.sigtype == 'box':
            self.w.box_w.set_text(str(self.boxwidth))
            self.w.box_h.set_text(str(self.boxheight))
            self._debug_str += f', w={self.boxwidth}, h={self.boxheight}'
        elif self.sigtype == 'circle':
            self.w.r.set_text(str(self.radius))
            self._debug_str += f', r={self.radius}'
        else:  # polygon
            self._poly_pts = sig_obj.points
            self._debug_str += f', pts={self._poly_pts}'

        # Set min SBR here, in case subclass reimplement this method to use
        # image metadata as selection criteria.
        minsbr = self.set_minsbr()

        sci_masked = image.cutout_shape(sig_obj)
        bg_masked = image.cutout_shape(bg_obj)

        # Extract ERR info
        errsrc = self.load_err(image, header)

        # Extract DQ info
        if self.ignore_badpix:
            dqsrc = self.load_dq(image, header)
        else:
            dqsrc = False

        # Extract signal and background masks for SBR.
        # If DQ is present, use drawn regions with good pixels only.
        # Otherwise, use all drawn regions.
        if dqsrc is False:
            mask_sci = ~sci_masked.mask
            mask_bg = ~bg_masked.mask
        else:
            dq_sci_masked = dqsrc.cutout_shape(sig_obj)
            dq_bg_masked = dqsrc.cutout_shape(bg_obj)
            mask_sci = (~dq_sci_masked.mask) & (dq_sci_masked.data == 0)
            mask_bg = (~dq_bg_masked.mask) & (dq_bg_masked.data == 0)

        # Extract signal and background for SBR
        try:
            sci_data = sci_masked.data[mask_sci]
            bg_data = bg_masked.data[mask_bg]
        except Exception as e:
            self.logger.error(f'{e.__class__.__name__}: {repr(e)}')
            return

        # Calculate SBR
        sig_med = np.median(sci_data)
        bg_std = utils.calc_stat(bg_data,
                                 sigma=self.sigma,
                                 niter=self.niter,
                                 algorithm='stddev')
        bg_mean = utils.calc_stat(bg_data,
                                  sigma=self.sigma,
                                  niter=self.niter,
                                  algorithm='mean')
        self.w.sig_med.set_text(str(sig_med))
        self.w.bg_std.set_text(str(bg_std))
        self.w.bg_mean.set_text(str(bg_mean))
        self._debug_str += (
            f', bg_x={bg_obj.x}, bg_y={bg_obj.y}, bg_r={bg_obj.radius}, '
            f'dannulus={bg_obj.width}, sigma={self.sigma}, '
            f'niter={self.niter}, sig_med={sig_med}, bg_std={bg_std}, '
            f'bg_mean={bg_mean}')

        if bg_std != 0:
            sbrval = sig_med / bg_std
            self.w.sbr_value.set_text(str(sbrval))
            self._debug_str += f', sbr={sbrval}, minsbr={minsbr}'

            # Update SBR status
            if sbrval > minsbr:
                self.set_sbr_status(ok_status=True)
            else:
                self.set_sbr_status(ok_status=False)

        if errsrc is not False:
            err_masked = errsrc.cutout_shape(sig_obj)

            # Extract signal mask for SNR.
            # Only use drawn region with non-zero ERR to avoid div by zero.
            # If DQ is present, also exclude non-good pixels.
            if dqsrc is False:
                mask_snr = (~err_masked.mask) & (err_masked.data != 0)
            else:
                mask_snr = ((~err_masked.mask) & (err_masked.data != 0) &
                            (dq_sci_masked.data == 0))

            # Extract science and error arrays for SNR
            try:
                snr_sci_data = sci_masked.data[mask_snr]
                snr_err_data = err_masked.data[mask_snr]
            except Exception as e:
                self.logger.error(f'{e.__class__.__name__}: {repr(e)}')
                return

            # Calculate SNR
            snr_data = snr_sci_data / snr_err_data
            snrmin = snr_data.min()
            snrmean = snr_data.mean()
            snrmax = snr_data.max()
            self.w.min_snr.set_text(str(snrmin))
            self.w.mean_snr.set_text(str(snrmean))
            self.w.max_snr.set_text(str(snrmax))
            self._debug_str += f', snrmin={snrmin}, snrmean={snrmean}, snrmax={snrmax}'  # noqa: E501

        self.logger.debug(self._debug_str)
        self.w.update_hdr.set_enabled(True)
        return True
Ejemplo n.º 6
0
    def redo(self):
        """Calculate SBR and SNR."""
        if not self.gui_up:
            return True

        self._clear_results()

        self.w.x.set_text(str(self.xcen))
        self.w.y.set_text(str(self.ycen))
        self._debug_str = "x={0}, y={1}".format(self.xcen, self.ycen)

        image = self.fitsimage.get_image()
        if image is None:
            return True

        depth = image.get_depth()
        if depth == 3:
            self.logger.error("SNR/SBR calculation for RGB image is not supported")
            return True

        header = image.get_header()
        extname = header.get(self._ext_key, self._no_keyword).upper()

        # Only calculate for science extension.
        # If EXTNAME does not exist, just assume user knows best.
        if extname not in (self._sci_extname, self._no_keyword):
            self.logger.warn(
                "SNR/SBR calculations not possible for {0} extension in " "{1}".format(extname, image.get("name"))
            )
            return True

        try:
            obj = self.canvas.getObjectByTag(self.sbrtag)
        except KeyError:
            return True
        if obj.kind != "compound":
            return True
        sig_obj = obj.objects[0]
        bg_obj = obj.objects[1]

        if self.sigtype == "box":
            self.w.box_w.set_text(str(self.boxwidth))
            self.w.box_h.set_text(str(self.boxheight))
            self._debug_str += ", w={0}, h={1}".format(self.boxwidth, self.boxheight)
        elif self.sigtype == "circular":
            self.w.r.set_text(str(self.radius))
            self._debug_str += ", r={0}".format(self.radius)
        else:  # polygon
            self._poly_pts = sig_obj.points
            self._debug_str += ", pts={0}".format(self._poly_pts)

        # Set min SBR here, in case subclass reimplement this method to use
        # image metadata as selection criteria.
        minsbr = self.set_minsbr()

        sci_masked = image.cutout_shape(sig_obj)
        bg_masked = image.cutout_shape(bg_obj)

        # Extract ERR info
        errsrc = self.load_err(image, header)

        # Extract DQ info
        if self.ignore_badpix:
            dqsrc = self.load_dq(image, header)
        else:
            dqsrc = False

        # Extract signal and background masks for SBR.
        # If DQ is present, use drawn regions with good pixels only.
        # Otherwise, use all drawn regions.
        if dqsrc is False:
            mask_sci = ~sci_masked.mask
            mask_bg = ~bg_masked.mask
        else:
            dq_sci_masked = dqsrc.cutout_shape(sig_obj)
            dq_bg_masked = dqsrc.cutout_shape(bg_obj)
            mask_sci = (~dq_sci_masked.mask) & (dq_sci_masked.data == 0)
            mask_bg = (~dq_bg_masked.mask) & (dq_bg_masked.data == 0)

        # Extract signal and background for SBR
        try:
            sci_data = sci_masked.data[mask_sci]
            bg_data = bg_masked.data[mask_bg]
        except Exception as e:
            self.logger.error("{0}: {1}".format(e.__class__.__name__, str(e)))
            return

        # Calculate SBR
        sig_med = np.median(sci_data)
        bg_std = utils.calc_stat(bg_data, sigma=self.sigma, niter=self.niter, algorithm="stddev")
        bg_mean = utils.calc_stat(bg_data, sigma=self.sigma, niter=self.niter, algorithm="mean")
        self.w.sig_med.set_text(str(sig_med))
        self.w.bg_std.set_text(str(bg_std))
        self.w.bg_mean.set_text(str(bg_mean))
        self._debug_str += (
            ", bg_x={0}, bg_y={1}, bg_r={2}, dannulus={3}, "
            "sigma={4}, niter={5}, sig_med={6}, bg_std={7}, "
            "bg_mean={8}".format(
                bg_obj.x, bg_obj.y, bg_obj.radius, bg_obj.width, self.sigma, self.niter, sig_med, bg_std, bg_mean
            )
        )

        if bg_std != 0:
            sbrval = sig_med / bg_std
            self.w.sbr_value.set_text(str(sbrval))
            self._debug_str += ", sbr={0}, minsbr={1}".format(sbrval, minsbr)

            # Update SBR status
            if sbrval > minsbr:
                self.set_sbr_status(ok_status=True)
            else:
                self.set_sbr_status(ok_status=False)

        if errsrc is not False:
            err_masked = errsrc.cutout_shape(sig_obj)

            # Extract signal mask for SNR.
            # Only use drawn region with non-zero ERR to avoid div by zero.
            # If DQ is present, also exclude non-good pixels.
            if dqsrc is False:
                mask_snr = (~err_masked.mask) & (err_masked.data != 0)
            else:
                mask_snr = (~err_masked.mask) & (err_masked.data != 0) & (dq_sci_masked.data == 0)

            # Extract science and error arrays for SNR
            try:
                snr_sci_data = sci_masked.data[mask_snr]
                snr_err_data = err_masked.data[mask_snr]
            except Exception as e:
                self.logger.error("{0}: {1}".format(e.__class__.__name__, str(e)))
                return

            # Calculate SNR
            snr_data = snr_sci_data / snr_err_data
            snrmin = snr_data.min()
            snrmean = snr_data.mean()
            snrmax = snr_data.max()
            self.w.min_snr.set_text(str(snrmin))
            self.w.mean_snr.set_text(str(snrmean))
            self.w.max_snr.set_text(str(snrmax))
            self._debug_str += ", snrmin={0}, snrmean={1}, snrmax={2}".format(snrmin, snrmean, snrmax)

        self.logger.debug(self._debug_str)
        self.w.update_hdr.set_enabled(True)
        return True
Ejemplo n.º 7
0
    def redo(self):
        """This updates circle/point values from drawing.
        It also calculates fill value but does not apply it.

        """
        if not self.gui_up:
            return True

        self.w.fix_bad_pixels.set_enabled(False)
        self.w.x.set_text(str(self.xcen))
        self.w.y.set_text(str(self.ycen))

        if self.corrtype == 'circular':
            self.w.r.set_text(str(self.radius))

        if self.filltype != 'spline':
            self.fillval = self._dummy_value
            self.w.fill_value.set_text(str(self.fillval))

        image = self.fitsimage.get_image()
        if image is None:
            return True

        depth = image.get_depth()
        if depth == 3:
            self.logger.error(
                'Bad pixel correction for RGB image is not supported')
            return True

        header = image.get_header()
        extname = header.get(self._ext_key, self._no_keyword).upper()

        # Only calculate for science extension.
        # If EXTNAME does not exist, just assume user knows best.
        if extname not in (self._sci_extname, self._no_keyword):
            self.logger.debug(
                'Bad pixel correction for science data not possible for {0} '
                'extension in {1}'.format(extname, image.get('name')))
            return True

        # Nothing to do
        if self.filltype == 'constant':
            self.w.fix_bad_pixels.set_enabled(True)
            return True

        try:
            obj = self.canvas.getObjectByTag(self.bpixcorrtag)
        except KeyError:
            return True
        if obj.kind != 'compound':
            return True
        sig_obj = obj.objects[1]

        # Just have to make sure signal region exists
        if self.filltype == 'spline':
            self.w.fix_bad_pixels.set_enabled(True)
            return True

        # Extract DQ info
        dqsrc = self.load_dq(image, header)

        sci_masked = image.cutout_shape(sig_obj)

        # Extract DQ mask
        if dqsrc is not False:
            dqsrc_masked = dqsrc.cutout_shape(sig_obj)
            mask = (~dqsrc_masked.mask) & (dqsrc_masked.data == 0)
        else:
            mask = ~sci_masked.mask

        # Extract annulus data
        try:
            sci_data = sci_masked[mask]
        except Exception as e:
            self.logger.error('{0}: {1}'.format(e.__class__.__name__, str(e)))
            return True

        # Calculate fill value from annulus
        self.fillval = utils.calc_stat(
            sci_data, sigma=self.sigma, niter=self.niter,
            algorithm=self.algorithm)
        self.w.fill_value.set_text(str(self.fillval))

        self.w.fix_bad_pixels.set_enabled(True)
        return True
Ejemplo n.º 8
0
    def redo(self):
        if not self.gui_up:
            return True

        self.w.background_value.set_text(str(self._dummy_value))
        self.w.subtract.set_enabled(False)

        if self.bgtype not in ("annulus", "box"):
            return True

        self.w.x.set_text(str(self.xcen))
        self.w.y.set_text(str(self.ycen))
        self._debug_str = "x={0}, y={1}".format(self.xcen, self.ycen)

        image = self.fitsimage.get_image()
        if image is None:
            return True

        depth = image.get_depth()
        if depth == 3:
            self.logger.error("Background calculation for RGB image is not supported")
            return True

        header = image.get_header()
        extname = header.get(self._ext_key, self._no_keyword).upper()

        # Only calculate for science extension.
        # If EXTNAME does not exist, just assume user knows best.
        if extname not in (self._sci_extname, self._no_keyword):
            self.logger.warn(
                "Background calculation not possible for {0} extension in " "{1}".format(extname, image.get("name"))
            )
            return True

        try:
            obj = self.canvas.getObjectByTag(self.bgsubtag)
        except KeyError:
            return True
        if obj.kind != "compound":
            return True
        bg_obj = obj.objects[0]

        if self.bgtype == "annulus":
            self.w.r.set_text(str(self.radius))
            self._debug_str += ", r={0}, dannulus={1}".format(self.radius, self.annulus_width)
        else:  # box
            self.w.box_w.set_text(str(self.boxwidth))
            self.w.box_h.set_text(str(self.boxheight))
            self._debug_str += ", w={0}, h={1}".format(self.boxwidth, self.boxheight)

        # Extract DQ info
        if self.ignore_badpix:
            dqsrc = self.load_dq(image, header)
        else:
            dqsrc = False

        bg_masked = image.cutout_shape(bg_obj)

        # Extract DQ mask
        if dqsrc is not False:
            dqsrc_masked = dqsrc.cutout_shape(bg_obj)
            mask = (~dqsrc_masked.mask) & (dqsrc_masked.data == 0)
        else:
            mask = ~bg_masked.mask

        # Extract background data
        try:
            bg_data = bg_masked.data[mask]
        except Exception as e:
            self.logger.warn("{0}: {1}".format(e.__class__.__name__, str(e)))
            self.bgval = self._dummy_value
        else:
            self.bgval = utils.calc_stat(bg_data, sigma=self.sigma, niter=self.niter, algorithm=self.algorithm)

        self._debug_str += ", bgval={0}, salgo={1}, sigma={2}, niter={3}, " "ignore_badpix={4}".format(
            self.bgval, self.algorithm, self.sigma, self.niter, self.ignore_badpix
        )
        self.logger.debug(self._debug_str)
        self.w.background_value.set_text(str(self.bgval))

        if self.bgval != 0:
            self.w.subtract.set_enabled(True)

        return True