<reponame>malekijoo/Visual-question-answering
import time
import operator
import pandas as pd
from pathlib import Path
from matplotlib import pyplot as plt
from config import configs as conf
configs = conf()


def plot_questions_types_frequency(freqs, fname='Questions_Types'):
    temp = {}
    temp['keys'] = list(freqs.keys())
    temp['vals'] = list(freqs.values())
    temp = pd.DataFrame(temp)
    temp.set_index('keys').vals.plot(kind='bar', figsize=(25,25))
    Path(configs['path_plots']).mkdir(parents=True, exist_ok=True)
    path = configs['path_plots'] + fname + str(time.strftime("%Y-%m-%d_%H-%M-%S")) +'.png'
    plt.savefig(r'{}'.format(path))


def plot_answers_types_frequency(freqs, fname='Questions_Types'):
    temp = {}
    temp['keys'] = list(freqs.keys())
    temp['vals'] = list(freqs.values())
    temp = pd.DataFrame(temp)
    temp.set_index('keys').vals.plot(kind='bar', figsize=(25,25))
    Path(configs['path_plots']).mkdir(parents=True, exist_ok=True)
    path = configs['path_plots'] + fname + str(time.strftime("%Y-%m-%d_%H-%M-%S")) +'.png'
    plt.savefig(r'{}'.format(path))



def plot_answers_frequency(freqs, fname, split_keys=[]):

    sorted_temp = sorted(freqs.items(), key=operator.itemgetter(1), reverse=True)
    data_size = sum([i[1] for i in sorted_temp])

    print('Data Size: {0}'.format(data_size))
    print('Number of Unique Answers: {0}'.format(len(sorted_temp)))

    temp = {}
    temp['keys'] = split_keys + [len(sorted_temp)]
    temp['vals'] = [sum([j[1] for j in sorted_temp[:sk]]) / data_size  for sk in split_keys] + [1]

    temp = pd.DataFrame(temp)
    temp.set_index('keys').vals.plot(kind='bar', figsize=(25,25))
    Path(configs['path_plots']).mkdir(parents=True, exist_ok=True)
    path = configs['path_plots'] + fname + str(time.strftime("%Y-%m-%d_%H-%M-%S")) +'.png'
    plt.savefig(r'{}'.format(path))

    for sk in split_keys:
        sum_temp = sum([j[1] for j in sorted_temp[:sk]])
        print('{0} of Most Frequent Answers are in {1} of {2} questions, ({3} % of total data)'.format(sk, sum_temp, data_size, sum_temp / data_size * 100))

    print('{0} of Most Frequent Answers are in {1} of {2} questions, ({3} % of total data)'.format(len(sorted_temp), data_size, data_size, data_size / data_size * 100))


def plot_tokens_length_frequency(freqs, fname, split_keys=[]):
    sorted_freqs = sorted(freqs.items(), key=operator.itemgetter(0), reverse=False)
    data_size = sum([i[1] for i in sorted_freqs])

    temp = {}
    temp['keys'] = [i[0] for i in sorted_freqs]
    temp['vals'] = [i[1] for i in sorted_freqs]
    temp = pd.DataFrame(temp)
    temp.set_index('keys').vals.plot(kind='bar', figsize=(25,25))
    Path(configs['path_plots']).mkdir(parents=True, exist_ok=True)
    path = configs['path_plots'] + fname + str(time.strftime("%Y-%m-%d_%H-%M-%S")) +'.png'
    plt.savefig(r'{}'.format(path))

    count = 0
    for sf in sorted_freqs:
        count = count + sf[1]
        print('{0} Tokens = {1} of {2} ({3} %)'.format(sf[0], count, data_size, count / data_size * 100))



def plot_tokens_frequency(freqs, fname, split_keys=[]):

    sorted_temp = sorted(freqs.items(), key=operator.itemgetter(1), reverse=True)
    data_size = sum([i[1] for i in sorted_temp])

    print('Data Size: {0}'.format(data_size))
    print('Number of Unique Tokens: {0}'.format(len(sorted_temp)))

    temp = {}
    temp['keys'] = split_keys + [len(sorted_temp)]
    temp['vals'] = [sum([j[1] for j in sorted_temp[:sk]]) / data_size  for sk in split_keys] + [1]

    temp = pd.DataFrame(temp)
    temp.set_index('keys').vals.plot(kind='bar', figsize=(25,25))
    Path(configs['path_plots']).mkdir(parents=True, exist_ok=True)
    path = configs['path_plots'] + fname + str(time.strftime("%Y-%m-%d_%H-%M-%S")) +'.png'
    plt.savefig(r'{}'.format(path))

    for sk in split_keys:
        sum_temp = sum([j[1] for j in sorted_temp[:sk]])
        print('{0} of Most Frequent Tokens are in {1} of {2} questions, ({3} % of total data)'.format(sk, sum_temp, data_size, sum_temp / data_size * 100))

    print('{0} of Most Frequent Tokens are in {1} of {2} questions, ({3} % of total data)'.format(len(sorted_temp), data_size, data_size, data_size / data_size * 100))

import time
import operator
import pandas as pd
from pathlib import Path
from matplotlib import pyplot as plt
from config import configs as conf
configs = conf()


def plot_questions_types_frequency(freqs, fname='Questions_Types'):
    temp = {}
    temp['keys'] = list(freqs.keys())
    temp['vals'] = list(freqs.values())
    temp = pd.DataFrame(temp)
    temp.set_index('keys').vals.plot(kind='bar', figsize=(25,25))
    Path(configs['path_plots']).mkdir(parents=True, exist_ok=True)
    path = configs['path_plots'] + fname + str(time.strftime("%Y-%m-%d_%H-%M-%S")) +'.png'
    plt.savefig(r'{}'.format(path))


def plot_answers_types_frequency(freqs, fname='Questions_Types'):
    temp = {}
    temp['keys'] = list(freqs.keys())
    temp['vals'] = list(freqs.values())
    temp = pd.DataFrame(temp)
    temp.set_index('keys').vals.plot(kind='bar', figsize=(25,25))
    Path(configs['path_plots']).mkdir(parents=True, exist_ok=True)
    path = configs['path_plots'] + fname + str(time.strftime("%Y-%m-%d_%H-%M-%S")) +'.png'
    plt.savefig(r'{}'.format(path))



def plot_answers_frequency(freqs, fname, split_keys=[]):

    sorted_temp = sorted(freqs.items(), key=operator.itemgetter(1), reverse=True)
    data_size = sum([i[1] for i in sorted_temp])

    print('Data Size: {0}'.format(data_size))
    print('Number of Unique Answers: {0}'.format(len(sorted_temp)))

    temp = {}
    temp['keys'] = split_keys + [len(sorted_temp)]
    temp['vals'] = [sum([j[1] for j in sorted_temp[:sk]]) / data_size  for sk in split_keys] + [1]

    temp = pd.DataFrame(temp)
    temp.set_index('keys').vals.plot(kind='bar', figsize=(25,25))
    Path(configs['path_plots']).mkdir(parents=True, exist_ok=True)
    path = configs['path_plots'] + fname + str(time.strftime("%Y-%m-%d_%H-%M-%S")) +'.png'
    plt.savefig(r'{}'.format(path))

    for sk in split_keys:
        sum_temp = sum([j[1] for j in sorted_temp[:sk]])
        print('{0} of Most Frequent Answers are in {1} of {2} questions, ({3} % of total data)'.format(sk, sum_temp, data_size, sum_temp / data_size * 100))

    print('{0} of Most Frequent Answers are in {1} of {2} questions, ({3} % of total data)'.format(len(sorted_temp), data_size, data_size, data_size / data_size * 100))


def plot_tokens_length_frequency(freqs, fname, split_keys=[]):
    sorted_freqs = sorted(freqs.items(), key=operator.itemgetter(0), reverse=False)
    data_size = sum([i[1] for i in sorted_freqs])

    temp = {}
    temp['keys'] = [i[0] for i in sorted_freqs]
    temp['vals'] = [i[1] for i in sorted_freqs]
    temp = pd.DataFrame(temp)
    temp.set_index('keys').vals.plot(kind='bar', figsize=(25,25))
    Path(configs['path_plots']).mkdir(parents=True, exist_ok=True)
    path = configs['path_plots'] + fname + str(time.strftime("%Y-%m-%d_%H-%M-%S")) +'.png'
    plt.savefig(r'{}'.format(path))

    count = 0
    for sf in sorted_freqs:
        count = count + sf[1]
        print('{0} Tokens = {1} of {2} ({3} %)'.format(sf[0], count, data_size, count / data_size * 100))



def plot_tokens_frequency(freqs, fname, split_keys=[]):

    sorted_temp = sorted(freqs.items(), key=operator.itemgetter(1), reverse=True)
    data_size = sum([i[1] for i in sorted_temp])

    print('Data Size: {0}'.format(data_size))
    print('Number of Unique Tokens: {0}'.format(len(sorted_temp)))

    temp = {}
    temp['keys'] = split_keys + [len(sorted_temp)]
    temp['vals'] = [sum([j[1] for j in sorted_temp[:sk]]) / data_size  for sk in split_keys] + [1]

    temp = pd.DataFrame(temp)
    temp.set_index('keys').vals.plot(kind='bar', figsize=(25,25))
    Path(configs['path_plots']).mkdir(parents=True, exist_ok=True)
    path = configs['path_plots'] + fname + str(time.strftime("%Y-%m-%d_%H-%M-%S")) +'.png'
    plt.savefig(r'{}'.format(path))

    for sk in split_keys:
        sum_temp = sum([j[1] for j in sorted_temp[:sk]])
        print('{0} of Most Frequent Tokens are in {1} of {2} questions, ({3} % of total data)'.format(sk, sum_temp, data_size, sum_temp / data_size * 100))

    print('{0} of Most Frequent Tokens are in {1} of {2} questions, ({3} % of total data)'.format(len(sorted_temp), data_size, data_size, data_size / data_size * 100))