def test_variable_selection_explanation(self):
        """Test variable selection (feature importance) explanation results and plotting."""
        model = self.helper_create_model(use_encoders=True, add_relative_idx=True)
        series, pc, fc = self.helper_get_input(series_option="multivariate")
        model.fit(series, past_covariates=pc, future_covariates=fc)
        explainer = TFTExplainer(model)
        results = explainer.explain()

        imps = results.get_feature_importances()
        enc_imp = results.get_encoder_importance()
        dec_imp = results.get_decoder_importance()
        stc_imp = results.get_static_covariates_importance()
        imps_direct = [enc_imp, dec_imp, stc_imp]

        imp_names = [
            "encoder_importance",
            "decoder_importance",
            "static_covariates_importance",
        ]
        assert list(imps.keys()) == imp_names
        for imp, imp_name in zip(imps_direct, imp_names):
            assert imps[imp_name].equals(imp)

        enc_expected = pd.DataFrame(
            {
                "linear_target": 1.7,
                "sine_target": 3.1,
                "add_relative_index_futcov": 3.6,
                "constant_pastcov": 3.9,
                "darts_enc_fc_cyc_month_sin_futcov": 5.0,
                "darts_enc_pc_cyc_month_sin_pastcov": 10.1,
                "darts_enc_pc_cyc_month_cos_pastcov": 19.9,
                "constant_futcov": 21.8,
                "darts_enc_fc_cyc_month_cos_futcov": 31.0,
            },
            index=[0],
        )
        # relaxed comparison because M1 chip gives slightly different results than intel chip
        assert ((enc_imp.round(decimals=1) - enc_expected).abs() <= 3).all().all()

        dec_expected = pd.DataFrame(
            {
                "darts_enc_fc_cyc_month_sin_futcov": 5.3,
                "darts_enc_fc_cyc_month_cos_futcov": 7.4,
                "constant_futcov": 24.5,
                "add_relative_index_futcov": 62.9,
            },
            index=[0],
        )
        # relaxed comparison because M1 chip gives slightly different results than intel chip
        assert ((dec_imp.round(decimals=1) - dec_expected).abs() <= 0.6).all().all()

        stc_expected = pd.DataFrame(
            {"num_statcov": 11.9, "cat_statcov": 88.1}, index=[0]
        )
        # relaxed comparison because M1 chip gives slightly different results than intel chip
        assert ((stc_imp.round(decimals=1) - stc_expected).abs() <= 0.1).all().all()

        with patch("matplotlib.pyplot.show") as _:
            _ = explainer.plot_variable_selection(results)
Test variable selection (feature importance) explanation results and plotting.
darts/tests/explainability/test_tft_explainer.py