<commit_before>#!/usr/bin/python
from os import sys
import os
import fcntl

sys.path += ['/usr/texbin','', '//anaconda/lib/python27.zip', '//anaconda/lib/python2.7', '//anaconda/lib/python2.7/plat-darwin', '//anaconda/lib/python2.7/plat-mac', '//anaconda/lib/python2.7/plat-mac/lib-scriptpackages', '//anaconda/lib/python2.7/lib-tk', '//anaconda/lib/python2.7/lib-old', '//anaconda/lib/python2.7/lib-dynload', '//anaconda/lib/python2.7/site-packages', '//anaconda/lib/python2.7/site-packages/PIL', '//anaconda/lib/python2.7/site-packages/setuptools-2.1-py2.7.egg']

from jinja2 import nodes, contextfunction
from jinja2.ext import Extension
from jinja2 import Environment, FileSystemLoader
import numpy as np
import scipy as sp
from sympy import Symbol, sympify, lambdify, latex
from scipy.interpolate import interp1d
import matplotlib.pyplot as plot
import subprocess



class PPExtension(Extension):
    tags = set(['figure', 'table', 'evaluate', 'evaltex'])
    def __init(self, environment):
        super(PPExtension, self).__init__(environment)
        #add defaults
        environment.extend(error_calculation='gauss', data_mode='tuples', print_figure_for_each_table=True)

    def parse(self, parser):
        #the token
        lineno = parser.stream.next()
        linnum = lineno.lineno
        if(lineno.value == 'figure'):
            #ther argument
            arg = [parser.parse_expression()]
            #the figure data
            if (parser.stream.skip_if('comma')):
                arg.append(parser.parse_expression())
            body = parser.parse_statements(['name:endfigure'], drop_needle=True)
            return nodes.CallBlock(self.call_method('_create_figure', arg),[], [], body).set_lineno(linnum)
        elif(lineno.value == 'table'):
            arg = [parser.parse_expression()]
            return nodes.Output([self.call_method('_print_latex_table', arg)])
        elif(lineno.value == 'evaluate'):
            arg = [parser.parse_expression()]
            return nodes.Output([self.call_method('_evaluate_function', arg)])
        elif(lineno.value == 'evaltex'):
            arg = [parser.parse_expression()]
            return nodes.Output([self.call_method('_evaltex_function', arg)])


        #body = parser.parse_statements(['name:endfigure'], drop_needle=True)
        return nodes.Const(None)

    def _evaltex_function(self, data):
        s = sympify(data['function'])
        l = latex(s)
        s = s.doit()
        print(latex(s))
        vals = []
        syms = []
        for symbol in data['symbols']:
            syms.append(Symbol(symbol['sym']))
            vals.append(symbol['val'])
        print(syms, vals)
        my_function = lambdify(syms,s, 'numpy')
        result = my_function(*vals)
        print(str(result))
        print(l + " = " + str(result))
        return l + " = " + str(result)

# dictionary with entries
# data
#   |_ [xdata, ydata, desc]
# xlabel
# ylabel
    def _print_latex_table(self, data):

        for tab in data['data']:
            table = "\\begin{figure}\\centering\\begin{tabular}{|c|c|}"
            i = 0
            table += "\\hline\n"
            table += data['xlabel'] + " & " + data['ylabel'] + "\\\\\n"
            table += "\\hline\n"
            for entry in tab['xdata']:
                table += str(entry) + " & " + str(tab['ydata'][i]) + "\\\\\n"
                table += "\\hline\n"
                i+=1
            table += "\\end{tabular} \\caption{" + tab['desc'] + "} \\end{figure}\n"
        return table





# dictionary with entries
# data
#   |_ (xdata,ydata,range = [xmin,xmax,ymin,ymax], title, interpolate)<commit_msg>added extended table draw mode<commit_after>#!/usr/bin/python
from os import sys
import os
import fcntl

sys.path += ['/usr/texbin','', '//anaconda/lib/python27.zip', '//anaconda/lib/python2.7', '//anaconda/lib/python2.7/plat-darwin', '//anaconda/lib/python2.7/plat-mac', '//anaconda/lib/python2.7/plat-mac/lib-scriptpackages', '//anaconda/lib/python2.7/lib-tk', '//anaconda/lib/python2.7/lib-old', '//anaconda/lib/python2.7/lib-dynload', '//anaconda/lib/python2.7/site-packages', '//anaconda/lib/python2.7/site-packages/PIL', '//anaconda/lib/python2.7/site-packages/setuptools-2.1-py2.7.egg']

from jinja2 import nodes, contextfunction
from jinja2.ext import Extension
from jinja2 import Environment, FileSystemLoader
import numpy as np
import scipy as sp
from sympy import Symbol, sympify, lambdify, latex
from scipy.interpolate import interp1d
import matplotlib.pyplot as plot
import subprocess



class PPExtension(Extension):
    tags = set(['figure', 'table', 'evaluate', 'evaltex'])
    def __init(self, environment):
        super(PPExtension, self).__init__(environment)
        #add defaults
        environment.extend(error_calculation='gauss', data_mode='tuples', print_figure_for_each_table=True)

    def parse(self, parser):
        #the token
        lineno = parser.stream.next()
        linnum = lineno.lineno
        if(lineno.value == 'figure'):
            #ther argument
            arg = [parser.parse_expression()]
            #the figure data
            if (parser.stream.skip_if('comma')):
                arg.append(parser.parse_expression())
            body = parser.parse_statements(['name:endfigure'], drop_needle=True)
            return nodes.CallBlock(self.call_method('_create_figure', arg),[], [], body).set_lineno(linnum)
        elif(lineno.value == 'table'):
            arg = [parser.parse_expression()]
            return nodes.Output([self.call_method('_print_latex_table', arg)])
        elif(lineno.value == 'evaluate'):
            arg = [parser.parse_expression()]
            return nodes.Output([self.call_method('_evaluate_function', arg)])
        elif(lineno.value == 'evaltex'):
            arg = [parser.parse_expression()]
            return nodes.Output([self.call_method('_evaltex_function', arg)])


        #body = parser.parse_statements(['name:endfigure'], drop_needle=True)
        return nodes.Const(None)

    def _evaltex_function(self, data):
        s = sympify(data['function'])
        l = latex(s)
        s = s.doit()
        #print(latex(s))
        vals = []
        syms = []
        for symbol in data['symbols']:
            syms.append(Symbol(symbol['sym']))
            vals.append(symbol['val'])
        #print(syms, vals)
        my_function = lambdify(syms,s, 'numpy')
        result = my_function(*vals)
        #print(str(result))
        #print(l + " = " + str(result))
        return l + " = " + str(result)

# dictionary with entries
# data
#   |_ [xdata, ydata, desc]
# xlabel
# ylabel
    def _print_latex_table(self, data):
        if 'extended' in data:
            #we have in xdata an array and there is an array xheader and yheader (optional otherwise same as xheader) where xheader matches size of xdata and yheader matches size of one entry array of xdata
            #at least one entry
            ylen = len(data['xdata'][0])
            #since len(xheader) and len (xdata) should match we take xheader
            xlen = len(data['xheader'])

            #the xheader string (since latex builds tables per row)
            yheader = data['yheader']
            xheader = "&" if len(yheader) >0 else ""
            #xheader += "&".join(data['xheader'])
            isfirst = True
            for h in data['xheader']:
                if isfirst:
                    xheader += "\\textbf{" + str(h) + "}"
                    isfirst = False
                else:
                    xheader += "&\\textbf{" + str(h) + "}"
            table = '\\begin{figure}\\centering\\begin{tabular}{' + 'c' * (xlen+ (1 if len(yheader) > 0 else 0)) +'}'
            #table += "\\hline\n"
            table += xheader + "\\\\\n\\cline{2-" + str(xlen+1) + "}"
            print(ylen)
            #now iterate over all rows, remember to print in the first row the yheader if there is one
            for i in xrange(0, ylen):
                if(len(yheader) > 0):
                    table += "\\multicolumn{1}{r|}{\\textbf{" + str(data['yheader'][i]) + "}}"
                for o in xrange(0,xlen):
                    print("[",o, i,"]")
                    if o == xlen-1:
                        table += "&\multicolumn{1}{r|}{" + str(data['xdata'][o][i]) + "}"
                    else:
                        table += "&" + str(data['xdata'][o][i])
                table += "\\\\\\cline{2-"  + str(xlen+1) + "}\n"

            table += "\\end{tabular} \\caption{" + data['desc'] + "} \\end{figure}\n"
            print (table)
        else:
            for tab in data['data']:
                table = "\\begin{figure}\\centering\\begin{tabular}{|c|c|}"
                i = 0
                table += "\\hline\n"
                table += str(data['xlabel']) + " & " + str(data['ylabel']) + "\\\\\n"
                table += "\\hline\n"
                for entry in tab['xdata']:
                    table += str(entry) + " & " + str(tab['ydata'][i]) + "\\\\\n"
                    table += "\\hline\n"
                    i+=1
                table += "\\end{tabular} \\caption{" + str(tab['desc']) + "} \\end{figure}\n"
        return table





# dictionary with entries
# data
#   |_ (xdata,ydata,range = [xmin,xmax,ymin,ymax], title, interpolate)
{'fraction_non_alphanumeric': 0.16088362068965517, 'fraction_numerical': 0.008512931034482758, 'mean_word_length': 2.2679577464788734, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1634486', 'n_tokens_mistral': 3027, 'n_tokens_neox': 2923, 'n_words': 601}