-
Notifications
You must be signed in to change notification settings - Fork 1
/
histogram.py
95 lines (83 loc) · 3.15 KB
/
histogram.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# **************************************************************************** #
# #
# ::: :::::::: #
# histogram.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: obelouch <obelouch@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/11/24 17:31:50 by obelouch #+# #+# #
# Updated: 2020/12/17 06:12:37 by obelouch ### ########.fr #
# #
# **************************************************************************** #
from mylib.libft import get_flags_and_args
from src.df_houses import get_df_houses
from mylib.consts import bcolors, errors
import src.display_hist as dh
import re
# Global Variable:
dataset_file = 'ressources/dataset_train.csv'
is_all = False
feature = 11
def exit_usage(error):
'''
Print the error Msg and Exit
'''
print(f'\n{bcolors.FAIL}Error{bcolors.ENDC}: ', end='')
if error == errors.ARG_NBR:
print('Wrong number of arguments!')
elif error == errors.FLAG_NBR:
print('Too much options used!')
elif error == errors.WRONG_FLAG:
print('Wrong option used!')
elif error == errors.OUT_FTRS:
print('The feature numbers is out of range!')
else:
print('Syntax!')
print(f'\n{bcolors.WARNING}Usage{bcolors.ENDC}: ', end='')
print('python3 histogram.py %s[-d | -f{n}]%s' % (bcolors.OKCYAN, bcolors.ENDC))
print(' %s-d%s: Show all features histograms' % (bcolors.BOLD, bcolors.ENDC))
print(' %s-f%s: Show histogram of the feature "n"' % (bcolors.BOLD, bcolors.ENDC))
exit(1)
def set_flag(flags):
'''
Set the Global variable is_all
'''
global is_all
global feature
if len(flags) > 1:
exit_usage(errors.FLAG_NBR)
if flags[0] == 'd':
is_all = True
elif re.match(r'^f\{[0-9]+\}$', flags[0]):
try:
tmp = int(re.findall(r'[0-9]+', flags[0])[0])
except:
exit_usage(errors.SYNTAX)
if tmp < 1 or tmp > 13 :
exit_usage(errors.OUT_FTRS)
feature = tmp
else:
exit_usage(errors.WRONG_FLAG)
def histogram():
'''
Display the Hogwarts course that has a homogeneous score
distribution between all the four houses
'''
# Get Arguments & Flags
flags, args = get_flags_and_args()
# Check Arguments:
if len(args) > 0:
exit_usage(errors.ARG_NBR)
if len(flags) > 0:
set_flag(flags)
# get dictionary of dataframes from a csv file
df_houses = get_df_houses(dataset_file)
# Show all histograms
if is_all:
dh.display_histograms(df_houses)
# Default display the Histogram of the homogenous course (CMC)
# or the selected course via -f
else:
dh.display_histogram(df_houses, feature)
# Launch the program
histogram()