-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
72 lines (52 loc) · 2.26 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import duckdb
import matplotlib.pyplot as plt
from test_scripts import *
from dotenv import load_dotenv
import os
load_dotenv()
DB_NAME = os.getenv('DB_NAME')
def get_number_of_pokemons():
conn = duckdb.connect(DB_NAME)
table_name = 'core_pokeapi__type_and_color'
query = f"SELECT DISTINCT pokemon_name FROM {table_name}"
unique_pokemons_df = conn.execute(query).fetchdf()
conn.close()
return unique_pokemons_df.shape[0]
def get_list_of_types():
conn = duckdb.connect(DB_NAME)
table_name = 'core_pokeapi__type_and_color'
query = f"SELECT DISTINCT type_name FROM {table_name}"
unique_colors_series = conn.execute(query).fetchdf()
conn.close()
return list(unique_colors_series['type_name'])
def get_pokemons_of_type(type_name):
conn = duckdb.connect(DB_NAME)
table_name = 'core_pokeapi__type_and_color'
query = f"SELECT DISTINCT pokemon_name, color_name FROM {table_name} WHERE type_name = '{type_name}'"
pokemons_of_type_df = conn.execute(query).fetchdf()
conn.close()
return pokemons_of_type_df
def compute_color_percentages_for_type(type_pokemons_df):
number_of_pokemons = get_number_of_pokemons()
color_percentages = type_pokemons_df.groupby('color_name').size()/number_of_pokemons
return color_percentages
def plot_and_save_pie_charts():
pokemon_types = get_list_of_types()
for pokemon_type in pokemon_types:
fire_pokemons_df = get_pokemons_of_type(pokemon_type)
color_percentages = compute_color_percentages_for_type(fire_pokemons_df)
color_percentages = color_percentages.sort_values(ascending=False)
current_directory = os.path.dirname(os.path.abspath(__file__))
save_folder_path = os.path.join(current_directory, 'result_figures')
if not os.path.exists(save_folder_path):
os.makedirs(save_folder_path)
figure_name = f'{pokemon_type}_pokemons.png'
figure_path = os.path.join(save_folder_path, figure_name)
plt.figure()
plt.pie(color_percentages, labels=color_percentages.index, autopct='%1.1f%%', colors=color_percentages.keys())
plt.title(f'Pokemon type: {pokemon_type}')
plt.savefig(figure_path)
def main():
plot_and_save_pie_charts()
if __name__ == '__main__':
main()