-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatasampling.py
executable file
·181 lines (142 loc) · 5.25 KB
/
datasampling.py
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
#!/usr/bin/env python
import numpy as np
__author__ = "Sebastian Kiehlmann"
__credits__ = ["Sebastian Kiehlmann"]
__license__ = "BSD 3"
__version__ = "1.0"
__maintainer__ = "Sebastian Kiehlmann"
__email__ = "[email protected]"
__status__ = "Production"
#==============================================================================
# FUNCTIONS
#==============================================================================
def smart_binning(time, interval, verbose=0, recursion=0):
"""Find data binning intervals.
Iteratively finds ranges of time-sorted data, where the data falls into a
defined time interval. This function provides an alternative to regular
binning of data with uneven time sampling.
Parmeters
---------
time : array-like
Sorted time marks.
interval : float
The length of the interval, in which data points are considered to be
close to one another.
verbose : int, optional
If zero, no information is printed. Otherwise, information about the
identified intervals is printed. The default is 0.
recursion : int, optional
Defines the current recursion level. This argument is required for the
algorithm. Do not set a value different from 0. The default is 0.
Raises
------
ValueError
Raised if `time` is not sorted increasingly.
Returns
-------
bin_ids : list
A list of arrays containing indices to the data points which are in the
same bin.
unbinned_ids : numpy.ndarray
IDs of data points that do not need to be binned.
"""
time = np.asarray(time)
# check that time is sorted increasingly:
if np.any(np.diff(time) < 0):
raise ValueError("The provided time are not sorted increasingly.")
if len(time) < 2:
return [], False
# first, find all intervals:
intervals = []
# iterate through time values:
for i, value in enumerate(time):
# iterate backwards throuch preceding values:
j = i-1
while j>=0 and time[j] > value - interval:
j -= 1
else:
# replace latest interval with extended interval:
if j+1 < i and len(intervals)>0 and intervals[-1][0] == j+1:
intervals[-1] = [j+1, i+1]
# add new interval:
elif j+1 < i:
intervals.append([j+1, i+1])
if len(intervals) == 0:
if recursion == 0:
return [], np.arange(time.shape[0])
else:
return False, False
# second, find best interval:
spreading = []
for inter in intervals:
spreading.append(np. std(time[inter[0]:inter[1]]))
index = spreading.index(min(spreading))
bin_ids_cur = np.arange(intervals[index][0], intervals[index][1])
# third, recursion on preceeding time:
time_pre = time[:bin_ids_cur[0]]
bin_ids_pre, __ = smart_binning(time_pre, interval, recursion=recursion+1)
# fourth, recursion on succeeding time:
time_suc = time[bin_ids_cur[-1]+1:]
bin_ids_suc, __ = smart_binning(time_suc, interval, recursion=recursion+1)
# fifth, join indices lists:
if bin_ids_pre:
bin_ids = bin_ids_pre
bin_ids.append(bin_ids_cur)
else:
bin_ids = [bin_ids_cur]
if bin_ids_suc:
# adjust indices:
add = bin_ids_cur[-1]+1
for inter in bin_ids_suc:
bin_ids.append(inter +add)
# determine data points that do not need binning:
if recursion == 0:
binned_ids = np.concatenate(bin_ids)
unbinned_ids = []
for i in np.arange(time.shape[0]):
if not i in binned_ids:
unbinned_ids.append(i)
unbinned_ids = np.array(unbinned_ids)
n_unbinned = unbinned_ids.shape[0]
else:
unbinned_ids = None
# print information:
if verbose:
n = [len(ids) for ids in bin_ids]
print(f'{len(bin_ids)} bins found.')
print('time points per bin:')
print(f' Min: {np.min(n):8d}')
print(f' Median: {np.median(n):8.0f}')
print(f' Mean: {np.mean(n):8.0f}')
print(f' Max: {np.max(n):8d}')
print(f'{n_unbinned} data points do not require binning.')
return bin_ids, unbinned_ids
#==============================================================================
def split_data(time, gap):
"""Split a time series at large gaps.
Parameters
----------
time : array-like
Sorted time marks.
gap : float
Gap length threshold. The data is split when the time interval between
data points exceeds this value.
Returns
-------
out : list
List of arrays containing indices to the split data sets.
"""
time = np.asarray(time)
# check that time is sorted increasingly:
if np.any(np.diff(time) < 0):
raise ValueError("The provided time are not sorted increasingly.")
if len(time) < 2:
return []
# identify large gaps:
split = np.r_[0, np.nonzero(np.diff(data)>gap)[0] + 1, len(data)]
indices = []
# prepare list of indices to the split data sets:
for start, stop in zip(split[:-1], split[1:]):
indices.append(np.arange(start, stop))
return indices
#==============================================================================