{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport pyarrow.parquet as pq #data reading\nimport os\nimport matplotlib.pyplot as plt # visulation\n\n#Efficient Implementation for moving average\ndef runningMeanFast(x, N):\n    return np.convolve(x, np.ones((N,))/N)[(N-1):]\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n\n# Any results you write to the current directory are saved as output.\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"edb8152f7aef4a4a33ac630f1e38e0bb05d39195"},"cell_type":"code","source":"#In the power signals there are lot of verticals lines. These flucuations are seen in non faulty and faulty lines.\n#Let us examine one of these signals. \n#The signal 2576 have lot of these patterns and till not faulty.\ncolumn = str((3*2576)+0)\nsignalOrg = pd.read_parquet('../input/train.parquet', engine='pyarrow', columns=[column]) \nplt.figure(figsize=(24, 10))\nplt.plot(signalOrg,'g')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3f06e06e2d75fca31a026827dd1fdc86de0f8227"},"cell_type":"code","source":"#let us zoom in for vertical line around 450,000\nplt.figure(figsize=(24, 10))\nplt.plot(signalOrg[495000:497000],'g')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"3fb1e3aaaa9fcfcd521e29d9af1c6f11b6fa7a37"},"cell_type":"code","source":"#It is clearly visible that the voltage is following exponential decay pattern.\n#let us define a pattern of these behaviour. \n# These pattern has approx. 10 length.\npattern = [42,-34,27,-19,14,-10,7,-5,3,-2,1] \npattern = pattern - np.mean(pattern)\nimg=plt.plot(np.asarray(pattern),'b')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cb7123c9552dc5e9e2f1a03a550bd287b5718a2b"},"cell_type":"code","source":"#Convluing with pattern will help detect  presense of these pattern. Let us see how it performs.\n#Before we convul, we need to get signal stationary. We substract moving average from signal from the same.\nsignal = pd.read_parquet('../input/train.parquet', engine='pyarrow', columns=[column]) \nsignal = signal[column]\n\nmvSignal = runningMeanFast(signal,30)\ncon = np.convolve(signal-mvSignal,pattern,mode ='same')\nmv = runningMeanFast(abs(con),10)\nmv = np.roll(mv,7)\nplt.figure(figsize=(24, 10))\nplt.plot(mv,'b+')\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6ff1c39c0c06d515c1d3d385603693e4bb7fece7"},"cell_type":"code","source":"#Let us replace the variations with moving average\nsignal [mv > 200] = mvSignal[mv > 200]\nplt.figure(figsize=(24, 10))\nimg=plt.plot(np.asarray(signalOrg),'r-')\nimg=plt.plot(np.asarray(signal),'g-')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"06e4c72066857a76acb932aa573a1fce9139b1e1"},"cell_type":"markdown","source":"The red portion is removed from signal. \nThis denosing has **preserved all other variations** ( before 200000 and 600000). \nWe can define more patterns and extract the features.\nWe can use **CNN with filter size of 10** as smaller filters will remove almost all variations."},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4637a168e5324d4bb62b405e10314c14ed477072"},"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}