Feat(random): random tree generation flow is ok. Need to create generate
function
This commit is contained in:
parent
a77c2f9795
commit
c7561fa6fe
@ -7,7 +7,7 @@
|
|||||||
# Distributed under terms of the MIT license.
|
# Distributed under terms of the MIT license.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Tools to extract random nodes, random variables, generate random values and
|
Tools to extract random leafs, random variables, generate random values and
|
||||||
fill new trees
|
fill new trees
|
||||||
|
|
||||||
Flow
|
Flow
|
||||||
@ -15,9 +15,9 @@ Flow
|
|||||||
|
|
||||||
Tree with RdLeaf
|
Tree with RdLeaf
|
||||||
|
|
|
|
||||||
| Extract nodes
|
| Extract rdLeaf
|
||||||
|
|
|
|
||||||
List of nodes to generate
|
List of leafs to generate
|
||||||
|
|
|
|
||||||
| extract_rv
|
| extract_rv
|
||||||
|
|
|
|
||||||
@ -27,41 +27,100 @@ List random variables to generate
|
|||||||
|
|
|
|
||||||
Dictionnary of generated random variables
|
Dictionnary of generated random variables
|
||||||
|
|
|
|
||||||
| Compute nodes
|
| Compute leafs
|
||||||
|
|
|
|
||||||
Dictionnary of computed nodes
|
Dictionnary of computed leafs
|
||||||
|
|
|
|
||||||
| Replace
|
| Replace
|
||||||
|
|
|
|
||||||
Tree with RdLeaf replaced by generated values
|
Tree with RdLeaf replaced by generated values
|
||||||
|
|
||||||
|
:example:
|
||||||
|
|
||||||
|
>>> from ..tree import Tree
|
||||||
|
>>> rd_t = Tree("+", RdLeaf("a"), RdLeaf("a*k"))
|
||||||
|
>>> print(rd_t)
|
||||||
|
+
|
||||||
|
> {a}
|
||||||
|
> {a*k}
|
||||||
|
>>> extract_rdleaf(rd_t)
|
||||||
|
['a', 'a*k']
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from .leaf import RdLeaf
|
||||||
|
|
||||||
def extract_rv(node_list):
|
def extract_rdleaf(tree):
|
||||||
""" Extract the set of random values from the node list
|
""" Extract rdLeaf in a Tree
|
||||||
|
|
||||||
:param node_list: list of nodes
|
:example:
|
||||||
|
>>> from ..tree import Tree
|
||||||
|
>>> rd_t = Tree("+", RdLeaf("a"), RdLeaf("a*k"))
|
||||||
|
>>> extract_rdleaf(rd_t)
|
||||||
|
['a', 'a*k']
|
||||||
|
>>> rd_t = Tree("+", RdLeaf("a"), 2)
|
||||||
|
>>> extract_rdleaf(rd_t)
|
||||||
|
['a']
|
||||||
|
"""
|
||||||
|
rd_leafs = []
|
||||||
|
for leaf in tree.get_leafs():
|
||||||
|
try:
|
||||||
|
leaf.rdleaf
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
rd_leafs.append(leaf.name)
|
||||||
|
return rd_leafs
|
||||||
|
|
||||||
|
def extract_rv(leafs):
|
||||||
|
""" Extract the set of random values from the leaf list
|
||||||
|
|
||||||
|
:param leafs: list of leafs
|
||||||
:return: set of random values
|
:return: set of random values
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
>>> node_list = ["{a}", "{a*k}"]
|
>>> leafs = ["a", "a*k"]
|
||||||
>>> extract_rv(node_list)
|
>>> list(extract_rv(leafs))
|
||||||
{"a", "k"}
|
['a', 'k']
|
||||||
"""
|
"""
|
||||||
pass
|
rd_values = set()
|
||||||
|
for leaf in leafs:
|
||||||
|
for c in leaf:
|
||||||
|
if c.isalpha():
|
||||||
|
rd_values.add(c)
|
||||||
|
return rd_values
|
||||||
|
|
||||||
def compute_nodes(node_list, generated_values):
|
def compute_leafs(leafs, generated_values):
|
||||||
""" Compute nodes from generated random values
|
""" Compute leafs from generated random values
|
||||||
|
|
||||||
:param generated_values: Dictionnary of name:generated value
|
:param generated_values: Dictionnary of name:generated value
|
||||||
:param node_list: list of nodes
|
:param leafs: list of leafs
|
||||||
:return: Dictionnary of evaluated nodes from generated values
|
:return: Dictionnary of evaluated leafs from generated values
|
||||||
|
|
||||||
:example:
|
:example:
|
||||||
>>> node_list = ["{a}", "{a*k}"]
|
>>> leafs = ["a", "a*k"]
|
||||||
>>> generated_values = {"a":2, "k":3}
|
>>> generated_values = {"a":2, "k":3}
|
||||||
>>> compute_nodes(node_list, generated_values)
|
>>> compute_leafs(leafs, generated_values)
|
||||||
{"{a}": 2, "{a*k}": 6}
|
{'a': 2, 'a*k': 6}
|
||||||
"""
|
"""
|
||||||
pass
|
return {leaf: eval(leaf, generated_values) for leaf in leafs}
|
||||||
|
|
||||||
|
def replace_rdleaf(tree, computed_leafs):
|
||||||
|
""" Replace RdLeaf by the corresponding computed value
|
||||||
|
|
||||||
|
>>> from ..tree import Tree
|
||||||
|
>>> rd_t = Tree("+", RdLeaf("a"), RdLeaf("a*k"))
|
||||||
|
>>> computed_leafs = {'a': 2, 'a*k': 6}
|
||||||
|
>>> print(replace_rdleaf(rd_t, computed_leafs))
|
||||||
|
+
|
||||||
|
> 2
|
||||||
|
> 6
|
||||||
|
"""
|
||||||
|
def replace(leaf):
|
||||||
|
try:
|
||||||
|
return leaf.replace(computed_leafs)
|
||||||
|
except AttributeError:
|
||||||
|
return leaf
|
||||||
|
return tree.map_on_leaf(replace)
|
||||||
|
|
||||||
|
32
mapytex/calculus/core/random/leaf.py
Normal file
32
mapytex/calculus/core/random/leaf.py
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#! /usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# vim:fenc=utf-8
|
||||||
|
#
|
||||||
|
# Copyright © 2017 lafrite <lafrite@Poivre>
|
||||||
|
#
|
||||||
|
# Distributed under terms of the MIT license.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
class RdLeaf():
|
||||||
|
""" Random leaf
|
||||||
|
|
||||||
|
"""
|
||||||
|
def __init__(self, name):
|
||||||
|
self._name = name
|
||||||
|
self.rdleaf = True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def name(self):
|
||||||
|
return self._name
|
||||||
|
|
||||||
|
def replace(self, computed):
|
||||||
|
return computed[self.name]
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return "{" + self.name + "}"
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<{self.__class__.__name__} {self.__str__}>"
|
Loading…
Reference in New Issue
Block a user