Example #1
0
def plot_collection(
    collection: DatasetCollection,
    order: Optional[list] = list(),
    styles: Optional[dict] = dict(),
    interactive: bool = True,
    n: int = 0,
) -> Figure:

    # check if collection is not empty
    if not collection.store:
        raise ValueError("Collection is empty.")

    # check if time and iteration arrays are equal
    for check in ["iteration", "time"]:
        arr = [
            np.array(dataset.axes[check])
            for dataset in collection.store.values()
        ]
        if not all(np.array_equal(arr[0], i) for i in arr):
            raise ValueError(
                f"Attribute `{check}` is not the same for all datasets " +
                "in the collection.")

    f_a = []

    for dataset in collection.store.values():
        i_style = styles[
            dataset.name] if dataset.name in styles.keys() else None

        p_plan = PlotPlan(
            dataset=dataset,
            style=filter_style(dataset.plot_type(), i_style),
        )

        a_plan = AxesPlan(axes=None,
                          plots=[p_plan],
                          style=filter_style(Axes, i_style))

        f_a.append(a_plan)

    f_plan = FigurePlan(
        fig=None,
        axes=f_a,
        style=filter_style(Figure,
                           styles["fig"] if "fig" in styles.keys() else None),
    )

    if len(dataset) > 1 and inside_notebook() and interactive:
        f_plan.build_interactive(n)

    else:
        return f_plan.build()
Example #2
0
def plot_particle_dataset(
        dataset: ParticleDataset,
        fig: Optional[Figure] = None,
        axes: Optional[Axes] = None,
        style: dict = dict(),
        interactive: bool = True,
        n: int = 0,
):
    """Plots a single/multiple iteration :class:`nata.containers.ParticleDataset`\
       using a :class:`nata.plots.types.ScatterPlot`.

        Parameters
        ----------
        fig: :class:`nata.plots.Figure`, optional
            If provided, the plot is drawn on ``fig``. The plot is drawn on
            ``axes`` if it is a child axes of ``fig``, otherwise a new axes
            is created on ``fig``. If ``fig`` is not provided, a new
            :class:`nata.plots.Figure` is created.

        axes: :class:`nata.plots.Axes`, optional
            If provided, the plot is drawn on ``axes``, which must be an axes
            of ``fig``. If ``axes`` is not provided or is provided without a
            corresponding ``fig``, a new :class:`nata.plots.Axes` is created in
            a new :class:`nata.plots.Figure`.

        style: ``dict``, optional
            Dictionary that takes a mix of style properties of
            :class:`nata.plots.Figure`, :class:`nata.plots.Axes` and any plot
            type (see :class:`nata.plots.types.ScatterPlot`).

        interactive: ``bool``, optional
            Controls wether interactive widgets should be shown with the plot
            to allow for temporal navigation. Only applicable if ``dataset``
            has multiple iterations.

        n: ``int``, optional
            Selects the index of the iteration to be shown initially. Only
            applicable if ``dataset`` has multiple iterations, .

        Returns
        ------
        :class:`nata.plots.Figure` or ``None``:
            Figure with plot built based on ``dataset``. Interactive widgets
            are shown with the figure if ``dataset`` has multiple iterations,
            in which case this method returns  ``None``.

        Examples
        --------
        To get a plot with default style properties in a new figure, simply
        call the ``.plot()`` method. The first two quantities in the dataset
        ``quantities`` dictionary will be represented in the horizontal and
        vertical plot axes, respectively. If a third quantity is available, it
        will be represented in colors.

        >>> from nata.containers import ParticleDataset
        >>> import numpy as np
        >>> arr = np.arange(30).reshape(1,10,3)
        >>> ds = ParticleDataset("path/to/file")
        >>> fig = ds.plot()

        The list of quantities in the dataset can be filtered with the
        :meth:`nata.containers.ParticleDataset.filter` method.

        >>> fig = ds.filter(quantities=["x1", "p1", "ene"]).plot()

    """

    p_plan = PlotPlan(
        dataset=dataset,
        style=filter_style(dataset.plot_type(), style),
    )

    a_plan = AxesPlan(axes=axes,
                      plots=[p_plan],
                      style=filter_style(Axes, style))

    f_plan = FigurePlan(fig=fig,
                        axes=[a_plan],
                        style=filter_style(Figure, style))

    if len(dataset) > 1:
        if inside_notebook():
            if interactive:
                f_plan.build_interactive(n)
            else:
                return f_plan[n].build()
        else:
            # TODO: remove last line from warn
            warn(f"Plotting only iteration with index n={str(n)}." +
                 " Interactive plots of multiple iteration datasets are not" +
                 " supported outside notebook environments.")
            return f_plan[n].build()

    else:
        return f_plan.build()
Example #3
0
def plot_grid_dataset(
    dataset: GridDataset,
    fig: Optional[Figure] = None,
    axes: Optional[Axes] = None,
    style: Optional[dict] = {},
    interactive: Optional[bool] = True,
    n: Optional[int] = 0,
) -> Union[Figure, None]:
    """Plots a single/multiple iteration :class:`nata.containers.GridDataset`\
       using a :class:`nata.plots.types.LinePlot` or\
       :class:`nata.plots.types.ColorPlot` if the dataset is one- or\
       two-dimensional, respectively.

        Parameters
        ----------
        fig: :class:`nata.plots.Figure`, optional
            If provided, the plot is drawn on ``fig``. The plot is drawn on
            ``axes`` if it is a child axes of ``fig``, otherwise a new axes
            is created on ``fig``. If ``fig`` is not provided, a new
            :class:`nata.plots.Figure` is created.

        axes: :class:`nata.plots.Axes`, optional
            If provided, the plot is drawn on ``axes``, which must be an axes
            of ``fig``. If ``axes`` is not provided or is provided without a
            corresponding ``fig``, a new :class:`nata.plots.Axes` is created in
            a new :class:`nata.plots.Figure`.

        style: ``dict``, optional
            Dictionary that takes a mix of style properties of
            :class:`nata.plots.Figure`, :class:`nata.plots.Axes` and any plot
            type (see :class:`nata.plots.types.LinePlot` or
            :class:`nata.plots.types.ColorPlot`).

        interactive: ``bool``, optional
            Controls wether interactive widgets should be shown with the plot
            to allow for temporal navigation. Only applicable if ``dataset``
            has multiple iterations.

        n: ``int``, optional
            Selects the index of the iteration to be shown initially. Only
            applicable if ``dataset`` has multiple iterations, .

        Returns
        ------
        :class:`nata.plots.Figure` or ``None``:
            Figure with plot built based on ``dataset``. Interactive widgets
            are shown with the figure if ``dataset`` has multiple iterations,
            in which case this method returns  ``None``.

        Examples
        --------
        To get a plot with default style properties in a new figure, simply
        call the ``.plot()`` method of the dataset.

        >>> from nata.containers import GridDataset
        >>> import numpy as np
        >>> arr = np.arange(10)
        >>> ds = GridDataset.from_array(arr)
        >>> fig = ds.plot()

        In case a :class:`nata.plots.Figure` is returned by the method, it can
        be shown by calling the :func:`nata.plots.Figure.show` method.

        >>> fig.show()

        To draw a new plot on ``fig``, we can pass it as an argument to the
        ``.plot()`` method. If ``axes`` is provided, the new plot is drawn on
        the selected axes.

        >>> ds2 = GridDataset.from_array(arr**2)
        >>> fig = ds2.plot(fig=fig, axes=fig.axes[0])


    """

    p_plan = PlotPlan(dataset=dataset,
                      style=filter_style(dataset.plot_type(), style))

    a_plan = AxesPlan(axes=axes,
                      plots=[p_plan],
                      style=filter_style(Axes, style))

    f_plan = FigurePlan(fig=fig,
                        axes=[a_plan],
                        style=filter_style(Figure, style))

    if len(dataset) > 1:
        if inside_notebook():
            if interactive:
                f_plan.build_interactive(n)
            else:
                return f_plan[n].build()
        else:
            # TODO: remove last line from warn
            warn(f"Plotting only iteration with index n={str(n)}." +
                 " Interactive plots of multiple iteration datasets are not" +
                 " supported outside notebook environments.")
            return f_plan[n].build()

    else:
        return f_plan.build()