| 1 | |
|---|
| 2 | from parameters import ParameterRange |
|---|
| 3 | from parameters import ParameterTable |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | def parameters_to_latex(filename,d,indent=0.5): |
|---|
| 7 | lines = [] |
|---|
| 8 | tables = [] |
|---|
| 9 | |
|---|
| 10 | def remove_non_valid_characters(lines): |
|---|
| 11 | non_valid_characters = [('_',' ')] |
|---|
| 12 | if isinstance(lines,list): |
|---|
| 13 | new_lines = [] |
|---|
| 14 | for line in lines: |
|---|
| 15 | for non_char in non_valid_characters: |
|---|
| 16 | line = line.replace(non_char[0],non_char[1]) |
|---|
| 17 | new_lines.append(line) |
|---|
| 18 | elif isinstance(lines,str): |
|---|
| 19 | for non_char in non_valid_characters: |
|---|
| 20 | lines = lines.replace(non_char[0],non_char[1]) |
|---|
| 21 | new_lines = lines |
|---|
| 22 | return new_lines |
|---|
| 23 | |
|---|
| 24 | def latex_table(k,v): |
|---|
| 25 | """ |
|---|
| 26 | """ |
|---|
| 27 | tables.append((k,v)) |
|---|
| 28 | |
|---|
| 29 | def add_latex_tables(): |
|---|
| 30 | """ |
|---|
| 31 | """ |
|---|
| 32 | def write_first_row(content): |
|---|
| 33 | line = ' &' |
|---|
| 34 | for column in content.column_labels(): |
|---|
| 35 | line += ' '+column |
|---|
| 36 | line += ' &' |
|---|
| 37 | return line[:-1] |
|---|
| 38 | def write_follwing_rows(content,lines): |
|---|
| 39 | for row in content.rows(): |
|---|
| 40 | line = row[0]+' &' |
|---|
| 41 | for value in row[1].values(): |
|---|
| 42 | if isinstance(value, basestring): |
|---|
| 43 | line += value+' &' |
|---|
| 44 | else: |
|---|
| 45 | line += ' %s &'%value |
|---|
| 46 | lines.append(line[:-1]+'\\\ \n') |
|---|
| 47 | |
|---|
| 48 | for table in tables: |
|---|
| 49 | name,content = table |
|---|
| 50 | pos = 'c'*(len(content.column_labels())+1) |
|---|
| 51 | lines.append('\\begin{table*}[ht]\n') |
|---|
| 52 | lines.append('\\begin{center}\n') |
|---|
| 53 | lines.append('\\begin{tabular}{%s} \n'%pos) |
|---|
| 54 | lines.append(write_first_row(content)+'\\\ \n') |
|---|
| 55 | write_follwing_rows(content,lines) |
|---|
| 56 | lines.append('\\end{tabular} \n') |
|---|
| 57 | lines.append('\\end{center}\n') |
|---|
| 58 | lines.append('\\caption{%s}\n'% name) |
|---|
| 59 | lines.append('\\label{%s}'%name+'\n') |
|---|
| 60 | lines.append('\\end{table*}\n') |
|---|
| 61 | |
|---|
| 62 | def walk(d,indent, ind_incr): |
|---|
| 63 | """ |
|---|
| 64 | """ |
|---|
| 65 | s = [] |
|---|
| 66 | keys = d.keys() |
|---|
| 67 | keys.sort() |
|---|
| 68 | for key in keys: |
|---|
| 69 | k = key |
|---|
| 70 | v = d[key] |
|---|
| 71 | if hasattr(v, 'items') and not isinstance(v,ParameterTable): |
|---|
| 72 | s.append("\\hspace*{%scm} %s: " % (indent, k)) |
|---|
| 73 | s.append(walk(v, indent+ind_incr, ind_incr)) |
|---|
| 74 | s.append('\\hspace*{%scm} ' % indent) |
|---|
| 75 | elif isinstance(v,ParameterRange): |
|---|
| 76 | s.append("\\hspace*{%scm} %s : %s" % (indent, k, str(v._values))) |
|---|
| 77 | elif isinstance(v,ParameterTable): |
|---|
| 78 | s.append("\\hspace*{%scm} %s : see Table~\\ref{%s} " % (indent, k,k)) |
|---|
| 79 | latex_table(k,v) |
|---|
| 80 | elif isinstance(v, basestring): |
|---|
| 81 | s.append("\\hspace*{%scm} %s : %s" % (indent, k, v)) |
|---|
| 82 | else: |
|---|
| 83 | s.append("\\hspace*{%scm} %s : %s" % (indent, k, v)) |
|---|
| 84 | return '\\\ \n'.join(s) |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | line = walk(d,0.0,indent) |
|---|
| 88 | f = open(filename,'w') |
|---|
| 89 | line = remove_non_valid_characters(line) |
|---|
| 90 | f.write(line) |
|---|
| 91 | f.close() |
|---|
| 92 | |
|---|
| 93 | add_latex_tables() |
|---|
| 94 | f = open('tables_'+filename,'w') |
|---|
| 95 | lines = remove_non_valid_characters(lines) |
|---|
| 96 | f.writelines(lines) |
|---|
| 97 | f.close() |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | |
|---|