Skip to content

reporting #

Utilities for reporting results.

Functions:

mols_to_img #

mols_to_img(
    *smiles: str, width: int = 400, height: int = 200
) -> str

Renders a set of molecules as an embeddable HTML image tag.

Parameters:

  • *smiles (str, default: () ) –

    The SMILES patterns of the molecules to render.

  • width (int, default: 400 ) –

    The width of the image.

  • height (int, default: 200 ) –

    The height of the image.

Returns:

  • str

    The HTML image tag.

Source code in descent/utils/reporting.py
def mols_to_img(*smiles: str, width: int = 400, height: int = 200) -> str:
    """Renders a set of molecules as an embeddable HTML image tag.

    Args:
        *smiles: The SMILES patterns of the molecules to render.
        width: The width of the image.
        height: The height of the image.

    Returns:
        The HTML image tag.
    """
    from rdkit import Chem
    from rdkit.Chem import Draw

    assert len(smiles) > 0

    mol = _mol_from_smiles(smiles[0])

    for pattern in smiles[1:]:
        mol = Chem.CombineMols(mol, _mol_from_smiles(pattern))

    mol = Draw.PrepareMolForDrawing(mol, forceCoords=True)

    drawer = Draw.rdMolDraw2D.MolDraw2DSVG(width, height)
    drawer.DrawMolecule(mol)
    drawer.FinishDrawing()

    data = base64.b64encode(drawer.GetDrawingText().encode()).decode()
    return f'<img src="data:image/svg+xml;base64,{data}"></img>'

figure_to_img #

figure_to_img(figure: Figure) -> str

Convert a matplotlib figure to an embeddable HTML image tag.

Parameters:

  • figure (Figure) –

    The figure to convert.

Returns:

  • str

    The HTML image tag.

Source code in descent/utils/reporting.py
def figure_to_img(figure: "pyplot.Figure") -> str:
    """Convert a matplotlib figure to an embeddable HTML image tag.

    Args:
        figure: The figure to convert.

    Returns:
        The HTML image tag.
    """

    with io.BytesIO() as stream:
        figure.savefig(stream, format="svg")
        data = base64.b64encode(stream.getvalue()).decode()

    return f'<img src="data:image/svg+xml;base64,{data}"></img>'

print_potential_summary #

print_potential_summary(potential: TensorPotential)

Print a summary of the potential parameters to the terminal.

Parameters:

  • potential (TensorPotential) –

    The potential.

Source code in descent/utils/reporting.py
def print_potential_summary(potential: smee.TensorPotential):
    """Print a summary of the potential parameters to the terminal.

    Args:
        potential: The potential.
    """
    import pandas

    parameter_rows = []

    for key, value in zip(
        potential.parameter_keys, potential.parameters.detach(), strict=True
    ):
        row = {"ID": _format_parameter_id(key.id)}
        row.update(
            {
                f"{col}{_format_unit(potential.parameter_units[idx])}": (
                    f"{value[idx].item():.4f}"
                )
                for idx, col in enumerate(potential.parameter_cols)
            }
        )
        parameter_rows.append(row)

    print(f" {potential.type} ".center(88, "="), flush=True)
    print(f"fn={potential.fn}", flush=True)

    if potential.attributes is not None:
        attribute_rows = [
            {
                f"{col}{_format_unit(potential.attribute_units[idx])}": (
                    f"{potential.attributes[idx].item():.4f} "
                )
                for idx, col in enumerate(potential.attribute_cols)
            }
        ]
        print("")
        print("attributes=", flush=True)
        print("")
        print(pandas.DataFrame(attribute_rows).to_string(index=False), flush=True)

    print("")
    print("parameters=", flush=True)
    print("")
    print(pandas.DataFrame(parameter_rows).to_string(index=False), flush=True)

print_force_field_summary #

print_force_field_summary(force_field: TensorForceField)

Print a summary of the force field parameters to the terminal.

Parameters:

  • force_field (TensorForceField) –

    The force field.

Source code in descent/utils/reporting.py
def print_force_field_summary(force_field: smee.TensorForceField):
    """Print a summary of the force field parameters to the terminal.

    Args:
        force_field: The force field.
    """

    if force_field.v_sites is not None:
        print_v_site_summary(force_field.v_sites)
        print("")

    for potential in force_field.potentials:
        print_potential_summary(potential)
        print("")