示例#1
0
 def _median(self):
     # Create a median image.
     #print "*  Creating a median array..."
     image.median(self.__arrObjectList,
                  self.combArrObj,
                  nlow=self.__nlow,
                  nhigh=self.__nhigh,
                  badmasks=self.__masks)
     return None
示例#2
0
    def _minimum(self):
        # Nominally computes the minimum pixel value for a stack of
        # identically shaped images
        try:
            # Try using the numarray.images.combine function "minimum" that
            # is available as part of numarray version 1.3
            image.minimum(self.__arrObjectList,self.combArrObj,nlow=self.__nlow,nhigh=self.__nhigh,badmasks=self.__masks)
        except:
            # If numarray version 1.3 is not installed so that the "minimum" function is not available.  We will create our own minimum images but no clipping
            # will be available.
            errormsg =  "\n\n#################################################\n"
            errormsg += "#                                               #\n"
            errormsg += "# WARNING:                                      #\n"
            errormsg += "#  Cannot compute a clipped minimum because     #\n"
            errormsg += "#  NUMARRAY version 1.3 or later is not         #\n"
            errormsg += "#  installed.  A minimum image will be created  #\n"
            errormsg += "#  but no clipping will be used.                #\n"
            errormsg += "#                                               #\n"
            errormsg += "#################################################\n"
            print(errormsg)

            # Create the minimum image from the stack of input images.
            # Find the maximum pixel value for the image stack.
            _maxValue = -1e+9

            for imgobj in self.__arrObjectList:
                _newMax = imgobj.max()
                if (_newMax > _maxValue):
                    _maxValue = _newMax

            tmpList = []
            if (self.__arrMaskList != None):
                # Sum the weightMaskList elements
                __maskSum = self.__sumImages(self.__arrMaskList)

                # For each image, set pixels masked as "bad" to the "super-maximum" value.
                for imgobj in range(len(self.__arrObjectList)):
                    tmp = np.where(self.__arrMaskList[imgobj] == 1,_maxValue+1,self.__arrObjectList[imgobj])
                    tmpList.append(tmp)
            else:
                for imgobj in range(len(self.__arrObjectList)):
                  tmpList.append(imgobj)

            # Create the minimum image by computing a median array throwing out all but one of the pixels in the stack
            # starting from the top of the stack.
            image.median(tmpList,self.combArrObj,nlow=0,nhigh=self.__numObjs-1,badmasks=None)

            # If we have been given masks we need to reset the mask values to 0 in the image
            if (self.__arrMaskList != None):
                # Reset any pixl at _maxValue + 1 to 0.
                self.combArrObj = np.where(__maskSum == self.__numObjs, 0, self.combArrObj)
    def create_median(self, resampled_models):
        """IFU-specific version of create_median."""
        resampled_sci = [i.data for i in resampled_models]
        resampled_wht = [i.weightmap for i in resampled_models]
        nlow = self.outlierpars.get('nlow', 0)
        nhigh = self.outlierpars.get('nhigh', 0)
        maskpt = self.outlierpars.get('maskpt', 0.7)
        badmasks = []
        for w in resampled_wht:
            # Due to a bug in numpy.nanmean, need to check
            # for a completely zero array
            if not np.any(w):
                mean_weight = 0.
            else:
                mean_weight, _, _ = sigma_clipped_stats(w,
                                                        sigma=3.0,
                                                        mask_value=0.)
            weight_threshold = mean_weight * maskpt
            # Mask pixels were weight falls below MASKPT percent of
            #    the mean weight
            mask = np.less(w, weight_threshold)
            log.debug("Number of pixels with low weight: {}".format(
                np.sum(mask)))
            badmasks.append(mask)

        # Compute median of stack os images using BADMASKS to remove low weight
        # values
        median_image = median(resampled_sci,
                              nlow=nlow,
                              nhigh=nhigh,
                              badmasks=badmasks)
        # for i in range(len(resampled_sci)):
        #    data = resampled_sci[i]
        #    mask = badmasks[i]
        return median_image
    def create_median(self, resampled_models):
        """IFU-specific version of create_median."""
        resampled_sci = [i.data for i in resampled_models]
        resampled_wht = [i.weightmap for i in resampled_models]

        nlow = self.outlierpars.get('nlow', 0)
        nhigh = self.outlierpars.get('nhigh', 0)
        maskpt = self.outlierpars.get('maskpt', 0.7)
        badmasks = []
        for w in resampled_wht:
            mean_weight, _, _ = sigma_clipped_stats(w,
                                                    sigma=3.0,
                                                    mask_value=0.)
            weight_threshold = mean_weight * maskpt
            # Mask pixels were weight falls below MASKPT percent of
            #    the mean weight
            mask = np.less(w, weight_threshold)
            log.debug("Number of pixels with low weight: {}".format(
                        np.sum(mask)))
            badmasks.append(mask)

        # Compute median of stack os images using BADMASKS to remove low weight
        # values
        median_image = median(resampled_sci, nlow=nlow, nhigh=nhigh,
                              badmasks=badmasks)

        return median_image
示例#5
0
def create_median(resampled_models, **pars):
    """Create a median image from the singly resampled images.

    NOTE: This version is simplified from astrodrizzle's version in the
        following ways:
        - type of combination: fixed to 'median'
        - 'minmed' not implemented as an option
        - does not use buffers to try to minimize memory usage
        - astropy.stats.sigma_clipped_stats replaces stsci.imagestats.ImageStats
        - stsci.image.median replaces stsci.image.numcombine.numCombine
    """
    resampled_sci = [i.data for i in resampled_models]
    resampled_wht = [i.wht for i in resampled_models]

    nlow = pars.get('nlow', 0)
    nhigh = pars.get('nhigh', 0)
    maskpt = pars.get('maskpt', 0.7)

    badmasks = []
    for w in resampled_wht:
        mean_weight, _, _ = sigma_clipped_stats(w, sigma=3.0, mask_value=0.)
        weight_threshold = mean_weight * maskpt
        # Mask pixels were weight falls below MASKPT percent of the mean weight
        mask = np.less(w, weight_threshold)
        log.debug("Number of pixels with low weight: {}".format(np.sum(mask)))
        badmasks.append(mask)

    # Compute median of stack os images using BADMASKS to remove low weight
    # values
    median_image = median(resampled_sci,
                          nlow=nlow,
                          nhigh=nhigh,
                          badmasks=badmasks)

    return median_image
示例#6
0
    def create_median(self, resampled_models):
        """Create a median image from the singly resampled images.

        NOTES
        -----
        This version is simplified from astrodrizzle's version in the
        following ways:
        - type of combination: fixed to 'median'
        - 'minmed' not implemented as an option
        """
        resampled_sci = [i.data for i in resampled_models]
        resampled_weight = [i.wht for i in resampled_models]

        nlow = self.outlierpars.get('nlow', 0)
        nhigh = self.outlierpars.get('nhigh', 0)
        maskpt = self.outlierpars.get('maskpt', 0.7)

        # Create a mask for each input image, masking out areas where there is
        # no data or the data has very low weight
        badmasks = []
        for weight in resampled_weight:
            # Create boolean masks for weight being zero or NaN
            mask_zero_weight = np.equal(weight, 0.)
            mask_nans = np.isnan(weight)
            # Combine the masks
            weight_masked = np.ma.array(weight,
                                        mask=np.logical_or(
                                            mask_zero_weight, mask_nans))
            # Sigma-clip the unmasked data
            weight_masked = sigma_clip(weight_masked, sigma=3, maxiters=5)
            mean_weight = np.mean(weight_masked)
            # Mask pixels where weight falls below maskpt percent
            weight_threshold = mean_weight * maskpt
            badmask = np.less(weight, weight_threshold)
            log.debug("Percentage of pixels with low weight: {}".format(
                np.sum(badmask) / len(weight.flat) * 100))
            badmasks.append(badmask)

        # Compute median of stack of images using `badmasks` to remove
        # low-weight values.  In the future we should use a masked array
        # and np.median
        median_image = median(resampled_sci,
                              nlow=nlow,
                              nhigh=nhigh,
                              badmasks=badmasks)

        return median_image
示例#7
0
    def create_median(self, resampled_models):
        """Create a median image from the singly resampled images.

        NOTES
        -----
        This version is simplified from astrodrizzle's version in the
        following ways:
        - type of combination: fixed to 'median'
        - 'minmed' not implemented as an option
        - does not use buffers to try to minimize memory usage
        - `astropy.stats.sigma_clipped_stats` replaces `stsci.imagestats.ImageStats`
        - `stsci.image.median` replaces `stsci.image.numcombine.numCombine`
        """
        resampled_sci = [i.data for i in resampled_models]
        resampled_wht = [i.wht for i in resampled_models]

        nlow = self.outlierpars.get('nlow', 0)
        nhigh = self.outlierpars.get('nhigh', 0)
        maskpt = self.outlierpars.get('maskpt', 0.7)

        badmasks = []
        for w in resampled_wht:
            mean_weight, _, _ = sigma_clipped_stats(w,
                                                    sigma=3.0, mask_value=0.)
            weight_threshold = mean_weight * maskpt
            # Mask pixels were weight falls below
            #   MASKPT percent of the mean weight
            mask = np.less(w, weight_threshold)
            log.debug("Number of pixels with low weight: {}".format(
                np.sum(mask)))
            badmasks.append(mask)

        # Compute median of stack os images using BADMASKS to remove low weight
        # values
        median_image = median(resampled_sci, nlow=nlow, nhigh=nhigh,
                              badmasks=badmasks)

        return median_image
示例#8
0
def num_combine(data,
                masks=None,
                combination_type="median",
                nlow=0,
                nhigh=0,
                upper=None,
                lower=None):
    """ A lite version of the imcombine IRAF task

    Parameters
    ----------
    data : list of 2D numpy.ndarray or a 3D numpy.ndarray
        A sequence of inputs arrays, which are nominally a stack of identically
        shaped images.

    masks : list of 2D numpy.ndarray or a 3D numpy.ndarray
        A sequence of mask arrays to use for masking out 'bad' pixels from the
        combination. The ndarray should be a numpy array, despite the variable
        name.

    combinationType : {'median', 'imedian', 'iaverage', 'mean', 'sum', 'minimum'}
        Type of operation should be used to combine the images.
        The 'imedian' and 'iaverage' types ignore pixels which have been
        flagged as bad in all input arrays and returns the value from the last
        image in the stack for that pixel.

    nlow : int, optional
        Number of low pixels to throw out of the median calculation.

    nhigh : int, optional
        Number of high pixels to throw out of the median calculation.

    upper : float, optional
        Throw out values >= to upper in a median calculation.

    lower : float, optional
        Throw out values < lower in a median calculation.

    Returns
    -------
    comb_arr : numpy.ndarray
        Combined output array.

    Examples
    --------
    This function can be used to create a median image from a stack of images
    with the following commands:

    >>> import numpy as np
    >>> from stsci.image import numcombine as nc
    >>> a = np.ones([5,5],np.float32)
    >>> b = a - 0.05
    >>> c = a + 0.1
    >>> result = nc.num_combine([a, b, c], combination_type='mean')
    >>> result
    array([[1.0166667, 1.0166667, 1.0166667, 1.0166667, 1.0166667],
           [1.0166667, 1.0166667, 1.0166667, 1.0166667, 1.0166667],
           [1.0166667, 1.0166667, 1.0166667, 1.0166667, 1.0166667],
           [1.0166667, 1.0166667, 1.0166667, 1.0166667, 1.0166667],
           [1.0166667, 1.0166667, 1.0166667, 1.0166667, 1.0166667]], dtype=float32)
    """
    data = np.asarray(data)
    if masks is not None:
        masks = np.asarray(masks)

    # Simple sanity check to make sure that the min/max clipping doesn't throw
    # out all of the pixels.
    if data.shape[0] - nlow - nhigh < 1:
        raise ValueError("Rejecting all pixels due to large 'nval' and/or "
                         "'nhigh'!")

    # Create output numarray object
    comb_arr = np.empty_like(data[0])

    # Create the threshold masks if necessary and combine them with the
    # input masks (if any).
    if lower is not None or upper is not None:
        thmasks = image.threshhold(data, low=lower, high=upper)
        if masks is None:
            masks = thmasks
        else:
            masks = np.logical_or(masks, thmasks)

    # Combine the input images.
    combination_type = combination_type.lower()

    if combination_type == 'median':
        image.median(data,
                     comb_arr,
                     comb_arr.dtype,
                     nlow=nlow,
                     nhigh=nhigh,
                     badmasks=masks)
    elif combination_type == 'imedian':
        image.imedian(data,
                      comb_arr,
                      comb_arr.dtype,
                      nlow=nlow,
                      nhigh=nhigh,
                      badmasks=masks)
    elif combination_type in ['iaverage', 'imean']:
        image.iaverage(data,
                       comb_arr,
                       comb_arr.dtype,
                       nlow=nlow,
                       nhigh=nhigh,
                       badmasks=masks)
    elif combination_type == 'mean':
        image.average(data,
                      comb_arr,
                      comb_arr.dtype,
                      nlow=nlow,
                      nhigh=nhigh,
                      badmasks=masks)
    elif combination_type == 'sum':
        np.sum(data, axis=0, out=comb_arr)
    elif combination_type == 'minimum':
        image.minimum(data,
                      comb_arr,
                      comb_arr.dtype,
                      nlow=nlow,
                      nhigh=nhigh,
                      badmasks=masks)
    else:
        comb_arr.fill(0)
        print("Combination type not supported!!!")

    return comb_arr
示例#9
0
    def _minimum(self):
        # Nominally computes the minimum pixel value for a stack of
        # identically shaped images
        try:
            # Try using the numarray.images.combine function "minimum" that
            # is available as part of numarray version 1.3
            image.minimum(self.__arrObjectList,
                          self.combArrObj,
                          nlow=self.__nlow,
                          nhigh=self.__nhigh,
                          badmasks=self.__masks)
        except:
            # If numarray version 1.3 is not installed so that the "minimum" function is not available.  We will create our own minimum images but no clipping
            # will be available.
            errormsg = "\n\n#################################################\n"
            errormsg += "#                                               #\n"
            errormsg += "# WARNING:                                      #\n"
            errormsg += "#  Cannot compute a clipped minimum because     #\n"
            errormsg += "#  NUMARRAY version 1.3 or later is not         #\n"
            errormsg += "#  installed.  A minimum image will be created  #\n"
            errormsg += "#  but no clipping will be used.                #\n"
            errormsg += "#                                               #\n"
            errormsg += "#################################################\n"
            print(errormsg)

            # Create the minimum image from the stack of input images.
            # Find the maximum pixel value for the image stack.
            _maxValue = -1e+9

            for imgobj in self.__arrObjectList:
                _newMax = imgobj.max()
                if (_newMax > _maxValue):
                    _maxValue = _newMax

            tmpList = []
            if (self.__arrMaskList != None):
                # Sum the weightMaskList elements
                __maskSum = self.__sumImages(self.__arrMaskList)

                # For each image, set pixels masked as "bad" to the "super-maximum" value.
                for imgobj in range(len(self.__arrObjectList)):
                    tmp = np.where(self.__arrMaskList[imgobj] == 1,
                                   _maxValue + 1, self.__arrObjectList[imgobj])
                    tmpList.append(tmp)
            else:
                for imgobj in range(len(self.__arrObjectList)):
                    tmpList.append(imgobj)

            # Create the minimum image by computing a median array throwing out all but one of the pixels in the stack
            # starting from the top of the stack.
            image.median(tmpList,
                         self.combArrObj,
                         nlow=0,
                         nhigh=self.__numObjs - 1,
                         badmasks=None)

            # If we have been given masks we need to reset the mask values to 0 in the image
            if (self.__arrMaskList != None):
                # Reset any pixl at _maxValue + 1 to 0.
                self.combArrObj = np.where(__maskSum == self.__numObjs, 0,
                                           self.combArrObj)
示例#10
0
def num_combine(data, masks=None, combination_type="median",
                nlow=0, nhigh=0, upper=None, lower=None):
    """ A lite version of the imcombine IRAF task

    Parameters
    ----------
    data : list of 2D numpy.ndarray or a 3D numpy.ndarray
        A sequence of inputs arrays, which are nominally a stack of identically
        shaped images.

    masks : list of 2D numpy.ndarray or a 3D numpy.ndarray
        A sequence of mask arrays to use for masking out 'bad' pixels from the
        combination. The ndarray should be a numpy array, despite the variable
        name.

    combinationType : {'median', 'imedian', 'iaverage', 'mean', 'sum', 'minimum'}
        Type of operation should be used to combine the images.
        The 'imedian' and 'iaverage' types ignore pixels which have been
        flagged as bad in all input arrays and returns the value from the last
        image in the stack for that pixel.

    nlow : int, optional
        Number of low pixels to throw out of the median calculation.

    nhigh : int, optional
        Number of high pixels to throw out of the median calculation.

    upper : float, optional
        Throw out values >= to upper in a median calculation.

    lower : float, optional
        Throw out values < lower in a median calculation.

    Returns
    -------
    comb_arr : numpy.ndarray
        Combined output array.

    Examples
    --------
    This function can be used to create a median image from a stack of images
    with the following commands:

    >>> import numpy as np
    >>> from stsci.image import numcombine as nc
    >>> a = np.ones([5,5],np.float32)
    >>> b = a - 0.05
    >>> c = a + 0.1
    >>> result = nc.numcombine([a,b,c],combinationType='mean')
    >>> result
    array([[ 1.01666665,  1.01666665,  1.01666665,  1.01666665,  1.01666665],
           [ 1.01666665,  1.01666665,  1.01666665,  1.01666665,  1.01666665],
           [ 1.01666665,  1.01666665,  1.01666665,  1.01666665,  1.01666665],
           [ 1.01666665,  1.01666665,  1.01666665,  1.01666665,  1.01666665],
           [ 1.01666665,  1.01666665,  1.01666665,  1.01666665,  1.01666665]],
           dtype=float32)

    """
    data = np.asarray(data)
    if masks is not None:
        masks = np.asarray(masks)

    # Simple sanity check to make sure that the min/max clipping doesn't throw
    # out all of the pixels.
    if data.shape[0] - nlow - nhigh < 1:
        raise ValueError("Rejecting all pixels due to large 'nval' and/or "
                         "'nhigh'!")

    # Create output numarray object
    comb_arr = np.empty_like(data[0])

    # Create the threshold masks if necessary and combine them with the
    # input masks (if any).
    if lower is not None or upper is not None:
        thmasks = image.threshhold(data, low=lower, high=upper)
        if masks is None:
            masks = thmasks
        else:
            masks = np.logical_or(masks, thmasks)

    # Combine the input images.
    combination_type = combination_type.lower()

    if combination_type == 'median':
        image.median(data, comb_arr, comb_arr.dtype, nlow=nlow, nhigh=nhigh,
                     badmasks=masks)
    elif combination_type == 'imedian':
        image.imedian(data, comb_arr, comb_arr.dtype, nlow=nlow, nhigh=nhigh,
                      badmasks=masks)
    elif combination_type in ['iaverage', 'imean']:
        image.iaverage(data, comb_arr, comb_arr.dtype, nlow=nlow, nhigh=nhigh,
                       badmasks=masks)
    elif combination_type == 'mean':
        image.average(data, comb_arr, comb_arr.dtype, nlow=nlow, nhigh=nhigh,
                      badmasks=masks)
    elif combination_type == 'sum':
        np.sum(data, axis=0, out=comb_arr)
    elif combination_type == 'minimum':
        image.minimum(data, comb_arr, comb_arr.dtype, nlow=nlow, nhigh=nhigh,
                      badmasks=masks)
    else:
        comb_arr.fill(0)
        print("Combination type not supported!!!")

    return comb_arr
示例#11
0
 def _median(self):
     # Create a median image.
     #print "*  Creating a median array..."
     image.median(self.__arrObjectList,self.combArrObj,nlow=self.__nlow,nhigh=self.__nhigh,badmasks=self.__masks)
     return None