Пример #1
0
def create_classifier(x, z, toe, window=40, min_buffer=40, max_buffer=200):
    """
    Create dune toe classifier.

    ...

    Parameters
    ----------
    x : ndarray
        Array of cross-shore locations of size (m,).
    z : ndarray
        Array of elevations matching x. May be of size (m,) or (m,n).
    toe : ndarray
        Array of dune toe locations of size (n,).
    window : int, default 40
        Size of the window for training data.
    min_buffer : int, default 40
        Minimum buffer around the real dune toe.
    max_buffer : int, default 200
        Maximum buffer range.

    Returns
    -------
    clf : scikit-learn classifier
        Created random forest classifier.
    """

    # Pre-processing
    z = ds.interp_nan(x, z)  # interp nan
    xx = np.arange(np.min(x), np.max(x) + 0.5, 0.5)
    z = ds.interp_to_grid(x, xx, z)  # interp to grid
    toe = ds.interp_toe_to_grid(x, xx, toe)
    z = ds.moving_average(z, 5)  # apply moving average to smooth
    z = ds.diff_data(z, 1)  # differentiate

    # Create data
    features, labels = create_training_data(xx, z, toe, window, min_buffer,
                                            max_buffer)

    # Build classifier
    clf = RandomForestClassifier(n_estimators=100,
                                 criterion="gini",
                                 random_state=123).fit(features,
                                                       labels.ravel())
    return clf
Пример #2
0
    def __init__(self, x, z, window_size=5):
        """
        A class used to represent a 2D beach profile transect.

        ...

        Parameters
        ----------
        x : ndarray
            Array of cross-shore locations of size (m,).
        z : ndarray
            Array of elevations matching x. May be of size (m,) or (m,n).
        window_size : int, default 5
            Size of window used to smooth z with a moving average.

        Attributes
        ----------
        x_orig : ndarray
            Original input array of cross-shore locations.
        z_orig : ndarray
            Original array of profile elevations matching x_orig.
        x : ndarray
            x_orig interpolated to 0.5 m grid.
        z : ndarray
            z_orig interpolated to 0.5 m grid and smoothed by a moving average with
            window size smooth_window.

        Methods
        -------
        predict_dunetoe_ml(self, clf_name, no_of_output=1, dune_crest='rr', **kwargs)
        predict_dunetoe_mc(self, dune_crest='rr', shoreline=True, window_size=None, **kwargs)
        predict_dunetoe_pd(self, dune_crest=None, shoreline=None, **kwargs)
        predict_dunetoe_rr(self, window_size=11, threshold=0.2, water_level=0)
        predict_dunecrest(self, method="max", window_size=50, threshold=0.8, water_level=0)
        predict_shoreline(self, water_level=0, dune_crest='rr', **kwargs)

        """
        assert isinstance(x, np.ndarray) & (
            np.ndim(x) == 1), 'x should be of type ndarray and shape (m,).'
        assert (np.ndim(x) == 1), 'x should be a 1-d array of size (m,).'
        assert (len(x) > 1), 'x should have length > 1.'
        assert isinstance(z, np.ndarray), 'z should be of type ndarray.'
        assert isinstance(window_size, int) & \
               (window_size > 0) & \
               (window_size < len(x)), f'window_size must be int between 0 and {len(x)}.'

        # Ensure inputs are row vectors
        x = np.atleast_1d(x)
        z = np.atleast_2d(z)
        if len(x) not in z.shape:
            raise ValueError(
                f'Input x of shape ({x.shape[0]},) must share a dimension with input z which has shape {z.shape[0], z.shape[1]}.'
            )
        if x.shape[0] != z.shape[1]:
            z = z.T

        # Store original inputs
        self.x = x
        self.z = z

        # Interp nan values
        z = ds.interp_nan(x, z)
        flag = np.polyfit(x, z.T, 1)[0]
        if np.any(flag > 0):
            #raise Warning(f'Input profiles should be oriented from landward (left) to seaward (right), '
            #             f'some inputted profiles appear to have the sea on the left. This may cause errors.')
            print(
                f'Input profiles should be oriented from landward (left) to seaward (right), '
                f'some inputted profiles appear to have the sea on the left. This may cause errors.'
            )
        # Interp to 0.5 m grid
        self.x_interp = np.arange(np.min(x), np.max(x) + 0.5, 0.5)
        z = ds.interp_to_grid(x, self.x_interp, z)

        # Apply moving average to smooth data
        z = ds.moving_average(z, window_size)

        # Store transformed inputs
        self.z_interp = z