Skip to content

Commit 93b751c

Browse files
authored
Temporal correlation (#20)
* addedc * edit files * added comments * edits after first round of comments * edits plus test file * edits * fix test * fixed notebook * addressing suggestions * fixed import
1 parent d4f4864 commit 93b751c

File tree

4 files changed

+973
-0
lines changed

4 files changed

+973
-0
lines changed

examples/test_correlation.ipynb

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "bd8840d7",
6+
"metadata": {},
7+
"source": [
8+
"# Temporal correlation analysis between discrete point events and times eries data"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": null,
14+
"id": "aa829baf-0bc9-4aba-b0a5-4b7bee0a7b96",
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"import numpy as np\n",
19+
"import pandas as pd\n",
20+
"import matplotlib.pyplot as plt\n",
21+
"from sklearn.metrics import pairwise_distances\n",
22+
"\n",
23+
"import os, sys\n",
24+
"\n",
25+
"cwd = os.getcwd()\n",
26+
"frameworkDir = os.path.abspath(os.path.join(cwd, os.pardir, 'src'))\n",
27+
"sys.path.append(frameworkDir)\n",
28+
"\n",
29+
"from dackar.utils.num import t_score\n",
30+
"\n",
31+
"np.random.seed(6)"
32+
]
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"id": "e04a959f",
37+
"metadata": {},
38+
"source": [
39+
"## Generate example time series"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": null,
45+
"id": "5bcc16ab-88f8-493f-9974-20b9f7469a01",
46+
"metadata": {},
47+
"outputs": [],
48+
"source": [
49+
"def generateSeries(maxT, t1, t2, tsql, sqlDur):\n",
50+
" N = 5000\n",
51+
" N_spk = 7\n",
52+
"\n",
53+
" cpu = np.zeros(N)\n",
54+
" t = np.linspace(0,maxT,N)\n",
55+
" noise_base = np.random.normal(0,0.005,N)\n",
56+
"\n",
57+
" spikesLoc = np.random.randint(low=0, high=N, size=N_spk)\n",
58+
" spikesDT = np.random.randint(low=10, high=20, size=N_spk)\n",
59+
" \n",
60+
" difference_array_1 = np.absolute(t-t1)\n",
61+
" index1 = difference_array_1.argmin()\n",
62+
"\n",
63+
" difference_array_2 = np.absolute(t-t2)\n",
64+
" index2 = difference_array_2.argmin()\n",
65+
" \n",
66+
" cpu[0:index1] = .3\n",
67+
" cpu[index2:N] = .3\n",
68+
" cpu[index1:index2] = .9\n",
69+
" cpu = cpu + noise_base\n",
70+
" \n",
71+
" # random spikes\n",
72+
" for i in range(N_spk):\n",
73+
" noise_spike = np.absolute(np.random.normal(0,0.04, spikesDT[i]))\n",
74+
" for dt in range(spikesDT[i]):\n",
75+
" cpu[spikesLoc[i] + dt] = cpu[spikesLoc[i] + dt] + noise_spike[dt]\n",
76+
" \n",
77+
" # SQL spikes\n",
78+
" noiseSQL = np.absolute(np.random.normal(0,0.08, sqlDur))\n",
79+
" for dt in range(sqlDur):\n",
80+
" cpu[tsql + dt] = cpu[tsql + dt] + noiseSQL[dt]\n",
81+
"\n",
82+
" print('Anomaly ratio: total', (index2-index1 + np.sum(spikesDT[i]))/N)\n",
83+
" print('Anomaly ratio: large spikes:', (index2-index1)/N)\n",
84+
" print('Anomaly ratio: small spikes', (np.sum(spikesDT[i]))/N)\n",
85+
" \n",
86+
" return t,cpu"
87+
]
88+
},
89+
{
90+
"cell_type": "markdown",
91+
"id": "62b349dd",
92+
"metadata": {},
93+
"source": [
94+
"## Generate temporal location of a set of events\n",
95+
"True positive events:\n",
96+
"* E1: located right before rump-up\n",
97+
"* E2: located right after rump-up\n",
98+
"* E3: located right before rump-down\n",
99+
"* E4: located right after rump-down\n",
100+
"* E5: located in the middle of squared pulse\n",
101+
"True negative events:\n",
102+
"* EA: located before the squared pulse \n",
103+
"* EB: located after the squared pulse \n",
104+
"* EC: located after the squared pulse "
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": null,
110+
"id": "f97c5cc0-b2e8-428f-bc56-787b664beb2d",
111+
"metadata": {},
112+
"outputs": [],
113+
"source": [
114+
"maxT = 1000\n",
115+
"t1 = 300\n",
116+
"t2 = 370\n",
117+
"tsql = 650\n",
118+
"sqlDur = 50\n",
119+
"t, cpu = generateSeries(maxT, t1, t2, tsql, sqlDur)\n",
120+
"\n",
121+
"difference_array_1 = np.absolute(t-t1)\n",
122+
"loc1 = difference_array_1.argmin()\n",
123+
"difference_array_2 = np.absolute(t-t2)\n",
124+
"loc2 = difference_array_2.argmin()\n",
125+
"\n",
126+
"# True negative\n",
127+
"E1_loc = loc1 - 10\n",
128+
"E2_loc = loc1 + 10\n",
129+
"E3_loc = loc2 - 10\n",
130+
"E4_loc = loc2 + 10\n",
131+
"E5_loc = loc1 + 35\n",
132+
"\n",
133+
"# True positive\n",
134+
"EA = 200\n",
135+
"difference_array_0 = np.absolute(t-EA)\n",
136+
"EA_loc = difference_array_0.argmin()\n",
137+
"\n",
138+
"EB = 500\n",
139+
"difference_array_0 = np.absolute(t-EB)\n",
140+
"EB_loc = difference_array_0.argmin()\n",
141+
"\n",
142+
"EC = 900\n",
143+
"difference_array_0 = np.absolute(t-EC)\n",
144+
"EC_loc = difference_array_0.argmin()"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": null,
150+
"id": "8a746b18-8458-4996-a8b3-b9d48800283b",
151+
"metadata": {},
152+
"outputs": [],
153+
"source": [
154+
"plt.close()\n",
155+
"fig, ax = plt.subplots()\n",
156+
"ax.plot(t,cpu)\n",
157+
"\n",
158+
"plt.xlabel('time [s]')\n",
159+
"plt.ylabel('cpu load [%]')\n",
160+
"plt.show()"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": null,
166+
"id": "849c098d-822a-450b-95fc-85f88b708273",
167+
"metadata": {},
168+
"outputs": [],
169+
"source": [
170+
"windowSize = 100\n",
171+
"omegaSize = 500\n",
172+
"alpha = 0.05\n",
173+
"N_iterations = 100\n",
174+
"alpha_value = 0.0736"
175+
]
176+
},
177+
{
178+
"cell_type": "code",
179+
"execution_count": null,
180+
"id": "610b02fa-7f9e-43f7-bdaf-d0582e3d17b2",
181+
"metadata": {},
182+
"outputs": [],
183+
"source": [
184+
"t_score.event2TStest(E_loc=E1_loc, TS=cpu, iterations=N_iterations, alphaTest=alpha, alphaOmegaset=alpha_value, windowSize=windowSize, omegaSize=omegaSize)"
185+
]
186+
},
187+
{
188+
"cell_type": "code",
189+
"execution_count": null,
190+
"id": "540ee985-05c4-4c9a-bcfc-16490917b75e",
191+
"metadata": {},
192+
"outputs": [],
193+
"source": [
194+
"t_score.event2TStest(E_loc=E2_loc, TS=cpu, iterations=N_iterations, alphaTest=alpha, alphaOmegaset=alpha_value, windowSize=windowSize, omegaSize=omegaSize)"
195+
]
196+
},
197+
{
198+
"cell_type": "code",
199+
"execution_count": null,
200+
"id": "74920e98-40d6-4719-a5f4-c0412abff925",
201+
"metadata": {},
202+
"outputs": [],
203+
"source": [
204+
"t_score.event2TStest(E_loc=E3_loc, TS=cpu, iterations=N_iterations, alphaTest=alpha, alphaOmegaset=alpha_value, windowSize=windowSize, omegaSize=omegaSize)"
205+
]
206+
},
207+
{
208+
"cell_type": "code",
209+
"execution_count": null,
210+
"id": "16259e81-9b24-4b7a-9b84-4baf9d6a9890",
211+
"metadata": {},
212+
"outputs": [],
213+
"source": [
214+
"t_score.event2TStest(E_loc=E4_loc, TS=cpu, iterations=N_iterations, alphaTest=alpha, alphaOmegaset=alpha_value, windowSize=windowSize, omegaSize=omegaSize)"
215+
]
216+
},
217+
{
218+
"cell_type": "code",
219+
"execution_count": null,
220+
"id": "93b07cf3-81db-43bb-ae04-0f182a062446",
221+
"metadata": {},
222+
"outputs": [],
223+
"source": [
224+
"t_score.event2TStest(E_loc=E5_loc, TS=cpu, iterations=N_iterations, alphaTest=alpha, alphaOmegaset=alpha_value, windowSize=windowSize, omegaSize=omegaSize)"
225+
]
226+
},
227+
{
228+
"cell_type": "code",
229+
"execution_count": null,
230+
"id": "9c25e27d-d531-48fb-bac8-aa9ace3ed353",
231+
"metadata": {},
232+
"outputs": [],
233+
"source": [
234+
"t_score.event2TStest(E_loc=EA_loc, TS=cpu, iterations=N_iterations, alphaTest=alpha, alphaOmegaset=alpha_value, windowSize=windowSize, omegaSize=omegaSize)"
235+
]
236+
},
237+
{
238+
"cell_type": "code",
239+
"execution_count": null,
240+
"id": "9b8abed4-a57c-41cc-a44b-f5106c2b3431",
241+
"metadata": {},
242+
"outputs": [],
243+
"source": [
244+
"t_score.event2TStest(E_loc=EB_loc, TS=cpu, iterations=N_iterations, alphaTest=alpha, alphaOmegaset=alpha_value, windowSize=windowSize, omegaSize=omegaSize)"
245+
]
246+
},
247+
{
248+
"cell_type": "code",
249+
"execution_count": null,
250+
"id": "b33ac31f-953e-4b07-bb36-62726e9540ff",
251+
"metadata": {},
252+
"outputs": [],
253+
"source": [
254+
"t_score.event2TStest(E_loc=EC_loc, TS=cpu, iterations=N_iterations, alphaTest=alpha, alphaOmegaset=alpha_value, windowSize=windowSize, omegaSize=omegaSize)"
255+
]
256+
}
257+
],
258+
"metadata": {
259+
"kernelspec": {
260+
"display_name": "dackar_libs",
261+
"language": "python",
262+
"name": "python3"
263+
},
264+
"language_info": {
265+
"codemirror_mode": {
266+
"name": "ipython",
267+
"version": 3
268+
},
269+
"file_extension": ".py",
270+
"mimetype": "text/x-python",
271+
"name": "python",
272+
"nbconvert_exporter": "python",
273+
"pygments_lexer": "ipython3",
274+
"version": "3.11.8"
275+
}
276+
},
277+
"nbformat": 4,
278+
"nbformat_minor": 5
279+
}

0 commit comments

Comments
 (0)