Skip to content

afmslicer.io

Input/Output related functions.

dict_to_df(data)

Convert a nested dictionary to a dataframe.

Layers of pore statistics are converted to a dataframe.

Parameters:

Name Type Description Default
data dict[int | str, Any]

Nested dictionary to be converted.

required

Returns:

Type Description
DataFrame

Pandas dataframe of statistics.

Source code in src/afmslicer/io.py
def dict_to_df(data: dict[int | str, Any]) -> pd.DataFrame:
    """
    Convert a nested dictionary to a dataframe.

    Layers of pore statistics are converted to a dataframe.

    Parameters
    ----------
    data : dict[int | str, Any]
        Nested dictionary to be converted.

    Returns
    -------
    pd.DataFrame
        Pandas dataframe of statistics.
    """
    df = pd.json_normalize(
        [
            {"layer": layer, "pore": pore, **props}
            for layer, pores in data.items()
            for pore, props in pores.items()
        ]
    )
    df.set_index(["layer", "pore"], inplace=True)  # noqa: PD002
    return df

write_csv(df, outdir='output', filename='results.csv', index=False)

Write a dataframe to .csv file in output directory.

Parameters:

Name Type Description Default
df DataFrame

Pandas dataframe to write to disk.

required
outdir Path | str

Path where file is to be written.

'output'
filename str

Filename for output, defaults to results.csv.

'results.csv'
index bool

Whether to include the index or not.

False
Source code in src/afmslicer/io.py
def write_csv(
    df: pd.DataFrame,
    outdir: Path | str = "output",
    filename: str = "results.csv",
    index: bool = False,
) -> None:
    """
    Write a dataframe to ``.csv`` file in ``output`` directory.

    Parameters
    ----------
    df : pd.DataFrame
        Pandas dataframe to write to disk.
    outdir : Path | str
        Path where file is to be written.
    filename : str
        Filename for output, defaults to ``results.csv``.
    index : bool
        Whether to include the index or not.
    """
    df.to_csv(Path(outdir) / filename, index=index)