Skip to content
Open
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
2 changes: 2 additions & 0 deletions docs/source/tendencies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Most tendencies accept the following parameters to define their time interval. Y
.. note::
The :ref:`Piecewise Linear Tendency <piecewise-linear-tendency>` is an exception and derives its time interval solely from its ``time`` parameter list. It does *not* accept ``start``, ``duration``, or ``end``.

.. _constant-tendency:

Constant Tendency
=================

Expand Down
6 changes: 4 additions & 2 deletions docs/source/yaml_format.rst
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ a list of waveforms, or a single number (float or integer).

Refer to the :ref:`Available Tendencies <available-tendencies>` documentation for details on the different tendency types and their parameters.

2. **Constant Value:** A simple number (integer or float) defines a constant waveform over time.
2. **Constant Value:** A bare number (integer or float) or string defines a constant waveform over time, equivalent to a single :ref:`constant tendency <constant-tendency>`.

.. code-block:: yaml

ec_launchers/beam(1)/phase/angle: -1.65898 # Constant value
ec_launchers/beam(1)/phase/angle: -1.65898 # Constant float value
pulse_schedule/ec/mode: 3 # Constant int value
core_sources/source(1)/identifier/name: ec # Constant string value

3. **Empty Waveform:** An empty list ``[{}]`` defines a waveform that is constantly zero.

Expand Down
21 changes: 14 additions & 7 deletions tests/test_yaml_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from pytest import approx

from waveform_editor.configuration import WaveformConfiguration
from waveform_editor.derived_waveform import DerivedWaveform
from waveform_editor.tendencies.constant import ConstantTendency
from waveform_editor.tendencies.linear import LinearTendency
from waveform_editor.tendencies.periodic.sawtooth_wave import SawtoothWaveTendency
Expand Down Expand Up @@ -132,18 +131,26 @@ def test_scientific_notation(yaml_parser):


def test_constant_shorthand_notation(yaml_parser):
"""Test if shorthand notation is parsed correctly."""
"""Test if shorthand notation is parsed as a constant tendency."""

waveforms = {"waveform: 5": 5, "waveform: 1.23": 1.23}
waveforms = {
"waveform: 5": (5, int),
"waveform: 1.23": (1.23, float),
"waveform: test": ("test", str),
}

for waveform, expected_value in waveforms.items():
for waveform, (expected_value, expected_type) in waveforms.items():
waveform = yaml_parser.parse_waveform(waveform)
assert isinstance(waveform, DerivedWaveform)
assert waveform.yaml == expected_value
assert isinstance(waveform, Waveform)
assert len(waveform.tendencies) == 1
assert isinstance(waveform.tendencies[0], ConstantTendency)
assert waveform.value_type is expected_type
assert not waveform.annotations
assert waveform.dependencies == set()
assert not yaml_parser.parse_errors

_, values = waveform.get_value()
assert all(v == expected_value for v in values)


def test_load_yaml(config):
"""Test if yaml is loaded correctly."""
Expand Down
20 changes: 20 additions & 0 deletions waveform_editor/yaml/yaml_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,26 @@ def parse_waveform(self, yaml_str):
name=name,
dd_version=dd_version,
)
# Shorthand notation for waveforms containing a constant tendency, e.g.
# ec_launchers/beam(1)/phase/angle: -1.65898 # float
# pulse_schedule/ec/mode: 3 # int
# core_sources/source(1)/identifier/name: ec # string
elif isinstance(waveform, (int, float)) or (
"'" not in waveform and '"' not in waveform
):
waveform = Waveform(
waveform=[
{
"user_type": "constant",
"user_value": waveform,
"line_number": line_number,
}
],
yaml_str=yaml_str,
line_number=line_number,
name=name,
dd_version=dd_version,
)
Comment on lines +175 to +187

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this exported to a waveform with single constant tendency again?

Would it be better to have a ConstantWaveform class instead?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, this used to be handled as a derived waveform without dependencies, right? Can you explain why we need this distinction?

else:
waveform = DerivedWaveform(
yaml_str, name, self.config, dd_version=dd_version
Expand Down
Loading