Skip to content

Commit

Permalink
Merge pull request #21 from scipp/move_wrappers
Browse files Browse the repository at this point in the history
Move files around into folders
  • Loading branch information
nvaytet authored Sep 20, 2022
2 parents c554938 + c69088d commit e71f3b5
Show file tree
Hide file tree
Showing 21 changed files with 125 additions and 87 deletions.
5 changes: 2 additions & 3 deletions src/plopp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@

plt.ioff()

from .graph import show_graph
from .model import Node, node, input_node, widget_node
from .wrappers import plot, figure, slicer
from .core import Node, node, input_node, widget_node, show_graph
from .functions import figure, plot, slicer

from . import data

Expand Down
8 changes: 8 additions & 0 deletions src/plopp/core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp)

# flake8: noqa E402, F401

from .graph import show_graph
from .model import Node, node, input_node, widget_node
from .view import View
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions src/plopp/functions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp)

# flake8: noqa E402, F401

from .figure import figure
from .plot import plot
from .slicer import slicer
12 changes: 10 additions & 2 deletions src/plopp/prep.py → src/plopp/functions/common.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp)

from .tools import number_to_variable
from ..core.utils import number_to_variable

from scipp import Variable, DataArray, arange, to_unit
from matplotlib import get_backend
from numpy import ndarray, prod
from scipp import Variable, DataArray, arange, to_unit


def is_interactive_backend():
"""
Return `True` if the current backend used by Matplotlib is the widget backend.
"""
return 'ipympl' in get_backend()


def _to_data_array(obj):
Expand Down
16 changes: 16 additions & 0 deletions src/plopp/functions/figure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp)

from .common import is_interactive_backend


def figure(*args, **kwargs):
"""
Make a figure that is either static or interactive depending on the backend in use.
"""
if is_interactive_backend():
from ..graphics import InteractiveFig
return InteractiveFig(*args, **kwargs)
else:
from ..graphics import StaticFig
return StaticFig(*args, **kwargs)
78 changes: 5 additions & 73 deletions src/plopp/wrappers.py → src/plopp/functions/plot.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,15 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp)

from .figure import Figure
from .model import input_node, widget_node
from .prep import preprocess
from .figure import figure
from ..core import input_node
from .common import preprocess

from scipp import Variable, Dataset
from scipp.typing import VariableLike
import inspect
from matplotlib import get_backend
from numpy import ndarray
from typing import Union, Dict, Literal, List


def _is_interactive_backend():
"""
Return `True` if the current backend used by Matplotlib is the widget backend.
"""
return 'ipympl' in get_backend()


def figure(*args, **kwargs):
"""
Make a figure that is either static or interactive depending on the backend in use.
"""
if _is_interactive_backend():
from .interactive import InteractiveFig
return InteractiveFig(*args, **kwargs)
else:
from .static import StaticFig
return StaticFig(*args, **kwargs)
from typing import Union, Dict, Literal


def plot(obj: Union[VariableLike, ndarray, Dict[str, Union[VariableLike, ndarray]]],
Expand All @@ -45,7 +25,7 @@ def plot(obj: Union[VariableLike, ndarray, Dict[str, Union[VariableLike, ndarray
title: str = None,
vmin: Variable = None,
vmax: Variable = None,
**kwargs) -> Figure:
**kwargs):
"""Plot a Scipp object.
Parameters
Expand Down Expand Up @@ -113,51 +93,3 @@ def plot(obj: Union[VariableLike, ndarray, Dict[str, Union[VariableLike, ndarray
else:
return figure(input_node(preprocess(obj, crop=crop, ignore_size=ignore_size)),
**all_args)


def slicer(obj: Union[VariableLike, ndarray],
keep: List[str] = None,
*,
crop: Dict[str, Dict[str, Variable]] = None,
**kwargs):
"""
Plot a multi-dimensional object by slicing one or more of the dimensions.
This will produce one slider per sliced dimension, below the figure.
Parameters
----------
obj:
The object to be plotted.
keep:
The dimensions to be kept, all remaining dimensions will be sliced. This should
be a list of dims. If no dims are provided, the last dim will be kept in the
case of a 2-dimensional input, while the last two dims will be kept in the case
of higher dimensional inputs.
crop:
Set the axis limits. Limits should be given as a dict with one entry per
dimension to be cropped. Each entry should be a nested dict containing scalar
values for `'min'` and/or `'max'`. Example:
`da.plot(crop={'time': {'min': 2 * sc.Unit('s'), 'max': 40 * sc.Unit('s')}})`
**kwargs:
See :py:func:`plopp.plot` for the full list of figure customization arguments.
Returns
-------
:
A :class:`Box` which will contain a :class:`Figure` and slider widgets.
"""
if not _is_interactive_backend():
raise RuntimeError("The slicer can only be used with the interactive widget "
"backend. Use `%matplotlib widget` at the start of your "
"notebook.")
from plopp.widgets import SliceWidget, slice_dims, Box
da = preprocess(obj, crop=crop, ignore_size=True)
a = input_node(da)

if keep is None:
keep = da.dims[-(2 if da.ndim > 2 else 1):]
sl = SliceWidget(da, dims=list(set(da.dims) - set(keep)))
w = widget_node(sl)
slice_node = slice_dims(a, w)
fig = figure(slice_node, **{**{'crop': crop}, **kwargs})
return Box([fig, sl])
59 changes: 59 additions & 0 deletions src/plopp/functions/slicer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp)

from .common import is_interactive_backend, preprocess
from .figure import figure
from ..core import input_node, widget_node

from scipp import Variable
from scipp.typing import VariableLike
from numpy import ndarray
from typing import Union, Dict, List


def slicer(obj: Union[VariableLike, ndarray],
keep: List[str] = None,
*,
crop: Dict[str, Dict[str, Variable]] = None,
**kwargs):
"""
Plot a multi-dimensional object by slicing one or more of the dimensions.
This will produce one slider per sliced dimension, below the figure.
Parameters
----------
obj:
The object to be plotted.
keep:
The dimensions to be kept, all remaining dimensions will be sliced. This should
be a list of dims. If no dims are provided, the last dim will be kept in the
case of a 2-dimensional input, while the last two dims will be kept in the case
of higher dimensional inputs.
crop:
Set the axis limits. Limits should be given as a dict with one entry per
dimension to be cropped. Each entry should be a nested dict containing scalar
values for `'min'` and/or `'max'`. Example:
`da.plot(crop={'time': {'min': 2 * sc.Unit('s'), 'max': 40 * sc.Unit('s')}})`
**kwargs:
See :py:func:`plopp.plot` for the full list of figure customization arguments.
Returns
-------
:
A :class:`Box` which will contain a :class:`Figure` and slider widgets.
"""
if not is_interactive_backend():
raise RuntimeError("The slicer can only be used with the interactive widget "
"backend. Use `%matplotlib widget` at the start of your "
"notebook.")
from plopp.widgets import SliceWidget, slice_dims, Box
da = preprocess(obj, crop=crop, ignore_size=True)
a = input_node(da)

if keep is None:
keep = da.dims[-(2 if da.ndim > 2 else 1):]
sl = SliceWidget(da, dims=list(set(da.dims) - set(keep)))
w = widget_node(sl)
slice_node = slice_dims(a, w)
fig = figure(slice_node, **{**{'crop': crop}, **kwargs})
return Box([fig, sl])
8 changes: 8 additions & 0 deletions src/plopp/graphics/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp)

# flake8: noqa E402, F401

from .interactive import InteractiveFig
from .static import StaticFig
from .toolbar import Toolbar
4 changes: 2 additions & 2 deletions src/plopp/figure.py → src/plopp/graphics/fig.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp)

from .tools import number_to_variable, name_with_unit
from ..core.utils import number_to_variable, name_with_unit
from ..core import View
from .io import fig_to_bytes
from .mesh import Mesh
from .line import Line
from .view import View

import matplotlib.pyplot as plt
from scipp import DataArray, to_unit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp)

from .figure import Figure
from .fig import Figure
from .toolbar import Toolbar

from ipywidgets import VBox, HBox
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/plopp/line.py → src/plopp/graphics/line.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp)

from .limits import find_limits, fix_empty_range, delta
from ..core.limits import find_limits, fix_empty_range, delta

from scipp import DataArray, stddevs
from functools import reduce
Expand Down
4 changes: 2 additions & 2 deletions src/plopp/mesh.py → src/plopp/graphics/mesh.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp)

from .limits import find_limits, fix_empty_range
from .tools import coord_as_bin_edges, name_with_unit, repeat
from ..core.limits import find_limits, fix_empty_range
from ..core.utils import coord_as_bin_edges, name_with_unit, repeat

from copy import copy
from functools import reduce
Expand Down
2 changes: 1 addition & 1 deletion src/plopp/static.py → src/plopp/graphics/static.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp)

from .figure import Figure
from .fig import Figure
from .io import fig_to_bytes


Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/plopp/widgets/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# Copyright (c) 2022 Scipp contributors (https://github.com/scipp)

from scipp import DataArray
from ..tools import value_to_string
from ..model import node
from ..core.utils import value_to_string
from ..core import node

import ipywidgets as ipw
from typing import Callable
Expand Down

0 comments on commit e71f3b5

Please sign in to comment.