Fix: Black does its job
This commit is contained in:
parent
02214b0f82
commit
d6bb61dc48
@ -1,9 +1,9 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
|
||||
from .calculus import Expression#, Polynom, Fraction, random_str, txt, Equation
|
||||
from .calculus import Expression # , Polynom, Fraction, random_str, txt, Equation
|
||||
|
||||
#Expression.set_render('tex')
|
||||
# Expression.set_render('tex')
|
||||
|
||||
from .stat import Dataset, WeightedDataset
|
||||
from .geometry import random_pythagore
|
||||
|
@ -12,7 +12,13 @@ Expression
|
||||
"""
|
||||
from functools import partial
|
||||
from ..core import AssocialTree, Tree, compute, typing, TypingError
|
||||
from ..core.random import extract_rdleaf, extract_rv, random_generator, compute_leafs, replace_rdleaf
|
||||
from ..core.random import (
|
||||
extract_rdleaf,
|
||||
extract_rv,
|
||||
random_generator,
|
||||
compute_leafs,
|
||||
replace_rdleaf,
|
||||
)
|
||||
from ..core.MO import moify
|
||||
from .tokens import factory
|
||||
from .renders import renders
|
||||
@ -75,6 +81,7 @@ class Expression(object):
|
||||
>>> Expression.set_render('txt')
|
||||
"""
|
||||
from .tokens.token import Token
|
||||
|
||||
Token.set_render(render)
|
||||
cls.RENDER = render
|
||||
|
||||
@ -485,6 +492,7 @@ def extract_variable(leaf):
|
||||
except AttributeError:
|
||||
return None
|
||||
|
||||
|
||||
def replace(leaf, origin, dest):
|
||||
""" Recursively replace origin to dest in leaf """
|
||||
try:
|
||||
@ -497,6 +505,7 @@ def replace(leaf, origin, dest):
|
||||
replace_var = partial(replace, origin=origin, dest=dest)
|
||||
return leaf.tree.map_on_leaf(replace_var)
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
|
@ -69,7 +69,7 @@ class Token(object):
|
||||
else:
|
||||
raise ValueError(f"Unknow render {self.RENDER}")
|
||||
|
||||
#return renders[self.RENDER](self._mo)
|
||||
# return renders[self.RENDER](self._mo)
|
||||
|
||||
@property
|
||||
def __txt__(self):
|
||||
@ -203,7 +203,6 @@ class Token(object):
|
||||
return self._get_soul() <= self._get_soul(other)
|
||||
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# Reglages pour 'vim'
|
||||
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||||
|
@ -26,6 +26,7 @@ def moify(token):
|
||||
except MOError:
|
||||
return token
|
||||
|
||||
|
||||
@coroutine
|
||||
def moify_cor(target):
|
||||
""" Coroutine which try to convert a parsed token into an MO
|
||||
|
@ -101,7 +101,6 @@ class MO(ABC):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
|
||||
class Atom(MO):
|
||||
|
||||
""" Base Math Object with only one component.
|
||||
|
@ -110,6 +110,7 @@ class MOstrPower(Molecule):
|
||||
'monome2'
|
||||
"""
|
||||
return f"monome{self.power}"
|
||||
|
||||
def differentiate(self):
|
||||
""" differentiate a MOstrPower and get a tree
|
||||
|
||||
@ -121,8 +122,10 @@ class MOstrPower(Molecule):
|
||||
> x^2
|
||||
"""
|
||||
if self._power > 2:
|
||||
return Tree('*', self.power, MOstrPower(self.variable, self._power._value-1))
|
||||
return Tree('*', self.power, MOstr(self.variable))
|
||||
return Tree(
|
||||
"*", self.power, MOstrPower(self.variable, self._power._value - 1)
|
||||
)
|
||||
return Tree("*", self.power, MOstr(self.variable))
|
||||
|
||||
|
||||
class MOMonomial(Molecule):
|
||||
|
@ -27,6 +27,7 @@ multiply_doc = """ Multiply MOs
|
||||
|
||||
multiply = Dispatcher("multiply", doc=multiply_doc)
|
||||
|
||||
|
||||
def multiply_filter(left, right):
|
||||
""" Automatic multiply on MO
|
||||
|
||||
@ -56,6 +57,7 @@ def multiply_filter(left, right):
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
|
||||
@multiply.register((MOnumber, MOFraction), MOstr)
|
||||
@special_case(multiply_filter)
|
||||
def moscalar_mostr(left, right):
|
||||
@ -102,7 +104,7 @@ def moscalar_mostrpower(left, right):
|
||||
>>> multiply(a, x)
|
||||
<MOstrPower x^4>
|
||||
"""
|
||||
#if left == 1:
|
||||
# if left == 1:
|
||||
# return right
|
||||
return MOMonomial(left, right)
|
||||
|
||||
|
@ -5,7 +5,7 @@
|
||||
from random import randint
|
||||
|
||||
|
||||
def random_pythagore(v_min = 1, v_max = 10, nbr_format = lambda x : x) :
|
||||
def random_pythagore(v_min=1, v_max=10, nbr_format=lambda x: x):
|
||||
""" Generate a pythagore triplet
|
||||
:returns: (a,b,c) such that a^2 = b^2 + c^2
|
||||
|
||||
@ -14,10 +14,11 @@ def random_pythagore(v_min = 1, v_max = 10, nbr_format = lambda x : x) :
|
||||
while u == v:
|
||||
u, v = randint(v_min, v_max), randint(v_min, v_max)
|
||||
u, v = max(u, v), min(u, v)
|
||||
triplet = (u**2+v**2, 2*u*v, u**2-v**2)
|
||||
triplet = (u ** 2 + v ** 2, 2 * u * v, u ** 2 - v ** 2)
|
||||
formated_triplet = [nbr_format(i) for i in triplet]
|
||||
return formated_triplet
|
||||
|
||||
|
||||
# -----------------------------
|
||||
# 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 -*-
|
||||
|
||||
#
|
||||
@ -32,11 +32,17 @@ class Dataset(list):
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def random(cls, length, data_name="Valeurs",
|
||||
distrib="gauss", rd_args=(0, 1),
|
||||
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):
|
||||
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),
|
||||
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):
|
||||
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()),
|
||||
return (
|
||||
min(self.keys()),
|
||||
self.quartile(1),
|
||||
self.quartile(2),
|
||||
self.quartile(3),
|
||||
max(self.keys()))
|
||||
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)
|
||||
|
23
setup.py
23
setup.py
@ -6,17 +6,14 @@ except ImportError:
|
||||
from distutils.core import setup
|
||||
|
||||
setup(
|
||||
name='mapytex',
|
||||
version='2.1',
|
||||
description='Computing like a student',
|
||||
author='Benjamin Bertrand',
|
||||
author_email='programming@opytex.org',
|
||||
url='http://git.opytex.org/lafrite/Mapytex',
|
||||
#packages=['mapytex'],
|
||||
name="mapytex",
|
||||
version="2.1",
|
||||
description="Computing like a student",
|
||||
author="Benjamin Bertrand",
|
||||
author_email="programming@opytex.org",
|
||||
url="http://git.opytex.org/lafrite/Mapytex",
|
||||
# packages=['mapytex'],
|
||||
packages=find_packages(),
|
||||
include_package_data = True,
|
||||
install_requires=[
|
||||
'multipledispatch',
|
||||
'tabulate',
|
||||
],
|
||||
)
|
||||
include_package_data=True,
|
||||
install_requires=["multipledispatch", "tabulate"],
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user