Created
October 11, 2016 18:50
-
-
Save mutaku/55c9ed7a70767ff302f29a4f74f891e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def local_difference(p, data, constrain_metric, change_metric, walk_length=10): | |
"""Identify difference from local smoothing | |
p is an x declaration of dataset data | |
""" | |
p = int(p) | |
constrain_data = functools.reduce(getattr, | |
[data] + constrain_metric.split('.')) | |
change_data = functools.reduce(getattr, | |
[data] + change_metric.split('.')) | |
surrounds = int(np.floor(walk_length / 2)) | |
window = [p - surrounds, p + surrounds + 1] | |
hindering = [x for x in change_data | |
if x in range(*window) and x != p] | |
padding_list = [-1 if h < p else 1 for h in hindering ] | |
left_padding, right_padding = padding_list.count(-1), padding_list.count(1) | |
window[0] -= left_padding | |
window[1] += right_padding | |
do_not_index = [(surrounds + left_padding) - (p - x) for x in hindering] | |
data_window_adjusted = [v for i, v | |
in enumerate(constrain_data[slice(*window)]) | |
if i not in frozenset(do_not_index)] | |
time = [i for i in xrange(len(data_window_adjusted))] | |
filtered = lowess(data_window_adjusted, time) | |
offset_index = data_window_adjusted.index(constrain_data[p]) | |
return abs(constrain_data[p] - filtered[offset_index, 1]), \ | |
filtered[:, 1], data_window_adjusted |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment