##
## 1. Feature selection - filtering methods
##
## Read the documentation at 
## http://scikit-learn.org/stable/modules/feature_selection.html
## sections 1.11.1, 1.11.2

import numpy as np
import matplotlib.pyplot as plt

## Apply the method described to gene expression data:
##
## Download data file mda.zip from IS (sources/mda.zip)

Xtr = np.load('X-train.npy')  # 22283 variables, 130 observations
Ytr = np.load('Y-train.npy')  # Ytr[:,0] - ER positive; Ytr[:,1] - pCR
Ytr = Ytr.astype('int32')     # make sure the labels are INTs

Xts = np.load('X-test.npy')  # 22283 variables, 100 observations
Yts = np.load('Y-test.npy')  # Ytr[:,0] - ER positive; Ytr[:,1] - pCR
Yts = Yts.astype('int32')     # make sure the labels are INTs


from sklearn.feature_selection import SelectKBest, SelectFwe
from sklearn.feature_selection import chi2

# variables related to ER status prediction:
# (X must contain non negative values)
X_tr_1 = SelectKBest(chi2, k=10).fit_transform(Xtr-Xtr.min()+0.1, Ytr[:,0]) 
X_tr_2 = SelectFwe(chi2, alpha=0.1).fit_transform(Xtr-Xtr.min()+0.1, Ytr[:,0])

print(X_tr_2.shape)  # how many variables are selected?

## If you want to apply the transformation to a validation set:
## -first fit the model:
s2 = SelectFwe(chi2, alpha=0.1).fit(Xtr-Xtr.min()+0.1, Ytr[:,0])
## -then get the set of variables as a mask vector:
s2.get_support()  # True will stand for a selected variable...


##
## 2. RFE - see section 1.11.3
##
## and the example at 
## http://scikit-learn.org/stable/auto_examples/plot_rfe_with_cross_validation.html#example-plot-rfe-with-cross-validation-py


##
## TODO: Implement the cross validation approach (similar to the example above) for the
## case of feature filtering (part 1) with chi2 statistic, when the number of
## features has to be optimized.
## Apply both RFE and the implemented procedure to the gene expression data
## (predict ER-status, Y[:,0])
