def fit_collapsed_mixture_model(self): ''' Fit the new Collapsed Mixture Model, using data created just above in generate_data_subject_split. One fit per subject, obtain parametric estimates of kappa. ''' self.dataset['collapsed_em_fits_subjects'] = dict() self.dataset['collapsed_em_fits'] = dict() for subject, subject_data_dict in self.dataset['data_subject_split']['data_subject'].iteritems(): print 'Fitting Collapsed Mixture model for subject %d' % subject # Bug here, fit is not using the good dimensionality for the number of Nontarget angles... params_fit = em_circmixtmodel_parametric.fit( self.dataset['data_subject_split']['nitems_space'], subject_data_dict['responses'], subject_data_dict['targets'], subject_data_dict['nontargets'], debug=False ) self.dataset['collapsed_em_fits_subjects'][subject] = params_fit ## Now compute mean/std collapsed_em_fits self.dataset['collapsed_em_fits']['mean'] = dict() self.dataset['collapsed_em_fits']['std'] = dict() self.dataset['collapsed_em_fits']['sem'] = dict() self.dataset['collapsed_em_fits']['values'] = dict() # Need to extract the values for a subject/nitems pair, for all keys of em_fits. Annoying dictionary indexing needed emfits_keys = params_fit.keys() for key in emfits_keys: values_allsubjects = [self.dataset['collapsed_em_fits_subjects'][subject][key] for subject in self.dataset['data_subject_split']['subjects_space']] self.dataset['collapsed_em_fits']['mean'][key] = np.mean(values_allsubjects, axis=0) self.dataset['collapsed_em_fits']['std'][key] = np.std(values_allsubjects, axis=0) self.dataset['collapsed_em_fits']['sem'][key] = self.dataset['collapsed_em_fits']['std'][key]/np.sqrt(self.dataset['data_subject_split']['subjects_space'].size) self.dataset['collapsed_em_fits']['values'][key] = values_allsubjects
def fit_collapsed_mixture_model(self): ''' Fit the new Collapsed Mixture Model, using data created just above in generate_data_subject_split. Do: * One fit per subject/nitems, using trecall as T_space * One fit per subject/trecall, using nitems as T_space * One fit per subject, using the double-powerlaw on nitems/trecall ''' Tmax = self.dataset['data_subject_split']['nitems_space'].max() Tnum = self.dataset['data_subject_split']['nitems_space'].size self.dataset['collapsed_em_fits_subjects_nitems'] = dict() self.dataset['collapsed_em_fits_nitems'] = dict() self.dataset['collapsed_em_fits_subjects_trecall'] = dict() self.dataset['collapsed_em_fits_trecall'] = dict() self.dataset['collapsed_em_fits_doublepowerlaw_subjects'] = dict() self.dataset['collapsed_em_fits_doublepowerlaw'] = dict() self.dataset['collapsed_em_fits_doublepowerlaw_array'] = np.nan*np.empty((Tnum, Tnum, 4)) for subject, subject_data_dict in self.dataset['data_subject_split']['data_subject'].iteritems(): print 'Fitting Collapsed Mixture model for subject %d' % subject if True: # Use trecall as T_space, bit weird for n_items_i, n_items in enumerate(self.dataset['data_subject_split']['nitems_space']): print '%d nitems, using trecall as T_space' % n_items params_fit = em_circularmixture_parametrickappa.fit(np.arange(1, n_items+1), subject_data_dict['responses'][n_items_i, :(n_items)], subject_data_dict['targets'][n_items_i, :(n_items)], subject_data_dict['nontargets'][n_items_i, :(n_items), :, :(n_items - 1)], debug=False) self.dataset['collapsed_em_fits_subjects_nitems'].setdefault(subject, dict())[n_items] = params_fit # Use nitems as T_space, as a function of trecall (be careful) for trecall_i, trecall in enumerate(self.dataset['data_subject_split']['nitems_space']): print 'trecall %d, using n_items as T_space' % trecall params_fit = em_circularmixture_parametrickappa.fit(np.arange(trecall, Tmax+1), subject_data_dict['responses'][trecall_i:, trecall_i], subject_data_dict['targets'][trecall_i:, trecall_i], subject_data_dict['nontargets'][trecall_i:, trecall_i], debug=False) self.dataset['collapsed_em_fits_subjects_trecall'].setdefault(subject, dict())[trecall] = params_fit # Now do the correct fit, with double powerlaw on nitems+trecall print 'Double powerlaw fit' params_fit_double = em_circularmixture_parametrickappa_doublepowerlaw.fit(self.dataset['data_subject_split']['nitems_space'], subject_data_dict['responses'], subject_data_dict['targets'], subject_data_dict['nontargets'], debug=False) self.dataset['collapsed_em_fits_doublepowerlaw_subjects'][subject] = params_fit_double if True: ## Now compute mean/std collapsed_em_fits_nitems self.dataset['collapsed_em_fits_nitems']['mean'] = dict() self.dataset['collapsed_em_fits_nitems']['std'] = dict() self.dataset['collapsed_em_fits_nitems']['sem'] = dict() self.dataset['collapsed_em_fits_nitems']['values'] = dict() # Need to extract the values for a subject/nitems pair, for all keys of em_fits. Annoying dictionary indexing needed emfits_keys = params_fit.keys() for n_items_i, n_items in enumerate(self.dataset['data_subject_split']['nitems_space']): for key in emfits_keys: values_allsubjects = [self.dataset['collapsed_em_fits_subjects_nitems'][subject][n_items][key] for subject in self.dataset['data_subject_split']['subjects_space']] self.dataset['collapsed_em_fits_nitems']['mean'].setdefault(n_items, dict())[key] = np.mean(values_allsubjects, axis=0) self.dataset['collapsed_em_fits_nitems']['std'].setdefault(n_items, dict())[key] = np.std(values_allsubjects, axis=0) self.dataset['collapsed_em_fits_nitems']['sem'].setdefault(n_items, dict())[key] = self.dataset['collapsed_em_fits_nitems']['std'][n_items][key]/np.sqrt(self.dataset['data_subject_split']['subjects_space'].size) self.dataset['collapsed_em_fits_nitems']['values'].setdefault(n_items, dict())[key] = values_allsubjects ## Same for the other ones self.dataset['collapsed_em_fits_trecall']['mean'] = dict() self.dataset['collapsed_em_fits_trecall']['std'] = dict() self.dataset['collapsed_em_fits_trecall']['sem'] = dict() self.dataset['collapsed_em_fits_trecall']['values'] = dict() # Need to extract the values for a subject/nitems pair, for all keys of em_fits. Annoying dictionary indexing needed emfits_keys = params_fit.keys() for trecall_i, trecall in enumerate(self.dataset['data_subject_split']['nitems_space']): for key in emfits_keys: values_allsubjects = [self.dataset['collapsed_em_fits_subjects_trecall'][subject][trecall][key] for subject in self.dataset['data_subject_split']['subjects_space']] self.dataset['collapsed_em_fits_trecall']['mean'].setdefault(trecall, dict())[key] = np.mean(values_allsubjects, axis=0) self.dataset['collapsed_em_fits_trecall']['std'].setdefault(trecall, dict())[key] = np.std(values_allsubjects, axis=0) self.dataset['collapsed_em_fits_trecall']['sem'].setdefault(trecall, dict())[key] = self.dataset['collapsed_em_fits_trecall']['std'][trecall][key]/np.sqrt(self.dataset['data_subject_split']['subjects_space'].size) self.dataset['collapsed_em_fits_trecall']['values'].setdefault(trecall, dict())[key] = values_allsubjects # Collapsed full double powerlaw model across subjects self.dataset['collapsed_em_fits_doublepowerlaw']['mean'] = dict() self.dataset['collapsed_em_fits_doublepowerlaw']['std'] = dict() self.dataset['collapsed_em_fits_doublepowerlaw']['sem'] = dict() self.dataset['collapsed_em_fits_doublepowerlaw']['values'] = dict() # Need to extract the values for a subject/nitems pair, for all keys of em_fits. Annoying dictionary indexing needed emfits_keys = params_fit_double.keys() for key in emfits_keys: values_allsubjects = [self.dataset['collapsed_em_fits_doublepowerlaw_subjects'][subject][key] for subject in self.dataset['data_subject_split']['subjects_space']] self.dataset['collapsed_em_fits_doublepowerlaw']['mean'][key] = np.mean(values_allsubjects, axis=0) self.dataset['collapsed_em_fits_doublepowerlaw']['std'][key] = np.std(values_allsubjects, axis=0) self.dataset['collapsed_em_fits_doublepowerlaw']['sem'][key] = self.dataset['collapsed_em_fits_doublepowerlaw']['std'][key]/np.sqrt(self.dataset['data_subject_split']['subjects_space'].size) self.dataset['collapsed_em_fits_doublepowerlaw']['values'][key] = values_allsubjects # Construct some easy arrays to compare the fit to the dataset self.dataset['collapsed_em_fits_doublepowerlaw_array'][..., 0] = self.dataset['collapsed_em_fits_doublepowerlaw']['mean']['kappa'] self.dataset['collapsed_em_fits_doublepowerlaw_array'][..., 1] = self.dataset['collapsed_em_fits_doublepowerlaw']['mean']['mixt_target_tr'] self.dataset['collapsed_em_fits_doublepowerlaw_array'][..., 2] = self.dataset['collapsed_em_fits_doublepowerlaw']['mean']['mixt_nontargets_tr'] self.dataset['collapsed_em_fits_doublepowerlaw_array'][..., 3] = self.dataset['collapsed_em_fits_doublepowerlaw']['mean']['mixt_random_tr']