-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster_phase.py
More file actions
242 lines (218 loc) · 9.2 KB
/
cluster_phase.py
File metadata and controls
242 lines (218 loc) · 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#--------------------------------------------------------------------------
# cluster_phase.py
#
# Copyright (c) 2016, Michael J. Richardson & Jean-Sebastien Senecal
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted
# provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions
# and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of
# conditions and the following disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to
# endorse or promote products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
# USAGE
#
# data, meanGrpRho, meanIndRho, meanIndRp, grpRho, indRp
# = cluster_phase(dataset,nTimeSeries,sampleRate,firstSample=0,lastSample=None,plotFlag=False)
#
# Input:
# dataset : dataset as either a filename OR directly a numpy array
# nTimeSeries : number of time series
# sampleRate : sample rate of the time series
# firstSample : first data point in time series used
# lastSample : last data point in time series used (if None, lastSample is dataset's last sample)
# plotFlag : do plots (True, False, or filenme.plt to save using pickle)
# plotTitle : title of plot (default: None)
#
# Output:
# meanGrpRho : mean group rho (0 to 1; 1 = perfect sync)
# meanIndRho : mean rho for each TS to group (0 to 1; 1 = perfect sync)
# meanIndRp : mean Relative Phase for each TS to group cluster phase
# grpRho : group rho time-series
# indRp : relative phase time-series for each individual TS to cluster phase
#
# Example:
# [meanGrpRho meanIndRho meanIndRp grpRho indRp] = closter_phase('G201EO1.txt', 6, 1, 7200, 120, True);
#
# ADAPTED TO PYTHON BY (2016):
# J. S. Senecal (Concordia University)
#
# BY (2008):
# Michael J Richardson (Univeristy of Cincinnati) & Till D. Frank (UCONN)
#
# UPDATED (2011):
# Michael J Richardson (Univeristy of Cincinnati)
#
# References:
# [1] Frank, T. D., & Richardson, M. J. (2010). On a test statistic for
# the Kuramoto order parameter of synchronization: with an illustration
# for group synchronization during rocking chairs.
#
# [2] Richardson,M.J., Garcia, R., Frank, T. D., Gregor, M., &
# Marsh,K. L. (2010). Measuring Group Synchrony: A Cluster-Phase Method
# for Analyzing Multivariate Movement Time-Series
#
# Code Contact & References:
# michael.richardson@uc.edu
# http://homepages.uc.edu/~richamo/
#--------------------------------------------------------------------------
#--------------------------------------------------------------------------
import numpy, scipy, scipy.signal, matplotlib.pyplot, pickle
from numpy import *
from scipy import *
from matplotlib.pyplot import *
# Generate plot based on data compiled by cluster_phase() (used in cluster_phase() with option plotFlag)
def generate_plot(data, sampleRate, meanGrpRho, meanIndRho, meanIndRp, grpRho, indRp, plotFlag=True, plotTitle=None):
fig = figure(1)
dataLength = data.shape[0]
nTimeSeries = data.shape[1]
t = arange(0, dataLength) / sampleRate
subplot(3,1,1);
tmpdata = zeros((dataLength,nTimeSeries));
for nts in range(0,nTimeSeries):
tmpdata[:,nts] = (data[:,nts] + (nts*4));
plot(t, tmpdata)
xlabel('Time');
ylabel('RAW Data');
xlim([0, max(t)]);
#ylim([-185, 185]);
# plot individ-cluster relative phase
subplot(3,1,2);
plot(t[0:dataLength-1], indRp);
xlabel('Time');
ylabel('IND-Clust Relative Phase');
xlim([0, max(t)]);
ylim([-185, 185]);
# plot group-cluster amplitiude (rho) timeseries
subplot(3,1,3);
plot(t[0:dataLength-1], grpRho)
xlabel('Time');
ylabel('GRP-Clust Amplitude');
xlim([0, max(t)]);
ylim([0, 1]);
# add text
if (plotTitle != None):
fig.suptitle(plotTitle)
text(0, -.4, "Mean GRP Rho: {:.3f} Mean IND Rhos: {:s}".format(meanGrpRho, array_str(meanIndRho,precision=3)))
# save or display plot
if plotFlag == True:
fig.show()
elif plotFlag.endswith(".plt"):
print "Dumping to " + plotFlag
pickle.dump(fig, file(plotFlag, 'w'))
else:
fig.savefig(plotFlag)
close(fig)
# Compiles cluster phase.
def cluster_phase(dataset,nTimeSeries,sampleRate,firstSample=0,lastSample=None,plotFlag=False):
filterfreq = 10
# load time-series (TS)
# **************************************************************************
if (type(dataset) == str):
if (dataset.endswith(".txt")):
fulldata = loadtxt(dataset)
else:
fulldata = load(dataset)
elif (type(dataset) == numpy.darray):
fulldata = dataset
# Builds a subset by taking only rows firstSample .. lastSample from base dataset
if (lastSample != None):
data = fulldata[firstSample:lastSample,0:nTimeSeries]
else:
data = fulldata[firstSample:,0:nTimeSeries]
dataLength = data.shape[0]
delta_t = 1.0/sampleRate
t = arange(0, dataLength) * delta_t
# Downsample, Filter and normalize data
# **************************************************************************
# linear detrend data to remove drift (chiar moving slightly during trial
for nts in range(0,nTimeSeries):
data[:,nts] = scipy.signal.detrend(data[:,nts])
# normalize
for nts in range(0,nTimeSeries):
data[:,nts] = scipy.stats.mstats.zscore(data[:,nts], ddof=1)
# filter
for nts in range(0,nTimeSeries):
weight_b,weight_a = scipy.signal.butter(2, filterfreq/(sampleRate/2.0));
irlength = max(weight_b.size-1,weight_a.size-1)
data[:,nts] = scipy.signal.filtfilt(weight_b, weight_a, data[:,nts])
# Compute phase for each TS using Hilbert transform
# **************************************************************************
phase = zeros((dataLength-1,nTimeSeries))
for k in range(0,nTimeSeries):
hrp = scipy.signal.hilbert(data[:,k])
for n in range(0,dataLength-1):
phase[n,k] = arctan2( real(hrp[n]), imag(hrp[n]));
phase[:,k]=unwrap(phase[:,k]);
# Compute mean running (Cluster) phase
# **************************************************************************
clusterphase = zeros(dataLength-1)
for n in range(0,dataLength-1):
ztot = complex(0,0);
for k in range(0,nTimeSeries):
z = exp(1j * phase[n,k]);
ztot = ztot+z;
ztot = ztot/nTimeSeries;
clusterphase[n] = angle(ztot);
clusterphase = unwrap(clusterphase);
# Compute relative phases between phase of TS and cluster phase
# **************************************************************************
complexIndRp = zeros((dataLength-1,nTimeSeries),dtype=complex);
indRp = zeros((dataLength-1,nTimeSeries))
meanIndRp = zeros(nTimeSeries);
meanIndRho = zeros(nTimeSeries);
for k in range(0,nTimeSeries):
ztot = complex(0,0);
for n in range(0,dataLength-1):
z = exp(1j * (phase[n,k] - clusterphase[n]));
complexIndRp[n,k] = z;
ztot = ztot+z;
indRp[:,k] = angle(complexIndRp[:,k]) * 360/(2*numpy.pi); # convert radian to degrees
ztot = ztot / (dataLength-1);
meanIndRp[k] = angle(ztot);
meanIndRho[k] = abs(ztot);
meanRp = meanIndRp;
meanIndRp = (meanIndRp / (2*numpy.pi)*360); # convert radian to degrees
print(' ');
print('Mean relative phases of individuals to cluster phase')
print(meanIndRp);
print('Averaged degree of synchronization of individuals (Rho = 1-circular variance)')
print(meanIndRho);
# Compute cluster amplitude rhotot in rotation frame
# **************************************************************************
grpRho=zeros(dataLength-1);
for n in range(0,dataLength-1):
ztot = complex(0,0);
for k in range(0,nTimeSeries):
z = exp(1j * (phase[n,k] - clusterphase[n] - meanRp[k]));
ztot = ztot+z;
ztot = ztot / nTimeSeries;
grpRho[n] = abs(ztot);
print grpRho
meanGrpRho = mean(grpRho);
print('Averaged degree of synchronization of the group')
print(meanGrpRho);
# Do Plot
# **************************************************************************
# plot data for time-series (separeted on graph for display purposes)
if plotFlag != False:
generate_plot(data, sampleRate, meanGrpRho, meanIndRho, meanIndRp, grpRho, indRp, plotFlag)
return data, meanGrpRho, meanIndRho, meanIndRp, grpRho, indRp