Пример #1
0
def test_call_or_eval_magical_signature_inspection():
    "Test magical signature inspection."
    run = build_simple_run({"motor": [1, 2], "det": [10, 20]})

    def func1(motor, det):
        "Access fields by name."
        return motor + det

    result = call_or_eval({"x": func1}, run, ["primary"])
    assert numpy.array_equal(result["x"], [11, 22])

    def func2(primary):
        "Access a stream by name."
        return primary["motor"]

    result = call_or_eval({"x": func2}, run, ["primary"])
    assert numpy.array_equal(result["x"], [1, 2])

    def func3(does_not_exist):
        "Test a missing variable."
        ...

    with pytest.raises(ValueError, match="Cannot find match for.*"):
        call_or_eval({"x": func3}, run, ["primary"])

    def func4(thing):
        "Test an item in the user-provided namespace."
        return thing

    thing = object()
    result = call_or_eval({"x": func4}, run, [], namespace={"thing": thing})
    assert result["x"] is thing
Пример #2
0
def test_image(FigureView):
    "Test Images with a 2D array."
    run = build_simple_run({"ccd": numpy.random.random((11, 13))})
    model = Images("ccd")
    view = FigureView(model.figure)
    assert not model.figure.axes[0].artists
    model.add_run(run)
    assert model.figure.axes[0].artists
    view.close()
Пример #3
0
def test_image_reduction(FigureView):
    "Test Images with higher-dimensional arrays."
    dims = (5, 7, 11, 13, 17, 19)
    for i in range(3, len(dims)):
        run = build_simple_run({"ccd": numpy.random.random(dims[:i])})
    model = Images("ccd")
    view = FigureView(model.figure)
    model.add_run(run)
    view.close()
Пример #4
0
def test_call_or_eval_errors():
    "Test that various failrue modes raise expected errors."
    run = build_simple_run({"motor": [1, 2], "det": [10, 20]})
    with pytest.raises(ValueError, match=".*callable or string.*"):
        call_or_eval({"x": 1}, run, ["primary"])
    with pytest.raises(ValueError, match=".*parse.*"):
        call_or_eval({"x": "invalid***syntax"}, run, ["primary"])
    with pytest.raises(ValueError, match=".*evaluate.*"):
        call_or_eval({"x": "missing_key"}, run, ["primary"])
Пример #5
0
def test_images():
    "Test AutoImages with a 2D array."
    run = build_simple_run({"ccd": numpy.random.random((11, 13))})
    model = AutoImages()
    view = HeadlessFigures(model.figures)
    assert not model.figures
    model.add_run(run)
    assert len(model.figures) == 1
    assert model.figures[0].axes[0].artists
    view.close()
Пример #6
0
def test_properties(FigureView):
    "Touch various accessors"
    run = build_simple_run({"ccd": numpy.random.random((11, 13))})
    model = Images("c * ccd", namespace={"c": 3})
    view = FigureView(model.figure)
    model.add_run(run)
    assert model.runs[0] is run
    assert model.field == "c * ccd"
    assert dict(model.namespace) == {"c": 3}
    assert model.needs_streams == ("primary", )
    view.close()
Пример #7
0
def test_images_multiple_fields():
    "Test AutoImages with multiple fields with varied shapes."
    run = build_simple_run({
        "ccd": numpy.random.random((11, 13)),
        "ccd2": numpy.random.random((17, 19, 23)),
    })
    model = AutoImages()
    view = HeadlessFigures(model.figures)
    assert not model.figures
    model.add_run(run)
    assert len(model.figures) == 2
    assert model.figures[0].axes[0].artists
    assert model.figures[1].axes[0].artists
    view.close()
Пример #8
0
def test_namespace():
    "Test the contents of a namespace for eval-ing expressions with a run."
    run = build_simple_run({"motor": [1, 2], "det": [10, 20]})
    namespace = construct_namespace(run, ["primary"])

    # Test entities from run....
    # the run itself
    assert eval("run", namespace) is run
    # a stream
    assert numpy.array_equal(eval("primary['motor']", namespace), numpy.array([1, 2]))
    # a field in the 'primary' stream
    assert numpy.array_equal(eval("motor", namespace), numpy.array([1, 2]))
    # numpy, three different ways
    expected = 3 + numpy.log(numpy.array([1, 2]))
    assert numpy.array_equal(eval("3 + log(motor)", namespace), expected)
    assert numpy.array_equal(eval("3 + np.log(motor)", namespace), expected)
    assert numpy.array_equal(eval("3 + numpy.log(motor)", namespace), expected)
Пример #9
0
from bluesky_live.run_builder import build_simple_run
import pytest

from ..plot_builders import Lines
from ..plot_specs import Axes, Figure

# Make some runs to use.
runs = [
    build_simple_run(
        {
            "motor": [1, 2],
            "det": [10, 20],
            "det2": [15, 25]
        },
        metadata={"scan_id": 1 + i},
    ) for i in range(10)
]
MAX_RUNS = 3


def test_pinned(FigureView):
    "Test Lines with 'pinned' and un-pinned runs."
    ys = ["det", "det2"]
    num_ys = len(ys)
    model = Lines("motor", ys, max_runs=MAX_RUNS)
    view = FigureView(model.figure)

    # Add MAX_RUNS and then some more and check that they do get bumped off.
    for run in runs[:5]:
        model.add_run(run)
        assert len(model.runs) <= MAX_RUNS
Пример #10
0
def test_call_or_eval_with_user_namespace():
    "Test that user-injected items in the namespace are found."
    run = build_simple_run({"motor": [1, 2], "det": [10, 20]})
    thing = object()
    result = call_or_eval({"x": "thing"}, run, [], namespace={"thing": thing})
    assert result["x"] is thing
Пример #11
0
def test_collision_of_stream_name_and_field_name():
    "If there is a field named 'primary', the stream should take precedence."
    run = build_simple_run({"primary": [1, 2], "det": [10, 20]})
    namespace = construct_namespace(run, ["primary"])
    assert isinstance(eval("primary", namespace), xarray.Dataset)
    assert isinstance(eval("det", namespace), xarray.DataArray)