Skip to content

xy_data_plotter

xy_data_plotter

XYDataPlotter

XYDataPlotter(in_dirs: List[Path], out_dir: Path, dpi: int = 500)

Plots xy data from user-specified directories to a single axis figure

Parameters:

Name Type Description Default
in_dirs List[Path]

Directories in which to search for data products from JSON files

required
out_dir Path

directory to which figure will be output

required
dpi int

Saved image resolution

500
Source code in src/trendify/api/generator/xy_data_plotter.py
def __init__(
    self,
    in_dirs: List[Path],
    out_dir: Path,
    dpi: int = 500,
):
    self.in_dirs = in_dirs
    self.out_dir = out_dir
    self.dpi = dpi

handle_points_and_traces classmethod

handle_points_and_traces(
    tag: Tag,
    points: List[Point2D],
    traces: List[Trace2D],
    axlines: List[AxLine],
    dir_out: Path,
    dpi: int,
    saf: SingleAxisFigure | None = None,
)

Plots points, traces, and axlines, formats figure, saves figure, and closes matplotlinb figure.

Parameters:

Name Type Description Default
tag Tag

Tag corresponding to the provided points and traces

required
points List[Point2D]

Points to be scattered

required
traces List[Trace2D]

List of traces to be plotted

required
axlines List[AxLine]

List of axis lines to be plotted

required
dir_out Path

directory to output the plot

required
dpi int

resolution of plot

required
Source code in src/trendify/api/generator/xy_data_plotter.py
@classmethod
def handle_points_and_traces(
    cls,
    tag: Tag,
    points: List[Point2D],
    traces: List[Trace2D],
    axlines: List[AxLine],  # Add this parameter
    dir_out: Path,
    dpi: int,
    saf: SingleAxisFigure | None = None,
):
    """
    Plots points, traces, and axlines, formats figure, saves figure, and closes matplotlinb figure.

    Args:
        tag (Tag): Tag  corresponding to the provided points and traces
        points (List[Point2D]): Points to be scattered
        traces (List[Trace2D]): List of traces to be plotted
        axlines (List[AxLine]): List of axis lines to be plotted
        dir_out (Path): directory to output the plot
        dpi (int): resolution of plot
    """

    if saf is None:
        saf = SingleAxisFigure.new(tag=tag)

    if points:
        markers = set([p.marker for p in points])
        for marker in markers:
            matching_points = [p for p in points if p.marker == marker]
            x = [p.x for p in matching_points]
            y = [p.y for p in matching_points]
            if x and y:
                saf.ax.scatter(x, y, **marker.as_scatter_plot_kwargs())

    for trace in traces:
        trace.plot_to_ax(saf.ax)

    # Add plotting of axlines
    for axline in axlines:
        axline.plot_to_ax(saf.ax)

    # formats = list(
    #     set(
    #         [p.format2d for p in points]
    #         + [t.format2d for t in traces]
    #         + [a.format2d for a in axlines]
    #     )
    # )

    # format2d = Format2D.union_from_iterable(formats)
    # saf.apply_format(format2d)
    # saf.ax.autoscale(enable=True, axis='both', tight=True)

    # save_path = dir_out.joinpath(*tuple(atleast_1d(tag))).with_suffix(".jpg")
    # save_path.parent.mkdir(exist_ok=True, parents=True)
    # print(f"Saving to {save_path = }")
    # saf.savefig(path=save_path, dpi=dpi)
    # del saf

    return saf

plot

plot(tag: Tag, data_products_fname: str = DATA_PRODUCTS_FNAME_DEFAULT)
  • Collects data from json files in stored self.in_dirs,
  • plots the relevant products,
  • applies labels and formatting,
  • saves the figure
  • closes matplotlib figure

Parameters:

Name Type Description Default
tag Tag

data tag for which products are to be collected and plotted.

required
data_products_fname str

Data products file name

DATA_PRODUCTS_FNAME_DEFAULT
Source code in src/trendify/api/generator/xy_data_plotter.py
def plot(
    self,
    tag: Tag,
    data_products_fname: str = DATA_PRODUCTS_FNAME_DEFAULT,
):
    """
    - Collects data from json files in stored `self.in_dirs`,
    - plots the relevant products,
    - applies labels and formatting,
    - saves the figure
    - closes matplotlib figure

    Args:
        tag (Tag): data tag for which products are to be collected and plotted.
        data_products_fname (str): Data products file name
    """
    logger.info(f"Making xy plot for {tag = }")
    saf = SingleAxisFigure.new(tag=tag)

    for subdir in self.in_dirs:
        collection = DataProductCollection.model_validate_json(
            subdir.joinpath(data_products_fname).read_text()
        )
        traces: List[Trace2D] = collection.get_products(
            tag=tag, object_type=Trace2D
        ).elements
        points: List[Point2D] = collection.get_products(
            tag=tag, object_type=Point2D
        ).elements

        if points or traces:
            if points:
                markers = set([p.marker for p in points])
                for marker in markers:
                    matching_points = [p for p in points if p.marker == marker]
                    x = [p.x for p in matching_points]
                    y = [p.y for p in matching_points]
                    if x and y:
                        if marker is not None:
                            saf.ax.scatter(x, y, **marker.as_scatter_plot_kwargs())
                        else:
                            saf.ax.scatter(x, y)

            for trace in traces:
                trace.plot_to_ax(saf.ax)

            formats = list(
                set(
                    [p.format2d for p in points if p.format2d]
                    + [t.format2d for t in traces]
                )
                - {None}
            )
            format2d = Format2D.union_from_iterable(formats)
            saf.apply_format(format2d)
            # saf.ax.autoscale(enable=True, axis='both', tight=True)

    save_path = self.out_dir.joinpath(*tuple(atleast_1d(tag))).with_suffix(".jpg")
    save_path.parent.mkdir(exist_ok=True, parents=True)
    logger.info(f"Saving to {save_path = }")
    saf.savefig(path=save_path, dpi=self.dpi)
    del saf