!pip install -U tfx

# getting the code directly from the repo
x = !pwd

if 'feature_selection' not in str(x):
  !git clone  -b main https://github.com/deutranium/tfx-addons.git
  %cd tfx-addons/tfx_addons/feature_selection

"""
Explanation: <a href="https://colab.research.google.com/github/deutranium/tfx-addons/blob/main/tfx_addons/feature_selection/example/Iris_example_colab.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>
TFX Feature Selection Component
You may find the source code for the same here
This example demonstrate the use of feature selection component. This project allows the user to select different algorithms for performing feature selection on datasets artifacts in TFX pipelines
Base code taken from: https://github.com/tensorflow/tfx/blob/master/docs/tutorials/tfx/components_keras.ipynb
Setup
Install TFX
Note: In Google Colab, because of package updates, the first time you run this cell you must restart the runtime (Runtime > Restart runtime ...).
End of explanation
"""


import os
import pprint
import tempfile
import urllib

import absl
import tensorflow as tf
import tensorflow_model_analysis as tfma
tf.get_logger().propagate = False
import importlib
pp = pprint.PrettyPrinter()

from tfx import v1 as tfx
import importlib
from tfx.components import CsvExampleGen
from tfx.orchestration.experimental.interactive.interactive_context import InteractiveContext

%load_ext tfx.orchestration.experimental.interactive.notebook_extensions.skip

# importing the feature selection component
from component import FeatureSelection


# This is the root directory for your TFX pip package installation.
_tfx_root = tfx.__path__[0]

"""
Explanation: Import packages
Importing the necessary packages, including the standard TFX component classes
End of explanation
"""


# getting the dataset
_data_root = tempfile.mkdtemp(prefix='tfx-data')
DATA_PATH = 'https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv'
             
_data_filepath = os.path.join(_data_root, "data.csv")
urllib.request.urlretrieve(DATA_PATH, _data_filepath)

"""
Explanation: Palmer Penguins example pipeline
Download Example Data
We download the example dataset for use in our TFX pipeline.
The dataset we're using is the Palmer Penguins dataset which is also used in other
TFX examples.
There are four numeric features in this dataset:

culmen_length_mm
culmen_depth_mm
flipper_length_mm
body_mass_g

All features were already normalized to have range [0,1]. We will build a
that selects 2 features to be eliminated from the dataset in other to improve the performance of the mode in predicting the species of penguins.
End of explanation
"""


context = InteractiveContext()

#create and run exampleGen component
example_gen = CsvExampleGen(input_base=_data_root )
context.run(example_gen)

#create and run statisticsGen component
statistics_gen = tfx.components.StatisticsGen(
    examples=example_gen.outputs['examples'])
context.run(statistics_gen)

# using the feature selection component
#feature selection component

feature_selector = FeatureSelection(orig_examples = example_gen.outputs['examples'],
                                   module_file='example.modules.iris_module_file')
context.run(feature_selector)

# Display Selected Features
context.show(feature_selector.outputs['feature_selection']._artifacts[0])

"""
Explanation: Run TFX Components
In the cells that follow, we create TFX components one-by-one and generates example using exampleGen component.
End of explanation
"""


context.show(feature_selector.outputs['updated_data']._artifacts[0])

"""
Explanation: As seen above, .selected_features contains the features selected after running the component with the speified parameters.
To get the info about updated Example artifact, one can view it as follows:
End of explanation
"""


