Fast-dm Python wrapper

I implemented a little wrapper for fast-dm which makes it easier to use fast-dm in a python environment. You can fide the code on github. There is also an IPython notebook. that shows how to use it.  

FastDM Python wrapper demo

A short demo of how easy you can fit DDM models using fastdm and Python, on multiple cores
In [5]:
import fastdm
import pandas
load in data
In [9]:
data = pandas.load('/home/gdholla1/data/tdcs/data/leiden/leiden.pandas')
Set up the model. We feed in: * A Pandas DataFrame with columns RT and response * A dictionary indicating which parameters depend on which conditions
In [10]:
model = fastdm.FastDM(data, 
                      depends_on={'a':['acc_spd', 'stimulation'],
                                  't0':['acc_spd', 'stimulation'],})
We fit using 10 CPU cores at the same time
In [11]:
model.fit(nproc=10)
Running fast-dm on experiment_2.ctl
Running fast-dm on experiment_7.ctl
Running fast-dm on experiment_4.ctl
Running fast-dm on experiment_5.ctl
Running fast-dm on experiment_1.ctl
Running fast-dm on experiment_3.ctl
Running fast-dm on experiment_6.ctl
Out[11]:
<multiprocessing.pool.Pool at 0x4ba1490>
Now we can extract a FastDMResult-object
In [31]:
pars = model.get_parameters()
That has a dataframe parameters as an attribute
In [41]:
pars.parameters
Out[41]:
a_acc_anodal a_acc_sham a_spd_anodal a_spd_sham d fit method penalty st0 sv szr t0_acc_anodal t0_acc_sham t0_spd_anodal t0_spd_sham time v subj_idx
subj_idx
2 NaN 1.2304 NaN 0.8493 -0.0036 0.3139 KS 0 0.1019 0.4961 0.4870 NaN 0.4718 NaN 0.4259 11.58 1.4959 2
1 2.0622 NaN 1.0335 NaN 0.0385 0.1232 KS 0 0.0786 1.0405 0.3692 0.4299 NaN 0.3931 NaN 27.93 2.6697 1
4 NaN 0.8207 NaN 0.7250 -0.0145 0.4445 KS 0 0.1906 0.6474 0.3718 NaN 0.3533 NaN 0.3469 6.59 0.1914 4
7 1.1676 NaN 1.1529 NaN -0.0145 0.1342 KS 0 0.1796 0.8161 0.3894 0.2723 NaN 0.2697 NaN 22.60 0.4535 7
5 1.5793 NaN 0.8046 NaN 0.0079 0.4626 KS 0 0.1140 0.5652 0.4924 0.5314 NaN 0.4500 NaN 10.38 2.0744 5
3 1.2111 NaN 0.7283 NaN -0.0202 0.8502 KS 0 0.2516 1.0173 0.1926 0.5259 NaN 0.3912 NaN 17.62 0.3991 3
6 NaN 0.7969 NaN 0.6579 -0.0094 0.0999 KS 0 0.1201 1.2397 0.3093 NaN 0.3737 NaN 0.3776 6.57 3.0034 6
You can also make a ‘melted’ dataframe for a single parameter
In [43]:
p = pars.melted_parameters('t0')
p
Out[43]:
subj_idx t0 acc_spd stimulation
1 1 0.4299 acc anodal
3 7 0.2723 acc anodal
4 5 0.5314 acc anodal
5 3 0.5259 acc anodal
7 2 0.4718 acc sham
9 4 0.3533 acc sham
13 6 0.3737 acc sham
15 1 0.3931 spd anodal
17 7 0.2697 spd anodal
18 5 0.4500 spd anodal
19 3 0.3912 spd anodal
21 2 0.4259 spd sham
23 4 0.3469 spd sham
27 6 0.3776 spd sham
Let’s make some nice plots using seaborn
In [44]:
import seaborn as sns
In [45]:
p = pars.melted_parameters('t0')
sns.factorplot('acc_spd', 't0', hue='stimulation', data=p, units='subj_idx',)
Out[45]:
<seaborn.axisgrid.FacetGrid at 0x62cd290>
In [47]:
p = pars.melted_parameters('a')
sns.factorplot('stimulation', 'a', hue='acc_spd', data=p, units='subj_idx',)
Out[47]:
<seaborn.axisgrid.FacetGrid at 0x62a47d0>