Exemple #1
0
 def _set_arrays(self):
     imarr, darr = equalize_array_masks(self._unequal_image_subset,
                                        self._unequal_depth_subset)
     fullim = self.img_rds.band_array
     if self.surf_refraction:
         imarr = surface_refraction_correction(imarr)
         fullim = surface_refraction_correction(fullim)
     if self.surf_reflectance:
         acceptable = ['ndarray', 'list', 'tuple']
         if type(self.surf_reflectance).__name__ in acceptable:
             imarr = surface_reflectance_correction(imarr,
                                                    self.surf_reflectance)
             fullim = surface_reflectance_correction(
                 fullim, self.surf_reflectance)
         elif self.surf_reflectance == True:
             imarr = surface_reflectance_correction(imarr)
             fullim = surface_reflectance_correction(fullim)
         else:
             raise TypeError("If self.surf_reflectance doesn't evaluate to \
                         False, then it should be an ndarray, a list, or a \
                         tuple.")
     self.image_subset_array = imarr
     self.full_image_array = fullim
     self.depth_subset_array = darr.squeeze()
     return True
Exemple #2
0
def regression_plot(Z,X,band_names=None,visible_only=True,figsize=(12,7)):
    """
    Produce a figure with a plot for each image band that displays the
    relationship between depth and radiance and gives a visual representation
    of the regression carried out in the `slopes` and `regressions` methods.

    Notes
    -----
    This method doesn't come directly from Lyzenga 1978 but the author of this
    code found it helpful.

    Parameters
    ----------
    Z : np.ma.MaskedArray
        Array of depth values repeated for each band so that Z.shape==X.shape.
        The mask needs to be the same too so that Z.mask==X.mask for all the
        bands.
    X : np.ma.MaskedArray
        The array of log transformed radiance values from equation B1 of
        Lyzenga 1978.

    Returns
    -------
    figure
        A matplotlib figure.
    """
    if band_names is None:
        band_names = ['Band'+str(i+1) for i in range(X.shape[-1])]
    nbands = X.shape[-1]
    if np.atleast_3d(Z).shape[-1] == 1:
        Z = np.repeat(np.atleast_3d(Z), nbands, 2)
    if visible_only:
        fig, axs = plt.subplots( 2, 3, figsize=figsize)
    else:
        fig, axs = plt.subplots( 2, 4, figsize=figsize )
    regs = regressions(Z,X)
    for i, ax in enumerate(axs.flatten()):
        if i > nbands-1:
            continue
        slp, incpt, rval = regs[:,i]
        # print X.shape, Z.shape
        x, y = equalize_array_masks(Z[...,i], X[...,i])
        if x.count() < 2:
            continue
        x, y = x.compressed(), y.compressed()
        # print "i = {}, x.shape = {}, y.shape = {}".format(i, x.shape, y.shape)
        ax.scatter( x, y, alpha=0.1, edgecolor='none', c='gold' )
        smth = lowess(y,x,frac=0.2)
        # ax.plot(smth.T[0],smth.T[1],c='black',alpha=0.5)
        ax.plot(smth.T[0],smth.T[1],c='black',alpha=0.5,linestyle='--')
        reglabel = "m=%.2f, r=%.2f" % (slp,rval)
        f = lambda x: incpt + slp * x
        ax.plot( x, f(x), c='brown', label=reglabel, alpha=1.0 )
        ax.set_title( band_names[i] )
        ax.set_xlabel( r'Depth (m)' )
        ax.set_ylabel( r'$X_i$' )
        ax.legend(fancybox=True, framealpha=0.5)
    plt.tight_layout()
    return fig
Exemple #3
0
def regression_plot(Z, X, band_names=None, visible_only=True, figsize=(12, 7)):
    """
    Produce a figure with a plot for each image band that displays the
    relationship between depth and radiance and gives a visual representation
    of the regression carried out in the `slopes` and `regressions` methods.

    Notes
    -----
    This method doesn't come directly from Lyzenga 1978 but the author of this
    code found it helpful.

    Parameters
    ----------
    Z : np.ma.MaskedArray
        Array of depth values repeated for each band so that Z.shape==X.shape.
        The mask needs to be the same too so that Z.mask==X.mask for all the
        bands.
    X : np.ma.MaskedArray
        The array of log transformed radiance values from equation B1 of
        Lyzenga 1978.

    Returns
    -------
    figure
        A matplotlib figure.
    """
    if band_names is None:
        band_names = ['Band' + str(i + 1) for i in range(X.shape[-1])]
    nbands = X.shape[-1]
    if np.atleast_3d(Z).shape[-1] == 1:
        Z = np.repeat(np.atleast_3d(Z), nbands, 2)
    if visible_only:
        fig, axs = plt.subplots(2, 3, figsize=figsize)
    else:
        fig, axs = plt.subplots(2, 4, figsize=figsize)
    regs = regressions(Z, X)
    for i, ax in enumerate(axs.flatten()):
        if i > nbands - 1:
            continue
        slp, incpt, rval = regs[:, i]
        # print X.shape, Z.shape
        x, y = equalize_array_masks(Z[..., i], X[..., i])
        if x.count() < 2:
            continue
        x, y = x.compressed(), y.compressed()
        # print "i = {}, x.shape = {}, y.shape = {}".format(i, x.shape, y.shape)
        ax.scatter(x, y, alpha=0.1, edgecolor='none', c='gold')
        smth = lowess(y, x, frac=0.2)
        # ax.plot(smth.T[0],smth.T[1],c='black',alpha=0.5)
        ax.plot(smth.T[0], smth.T[1], c='black', alpha=0.5, linestyle='--')
        reglabel = "m=%.2f, r=%.2f" % (slp, rval)
        f = lambda x: incpt + slp * x
        ax.plot(x, f(x), c='brown', label=reglabel, alpha=1.0)
        ax.set_title(band_names[i])
        ax.set_xlabel(r'Depth (m)')
        ax.set_ylabel(r'$X_i$')
        ax.legend(fancybox=True, framealpha=0.5)
    plt.tight_layout()
    return fig
 def _set_arrays(self):
     # get prediction and true arrays
     parr, tarr = self.pred_rds.band_array.squeeze(), self.true_rds.band_array.squeeze()
     if type(self.pred_range).__name__ != 'NoneType':
         parr = np.ma.masked_outside(parr, *self.pred_range)
     if type(self.true_range).__name__ != 'NoneType':
         tarr = np.ma.masked_outside(tarr, *self.true_range)
     parr, tarr = equalize_array_masks(parr, tarr)
     self.pred_arr = parr
     self.true_arr = tarr
     return True
Exemple #5
0
 def _set_arrays(self):
     # get prediction and true arrays
     parr, tarr = self.pred_rds.band_array.squeeze(
     ), self.true_rds.band_array.squeeze()
     if type(self.pred_range).__name__ != 'NoneType':
         parr = np.ma.masked_outside(parr, *self.pred_range)
     if type(self.true_range).__name__ != 'NoneType':
         tarr = np.ma.masked_outside(tarr, *self.true_range)
     parr, tarr = equalize_array_masks(parr, tarr)
     self.pred_arr = parr
     self.true_arr = tarr
     return True
Exemple #6
0
 def lyzenga_depth_estimation(self, Rinf=None, bands=None, n_std=0,
                                 n_jobs=4):
     """
     This will implement the linear depth estimation method described in
     Lyzenga et al. 2006. This doc string needs a bit more detail but I don't
     have time right now. Check `OpticalRS.Lyzenga2006` for more detail. This
     method just wraps some of the code from that module to make it easier to
     run.
     """
     if bands is None:
         bands = self.nbands
     if Rinf is None:
         Rinf = deep_water_means(self.imarr[...,:bands], n_std=n_std)
     X = np.ma.log(self.imarr[...,:bands] - Rinf)
     X = equalize_band_masks(X)
     # need to re-equalize, might have lost pixels in log transform
     Xtrain, deparr = equalize_array_masks(X, self.known_depth_arr)
     return fit_and_predict(Xtrain, deparr, X, n_jobs=n_jobs)
Exemple #7
0
def regressions(Z, X):
    """
    Carry out the regression discussed in Appendix B of Lyzenga 1978. `Z` is
    the depths and `X` is the log radiance values. X = a - b z is the
    formula so depth is the x in the regression and radiance is the y.

    Parameters
    ----------
    Z : np.ma.MaskedArray
        Array of depth values.
    X : np.ma.MaskedArray
        The array of log transformed radiance values from equation B1 of
        Lyzenga 1978.

    Returns
    -------
    np.array
        A 3 x N-bands array containing the slopes, intercepts, and r_values
        of the regressions. In terms of equation B1 from Lyzenga 1978, that
        would be (b_i, a_i, r_value_i) for each band.
    """
    nbands = X.shape[-1]
    if np.atleast_3d(Z).shape[-1] == 1:
        Z = np.repeat(np.atleast_3d(Z), nbands, 2)
    slopes = []
    intercepts = []
    rvals = []
    for band in range(nbands):
        z, x = equalize_array_masks(Z[..., band], X[..., band])
        # print z.count(), x.count()
        try:
            slope,intercept,r_value,p_value,std_err = linregress(z.compressed(),\
                                                                 x.compressed())
        except IndexError:
            slope, intercept, r_value, p_value, std_err = np.repeat(
                np.nan, 5, 0)
        # eq B1 is X=a-bz
        slopes.append(slope)
        intercepts.append(intercept)
        rvals.append(r_value)
    return np.array((slopes, intercepts, rvals))
Exemple #8
0
def regressions(Z,X):
    """
    Carry out the regression discussed in Appendix B of Lyzenga 1978. `Z` is
    the depths and `X` is the log radiance values. X = a - b z is the
    formula so depth is the x in the regression and radiance is the y.

    Parameters
    ----------
    Z : np.ma.MaskedArray
        Array of depth values.
    X : np.ma.MaskedArray
        The array of log transformed radiance values from equation B1 of
        Lyzenga 1978.

    Returns
    -------
    np.array
        A 3 x N-bands array containing the slopes, intercepts, and r_values
        of the regressions. In terms of equation B1 from Lyzenga 1978, that
        would be (b_i, a_i, r_value_i) for each band.
    """
    nbands = X.shape[-1]
    if np.atleast_3d(Z).shape[-1] == 1:
        Z = np.repeat(np.atleast_3d(Z), nbands, 2)
    slopes = []
    intercepts = []
    rvals = []
    for band in range(nbands):
        z, x = equalize_array_masks(Z[...,band], X[...,band])
        # print z.count(), x.count()
        try:
            slope,intercept,r_value,p_value,std_err = linregress(z.compressed(),\
                                                                 x.compressed())
        except IndexError:
            slope,intercept,r_value,p_value,std_err = np.repeat(np.nan, 5, 0)
        # eq B1 is X=a-bz
        slopes.append( slope )
        intercepts.append( intercept )
        rvals.append( r_value )
    return np.array((slopes,intercepts,rvals))
 def _set_arrays(self):
     imarr, darr = equalize_array_masks(self._unequal_image_subset, self._unequal_depth_subset)
     fullim = self.img_rds.band_array
     if self.surf_refraction:
         imarr = surface_refraction_correction(imarr)
         fullim = surface_refraction_correction(fullim)
     if self.surf_reflectance:
         acceptable = ['ndarray','list','tuple']
         if type(self.surf_reflectance).__name__ in acceptable:
             imarr = surface_reflectance_correction(imarr, self.surf_reflectance)
             fullim = surface_reflectance_correction(fullim, self.surf_reflectance)
         elif self.surf_reflectance == True:
             imarr = surface_reflectance_correction(imarr)
             fullim = surface_reflectance_correction(fullim)
         else:
             raise TypeError("If self.surf_reflectance doesn't evaluate to \
                         False, then it should be an ndarray, a list, or a \
                         tuple.")
     self.image_subset_array = imarr
     self.full_image_array = fullim
     self.depth_subset_array = darr.squeeze()
     return True