def main(): # data = hddm.load_csv('/data/pdmattention/TestData.csv') data = hddm.load_csv('/data/pdmattention/TestData.csv') data.head(10) # returns first n rows for the object based on position # data = hddm.utils.flip_errors(data) # flips sign for lower boundary # response --> what are lower # boundaries # fig = plt.figure() # creates an empty figure # ax = fig.add_subplot(111, xlabel = 'RT', ylabel='count', title='RT distributions') # for i, subj_data in data.groupby('subj_idx'): # data.groupby splits that data into the groups based on some criterion # grouped = obj.groupby(key) # grouped = obj.groupby(key, axis = 1) # grouped = obj.groupby([key1, key2]) # data.groupby('subj_idx') groups data horizontally by # the column subj_idx # for i, subj_data in data.groupby('subj_idx'): # subj_data.rt.hist(bins=20, histtype='step', ax=ax) # plt.savefig('/data/pdmattention/hddm_fig1.pdf') # plt.show() # time.sleep(5) # print('\n') # print('Done!') next_step(data)
def run_exp1mod16(id): """ drift-rate: rwd/pen tradeoff, stable target variance model bias : rwd/pen tradeoff, stable target variance model""" #imports import hddm data = hddm.load_csv('~/Pub_Code_master/Data/exp1data.csv') #data grooming, remove fast outliers rtSig = data.rt.std() rtMu = data.rt.mean() cutoff = rtMu - rtSig data = data[data.rt > cutoff] data.reset_index(drop=True, inplace=True) #build model exp1model16 = hddm.HDDM(data, depends_on={ 'v': ['rwd_pen', 'stableVar'], 'z': ['rwd_pen', 'stabelVar'] }, bias=True, include=['v', 'a', 't', 'z'], p_outlier=0.05) exp1model16.find_starting_values() exp1model16.sample(3000, burn=1000, dbname='exp1mod16_%i.db' % id, db='pickle') return exp1model16
def main(): path = "/home/megan/Desktop/Drift-Diffusion/data" data = hddm.load_csv( "/home/megan/Desktop/Drift-Diffusion/modeling/diffusion-nov-cutoff.csv" ) #visualizeRT(data) #simpleModel(data) #GelmanRubinModel(data) modelFitDrift(data)
def setUp(self): print("Passing setUp") self.models = list( hddm.torch.torch_config.TorchConfig(model="ddm").network_files. keys()) + ["ddm_vanilla", "full_ddm_vanilla"] self.n_samples_per_subject = 100 self.n_trials = 10 self.n_subjects = 5 self.cav_data = hddm.load_csv(hddm.__path__[0] + "/examples/cavanagh_theta_nn.csv") pass
def run_testmod(id): #imports import hddm data = hddm.load_csv('~/Pub_Code_master/Data/exp1data.csv') #data grooming, remove fast outliers rtSig = data.rt.std() rtMu = data.rt.mean() cutoff = rtMu - rtSig data = data[data.rt > cutoff] data.reset_index(drop=True, inplace=True) #build model testmod = hddm.HDDM(data) testmod.sample(200, burn=20, dbname='testmod_%i.db' % id, db='pickle') return testmod
def setUp(self): self.models = list( hddm.torch.torch_config.TorchConfig( model="ddm").network_files.keys()) self.n_samples_per_trial = 1000 self.nmcmc = 200 self.nburn = 100 self.filepath = hddm.__path__[0] + "/tests/torch_models/" self.cav_data = hddm.load_csv(hddm.__path__[0] + "/examples/cavanagh_theta_nn.csv") # Make model test folder if it is not there if not os.path.isdir(self.filepath): os.mkdir(self.filepath) pass
def run_exp1mod6(id): """ drift-rate: stable target variance model """ import hddm data = hddm.load_csv('~/Pub_Code_master/Data/exp1data.csv') rtSig = data.rt.std() rtMu = data.rt.mean() cutoff = rtMu - rtSig data = data[data.rt > cutoff] data.reset_index(drop=True, inplace=True) exp1model6 = hddm.HDDM(data, depends_on={'v': 'stableVar'}, p_outlier=0.05) exp1model6.find_starting_values() exp1model6.sample(1500, burn=500, dbname='exp1mod6_%i.db' % id, db='pickle') return exp1model6
def run_exp2mod6(id): """ drift-rate: rwd prob model """ import hddm data = hddm.load_csv('~/Pub_Code_master/Data/exp2data.csv') #data grooming, remove fast outliers #rtSig = data.rt.std() #rtMu = data.rt.mean() #cutoff = rtMu - rtSig #data = data[data.rt>cutoff] #data.reset_index(drop=True, inplace=True) exp2model6 = hddm.HDDM(data, depends_on={'v': 'rwd_prob'}, p_outlier=0.05) exp2model6.find_starting_values() exp2model6.sample(1500, burn=500, dbname='exp2mod6_%i.db' % id, db='pickle') return exp2model6
def run_exp2mod2(id): """ Bias: task params model """ import hddm data = hddm.load_csv('~/Pub_Code_master/Data/exp2data.csv') #data grooming, remove fast outliers #rtSig = data.rt.std() #rtMu = data.rt.mean() #cutoff = rtMu - rtSig #data = data[data.rt>cutoff] #data.reset_index(drop=True, inplace=True) exp2model2 = hddm.HDDM(data, depends_on={'z': ['rwd_pen', 'rwd_prob']}, include='z', p_outlier=0.05) exp2model2.find_starting_values() exp2model2.sample(1500, burn=500, dbname='exp2mod2%i.db' % id, db='pickle') return exp2model2
def run_exp1mod2(id): """ bias:task params model """ import hddm data = hddm.load_csv('~/Pub_Code_master/Data/exp1data.csv') rtSig = data.rt.std() rtMu = data.rt.mean() cutoff = rtMu - rtSig data = data[data.rt > cutoff] data.reset_index(drop=True, inplace=True) exp1model2 = hddm.HDDM(data, depends_on={'z': ['rwd_pen', 'stableVar']}, include='z', p_outlier=0.05) exp1model2.find_starting_values() exp1model2.sample(1500, burn=500, dbname='exp1mod2_%i.db' % id, db='pickle') return exp1model2
def run_exp3mod1(id): """ Pure drift-rate to task params model """ import hddm data = hddm.load_csv( 'C:/Users/Rory/Dropbox/Net_Worth/Pub_Code/Data/Experiment3/Model/Exp3ModelData.csv' ) #data grooming, remove fast outliers #rtSig = data.rt.std() #rtMu = data.rt.mean() #cutoff = rtMu - rtSig #data = data[data.rt>cutoff] #data.reset_index(drop=True, inplace=True) exp3model1 = hddm.HDDM(data, depends_on={'v': ['rwd_prob', 'stableVar']}, p_outlier=0.05) exp3model1.find_starting_values() exp3model1.sample(1500, burn=250, dbname='exp3mod1%i.db' % id, db='pickle') return exp3model1
def gelman_rubin_models(n): import hddm import pandas as pd import pickle import os #load data data = hddm.load_csv( 'https://raw.githubusercontent.com/xuankai91/MResProject/master/tofit_HDDM.csv' ) print('data loaded') #setup samples = 5000 savepath = './' #create save folders if not os.path.exists(savepath + 'gelman_rubin'): os.makedirs(savepath + 'gelman_rubin') #start modelling print('starting...') #instantiate model object model = hddm.HDDM(data, depends_on={ 'v': 'stim', 'a': 'stim', 't': 'stim' }, include=('sv', 'st', 'sz'), p_outlier=.05) #start model fitting model.find_starting_values() model.sample(samples, burn=int(samples / 10), dbname='%s/gelman_rubin/m_grs_%d_traces.db' % (savepath, n), db='pickle') model.save('%s/gelman_rubin/m_grs_%d' % (savepath, n))
def run_exp1mod1(id): """ pure drift-rate:task params model """ #imports import hddm data = hddm.load_csv('~/Pub_Code_master/Data/exp1data.csv') #data grooming, remove fast outliers rtSig = data.rt.std() rtMu = data.rt.mean() cutoff = rtMu - rtSig data = data[data.rt > cutoff] data.reset_index(drop=True, inplace=True) #build model exp1model1 = hddm.HDDM(data, depends_on={'v': ['rwd_pen', 'stableVar']}, p_outlier=0.05) exp1model1.find_starting_values() exp1model1.sample(3000, burn=1500, dbname='exp1mod1_%i.db' % id, db='pickle') return exp1model1
def run_exp3mod8(id): """ bias: rwd prob model """ import hddm data = hddm.load_csv( 'C:/Users/Rory/Dropbox/Net_Worth/Pub_Code/Data/Experiment3/Model/Exp3ModelData.csv' ) #data grooming, remove fast outliers #rtSig = data.rt.std() #rtMu = data.rt.mean() #cutoff = rtMu - rtSig #data = data[data.rt>cutoff] #data.reset_index(drop=True, inplace=True) exp3model8 = hddm.HDDM(data, depends_on={'z': 'rwd_prob'}, include='z', p_outlier=0.05) exp3model8.find_starting_values() exp3model8.sample(1500, burn=250, dbname='exp3mod8_%i.db' % id, db='pickle') return exp3model8
def run_exp2mod10(id): """ drift-rate: rwd/pen tradeoff with free bias model """ #imports import hddm data = hddm.load_csv('~/Pub_Code_master/Data/exp2data.csv') #data grooming, remove fast outliers #rtSig = data.rt.std() #rtMu = data.rt.mean() #cutoff = rtMu - rtSig #data = data[data.rt>cutoff] #data.reset_index(drop=True, inplace=True) #build model exp2model10 = hddm.HDDM(data, depends_on={'v': 'rwd_pen'}, include='z', p_outlier=0.05) exp2model10.find_starting_values() exp2model10.sample(1500, burn=500, dbname='exp2mod10_%i.db' % id, db='pickle') return exp2model10
def run_exp3mod10(id): """ drift-rate: stable target variance with free bias model """ #imports import hddm data = hddm.load_csv( 'C://Users/Rory/Dropbox/Net_Worth/Pub_Code/Data/Experiment3/Model/Exp3ModelData.csv' ) #data grooming, remove fast outliers #rtSig = data.rt.std() #rtMu = data.rt.mean() #cutoff = rtMu - rtSig #data = data[data.rt>cutoff] #data.reset_index(drop=True, inplace=True) #build model exp3model10 = hddm.HDDM(data, depends_on={'v': 'stableVar'}, include='z', p_outlier=0.05) exp3model10.find_starting_values() exp3model10.sample(1500, burn=250, dbname='exp3mod10_%i.db' % id, db='pickle') return exp3model10
def run_model(id): from patsy import dmatrix from pandas import Series import numpy as np import hddm dataHDDM = hddm.load_csv('DDM/dataHDDM_pmt.csv') dataHDDM["subj_idx"] = dataHDDM["participant"] del dataHDDM["participant"] dataHDDM["SAT"] = dataHDDM.apply(lambda row: 0 if row['SAT'] == "Accuracy" else 1, axis=1) dataHDDM["FC"] = dataHDDM.apply(lambda row: -0.5 if row['FC'] == "low" else 0.5, axis=1) dataHDDM["contrast"] = dataHDDM.contrast.replace([1,2,3,4,5,6], [-.5,-.3,-.1,.1,.3,.5]) dataHDDM["givenResp"] = dataHDDM["response"] dataHDDM["stim"] = dataHDDM.apply(lambda row: 1 if row['stim'] == 'Right' else 0, axis=1) dataHDDM["response"] = dataHDDM.apply(lambda row: 1 if row['givenResp'] == 'Right' else 0, axis=1) def v_link_func(x, data=dataHDDM): stim = (np.asarray(dmatrix('0 + C(s, [[1], [-1]])', {'s': data.stim.ix[x.index]}))) return x*stim if id < 4: ############## M1 LM = [{'model':'t ~ SAT + FC + contrast + SAT:FC + SAT:contrast + FC:contrast + SAT:FC:contrast', 'link_func': lambda x: x} , {'model':'v ~ contrast', 'link_func':v_link_func} , {'model':'a ~ FC + SAT + SAT:FC', 'link_func': lambda x: x} ] deps = {'sz' : 'SAT'} inc = ['sv','sz','st','z'] model_name = "Joint_t0" else : return np.nan() name = 'light_reg_PMT_%s' %str(id) m = hddm.HDDMRegressor(dataHDDM, LM , depends_on = deps, include=inc, group_only_nodes=['sv', 'sz','st', "sz_SAT"], group_only_regressors=False, keep_regressor_trace=True) m.find_starting_values() m.sample(iter=10000, burn=8500, thin=1, dbname='DDM/traces/db_%s'%name, db='pickle') m.save('DDM/Fits/%s'%name) return m
import hddm print(hddm.__version__) # Python version: import sys print (sys.version) print (pd.__version__) # Adding a monkey in order to save the models dbs: import pickle # Get around a problem with saving regression outputs in Python 3 def savePatch(self, fname): with open(fname, 'wb') as f: pickle.dump(self, f) hddm.HDDM.savePatch = savePatch data = hddm.load_csv("/ems/elsc-labs/loewenstein-y/lior.lebovich/DD/g27/data_csv_g27_dl0_3columns.csv") # data = data[data.subj_idx!=125] docdir = "/ems/elsc-labs/loewenstein-y/lior.lebovich/DD/g27/none1" os.chdir(docdir) i = sys.argv[1] print(i) m = hddm.HDDM(data, p_outlier=.05, informative=True) m.find_starting_values() m.sample(40000, burn=20000, thin=5, dbname='db'+str(i), db='pickle') m.savePatch('submodel'+str(i)) print('Thats it!!')
'Nburn: ' + str(nburn) + '\n################################################\n################################################\n' ) #if args.verbose==None: # verb = True #else: # verb = False ######################## ############################################## ## load data and flip incorrect RTs if accuracy coding ############################################## print('loading raw RT data from: ' + rawdf) data = hddm.load_csv(rawdf) print('data structure: ') print(data.head(5)) if code == 'acc': print('\nflipping RT errors\n') data = hddm.utils.flip_errors(data) ## navigate to outputdir os.chdir(outputdir) print('Navigated to ' + os.getcwd()) print('Directory contents:\n') print(os.listdir('.'))
import pandas as pd import matplotlib.pyplot as plt import hddm import pickle # Load data from csv file into a NumPy structured array data = hddm.load_csv('C://python//hddm//data//simple_hddm.csv') # Create a HDDM model multi object model = hddm.HDDM(data) # find a good starting point which helps with the convergence. model.find_starting_values() # Create model and start MCMC sampling model.sample(2000, burn=20, dbname='traces.db', db='pickle') # Print fitted parameters and other model statistics model.save('C://python//hddm//models//simple//mymodel_jupyter')
code = 'stim' #coding scheme: stimulus(stim) or accuracy(acc) nchains = 5 # vestigial from earlier days #nmodels=6 #number of distinct models to run below #mods = 1 #estimate baseline models? #SNAP_mods = 0 #estimate dimensional models ###################################################################### ##Read in data and set up burn and sample quantities if ics == 0: #local basedir = '/Users/natehall/github_repos/PD_Inhibition_DDM' os.chdir(basedir) if code == 'stim': data = hddm.load_csv('Data/preprocessed/flank_use.csv') elif code == 'acc': # data = hddm.load_csv('Flanker_reduced_acc_coded_nooutliers.csv') outputdir = basedir+'/Outputs/practice_hddm' nsample = 50 nburn = 3 os.chdir(outputdir) elif ics == 1: os.chdir('/gpfs/group/mnh5174/default/DEPENd_Box/Projects/PD_Inhibition_DDM') if code == 'stim': data = hddm.load_csv('Flanker_reduced_stim_coded_nooutliers.csv') elif code == 'acc': data = hddm.load_csv('Flanker_reduced_acc_coded_nooutliers.csv') #outputdir = ('practice_files') nsample = 20000 nburn = 5000
os.getcwd() os.chdir("/home/brain/host/hddm_exp7_rep/") # get the tool box import pandas as pd import matplotlib.pyplot as plt # plt.rcParams['image.cmap'] = 'viridis' # change default colormap import hddm import time from cycler import cycler plt.rcParams['axes.prop_cycle'] = cycler(color='bgrcmyk') # Load match data from csv file into a NumPy structured array dat_C_Id = hddm.load_csv('exp7_rep_categ_id_hddm.csv') dat_C_Id.head(10) # flip the error RTs to be negative dat_C_Id = hddm.utils.flip_errors(dat_C_Id) # check the RT distritubtion fig = plt.figure() ax = fig.add_subplot(111, xlabel='RT', ylabel='count', title='RT distributions') for i, subj_data in dat_C_Id.groupby('subj_idx'): subj_data.rt.hist(bins=20, histtype='step', ax=ax) plt.savefig('p_exp7_rep_Id_flipped.pdf')
# Run the Hierarchical Drift Diffusion Model (HDDM; Wiecki T.V, et al., 2013) on the discriminability data import pandas as pd import numpy as np import matplotlib.pyplot as plt import hddm # %matplotlib inline # load the data dt_data = hddm.load_csv('./hddm_dt_data.csv') # dt data: ''' subjects: 19 (0-18) pairs: 666 (630 different, 36 same) rt: reaction times (10 rts per different pair, 56 rts per same pair) response: correct(1), incorrect(0) condition: same objects pair(1), different objects pair(2) ''' # add headers headers = ["subNo", "pair", "rt", "response", "condition"] dt_data.columns = headers # in python indexing starts with zero, thus, subject one is actually zero. Change indexing dt_data["subNo"] = dt_data["subNo"] - 1 # quick check dt_data.head(20)
import pandas as pd import hddm import pickle # load data data = hddm.load_csv( 'Z://Work//UW//projects//RR_TMS//hddm//data//infins_hddm.csv') # init models models = [] # make models for i in range(5): m = hddm.HDDM(data, depends_on={'v': 'stim', 'a': 'stim'}) m.find_starting_values() m.sample( 10000, burn=5000, dbname= 'Z://Work//UW//projects//RR_TMS//hddm//db//ii_va_stim_traces%i.db' % i, db='pickle') models.append(m) # save models for i in range(5): fname = 'Z://Work//UW//projects//RR_TMS//hddm//models//by_cond//ii_va_stim' + str( i) models[i].save(fname)
import pandas as pd import matplotlib.pyplot as plt import numpy as npy # matplotlib inline import hddm from patsy import dmatrix data = hddm.load_csv('data/fmri_modeling.csv') data = hddm.utils.flip_errors(data) data = data[data.run > 2] # remove counts data = data[~npy.isnan(data.rt)] #remove nans fig = plt.figure() ax = fig.add_subplot(111, xlabel='RT', ylabel='count', title='RT distributions') for i, subj_data in data.groupby('subj_idx'): subj_data.rt.hist(bins=20, histtype='step', ax=ax) plt.savefig('fMRI_rt.pdf') # # dmatrix("C(stim,Treatment(1))", data.head(10)) m_stim = hddm.HDDMStimCoding(data, include='z', stim_col='stim', split_param='z', depends_on={ 'v': 'response', 'a': 'response',
##Create a string for SNAP (Schedule for Nonadaptive and Adaptive Personality) dimensions #SNAP_dim = ('DISINH', 'SUICPRON', 'NEGTEMP', 'MISTRUST', 'MANIP', 'AGG', 'SELFHARM', 'ECCPERC', 'DEPEN', 'POSTEMP', 'EXHIB', 'ENTITL', 'DETACH', 'IMPUL', 'PROPER', 'HDWK','DISINHP', 'LOSLFEST') #nmodels=6 #number of distinct models to run below #mods = 1 #estimate baseline models? #SNAP_mods = 0 #estimate dimensional models ###################################################################### ##Read in data and set up burn and sample quantities if ics == 0: #local basedir = '/Users/natehall/github_repos/PD_Inhibition_DDM' os.chdir(basedir) if code == 'stim': data = hddm.load_csv('Data/preprocessed/flank_stimCode.csv') elif code == 'acc': data = hddm.load_csv('Data/preprocessed/flank_accCode.csv') outputdir = basedir+'/Outputs/practice_hddm/flanker' nsample = 10 nburn = 3 elif ics == 1: basedir = '/gpfs/group/mnh5174/default/Nate/PD_Inhibition_DDM' os.chdir(basedir) if code == 'stim': data = hddm.load_csv('Data/preprocessed/flank_stimCode.csv') elif code == 'acc': data = hddm.load_csv('Data/preprocessed/flank_accCode.csv') data = hddm.utils.flip_errors(data) if short_chains: outputdir = basedir+'/Outputs/practice_hddm/flanker'
import pandas as pd import hddm import pickle # load data data = hddm.load_csv( 'Z://Work//UW//projects//RR_TMS//hddm//data//fullsplit_hddm.csv') regm = hddm.HDDMRegressor(data, "z ~ rulert:C(tms, Treatment('NV'))", depends_on={ 'v': 'stim', 'a': 'stim' }, bias=True, include='all', p_outlier=0.05) regm.find_starting_values() regm.sample( 2000, burn=200, dbname='Z://Work//UW//projects//RR_TMS//hddm//db//testmodel_traces.db', db='pickle') fname = 'Z://Work//UW//projects//RR_TMS//hddm//models//by_cond//testmodel' regm.save(fname)
import hddm # Load data from csv file into a NumPy structured array data = hddm.load_csv('simple_difficulty.csv') # Create a HDDM model multi object model = hddm.HDDM(data, depends_on={'v':'difficulty'}) # Create model and start MCMC sampling model.sample(10000, burn=5000) # Print fitted parameters and other model statistics model.print_stats() # Plot posterior distributions and theoretical RT distributions model.plot_posteriors(save=True) model.plot_posterior_predictive(save=True)
import os import hddm import matplotlib.pyplot as plt #folder = '/home/ausmanpa/Documents/gp/ACTR_DDM/simulations/' folder = '/projects/actr/models/ACTR_DDM/simulations01_redux/' files = os.listdir(folder) folderIdx = files.index('DDM_results') files.pop(folderIdx) #listdir will list the results folder, remove it for f in files: hddmData = hddm.load_csv(folder + f) #create model object where v, a, and t all depend on the difficulty model = hddm.HDDM(hddmData, depends_on={ 'v': 'stim', 'a': 'stim', 't': 'stim' }) #find a good starting point to help with convergence - but, seems to run into a warning/error model.find_starting_values() #draw 2000 samples, discard 20 as burn-in model.sample(2000, burn=20, dbname='traces.db', db='pickle')
## Speed-Acc HDDM Test ## Aviva Blonder import pandas as pd import matplotlib.pyplot as plt import numpy as np import hddm ## Loading and arranging data for analysis data = hddm.load_csv("data/3_final_merged_data/speed-acc-ss-df.csv") data = data.dropna(subset = ['RT']) data['stimuli'] = np.where(data['stimuli'] == "V1", "ASL", data['stimuli']) data['stimuli'] = np.where(data['stimuli'] == "V2", "ASL", data['stimuli']) data['stimuli'] = np.where(data['stimuli'] == "Trio", "Object", data['stimuli']) data['stimuli'] = np.where(data['stimuli'] == "Bull", "Bullseye", data['stimuli']) data['rt'] = data['RT_sec'] data['response'] = data['correct'] # print(data.head(10)) #%% ## Mixture model - no parameters vary by condition model = hddm.HDDM(data, include = ('p_outlier')) model.find_starting_values() model.sample(2000, burn = 20) model.print_stats() model.plot_posteriors() model.plot_posterior_predictive(figsize = (14, 10)) #%% ## Drift and boundary separation can vary by condition var_model = hddm.HDDM(data, include = ('p_outlier'), depends_on = {'v': 'stimuli', 'a' : 'stimuli'})
def run_model(trace_id): import os, pickle, sys, time, hddm model_name = 'basic_stimcoding' nsamples = 10000 # 50.000 for real results? # find path depending on local/klimag usr = os.environ.get('USER') if usr in ['anne']: mypath = '/Users/anne/Data/projects/0/neurodec/Data/MEG-PL/Data/HDDM' if usr in ['aurai']: mypath = '/home/aurai/Data/MEG-PL/Data/HDDM' # make a folder for the outputs, combine name and time # currentTime = datetime.now().strftime('%Y-%m-%d-%H-%M-%S') # thispath = os.path.join(mypath, model_name + '_' + currentTime) thispath = os.path.join(mypath, model_name) if not os.path.exists(thispath): os.mkdir(thispath) # ============================================ # # load in data # ============================================ # mydata = hddm.load_csv(os.path.join(mypath, '2ifc_data_hddm.csv')) # specify the model m = hddm.HDDMStimCoding(mydata, stim_col='stimulus', split_param='v', drift_criterion=True, bias=True, include=('sv'), group_only_nodes=['sv'], depends_on={'t':['sessionnr'], 'v':['sessionnr'], 'a':['sessionnr'], 'dc':['sessionnr'], 'z':['sessionnr']}, p_outlier=.05) # ============================================ # # do the actual sampling # ============================================ # model_filename = os.path.join(thispath, 'modelfit-md%d'%trace_id) m.sample(nsamples, burn=nsamples/4, thin=3, db='pickle', dbname=model_filename) m.save(model_filename) # save the model, see if this works with pickle # ============================================ # # save the output values # ============================================ # results = m.gen_stats() results.to_csv(os.path.join(thispath, 'results-md%d.csv'%trace_id)) # save the DIC for this model text_file = open(os.path.join(thispath, 'DIC-md%d.txt'%trace_id), 'w') text_file.write("Model {}: {}\n".format(trace_id, m.dic)) text_file.close() # plot some output stuff in figures subfolder figpath = os.path.join(thispath, 'figures-md%d'%trace_id) if not os.path.exists(figpath): os.mkdir(figpath) m.plot_posteriors(save=True, path=figpath, format='pdf')
import os import hddm from patsy import dmatrix # for generation of (regression) design matrices import numpy as np # for basic matrix operations model_dir = os.getcwd() ############################################ ### 1. FIT THE MODEL ####################### ############################################ data = hddm.load_csv("EEGdata_hddm.csv") samples = 10000 #run 10 models of 1000O data.prevpe_bin_noern[data.prevpe_bin_noern==1] = 'A_bin' #this becomes the reference data.prevpe_bin_noern[data.prevpe_bin_noern==2] = 'B_bin' # data.prevpe_bin_noern[data.prevpe_bin_noern==3] = 'C_bin' # data.prevpe_bin_noern[data.prevpe_bin_noern==4] = 'D_bin' # data.prevpe_bin_noern[data.prevpe_bin_noern==5] = 'E_bin' # data.postpe_bin_noern[data.postpe_bin_noern==1] = 'A_bin' #this becomes the reference data.postpe_bin_noern[data.postpe_bin_noern==2] = 'B_bin' # data.postpe_bin_noern[data.postpe_bin_noern==3] = 'C_bin' # data.postpe_bin_noern[data.postpe_bin_noern==4] = 'D_bin' # data.postpe_bin_noern[data.postpe_bin_noern==5] = 'E_bin' # #define the specific link functions for z and v def z_link_func(x, data=data): stim = (np.asarray(dmatrix('0 + C(s,[[1],[-1]])', {'s':data.stimulus.ix[x.index]}))) return 1 / (1 + np.exp(-(x * stim)))