consider the following code:
class MyClass(object): def __init__(self): self.data_a = np.array(range(100)) self.data_b = np.array(range(100,200)) self.data_c = np.array(range(200,300)) def _method_i_do_not_have_access_to(self, data, window, func): output = np.empty(np.size(data)) for i in xrange(0, len(data)-window+1): output[i] = func(data[i:i+window]) output[-window+1:] = np.nan return output def apply_a(self): a = self.data_a def _my_func(val): return sum(val) return self._method_i_do_not_have_access_to(a, 5, _my_func) my_class = MyClass() print my_class.apply_a()
The _method_i_do_not_have_access_to method accepts a numpy array, a window parameter, and a user-defined function descriptor and returns an array containing the values โโdisplayed by the function descriptor at the window data points during the input data array โ a common rolling method. I do not have access to modify this method.
As you can see, _method_i_do_not_have_access_to passes one input to the function descriptor, which is the data array passed to _method_i_do_not_have_access_to . This function descriptor only calculates output based on window data points on a single data array passed to it through _method_i_do_not_have_access_to .
I need to make _my_func (function descriptor passed to _method_i_do_not_have_access_to ) work with data_b and data_c in addition to the array that is passed to _my_func via _method_i_do_not_have_access_to in the same window indexes . data_b and data_c defined globally in the MyClass class .
The only thing I thought about this is the links to data_b and data_c inside _my_func as follows:
def _my_func(val): b = self.data_b c = self.data_c
However, I need to trim b and c with the same indices as val (remember that val is the length segment of the window array that passes through _method_i_do_not_have_access_to ).
For example, if the loop in _method_i_do_not_have_access_to currently works with indices 45 -> 50 on the input array, _my_func should work on the same indices on b and c .
The end result will be something like this:
def _my_func(val): b = self.data_b
Any thoughts on how I can do this?