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 takes a numpy array, a window parameter, and a user-defined function handle and returns an array containing values output by the function handle on window data points at a time of the input data array – a generic rolling method. I do not have access to changing this method.
As you can see, _method_i_do_not_have_access_to passes one input to the function handle which is the data array passed to _method_i_do_not_have_access_to. That function handle only computes output based window data points on the one data array passed to it through _method_i_do_not_have_access_to.
What I need to do is allow _my_func (the function handle passed to _method_i_do_not_have_access_to) to operate on data_b and data_c in addition to the array that is passed to _my_func through _method_i_do_not_have_access_to at the same window indexes. data_b and data_c are defined globally in the MyClass class.
The only way I have thought of doing this is including references to data_b and data_c within _my_func like this:
def _my_func(val):
b = self.data_b
c = self.data_c
# do some calculations
return sum(val)
However, I need to slice b and c at the same indexes as val (remember val is the length-window slice of the array that is passed through _method_i_do_not_have_access_to).
For example, if the loop within _method_i_do_not_have_access_to is currently operating on indexes 45 -> 50 on the input array, _my_func has to be operating on the same indexes on b and c.
The final result would be something like this:
def _my_func(val):
b = self.data_b # somehow identify which slide we are at
c = self.data_c # somehow identify which slide we are at
# if _method_i_do_not_have_access_to is currently
# operating on indexes 45->50, then the sum of
# val, b, and c should be the sum of the values at
# index 45->50 at each
return sum(val) * sum(b) + sum(c)
Any thoughts on how I might accomplish this?
The question is how would _my_func know on which indizes to operate? If you know the indizes in advance when calling your function, the simplest approach would be just using a lambda:
lambda val: self._my_func(self.a, self.b, index, val)with _my_func obviously changed to accommodate the additional parameters.Since you don’t know the indizes, you’ll have to write a wrapper around self.c that remembers which index was last accessed (or better yet catches the slice operator) and stores this in a variable for your function to use..
Edit: Knocked up a small example, not especially great coding style and all, but should give you the idea: