Reputation: 2959
I am trying to call function from python dictionary one by one into another function
feature={
1:[perm_entropy, 3, True],
2:[svd_entropy, 3, 1, True],
3:[petrosian_fd],
4:[katz_fd],
5:[higuchi_fd],
6:[hjorth_mobility],
7:[hjorth_complexity]}
i am trying to call above function as follows
sub_25=[]
for i in feature.values():
sub_25.append(sub25.groupby('trial')[col1].agg(i))
Except the first 2 function, all other functions are working correctly. I thinks it is because of extra parameters they have. These two function work well, if they are called separately as follow
sub_25.append(sub25.groupby('trial')[col1].agg(perm_entropy, 3, True))
How can i deal these two functions? EEG data
Upvotes: 0
Views: 67
Reputation: 1631
What you want is partially applied function for your use-case. Python's functools package can take care of this. You can change your code as per the following example:
from functools import partial
def addthree(a,b,c):
return a+b+c
partial_sum = partial(addthree,1,2)
fdict = {1: partial_sum}
fdict[1](4) // this returns 7 on the console
Similarly you can write perm_entropy
with its required arguments and pass
your values later
new_perm_entropy = partial(perm_entropy,3,True)
feature = {1: new_perm_entropy,..}
feature[1](your_args_here)
You can read up on Partially Applied Functions for further clarifications.
Upvotes: 1
Reputation: 2633
From the documentation (https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.agg.html), the pandas agg
method accepts a function as the first argument, optional axis second, and then positional arguments which will be passed to the function specified.
To do this in your for loop, you should use *
to unpack i
since i
is a list of the arguments you need to pass to agg
.
sub_25=[]
for i in feature.values():
sub_25.append(sub25.groupby('trial')[col1].agg(*i))
For example, imagine i = [perm_entropy, 3, True]
, then sub_25.append(sub25.groupby('trial')[col1].agg(*i))
is equivalent to sub_25.append(sub25.groupby('trial')[col1].agg(perm_entropy, 3, True))
which is what you are after.
Upvotes: 0