def test(): ret = '' ret += 'query=' + str(util.get_query()) + '\n' ret += '\n' ret += 'q=None\n' ret += 'query=' + str(util.get_query(q=None)) + '\n' ret += '\n' ret += 'a\n' ret += 'query=' + str(util.get_query('a')) + '\n' ret += '\n' ret += '?a=1 , a\n' ret += 'query=' + str(util.get_query('a', 'a=1')) + '\n' ret += '\n' ret += '?a=1&b=2 , b\n' ret += 'query=' + str(util.get_query('b', 'a=1&b=2')) + '\n' ret += '\n' ret += '?a=1&b=2&a=3 , a\n' ret += 'query=' + str(util.get_query('a', 'a=1&b=2&a=3')) + '\n' ret += '\n' ret += '?a=1&a=2+%26&b=3 , a\n' ret += 'query=' + str(util.get_query('a', 'a=1&a=2+%26&b=3')) + '\n' ret += '\n' ret += '?%E3%81%82=%E3%81%84 , %E3%81%82\n' ret += 'query=' + str(util.get_query('あ', '%E3%81%82=%E3%81%84')) + '\n' ret += '\n' return ret
def plot(dest, results_path, counterfactual, likelihood): if likelihood == 'ipe': likelihood = 'ipe_' + util.get_query() # load in the model data, which includes fitted parameters data = pd\ .read_csv(os.path.join(results_path, 'model_belief_by_trial_fit.csv'))\ .groupby(['fitted', 'counterfactual', 'likelihood'])\ .get_group((True, counterfactual, likelihood))\ .drop_duplicates(['version', 'model', 'pid', 'B'])\ .set_index(['version', 'model', 'pid'])\ .sortlevel() # double check that there is exactly one parameter for each pid assert data.index.is_unique # plotting config stuff plot_config = util.load_config()["plots"] all_colors = plot_config["colors"] colors = {'static': all_colors[1], 'learning': all_colors[0]} # create the figure and plot the histograms fig, axes = plt.subplots(2, 3, sharex=True) for i, version in enumerate(['H', 'G', 'I']): for j, model in enumerate(['static', 'learning']): hist(axes[j, i], data.ix[(version, model)]['B'], plot_config['darkgrey']) # set titles and axis labels for i in range(3): axes[0, i].set_title('Experiment {}'.format(i + 1), y=1.05) for ax in axes[:, 0]: ax.set_ylabel("% participants") for ax in axes[1]: ax.set_xlabel(r"Best fit learning rate ($\beta$)") for i, label in enumerate(['Static', 'Learning']): mid = sum(axes[i, 0].get_ylim()) / 2.0 axes[i, 0].text( -2.6, mid, label, rotation=90, fontsize=12, # same as title font size verticalalignment='center') # clear top and right axis lines sns.despine() # set figure size fig.set_figwidth(6.5) fig.set_figheight(3.25) plt.draw() plt.tight_layout() plt.subplots_adjust(left=0.14) # save for pth in dest: util.save(pth, close=False)
def load_virtual(self): from util import get_query for tb_name, vtable in self._connobj.vtables(): try: self._vtables[tb_name] = None query = get_query(vtable).bind(self._connobj).object if hasattr(query, 'alias'): query = query.alias(tb_name) self._vtables[tb_name] = query logger.info('Load virtual table success: %s' % tb_name) except Exception, e: logger.error('Load virtual table(%s) failed: %s' % (tb_name, e), exc_info=True)
def handle_query(request, name, **kwargs): ''' Handles generic view. Category is where this should be place (per student, per problem, etc.) Name is specific ''' global query_object if query_object is None: from util import get_query query_object = get_query(None) if name[0] == '_': raise SuspiciousOperation(name+' called') kwargs.update(request.POST.items()) kwargs.update(request.GET.items()) results = query_object.__getattr__(name)(**kwargs) if isinstance(results, basestring): return HttpResponse(results) else: return HttpResponse(json.dumps(results))
def test(): ret = 'query=' + str(util.get_query()) + '\n' ret += 'a=' + str(util.get_query('a')) + '\n' ret += 'b=' + str(util.get_query('b')) + '\n' return ret
def plot(dest, results_path, counterfactual, likelihood): if likelihood == 'ipe': likelihood = 'ipe_' + util.get_query() # load in the human responses human = pd\ .read_csv(os.path.join(results_path, 'human_mass_accuracy_by_trial.csv'))\ .groupby('kappa0')\ .get_group('all')\ .groupby(['version', 'num_mass_trials', 'trial'])\ .filter(filter_trials)\ .set_index('version') # load in the model responses model = pd\ .read_csv(os.path.join(results_path, 'model_mass_accuracy_by_trial.csv'))\ .groupby(['likelihood', 'counterfactual', 'kappa0'])\ .get_group((likelihood, counterfactual, 'all'))\ .groupby(['version', 'num_mass_trials', 'trial', 'model', 'fitted'])\ .filter(filter_trials)\ .set_index(['version', 'fitted', 'model'])\ .sortlevel() # load in the log likelihood ratios llhr = pd\ .read_csv(os.path.join(results_path, 'model_log_lh_ratios.csv'))\ .groupby(['likelihood', 'counterfactual', 'fitted'])\ .get_group((likelihood, counterfactual, True))\ .groupby(['version', 'num_mass_trials'])\ .filter(filter_trials)\ .set_index(['version'])['llhr'] # load in the bayes factors factors = pd\ .read_csv(os.path.join(results_path, 'bayes_factors.csv'))\ .groupby(['likelihood', 'counterfactual'])\ .get_group((likelihood, counterfactual))\ .groupby(['version', 'num_mass_trials'])\ .filter(filter_trials)\ .set_index(['version'])['logK'] # colors and line styles plot_config = util.load_config()["plots"] darkgrey = plot_config["darkgrey"] lines = {'human': '-', 'static': ':', 'learning': '--'} colors = {'human': darkgrey, 'static': darkgrey, 'learning': darkgrey} fig, axes = plt.subplots(2, 2, sharey=True) # top left subplot: IPE model for experiment 1 plot_all(axes[0, 0], None, model.ix[('H', False)], lines, colors) axes[0, 0].set_title('(a) IPE observer model') axes[0, 0].set_xlabel('Trial') axes[0, 0].set_ylabel('Pr(correct ratio chosen)') axes[0, 0].set_xticks([1, 5, 10, 15, 20]) axes[0, 0].set_xlim([1, 20]) axes[0, 0].set_ylim(0.48, 1.02) # top right subplot: experiment 1a plot_all(axes[0, 1], human.ix['H'], model.ix[('H', True)], lines, colors) axes[0, 1].set_title('(b) Experiment 1') axes[0, 1].set_xlabel('Trial') axes[0, 1].set_xticks([1, 5, 10, 15, 20]) axes[0, 1].set_xlim([1, 20]) add_llhr_and_factor(axes[0, 1], llhr.ix['H'], factors.ix['H']) # bottom left subplot: experiment 1b plot_all(axes[1, 0], human.ix['G'], model.ix[('G', True)], lines, colors) axes[1, 0].set_title('(c) Experiment 2') axes[1, 0].set_xlabel('Trial') axes[1, 0].set_ylabel('Pr(correct ratio chosen)') axes[1, 0].set_xticks([1, 2, 3, 4, 6, 9, 14, 20]) axes[1, 0].set_xlim([1, 20]) add_llhr_and_factor(axes[1, 0], llhr.ix['G'], factors.ix['G']) # bottom right subplot: experiment 2 (between subjects) plot_all(axes[1, 1], human.ix['I'], model.ix[('I', True)], lines, colors) axes[1, 1].set_title('(d) Experiment 3') axes[1, 1].set_xlabel('Trial') axes[1, 1].set_xticks([1, 2, 3, 5, 10]) axes[1, 1].set_xlim([1, 10]) add_llhr_and_factor(axes[1, 1], llhr.ix['I'], factors.ix['I']) # clear top and right axis lines sns.despine() # make the legend make_legend(axes[0, 0], lines, colors) # set figure size fig.set_figwidth(6) fig.set_figheight(4.8) plt.draw() plt.tight_layout() # save for pth in dest: util.save(pth, close=False)