from pygoose import *

import itertools

"""
Explanation: Feature: Meta-Features Using Pairwise Diffs/Ratios
Generate additional meta-features derived from pairwise differences and ratios of most important features (helpful for tree-based models).
Imports
This utility package imports numpy, pandas, matplotlib and a helper kg module into the root namespace.
End of explanation
"""


project = kg.Project.discover()

"""
Explanation: Config
Automatically discover the paths to various data folders and compose the project structure.
End of explanation
"""


feature_list_id = 'meta_pairwise_interactions'

"""
Explanation: Identifier for storing these features on disk and referring to them later.
End of explanation
"""


feature_lists = [
    # Place feature lists here.
]

"""
Explanation: Feature lists containing the most important features.
End of explanation
"""


most_important_features = [
    # Place features here.
]

"""
Explanation: Names of the features to use for pairwise interactions.
End of explanation
"""


df_train, df_test, _ = project.load_feature_lists(feature_lists)

"""
Explanation: Read data
End of explanation
"""


feature_names = []

for a, b in itertools.combinations(most_important_features, 2):
    diff_feature_name = f'diff_{a}_{b}'
    df_train[diff_feature_name] = df_train[a] - df_train[b]
    df_test[diff_feature_name] = df_test[a] - df_test[b]
    feature_names.append(diff_feature_name)
    
    ratio_feature_name = f'ratio_{a}_{b}'
    df_train[ratio_feature_name] = df_train[a] / df_train[b]
    df_test[ratio_feature_name] = df_test[a] / df_test[b]
    feature_names.append(ratio_feature_name)

"""
Explanation: Build features
End of explanation
"""


features_to_keep = feature_names

X_train = df_train[features_to_keep].values
X_test = df_test[features_to_keep].values

project.save_features(X_train, X_test, feature_names, feature_list_id)

"""
Explanation: Save features
End of explanation
"""


