forked from LucasRicciardi/4522
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_summary_statistics.py
211 lines (99 loc) · 4.53 KB
/
class_summary_statistics.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import sys
import traceback
import time
from math import sqrt
class SummaryStatistics(object):
"""
calculate number of observations, arithmetic mean, median
and sample standard deviation using standard procedures
"""
def __init__(self):
pass
def calculate_number_observation(self, one_dimensional_array):
"""
calculate number of observations
:param one_dimensional_array: numpy one dimensional array
:return number of observations value
"""
number_observation = 0
try:
number_observation = one_dimensional_array.size
except Exception:
self.print_exception_message()
return number_observation
def calculate_arithmetic_mean(self, one_dimensional_array, number_observation):
"""
calculate arithmetic mean
:param one_dimensional_array: numpy one dimensional array
:param number_observation: number of observations
:return arithmetic mean value
"""
arithmetic_mean = 0.0
try:
sum_result = 0.0
for i in range(number_observation):
sum_result += one_dimensional_array[i]
arithmetic_mean = sum_result / number_observation
except Exception:
self.print_exception_message()
return arithmetic_mean
def calculate_median(self, one_dimensional_array, number_observation):
"""
calculate median
:param one_dimensional_array: numpy one dimensional array
:param number_observation: number of observations
:return median value
"""
median = 0.0
try:
one_dimensional_array.sort()
half_position = number_observation // 2
if not number_observation % 2:
median = (one_dimensional_array[half_position - 1] + one_dimensional_array[half_position]) / 2.0
else:
median = one_dimensional_array[half_position]
except Exception:
self.print_exception_message()
return median
def calculate_sample_standard_deviation(self, one_dimensional_array, number_observation, arithmetic_mean):
"""
calculate sample standard deviation
:param one_dimensional_array: numpy one dimensional array
:param number_observation: number of observations
:param arithmetic_mean: arithmetic mean value
:return sample standard deviation value
"""
sample_standard_deviation = 0.0
try:
sum_result = 0.0
for i in range(number_observation):
sum_result += pow((one_dimensional_array[i] - arithmetic_mean), 2)
sample_variance = sum_result / (number_observation - 1)
sample_standard_deviation = sqrt(sample_variance)
except Exception:
self.print_exception_message()
return sample_standard_deviation
def print_exception_message(self, message_orientation = "horizontal"):
"""
print full exception message
:param message_orientation: horizontal or vertical
:return none
"""
try:
exc_type, exc_value, exc_tb = sys.exc_info()
file_name, line_number, procedure_name, line_code = traceback.extract_tb(exc_tb)[-1]
time_stamp = " [Time Stamp]: " + str(time.strftime("%Y-%m-%d %I:%M:%S %p"))
file_name = " [File Name]: " + str(file_name)
procedure_name = " [Procedure Name]: " + str(procedure_name)
error_message = " [Error Message]: " + str(exc_value)
error_type = " [Error Type]: " + str(exc_type)
line_number = " [Line Number]: " + str(line_number)
line_code = " [Line Code]: " + str(line_code)
if (message_orientation == "horizontal"):
print( "An error occurred:{};{};{};{};{};{};{}".format(time_stamp, file_name, procedure_name, error_message, error_type, line_number, line_code))
elif (message_orientation == "vertical"):
print( "An error occurred:\n{}\n{}\n{}\n{}\n{}\n{}\n{}".format(time_stamp, file_name, procedure_name, error_message, error_type, line_number, line_code))
else:
pass
except Exception:
pass