# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)
import numpy as np
from astropy.stats import sigma_clip

__all__ = ['sigmaclip_stats']


def sigmaclip_stats(image, image_mask=None, mask_val=None, sigma=3.0,
                    iters=None):
    """
    Calculate sigma-clipped statistics of an image.  For example,
    sigma-clipped statistics can be used to estimate the background and
    background noise in an image.

    Parameters
    ----------
    image : array_like
        The 2D array of the image.

    image_mask : array_like, bool, optional
        A boolean mask with the same shape as ``image``, where a `True`
        value indicates the corresponding element of ``image`` is
        invalid.  Masked pixels are ignored when computing the image
        statistics.

    mask_val : float, optional
        An image data value (e.g., ``0.0``) that is ignored when
        computing the image statistics.  ``mask_val`` will be ignored if
        ``image_mask`` is input.

    sigma : float, optional
        The number of standard deviations to use as the clipping limit.

    iters : float, optional
       The number of clipping iterations to perform, or `None` to clip
       until convergence is achieved (i.e. continue until the last
       iteration clips nothing).

    Returns
    -------
    mean, median, stddev : floats
        The mean, median, and standard deviation of the sigma-clipped
        image.
    """

    if image_mask is not None:
        assert image_mask.dtype == np.bool, \
            'image_mask must be a boolean ndarray'
        image = image[~image_mask]
    if mask_val is not None and image_mask is None:
        idx = (image != mask_val).nonzero()
        image = image[idx]
    image_clip = sigma_clip(image, sig=sigma, iters=iters)
    goodvals = image_clip.data[~image_clip.mask]
    return np.mean(goodvals), np.median(goodvals), np.std(goodvals)

@@ -279,21 +279,14 @@
 ats(
-image, image_
+data, 
 mask
@@ -317,36 +317,16 @@
 gma=3.0,
-%0A                   
  iters=N
@@ -390,16 +390,20 @@
 n image.
+%0A%0A  
   For ex
@@ -408,20 +408,16 @@
 example,
-%0A   
  sigma-c
@@ -461,16 +461,20 @@
 mate the
+%0A   
  backgro
@@ -480,20 +480,16 @@
 ound and
-%0A   
  backgro
@@ -546,21 +546,20 @@
 ---%0A    
-image
+data
  : array
@@ -608,37 +608,37 @@
     
-image_
 mask : 
-array_like, 
+%60numpy.ndarray%60 (
 bool
+)
 , op
@@ -692,21 +692,20 @@
 pe as %60%60
-image
+data
 %60%60, wher
@@ -774,35 +774,32 @@
 f %60%60
-image%60%60 is%0A        invalid.
+data%60%60 is masked.%0A      
   Ma
@@ -814,21 +814,22 @@
 els are 
-ignor
+exclud
 ed when 
@@ -847,24 +847,16 @@
 he image
-%0A       
  statist
@@ -1040,22 +1040,16 @@
       %60%60
-image_
 mask%60%60 i
@@ -1171,20 +1171,18 @@
 iters : 
-floa
+in
 t, optio
@@ -1181,24 +1181,25 @@
 t, optional%0A
+ 
        The n
@@ -1207,25 +1207,16 @@
 mber of 
-clipping 
 iteratio
@@ -1228,16 +1228,31 @@
  perform
+ sigma clipping
 , or %60No
@@ -1261,21 +1261,16 @@
 %60 to
- clip
 %0A       
 unti
@@ -1265,16 +1265,22 @@
 %0A       
+ clip 
 until co
@@ -1306,16 +1306,17 @@
 ed (i.e.
+,
  continu
@@ -1330,21 +1330,22 @@
  the
- last
 %0A
+ 
        
+last 
 iter
@@ -1364,16 +1364,62 @@
 nothing)
+ when calculating the image%0A        statistics
 .%0A%0A    R
@@ -1469,17 +1469,16 @@
  : float
-s
 %0A       
@@ -1571,22 +1571,16 @@
 %0A    if 
-image_
 mask is 
@@ -1601,21 +1601,11 @@
     
-assert image_
+if 
 mask
@@ -1611,17 +1611,17 @@
 k.dtype 
-=
+!
 = np.boo
@@ -1625,11 +1625,9 @@
 bool
-, %5C
+:
 %0A   
@@ -1639,15 +1639,25 @@
     
-'image_
+raise TypeError('
 mask
@@ -1683,16 +1683,17 @@
 ndarray'
+)
 %0A       
@@ -1697,29 +1697,21 @@
     
-image = image%5B~image_
+data = data%5B~
 mask
@@ -1744,22 +1744,16 @@
 one and 
-image_
 mask is 
@@ -1773,21 +1773,20 @@
  idx = (
-image
+data
  != mask
@@ -1813,21 +1813,19 @@
     
-image = image
+data = data
 %5Bidx
@@ -1830,21 +1830,20 @@
 dx%5D%0A    
-image
+data
 _clip = 
@@ -1853,21 +1853,20 @@
 ma_clip(
-image
+data
 , sig=si
@@ -1898,21 +1898,20 @@
 dvals = 
-image
+data
 _clip.da
@@ -1914,21 +1914,20 @@
 p.data%5B~
-image
+data
 _clip.ma

Change parameter names in sigmaclip_stats