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)

    s = SlopeTransformer(num_intervals=2).fit(X)
    res = s.transform(X)
    orig = convert_list_to_dataframe([[(5 + math.sqrt(41)) / 4,
                                       (1 + math.sqrt(101)) / -10]])
    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)
    s = s.fit(X)
    res = s.transform(X)
    orig = convert_list_to_dataframe([[
        (104.8 + math.sqrt(14704.04)) / 61,
        (143.752 + math.sqrt(20790.0775)) / -11.2,
    ]])
    orig.columns = X.columns
    assert check_if_dataframes_are_equal(res, orig)
def test_bad_input_args(bad_num_intervals):
    X = _make_nested_from_array(np.ones(10), n_instances=10, n_columns=1)

    if not isinstance(bad_num_intervals, int):
        with pytest.raises(TypeError):
            SlopeTransformer(
                num_intervals=bad_num_intervals).fit(X).transform(X)
    else:
        with pytest.raises(ValueError):
            SlopeTransformer(
                num_intervals=bad_num_intervals).fit(X).transform(X)
Beispiel #3
0
def test_bad_input_args(bad_num_intervals):
    X = generate_df_from_array(np.ones(10), n_rows=10, n_cols=1)

    if not isinstance(bad_num_intervals, int):
        with pytest.raises(TypeError):
            SlopeTransformer(num_intervals=bad_num_intervals) \
                            .fit(X).transform(X)
    else:
        with pytest.raises(ValueError):
            SlopeTransformer(num_intervals=bad_num_intervals) \
                            .fit(X).transform(X)
def test_slope_performs_correcly_along_each_dim():

    X = generate_df_from_array(np.array([4, 6, 10, 12, 8, 6, 5, 5]),
                               n_rows=1, n_cols=2)

    s = SlopeTransformer(num_intervals=2).fit(X)
    res = s.transform(X)
    orig = convert_list_to_dataframe([[(5+math.sqrt(41))/4,
                                       (1+math.sqrt(101))/-10],
                                      [(5+math.sqrt(41))/4,
                                       (1+math.sqrt(101))/-10]])
    orig.columns = X.columns
    assert check_if_dataframes_are_equal(res, orig)
def test_output_dimensions(num_intervals, corr_series_length):

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

    s = SlopeTransformer(num_intervals=num_intervals).fit(X)
    res = s.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
Beispiel #6
0
    def _get_transformer(self, tName):
        """
        Function to 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.")