{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nfrom zipfile import ZipFile\nimport matplotlib.pyplot as plt\nfrom scipy.signal import butter, lfilter, freqz\nfrom sklearn.metrics import f1_score\nfrom sklearn.model_selection import cross_val_score, cross_val_predict\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.pipeline import make_pipeline\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.svm import SVC\nfrom sklearn.metrics import accuracy_score, roc_auc_score, euclidean_distances\nfrom catboost import CatBoostClassifier\nfrom scipy.signal import decimate, fftconvolve\nfrom numba import jit","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_subject_data_df(zip_file, subject_id, series_ids, folder):\n    df = pd.DataFrame()\n    for series_id in series_ids:\n        file_name = f'{folder}/subj{subject_id}_series{series_id}_data.csv'\n        unzipped_file = zip_file.open(file_name)\n        tmp_df = pd.read_csv(unzipped_file, index_col='id')\n        df = pd.concat([df, tmp_df])\n    return df\n\ndef get_subject_target_df(zip_file, subject_id):\n    df = pd.DataFrame()\n    for series_id in range(1,9):\n        file_name = f'train/subj{subject_id}_series{series_id}_events.csv'\n        unzipped_file = zip_file.open(file_name)\n        tmp_df = pd.read_csv(unzipped_file, index_col='id')\n        df = pd.concat([df, tmp_df])\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def butter_highpass(cutoff, fs, order=5):\n    nyq = 0.5 * fs\n    normal_cutoff = cutoff / nyq\n    b, a = butter(order, normal_cutoff, btype='highpass', analog=False)\n    return b, a\n\ndef butter_highpass_filter(data, cutoff, fs, order=5):\n    b, a = butter_highpass(cutoff, fs, order=order)\n    y = lfilter(b, a, data, axis=0)\n    return y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def butter_lowpass(cutoff, fs, order=5):\n    nyq = 0.5 * fs\n    normal_cutoff = cutoff / nyq\n    b, a = butter(order, normal_cutoff, btype='lowpass', analog=False)\n    return b, a\n\ndef butter_lowpass_filter(data, cutoff, fs, order=5):\n    b, a = butter_lowpass(cutoff, fs, order=order)\n    freqz(b,a)\n    y = lfilter(b, a, data, axis=0)\n    return y","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def preprocess_data(x, low_cut_off, high_cut_off, fs):\n    x = butter_lowpass_filter(x, low_cut_off, fs)\n    x = butter_highpass_filter(x, high_cut_off, fs)\n    return x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"subject_ids = range(1,13)\nsubject_ids = subject_ids[:1]\ntrain_zip_path = '/kaggle/input/grasp-and-lift-eeg-detection/train.zip'\ntest_zip_path = '/kaggle/input/grasp-and-lift-eeg-detection/test.zip'\ntrain_zip = ZipFile(train_zip_path)\ntest_zip = ZipFile(test_zip_path)\ncb = LogisticRegression(solver='lbfgs', max_iter=400)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"### PREDICTION BASED ON rank-r appoximation of the sliding window ###\nsubject_ids = range(1,13)\nsubject_ids = [1]\naucs_sl = []\ndroprate = 100\nh = 16\nl = 0.2\n\ndef get_sliding_window (i, length, data):\n    return data[i-length:i+1,:]\n\nfor subject_id in subject_ids:\n    \n    x_train = get_subject_data_df(train_zip, subject_id, range(1, 9), 'train')\n    target_df = get_subject_target_df(train_zip, subject_id)\n    x_train = preprocess_data(x_train, l, h, 500)\n    \n    r = 32\n    \n    sliding_window = [50,100,200,500]\n\n    \n    for slide_l in sliding_window:\n        \n        auc = []\n        \n        start = max(slide_l, droprate)\n        indices = list(range(x_train.shape[0]))[start::droprate]\n        x = np.zeros((len(indices),(slide_l+1)*32))\n        \n        for i in range (len(indices)):\n            window = get_sliding_window (indices[i], slide_l, x_train)\n            u,s,vh = np.linalg.svd(window, False)\n            u = u[:,:r]\n            s = s[:r]\n            vh = vh[:r, :]\n            new = u @ np.diag(s) @ vh\n            x [i,:] = new.flatten()\n\n        scaler = StandardScaler()\n        scaler.fit(x)\n        x = scaler.transform(x)\n\n        print(f'done with training set {slide_l}')\n\n        for i in range(6):\n            y = target_df[start::droprate].values[:, i]\n            score = cross_val_score(cb, x, y, cv=2, scoring='roc_auc').mean()\n            auc.append(score)\n\n            # print ('done for model', i)\n            \n        aucs_sl.append((slide_l, np.array(auc).mean()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"aucs_sl_np = np.array(aucs_sl)\ndf = pd.DataFrame(data=aucs_sl_np, columns=['window size', 'roc auc'])\ndf.to_csv('r32_best_filters.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}