viz.py - DPRS
Author: Colin P. Stark
Description: Directed percolation and related models implemented in Rust and wrapped in Python.

Skip to content

# viz.py

# `` Viz

    Viz(dpi: int = 150, font_size: int = 11, font_family: str = 'Arial')

Provide a visualization class.

Parameters:

- ## `dpi`

  (`int`, default: `150` ) –

  set resolution for rasterized images

- ## `font_size`

  (`int`, default: `11` ) –

  set mpl default font size

- ## `font_family`

  (`str`, default: `'Arial'` ) –

  set mpl default font family

Attributes:

- `dpi` (`int`) –

  rasterization resolution

- `fdict` (`(dict`) –

  dictionary to which each figure is appended as it is generated

Methods:

- `create_figure` –

  Initialize a Pyplot figure.

- `image_lattice` –

  Plot colorized image of lattice.

- `image_lattice_history` –

  Plot colorized image of 1d lattice history.

- `plot_ρmean` –

  Plot time evolution of mean order parameter.

Source code in `python/dprs/viz.py`

    43
    44
    45
    46
    47
    48
    49
    50
    51
    def __init__(
            self, dpi: int=150, font_size: int=11, font_family: str="Arial",
        ) -> None:
        self.dpi = dpi
        self.fdict = {}
        try:
            mpl.rc("font", size=font_size, family=font_family)
        except:
            mpl.rc("font", size=font_size, family="")

## `` create_figure

    create_figure(
        fig_name: str, fig_size: tuple[float, float] | None = None, dpi: int | None = None
    ) -> Figure

Initialize a Pyplot figure.

Set its size and DPI. Append it to the figures dictionary.

Parameters:

- ### `fig_name`

  (`str`) –

  name of figure; used as key in figures dictionary

- ### `fig_size`

  (`tuple[float, float] | None`, default: `None` ) –

  optional width and height of figure in inches

- ### `dpi`

  (`int | None`, default: `None` ) –

  rasterization resolution

Returns:

- `figure` ( `Figure` ) –

  reference to MatPlotLib/Pyplot figure

Source code in `python/dprs/viz.py`

    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    def create_figure(
        self,
        fig_name: str,
        fig_size: tuple[float, float] | None = None,
        dpi: int | None = None,
    ) -> Figure:
        """
        Initialize a Pyplot figure.

        Set its size and DPI. Append it to the figures dictionary.

        Args:
            fig_name:
                name of figure; used as key in figures dictionary
            fig_size:
                optional width and height of figure in inches
            dpi:
                rasterization resolution

        Returns:
            figure:
                reference to MatPlotLib/Pyplot figure
        """
        fig_size_: tuple[float, float] = (
            (6, 4) if fig_size is None else fig_size
        )
        dpi_: float = self.dpi if dpi is None else dpi
        logging.info(
            "Viz:\n   "
            + f"Creating plot: {fig_name} size={fig_size_} @ {dpi_} dpi"
        )
        fig = plt.figure()
        self.fdict.update({fig_name: fig})
        if fig_size_ is not None:
            fig.set_size_inches(*fig_size_)
        fig.set_dpi(dpi_)
        return fig

## `` image_lattice

    image_lattice(
        name: str,
        title: str,
        lattices: NDArray | None,
        p: Parameters,
        i_lattice: int | None = 0,
        x: int | None = None,
        y: int | None = None,
        fig_size: tuple[float, float] = (6, 4),
    ) -> tuple[Figure, Any]

Plot colorized image of lattice.

Source code in `python/dprs/viz.py`

    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    def image_lattice(
            self,
            name: str,
            title: str,
            lattices: NDArray|None,
            p: sim.Parameters,
            i_lattice: int | None = 0,
            x: int | None=None,
            y: int | None=None,
            fig_size: tuple[float,float]=(6,4,),
        ) -> tuple[Figure, Any]:
        """
        Plot colorized image of lattice.
        """
        _ = self.create_figure(fig_name=name, fig_size=fig_size,)
        plt.title(title, fontdict={"fontsize": 11.5})
        color_map = ListedColormap(((0.9, 0.9, 0.9,), (0.65, 0, 0.65),))
        x = (lattices.shape[0] if x is None else min(x, lattices.shape[0]))
        y = (lattices.shape[1] if y is None else min(y, lattices.shape[1]))
        plt.imshow(
            lattices[0:x, 0:y, i_lattice,].T,
            vmin=0, vmax=1,
            cmap=color_map,
            origin="lower",
            extent=(0, x, y, 0),
        )
        color_bar = plt.colorbar(
            ticks=(0.25, 0.75,),
            shrink=0.5*(y/x)**0.25,
            aspect=15,
            label="cell state",
        )
        color_bar.set_ticklabels((0, 1,),)
        plt.xlabel(r"$x$")
        plt.ylabel(r"$y$")
        plt.grid(ls=":")

## `` image_lattice_history

    image_lattice_history(
        name: str,
        title: str,
        lattices: NDArray | None,
        p: Parameters,
        x: int | None = None,
        t: int | None = None,
        fig_size: tuple[float, float] = (6, 4),
    ) -> tuple[Figure, Any]

Plot colorized image of 1d lattice history.

Source code in `python/dprs/viz.py`

     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    def image_lattice_history(
            self,
            name: str,
            title: str,
            lattices: NDArray|None,
            p: sim.Parameters,
            x: int | None=None,
            t: int | None=None,
            fig_size: tuple[float,float]=(6,4,),
        ) -> tuple[Figure, Any]:
        """
        Plot colorized image of 1d lattice history.
        """
        _ = self.create_figure(fig_name=name, fig_size=fig_size,)
        plt.title(title, fontdict={"fontsize": 11.5})
        color_map = ListedColormap(((0.9, 0.9, 0.9,), (0.65, 0, 0.65),))
        x = (lattices.shape[0] if x is None else min(x, lattices.shape[0]))
        t = (lattices.shape[1] if t is None else min(t, lattices.shape[1]))
        plt.imshow(
            lattices[0:x, 0:t, ].T,
            vmin=0, vmax=1,
            cmap=color_map,
            extent=(0, x, t-0.5, -0.5),
        )
        color_bar = plt.colorbar(
            ticks=(0.25, 0.75,),
            shrink=0.5*(t/x)**0.25,
            aspect=15,
            label="cell state",
        )
        color_bar.set_ticklabels((0, 1,),)
        plt.xlabel(r"$x$")
        plt.ylabel(r"$t$")
        plt.grid(ls=":")

## `` plot_ρmean

    plot_ρmean(
        name: str,
        title: str,
        tracking: NDArray,
        δ: float,
        ρ_mean_ref: float,
        fig_size: tuple[float, float] = (6, 4),
        i_offset: int = 3,
        do_ref_curve: bool = True,
    ) -> tuple[Figure, Any]

Plot time evolution of mean order parameter.

Source code in `python/dprs/viz.py`

    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    def plot_ρmean(
            self,
            name: str,
            title: str,
            tracking: NDArray,
            δ: float,
            ρ_mean_ref: float,
            fig_size: tuple[float,float]=(6,4,),
            i_offset: int=3,
            do_ref_curve: bool=True,
        ) -> tuple[Figure, Any]:
        """
        Plot time evolution of mean order parameter.
        """
        _ = self.create_figure(fig_name=name, fig_size=fig_size,)
        plt.title(title, fontdict={"fontsize": 13})
        t: NDArray = tracking[0][i_offset:]
        ρ_mean: NDArray = tracking[1][i_offset:]
        ρ_mean_fn = lambda t: ρ_mean_ref*t**(-δ)
        plt.plot(
            t, ρ_mean, lw=0.4, color="k",
        )
        if do_ref_curve:
            plt.plot(
                t, ρ_mean_fn(t), color="blue", alpha=0.5,
                label=r"$\widebar\rho(t) \sim t^{-\delta}$" + rf"$\quad\delta={δ}$",
            )
        plt.legend()
        axes = plt.gca()
        axes.autoscale(enable=True, axis="both", tight=True)
        plt.loglog()
        # plt.ylim(0,)
        # plt.xlim(0,)
        plt.ylabel(r"Mean order parameter  $\widebar\rho(t)$")
        plt.xlabel(r"Time  $t$")
        plt.grid(ls=":")