{"cells":[{"metadata":{"_uuid":"0215f0d55d3e1ab3ffa1fc8dfbb7be0e8021478f"},"cell_type":"markdown","source":"Singal resampling from 800K to 1600 time steps using Scipy signal package and FFT module. I do not have expertise in signal processing. However, this approach could help someone to handle the large sequence data and to do rapid experiments."},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom scipy import signal\nimport warnings\nwarnings.filterwarnings('ignore')\n\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"df_train = pd.read_parquet('../input/train.parquet')\nprint(df_train.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"52dbb7034ac8eab2c521fe83893c420ff48a535f"},"cell_type":"code","source":"train_meta = pd.read_csv('../input/metadata_train.csv')\nprint(train_meta.shape)\nprint(train_meta.head(3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"1fa37f92124054507109ac9e7f34639b21a7205e"},"cell_type":"code","source":"train_meta_pos = train_meta[train_meta['target'] == 1]\ntrain_meta_neg = train_meta[train_meta['target'] == 0]\nprint(train_meta_pos.shape)\nprint(train_meta_neg.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"5730a1fae468abf5e8f6a7af2993bb87b9ebd6f9"},"cell_type":"code","source":"train_meta_pos_p0 = train_meta_pos[train_meta_pos['phase'] == 0]\ntrain_meta_pos_p0.head(10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"303399b59bb709e7be4d07db74fccb682f74cb4b"},"cell_type":"code","source":"# Negative signal\ntrain_meta_neg_p0 = train_meta_neg[train_meta_neg['phase'] == 0]\ntrain_meta_neg_p0.head(10)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ac75595f88dd9867bf284f1b669513190a086113"},"cell_type":"markdown","source":"Phase 0, Phase 1 and Phase 2 for an id_measurement may or may not have target value 1 i.e. individual phase line will casue discharge. I took Phase 0 signals that has target value 1"},{"metadata":{"trusted":true,"_uuid":"00a69295189644f0addfbcec73c3c43162e41ba8"},"cell_type":"code","source":"# Take some samples that has postive and negative target\ndf_train_sample = df_train.iloc[:, 201:270]\nprint(df_train_sample.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"740aa4a3e438792c9edba46146abc2ccc5571937"},"cell_type":"code","source":"df_train_sample.head(5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"931d0f9581cf39c28a7e9ffcb5aeac60d1ea6123"},"cell_type":"code","source":"# Plot the given data points to see how its trend looks like\ndf_train_sample.iloc[:, :3].plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"66f2672b3a5b1854d455006861f9f959e781c7b0"},"cell_type":"code","source":"# Function to to FFT and reduce the dimensions\ndef sample_signals(df):\n    cols = df.columns.values\n    b, a = signal.butter(4, 0.03, analog=False)\n    df_t = []\n    for idx in range(df.shape[1]):\n        sg = np.squeeze(df.iloc[:, idx:idx+1], axis=1)\n        sg_ff = signal.filtfilt(b, a, sg)\n        sg_rs = signal.resample(sg_ff, 16*2*50)\n        df_t.append(sg_rs)\n    df_t = np.asarray(df_t).T\n    df_t = pd.DataFrame(df_t, columns=cols)\n    return df_t","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"474b869ae9c0d1962baec2593b7c2227837c66cd"},"cell_type":"code","source":"# Show how the processed signals look like\nsample1 = sample_signals(df_train_sample.iloc[:,:3])\nprint(sample1.shape)\nsample1.plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"2daec74a5523193fb754a255fcbbe8c8d78c471d"},"cell_type":"code","source":"# Comparing given signals and the processed one - Positive signal\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16,6))\nax1.plot(df_train_sample.iloc[:, :3])\nax2.plot(sample1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"21eeb0e7f060e6286dd8068324567ab7537876c1"},"cell_type":"code","source":"# Negative signal - target = 0\nsample2 = sample_signals(df_train_sample.iloc[:,3:6])\ndf_train_sample.iloc[:,3:6].plot()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"bb5d3ae5f3d57b4fcff4015d324cc1063a7880a9"},"cell_type":"code","source":"# Comparing given signals and the processed one - Negative signal\nfig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16,6))\nax1.plot(df_train_sample.iloc[:, 3:6])\nax2.plot(sample2)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"36d9d45dd209f2ef750b2584a719ab18f2da4e20"},"cell_type":"markdown","source":"Future work,\n* Check if the preprocessing doesn't lose any information \n* Identify and emphasize positive and negative signal\n* Any thoughts?\n\nPlease feel free to comment if you have any concerns. Any help in singal processing is appreciated!"},{"metadata":{"trusted":true,"_uuid":"1d9e10af3bf90493e2a6929ee210b1c4dec05716"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}