Esempio n. 1
0
def test_deprecated_print_cyclic():
    p = Permutation(0, 1, 2)
    try:
        Permutation.print_cyclic = True
        with warns_deprecated_sympy():
            assert sstr(p) == '(0 1 2)'
        with warns_deprecated_sympy():
            assert srepr(p) == 'Permutation(0, 1, 2)'
        with warns_deprecated_sympy():
            assert pretty(p) == '(0 1 2)'
        with warns_deprecated_sympy():
            assert latex(p) == r'\left( 0\; 1\; 2\right)'

        Permutation.print_cyclic = False
        with warns_deprecated_sympy():
            assert sstr(p) == 'Permutation([1, 2, 0])'
        with warns_deprecated_sympy():
            assert srepr(p) == 'Permutation([1, 2, 0])'
        with warns_deprecated_sympy():
            assert pretty(p, use_unicode=False) == '/0 1 2\\\n\\1 2 0/'
        with warns_deprecated_sympy():
            assert latex(p) == \
                r'\begin{pmatrix} 0 & 1 & 2 \\ 1 & 2 & 0 \end{pmatrix}'
    finally:
        Permutation.print_cyclic = None
Esempio n. 2
0
    def pretty_config(self, tex):
        start = datetime.now()
        import sympy
        from sympy.parsing.sympy_parser import parse_expr, standard_transformations, implicit_multiplication_application
        from sympy import init_printing
        from sympy.printing import pretty
        from sympy import Eq, Symbol
        # definition of the transformation used in parse_expr
        transformations = transformations = (
            standard_transformations + (implicit_multiplication_application, ))
        # ^ is considered the bitwise-XOR in programming, as such, ** is adopted in python.
        tex = str(tex).replace("^", "**")
        # Sympy does not handle equations well ,so we need to split them
        if ("=" in tex):
            rhs, lhs = tex.split('=')
            try:
                rhs = parse_expr(rhs, transformations=transformations)
                lhs = parse_expr(lhs, transformations=transformations)
            except SyntaxError as e:
                print(e)
                print(
                    f"[StepPyStep][parse_tex][pretty_config] runtime: {datetime.now() - start}"
                )
                return tex
            # Actual pretty print of sympy
            # using more common unicode that looks bette
            rhs = pretty(rhs, use_unicode=True)

            rhs = rhs.replace("╲╱", "√")
            rhs = rhs.replace("sqrt", "√")

            lhs = pretty(lhs, use_unicode=True)

            lhs = lhs.replace("╲╱", "√")
            lhs = lhs.replace("sqrt", "√")

            tex = rhs + "=" + lhs + "\n"

            print(
                f"[StepPyStep][parse_tex][pretty_config] runtime: {datetime.now() - start}"
            )

            return tex
        # Without equations the procedure is very simple
        else:

            sympy_expr = parse_expr(tex, transformations=transformations)

            print(
                f"[StepPyStep][parse_tex][pretty_config] runtime: {datetime.now() - start}"
            )
            pretty_expr = pretty(sympy_expr)
            pretty_expr = pretty_expr.replace("╲╱", "√").replace("sqrt", "√")
            return pretty_expr
Esempio n. 3
0
def _report_about_generated_boolean_mtl_problem(functions, tasks):
    """Log a report about the generated synthetic Boolean MTL problem
    represented by the given functions and tasks.
    Note: The logger object must be a valid Logger.
    
    Parameters
    ----------
    functions : list
        A list of Boolean functions comprised of Boolean operators from
        sympy.logic, one function for each task group.
    tasks : list
        Either a list of Bunch objects corresponding to Boolean function
        learning tasks,
        or a list of lists of Bunch objects, where each list corresponds
        to a set of different learning sets for each task.
    
    """
    # extract group names from tasks' ids
    group_names = []
    for tl in tasks:
        if isinstance(tl, list):
            t = tl[0]
        else:
            t = tl
        match = re.search(r"(Group \d+)", t.ID)
        group_name = match.group(1)
        if group_name not in group_names:
            group_names.append(group_name)
    if len(group_names) != len(functions):
        raise ValueError("The number of task groups doesn't correspond to the "
                         "number of Boolean functions.")
    
    logger.debug("Report about the generated synthetic Boolean MTL problem:")
    logger.debug("  Boolean function of each group:")
    for group_name, func in zip(group_names, functions):
        # NOTE: sympy's pretty() function returns a unicode string, so the
        # string literal must also be a unicode string
        logger.debug(u"   - {}: {}".format(group_name, pretty(func,
                                                              wrap_line=False)))
    logger.debug("  % of True values in y for each task:")
    sum_true = 0
    sum_total = 0
    for tl in tasks:
        if isinstance(tl, list):
            for i, t in enumerate(tl):
                cur_true = sum(t.target == True)
                cur_len = len(t.target)
                sum_true += cur_true
                sum_total += cur_len
                logger.debug("   - {} (learning set #{}): {}".\
                             format(t.ID, i, cur_true / cur_len))
        else:
            t = tl
            cur_true = sum(t.target == True)
            cur_len = len(t.target)
            sum_true += cur_true
            sum_total += cur_len
            logger.debug("   - {}: {}".format(t.ID, cur_true / cur_len))
    logger.debug("  Average % of True values in y (across all tasks): {}".\
                 format(sum_true / sum_total))
Esempio n. 4
0
def _report_about_generated_boolean_mtl_problem(functions, tasks):
    """Log a report about the generated synthetic Boolean MTL problem
    represented by the given functions and tasks.
    Note: The logger object must be a valid Logger.
    
    Parameters
    ----------
    functions : list
        A list of Boolean functions comprised of Boolean operators from
        sympy.logic, one function for each task group.
    tasks : list
        Either a list of Bunch objects corresponding to Boolean function
        learning tasks,
        or a list of lists of Bunch objects, where each list corresponds
        to a set of different learning sets for each task.
    
    """
    # extract group names from tasks' ids
    group_names = []
    for tl in tasks:
        if isinstance(tl, list):
            t = tl[0]
        else:
            t = tl
        match = re.search(r"(Group \d+)", t.ID)
        group_name = match.group(1)
        if group_name not in group_names:
            group_names.append(group_name)
    if len(group_names) != len(functions):
        raise ValueError("The number of task groups doesn't correspond to the "
                         "number of Boolean functions.")

    logger.debug("Report about the generated synthetic Boolean MTL problem:")
    logger.debug("  Boolean function of each group:")
    for group_name, func in zip(group_names, functions):
        # NOTE: sympy's pretty() function returns a unicode string, so the
        # string literal must also be a unicode string
        logger.debug(u"   - {}: {}".format(group_name,
                                           pretty(func, wrap_line=False)))
    logger.debug("  % of True values in y for each task:")
    sum_true = 0
    sum_total = 0
    for tl in tasks:
        if isinstance(tl, list):
            for i, t in enumerate(tl):
                cur_true = sum(t.target == True)
                cur_len = len(t.target)
                sum_true += cur_true
                sum_total += cur_len
                logger.debug("   - {} (learning set #{}): {}".\
                             format(t.ID, i, cur_true / cur_len))
        else:
            t = tl
            cur_true = sum(t.target == True)
            cur_len = len(t.target)
            sum_true += cur_true
            sum_total += cur_len
            logger.debug("   - {}: {}".format(t.ID, cur_true / cur_len))
    logger.debug("  Average % of True values in y (across all tasks): {}".\
                 format(sum_true / sum_total))
def test_pretty():
    x = symbols('x')
    assert pretty(Q.positive(x)) == "Q.positive(x)"
Esempio n. 6
0
def test_pretty():
    assert pretty(Q.positive(x)) == "Q.positive(x)"
Esempio n. 7
0
def _generate_boolean_data(a, d, n, g, tg, noise, random_seed,
                           n_learning_sets=1):
    """Generate a synthetic MTL problem of learning Boolean functions according
    to the given parameters.
    
    Parameters
    ----------
    a : int
        Number of attributes/variables of the generated Boolean functions.
    d : int
        The expected number of attributes/variables in a disjunct.
    n : int
        The number of examples for each task to generate.
    g : int
        The number of task groups to generate. Each task group shares the
        same Boolean functions.
    tg : int
        The number of tasks (with their corresponding data) to generate for
        each task group.
    noise : float
        The proportion of examples of each task that have their class values
        determined randomly.
    random_seed : int
        The random seed with which to initialize a private Random object.
    n_learning_sets : int
        The number of different learning sets to create for each task.
    
    Returns
    -------
    tasks : list
        If n_learning_sets == 1, a list of Bunch objects corresponding to
        Boolean function learning tasks.
        Otherwise, a list of lists of Bunch objects, where each list corresponds
        to a set of different learning sets for each task.
    funcs : list
        A list of Boolean functions comprised of Boolean operators from
        sympy.logic, one function for each task group.
    attr : list of sympy's Symbol objects representing the attributes of the
        generated functions
    
    """
    rnd = random.Random(random_seed)
    # generate Boolean functions
    attrs = []
    funcs = []
    for i in range(g):
        attr, func = generate_boolean_function(a, d,
                                               random_seed=rnd.randint(1, 100))
        attrs.append(attr)
        funcs.append(func)
    # generate examples for all tasks 
    tasks = [[] for i in range(g * tg)]
    for i in range(g):
        attr, func = attrs[i], funcs[i]
        attr_names = [str(a_) for a_ in attr]
        for j in range(tg):
            # NOTE: sympy's pretty() function returns a unicode string, so
            # the string literal must also be a unicode string
            descr = (u"Synthetic boolean data for task {} of group {} "
                     "(function: {})".format(j + 1, i + 1, pretty(func,
                                                            wrap_line=False)))
            id = "Group {}, task {}".format(i + 1, j + 1)
            for k in range(n_learning_sets):
                X, y = generate_examples(attr, func, n, noise=noise,
                                         random_state=rnd.randint(1, 100))
                tasks[i * tg + j].append(Bunch(data=X, target=y,
                                               feature_names=attr_names,
                                               DESCR=descr, ID=id))
    if n_learning_sets == 1:
        tasks = [t[0] for t in tasks]
    return tasks, funcs, attr
Esempio n. 8
0
        update_progress(1.* (i + 1) / n_funcs)
    print
    
    _report_about_generated_boolean_mtl_problem(funcs, tasks)
    return tasks, tasks_complete_test_sets


if __name__ == "__main__":
    # generate a Boolean function with 8 variables and disjuncts with an average
    # length of 4
    a, d = 8, 4
    attr, func = generate_boolean_function(a, d, random_seed=2)
    # NOTE: sympy's pretty() function returns a unicode string, so the string
    # literal must also be a unicode string
    print u"Boolean function (a={}, d={}): {}".format(a, d,
                                                pretty(func, wrap_line=False))
    X, y = generate_examples(attr, func, n=1000, random_state=10)
    print "% of True values in y: {:.2f}".format(100 * sum(y == True) / len(y))
    X_noise, y_noise = generate_examples(attr, func, n=1000, noise=0.3,
                                         random_state=10)
    
    # try different learning algorithms in scikit-learn and report their
    # cross-validation scores
    from sklearn.linear_model import LogisticRegression
    from sklearn import cross_validation
    lr = LogisticRegression()
    print "Log. reg. scores: ", cross_validation.cross_val_score(lr, X, y, cv=5)
    from sklearn.tree import DecisionTreeClassifier
    dt = DecisionTreeClassifier()
    print "Dec. tree scores: ", cross_validation.cross_val_score(dt, X, y, cv=5)
    from sklearn.naive_bayes import MultinomialNB
def test_pretty():
    x = symbols('x')
    assert pretty(Q.positive(x)) == "Q.positive(x)"
Esempio n. 10
0
X = S1T
Y = S1
reps = [(X[i] * Y[j], int(i == j)) for i in range(3) for j in range(3)]
X = P0T
Y = P0
reps += [(X[i] * Y[j], int(i == j)) for i in range(2) for j in range(2)]


def eval(expr):
    return expr.expand().subs(reps)


for v in S1T:
    for u in S1:
        print('<%s, %s> = %s' % (pretty(v), pretty(u), eval(v * u)))
print()
for v in P0T:
    for u in P0:
        print('<%s, %s> = %s' % (pretty(v), pretty(u), eval(v * u)))
print()


def transpose(expr):
    expr = expr.expand()
    expr = expr.subs(zip(S1, symbols('x:3')))
    expr = expr.subs(zip(S1T, S1))
    expr = expr.subs(zip(symbols('x:3'), S1T))

    expr = expr.subs(zip(P0, symbols('x:2')))
    expr = expr.subs(zip(P0T, P0))
Esempio n. 11
0
def test_pretty():
    assert pretty(Q.positive(x)) == "Q.positive(x)"
    assert pretty(
        {Q.positive, Q.integer}) == "{Q.integer, Q.positive}"
Esempio n. 12
0
def _generate_boolean_data(a,
                           d,
                           n,
                           g,
                           tg,
                           noise,
                           random_seed,
                           n_learning_sets=1):
    """Generate a synthetic MTL problem of learning Boolean functions according
    to the given parameters.
    
    Parameters
    ----------
    a : int
        Number of attributes/variables of the generated Boolean functions.
    d : int
        The expected number of attributes/variables in a disjunct.
    n : int
        The number of examples for each task to generate.
    g : int
        The number of task groups to generate. Each task group shares the
        same Boolean functions.
    tg : int
        The number of tasks (with their corresponding data) to generate for
        each task group.
    noise : float
        The proportion of examples of each task that have their class values
        determined randomly.
    random_seed : int
        The random seed with which to initialize a private Random object.
    n_learning_sets : int
        The number of different learning sets to create for each task.
    
    Returns
    -------
    tasks : list
        If n_learning_sets == 1, a list of Bunch objects corresponding to
        Boolean function learning tasks.
        Otherwise, a list of lists of Bunch objects, where each list corresponds
        to a set of different learning sets for each task.
    funcs : list
        A list of Boolean functions comprised of Boolean operators from
        sympy.logic, one function for each task group.
    attr : list of sympy's Symbol objects representing the attributes of the
        generated functions
    
    """
    rnd = random.Random(random_seed)
    # generate Boolean functions
    attrs = []
    funcs = []
    for i in range(g):
        attr, func = generate_boolean_function(a,
                                               d,
                                               random_seed=rnd.randint(1, 100))
        attrs.append(attr)
        funcs.append(func)
    # generate examples for all tasks
    tasks = [[] for i in range(g * tg)]
    for i in range(g):
        attr, func = attrs[i], funcs[i]
        attr_names = [str(a_) for a_ in attr]
        for j in range(tg):
            # NOTE: sympy's pretty() function returns a unicode string, so
            # the string literal must also be a unicode string
            descr = (u"Synthetic boolean data for task {} of group {} "
                     "(function: {})".format(j + 1, i + 1,
                                             pretty(func, wrap_line=False)))
            id = "Group {}, task {}".format(i + 1, j + 1)
            for k in range(n_learning_sets):
                X, y = generate_examples(attr,
                                         func,
                                         n,
                                         noise=noise,
                                         random_state=rnd.randint(1, 100))
                tasks[i * tg + j].append(
                    Bunch(data=X,
                          target=y,
                          feature_names=attr_names,
                          DESCR=descr,
                          ID=id))
    if n_learning_sets == 1:
        tasks = [t[0] for t in tasks]
    return tasks, funcs, attr
Esempio n. 13
0
        update_progress(1. * (i + 1) / n_funcs)
    print

    _report_about_generated_boolean_mtl_problem(funcs, tasks)
    return tasks, tasks_complete_test_sets


if __name__ == "__main__":
    # generate a Boolean function with 8 variables and disjuncts with an average
    # length of 4
    a, d = 8, 4
    attr, func = generate_boolean_function(a, d, random_seed=2)
    # NOTE: sympy's pretty() function returns a unicode string, so the string
    # literal must also be a unicode string
    print u"Boolean function (a={}, d={}): {}".format(
        a, d, pretty(func, wrap_line=False))
    X, y = generate_examples(attr, func, n=1000, random_state=10)
    print "% of True values in y: {:.2f}".format(100 * sum(y == True) / len(y))
    X_noise, y_noise = generate_examples(attr,
                                         func,
                                         n=1000,
                                         noise=0.3,
                                         random_state=10)

    # try different learning algorithms in scikit-learn and report their
    # cross-validation scores
    from sklearn.linear_model import LogisticRegression
    from sklearn import cross_validation
    lr = LogisticRegression()
    print "Log. reg. scores: ", cross_validation.cross_val_score(lr,
                                                                 X,
Esempio n. 14
0
def test_pretty():
    assert pretty(Q.positive(x)) == "Q.positive(x)"
    assert pretty(set([Q.positive, Q.integer])) == "set([Q.integer, Q.positive])"
Esempio n. 15
0
def test_pretty():
    x = symbols('x')
    assert pretty(Assume(x, 'positive')) == "Assume(x, 'positive')"
Esempio n. 16
0
def test_pretty():
    x = symbols('x')
    assert pretty(Assume(x, 'positive')) == "Assume(x, 'positive')"
Esempio n. 17
0
def test_pretty():
    assert pretty(Q.positive(x)) == "Q.positive(x)"
Esempio n. 18
0
def test_pretty():
    assert pretty(Q.positive(x)) == "Q.positive(x)"
    assert pretty(set([Q.positive,
                       Q.integer])) == "set([Q.integer, Q.positive])"
Esempio n. 19
0
def test_pretty():
    x = symbols('x')
    assert pretty(Assume(x, 'positive', True)) == 'Assume(x, positive, True)'