Skip to content

Commit 4743504

Browse files
committed
Fixing formatting of tables.
1 parent 279b216 commit 4743504

1 file changed

Lines changed: 58 additions & 36 deletions

File tree

diffusivity_step/diffusivity.py

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import traceback
1313

1414
import numpy as np
15-
from tabulate import tabulate
15+
import tabulate
1616

1717
from .analysis import (
1818
read_dump_trajectory,
@@ -163,7 +163,7 @@ def __init__(
163163
("x", "red", "rgba(255,0,0,0.1)"),
164164
("y", "green", "rgba(0,255,0,0.1)"),
165165
("z", "blue", "rgba(0,0,255,0.1)"),
166-
("", "black", "rgba(0,0,0,0.1)"),
166+
(" ", "black", "rgba(0,0,0,0.1)"),
167167
]
168168

169169
self._configuration = None # The initial configuration
@@ -301,16 +301,16 @@ def analyze_run(
301301

302302
table = {
303303
"Run": [],
304-
"Method": [],
304+
" Method ": [],
305305
"Species": [],
306-
"Dx": [],
307-
"ex": [],
308-
"Dy": [],
309-
"ey": [],
310-
"Dz": [],
311-
"ez": [],
312-
"D": [],
313-
"e": [],
306+
" Dx ": [],
307+
" ex ": [],
308+
" Dy ": [],
309+
" ey ": [],
310+
" Dz ": [],
311+
" ez ": [],
312+
" D ": [],
313+
" e ": [],
314314
}
315315
if correction is not None:
316316
table["Correction"] = []
@@ -448,16 +448,16 @@ def analyze_run(
448448
value = round(value, 1)
449449
var = var.replace(",stderr", " ±")
450450
table[var].append(value)
451-
table["Method"].append("MSD")
451+
table[" Method "].append("MSD".center(20))
452452
else:
453453
table["Run"].append("")
454454
if correction is not None:
455455
table["Correction"].append("")
456-
table["Method"].append("")
456+
table[" Method "].append("")
457457
table["Species"].append(smiles)
458458
alpha = self._tensor_labels[i][0]
459-
table["D" + alpha].append(v)
460-
table["e" + alpha].append("")
459+
table[" D" + alpha + " "].append(v)
460+
table[" e" + alpha + " "].append("")
461461

462462
add_msd_trace(
463463
plot,
@@ -635,14 +635,16 @@ def analyze_run(
635635
value = round(value, 1)
636636
var = var.replace(",stderr", " ±")
637637
table[var].append(value)
638-
table["Method"].append("Helfand Moments")
638+
table[" Method "].append(
639+
"Helfand Moments".center(20)
640+
)
639641
else:
640642
table["Run"].append("")
641-
table["Method"].append("")
643+
table[" Method "].append("")
642644
table["Species"].append(smiles)
643645
alpha = self._tensor_labels[i][0]
644-
table["D" + alpha].append(v)
645-
table["e" + alpha].append("")
646+
table[" D" + alpha + " "].append(v)
647+
table[" e" + alpha + " "].append("")
646648

647649
add_helfand_trace(
648650
plot,
@@ -687,19 +689,23 @@ def analyze_run(
687689
table[var].append(t_v)
688690
table[var + " ±"].append(t_e)
689691
if self._use_velocity:
690-
table["Method"].append("Current MSD Estimate")
692+
table[" Method "].append(
693+
"Current MSD Estimate"
694+
)
691695
else:
692-
table["Method"].append("Current Estimate")
696+
table[" Method "].append(
697+
"Current Estimate".center(20)
698+
)
693699
else:
694700
table["Run"].append("")
695-
table["Method"].append("")
701+
table[" Method "].append("")
696702
table["Species"].append(smiles)
697703

698704
mean = msd_data["mean"][smiles][i][-1]
699705
stderr = msd_data["stderr"][smiles][i][-1]
700706
alpha = self._tensor_labels[i][0]
701-
table["D" + alpha].append(mean)
702-
table["e" + alpha].append(stderr)
707+
table[" D" + alpha + " "].append(mean)
708+
table[" e" + alpha + " "].append(stderr)
703709
if self._use_velocity:
704710
if i == 0:
705711
if spec == 0:
@@ -717,12 +723,16 @@ def analyze_run(
717723
table[var].append(t_v)
718724
table[var + " ±"].append(t_e)
719725
if self._use_msd:
720-
table["Method"].append("Current HM Estimate")
726+
table[" Method "].append(
727+
"Current HM Estimate".center(20)
728+
)
721729
else:
722-
table["Method"].append("Current Estimate")
730+
table[" Method "].append(
731+
"Current Estimate".center(20)
732+
)
723733
else:
724734
table["Run"].append("")
725-
table["Method"].append("")
735+
table[" Method "].append("")
726736
table["Species"].append(smiles)
727737

728738
mean = M_data["mean"][smiles][i][-1]
@@ -746,10 +756,12 @@ def analyze_run(
746756
)
747757
table[var].append(t_v)
748758
table[var + " ±"].append(t_e)
749-
table["Method"].append("Combined Estimate")
759+
table[" Method "].append(
760+
"Combined Estimate".center(20)
761+
)
750762
else:
751763
table["Run"].append("")
752-
table["Method"].append("")
764+
table[" Method "].append("")
753765
table["Species"].append(smiles)
754766

755767
mean = M_data["combined mean"][smiles][i][-1]
@@ -760,13 +772,21 @@ def analyze_run(
760772

761773
# Print the table of results
762774
text = ""
763-
tmp = tabulate(
775+
tabulate.PRESERVE_WHITESPACE = True
776+
tmp = tabulate.tabulate(
764777
table,
765778
headers="keys",
766779
tablefmt="rounded_outline",
767780
disable_numparse=True,
768781
)
769-
if run == 1:
782+
tabulate.PRESERVE_WHITESPACE = False
783+
if False:
784+
length = len(tmp.splitlines()[0])
785+
text += "\n"
786+
text += f"Diffusion Coefficients (* {self._scale:.1e} m^2/s)".center(length)
787+
text += "\n"
788+
text += tmp
789+
elif run == 1:
770790
length = len(tmp.splitlines()[0])
771791
text += "\n"
772792
text += f"Diffusion Coefficients (* {self._scale:.1e} m^2/s)".center(length)
@@ -870,7 +890,7 @@ def analyze(
870890
err = round(e * self._scale, decimals)
871891
table["Species"].append(smiles if i == 0 else "")
872892
table["Method"].append("MSD" if i == 0 else "")
873-
if self._tensor_labels[i][0] == "":
893+
if self._tensor_labels[i][0] == " ":
874894
table["Dir"].append("total")
875895
if "D {key} (MSD)" in self.results:
876896
self.results["D {key} (MSD)"][smiles] = d_coeff
@@ -922,7 +942,7 @@ def analyze(
922942
err = round(e * self._scale, decimals)
923943
table["Species"].append(smiles if i == 0 else "")
924944
table["Method"].append("Helfand Moments" if i == 0 else "")
925-
if self._tensor_labels[i][0] == "":
945+
if self._tensor_labels[i][0] == " ":
926946
table["Dir"].append("total")
927947
if "D {key} (HM)" in self.results:
928948
self.results["D {key} (HM)"][smiles] = d_coeff
@@ -974,7 +994,7 @@ def analyze(
974994
err = round(e * self._scale, decimals)
975995
table["Species"].append(smiles if i == 0 else "")
976996
table["Method"].append("Combined" if i == 0 else "")
977-
if self._tensor_labels[i][0] == "":
997+
if self._tensor_labels[i][0] == " ":
978998
table["Dir"].append("total")
979999
if "D {key}" in self.results:
9801000
self.results["D {key}"][smiles] = d_coeff
@@ -1031,7 +1051,8 @@ def analyze(
10311051
table["95%"].append("")
10321052

10331053
text = ""
1034-
tmp = tabulate(
1054+
tabulate.PRESERVE_WHITESPACE = True
1055+
tmp = tabulate.tabulate(
10351056
table,
10361057
headers="keys",
10371058
tablefmt="rounded_outline",
@@ -1044,6 +1065,7 @@ def analyze(
10441065
"decimal",
10451066
),
10461067
)
1068+
tabulate.PRESERVE_WHITESPACE = False
10471069
length = len(tmp.splitlines()[0])
10481070
text += "\n"
10491071
text += f"Diffusion Coefficients (* {self._scale:.1e} m^2/s)".center(length)
@@ -1271,7 +1293,7 @@ def run(self):
12711293
f"{100*len(x)/self.n_molecules:.2f}" for x in self.species.values()
12721294
],
12731295
}
1274-
tmp = tabulate(
1296+
tmp = tabulate.tabulate(
12751297
table,
12761298
headers="keys",
12771299
tablefmt="simple_outline",

0 commit comments

Comments
 (0)