Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split pretty print from value calculations #615

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 47 additions & 11 deletions burnman/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,31 @@ def flatten(arr):
)


def pretty_print_values(popt, pcov, params, extra_decimal_places=0):
def pretty_string_values(
popt, pcov, extra_decimal_places=0, combine_value_and_sigma=False
):
"""
Takes a numpy array of parameters, the corresponding covariance matrix
and a set of parameter names and prints the parameters and
principal 1-s.d.uncertainties (np.sqrt(pcov[i][i]))
in a nice text based format.
and a set of parameter names and returns the scaled variables and
principal 1-s.d.uncertainties (np.sqrt(pcov[i][i])) and scaling factor
as three separate lists of strings.

:param popt: Parameter values
:type popt: numpy array
:param pcov: Variance-covariance matrix
:type pcov: 2D numpy array
:param extra_decimal_places: extra precision for values, defaults to 0
:type extra_decimal_places: int, optional
:param combine_value_and_sigma: give values in value(sigma) format, defaults to False
:type combine_value_and_sigma: bool, optional
:return: values, uncertainties and the scaling factors
:rtype: tuple of 3 lists
"""
for i, p in enumerate(params):
p_rnd = round_to_n(popt[i], np.sqrt(pcov[i][i]), 1 + extra_decimal_places)
pval = []
psig = []
pscale = []
for i, p in enumerate(popt):
p_rnd = round_to_n(p, np.sqrt(pcov[i][i]), 1 + extra_decimal_places)
c_rnd = round_to_n(
np.sqrt(pcov[i][i]), np.sqrt(pcov[i][i]), 1 + extra_decimal_places
)
Expand All @@ -90,11 +106,31 @@ def pretty_print_values(popt, pcov, params, extra_decimal_places=0):

scale = np.power(10.0, p_expnt)
nd = p_expnt - np.floor(np.log10(np.abs(c_rnd))) + extra_decimal_places
print(
"{0:s}: ({1:{4}{5}f} +/- {2:{4}{5}f}) x {3:.0e}".format(
p, p_rnd / scale, c_rnd / scale, scale, 0, (nd) / 10.0
)
)
pval.append(f"{p_rnd / scale:0{nd / 10.0}f}")
if combine_value_and_sigma:
pval[-1] = f"{pval[-1]}({int(c_rnd / scale * np.power(10, nd))})"
psig.append(f"{c_rnd / scale:0{nd / 10.0}f}")
pscale.append(f"{scale:.0e}")
return (pval, psig, pscale)


def pretty_print_values(popt, pcov, params, extra_decimal_places=0):
"""
Takes a numpy array of parameters, the corresponding covariance matrix
and a set of parameter names and prints the scaled variables and
principal 1-s.d.uncertainties (np.sqrt(pcov[i][i])) and scaling factor
in an easy to read format.

:param popt: Parameter values
:type popt: numpy array
:param pcov: Variance-covariance matrix
:type pcov: 2D numpy array
:param extra_decimal_places: extra precision for values, defaults to 0
:type extra_decimal_places: int, optional
"""
pval, psig, pscale = pretty_string_values(popt, pcov, extra_decimal_places)
for i, p in enumerate(params):
print(f"{p:s}: ({pval[i]} +/- {psig[i]}) x {pscale[i]}")


def pretty_print_table(table, use_tabs=False):
Expand Down
Loading