-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexTable.py
executable file
·115 lines (93 loc) · 3.01 KB
/
texTable.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
import numpy as np
from smartFormat import smartFormat
def texTable(array, headings=None, tableFormat=None, numFormat=[(1,5)],
precision=[5], nanSub=['---'], inftyThresh=[np.infty]):
if not isinstance(array, np.ndarray):
array = np.array(array)
nRows = np.shape(array)[0]
print nRows
try:
nCols = np.shape(array)[1]
except:
nCols = nRows
nRows = 1
#-- Begin environment and specify centering, etc. for each col
table = r"\begin{tabular}{"
if tableFormat != None:
table += tableFormat
else:
table += "c"*nCols
table += r"}" + "\n"
#-- Add headings if present
if headings != None:
table += r" & ".join(headings)
else:
table += r" & ".join([r"\ " for n in range(nCols)])
#-- Add horizontal line
table += r" \\ \midrule"
#-- Add table entries
for rowN in range(nRows):
table += "\n" + r" & ".join([smartFormat(array[rowN,colN])
for colN in range(nCols)])
table += r"\\ \addlinespace[5pt]"
if headings == None:
table += r" \midrule" + "\n"
else:
table += "\n"
#-- Close out environment
table += r"\end{tabular}" + "\n"
return table
#if len(numFormat) == 1:
# numFormat = numFormat*nCols
#if len(precision) == 1:
# precision = precision*nCols
#if len(nanSub) == 1:
# nanSub = nanSub*nCols
#if len(inftyThresh) == 1:
# inftyThresh = inftyThresh*nCols
#for colNum in range(nCols):
# vals = array[:,colNum]
# allInts = True
# for val in vals:
# allInts = allInts and isInt(val)
# maxEl = np.max(vals)
# minEl = np.min(vals)
# #-- Compute minimum resolution for discerning elements
# v2 = vals.copy()
# v2.sort()
# aDiff = np.abs(np.diff(v2))
# aDiff = aDiff[aDiff>0]
# if len(aDiff) > 0:
# res = np.min(aDiff[aDiff >
# (10**-(precision+np.floor(np.log10(minEl)))])
# else:
# res = 0
#
# #-- Multiplicative dynamic range
# MDR = np.ceil(np.log10(maxEl)-np.log10(minEl))
# #-- Additive dynamic range
# ADR = np.ceil(np.log10(maxEl-minEl))
#
# dynamicRange = np.ceil(np.log10(maxEl)-np.log10(minEl))
# if dynamicRange <= precision[colNum]:
# fixExp = True
# fixedExp = np.floor(np.log10(minEl))
# else:
# fixExp = False
if __name__ == "__main__":
header = r"""\documentclass{article}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{booktabs}
\usepackage[letterpaper,landscape]{geometry}
\begin{document}{
\begin{centering}
"""
a = np.array([np.logspace(-4,10,10),np.logspace(-4,10,10)*10])
headings = ['Col ' + str(n) for n in range(np.size(a,1))]
body = texTable(a, headings=headings)
footer = r"""
\end{centering}
}\end{document}"""
print header, body, footer