forked from alchemyst/Skogestad-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreporting.py
130 lines (115 loc) · 5.1 KB
/
reporting.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from __future__ import print_function
import numpy as np
def display_export_data(data, display_type, row_head, save=False, latex=False, width=None, sep='|'):
"""
Create a table view of data. Data can also be exported for a csv file or
LaTex tabular format. This function is ideal to display large amounts of
data.
Parameters
----------
data : array or arrays
The transfer function G(s) of the system. The first item (data[0]) in
the array must be an array of independent variables. From the second
item onward (data[1], data[2], ...), an array of dependent variables
are defined. The dependent varaiable array should be defined as an
array itself.
display_type : string
Choose the main display label and file name to use.
row_head : array
A list is row headings for the depended variables.
save : boolean
If true, a csv data file is saved, with wthe name pz_type.csv
(optional).
latex : boolean
If true, the data file is further converted to LaTex tabular format
(optional).
width : integer
If the width of the rows exceed the width of a page, this number will
limits the number of items to be displayed in a row. Multple tables are
created. (optional).
sep : char
Specify the separator operator to use in the table (optional).
Returns
-------
File : csv file
Print : screen output
"""
if latex:
save = True # a file needs to be saved for Latex output
f = open(display_type + '.tex', 'wb')
elif save: f = open(display_type + '.csv', 'wb')
row_heading = [] # array to store heading labels
n = np.shape(row_head)[0] # number of types of headings
for i in range(n):
m = len(data[0][i + 1]) # number of items for heading type
for j in range(m):
if m > 1: # increment heading label
if latex:
row_heading.append('${0}_{1}$'.format(row_head[i], j + 1))
else:
row_heading.append('{0}{1}'.format(row_head[i], j + 1))
else:
if latex:
row_heading.append('${0}$'.format(row_head[i]))
else:
row_heading.append('{0}'.format(row_head[i]))
if width is not None: # separate the data in sections for the specified width
sec = []
m = len(data)
section = m / width
sectionlast = m % width
if sectionlast != 0: # the last section of data might be less than the specified width
section += 1
for s in range(section):
sec.append(data[width * s:width * (s + 1)])
else:
sec = [data]
for s in sec: # cycle through all the data sections
top = display_type # main label
tabs = '' # used for LaTex format only
rows = row_heading[:] # reinitialise the main data array with heading labels
o = len(s) # number if columns in the section
for i in range(o): # cycle through columns
top += ' ' + sep + ' {:.3e}'.format(s[i][0]) # format independent variables
row_count = 0
for j in range(n): # cycle through row headings
m = len(data[0][j + 1]) # each heading type count could be different
for k in range(m): # cycle through items in heading type
u = s[i][j + 1][k] # extract data
if isinstance(u, (float)): # data is float
u = '{:.3e}'.format(u)
elif isinstance(u, (str, bool, int)): # data is string or boolean
u = ' {}'.format(u)
else: # data is matrix
if latex: # improves formatting
if u.imag == 0:
u = ' \\num{' + '{:.3e}'.format(u[0, 0]) + '}'
else:
u = ' \\num{' + '{:.3e}'.format(u[0, 0].real) + '}\
\\num{' + '{:.3e}'.format(u[0, 0].imag) + '}i'
else:
if u.imag == 0:
u = '{:.3e}'.format(u[0, 0])
else:
u = '{:.3e}'.format(u[0, 0].real) + '{:.3e}'.format(u[0, 0].imag)
rows[row_count] += ' ' + sep + u # format dependent variable
row_count += 1
tabs += 'c '
if latex:
header = '\\begin{tabular}{%sc}\n' % tabs # add an extra c
header += '\\toprule\n'
header += top + '\\\\\n'
header += '\\midrule\n'
f.write(header)
elif save: f.write(top + '\n')
if not latex: print(top)
if not latex: print('')
for i in range(len(rows)):
if not latex: print(rows[i])
if save: f.write(rows[i] + '\\\\\n')
if not latex: print('')
if latex:
footer = '\\bottomrule\n'
footer += '\\end{tabular}\n\n'
f.write(footer)
if save: f.close()