def get_pivots_by_errors(n, syllabus_id, pivot_type): "Get the top n pivots by the number of errors made on them." syllabus_query = _get_syllabus_query(syllabus_id, pivot_type) cursor = connection.cursor() cursor.execute(""" SELECT q.pivot_id, COUNT(*) AS n_errors FROM ( SELECT mco.question_id FROM drill_multiplechoiceresponse AS mcr INNER JOIN drill_multiplechoiceoption AS mco ON mcr.option_id = mco.id WHERE NOT mco.is_correct ) AS qr INNER JOIN drill_question AS q ON qr.question_id = q.id WHERE pivot_type = "%(pivot_type)s" AND pivot_id IN (%(syllabus_query)s) GROUP BY pivot_id ORDER BY n_errors DESC LIMIT %(n)d """ % { 'syllabus_query': syllabus_query, 'pivot_type': pivot_type, 'n': n, }) base_results = cursor.fetchall() pivot_ids = sequences.unzip(base_results)[0] if pivot_type == 'k': pivot_model = PartialKanji else: pivot_model = PartialLexeme pivot_map = pivot_model.objects.in_bulk(pivot_ids) return [(pivot_map[pivot_id], _get_n_responses(pivot_id), n_errors) for \ (pivot_id, n_errors) in base_results]
def get_pivots_by_questions(n, syllabus_id, pivot_type): """ Fetches the n pivots of the given type and from the given syllabus which have been used in the most questions. """ syllabus_query = _get_syllabus_query(syllabus_id, pivot_type) cursor = connection.cursor() cursor.execute(""" SELECT pivot_id, COUNT(*) AS n_exposures FROM drill_question WHERE pivot_type = "%(pivot_type)s" AND pivot_id IN (%(syllabus_query)s) GROUP BY pivot_id ORDER BY n_exposures DESC LIMIT %(n)d """ % { 'syllabus_query': syllabus_query, 'pivot_type': pivot_type, 'n': n, }) base_results = cursor.fetchall() pivot_ids = sequences.unzip(base_results)[0] if pivot_type == 'k': pivot_model = PartialKanji else: pivot_model = PartialLexeme pivot_map = pivot_model.objects.in_bulk(pivot_ids) return [(pivot_map[pid], c, _get_n_errors(pid)) for \ (pid, c) in base_results]
def __init__(self, data, x_axis=None, y_axis=None, **kwargs): if len(data) < 2: raise ValueError('need at least two columns of data') if settings.DEBUG: # Check data format assert isinstance(data, (list, tuple, numpy.ndarray)) for row in data: assert isinstance(row, (list, tuple, numpy.ndarray)) for val in row: assert isinstance(val, (int, float, numpy.number)) super(MultiLineChart, self).__init__(data, **kwargs) columns = unzip(data) self.x_data = columns[0] self.y_data = columns[1:] self.x_axis = x_axis or automatic_axis(self.x_data) self.y_axis = y_axis or automatic_axis(*self.y_data) self['cht'] = 'lxy' self['chxt'] = 'x,y' self['chxr'] = '0,%f,%f,%f|1,%f,%f,%f' % (self.x_axis + self.y_axis) self['chco'] = color_desc(len(self.y_data)) x_t = Transform(0, 100, self.x_axis[0], self.x_axis[1], strict=True) y_t = Transform(0, 100, self.y_axis[0], self.y_axis[1], strict=True) x_values = x_t.transform(self.x_data) y_values = [y_t.transform(col) for col in self.y_data] self['chd'] = 't:' + '|'.join( '|'.join((self._stringify(x_values), self._stringify(vec))) for vec in y_values)
def __init__(self, data, y_axis=None, **kwargs): labels, points = unzip(data) self.y_axis = y_axis or automatic_axis(points) super(BarChart, self).__init__(data, **kwargs) self['cht'] = 'bvg' self['chxl'] = '1:' + ('|%s|' % '|'.join(labels)) self['chxt'] = 'y,x' self['chxr'] = '0,%s,%s,%s' % tuple((map(smart_str, self.y_axis))) self['chbh'] = 'a' t = Transform(0, 100, self.y_axis[0], self.y_axis[1], strict=True) norm_points = t.transform(points) self['chd'] = 't:' + (','.join(map(smart_str, norm_points)))
def __init__(self, data, **kwargs): try: max_options = kwargs.pop('max_options') except KeyError: max_options = None super(PieChart, self).__init__(data, **kwargs) self['cht'] = 'p' self['chxt'] = 'x' sorted_data = sorted(data, key=lambda p: p[1], reverse=True) if max_options is not None and len(sorted_data) > max_options: self.__truncate_options(sorted_data, max_options) normalized_data = self.__normalize(sorted_data) labels, values = unzip(normalized_data) self['chd'] = 't:' + ','.join(smart_str(x) for x in values) self['chl'] = '|'.join(map(smart_str, labels))
def __init__(self, data, x_axis=None, y_axis=None, **kwargs): self.x_data, self.y_data = unzip(data) self.x_axis = x_axis or automatic_axis(self.x_data) self.y_axis = y_axis or automatic_axis(self.y_data) super(LineChart, self).__init__(data, **kwargs) self['cht'] = 'lxy' self['chxt'] = 'x,y' self['chxr'] = '0,%f,%f,%f|1,%f,%f,%f' % (self.x_axis + self.y_axis) self['chco'] = color_desc(1) x_t = Transform(0, 100, self.x_axis[0], self.x_axis[1], strict=True) y_t = Transform(0, 100, self.y_axis[0], self.y_axis[1], strict=True) x_values = x_t.transform(self.x_data) y_values = y_t.transform(self.y_data) self['chd'] = 't:' + self._stringify(x_values) + '|' + \ self._stringify(y_values)
def __init__(self, data, x_axis=None, y_axis=None, **kwargs): if len(data) < 2: raise ValueError('need at least two columns of data') if settings.DEBUG: # Check data format assert isinstance(data, (list, tuple, numpy.ndarray)) for row in data: assert isinstance(row, (list, tuple, numpy.ndarray)) for val in row: assert isinstance(val, (int, float, numpy.number)) super(MultiLineChart, self).__init__(data, **kwargs) columns = unzip(data) self.x_data = columns[0] self.y_data = columns[1:] self.x_axis = x_axis or automatic_axis(self.x_data) self.y_axis = y_axis or automatic_axis(*self.y_data) self['cht'] = 'lxy' self['chxt'] = 'x,y' self['chxr'] = '0,%f,%f,%f|1,%f,%f,%f' % (self.x_axis + self.y_axis) self['chco'] = color_desc(len(self.y_data)) x_t = Transform(0, 100, self.x_axis[0], self.x_axis[1], strict=True) y_t = Transform(0, 100, self.y_axis[0], self.y_axis[1], strict=True) x_values = x_t.transform(self.x_data) y_values = [y_t.transform(col) for col in self.y_data] self['chd'] = 't:' + '|'.join( '|'.join(( self._stringify(x_values), self._stringify(vec) )) for vec in y_values )