Esempio n. 1
0
def see_question_answers(request, question):
    #load csv
    df = utils.get_whole_survey()
    #load questionchoices txt
    file2_ = open(
        os.path.join(settings.BASE_DIR, 'clusters/RSQquestionchoices.txt'))
    dic_source = rt.read(file2_)
    about = dic_source['about']
    questions_txt = dic_source['questions']
    file2_.close()

    charts = []
    try:
        chart = get_question_chart(df, questions_txt[question])
        charts.append(chart)
    except KeyError:
        question_obj = questions_txt[question]
        choices = question_obj.choices
        letters = choices.keys()
        for letter in letters:
            try:
                chart = get_question_chart(df,
                                           question_obj,
                                           letter=letter,
                                           choice=choices[letter])
                charts.append(chart)
            except KeyError:
                continue

    context = {
        'charts': charts,
        'question': question,
        'about': about,
    }
    return render(request, 'clusters/question.html', context)
Esempio n. 2
0
def group_compare(request):
    cluster = get_object_or_404(Cluster, pk=3)
    #load csv into pandas.DataFrame
    dic = rc.filter_by_cluster(utils.get_whole_survey(), cluster)
    df = dic['df']
    size_str = "%d / %d" % (len(df.index), dic['orig_size'])
    clusters_dict = clustering.get_subclusters_length(cluster, dic['df'])
    #load questions textfile into list
    file2_ = open(
        os.path.join(settings.BASE_DIR, 'clusters/RSQquestionchoices.txt'))
    dic_source = rt.read(file2_)
    about = dic_source['about']
    questions_txt = dic_source['questions']
    file2_.close()

    #get cluster questions as strings
    questions_strs = get_questions_as_str(questions_txt)

    #get charts
    charts = get_charts(df, questions_txt, cluster, 'a')

    context = {
        'cluster': cluster,
        'charts': charts,
        #'compare_chart': compare_chart,
        'df_size': size_str,  #no of rows in df
        'questions_strs': questions_strs,
        'about': about,
        'subcluster_values': list(clusters_dict.values()),
        'num_of_clusters': cluster.num_of_clusters,
        'compare': False,
    }
    return render(request, 'clusters/group_compare.html', context)
Esempio n. 3
0
def json3(request, cluster_id, question_id):
    cluster = get_object_or_404(Cluster, pk=cluster_id)
    #load csv into pandas.DataFrame
    dic = rc.filter_by_cluster(utils.get_whole_survey(), cluster)
    df = dic['df']

    output = rc2.get_data_for_map3(df, question_id)

    return JsonResponse(output, safe=False)
Esempio n. 4
0
def json4(request, cluster_id, subcluster_id, question_id, choice_id):
    """for ward chart change
    """
    cluster = get_object_or_404(Cluster, pk=cluster_id)
    #load csv into pandas.DataFrame
    df = rc.filter_by_cluster_only(utils.get_whole_survey(), cluster)

    if subcluster_id != 'a':  #if is subcluster, then filter subcluster
        #TODO need to make permanent, subcluster id on df
        tempdf = clustering.get_subcluster_list(cluster, df)
        df = clustering.filter_by_subcluster(tempdf, subcluster_id)

    output = rc2.get_data_for_map4(df, question_id, choice_id)

    return JsonResponse(output, safe=False)
Esempio n. 5
0
def json2(request, cluster_id, subcluster_id, question_id, choice_id):
    """ creates json for map change
    """
    cluster = get_object_or_404(Cluster, pk=cluster_id)
    #load csv into pandas.DataFrame
    dic = rc.filter_by_cluster(utils.get_whole_survey(), cluster)
    df = dic['df']

    if subcluster_id != 'a':  #if is subcluster, then filter subcluster
        tempdf = clustering.get_subcluster_list(cluster, df)
        df = clustering.filter_by_subcluster(tempdf, subcluster_id)

    output = rc2.get_data_for_map2(df, question_id, choice_id)

    return JsonResponse(output, safe=False)
Esempio n. 6
0
def survey_questions(request):
    #load csv
    df = utils.get_whole_survey()
    #load questionchoices txt
    file2_ = open(
        os.path.join(settings.BASE_DIR, 'clusters/RSQquestionchoices.txt'))
    dic_source = rt.read(file2_)
    about = dic_source['about']
    questions_txt = dic_source['questions']
    file2_.close()

    context = {
        'questions': questions_txt.values(),
        'about': about,
    }
    return render(request, 'clusters/survey.html', context)
Esempio n. 7
0
def get_data_for_group_compare(question, choice):
    """ outputs list for Group Compare view
        Group = model.Cluster
        Value = value of group filtered by question and choice
        output = [[Group, Value], ...,  ...]
    """
    output = []

    #column names
    output.append(['Group', 'Orig Value', 'Percent', '% Diff from Avg'])

    if question == '0' and choice == '0':

        cluster_models = Cluster.objects.all()
        for cluster in cluster_models:
            output.append([cluster.name, 0, 0, 0])

        return output

    df = utils.get_whole_survey()
    """
    for each cluster
        v = get value(c)
        output.append(c, v)
    """
    cluster_all = Cluster.objects.get(pk=3)
    value = df[question].value_counts()[int(choice)]
    average = float(value) / df.shape[0]

    cluster_models = Cluster.objects.all()
    for cluster in cluster_models:
        temp = rc.filter_by_cluster_only(df, cluster)
        try:
            value = temp[question].value_counts()[int(choice)]
            percent = float(value) / temp.shape[0]  #percent
        except KeyError:
            value = 0
            percent = 0
        except IndexError:
            """ when value_counts only gives #NULL! """
            value = 0
            percent = 0
        diff_from_avg = (percent - average) * 100
        output.append([cluster.name, value, percent, diff_from_avg])

    return output
Esempio n. 8
0
def json1(request, cluster_id, subcluster_id):
    """ used for map """
    cluster = get_object_or_404(Cluster, pk=cluster_id)
    #load csv into pandas.DataFrame
    dic = rc.filter_by_cluster(utils.get_whole_survey(), cluster)
    df = dic['df']
    if subcluster_id != 'a':
        df = clustering.get_subclusters(cluster, df)[int(subcluster_id)]

    #load questions textfile into list
    file2_ = open(
        os.path.join(settings.BASE_DIR, 'clusters/RSQquestionchoices.txt'))
    dic_source = rt.read(file2_)
    about = dic_source['about']
    questions_txt = dic_source['questions']
    file2_.close()

    output = rc2.get_data_for_map(df, questions_txt['WARD'])

    return JsonResponse(output, safe=False)
Esempio n. 9
0
def detail(request, cluster_id):
    cluster = get_object_or_404(Cluster, pk=cluster_id)
    #load csv into pandas.DataFrame
    dic = rc.filter_by_cluster(utils.get_whole_survey(), cluster)
    df = dic['df']
    group_size = len(df.index)
    survey_size = dic['orig_size']

    if group_size == 0:
        return render(request, 'clusters/emptygroup.html',
                      {'cluster': cluster})

    clusters_dict = clustering.get_subclusters_length(cluster, dic['df'])
    #load questions textfile into list
    file2_ = open(
        os.path.join(settings.BASE_DIR, 'clusters/RSQquestionchoices.txt'))
    dic_source = rt.read(file2_)
    file2_.close()
    about = dic_source['about']
    questions_txt = dic_source['questions']

    #get cluster questions as strings
    questions_strs = get_questions_as_str(questions_txt)

    #get charts
    charts = get_charts(df, questions_txt, cluster, 'a')

    context = {
        'cluster': cluster,
        'subcluster_id': 'a',
        'charts': charts,
        'group_size': group_size,  #no of rows in df
        'survey_size': survey_size,
        'questions_strs': questions_strs,
        'about': about,
        'subcluster_values': list(clusters_dict.values()),
        'num_of_clusters': cluster.num_of_clusters,
        'compare': False,
    }
    return render(request, 'clusters/detail.html', context)
Esempio n. 10
0
def subcluster_detail(request, cluster_id, subcluster_id):
    cluster = get_object_or_404(Cluster, pk=cluster_id)
    #load csv into pandas.DataFrame
    dic = rc.filter_by_cluster(utils.get_whole_survey(), cluster)
    df = dic['df']
    df = clustering.get_subclusters(cluster, df)[0]  #dicitonary

    size_str = "%d / %d" % (len(df.index), dic['orig_size'])
    clusters_dict = clustering.get_subclusters_length(cluster, dic['df'])
    #load questions textfile into list
    file2_ = open(
        os.path.join(settings.BASE_DIR, 'clusters/RSQquestionchoices.txt'))
    dic_source = rt.read(file2_)
    about = dic_source['about']
    questions_txt = dic_source['questions']
    file2_.close()

    #get cluster questions as strings
    questions_strs = get_questions_as_str(questions_txt)

    #get charts
    charts = get_charts(df, questions_txt, cluster, subcluster_id)

    context = {
        'cluster': cluster,
        'subcluster_id': subcluster_id,
        'charts': charts,
        'group_size': len(df.index),  #no of rows in df
        'survey_size': dic['orig_size'],
        'questions_strs': questions_strs,
        'about': about,
        'subcluster_values': list(clusters_dict.values()),
        'num_of_clusters': cluster.num_of_clusters,
        'issubcluster': True,
    }
    return render(request, 'clusters/detail.html', context)