Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a minimal plot 2d function #734

Merged
merged 5 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes.d/703.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
New two dimensional plotting function ``qupulse.pulses.plotting.plot_2d``.
54 changes: 53 additions & 1 deletion qupulse/pulses/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
- plot: Plot a pulse using matplotlib.
"""

from typing import Dict, Tuple, Any, Optional, Set, List, Union
from typing import Dict, Tuple, Any, Optional, Set, List, Union, Mapping
from numbers import Real

import matplotlib.pyplot as plt
import numpy as np
import warnings
import operator
import itertools
import functools

from qupulse._program import waveforms
from qupulse.utils.types import ChannelID, MeasurementWindow, has_type_interface
Expand Down Expand Up @@ -252,6 +254,56 @@ def plot(pulse: PulseTemplate,
return axes.get_figure()


@functools.singledispatch
def plot_2d(program: Loop, channels: Tuple[ChannelID, ChannelID],
sample_rate: float = None,
ax: plt.Axes = None,
plot_kwargs: Mapping = None) -> plt.Figure:
"""Plot the pulse/program in the plane of the given channels.

Args:
program: The program to plot
channels: (x_axis, y_axis) name tuple
sample_rate: Sample rate to use. Defaults to max(1000 samples per program, 10 per nano second)
ax: Axis to plot into.
plot_kwargs: Forwarded to the plot function.
"""
if sample_rate is None:
sample_rate = max(1000 / program.duration, 10)

_, rendered, _ = render(program, sample_rate, plot_channels=set(channels))
x_y = np.array([rendered[channels[0]], rendered[channels[1]]])
keep = np.full(x_y.shape[1], fill_value=True)
keep[1:] = np.any(x_y[:, 1:] != x_y[:, :-1], axis=0)
x_y_plt = x_y[:, keep]

ax = ax or plt.subplots()[1]
ax.plot(x_y_plt[0, :], x_y_plt[1, :], **(plot_kwargs or {}))
ax.set_xlabel(channels[0])
ax.set_ylabel(channels[1])
return ax.get_figure()


@plot_2d.register
def _(pulse_template: PulseTemplate,
channels: Tuple[ChannelID, ChannelID],
sample_rate: float = None,
ax: plt.Axes = None,
plot_kwargs: Mapping = None,
parameters=None,
channel_mapping=None) -> plt.Figure:

if channel_mapping is None:
channel_mapping = {ch: ch if ch in channels else None
for ch in pulse_template.defined_channels}
create_program_kwargs = {'channel_mapping': channel_mapping}
if parameters is not None:
create_program_kwargs['parameters'] = parameters

program = pulse_template.create_program(**create_program_kwargs)
return plot_2d(program, channels, sample_rate=sample_rate, ax=ax, plot_kwargs=plot_kwargs)


class PlottingNotPossibleException(Exception):
"""Indicates that plotting is not possible because the sequencing process did not translate
the entire given PulseTemplate structure."""
Expand Down