Fix: Black does its job
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#/usr/bin/env python
|
||||
# /usr/bin/env python
|
||||
# -*- coding:Utf-8 -*-
|
||||
|
||||
#
|
||||
@@ -32,11 +32,17 @@ 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
|
||||
@@ -47,11 +53,9 @@ 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)
|
||||
|
||||
@@ -94,7 +98,7 @@ class Dataset(list):
|
||||
def deviation(self):
|
||||
""" Compute the deviation (not normalized) """
|
||||
mean = self.mean()
|
||||
return sum([(x - mean)**2 for x in self])
|
||||
return sum([(x - mean) ** 2 for x in self])
|
||||
|
||||
@number_factory
|
||||
def variance(self):
|
||||
@@ -120,7 +124,8 @@ class Dataset(list):
|
||||
self.quartile(1),
|
||||
self.quartile(2),
|
||||
self.quartile(3),
|
||||
max(self))
|
||||
max(self),
|
||||
)
|
||||
|
||||
@number_factory
|
||||
def quartile(self, quartile=1):
|
||||
@@ -173,18 +178,21 @@ class Dataset(list):
|
||||
""" 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)
|
||||
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"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#/usr/bin/env python
|
||||
# /usr/bin/env python
|
||||
# -*- coding:Utf-8 -*-
|
||||
|
||||
from functools import wraps
|
||||
@@ -6,6 +6,7 @@ from functools import wraps
|
||||
|
||||
def number_factory(fun):
|
||||
""" Decorator which format returned value """
|
||||
|
||||
@wraps(fun)
|
||||
def wrapper(*args, **kwargs):
|
||||
ans = fun(*args, **kwargs)
|
||||
@@ -16,6 +17,7 @@ def number_factory(fun):
|
||||
return round(ans, 2)
|
||||
except AttributeError:
|
||||
return ans
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
#/usr/bin/env python
|
||||
# /usr/bin/env python
|
||||
# -*- coding:Utf-8 -*-
|
||||
|
||||
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
|
||||
@@ -47,7 +51,8 @@ def random_generator(length,
|
||||
"gauss": gauss,
|
||||
"uniform": uniform,
|
||||
"randint": randint,
|
||||
"choice": choice}
|
||||
"choice": choice,
|
||||
}
|
||||
try:
|
||||
distrib(*rd_args)
|
||||
except TypeError:
|
||||
@@ -67,11 +72,13 @@ def random_generator(length,
|
||||
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")
|
||||
"Can't build the last value. Conflict between v_min/v_max and exact_mean"
|
||||
)
|
||||
data.append(last_v)
|
||||
|
||||
return data
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#/usr/bin/env python
|
||||
# /usr/bin/env python
|
||||
# -*- coding:Utf-8 -*-
|
||||
|
||||
"""
|
||||
@@ -12,9 +12,11 @@ from .dataset import Dataset
|
||||
from itertools import chain
|
||||
from .number_tools import number_factory
|
||||
|
||||
|
||||
def flatten_list(l):
|
||||
return list(chain(*l))
|
||||
|
||||
|
||||
class WeightedDataset(dict):
|
||||
""" A weighted dataset with statistics and latex rendering methods
|
||||
|
||||
@@ -37,11 +39,8 @@ class WeightedDataset(dict):
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
datas=[],
|
||||
data_name="Valeurs",
|
||||
weights=[],
|
||||
weight_name="Effectifs"):
|
||||
self, datas=[], data_name="Valeurs", weights=[], weight_name="Effectifs"
|
||||
):
|
||||
"""
|
||||
Initiate the WeightedDataset
|
||||
"""
|
||||
@@ -84,7 +83,7 @@ class WeightedDataset(dict):
|
||||
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):
|
||||
@@ -109,11 +108,13 @@ 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):
|
||||
@@ -146,8 +147,9 @@ class WeightedDataset(dict):
|
||||
position = self.posi_quartile(quartile) - 1
|
||||
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)]
|
||||
|
||||
@@ -167,7 +169,8 @@ class WeightedDataset(dict):
|
||||
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()))
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user