From f3e64fc40c0b68fe7440ce5c278498f7adf9ce2c Mon Sep 17 00:00:00 2001 From: Caitlin Lewis Date: Tue, 20 Jun 2023 12:09:44 -0400 Subject: [PATCH 01/12] automixin class for generating add_ methods --- fastplotlib/layouts/_base.py | 6 +- fastplotlib/layouts/_subplot.py | 20 +- fastplotlib/utils/__init__.py | 1 + fastplotlib/utils/add_graphics.py | 537 ++++++++++++++++++++++ fastplotlib/utils/generate_add_methods.py | 38 ++ 5 files changed, 591 insertions(+), 11 deletions(-) create mode 100644 fastplotlib/utils/add_graphics.py create mode 100644 fastplotlib/utils/generate_add_methods.py diff --git a/fastplotlib/layouts/_base.py b/fastplotlib/layouts/_base.py index af6009c8e..688969504 100644 --- a/fastplotlib/layouts/_base.py +++ b/fastplotlib/layouts/_base.py @@ -12,6 +12,8 @@ from ..graphics._base import Graphic, GraphicCollection from ..graphics.selectors._base_selector import BaseSelector +from ..utils import GraphicMethods + # dict to store Graphic instances # this is the only place where the real references to Graphics are stored in a Python session # {hex id str: Graphic} @@ -19,7 +21,7 @@ SELECTORS: Dict[str, BaseSelector] = dict() -class PlotArea: +class PlotArea(GraphicMethods): def __init__( self, parent, @@ -57,6 +59,8 @@ def __init__( name: str, optional name of ``subplot`` or ``plot`` subclass being instantiated """ + super(GraphicMethods, self).__init__() + self._parent: PlotArea = parent self._position = position diff --git a/fastplotlib/layouts/_subplot.py b/fastplotlib/layouts/_subplot.py index e2ae59d7e..3c2543e38 100644 --- a/fastplotlib/layouts/_subplot.py +++ b/fastplotlib/layouts/_subplot.py @@ -124,16 +124,16 @@ def __init__( self.docked_viewports[pos] = dv self.children.append(dv) - # attach all the add_ methods - for graphic_cls_name in graphics.__all__: - cls = getattr(graphics, graphic_cls_name) - - pfunc = partial(self._create_graphic, cls) - pfunc.__signature__ = signature(cls) - pfunc.__doc__ = cls.__init__.__doc__ - - # cls.type is defined in Graphic.__init_subclass__ - setattr(self, f"add_{cls.type}", pfunc) + # # attach all the add_ methods + # for graphic_cls_name in graphics.__all__: + # cls = getattr(graphics, graphic_cls_name) + # + # pfunc = partial(self._create_graphic, cls) + # pfunc.__signature__ = signature(cls) + # pfunc.__doc__ = cls.__init__.__doc__ + # + # # cls.type is defined in Graphic.__init_subclass__ + # setattr(self, f"add_{cls.type}", pfunc) self._title_graphic: TextGraphic = None if self.name is not None: diff --git a/fastplotlib/utils/__init__.py b/fastplotlib/utils/__init__.py index c8f754883..67d6b6356 100644 --- a/fastplotlib/utils/__init__.py +++ b/fastplotlib/utils/__init__.py @@ -1 +1,2 @@ from .functions import * +from .add_graphics import GraphicMethods diff --git a/fastplotlib/utils/add_graphics.py b/fastplotlib/utils/add_graphics.py new file mode 100644 index 000000000..bffcaddc0 --- /dev/null +++ b/fastplotlib/utils/add_graphics.py @@ -0,0 +1,537 @@ +from typing import * + +import numpy + +from fastplotlib.graphics import * + + +class GraphicMethods: + def __init__(self): + pass + + def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', + isolated_buffer: bool = True, *args, **kwargs): + """ + Create an Image Graphic + + Parameters + ---------- + data: array-like + array-like, usually numpy.ndarray, must support ``memoryview()`` + Tensorflow Tensors also work **probably**, but not thoroughly tested + | shape must be ``[x_dim, y_dim]`` or ``[x_dim, y_dim, rgb]`` + vmin: int, optional + minimum value for color scaling, calculated from data if not provided + vmax: int, optional + maximum value for color scaling, calculated from data if not provided + cmap: str, optional, default "plasma" + colormap to use to display the image data, ignored if data is RGB + filter: str, optional, default "nearest" + interpolation filter, one of "nearest" or "linear" + isolated_buffer: bool, default True + If True, initialize a buffer with the same shape as the input data and then + set the data, useful if the data arrays are ready-only such as memmaps. + If False, the input array is itself used as the buffer. + args: + additional arguments passed to Graphic + kwargs: + additional keyword arguments passed to Graphic + + Features + -------- + + **data**: :class:`.ImageDataFeature` + Manages the data buffer displayed in the ImageGraphic + + **cmap**: :class:`.ImageCmapFeature` + Manages the colormap + + **present**: :class:`.PresentFeature` + Control the presence of the Graphic in the scene + + + + Examples + -------- + .. code-block:: python + + from fastplotlib import Plot + # create a `Plot` instance + plot = Plot() + # make some random 2D image data + data = np.random.rand(512, 512) + # plot the image data + plot.add_image(data=data) + # show the plot + plot.show() + + """ + g = ImageGraphic(*args, **kwargs) + self.add_graphic(g) + + def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list] = 1, colors: numpy.ndarray = 'w', + alpha: float = 1.0, cmap: str = None, z_position: float = 0.0, *args, **kwargs): + """ + + Create a Scatter Graphic, 2d or 3d + + Parameters + ---------- + data: array-like + Scatter data to plot, 2D must be of shape [n_points, 2], 3D must be of shape [n_points, 3] + + sizes: float or iterable of float, optional, default 1.0 + size of the scatter points + + colors: str, array, or iterable, default "w" + specify colors as a single human readable string, a single RGBA array, + or an iterable of strings or RGBA arrays + + cmap: str, optional + apply a colormap to the scatter instead of assigning colors manually, this + overrides any argument passed to "colors" + + alpha: float, optional, default 1.0 + alpha value for the colors + + z_position: float, optional + z-axis position for placing the graphic + + args + passed to Graphic + + kwargs + passed to Graphic + + Features + -------- + + **data**: :class:`.ImageDataFeature` + Manages the scatter [x, y, z] positions data buffer, allows regular and fancy indexing. + ex: ``scatter.data[:, 0] = 5```, ``scatter.data[xs > 5] = 3`` + + **colors**: :class:`.ColorFeature` + Manages the color buffer, allows regular and fancy indexing. + ex: ``scatter.data[:, 1] = 0.5``, ``scatter.colors[xs > 5] = "cyan"`` + + **present**: :class:`.PresentFeature` + Control the presence of the Graphic in the scene, set to ``True`` or ``False`` + + + """ + g = ScatterGraphic(*args, **kwargs) + self.add_graphic(g) + + def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.ndarray, Iterable] = 'w', + alpha: float = 1.0, cmap: str = None, z_position: float = None, collection_index: int = None, *args, + **kwargs): + """ + + Create a line Graphic, 2d or 3d + + Parameters + ---------- + data: array-like + Line data to plot, 2D must be of shape [n_points, 2], 3D must be of shape [n_points, 3] + + thickness: float, optional, default 2.0 + thickness of the line + + colors: str, array, or iterable, default "w" + specify colors as a single human-readable string, a single RGBA array, + or an iterable of strings or RGBA arrays + + cmap: str, optional + apply a colormap to the line instead of assigning colors manually, this + overrides any argument passed to "colors" + + alpha: float, optional, default 1.0 + alpha value for the colors + + z_position: float, optional + z-axis position for placing the graphic + + args + passed to Graphic + + kwargs + passed to Graphic + + Features + -------- + + **data**: :class:`.ImageDataFeature` + Manages the line [x, y, z] positions data buffer, allows regular and fancy indexing. + ex: ``scatter.data[:, 0] = 5```, ``scatter.data[xs > 5] = 3`` + + **colors**: :class:`.ColorFeature` + Manages the color buffer, allows regular and fancy indexing. + ex: ``scatter.data[:, 1] = 0.5``, ``scatter.colors[xs > 5] = "cyan"`` + + **present**: :class:`.PresentFeature` + Control the presence of the Graphic in the scene, set to ``True`` or ``False`` + + + """ + g = LineGraphic(*args, **kwargs) + self.add_graphic(g) + + def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'auto', + pre_computed: Dict[str, numpy.ndarray] = None, colors: numpy.ndarray = 'w', + draw_scale_factor: float = 100.0, draw_bin_width_scale: float = 1.0, **kwargs): + """ + + Create a Histogram Graphic + + Parameters + ---------- + data: np.ndarray or None, optional + data to create a histogram from, can be ``None`` if pre-computed values are provided to ``pre_computed`` + + bins: int or str, default is "auto", optional + this is directly just passed to ``numpy.histogram`` + + pre_computed: dict in the form {"hist": vals, "bin_edges" : vals}, optional + pre-computed histogram values + + colors: np.ndarray, optional + + draw_scale_factor: float, default ``100.0``, optional + scale the drawing of the entire Graphic + + draw_bin_width_scale: float, default ``1.0`` + scale the drawing of the bin widths + + kwargs + passed to Graphic + + """ + g = HistogramGraphic(*args, **kwargs) + self.add_graphic(g) + + def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', + chunk_size: int = 8192, isolated_buffer: bool = True, *args, **kwargs): + """ + + Create an Image Graphic + + Parameters + ---------- + data: array-like + array-like, usually numpy.ndarray, must support ``memoryview()`` + Tensorflow Tensors also work **probably**, but not thoroughly tested + | shape must be ``[x_dim, y_dim]`` + vmin: int, optional + minimum value for color scaling, calculated from data if not provided + vmax: int, optional + maximum value for color scaling, calculated from data if not provided + cmap: str, optional, default "plasma" + colormap to use to display the data + filter: str, optional, default "nearest" + interpolation filter, one of "nearest" or "linear" + chunk_size: int, default 8192, max 8192 + chunk size for each tile used to make up the heatmap texture + isolated_buffer: bool, default True + If True, initialize a buffer with the same shape as the input data and then + set the data, useful if the data arrays are ready-only such as memmaps. + If False, the input array is itself used as the buffer. + args: + additional arguments passed to Graphic + kwargs: + additional keyword arguments passed to Graphic + + + Features + -------- + + **data**: :class:`.HeatmapDataFeature` + Manages the data buffer displayed in the HeatmapGraphic + + **cmap**: :class:`.HeatmapCmapFeature` + Manages the colormap + + **present**: :class:`.PresentFeature` + Control the presence of the Graphic in the scene + + + Examples + -------- + .. code-block:: python + + from fastplotlib import Plot + # create a `Plot` instance + plot = Plot() + + # make some random 2D heatmap data + data = np.random.rand(10_000, 8_000) + + # add a heatmap + plot.add_heatmap(data=data) + + # show the plot + plot.show() + + """ + g = HeatmapGraphic(*args, **kwargs) + self.add_graphic(g) + + def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, + thickness: Union[float, List[float]] = 2.0, + colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', alpha: float = 1.0, + cmap: Union[List[str], str] = None, name: str = None, + metadata: Union[list, tuple, numpy.ndarray] = None, *args, **kwargs): + """ + + Create a Line Collection + + Parameters + ---------- + + data: list of array-like or array + List of line data to plot, each element must be a 1D, 2D, or 3D numpy array + if elements are 2D, interpreted as [y_vals, n_lines] + + z_position: list of float or float, optional + | if ``float``, single position will be used for all lines + | if ``list`` of ``float``, each value will apply to the individual lines + + thickness: float or list of float, default 2.0 + | if ``float``, single thickness will be used for all lines + | if ``list`` of ``float``, each value will apply to the individual lines + + colors: str, RGBA array, list of RGBA array, or list of str, default "w" + | if single ``str`` such as "w", "r", "b", etc, represents a single color for all lines + | if single ``RGBA array`` (tuple or list of size 4), represents a single color for all lines + | if ``list`` of ``str``, represents color for each individual line, example ["w", "b", "r",...] + | if ``RGBA array`` of shape [data_size, 4], represents a single RGBA array for each line + + cmap: list of str or str, optional + | if ``str``, single cmap will be used for all lines + | if ``list`` of ``str``, each cmap will apply to the individual lines + **Note:** ``cmap`` overrides any arguments passed to ``colors`` + + name: str, optional + name of the line collection + + metadata: list, tuple, or array + metadata associated with this collection, this is for the user to manage. + ``len(metadata)`` must be same as ``len(data)`` + + args + passed to GraphicCollection + + kwargs + passed to GraphicCollection + + + Features + -------- + + Collections support the same features as the underlying graphic. You just have to slice the selection. + + .. code-block:: python + + # slice only the collection + line_collection[10:20].colors = "blue" + + # slice the collection and a feature + line_collection[20:30].colors[10:30] = "red" + + # the data feature also works like this + + See :class:`LineGraphic` details on the features. + + + Examples + -------- + .. code-block:: python + + from fastplotlib import Plot + from fastplotlib.graphics import LineCollection + + # creating data for sine and cosine waves + xs = np.linspace(-10, 10, 100) + ys = np.sin(xs) + + sine = np.dstack([xs, ys])[0] + + ys = np.sin(xs) + 10 + sine2 = np.dstack([xs, ys])[0] + + ys = np.cos(xs) + 5 + cosine = np.dstack([xs, ys])[0] + + # creating plot + plot = Plot() + + # creating a line collection using the sine and cosine wave data + line_collection = LineCollection(data=[sine, cosine, sine2], cmap=["Oranges", "Blues", "Reds"], thickness=20.0) + + # add graphic to plot + plot.add_graphic(line_collection) + + # show plot + plot.show() + + # change the color of the sine wave to white + line_collection[0].colors = "w" + + # change certain color indexes of the cosine data to red + line_collection[1].colors[0:15] = "r" + + # toggle presence of sine2 and rescale graphics + line_collection[2].present = False + + plot.autoscale() + + line_collection[2].present = True + + plot.autoscale() + + # can also do slicing + line_collection[1:].colors[35:70] = "magenta" + + + """ + g = LineCollection(*args, **kwargs) + self.add_graphic(g) + + def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, + thickness: Union[float, List[float]] = 2.0, + colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', cmap: Union[List[str], str] = None, + separation: float = 10, separation_axis: str = 'y', name: str = None, *args, **kwargs): + """ + + Create a line stack + + Parameters + ---------- + data: list of array-like + List of line data to plot, each element must be a 1D, 2D, or 3D numpy array + if elements are 2D, interpreted as [y_vals, n_lines] + + z_position: list of float or float, optional + | if ``float``, single position will be used for all lines + | if ``list`` of ``float``, each value will apply to individual lines + + thickness: float or list of float, default 2.0 + | if ``float``, single thickness will be used for all lines + | if ``list`` of ``float``, each value will apply to the individual lines + + colors: str, RGBA array, list of RGBA array, or list of str, default "w" + | if single ``str`` such as "w", "r", "b", etc, represents a single color for all lines + | if single ``RGBA array`` (tuple or list of size 4), represents a single color for all lines + | is ``list`` of ``str``, represents color for each individual line, example ["w", "b", "r",...] + | if ``list`` of ``RGBA array`` of shape [data_size, 4], represents a single RGBA array for each line + + cmap: list of str or str, optional + | if ``str``, single cmap will be used for all lines + | if ``list`` of ``str``, each cmap will apply to the individual lines + **Note:** ``cmap`` overrides any arguments passed to ``colors`` + + name: str, optional + name of the line stack + + separation: float, default 10 + space in between each line graphic in the stack + + separation_axis: str, default "y" + axis in which the line graphics in the stack should be separated + + name: str, optional + name of the line stack + + args + passed to LineCollection + + kwargs + passed to LineCollection + + + Features + -------- + + Collections support the same features as the underlying graphic. You just have to slice the selection. + + .. code-block:: python + + # slice only the collection + line_collection[10:20].colors = "blue" + + # slice the collection and a feature + line_collection[20:30].colors[10:30] = "red" + + # the data feature also works like this + + See :class:`LineGraphic` details on the features. + + + Examples + -------- + .. code-block:: python + + from fastplotlib import Plot + from fastplotlib.graphics import LineStack + + # create plot + plot = Plot() + + # create line data + xs = np.linspace(-10, 10, 100) + ys = np.sin(xs) + + sine = np.dstack([xs, ys])[0] + + ys = np.sin(xs) + cosine = np.dstack([xs, ys])[0] + + # create line stack + line_stack = LineStack(data=[sine, cosine], cmap=["Oranges", "Blues"], thickness=20.0, separation=5.0) + + # add graphic to plot + plot.add_graphic(line_stack) + + # show plot + plot.show() + + # change the color of the sine wave to white + line_stack[0].colors = "w" + + # change certain color indexes of the cosine data to red + line_stack[1].colors[0:15] = "r" + + # more slicing + line_stack[0].colors[35:70] = "magenta" + + + """ + g = LineStack(*args, **kwargs) + self.add_graphic(g) + + def add_text(self, text: str, position: Tuple[int] = (0, 0, 0), size: int = 10, + face_color: Union[str, numpy.ndarray] = 'w', outline_color: Union[str, numpy.ndarray] = 'w', + outline_thickness=0, name: str = None): + """ + + Create a text Graphic + + Parameters + ---------- + text: str + display text + position: int tuple, default (0, 0, 0) + int tuple indicating location of text in scene + size: int, default 10 + text size + face_color: str or array, default "w" + str or RGBA array to set the color of the text + outline_color: str or array, default "w" + str or RGBA array to set the outline color of the text + outline_thickness: int, default 0 + text outline thickness + name: str, optional + name of graphic, passed to Graphic + + """ + g = TextGraphic(*args, **kwargs) + self.add_graphic(g) diff --git a/fastplotlib/utils/generate_add_methods.py b/fastplotlib/utils/generate_add_methods.py new file mode 100644 index 000000000..8078f00db --- /dev/null +++ b/fastplotlib/utils/generate_add_methods.py @@ -0,0 +1,38 @@ +import inspect +import fastplotlib +from fastplotlib.graphics import * +import sys + +modules = dict(inspect.getmembers(fastplotlib.graphics))['__all__'] + + +def generate_add_graphics_methods(): + # clear file and regenerate from scratch + open('add_graphics.py', 'w').close() + + sys.stdout = open('add_graphics.py', 'w') + + print('from typing import *\n') + print('import numpy\n') + print('from fastplotlib.graphics import *\n') + + print("\nclass GraphicMethods:") + print("\tdef __init__(self):") + print("\t\tpass\n") + + for m in modules: + class_name = getattr(sys.modules[__name__], m) + method_name = class_name.type + + print(f"\tdef add_{method_name}{inspect.signature(class_name.__init__)}:") + print('\t\t"""') + print(f'\t{class_name.__init__.__doc__}') + print('\t\t"""') + print(f"\t\tg = {class_name.__name__}(*args, **kwargs)") + print(f'\t\tself.add_graphic(g)\n') + + sys.stdout.close() + + +if __name__ == '__main__': + generate_add_graphics_methods() From c5ccb569c2659e0c8ec3a5dd84ccd6b09cd3e417 Mon Sep 17 00:00:00 2001 From: Caitlin Lewis Date: Wed, 21 Jun 2023 08:48:59 -0400 Subject: [PATCH 02/12] generating the class and methods seems to work fine until I try to import the class elsewhere, graphic names not being defined --- fastplotlib/layouts/_base.py | 5 +- fastplotlib/layouts/_subplot.py | 4 +- fastplotlib/utils/__init__.py | 2 +- ...{add_graphics.py => _add_graphic_mixin.py} | 353 +++++++++--------- fastplotlib/utils/generate_add_methods.py | 46 ++- 5 files changed, 208 insertions(+), 202 deletions(-) rename fastplotlib/utils/{add_graphics.py => _add_graphic_mixin.py} (83%) diff --git a/fastplotlib/layouts/_base.py b/fastplotlib/layouts/_base.py index 688969504..b9943534c 100644 --- a/fastplotlib/layouts/_base.py +++ b/fastplotlib/layouts/_base.py @@ -12,8 +12,6 @@ from ..graphics._base import Graphic, GraphicCollection from ..graphics.selectors._base_selector import BaseSelector -from ..utils import GraphicMethods - # dict to store Graphic instances # this is the only place where the real references to Graphics are stored in a Python session # {hex id str: Graphic} @@ -21,7 +19,7 @@ SELECTORS: Dict[str, BaseSelector] = dict() -class PlotArea(GraphicMethods): +class PlotArea: def __init__( self, parent, @@ -59,7 +57,6 @@ def __init__( name: str, optional name of ``subplot`` or ``plot`` subclass being instantiated """ - super(GraphicMethods, self).__init__() self._parent: PlotArea = parent self._position = position diff --git a/fastplotlib/layouts/_subplot.py b/fastplotlib/layouts/_subplot.py index 3c2543e38..454efcdd9 100644 --- a/fastplotlib/layouts/_subplot.py +++ b/fastplotlib/layouts/_subplot.py @@ -38,9 +38,9 @@ from ._utils import make_canvas_and_renderer from ._base import PlotArea -from .. import graphics from ..graphics import TextGraphic from ._defaults import create_camera, create_controller +# from ..utils import GraphicMethodsMixin class Subplot(PlotArea): @@ -81,6 +81,8 @@ def __init__( name of the subplot, will appear as ``TextGraphic`` above the subplot """ + # super(GraphicMethodsMixin, self).__init__() + canvas, renderer = make_canvas_and_renderer(canvas, renderer) if position is None: diff --git a/fastplotlib/utils/__init__.py b/fastplotlib/utils/__init__.py index 67d6b6356..fd2d95537 100644 --- a/fastplotlib/utils/__init__.py +++ b/fastplotlib/utils/__init__.py @@ -1,2 +1,2 @@ from .functions import * -from .add_graphics import GraphicMethods +#from ._add_graphic_mixin import GraphicMethodsMixin diff --git a/fastplotlib/utils/add_graphics.py b/fastplotlib/utils/_add_graphic_mixin.py similarity index 83% rename from fastplotlib/utils/add_graphics.py rename to fastplotlib/utils/_add_graphic_mixin.py index bffcaddc0..10d903e5f 100644 --- a/fastplotlib/utils/add_graphics.py +++ b/fastplotlib/utils/_add_graphic_mixin.py @@ -1,33 +1,34 @@ from typing import * - import numpy - -from fastplotlib.graphics import * +from ..graphics import * +import weakref -class GraphicMethods: - def __init__(self): - pass +class GraphicMethodsMixin: + def __init__(self): + pass - def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', - isolated_buffer: bool = True, *args, **kwargs): - """ - Create an Image Graphic + def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', chunk_size: int = 8192, isolated_buffer: bool = True, *args, **kwargs) -> weakref.proxy(HeatmapGraphic): + """ + + Create an Image Graphic Parameters ---------- data: array-like array-like, usually numpy.ndarray, must support ``memoryview()`` Tensorflow Tensors also work **probably**, but not thoroughly tested - | shape must be ``[x_dim, y_dim]`` or ``[x_dim, y_dim, rgb]`` + | shape must be ``[x_dim, y_dim]`` vmin: int, optional minimum value for color scaling, calculated from data if not provided vmax: int, optional maximum value for color scaling, calculated from data if not provided cmap: str, optional, default "plasma" - colormap to use to display the image data, ignored if data is RGB + colormap to use to display the data filter: str, optional, default "nearest" interpolation filter, one of "nearest" or "linear" + chunk_size: int, default 8192, max 8192 + chunk size for each tile used to make up the heatmap texture isolated_buffer: bool, default True If True, initialize a buffer with the same shape as the input data and then set the data, useful if the data arrays are ready-only such as memmaps. @@ -37,20 +38,20 @@ def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = ' kwargs: additional keyword arguments passed to Graphic + Features -------- - **data**: :class:`.ImageDataFeature` - Manages the data buffer displayed in the ImageGraphic + **data**: :class:`.HeatmapDataFeature` + Manages the data buffer displayed in the HeatmapGraphic - **cmap**: :class:`.ImageCmapFeature` + **cmap**: :class:`.HeatmapCmapFeature` Manages the colormap **present**: :class:`.PresentFeature` Control the presence of the Graphic in the scene - Examples -------- .. code-block:: python @@ -58,128 +59,24 @@ def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = ' from fastplotlib import Plot # create a `Plot` instance plot = Plot() - # make some random 2D image data - data = np.random.rand(512, 512) - # plot the image data - plot.add_image(data=data) + + # make some random 2D heatmap data + data = np.random.rand(10_000, 8_000) + + # add a heatmap + plot.add_heatmap(data=data) + # show the plot plot.show() """ - g = ImageGraphic(*args, **kwargs) - self.add_graphic(g) - - def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list] = 1, colors: numpy.ndarray = 'w', - alpha: float = 1.0, cmap: str = None, z_position: float = 0.0, *args, **kwargs): - """ - - Create a Scatter Graphic, 2d or 3d - - Parameters - ---------- - data: array-like - Scatter data to plot, 2D must be of shape [n_points, 2], 3D must be of shape [n_points, 3] - - sizes: float or iterable of float, optional, default 1.0 - size of the scatter points - - colors: str, array, or iterable, default "w" - specify colors as a single human readable string, a single RGBA array, - or an iterable of strings or RGBA arrays - - cmap: str, optional - apply a colormap to the scatter instead of assigning colors manually, this - overrides any argument passed to "colors" - - alpha: float, optional, default 1.0 - alpha value for the colors - - z_position: float, optional - z-axis position for placing the graphic - - args - passed to Graphic - - kwargs - passed to Graphic - - Features - -------- - - **data**: :class:`.ImageDataFeature` - Manages the scatter [x, y, z] positions data buffer, allows regular and fancy indexing. - ex: ``scatter.data[:, 0] = 5```, ``scatter.data[xs > 5] = 3`` + g = HeatmapGraphic(*args, **kwargs) + self.add_graphic(g) - **colors**: :class:`.ColorFeature` - Manages the color buffer, allows regular and fancy indexing. - ex: ``scatter.data[:, 1] = 0.5``, ``scatter.colors[xs > 5] = "cyan"`` - - **present**: :class:`.PresentFeature` - Control the presence of the Graphic in the scene, set to ``True`` or ``False`` - - - """ - g = ScatterGraphic(*args, **kwargs) - self.add_graphic(g) - - def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.ndarray, Iterable] = 'w', - alpha: float = 1.0, cmap: str = None, z_position: float = None, collection_index: int = None, *args, - **kwargs): - """ - - Create a line Graphic, 2d or 3d - - Parameters - ---------- - data: array-like - Line data to plot, 2D must be of shape [n_points, 2], 3D must be of shape [n_points, 3] - - thickness: float, optional, default 2.0 - thickness of the line - - colors: str, array, or iterable, default "w" - specify colors as a single human-readable string, a single RGBA array, - or an iterable of strings or RGBA arrays - - cmap: str, optional - apply a colormap to the line instead of assigning colors manually, this - overrides any argument passed to "colors" - - alpha: float, optional, default 1.0 - alpha value for the colors + return weakref.proxy(g) - z_position: float, optional - z-axis position for placing the graphic - - args - passed to Graphic - - kwargs - passed to Graphic - - Features - -------- - - **data**: :class:`.ImageDataFeature` - Manages the line [x, y, z] positions data buffer, allows regular and fancy indexing. - ex: ``scatter.data[:, 0] = 5```, ``scatter.data[xs > 5] = 3`` - - **colors**: :class:`.ColorFeature` - Manages the color buffer, allows regular and fancy indexing. - ex: ``scatter.data[:, 1] = 0.5``, ``scatter.colors[xs > 5] = "cyan"`` - - **present**: :class:`.PresentFeature` - Control the presence of the Graphic in the scene, set to ``True`` or ``False`` - - + def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'auto', pre_computed: Dict[str, numpy.ndarray] = None, colors: numpy.ndarray = 'w', draw_scale_factor: float = 100.0, draw_bin_width_scale: float = 1.0, **kwargs) -> weakref.proxy(HistogramGraphic): """ - g = LineGraphic(*args, **kwargs) - self.add_graphic(g) - - def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'auto', - pre_computed: Dict[str, numpy.ndarray] = None, colors: numpy.ndarray = 'w', - draw_scale_factor: float = 100.0, draw_bin_width_scale: float = 1.0, **kwargs): - """ Create a Histogram Graphic @@ -206,12 +103,13 @@ def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'aut passed to Graphic """ - g = HistogramGraphic(*args, **kwargs) - self.add_graphic(g) + g = HistogramGraphic(*args, **kwargs) + self.add_graphic(g) + + return weakref.proxy(g) - def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', - chunk_size: int = 8192, isolated_buffer: bool = True, *args, **kwargs): - """ + def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', isolated_buffer: bool = True, *args, **kwargs) -> weakref.proxy(ImageGraphic): + """ Create an Image Graphic @@ -220,17 +118,15 @@ def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = data: array-like array-like, usually numpy.ndarray, must support ``memoryview()`` Tensorflow Tensors also work **probably**, but not thoroughly tested - | shape must be ``[x_dim, y_dim]`` + | shape must be ``[x_dim, y_dim]`` or ``[x_dim, y_dim, rgb]`` vmin: int, optional minimum value for color scaling, calculated from data if not provided vmax: int, optional maximum value for color scaling, calculated from data if not provided cmap: str, optional, default "plasma" - colormap to use to display the data + colormap to use to display the image data, ignored if data is RGB filter: str, optional, default "nearest" interpolation filter, one of "nearest" or "linear" - chunk_size: int, default 8192, max 8192 - chunk size for each tile used to make up the heatmap texture isolated_buffer: bool, default True If True, initialize a buffer with the same shape as the input data and then set the data, useful if the data arrays are ready-only such as memmaps. @@ -240,20 +136,20 @@ def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = kwargs: additional keyword arguments passed to Graphic - Features -------- - **data**: :class:`.HeatmapDataFeature` - Manages the data buffer displayed in the HeatmapGraphic + **data**: :class:`.ImageDataFeature` + Manages the data buffer displayed in the ImageGraphic - **cmap**: :class:`.HeatmapCmapFeature` + **cmap**: :class:`.ImageCmapFeature` Manages the colormap **present**: :class:`.PresentFeature` Control the presence of the Graphic in the scene + Examples -------- .. code-block:: python @@ -261,26 +157,21 @@ def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = from fastplotlib import Plot # create a `Plot` instance plot = Plot() - - # make some random 2D heatmap data - data = np.random.rand(10_000, 8_000) - - # add a heatmap - plot.add_heatmap(data=data) - + # make some random 2D image data + data = np.random.rand(512, 512) + # plot the image data + plot.add_image(data=data) # show the plot plot.show() """ - g = HeatmapGraphic(*args, **kwargs) - self.add_graphic(g) - - def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, - thickness: Union[float, List[float]] = 2.0, - colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', alpha: float = 1.0, - cmap: Union[List[str], str] = None, name: str = None, - metadata: Union[list, tuple, numpy.ndarray] = None, *args, **kwargs): - """ + g = ImageGraphic(*args, **kwargs) + self.add_graphic(g) + + return weakref.proxy(g) + + def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', alpha: float = 1.0, cmap: Union[List[str], str] = None, name: str = None, metadata: Union[list, tuple, numpy.ndarray] = None, *args, **kwargs) -> weakref.proxy(LineCollection): + """ Create a Line Collection @@ -393,14 +284,67 @@ def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[ """ - g = LineCollection(*args, **kwargs) - self.add_graphic(g) - - def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, - thickness: Union[float, List[float]] = 2.0, - colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', cmap: Union[List[str], str] = None, - separation: float = 10, separation_axis: str = 'y', name: str = None, *args, **kwargs): - """ + g = LineCollection(*args, **kwargs) + self.add_graphic(g) + + return weakref.proxy(g) + + def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.ndarray, Iterable] = 'w', alpha: float = 1.0, cmap: str = None, z_position: float = None, collection_index: int = None, *args, **kwargs) -> weakref.proxy(LineGraphic): + """ + + Create a line Graphic, 2d or 3d + + Parameters + ---------- + data: array-like + Line data to plot, 2D must be of shape [n_points, 2], 3D must be of shape [n_points, 3] + + thickness: float, optional, default 2.0 + thickness of the line + + colors: str, array, or iterable, default "w" + specify colors as a single human-readable string, a single RGBA array, + or an iterable of strings or RGBA arrays + + cmap: str, optional + apply a colormap to the line instead of assigning colors manually, this + overrides any argument passed to "colors" + + alpha: float, optional, default 1.0 + alpha value for the colors + + z_position: float, optional + z-axis position for placing the graphic + + args + passed to Graphic + + kwargs + passed to Graphic + + Features + -------- + + **data**: :class:`.ImageDataFeature` + Manages the line [x, y, z] positions data buffer, allows regular and fancy indexing. + ex: ``scatter.data[:, 0] = 5```, ``scatter.data[xs > 5] = 3`` + + **colors**: :class:`.ColorFeature` + Manages the color buffer, allows regular and fancy indexing. + ex: ``scatter.data[:, 1] = 0.5``, ``scatter.colors[xs > 5] = "cyan"`` + + **present**: :class:`.PresentFeature` + Control the presence of the Graphic in the scene, set to ``True`` or ``False`` + + + """ + g = LineGraphic(*args, **kwargs) + self.add_graphic(g) + + return weakref.proxy(g) + + def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', cmap: Union[List[str], str] = None, separation: float = 10, separation_axis: str = 'y', name: str = None, *args, **kwargs) -> weakref.proxy(LineStack): + """ Create a line stack @@ -505,13 +449,67 @@ def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float """ - g = LineStack(*args, **kwargs) - self.add_graphic(g) + g = LineStack(*args, **kwargs) + self.add_graphic(g) - def add_text(self, text: str, position: Tuple[int] = (0, 0, 0), size: int = 10, - face_color: Union[str, numpy.ndarray] = 'w', outline_color: Union[str, numpy.ndarray] = 'w', - outline_thickness=0, name: str = None): - """ + return weakref.proxy(g) + + def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list] = 1, colors: numpy.ndarray = 'w', alpha: float = 1.0, cmap: str = None, z_position: float = 0.0, *args, **kwargs) -> weakref.proxy(ScatterGraphic): + """ + + Create a Scatter Graphic, 2d or 3d + + Parameters + ---------- + data: array-like + Scatter data to plot, 2D must be of shape [n_points, 2], 3D must be of shape [n_points, 3] + + sizes: float or iterable of float, optional, default 1.0 + size of the scatter points + + colors: str, array, or iterable, default "w" + specify colors as a single human readable string, a single RGBA array, + or an iterable of strings or RGBA arrays + + cmap: str, optional + apply a colormap to the scatter instead of assigning colors manually, this + overrides any argument passed to "colors" + + alpha: float, optional, default 1.0 + alpha value for the colors + + z_position: float, optional + z-axis position for placing the graphic + + args + passed to Graphic + + kwargs + passed to Graphic + + Features + -------- + + **data**: :class:`.ImageDataFeature` + Manages the scatter [x, y, z] positions data buffer, allows regular and fancy indexing. + ex: ``scatter.data[:, 0] = 5```, ``scatter.data[xs > 5] = 3`` + + **colors**: :class:`.ColorFeature` + Manages the color buffer, allows regular and fancy indexing. + ex: ``scatter.data[:, 1] = 0.5``, ``scatter.colors[xs > 5] = "cyan"`` + + **present**: :class:`.PresentFeature` + Control the presence of the Graphic in the scene, set to ``True`` or ``False`` + + + """ + g = ScatterGraphic(*args, **kwargs) + self.add_graphic(g) + + return weakref.proxy(g) + + def add_text(self, text: str, position: Tuple[int] = (0, 0, 0), size: int = 10, face_color: Union[str, numpy.ndarray] = 'w', outline_color: Union[str, numpy.ndarray] = 'w', outline_thickness=0, name: str = None) -> weakref.proxy(TextGraphic): + """ Create a text Graphic @@ -533,5 +531,8 @@ def add_text(self, text: str, position: Tuple[int] = (0, 0, 0), size: int = 10, name of graphic, passed to Graphic """ - g = TextGraphic(*args, **kwargs) - self.add_graphic(g) + g = TextGraphic(*args, **kwargs) + self.add_graphic(g) + + return weakref.proxy(g) + diff --git a/fastplotlib/utils/generate_add_methods.py b/fastplotlib/utils/generate_add_methods.py index 8078f00db..be45c4235 100644 --- a/fastplotlib/utils/generate_add_methods.py +++ b/fastplotlib/utils/generate_add_methods.py @@ -1,37 +1,43 @@ import inspect -import fastplotlib -from fastplotlib.graphics import * import sys +from fastplotlib.graphics import * +modules = list() -modules = dict(inspect.getmembers(fastplotlib.graphics))['__all__'] - +for name, obj in inspect.getmembers(sys.modules[__name__]): + if inspect.isclass(obj): + modules.append(obj) def generate_add_graphics_methods(): # clear file and regenerate from scratch - open('add_graphics.py', 'w').close() + open('_add_graphic_mixin.py', 'w').close() - sys.stdout = open('add_graphics.py', 'w') + f = open('_add_graphic_mixin.py', 'w') - print('from typing import *\n') - print('import numpy\n') - print('from fastplotlib.graphics import *\n') + f.write('from typing import *\n') + f.write('import numpy\n') + f.write('from ..graphics import *\n') + f.write('import weakref\n\n') - print("\nclass GraphicMethods:") - print("\tdef __init__(self):") - print("\t\tpass\n") + f.write("\nclass GraphicMethodsMixin:\n") + f.write("\tdef __init__(self):\n") + f.write("\t\tpass\n\n") for m in modules: - class_name = getattr(sys.modules[__name__], m) + class_name = m method_name = class_name.type - print(f"\tdef add_{method_name}{inspect.signature(class_name.__init__)}:") - print('\t\t"""') - print(f'\t{class_name.__init__.__doc__}') - print('\t\t"""') - print(f"\t\tg = {class_name.__name__}(*args, **kwargs)") - print(f'\t\tself.add_graphic(g)\n') + f.write(f"\tdef add_{method_name}{inspect.signature(class_name.__init__)} -> weakref.proxy({class_name.__name__}):\n") + f.write('\t\t"""\n') + f.write(f'\t{class_name.__init__.__doc__}\n') + f.write('\t\t"""\n') + f.write(f"\t\tg = {class_name.__name__}(*args, **kwargs)\n") + f.write(f'\t\tself.add_graphic(g)\n\n') + + f.write(f'\t\treturn weakref.proxy(g)\n\n') + + f.close() - sys.stdout.close() + return if __name__ == '__main__': From 34d93e9a6e63e0a31e7215ec20ab4e464c79d85c Mon Sep 17 00:00:00 2001 From: Caitlin Lewis Date: Wed, 21 Jun 2023 10:10:40 -0400 Subject: [PATCH 03/12] moving mixin class to layouts, adding mixin to subplot --- fastplotlib/{utils => layouts}/_add_graphic_mixin.py | 0 fastplotlib/layouts/_subplot.py | 6 +++--- fastplotlib/utils/__init__.py | 1 - fastplotlib/utils/generate_add_methods.py | 5 +++-- 4 files changed, 6 insertions(+), 6 deletions(-) rename fastplotlib/{utils => layouts}/_add_graphic_mixin.py (100%) diff --git a/fastplotlib/utils/_add_graphic_mixin.py b/fastplotlib/layouts/_add_graphic_mixin.py similarity index 100% rename from fastplotlib/utils/_add_graphic_mixin.py rename to fastplotlib/layouts/_add_graphic_mixin.py diff --git a/fastplotlib/layouts/_subplot.py b/fastplotlib/layouts/_subplot.py index 454efcdd9..6989069dd 100644 --- a/fastplotlib/layouts/_subplot.py +++ b/fastplotlib/layouts/_subplot.py @@ -40,10 +40,10 @@ from ._base import PlotArea from ..graphics import TextGraphic from ._defaults import create_camera, create_controller -# from ..utils import GraphicMethodsMixin +from ._add_graphic_mixin import GraphicMethodsMixin -class Subplot(PlotArea): +class Subplot(PlotArea, GraphicMethodsMixin): def __init__( self, position: Tuple[int, int] = None, @@ -81,7 +81,7 @@ def __init__( name of the subplot, will appear as ``TextGraphic`` above the subplot """ - # super(GraphicMethodsMixin, self).__init__() + super(GraphicMethodsMixin, self).__init__() canvas, renderer = make_canvas_and_renderer(canvas, renderer) diff --git a/fastplotlib/utils/__init__.py b/fastplotlib/utils/__init__.py index fd2d95537..c8f754883 100644 --- a/fastplotlib/utils/__init__.py +++ b/fastplotlib/utils/__init__.py @@ -1,2 +1 @@ from .functions import * -#from ._add_graphic_mixin import GraphicMethodsMixin diff --git a/fastplotlib/utils/generate_add_methods.py b/fastplotlib/utils/generate_add_methods.py index be45c4235..0a1ca0ff7 100644 --- a/fastplotlib/utils/generate_add_methods.py +++ b/fastplotlib/utils/generate_add_methods.py @@ -1,6 +1,7 @@ import inspect import sys from fastplotlib.graphics import * +#from ..graphics import * modules = list() for name, obj in inspect.getmembers(sys.modules[__name__]): @@ -9,9 +10,9 @@ def generate_add_graphics_methods(): # clear file and regenerate from scratch - open('_add_graphic_mixin.py', 'w').close() + open('../layouts/_add_graphic_mixin.py', 'w').close() - f = open('_add_graphic_mixin.py', 'w') + f = open('../layouts/_add_graphic_mixin.py', 'w') f.write('from typing import *\n') f.write('import numpy\n') From 2da635f1eb6cb637035fe30279abf50b4bb665f2 Mon Sep 17 00:00:00 2001 From: Caitlin Lewis Date: Thu, 22 Jun 2023 09:59:54 -0400 Subject: [PATCH 04/12] requested changes, methods still not working --- fastplotlib/layouts/_subplot.py | 13 +- ...phic_mixin.py => graphic_methods_mixin.py} | 118 +++++++++--------- fastplotlib/utils/generate_add_methods.py | 27 ++-- 3 files changed, 76 insertions(+), 82 deletions(-) rename fastplotlib/layouts/{_add_graphic_mixin.py => graphic_methods_mixin.py} (82%) diff --git a/fastplotlib/layouts/_subplot.py b/fastplotlib/layouts/_subplot.py index 6989069dd..10c518a3a 100644 --- a/fastplotlib/layouts/_subplot.py +++ b/fastplotlib/layouts/_subplot.py @@ -40,7 +40,7 @@ from ._base import PlotArea from ..graphics import TextGraphic from ._defaults import create_camera, create_controller -from ._add_graphic_mixin import GraphicMethodsMixin +from .graphic_methods_mixin import GraphicMethodsMixin class Subplot(PlotArea, GraphicMethodsMixin): @@ -126,17 +126,6 @@ def __init__( self.docked_viewports[pos] = dv self.children.append(dv) - # # attach all the add_ methods - # for graphic_cls_name in graphics.__all__: - # cls = getattr(graphics, graphic_cls_name) - # - # pfunc = partial(self._create_graphic, cls) - # pfunc.__signature__ = signature(cls) - # pfunc.__doc__ = cls.__init__.__doc__ - # - # # cls.type is defined in Graphic.__init_subclass__ - # setattr(self, f"add_{cls.type}", pfunc) - self._title_graphic: TextGraphic = None if self.name is not None: self.set_title(self.name) diff --git a/fastplotlib/layouts/_add_graphic_mixin.py b/fastplotlib/layouts/graphic_methods_mixin.py similarity index 82% rename from fastplotlib/layouts/_add_graphic_mixin.py rename to fastplotlib/layouts/graphic_methods_mixin.py index 10d903e5f..65486df8a 100644 --- a/fastplotlib/layouts/_add_graphic_mixin.py +++ b/fastplotlib/layouts/graphic_methods_mixin.py @@ -5,12 +5,12 @@ class GraphicMethodsMixin: - def __init__(self): - pass + def __init__(self): + pass - def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', chunk_size: int = 8192, isolated_buffer: bool = True, *args, **kwargs) -> weakref.proxy(HeatmapGraphic): - """ - + def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', chunk_size: int = 8192, isolated_buffer: bool = True, *args, **kwargs) -> weakref.proxy(HeatmapGraphic): + """ + Create an Image Graphic Parameters @@ -69,15 +69,15 @@ def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = # show the plot plot.show() - """ - g = HeatmapGraphic(*args, **kwargs) - self.add_graphic(g) + """ + g = HeatmapGraphic(*args, **kwargs) + self.add_graphic(g) - return weakref.proxy(g) + return weakref.proxy(g) - def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'auto', pre_computed: Dict[str, numpy.ndarray] = None, colors: numpy.ndarray = 'w', draw_scale_factor: float = 100.0, draw_bin_width_scale: float = 1.0, **kwargs) -> weakref.proxy(HistogramGraphic): - """ - + def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'auto', pre_computed: Dict[str, numpy.ndarray] = None, colors: numpy.ndarray = 'w', draw_scale_factor: float = 100.0, draw_bin_width_scale: float = 1.0, **kwargs) -> weakref.proxy(HistogramGraphic): + """ + Create a Histogram Graphic Parameters @@ -102,15 +102,15 @@ def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'aut kwargs passed to Graphic - """ - g = HistogramGraphic(*args, **kwargs) - self.add_graphic(g) + """ + g = HistogramGraphic(*args, **kwargs) + self.add_graphic(g) - return weakref.proxy(g) + return weakref.proxy(g) - def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', isolated_buffer: bool = True, *args, **kwargs) -> weakref.proxy(ImageGraphic): - """ - + def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', isolated_buffer: bool = True, *args, **kwargs) -> weakref.proxy(ImageGraphic): + """ + Create an Image Graphic Parameters @@ -164,15 +164,17 @@ def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = ' # show the plot plot.show() - """ - g = ImageGraphic(*args, **kwargs) - self.add_graphic(g) + """ + print(args) + print(kwargs) + g = ImageGraphic(*args, **kwargs) + self.add_graphic(g) - return weakref.proxy(g) + return weakref.proxy(g) - def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', alpha: float = 1.0, cmap: Union[List[str], str] = None, name: str = None, metadata: Union[list, tuple, numpy.ndarray] = None, *args, **kwargs) -> weakref.proxy(LineCollection): - """ - + def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', alpha: float = 1.0, cmap: Union[List[str], str] = None, name: str = None, metadata: Union[list, tuple, numpy.ndarray] = None, *args, **kwargs) -> weakref.proxy(LineCollection): + """ + Create a Line Collection Parameters @@ -283,15 +285,15 @@ def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[ line_collection[1:].colors[35:70] = "magenta" - """ - g = LineCollection(*args, **kwargs) - self.add_graphic(g) + """ + g = LineCollection(*args, **kwargs) + self.add_graphic(g) - return weakref.proxy(g) + return weakref.proxy(g) - def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.ndarray, Iterable] = 'w', alpha: float = 1.0, cmap: str = None, z_position: float = None, collection_index: int = None, *args, **kwargs) -> weakref.proxy(LineGraphic): - """ - + def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.ndarray, Iterable] = 'w', alpha: float = 1.0, cmap: str = None, z_position: float = None, collection_index: int = None, *args, **kwargs) -> weakref.proxy(LineGraphic): + """ + Create a line Graphic, 2d or 3d Parameters @@ -337,15 +339,15 @@ def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.n Control the presence of the Graphic in the scene, set to ``True`` or ``False`` - """ - g = LineGraphic(*args, **kwargs) - self.add_graphic(g) + """ + g = LineGraphic(*args, **kwargs) + self.add_graphic(g) - return weakref.proxy(g) + return weakref.proxy(g) - def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', cmap: Union[List[str], str] = None, separation: float = 10, separation_axis: str = 'y', name: str = None, *args, **kwargs) -> weakref.proxy(LineStack): - """ - + def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', cmap: Union[List[str], str] = None, separation: float = 10, separation_axis: str = 'y', name: str = None, *args, **kwargs) -> weakref.proxy(LineStack): + """ + Create a line stack Parameters @@ -448,15 +450,15 @@ def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float line_stack[0].colors[35:70] = "magenta" - """ - g = LineStack(*args, **kwargs) - self.add_graphic(g) + """ + g = LineStack(*args, **kwargs) + self.add_graphic(g) - return weakref.proxy(g) + return weakref.proxy(g) - def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list] = 1, colors: numpy.ndarray = 'w', alpha: float = 1.0, cmap: str = None, z_position: float = 0.0, *args, **kwargs) -> weakref.proxy(ScatterGraphic): - """ - + def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list] = 1, colors: numpy.ndarray = 'w', alpha: float = 1.0, cmap: str = None, z_position: float = 0.0, *args, **kwargs) -> weakref.proxy(ScatterGraphic): + """ + Create a Scatter Graphic, 2d or 3d Parameters @@ -502,15 +504,15 @@ def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list Control the presence of the Graphic in the scene, set to ``True`` or ``False`` - """ - g = ScatterGraphic(*args, **kwargs) - self.add_graphic(g) + """ + g = ScatterGraphic(*args, **kwargs) + self.add_graphic(g) - return weakref.proxy(g) + return weakref.proxy(g) - def add_text(self, text: str, position: Tuple[int] = (0, 0, 0), size: int = 10, face_color: Union[str, numpy.ndarray] = 'w', outline_color: Union[str, numpy.ndarray] = 'w', outline_thickness=0, name: str = None) -> weakref.proxy(TextGraphic): - """ - + def add_text(self, text: str, position: Tuple[int] = (0, 0, 0), size: int = 10, face_color: Union[str, numpy.ndarray] = 'w', outline_color: Union[str, numpy.ndarray] = 'w', outline_thickness=0, name: str = None) -> weakref.proxy(TextGraphic): + """ + Create a text Graphic Parameters @@ -530,9 +532,9 @@ def add_text(self, text: str, position: Tuple[int] = (0, 0, 0), size: int = 10, name: str, optional name of graphic, passed to Graphic - """ - g = TextGraphic(*args, **kwargs) - self.add_graphic(g) + """ + g = TextGraphic(*args, **kwargs) + self.add_graphic(g) - return weakref.proxy(g) + return weakref.proxy(g) diff --git a/fastplotlib/utils/generate_add_methods.py b/fastplotlib/utils/generate_add_methods.py index 0a1ca0ff7..1e737fcbf 100644 --- a/fastplotlib/utils/generate_add_methods.py +++ b/fastplotlib/utils/generate_add_methods.py @@ -1,7 +1,7 @@ import inspect import sys from fastplotlib.graphics import * -#from ..graphics import * +import pathlib modules = list() for name, obj in inspect.getmembers(sys.modules[__name__]): @@ -10,9 +10,11 @@ def generate_add_graphics_methods(): # clear file and regenerate from scratch - open('../layouts/_add_graphic_mixin.py', 'w').close() + current_module = pathlib.Path(__file__).parent.parent.resolve() - f = open('../layouts/_add_graphic_mixin.py', 'w') + open(current_module.joinpath('layouts/graphic_methods_mixin.py'), 'w').close() + + f = open(current_module.joinpath('layouts/graphic_methods_mixin.py'), 'w') f.write('from typing import *\n') f.write('import numpy\n') @@ -20,21 +22,21 @@ def generate_add_graphics_methods(): f.write('import weakref\n\n') f.write("\nclass GraphicMethodsMixin:\n") - f.write("\tdef __init__(self):\n") - f.write("\t\tpass\n\n") + f.write(" def __init__(self):\n") + f.write(" pass\n\n") for m in modules: class_name = m method_name = class_name.type - f.write(f"\tdef add_{method_name}{inspect.signature(class_name.__init__)} -> weakref.proxy({class_name.__name__}):\n") - f.write('\t\t"""\n') - f.write(f'\t{class_name.__init__.__doc__}\n') - f.write('\t\t"""\n') - f.write(f"\t\tg = {class_name.__name__}(*args, **kwargs)\n") - f.write(f'\t\tself.add_graphic(g)\n\n') + f.write(f" def add_{method_name}{inspect.signature(class_name.__init__)} -> weakref.proxy({class_name.__name__}):\n") + f.write(' """\n') + f.write(f' {class_name.__init__.__doc__}\n') + f.write(' """\n') + f.write(f" g = {class_name.__name__}(*args, **kwargs)\n") + f.write(f' self.add_graphic(g)\n\n') - f.write(f'\t\treturn weakref.proxy(g)\n\n') + f.write(f' return weakref.proxy(g)\n\n') f.close() @@ -43,3 +45,4 @@ def generate_add_graphics_methods(): if __name__ == '__main__': generate_add_graphics_methods() + From b82a07b8bae2419429caaf28d506343d95dd70b1 Mon Sep 17 00:00:00 2001 From: Caitlin Lewis Date: Mon, 26 Jun 2023 08:50:25 -0400 Subject: [PATCH 05/12] fix add graphic methods --- fastplotlib/layouts/graphic_methods_mixin.py | 22 ++++++++++---------- fastplotlib/utils/generate_add_methods.py | 11 ++++++---- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/fastplotlib/layouts/graphic_methods_mixin.py b/fastplotlib/layouts/graphic_methods_mixin.py index 65486df8a..826391d7e 100644 --- a/fastplotlib/layouts/graphic_methods_mixin.py +++ b/fastplotlib/layouts/graphic_methods_mixin.py @@ -1,8 +1,10 @@ from typing import * + import numpy -from ..graphics import * import weakref +from ..graphics import * + class GraphicMethodsMixin: def __init__(self): @@ -70,7 +72,7 @@ def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = plot.show() """ - g = HeatmapGraphic(*args, **kwargs) + g = HeatmapGraphic(data=data, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -103,7 +105,7 @@ def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'aut passed to Graphic """ - g = HistogramGraphic(*args, **kwargs) + g = HistogramGraphic(data=data, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -165,9 +167,7 @@ def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = ' plot.show() """ - print(args) - print(kwargs) - g = ImageGraphic(*args, **kwargs) + g = ImageGraphic(data=data, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -286,7 +286,7 @@ def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[ """ - g = LineCollection(*args, **kwargs) + g = LineCollection(data=data, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -340,7 +340,7 @@ def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.n """ - g = LineGraphic(*args, **kwargs) + g = LineGraphic(data=data, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -451,7 +451,7 @@ def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float """ - g = LineStack(*args, **kwargs) + g = LineStack(data=data, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -505,7 +505,7 @@ def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list """ - g = ScatterGraphic(*args, **kwargs) + g = ScatterGraphic(data=data, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -533,7 +533,7 @@ def add_text(self, text: str, position: Tuple[int] = (0, 0, 0), size: int = 10, name of graphic, passed to Graphic """ - g = TextGraphic(*args, **kwargs) + g = TextGraphic(text=text, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) diff --git a/fastplotlib/utils/generate_add_methods.py b/fastplotlib/utils/generate_add_methods.py index 1e737fcbf..2e621d987 100644 --- a/fastplotlib/utils/generate_add_methods.py +++ b/fastplotlib/utils/generate_add_methods.py @@ -1,7 +1,10 @@ import inspect import sys -from fastplotlib.graphics import * import pathlib + +from fastplotlib.graphics import * + + modules = list() for name, obj in inspect.getmembers(sys.modules[__name__]): @@ -16,10 +19,10 @@ def generate_add_graphics_methods(): f = open(current_module.joinpath('layouts/graphic_methods_mixin.py'), 'w') - f.write('from typing import *\n') + f.write('from typing import *\n\n') f.write('import numpy\n') - f.write('from ..graphics import *\n') f.write('import weakref\n\n') + f.write('from ..graphics import *\n\n') f.write("\nclass GraphicMethodsMixin:\n") f.write(" def __init__(self):\n") @@ -33,7 +36,7 @@ def generate_add_graphics_methods(): f.write(' """\n') f.write(f' {class_name.__init__.__doc__}\n') f.write(' """\n') - f.write(f" g = {class_name.__name__}(*args, **kwargs)\n") + f.write(f" g = {class_name.__name__}(data=data, *args, **kwargs)\n") f.write(f' self.add_graphic(g)\n\n') f.write(f' return weakref.proxy(g)\n\n') From cb58a577a721ef7a7ba5de475cb319735ca3849f Mon Sep 17 00:00:00 2001 From: Caitlin Lewis Date: Mon, 26 Jun 2023 16:39:54 -0400 Subject: [PATCH 06/12] revert to orig --- fastplotlib/layouts/graphic_methods_mixin.py | 23 +++++++++++--------- fastplotlib/utils/generate_add_methods.py | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/fastplotlib/layouts/graphic_methods_mixin.py b/fastplotlib/layouts/graphic_methods_mixin.py index 826391d7e..b1bc4060d 100644 --- a/fastplotlib/layouts/graphic_methods_mixin.py +++ b/fastplotlib/layouts/graphic_methods_mixin.py @@ -72,7 +72,7 @@ def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = plot.show() """ - g = HeatmapGraphic(data=data, *args, **kwargs) + g = HeatmapGraphic(*args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -105,7 +105,7 @@ def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'aut passed to Graphic """ - g = HistogramGraphic(data=data, *args, **kwargs) + g = HistogramGraphic(*args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -167,10 +167,13 @@ def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = ' plot.show() """ - g = ImageGraphic(data=data, *args, **kwargs) - self.add_graphic(g) + print(args, kwargs) - return weakref.proxy(g) + return None + # g = ImageGraphic(*args, **kwargs) + # self.add_graphic(g) + # + # return weakref.proxy(g) def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', alpha: float = 1.0, cmap: Union[List[str], str] = None, name: str = None, metadata: Union[list, tuple, numpy.ndarray] = None, *args, **kwargs) -> weakref.proxy(LineCollection): """ @@ -286,7 +289,7 @@ def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[ """ - g = LineCollection(data=data, *args, **kwargs) + g = LineCollection(*args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -340,7 +343,7 @@ def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.n """ - g = LineGraphic(data=data, *args, **kwargs) + g = LineGraphic(*args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -451,7 +454,7 @@ def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float """ - g = LineStack(data=data, *args, **kwargs) + g = LineStack(*args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -505,7 +508,7 @@ def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list """ - g = ScatterGraphic(data=data, *args, **kwargs) + g = ScatterGraphic(*args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -533,7 +536,7 @@ def add_text(self, text: str, position: Tuple[int] = (0, 0, 0), size: int = 10, name of graphic, passed to Graphic """ - g = TextGraphic(text=text, *args, **kwargs) + g = TextGraphic(*args, **kwargs) self.add_graphic(g) return weakref.proxy(g) diff --git a/fastplotlib/utils/generate_add_methods.py b/fastplotlib/utils/generate_add_methods.py index 2e621d987..6b633132f 100644 --- a/fastplotlib/utils/generate_add_methods.py +++ b/fastplotlib/utils/generate_add_methods.py @@ -36,7 +36,7 @@ def generate_add_graphics_methods(): f.write(' """\n') f.write(f' {class_name.__init__.__doc__}\n') f.write(' """\n') - f.write(f" g = {class_name.__name__}(data=data, *args, **kwargs)\n") + f.write(f" g = {class_name.__name__}(*args, **kwargs)\n") f.write(f' self.add_graphic(g)\n\n') f.write(f' return weakref.proxy(g)\n\n') From 28f82715ab67b68496182ef2e382711825eda8a9 Mon Sep 17 00:00:00 2001 From: Caitlin Lewis Date: Tue, 27 Jun 2023 08:19:16 -0400 Subject: [PATCH 07/12] trying to use locals() to circumvent issue --- fastplotlib/layouts/graphic_methods_mixin.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/fastplotlib/layouts/graphic_methods_mixin.py b/fastplotlib/layouts/graphic_methods_mixin.py index b1bc4060d..f120252d9 100644 --- a/fastplotlib/layouts/graphic_methods_mixin.py +++ b/fastplotlib/layouts/graphic_methods_mixin.py @@ -167,13 +167,17 @@ def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = ' plot.show() """ - print(args, kwargs) + print(list(locals().keys())) + print(locals()) - return None - # g = ImageGraphic(*args, **kwargs) - # self.add_graphic(g) - # - # return weakref.proxy(g) + l = locals() + del l["self"] + del l["args"] + + g = ImageGraphic(**l) + self.add_graphic(g) + + return weakref.proxy(g) def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', alpha: float = 1.0, cmap: Union[List[str], str] = None, name: str = None, metadata: Union[list, tuple, numpy.ndarray] = None, *args, **kwargs) -> weakref.proxy(LineCollection): """ From 0892a5366c7a7b5515668814e744a664bb63677a Mon Sep 17 00:00:00 2001 From: Caitlin Lewis Date: Tue, 27 Jun 2023 13:42:02 -0400 Subject: [PATCH 08/12] generated methods should work now --- fastplotlib/layouts/graphic_methods_mixin.py | 23 +++++++------------- fastplotlib/utils/generate_add_methods.py | 8 ++++++- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/fastplotlib/layouts/graphic_methods_mixin.py b/fastplotlib/layouts/graphic_methods_mixin.py index f120252d9..b9429c33b 100644 --- a/fastplotlib/layouts/graphic_methods_mixin.py +++ b/fastplotlib/layouts/graphic_methods_mixin.py @@ -72,7 +72,7 @@ def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = plot.show() """ - g = HeatmapGraphic(*args, **kwargs) + g = HeatmapGraphic(data, vmin, vmax, cmap, filter, chunk_size, isolated_buffer, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -105,7 +105,7 @@ def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'aut passed to Graphic """ - g = HistogramGraphic(*args, **kwargs) + g = HistogramGraphic(data, bins, pre_computed, colors, draw_scale_factor, draw_bin_width_scale, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -167,14 +167,7 @@ def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = ' plot.show() """ - print(list(locals().keys())) - print(locals()) - - l = locals() - del l["self"] - del l["args"] - - g = ImageGraphic(**l) + g = ImageGraphic(data, vmin, vmax, cmap, filter, isolated_buffer, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -293,7 +286,7 @@ def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[ """ - g = LineCollection(*args, **kwargs) + g = LineCollection(data, z_position, thickness, colors, alpha, cmap, name, metadata, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -347,7 +340,7 @@ def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.n """ - g = LineGraphic(*args, **kwargs) + g = LineGraphic(data, thickness, colors, alpha, cmap, z_position, collection_index, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -458,7 +451,7 @@ def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float """ - g = LineStack(*args, **kwargs) + g = LineStack(data, z_position, thickness, colors, cmap, separation, separation_axis, name, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -512,7 +505,7 @@ def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list """ - g = ScatterGraphic(*args, **kwargs) + g = ScatterGraphic(data, sizes, colors, alpha, cmap, z_position, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -540,7 +533,7 @@ def add_text(self, text: str, position: Tuple[int] = (0, 0, 0), size: int = 10, name of graphic, passed to Graphic """ - g = TextGraphic(*args, **kwargs) + g = TextGraphic(text, position, size, face_color, outline_color, outline_thickness, name, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) diff --git a/fastplotlib/utils/generate_add_methods.py b/fastplotlib/utils/generate_add_methods.py index 6b633132f..9ebb09481 100644 --- a/fastplotlib/utils/generate_add_methods.py +++ b/fastplotlib/utils/generate_add_methods.py @@ -32,11 +32,17 @@ def generate_add_graphics_methods(): class_name = m method_name = class_name.type + class_args = inspect.getfullargspec(class_name)[0][1:] + class_args = [arg + ', ' for arg in class_args] + s = "" + for a in class_args: + s += a + f.write(f" def add_{method_name}{inspect.signature(class_name.__init__)} -> weakref.proxy({class_name.__name__}):\n") f.write(' """\n') f.write(f' {class_name.__init__.__doc__}\n') f.write(' """\n') - f.write(f" g = {class_name.__name__}(*args, **kwargs)\n") + f.write(f" g = {class_name.__name__}({s}*args, **kwargs)\n") f.write(f' self.add_graphic(g)\n\n') f.write(f' return weakref.proxy(g)\n\n') From 3b77c271ac27743d4ac25a13858b48ac7d428b3d Mon Sep 17 00:00:00 2001 From: Caitlin Lewis Date: Tue, 27 Jun 2023 13:50:44 -0400 Subject: [PATCH 09/12] regenerate methods to align with most up-to-date fpl graphics --- fastplotlib/layouts/graphic_methods_mixin.py | 51 +++++++++++++++----- 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/fastplotlib/layouts/graphic_methods_mixin.py b/fastplotlib/layouts/graphic_methods_mixin.py index b9429c33b..f6eb32ab7 100644 --- a/fastplotlib/layouts/graphic_methods_mixin.py +++ b/fastplotlib/layouts/graphic_methods_mixin.py @@ -21,26 +21,33 @@ def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = array-like, usually numpy.ndarray, must support ``memoryview()`` Tensorflow Tensors also work **probably**, but not thoroughly tested | shape must be ``[x_dim, y_dim]`` + vmin: int, optional minimum value for color scaling, calculated from data if not provided + vmax: int, optional maximum value for color scaling, calculated from data if not provided + cmap: str, optional, default "plasma" colormap to use to display the data + filter: str, optional, default "nearest" interpolation filter, one of "nearest" or "linear" + chunk_size: int, default 8192, max 8192 chunk size for each tile used to make up the heatmap texture + isolated_buffer: bool, default True If True, initialize a buffer with the same shape as the input data and then set the data, useful if the data arrays are ready-only such as memmaps. If False, the input array is itself used as the buffer. + args: additional arguments passed to Graphic + kwargs: additional keyword arguments passed to Graphic - Features -------- @@ -53,7 +60,6 @@ def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = **present**: :class:`.PresentFeature` Control the presence of the Graphic in the scene - Examples -------- .. code-block:: python @@ -70,6 +76,7 @@ def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = # show the plot plot.show() + """ g = HeatmapGraphic(data, vmin, vmax, cmap, filter, chunk_size, isolated_buffer, *args, **kwargs) @@ -121,20 +128,27 @@ def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = ' array-like, usually numpy.ndarray, must support ``memoryview()`` Tensorflow Tensors also work **probably**, but not thoroughly tested | shape must be ``[x_dim, y_dim]`` or ``[x_dim, y_dim, rgb]`` + vmin: int, optional minimum value for color scaling, calculated from data if not provided + vmax: int, optional maximum value for color scaling, calculated from data if not provided + cmap: str, optional, default "plasma" colormap to use to display the image data, ignored if data is RGB + filter: str, optional, default "nearest" interpolation filter, one of "nearest" or "linear" + isolated_buffer: bool, default True If True, initialize a buffer with the same shape as the input data and then set the data, useful if the data arrays are ready-only such as memmaps. If False, the input array is itself used as the buffer. + args: additional arguments passed to Graphic + kwargs: additional keyword arguments passed to Graphic @@ -150,8 +164,6 @@ def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = ' **present**: :class:`.PresentFeature` Control the presence of the Graphic in the scene - - Examples -------- .. code-block:: python @@ -165,6 +177,7 @@ def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = ' plot.add_image(data=data) # show the plot plot.show() + """ g = ImageGraphic(data, vmin, vmax, cmap, filter, isolated_buffer, *args, **kwargs) @@ -172,7 +185,7 @@ def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = ' return weakref.proxy(g) - def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', alpha: float = 1.0, cmap: Union[List[str], str] = None, name: str = None, metadata: Union[list, tuple, numpy.ndarray] = None, *args, **kwargs) -> weakref.proxy(LineCollection): + def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', alpha: float = 1.0, cmap: Union[List[str], str] = None, cmap_values: Union[numpy.ndarray, List] = None, name: str = None, metadata: Union[list, tuple, numpy.ndarray] = None, *args, **kwargs) -> weakref.proxy(LineCollection): """ Create a Line Collection @@ -203,6 +216,9 @@ def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[ | if ``list`` of ``str``, each cmap will apply to the individual lines **Note:** ``cmap`` overrides any arguments passed to ``colors`` + cmap_values: 1D array-like or list of numerical values, optional + if provided, these values are used to map the colors from the cmap + name: str, optional name of the line collection @@ -216,7 +232,6 @@ def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[ kwargs passed to GraphicCollection - Features -------- @@ -234,7 +249,6 @@ def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[ See :class:`LineGraphic` details on the features. - Examples -------- .. code-block:: python @@ -286,12 +300,12 @@ def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[ """ - g = LineCollection(data, z_position, thickness, colors, alpha, cmap, name, metadata, *args, **kwargs) + g = LineCollection(data, z_position, thickness, colors, alpha, cmap, cmap_values, name, metadata, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) - def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.ndarray, Iterable] = 'w', alpha: float = 1.0, cmap: str = None, z_position: float = None, collection_index: int = None, *args, **kwargs) -> weakref.proxy(LineGraphic): + def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.ndarray, Iterable] = 'w', alpha: float = 1.0, cmap: str = None, cmap_values: Union[numpy.ndarray, List] = None, z_position: float = None, collection_index: int = None, *args, **kwargs) -> weakref.proxy(LineGraphic): """ Create a line Graphic, 2d or 3d @@ -312,6 +326,9 @@ def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.n apply a colormap to the line instead of assigning colors manually, this overrides any argument passed to "colors" + cmap_values: 1D array-like or list of numerical values, optional + if provided, these values are used to map the colors from the cmap + alpha: float, optional, default 1.0 alpha value for the colors @@ -340,7 +357,7 @@ def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.n """ - g = LineGraphic(data, thickness, colors, alpha, cmap, z_position, collection_index, *args, **kwargs) + g = LineGraphic(data, thickness, colors, alpha, cmap, cmap_values, z_position, collection_index, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -456,7 +473,7 @@ def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float return weakref.proxy(g) - def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list] = 1, colors: numpy.ndarray = 'w', alpha: float = 1.0, cmap: str = None, z_position: float = 0.0, *args, **kwargs) -> weakref.proxy(ScatterGraphic): + def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list] = 1, colors: numpy.ndarray = 'w', alpha: float = 1.0, cmap: str = None, cmap_values: Union[numpy.ndarray, List] = None, z_position: float = 0.0, *args, **kwargs) -> weakref.proxy(ScatterGraphic): """ Create a Scatter Graphic, 2d or 3d @@ -477,6 +494,9 @@ def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list apply a colormap to the scatter instead of assigning colors manually, this overrides any argument passed to "colors" + cmap_values: 1D array-like or list of numerical values, optional + if provided, these values are used to map the colors from the cmap + alpha: float, optional, default 1.0 alpha value for the colors @@ -505,7 +525,7 @@ def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list """ - g = ScatterGraphic(data, sizes, colors, alpha, cmap, z_position, *args, **kwargs) + g = ScatterGraphic(data, sizes, colors, alpha, cmap, cmap_values, z_position, *args, **kwargs) self.add_graphic(g) return weakref.proxy(g) @@ -519,18 +539,25 @@ def add_text(self, text: str, position: Tuple[int] = (0, 0, 0), size: int = 10, ---------- text: str display text + position: int tuple, default (0, 0, 0) int tuple indicating location of text in scene + size: int, default 10 text size + face_color: str or array, default "w" str or RGBA array to set the color of the text + outline_color: str or array, default "w" str or RGBA array to set the outline color of the text + outline_thickness: int, default 0 text outline thickness + name: str, optional name of graphic, passed to Graphic + """ g = TextGraphic(text, position, size, face_color, outline_color, outline_thickness, name, *args, **kwargs) From c468518dade58e7279efca3f6fb145c8ca69aa9f Mon Sep 17 00:00:00 2001 From: Caitlin Lewis Date: Tue, 27 Jun 2023 15:23:54 -0400 Subject: [PATCH 10/12] fix automixin generated methods --- fastplotlib/layouts/_subplot.py | 15 ------ fastplotlib/layouts/graphic_methods_mixin.py | 55 ++++++++------------ fastplotlib/utils/generate_add_methods.py | 20 +++++-- 3 files changed, 38 insertions(+), 52 deletions(-) diff --git a/fastplotlib/layouts/_subplot.py b/fastplotlib/layouts/_subplot.py index a035dfdad..1ed52bc7c 100644 --- a/fastplotlib/layouts/_subplot.py +++ b/fastplotlib/layouts/_subplot.py @@ -121,21 +121,6 @@ def __init__( if self.name is not None: self.set_title(self.name) - def _create_graphic(self, graphic_class, *args, **kwargs) -> weakref.proxy: - if "center" in kwargs.keys(): - center = kwargs.pop("center") - else: - center = False - - if "name" in kwargs.keys(): - self._check_graphic_name_exists(kwargs["name"]) - - graphic = graphic_class(*args, **kwargs) - self.add_graphic(graphic, center=center) - - # only return a proxy to the real graphic - return weakref.proxy(graphic) - @property def name(self) -> Any: return self._name diff --git a/fastplotlib/layouts/graphic_methods_mixin.py b/fastplotlib/layouts/graphic_methods_mixin.py index f6eb32ab7..46c539735 100644 --- a/fastplotlib/layouts/graphic_methods_mixin.py +++ b/fastplotlib/layouts/graphic_methods_mixin.py @@ -10,6 +10,21 @@ class GraphicMethodsMixin: def __init__(self): pass + def _create_graphic(self, graphic_class, *args, **kwargs) -> weakref.proxy: + if 'center' in kwargs.keys(): + center = kwargs.pop('center') + else: + center = False + + if 'name' in kwargs.keys(): + self._check_graphic_name_exists(kwargs['name']) + + graphic = graphic_class(*args, **kwargs) + self.add_graphic(graphic, center=center) + + # only return a proxy to the real graphic + return weakref.proxy(graphic) + def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', chunk_size: int = 8192, isolated_buffer: bool = True, *args, **kwargs) -> weakref.proxy(HeatmapGraphic): """ @@ -79,10 +94,7 @@ def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = """ - g = HeatmapGraphic(data, vmin, vmax, cmap, filter, chunk_size, isolated_buffer, *args, **kwargs) - self.add_graphic(g) - - return weakref.proxy(g) + return self._create_graphic(HeatmapGraphic, data, vmin, vmax, cmap, filter, chunk_size, isolated_buffer, *args, **kwargs) def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'auto', pre_computed: Dict[str, numpy.ndarray] = None, colors: numpy.ndarray = 'w', draw_scale_factor: float = 100.0, draw_bin_width_scale: float = 1.0, **kwargs) -> weakref.proxy(HistogramGraphic): """ @@ -112,10 +124,7 @@ def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'aut passed to Graphic """ - g = HistogramGraphic(data, bins, pre_computed, colors, draw_scale_factor, draw_bin_width_scale, *args, **kwargs) - self.add_graphic(g) - - return weakref.proxy(g) + return self._create_graphic(HistogramGraphic, data, bins, pre_computed, colors, draw_scale_factor, draw_bin_width_scale, *args, **kwargs) def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', isolated_buffer: bool = True, *args, **kwargs) -> weakref.proxy(ImageGraphic): """ @@ -180,10 +189,7 @@ def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = ' """ - g = ImageGraphic(data, vmin, vmax, cmap, filter, isolated_buffer, *args, **kwargs) - self.add_graphic(g) - - return weakref.proxy(g) + return self._create_graphic(ImageGraphic, data, vmin, vmax, cmap, filter, isolated_buffer, *args, **kwargs) def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', alpha: float = 1.0, cmap: Union[List[str], str] = None, cmap_values: Union[numpy.ndarray, List] = None, name: str = None, metadata: Union[list, tuple, numpy.ndarray] = None, *args, **kwargs) -> weakref.proxy(LineCollection): """ @@ -300,10 +306,7 @@ def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[ """ - g = LineCollection(data, z_position, thickness, colors, alpha, cmap, cmap_values, name, metadata, *args, **kwargs) - self.add_graphic(g) - - return weakref.proxy(g) + return self._create_graphic(LineCollection, data, z_position, thickness, colors, alpha, cmap, cmap_values, name, metadata, *args, **kwargs) def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.ndarray, Iterable] = 'w', alpha: float = 1.0, cmap: str = None, cmap_values: Union[numpy.ndarray, List] = None, z_position: float = None, collection_index: int = None, *args, **kwargs) -> weakref.proxy(LineGraphic): """ @@ -357,10 +360,7 @@ def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.n """ - g = LineGraphic(data, thickness, colors, alpha, cmap, cmap_values, z_position, collection_index, *args, **kwargs) - self.add_graphic(g) - - return weakref.proxy(g) + return self._create_graphic(LineGraphic, data, thickness, colors, alpha, cmap, cmap_values, z_position, collection_index, *args, **kwargs) def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', cmap: Union[List[str], str] = None, separation: float = 10, separation_axis: str = 'y', name: str = None, *args, **kwargs) -> weakref.proxy(LineStack): """ @@ -468,10 +468,7 @@ def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float """ - g = LineStack(data, z_position, thickness, colors, cmap, separation, separation_axis, name, *args, **kwargs) - self.add_graphic(g) - - return weakref.proxy(g) + return self._create_graphic(LineStack, data, z_position, thickness, colors, cmap, separation, separation_axis, name, *args, **kwargs) def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list] = 1, colors: numpy.ndarray = 'w', alpha: float = 1.0, cmap: str = None, cmap_values: Union[numpy.ndarray, List] = None, z_position: float = 0.0, *args, **kwargs) -> weakref.proxy(ScatterGraphic): """ @@ -525,10 +522,7 @@ def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list """ - g = ScatterGraphic(data, sizes, colors, alpha, cmap, cmap_values, z_position, *args, **kwargs) - self.add_graphic(g) - - return weakref.proxy(g) + return self._create_graphic(ScatterGraphic, data, sizes, colors, alpha, cmap, cmap_values, z_position, *args, **kwargs) def add_text(self, text: str, position: Tuple[int] = (0, 0, 0), size: int = 10, face_color: Union[str, numpy.ndarray] = 'w', outline_color: Union[str, numpy.ndarray] = 'w', outline_thickness=0, name: str = None) -> weakref.proxy(TextGraphic): """ @@ -560,8 +554,5 @@ def add_text(self, text: str, position: Tuple[int] = (0, 0, 0), size: int = 10, """ - g = TextGraphic(text, position, size, face_color, outline_color, outline_thickness, name, *args, **kwargs) - self.add_graphic(g) - - return weakref.proxy(g) + return self._create_graphic(TextGraphic, text, position, size, face_color, outline_color, outline_thickness, name, *args, **kwargs) diff --git a/fastplotlib/utils/generate_add_methods.py b/fastplotlib/utils/generate_add_methods.py index 9ebb09481..6c1294fe3 100644 --- a/fastplotlib/utils/generate_add_methods.py +++ b/fastplotlib/utils/generate_add_methods.py @@ -28,6 +28,19 @@ def generate_add_graphics_methods(): f.write(" def __init__(self):\n") f.write(" pass\n\n") + f.write(" def _create_graphic(self, graphic_class, *args, **kwargs) -> weakref.proxy:\n") + f.write(" if 'center' in kwargs.keys():\n") + f.write(" center = kwargs.pop('center')\n") + f.write(" else:\n") + f.write(" center = False\n\n") + f.write(" if 'name' in kwargs.keys():\n") + f.write(" self._check_graphic_name_exists(kwargs['name'])\n\n") + f.write(" graphic = graphic_class(*args, **kwargs)\n") + f.write(" self.add_graphic(graphic, center=center)\n\n") + f.write(" # only return a proxy to the real graphic\n") + f.write(" return weakref.proxy(graphic)\n\n") + + for m in modules: class_name = m method_name = class_name.type @@ -38,14 +51,11 @@ def generate_add_graphics_methods(): for a in class_args: s += a - f.write(f" def add_{method_name}{inspect.signature(class_name.__init__)} -> weakref.proxy({class_name.__name__}):\n") + f.write(f" def add_{method_name}{inspect.signature(class_name.__init__)} -> weakref.proxy:\n") f.write(' """\n') f.write(f' {class_name.__init__.__doc__}\n') f.write(' """\n') - f.write(f" g = {class_name.__name__}({s}*args, **kwargs)\n") - f.write(f' self.add_graphic(g)\n\n') - - f.write(f' return weakref.proxy(g)\n\n') + f.write(f" return self._create_graphic({class_name.__name__}, {s}*args, **kwargs)\n\n") f.close() From efc997980981c1ba0eb18e8f0e2071bf091d3ca3 Mon Sep 17 00:00:00 2001 From: Caitlin Lewis Date: Wed, 28 Jun 2023 19:45:05 -0400 Subject: [PATCH 11/12] add disclaimer, change return type annotation --- fastplotlib/layouts/graphic_methods_mixin.py | 19 +++++++++++-------- fastplotlib/utils/generate_add_methods.py | 5 ++++- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/fastplotlib/layouts/graphic_methods_mixin.py b/fastplotlib/layouts/graphic_methods_mixin.py index 46c539735..bb800151f 100644 --- a/fastplotlib/layouts/graphic_methods_mixin.py +++ b/fastplotlib/layouts/graphic_methods_mixin.py @@ -1,9 +1,12 @@ +# This is an auto-generated file and should not be modified directly + from typing import * import numpy import weakref from ..graphics import * +from ..graphics._base import Graphic class GraphicMethodsMixin: @@ -25,7 +28,7 @@ def _create_graphic(self, graphic_class, *args, **kwargs) -> weakref.proxy: # only return a proxy to the real graphic return weakref.proxy(graphic) - def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', chunk_size: int = 8192, isolated_buffer: bool = True, *args, **kwargs) -> weakref.proxy(HeatmapGraphic): + def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', chunk_size: int = 8192, isolated_buffer: bool = True, *args, **kwargs) -> weakref.proxy: """ Create an Image Graphic @@ -96,7 +99,7 @@ def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = """ return self._create_graphic(HeatmapGraphic, data, vmin, vmax, cmap, filter, chunk_size, isolated_buffer, *args, **kwargs) - def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'auto', pre_computed: Dict[str, numpy.ndarray] = None, colors: numpy.ndarray = 'w', draw_scale_factor: float = 100.0, draw_bin_width_scale: float = 1.0, **kwargs) -> weakref.proxy(HistogramGraphic): + def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'auto', pre_computed: Dict[str, numpy.ndarray] = None, colors: numpy.ndarray = 'w', draw_scale_factor: float = 100.0, draw_bin_width_scale: float = 1.0, **kwargs) -> weakref.proxy: """ Create a Histogram Graphic @@ -126,7 +129,7 @@ def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'aut """ return self._create_graphic(HistogramGraphic, data, bins, pre_computed, colors, draw_scale_factor, draw_bin_width_scale, *args, **kwargs) - def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', isolated_buffer: bool = True, *args, **kwargs) -> weakref.proxy(ImageGraphic): + def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', isolated_buffer: bool = True, *args, **kwargs) -> weakref.proxy: """ Create an Image Graphic @@ -191,7 +194,7 @@ def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = ' """ return self._create_graphic(ImageGraphic, data, vmin, vmax, cmap, filter, isolated_buffer, *args, **kwargs) - def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', alpha: float = 1.0, cmap: Union[List[str], str] = None, cmap_values: Union[numpy.ndarray, List] = None, name: str = None, metadata: Union[list, tuple, numpy.ndarray] = None, *args, **kwargs) -> weakref.proxy(LineCollection): + def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', alpha: float = 1.0, cmap: Union[List[str], str] = None, cmap_values: Union[numpy.ndarray, List] = None, name: str = None, metadata: Union[list, tuple, numpy.ndarray] = None, *args, **kwargs) -> weakref.proxy: """ Create a Line Collection @@ -308,7 +311,7 @@ def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[ """ return self._create_graphic(LineCollection, data, z_position, thickness, colors, alpha, cmap, cmap_values, name, metadata, *args, **kwargs) - def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.ndarray, Iterable] = 'w', alpha: float = 1.0, cmap: str = None, cmap_values: Union[numpy.ndarray, List] = None, z_position: float = None, collection_index: int = None, *args, **kwargs) -> weakref.proxy(LineGraphic): + def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.ndarray, Iterable] = 'w', alpha: float = 1.0, cmap: str = None, cmap_values: Union[numpy.ndarray, List] = None, z_position: float = None, collection_index: int = None, *args, **kwargs) -> weakref.proxy: """ Create a line Graphic, 2d or 3d @@ -362,7 +365,7 @@ def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.n """ return self._create_graphic(LineGraphic, data, thickness, colors, alpha, cmap, cmap_values, z_position, collection_index, *args, **kwargs) - def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', cmap: Union[List[str], str] = None, separation: float = 10, separation_axis: str = 'y', name: str = None, *args, **kwargs) -> weakref.proxy(LineStack): + def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', cmap: Union[List[str], str] = None, separation: float = 10, separation_axis: str = 'y', name: str = None, *args, **kwargs) -> weakref.proxy: """ Create a line stack @@ -470,7 +473,7 @@ def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float """ return self._create_graphic(LineStack, data, z_position, thickness, colors, cmap, separation, separation_axis, name, *args, **kwargs) - def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list] = 1, colors: numpy.ndarray = 'w', alpha: float = 1.0, cmap: str = None, cmap_values: Union[numpy.ndarray, List] = None, z_position: float = 0.0, *args, **kwargs) -> weakref.proxy(ScatterGraphic): + def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list] = 1, colors: numpy.ndarray = 'w', alpha: float = 1.0, cmap: str = None, cmap_values: Union[numpy.ndarray, List] = None, z_position: float = 0.0, *args, **kwargs) -> weakref.proxy: """ Create a Scatter Graphic, 2d or 3d @@ -524,7 +527,7 @@ def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list """ return self._create_graphic(ScatterGraphic, data, sizes, colors, alpha, cmap, cmap_values, z_position, *args, **kwargs) - def add_text(self, text: str, position: Tuple[int] = (0, 0, 0), size: int = 10, face_color: Union[str, numpy.ndarray] = 'w', outline_color: Union[str, numpy.ndarray] = 'w', outline_thickness=0, name: str = None) -> weakref.proxy(TextGraphic): + def add_text(self, text: str, position: Tuple[int] = (0, 0, 0), size: int = 10, face_color: Union[str, numpy.ndarray] = 'w', outline_color: Union[str, numpy.ndarray] = 'w', outline_thickness=0, name: str = None) -> weakref.proxy: """ Create a text Graphic diff --git a/fastplotlib/utils/generate_add_methods.py b/fastplotlib/utils/generate_add_methods.py index 6c1294fe3..23b14042c 100644 --- a/fastplotlib/utils/generate_add_methods.py +++ b/fastplotlib/utils/generate_add_methods.py @@ -19,10 +19,13 @@ def generate_add_graphics_methods(): f = open(current_module.joinpath('layouts/graphic_methods_mixin.py'), 'w') + f.write('# This is an auto-generated file and should not be modified directly\n\n') + f.write('from typing import *\n\n') f.write('import numpy\n') f.write('import weakref\n\n') - f.write('from ..graphics import *\n\n') + f.write('from ..graphics import *\n') + f.write('from ..graphics._base import Graphic\n\n') f.write("\nclass GraphicMethodsMixin:\n") f.write(" def __init__(self):\n") From 223514f2fed657aa7b16890d6d1e603afea94495 Mon Sep 17 00:00:00 2001 From: Caitlin Lewis Date: Wed, 28 Jun 2023 20:22:33 -0400 Subject: [PATCH 12/12] change return type --- fastplotlib/layouts/graphic_methods_mixin.py | 18 +++++++++--------- fastplotlib/utils/generate_add_methods.py | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/fastplotlib/layouts/graphic_methods_mixin.py b/fastplotlib/layouts/graphic_methods_mixin.py index bb800151f..ab697637b 100644 --- a/fastplotlib/layouts/graphic_methods_mixin.py +++ b/fastplotlib/layouts/graphic_methods_mixin.py @@ -13,7 +13,7 @@ class GraphicMethodsMixin: def __init__(self): pass - def _create_graphic(self, graphic_class, *args, **kwargs) -> weakref.proxy: + def _create_graphic(self, graphic_class, *args, **kwargs) -> Graphic: if 'center' in kwargs.keys(): center = kwargs.pop('center') else: @@ -28,7 +28,7 @@ def _create_graphic(self, graphic_class, *args, **kwargs) -> weakref.proxy: # only return a proxy to the real graphic return weakref.proxy(graphic) - def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', chunk_size: int = 8192, isolated_buffer: bool = True, *args, **kwargs) -> weakref.proxy: + def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', chunk_size: int = 8192, isolated_buffer: bool = True, *args, **kwargs) -> HeatmapGraphic: """ Create an Image Graphic @@ -99,7 +99,7 @@ def add_heatmap(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = """ return self._create_graphic(HeatmapGraphic, data, vmin, vmax, cmap, filter, chunk_size, isolated_buffer, *args, **kwargs) - def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'auto', pre_computed: Dict[str, numpy.ndarray] = None, colors: numpy.ndarray = 'w', draw_scale_factor: float = 100.0, draw_bin_width_scale: float = 1.0, **kwargs) -> weakref.proxy: + def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'auto', pre_computed: Dict[str, numpy.ndarray] = None, colors: numpy.ndarray = 'w', draw_scale_factor: float = 100.0, draw_bin_width_scale: float = 1.0, **kwargs) -> HistogramGraphic: """ Create a Histogram Graphic @@ -129,7 +129,7 @@ def add_histogram(self, data: numpy.ndarray = None, bins: Union[int, str] = 'aut """ return self._create_graphic(HistogramGraphic, data, bins, pre_computed, colors, draw_scale_factor, draw_bin_width_scale, *args, **kwargs) - def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', isolated_buffer: bool = True, *args, **kwargs) -> weakref.proxy: + def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = 'plasma', filter: str = 'nearest', isolated_buffer: bool = True, *args, **kwargs) -> ImageGraphic: """ Create an Image Graphic @@ -194,7 +194,7 @@ def add_image(self, data: Any, vmin: int = None, vmax: int = None, cmap: str = ' """ return self._create_graphic(ImageGraphic, data, vmin, vmax, cmap, filter, isolated_buffer, *args, **kwargs) - def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', alpha: float = 1.0, cmap: Union[List[str], str] = None, cmap_values: Union[numpy.ndarray, List] = None, name: str = None, metadata: Union[list, tuple, numpy.ndarray] = None, *args, **kwargs) -> weakref.proxy: + def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', alpha: float = 1.0, cmap: Union[List[str], str] = None, cmap_values: Union[numpy.ndarray, List] = None, name: str = None, metadata: Union[list, tuple, numpy.ndarray] = None, *args, **kwargs) -> LineCollection: """ Create a Line Collection @@ -311,7 +311,7 @@ def add_line_collection(self, data: List[numpy.ndarray], z_position: Union[List[ """ return self._create_graphic(LineCollection, data, z_position, thickness, colors, alpha, cmap, cmap_values, name, metadata, *args, **kwargs) - def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.ndarray, Iterable] = 'w', alpha: float = 1.0, cmap: str = None, cmap_values: Union[numpy.ndarray, List] = None, z_position: float = None, collection_index: int = None, *args, **kwargs) -> weakref.proxy: + def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.ndarray, Iterable] = 'w', alpha: float = 1.0, cmap: str = None, cmap_values: Union[numpy.ndarray, List] = None, z_position: float = None, collection_index: int = None, *args, **kwargs) -> LineGraphic: """ Create a line Graphic, 2d or 3d @@ -365,7 +365,7 @@ def add_line(self, data: Any, thickness: float = 2.0, colors: Union[str, numpy.n """ return self._create_graphic(LineGraphic, data, thickness, colors, alpha, cmap, cmap_values, z_position, collection_index, *args, **kwargs) - def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', cmap: Union[List[str], str] = None, separation: float = 10, separation_axis: str = 'y', name: str = None, *args, **kwargs) -> weakref.proxy: + def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float], float] = None, thickness: Union[float, List[float]] = 2.0, colors: Union[List[numpy.ndarray], numpy.ndarray] = 'w', cmap: Union[List[str], str] = None, separation: float = 10, separation_axis: str = 'y', name: str = None, *args, **kwargs) -> LineStack: """ Create a line stack @@ -473,7 +473,7 @@ def add_line_stack(self, data: List[numpy.ndarray], z_position: Union[List[float """ return self._create_graphic(LineStack, data, z_position, thickness, colors, cmap, separation, separation_axis, name, *args, **kwargs) - def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list] = 1, colors: numpy.ndarray = 'w', alpha: float = 1.0, cmap: str = None, cmap_values: Union[numpy.ndarray, List] = None, z_position: float = 0.0, *args, **kwargs) -> weakref.proxy: + def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list] = 1, colors: numpy.ndarray = 'w', alpha: float = 1.0, cmap: str = None, cmap_values: Union[numpy.ndarray, List] = None, z_position: float = 0.0, *args, **kwargs) -> ScatterGraphic: """ Create a Scatter Graphic, 2d or 3d @@ -527,7 +527,7 @@ def add_scatter(self, data: numpy.ndarray, sizes: Union[int, numpy.ndarray, list """ return self._create_graphic(ScatterGraphic, data, sizes, colors, alpha, cmap, cmap_values, z_position, *args, **kwargs) - def add_text(self, text: str, position: Tuple[int] = (0, 0, 0), size: int = 10, face_color: Union[str, numpy.ndarray] = 'w', outline_color: Union[str, numpy.ndarray] = 'w', outline_thickness=0, name: str = None) -> weakref.proxy: + def add_text(self, text: str, position: Tuple[int] = (0, 0, 0), size: int = 10, face_color: Union[str, numpy.ndarray] = 'w', outline_color: Union[str, numpy.ndarray] = 'w', outline_thickness=0, name: str = None) -> TextGraphic: """ Create a text Graphic diff --git a/fastplotlib/utils/generate_add_methods.py b/fastplotlib/utils/generate_add_methods.py index 23b14042c..e3993fff2 100644 --- a/fastplotlib/utils/generate_add_methods.py +++ b/fastplotlib/utils/generate_add_methods.py @@ -31,7 +31,7 @@ def generate_add_graphics_methods(): f.write(" def __init__(self):\n") f.write(" pass\n\n") - f.write(" def _create_graphic(self, graphic_class, *args, **kwargs) -> weakref.proxy:\n") + f.write(" def _create_graphic(self, graphic_class, *args, **kwargs) -> Graphic:\n") f.write(" if 'center' in kwargs.keys():\n") f.write(" center = kwargs.pop('center')\n") f.write(" else:\n") @@ -54,7 +54,7 @@ def generate_add_graphics_methods(): for a in class_args: s += a - f.write(f" def add_{method_name}{inspect.signature(class_name.__init__)} -> weakref.proxy:\n") + f.write(f" def add_{method_name}{inspect.signature(class_name.__init__)} -> {class_name.__name__}:\n") f.write(' """\n') f.write(f' {class_name.__init__.__doc__}\n') f.write(' """\n') pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy