def test_bad_num_bins(bad_num_bins):
    X = _make_nested_from_array(np.ones(10), n_instances=10, n_columns=1)

    if not isinstance(bad_num_bins, int):
        with pytest.raises(TypeError):
            HOG1DTransformer(num_bins=bad_num_bins).fit(X).transform(X)
    else:
        with pytest.raises(ValueError):
            HOG1DTransformer(num_bins=bad_num_bins).fit(X).transform(X)
def test_bad_scaling_factor(bad_scaling_factor):

    X = _make_nested_from_array(np.ones(10), n_instances=10, n_columns=1)

    if not isinstance(bad_scaling_factor, numbers.Number):
        with pytest.raises(TypeError):
            HOG1DTransformer(
                scaling_factor=bad_scaling_factor).fit(X).transform(X)
    else:
        HOG1DTransformer(scaling_factor=bad_scaling_factor).fit(X).transform(X)
def test_hog1d_performs_correcly_along_each_dim():

    X = _make_nested_from_array(np.array([4, 6, 10, 12, 8, 6, 5, 5]),
                                n_instances=1,
                                n_columns=2)

    h = HOG1DTransformer().fit(X)
    res = h.transform(X)
    orig = convert_list_to_dataframe([
        [0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0],
        [0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0],
    ])
    orig.columns = X.columns
    assert check_if_dataframes_are_equal(res, orig)
def test_output_dimensions(num_bins, corr_series_length):

    X = _make_nested_from_array(np.ones(13), n_instances=10, n_columns=1)

    h = HOG1DTransformer(num_bins=num_bins).fit(X)
    res = h.transform(X)

    # get the dimension of the generated dataframe.
    act_time_series_length = res.iloc[0, 0].shape[0]
    num_rows = res.shape[0]
    num_cols = res.shape[1]

    assert act_time_series_length == corr_series_length
    assert num_rows == 10
    assert num_cols == 1
def test_output_of_transformer():

    X = _make_nested_from_array(np.array([4, 6, 10, 12, 8, 6, 5, 5]),
                                n_instances=1,
                                n_columns=1)

    h = HOG1DTransformer().fit(X)
    res = h.transform(X)
    orig = convert_list_to_dataframe(
        [[0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0]])
    orig.columns = X.columns
    assert check_if_dataframes_are_equal(res, orig)

    X = _make_nested_from_array(np.array(
        [-5, 2.5, 1, 3, 10, -1.5, 6, 12, -3, 0.2]),
                                n_instances=1,
                                n_columns=1)
    h = h.fit(X)
    res = h.transform(X)
    orig = convert_list_to_dataframe(
        [[0, 0, 0, 0, 4, 1, 0, 0, 0, 0, 2, 0, 2, 1, 0, 0]])
    orig.columns = X.columns
    assert check_if_dataframes_are_equal(res, orig)
Beispiel #6
0
    def _get_transformer(self, tName):
        """Extract the appropriate transformer.

        Parameters
        ----------
        self   : the ShapeDTW object.
        tName  : the name of the required transformer.

        Returns
        -------
        output : Base Transformer object corresponding to the class
                 (or classes if its a compound transformer) of the
                 required transformer. The transformer is
                 configured with the parameters given in self.metric_params.

        throws : ValueError if a shape descriptor doesn't exist.
        """
        parameters = self.metric_params

        tName = tName.lower()

        if parameters is None:
            parameters = {}

        parameters = {k.lower(): v for k, v in parameters.items()}

        self._check_metric_params(parameters)

        if tName == "raw":
            return None
        elif tName == "paa":
            num_intervals = parameters.get("num_intervals_paa")
            if num_intervals is None:
                return PAA()
            return PAA(num_intervals)
        elif tName == "dwt":
            num_levels = parameters.get("num_levels_dwt")
            if num_levels is None:
                return DWTTransformer()
            return DWTTransformer(num_levels)
        elif tName == "slope":
            num_intervals = parameters.get("num_intervals_slope")
            if num_intervals is None:
                return SlopeTransformer()
            return SlopeTransformer(num_intervals)
        elif tName == "derivative":
            return DerivativeSlopeTransformer()
        elif tName == "hog1d":
            num_intervals = parameters.get("num_intervals_hog1d")
            num_bins = parameters.get("num_bins_hog1d")
            scaling_factor = parameters.get("scaling_factor_hog1d")

            # All 3 paramaters are None
            if num_intervals is None and num_bins is None and scaling_factor is None:
                return HOG1DTransformer()

            # 2 parameters are None
            if num_intervals is None and num_bins is None:
                return HOG1DTransformer(scaling_factor=scaling_factor)
            if num_intervals is None and scaling_factor is None:
                return HOG1DTransformer(num_bins=num_bins)
            if num_bins is None and scaling_factor is None:
                return HOG1DTransformer(num_intervals=num_intervals)

            # 1 parameter is None
            if num_intervals is None:
                return HOG1DTransformer(scaling_factor=scaling_factor,
                                        num_bins=num_bins)
            if scaling_factor is None:
                return HOG1DTransformer(num_intervals=num_intervals,
                                        num_bins=num_bins)
            if num_bins is None:
                return HOG1DTransformer(scaling_factor=scaling_factor,
                                        num_intervals=num_intervals)

            # All parameters are given
            return HOG1DTransformer(
                num_intervals=num_intervals,
                num_bins=num_bins,
                scaling_factor=scaling_factor,
            )
        else:
            raise ValueError("Invalid shape desciptor function.")