autopep8 on all files but operator.py

This commit is contained in:
Benjamin Bertrand
2016-02-13 07:04:08 +03:00
parent e43544006e
commit 47d43849d0
15 changed files with 536 additions and 366 deletions

View File

@@ -7,12 +7,14 @@
#
#
# TODO: Rendre toutes les réponses Explicable!! |mar. janv. 12 09:41:00 EAT 2016
# TODO: Rendre toutes les réponses Explicable!! |mar. janv. 12 09:41:00
# EAT 2016
from math import sqrt, ceil
from .number_tools import number_factory
from .random_generator import random_generator
class Dataset(list):
""" A dataset (a list) with statistics and latex rendering methods
@@ -30,11 +32,11 @@ class Dataset(list):
"""
@classmethod
def random(cls, length, data_name = "Valeurs", \
distrib = "gauss", rd_args = (0,1), \
nbr_format = lambda x:round(x,2), \
v_min = None, v_max = None, \
exact_mean = None):
def random(cls, length, data_name="Valeurs",
distrib="gauss", rd_args=(0, 1),
nbr_format=lambda x: round(x, 2),
v_min=None, v_max=None,
exact_mean=None):
""" Generate a random list of value
:param length: length of the dataset
@@ -45,15 +47,15 @@ class Dataset(list):
:param v_max: maximum accepted value
:param exact_mean: if set, the last generated number will be create in order that the computed mean is exacly equal to "exact_mean"
"""
data = random_generator(length,\
distrib, rd_args, \
nbr_format, \
v_min, v_max, \
exact_mean)
data = random_generator(length,
distrib, rd_args,
nbr_format,
v_min, v_max,
exact_mean)
return cls(data, data_name = data_name)
return cls(data, data_name=data_name)
def __init__(self, data = [], data_name = "Valeurs"):
def __init__(self, data=[], data_name="Valeurs"):
"""
Create a numeric data set
@@ -86,7 +88,7 @@ class Dataset(list):
@number_factory
def mean(self):
return self.sum()/self.effectif_total()
return self.sum() / self.effectif_total()
@number_factory
def deviation(self):
@@ -96,7 +98,7 @@ class Dataset(list):
@number_factory
def variance(self):
return self.deviation()/self.effectif_total()
return self.deviation() / self.effectif_total()
@number_factory
def sd(self):
@@ -113,10 +115,15 @@ class Dataset(list):
>>> w.quartiles()
(0, 2.5, 5.5, 8.5, 11)
"""
return (min(self) , self.quartile(1) , self.quartile(2) , self.quartile(3), max(self))
return (
min(self),
self.quartile(1),
self.quartile(2),
self.quartile(3),
max(self))
@number_factory
def quartile(self, quartile = 1):
def quartile(self, quartile=1):
"""
Calcul un quartile de la série.
@@ -145,11 +152,11 @@ class Dataset(list):
# -1 to match with list indexing
position = self.posi_quartile(quartile) - 1
if position.is_integer():
return (self[int(position)] + self[int(position)+1])/2
return (self[int(position)] + self[int(position) + 1]) / 2
else:
return self[ceil(position)]
def posi_quartile(self, quartile = 1):
def posi_quartile(self, quartile=1):
"""
Calcul la position du quartile
@@ -162,20 +169,22 @@ class Dataset(list):
# --------------------------
# Rendu latex
def tabular_latex(self, nbr_lines = 1):
def tabular_latex(self, nbr_lines=1):
""" Latex code to display dataset as a tabular """
d_per_line = self.effectif_total() // nbr_lines
d_last_line = self.effectif_total() % d_per_line
splited_data = [self[x:x+d_per_line] for x in range(0, self.effectif_total(), d_per_line)]
splited_data = [self[x:x + d_per_line]
for x in range(0, self.effectif_total(), d_per_line)]
# On ajoute les éléments manquant pour la dernière line
if d_last_line:
splited_data[-1] += [' ']*(d_per_line - d_last_line)
splited_data[-1] += [' '] * (d_per_line - d_last_line)
# Construction du tableau
latex = "\\begin{{tabular}}{{|c|*{{{nbr_col}}}{{c|}}}} \n".format(nbr_col = d_per_line)
latex = "\\begin{{tabular}}{{|c|*{{{nbr_col}}}{{c|}}}} \n".format(
nbr_col=d_per_line)
latex += "\t\t \hline \n"
d_lines = [' & '.join(map(str,l)) for l in splited_data]
d_lines = [' & '.join(map(str, l)) for l in splited_data]
latex += " \\\\ \n \\hline \n".join(d_lines)
latex += " \\\\ \n \\hline \n"
@@ -184,9 +193,7 @@ class Dataset(list):
return latex
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

View File

@@ -19,9 +19,7 @@ def number_factory(fun):
return wrapper
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

View File

@@ -4,11 +4,11 @@
from random import randint, uniform, gauss, choice
def random_generator(length,\
distrib = gauss, rd_args = (0,1), \
nbr_format = lambda x:round(x,2), \
v_min = None, v_max = None, \
exact_mean = None):
def random_generator(length,
distrib=gauss, rd_args=(0, 1),
nbr_format=lambda x: round(x, 2),
v_min=None, v_max=None,
exact_mean=None):
""" Generate a random list of value
:param length: length of the dataset
@@ -28,22 +28,26 @@ def random_generator(length,\
"""
# if exact_mean is set, we create automaticaly only length-1 value
if exact_mean != None:
if exact_mean is not None:
length = length - 1
# build function to test created values
if v_min == None:
if v_min is None:
v1 = lambda x: True
else:
v1 = lambda x: x >= v_min
if v_max == None:
if v_max is None:
v2 = lambda x: True
else:
v2 = lambda x: x <= v_max
validate = lambda x : v1(x) and v2(x)
validate = lambda x: v1(x) and v2(x)
# get distrib function
distribs = {"gauss": gauss, "uniform": uniform, "randint":randint, "choice":choice}
distribs = {
"gauss": gauss,
"uniform": uniform,
"randint": randint,
"choice": choice}
try:
distrib(*rd_args)
except TypeError:
@@ -59,10 +63,11 @@ def random_generator(length,\
data.append(v)
# Build last value
if exact_mean != None:
last_v = nbr_format((length+1) * exact_mean - sum(data))
if exact_mean is not None:
last_v = nbr_format((length + 1) * exact_mean - sum(data))
if not validate(last_v):
raise ValueError("Can't build the last value. Conflict between v_min/v_max and exact_mean")
raise ValueError(
"Can't build the last value. Conflict between v_min/v_max and exact_mean")
data.append(last_v)
return data
@@ -71,4 +76,3 @@ def random_generator(length,\
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

View File

@@ -35,7 +35,12 @@ class WeightedDataset(dict):
"""
def __init__(self, datas = [], data_name = "Valeurs", weights = [], weight_name = "Effectifs"):
def __init__(
self,
datas=[],
data_name="Valeurs",
weights=[],
weight_name="Effectifs"):
"""
Initiate the WeightedDataset
"""
@@ -45,14 +50,14 @@ class WeightedDataset(dict):
if len(datas) != len(weights):
raise ValueError("Datas and weights should have same length")
else:
weightedDatas = {i[0]:i[1] for i in zip(datas, weights)}
weightedDatas = {i[0]: i[1] for i in zip(datas, weights)}
dict.__init__(self, weightedDatas)
self.data_name = data_name
self.weight_name = weight_name
def add_data(self, data, weight = 1):
def add_data(self, data, weight=1):
try:
self[data] += weight
except KeyError:
@@ -68,21 +73,21 @@ class WeightedDataset(dict):
@number_factory
def sum(self):
""" Not really a sum but the sum of the product of key and values """
return sum([k*v for (k,v) in self.items()])
return sum([k * v for (k, v) in self.items()])
@number_factory
def mean(self):
return self.sum()/self.effectif_total()
return self.sum() / self.effectif_total()
@number_factory
def deviation(self):
""" Compute the deviation (not normalized) """
mean = self.mean()
return sum([v*(k - mean)**2 for (k,v) in self.items()])
return sum([v * (k - mean)**2 for (k, v) in self.items()])
@number_factory
def variance(self):
return self.deviation()/self.effectif_total()
return self.deviation() / self.effectif_total()
@number_factory
def sd(self):
@@ -103,10 +108,14 @@ class WeightedDataset(dict):
(1, 3, 4, 5, 5)
"""
return (min(self.keys()) , self.quartile(1) , self.quartile(2) , self.quartile(3), max(self.keys()))
return (min(self.keys()),
self.quartile(1),
self.quartile(2),
self.quartile(3),
max(self.keys()))
@number_factory
def quartile(self, quartile = 1):
def quartile(self, quartile=1):
"""
Calcul un quartile de la série.
@@ -134,13 +143,14 @@ class WeightedDataset(dict):
"""
# -1 to match with list indexing
position = self.posi_quartile(quartile) - 1
expanded_values = flatten_list([v*[k] for (k,v) in self.items()])
expanded_values = flatten_list([v * [k] for (k, v) in self.items()])
if position.is_integer():
return (expanded_values[int(position)] + expanded_values[int(position)+1])/2
return (expanded_values[int(position)] +
expanded_values[int(position) + 1]) / 2
else:
return expanded_values[ceil(position)]
def posi_quartile(self, quartile = 1):
def posi_quartile(self, quartile=1):
"""
Calcul la position du quartile
@@ -150,21 +160,22 @@ class WeightedDataset(dict):
"""
return quartile * self.effectif_total() / 4
# --------------------------
# Rendu latex
def tabular_latex(self):
""" Latex code to display dataset as a tabular """
latex = "\\begin{{tabular}}{{|c|*{{{nbr_col}}}{{c|}}}} \n".format(nbr_col = len(self.keys()))
latex = "\\begin{{tabular}}{{|c|*{{{nbr_col}}}{{c|}}}} \n".format(
nbr_col=len(self.keys()))
latex += "\t \hline \n"
data_line = "\t {data_name} ".format(data_name = self.data_name)
weight_line = "\t {weight_name} ".format(weight_name = self.weight_name)
data_line = "\t {data_name} ".format(data_name=self.data_name)
weight_line = "\t {weight_name} ".format(weight_name=self.weight_name)
# TODO: Il faudra trouver une solution pour le formatage des données |sam. janv. 9 13:14:26 EAT 2016
for (v,e) in self.items():
data_line += "& {val} ".format(val = v)
weight_line += "& {eff} ".format(eff = e)
# TODO: Il faudra trouver une solution pour le formatage des données
# |sam. janv. 9 13:14:26 EAT 2016
for (v, e) in self.items():
data_line += "& {val} ".format(val=v)
weight_line += "& {eff} ".format(eff=e)
latex += data_line + "\\\\ \n \t \\hline \n"
latex += weight_line + "\\\\ \n \t \\hline \n"
@@ -177,4 +188,3 @@ class WeightedDataset(dict):
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del