Newer
Older
HepData / KstarMuMu / boot_MoM_LS.py
@mchrzasz mchrzasz on 1 Nov 2016 6 KB added K* mumu
#!/usr/bin/python

import sys
from enum import Enum


class Table:
    def __init__(self, header, columns, matrix):
        self.header = header
        self.columns = columns
        self.matrix = matrix
        self.matrix_fix = []
        self.table = ''

    def get(self, row, column):
        if row > column:
            return self.get(column, row)
        return self.matrix[row][column - row]

    def get_row(self, row):
        return self.matrix[row]

    def __str__(self):
        return self.__unicode__()

    def fixmatrix(self):
        #print('Matrix fix', self.matrix)
        self.matrix_fix = self.matrix
        for i in range(0, len(self.matrix)):
            print self.matrix[i]
            for j in range(0, len(self.matrix[i])):
                if (self.matrix[i][j] == ' ' or self.matrix[i][j] == '   ' or self.matrix[i][j] ==  '  ' or self.matrix[i][j] == '    '  or self.matrix[i][j] == r'$      $' or self.matrix[i][j] == '        ' or self.matrix[i][j] == r' $      $ '):
                    self.matrix_fix[i][j] = get_ride_of_latex(self.matrix[j - 1][i + 1])
                    #print(i,j, self.matrix[j-1][i+1])
                else:
                    #print(i,j, get_ride_of_latex(self.matrix[i][j]))
                    self.matrix_fix[i][j] = get_ride_of_latex(self.matrix[i][j])
                    #self.matrix_fix[j][i] = get_ride_of_latex(self.matrix[j - 1][i + 1]) 
                        
    def get_string(self):
        s = '*data:  x : '
        for i in range(1, len(self.matrix_fix[0]) - 1):
            s += "y :"
        s += " y "
        self.table += s + '\n'
        
        for i in range(0, len(self.matrix_fix)):
            s = ''
            s += self.columns[i + 1] + " : "
            for j in range(1, len(self.matrix_fix[i])):
                #print('a',self.matrix_fix[j-1][i+1])
                s += get_ride_of_latex(self.matrix_fix[j - 1][i + 1]) + ' : '
            self.table += s + '\n'
            # self.table+= \
        
        #print("TEST: ", self.matrix_fix)
    def __unicode__(self):
        dupa=":".join(self.columns)
        dupa=dupa.replace(': ', '', 1)
        dupa=dupa.replace(r'$P_8^\prime$ \\ \hl', '$P_8^\prime$')
        #print(dupa)
        
        
        return """*dataset:
*location: Appendix C
*dscomment: %s
*yheader: %s

*data CHANGEME
%s
*dataend:
""" % (self.header, dupa, self.table)


class TableBuilder:
    def __init__(self):
        self.header = None
        self.columns = None
        self.matrix = []
        self.matrix_fix = []

    def add_row(self, row):
        self.matrix.append(row)

    def build(self):
        return Table(self.header, self.columns, self.matrix)

    def parse_table_header(self, line):
        line[:-2].split('&')
        line=line.replace('\subsection*{', '')[:-1]
        line=line.replace(r'\gevgevcccc', r'~\rm GeV/c^4') 
        line=line.replace(r'\boldmath{', r'')
        line=line.replace(r'4$}', r'4$')
        return line.replace('\subsection*{', '')[:-1]

    def parse_columns_names(self, line):
        #print('wtf ', line)
        line = line.replace(': ', '', 4)
        a = (line[:-4].split('&'))
        #a = a.replace(': ', '', 4) 
        #print('a',a)
        return a  # line[:-2].split('&')

    def parse_row(self, line):
        return line[:-5].split('&')

    def fixmatrix(self):
        self.matrix_fix = self.matrix
        for i in range(0, len(self.matrix)):
            for j in range(0, len(self.matrix[i])):
                if self.matrix[i][j] == ' ':
                    self.matrix_fix[i][j] = get_ride_of_latex(self.matrix[j - 1][i + 1])
                    # print(i,j, table.matrix[j-1][i+1])
                else:
                    # print(i,j, get_ride_of_latex(table.matrix[i][j]))
                    self.matrix_fix[i][j] = get_ride_of_latex(self.matrix[i][j])


class ParserStateEnum(Enum):
    BEFORE_TABLE = 1
    READING_COLUMNS_NAMES = 2
    READING_TABLE = 3
    DONE = 4


class TableParser:
    def __init__(self):
        self.state = ParserStateEnum.BEFORE_TABLE
        self.builder = TableBuilder()
        self.table = None

    def feed_line(self, line):
        if self.state is ParserStateEnum.BEFORE_TABLE:
            self.feed_line_before_table(line)
        elif self.state is ParserStateEnum.READING_COLUMNS_NAMES:
            self.feed_line_columns_names(line)
        else:
            self.feed_line_reading_table(line)

    def feed_line_before_table(self, line):
        if self.is_table_header(line):
            self.builder.header = self.builder.parse_table_header(line)
        elif self.is_table_begin(line):
            self.state = ParserStateEnum.READING_COLUMNS_NAMES

    def feed_line_columns_names(self, line):
        if line:
            self.builder.columns = self.builder.parse_columns_names(line)
            self.state = ParserStateEnum.READING_TABLE

    def feed_line_reading_table(self, line):
        if self.is_table_end(line):
            self.build()
        elif not self.is_hline(line):
            self.builder.add_row(self.builder.parse_row(line))

    def is_hline(self, line):
        return line.startswith(r'\hline')

    def is_table_header(self, line):
        return line.startswith(r'\subsection')
        
    def is_table_begin(self, line):
        return line.startswith(r'\begin{tabular}')

    def is_table_end(self, line):
        return line.startswith(r'\end{tabular}')

    def build(self):
        self.state = ParserStateEnum.DONE
        self.table = self.builder.build()


def get_ride_of_latex(line):
    line2 = line
    line2 = line2.replace(r"\\rm", r"\rm")
    line2 = line2.replace(r"\\", "")
    line2 = line2.replace(r'$', '')
    line2 = line2.replace(r'\ ', '')
    line2 = line2.replace(r'\ ', '')
    line2 = line2.replace(r'$P_8^\prime$ \\ \hl', r'$P_8^\prime$')
    #line2 = line2.replace(r'\gevgevcccc', 'GeV/c^4')
    return line2


def parse_file(fd):
    tables = []
    tables_fix = []
    parser = TableParser()

    for line in fd.readlines():
        parser.feed_line(line)
        if parser.state is ParserStateEnum.DONE:
            tables.append(parser.table)
            parser = TableParser()

    for table in tables:
        table.fixmatrix()
        table.get_string()

        print(table)
        #print(table.matrix_fix)

        '''
        for i in range(0,len(table.matrix)):
            for j in range(0,len(table.matrix[i])):
                if table.matrix[i][j] == ' ':
                    print(i,j, get_ride_of_latex(table.matrix[j-1][i+1]))
                else:
                    print(i,j, get_ride_of_latex(table.matrix[i][j]))
                    #get_ride_of_latex(table.get(i,j)))
        '''
        # print(tables[0])
        # print(tables[0].get(0,1,9))


fd = open('appendix_likelihood_correlations.tex', 'r')
parse_file(fd)