Output classes¶
[1]:
import numpy as np
import pandas as pd
from tabulate import tabulate
from streng.common.io.output import OutputTable, OutputString, OutputExtended
Some example data¶
given as a list of dictionaries
[2]:
data = [{'__iteration': 1,
'x_y': 0.04213429287708525,
'y_y': 1310.8730715333027,
'x_u': 0.44886,
'y_u': 1311.3866666666665,
'kinel': 1.2627555238564436,
'kel': 31111.785247171465,
'k_06': 24639.615704484408,
'error': 0.26267331521364284},
{'__iteration': 2,
'x_y': 0.05395737709629134,
'y_y': 1329.4890360745674,
'x_u': 0.44886,
'y_u': 1329.4890360745674,
'kinel': -91.76544991341024,
'kel': 24639.615704484408,
'k_06': 24476.018099972942,
'error': 0.006683995895216576},
{'__iteration': 3,
'x_y': 0.05434285482397955,
'y_y': 1330.0966982759253,
'x_u': 0.44886,
'y_u': 1330.0966982759253,
'kinel': -94.94217563576123,
'kel': 24476.018099972942,
'k_06': 24469.75466696404,
'error': 0.0002559663181812895},
{'__iteration': 4,
'x_y': 0.05435772305859232,
'y_y': 1330.1201474985282,
'x_u': 0.44886,
'y_u': 1330.1201474985282,
'kinel': -95.064891451666,
'kel': 24469.75466696404,
'k_06': 24469.51314473135,
'error': 9.870332575089462e-06},
{'__iteration': 5,
'x_y': 0.05435829655145874,
'y_y': 1330.1210519911244,
'x_u': 0.44886,
'y_u': 1330.1210519911244,
'kinel': -95.0696250854122,
'kel': 24469.51314473135,
'k_06': 24469.503828907287,
'error': 3.807116044736897e-07}]
Tabulate¶
Not in class but used
using tabulate to show data as markdown table¶
[3]:
print(tabulate(data, headers='keys', tablefmt="pipe", floatfmt=".3E"))
| __iteration | x_y | y_y | x_u | y_u | kinel | kel | k_06 | error |
|--------------:|----------:|----------:|----------:|----------:|-----------:|----------:|----------:|----------:|
| 1 | 4.213E-02 | 1.311E+03 | 4.489E-01 | 1.311E+03 | 1.263E+00 | 3.111E+04 | 2.464E+04 | 2.627E-01 |
| 2 | 5.396E-02 | 1.329E+03 | 4.489E-01 | 1.329E+03 | -9.177E+01 | 2.464E+04 | 2.448E+04 | 6.684E-03 |
| 3 | 5.434E-02 | 1.330E+03 | 4.489E-01 | 1.330E+03 | -9.494E+01 | 2.448E+04 | 2.447E+04 | 2.560E-04 |
| 4 | 5.436E-02 | 1.330E+03 | 4.489E-01 | 1.330E+03 | -9.506E+01 | 2.447E+04 | 2.447E+04 | 9.870E-06 |
| 5 | 5.436E-02 | 1.330E+03 | 4.489E-01 | 1.330E+03 | -9.507E+01 | 2.447E+04 | 2.447E+04 | 3.807E-07 |
Pandas dataframes¶
Not in class but used
[4]:
df = pd.DataFrame(data=data, columns=list(data[0].keys()))
df
[4]:
__iteration | x_y | y_y | x_u | y_u | kinel | kel | k_06 | error | |
---|---|---|---|---|---|---|---|---|---|
0 | 1 | 0.042134 | 1310.873072 | 0.44886 | 1311.386667 | 1.262756 | 31111.785247 | 24639.615704 | 2.626733e-01 |
1 | 2 | 0.053957 | 1329.489036 | 0.44886 | 1329.489036 | -91.765450 | 24639.615704 | 24476.018100 | 6.683996e-03 |
2 | 3 | 0.054343 | 1330.096698 | 0.44886 | 1330.096698 | -94.942176 | 24476.018100 | 24469.754667 | 2.559663e-04 |
3 | 4 | 0.054358 | 1330.120147 | 0.44886 | 1330.120147 | -95.064891 | 24469.754667 | 24469.513145 | 9.870333e-06 |
4 | 5 | 0.054358 | 1330.121052 | 0.44886 | 1330.121052 | -95.069625 | 24469.513145 | 24469.503829 | 3.807116e-07 |
Class: OutputTable¶
[5]:
print(OutputTable.__doc__)
An output table given as a list of dictionaries.
.. uml::
class OutputTable {
.. members ..
+ data: List[dict] = field(default_factory=list)
.. properties ..
+ to_markdown()
+ to_panda_dataframe()
.. methods ..
+ retrieve()
+ retrieve_column_to_list()
+ to_quantity_value()
}
It can be converted for presentation (or usage) as a pandas dataframe or a markdown table
Attributes:
data (List[dict]): A list of dictionaries
get the data¶
[6]:
ot = OutputTable(data=data)
property: to_markdown¶
[7]:
print(ot.to_markdown)
| __iteration | x_y | y_y | x_u | y_u | kinel | kel | k_06 | error |
|--------------:|----------:|----------:|----------:|----------:|-----------:|----------:|----------:|----------:|
| 1 | 4.213E-02 | 1.311E+03 | 4.489E-01 | 1.311E+03 | 1.263E+00 | 3.111E+04 | 2.464E+04 | 2.627E-01 |
| 2 | 5.396E-02 | 1.329E+03 | 4.489E-01 | 1.329E+03 | -9.177E+01 | 2.464E+04 | 2.448E+04 | 6.684E-03 |
| 3 | 5.434E-02 | 1.330E+03 | 4.489E-01 | 1.330E+03 | -9.494E+01 | 2.448E+04 | 2.447E+04 | 2.560E-04 |
| 4 | 5.436E-02 | 1.330E+03 | 4.489E-01 | 1.330E+03 | -9.506E+01 | 2.447E+04 | 2.447E+04 | 9.870E-06 |
| 5 | 5.436E-02 | 1.330E+03 | 4.489E-01 | 1.330E+03 | -9.507E+01 | 2.447E+04 | 2.447E+04 | 3.807E-07 |
property: to_panda_dataframe¶
[8]:
ot.to_panda_dataframe
[8]:
__iteration | x_y | y_y | x_u | y_u | kinel | kel | k_06 | error | |
---|---|---|---|---|---|---|---|---|---|
0 | 1 | 0.042134 | 1310.873072 | 0.44886 | 1311.386667 | 1.262756 | 31111.785247 | 24639.615704 | 2.626733e-01 |
1 | 2 | 0.053957 | 1329.489036 | 0.44886 | 1329.489036 | -91.765450 | 24639.615704 | 24476.018100 | 6.683996e-03 |
2 | 3 | 0.054343 | 1330.096698 | 0.44886 | 1330.096698 | -94.942176 | 24476.018100 | 24469.754667 | 2.559663e-04 |
3 | 4 | 0.054358 | 1330.120147 | 0.44886 | 1330.120147 | -95.064891 | 24469.754667 | 24469.513145 | 9.870333e-06 |
4 | 5 | 0.054358 | 1330.121052 | 0.44886 | 1330.121052 | -95.069625 | 24469.513145 | 24469.503829 | 3.807116e-07 |
method: to_quantity_value(row_number)¶
Μετατρέπει σε νέο OutputTable όπου υπάρχουν 2 στήλες: quantity-value.
Αν δεν οριστεί γραμμή του αρχικού OutputTable, διαβάζει την 1η
[9]:
ot_quantity_value = ot.to_quantity_value()
print(ot_quantity_value.to_markdown)
| quantity | value |
|:------------|----------:|
| __iteration | 1.000E+00 |
| x_y | 4.213E-02 |
| y_y | 1.311E+03 |
| x_u | 4.489E-01 |
| y_u | 1.311E+03 |
| kinel | 1.263E+00 |
| kel | 3.111E+04 |
| k_06 | 2.464E+04 |
| error | 2.627E-01 |
[10]:
ot_quantity_value2 = OutputTable()
ot_quantity_value2 = ot.to_quantity_value(2)
print(ot_quantity_value2.to_markdown)
| quantity | value |
|:------------|-----------:|
| __iteration | 3.000E+00 |
| x_y | 5.434E-02 |
| y_y | 1.330E+03 |
| x_u | 4.489E-01 |
| y_u | 1.330E+03 |
| kinel | -9.494E+01 |
| kel | 2.448E+04 |
| k_06 | 2.447E+04 |
| error | 2.560E-04 |
Class usage¶
show data as pandas dataframe¶
[12]:
ot.to_panda_dataframe
[12]:
__iteration | x_y | y_y | x_u | y_u | kinel | kel | k_06 | error | |
---|---|---|---|---|---|---|---|---|---|
0 | 1 | 0.042134 | 1310.873072 | 0.44886 | 1311.386667 | 1.262756 | 31111.785247 | 24639.615704 | 2.626733e-01 |
1 | 2 | 0.053957 | 1329.489036 | 0.44886 | 1329.489036 | -91.765450 | 24639.615704 | 24476.018100 | 6.683996e-03 |
2 | 3 | 0.054343 | 1330.096698 | 0.44886 | 1330.096698 | -94.942176 | 24476.018100 | 24469.754667 | 2.559663e-04 |
3 | 4 | 0.054358 | 1330.120147 | 0.44886 | 1330.120147 | -95.064891 | 24469.754667 | 24469.513145 | 9.870333e-06 |
4 | 5 | 0.054358 | 1330.121052 | 0.44886 | 1330.121052 | -95.069625 | 24469.513145 | 24469.503829 | 3.807116e-07 |
retrieve value from output table¶
[13]:
ot.retrieve(search_field='__iteration',
search_value=2,
find_field='kinel')
[13]:
-91.76544991341024
retrieve column to list¶
[14]:
ot.retrieve_column_to_list('x_y')
[14]:
[0.04213429287708525,
0.05395737709629134,
0.05434285482397955,
0.05435772305859232,
0.05435829655145874]
OutputString¶
coming soon…see bilin notebook
OutputExtended¶
coming soon…see bilin notebook
[ ]:
[ ]:
[15]:
data
[15]:
[{'__iteration': 1,
'x_y': 0.04213429287708525,
'y_y': 1310.8730715333027,
'x_u': 0.44886,
'y_u': 1311.3866666666665,
'kinel': 1.2627555238564436,
'kel': 31111.785247171465,
'k_06': 24639.615704484408,
'error': 0.26267331521364284},
{'__iteration': 2,
'x_y': 0.05395737709629134,
'y_y': 1329.4890360745674,
'x_u': 0.44886,
'y_u': 1329.4890360745674,
'kinel': -91.76544991341024,
'kel': 24639.615704484408,
'k_06': 24476.018099972942,
'error': 0.006683995895216576},
{'__iteration': 3,
'x_y': 0.05434285482397955,
'y_y': 1330.0966982759253,
'x_u': 0.44886,
'y_u': 1330.0966982759253,
'kinel': -94.94217563576123,
'kel': 24476.018099972942,
'k_06': 24469.75466696404,
'error': 0.0002559663181812895},
{'__iteration': 4,
'x_y': 0.05435772305859232,
'y_y': 1330.1201474985282,
'x_u': 0.44886,
'y_u': 1330.1201474985282,
'kinel': -95.064891451666,
'kel': 24469.75466696404,
'k_06': 24469.51314473135,
'error': 9.870332575089462e-06},
{'__iteration': 5,
'x_y': 0.05435829655145874,
'y_y': 1330.1210519911244,
'x_u': 0.44886,
'y_u': 1330.1210519911244,
'kinel': -95.0696250854122,
'kel': 24469.51314473135,
'k_06': 24469.503828907287,
'error': 3.807116044736897e-07}]
[16]:
def convertDL(val):
if isinstance(val, dict):
return [[k] + v for k, v in val.iteritems()]
return {v[0]: v[1:] for v in val}
[17]:
# data is list of dicts
[18]:
# make it a dict of lists
ddff = pd.DataFrame(data=data)
[19]:
ddff['kel'].tolist()
[19]:
[31111.785247171465,
24639.615704484408,
24476.018099972942,
24469.75466696404,
24469.51314473135]
[20]:
data_dict_of_lists = ddff.to_dict()
[21]:
for i in data[0].keys():
print(i)
__iteration
x_y
y_y
x_u
y_u
kinel
kel
k_06
error
[22]:
data_dict_of_lists['x_y'].values()
[22]:
dict_values([0.04213429287708525, 0.05395737709629134, 0.05434285482397955, 0.05435772305859232, 0.05435829655145874])
[ ]:
[23]:
def lod_to_dol(list_of_dicts):
'''
converts list of dicts --> dict of lists
'''
# _df = pd.DataFrame(data=list_of_dicts)
# _dol = {}
# for key in [*list_of_dicts[0]]: # [*list_of_dicts[0]] = _column_names = list(list_of_dicts[0].keys())
# _dol[key] = _df[key].tolist()
_dol = {k: [dic[k] for dic in list_of_dicts] for k in list_of_dicts[0]}
return _dol
[24]:
dol = lod_to_dol(data)
[25]:
# dol['error']
[26]:
# dol.keys()
[27]:
def dol_to_lod(dict_of_lists):
'''
converts dict of lists --> list of dicts
'''
_lod = [dict(zip(dict_of_lists,t)) for t in zip(*dict_of_lists.values())]
return _lod
[28]:
dol_to_lod(dol)
[28]:
[{'__iteration': 1,
'x_y': 0.04213429287708525,
'y_y': 1310.8730715333027,
'x_u': 0.44886,
'y_u': 1311.3866666666665,
'kinel': 1.2627555238564436,
'kel': 31111.785247171465,
'k_06': 24639.615704484408,
'error': 0.26267331521364284},
{'__iteration': 2,
'x_y': 0.05395737709629134,
'y_y': 1329.4890360745674,
'x_u': 0.44886,
'y_u': 1329.4890360745674,
'kinel': -91.76544991341024,
'kel': 24639.615704484408,
'k_06': 24476.018099972942,
'error': 0.006683995895216576},
{'__iteration': 3,
'x_y': 0.05434285482397955,
'y_y': 1330.0966982759253,
'x_u': 0.44886,
'y_u': 1330.0966982759253,
'kinel': -94.94217563576123,
'kel': 24476.018099972942,
'k_06': 24469.75466696404,
'error': 0.0002559663181812895},
{'__iteration': 4,
'x_y': 0.05435772305859232,
'y_y': 1330.1201474985282,
'x_u': 0.44886,
'y_u': 1330.1201474985282,
'kinel': -95.064891451666,
'kel': 24469.75466696404,
'k_06': 24469.51314473135,
'error': 9.870332575089462e-06},
{'__iteration': 5,
'x_y': 0.05435829655145874,
'y_y': 1330.1210519911244,
'x_u': 0.44886,
'y_u': 1330.1210519911244,
'kinel': -95.0696250854122,
'kel': 24469.51314473135,
'k_06': 24469.503828907287,
'error': 3.807116044736897e-07}]
[29]:
isinstance(dol, dict)
[29]:
True