def test_same_hdr_many_times(hw, RE): source = Stream() fes1 = FromEventStream("start", ("number", ), source, principle=True) fes2 = FromEventStream("event", ("data", "motor"), source, principle=True) out1 = fes1.map(op.add, 1) out2 = fes2.combine_latest(out1, emit_on=0).starmap(op.mul) a = ToEventStream(out1, ("out1", )) b = ToEventStream(out2, ("out2", )) la = a.sink_to_list() lb = b.sink_to_list() L = [] RE.subscribe(lambda *x: L.append(x)) RE(count([hw.motor], md={"number": 5})) for i in range(1, 3): for ll in L: source.emit(ll) for lst in [la, lb]: o1 = [z[0] for z in lst] o2 = ["start", "descriptor", "event", "stop"] * i assert o1 == o2
def test_last_cache(RE, hw): source = Stream() t = FromEventStream("event", ("data", "motor"), source, principle=True) assert t.principle n = ToEventStream(t, ("ct", ), data_key_md={ "ct": { "units": "arb" } }).LastCache() tt = t.sink_to_list() names = n.pluck(0).sink_to_list() docs = n.pluck(1).sink_to_list() RE.subscribe(unstar(source.emit)) RE.subscribe(print) RE(scan([hw.motor], hw.motor, 0, 9, 10)) assert len(docs) == 10 + 3 + 2 assert names[-3] == "descriptor" assert names[-2] == "event" assert tt assert set(names) == {"start", "stop", "event", "descriptor"} assert docs[1]["hints"] == {"analyzer": {"fields": ["ct"]}} assert docs[1]["data_keys"]["ct"]["units"] == "arb" assert docs[-1]["run_start"]
def test_walk_up_partial(): raw = Stream() a_translation = FromEventStream("start", ("time", ), raw, principle=True) b_translation = FromEventStream("event", ("data", "pe1_image"), raw) d = b_translation.zip_latest(a_translation) ddd = ToEventStream(d, ("data", )) dd = d.map(op.truediv) e = ToEventStream(dd, ("data", )) g = nx.DiGraph() walk_to_translation(e, g) att = [] for node, attrs in g.nodes.items(): att.append(attrs["stream"]) s = {ddd, dd, e, d} assert s == set(att) assert {_hash_or_uid(k) for k in s} == set(g.nodes)
def test_build_upstream_node_set(): source = Stream() t = FromEventStream("event", ("data", "motor"), source, principle=True) assert t.principle n = ToEventStream(t, ("ct", ), data_key_md={ "ct": { "units": "arb" } }).LastCache() s = build_upstream_node_set(n) assert len(s) == 3
def test_align_res_dat(RE, hw): a = Stream() b = FromEventStream("event", ("data", "motor"), a, principle=True).map(op.add, 1) c = ToEventStream(b, ("out", )) z = a.AlignEventStreams(c) sl = z.sink_to_list() RE.subscribe(lambda *x: a.emit(x)) osu = RE(scan([hw.img], hw.motor, 0, 10, 10)) for n, d in sl: if n == "start": assert d["original_start_uid"] == osu[0] if n == "event": assert d["data"]["out"] == d["data"]["motor"] + 1
def test_align_interrupted(RE, hw): a = Stream() b = FromEventStream("event", ("data", "img"), a, principle=True).map(op.add, 1) b.sink(print) c = ToEventStream(b, ("out", )) z = move_to_first(a.AlignEventStreams(c)) sl = z.sink_to_list() L = [] RE.subscribe(lambda *x: L.append(x)) RE(count([hw.img])) for nd in L: name, doc = nd # cause an exception if name == "event": doc["data"]["img"] = "hi" try: a.emit((name, doc)) except TypeError: pass assert {"start", "stop"} == set(list(zip(*sl))[0]) # check that buffers are not cleared, yet sl.clear() # If there are elements in the buffer they need to be cleared when all # start docs come in. for nd in L: name, doc = nd # cause an exception if name == "event": doc["data"]["img"] = 1 a.emit((name, doc)) if name == "start": # now buffers should be clear assert not any( [b for n, tb in z.true_buffers.items() for u, b in tb.items()]) assert {"start", "descriptor", "event", "stop"} == set(list(zip(*sl))[0]) # now buffers should be clear (as all docs were emitted) assert not any( [b for n, tb in z.true_buffers.items() for u, b in tb.items()])
def test_to_event_model(RE, hw): source = Stream() t = FromEventStream("event", ("data", "motor"), source, principle=True) assert t.principle n = ToEventStream(t, ("ct", ), data_key_md={"ct": {"units": "arb"}}) tt = t.sink_to_list() p = n.pluck(0).sink_to_list() d = n.pluck(1).sink_to_list() RE.subscribe(unstar(source.emit)) RE.subscribe(print) RE(scan([hw.motor], hw.motor, 0, 9, 10)) assert tt assert set(p) == {"start", "stop", "event", "descriptor"} assert d[1]["hints"] == {"analyzer": {"fields": ["ct"]}} assert d[1]["data_keys"]["ct"]["units"] == "arb" assert d[-1]["run_start"]
def test_no_stop(hw, RE): source = Stream().filter(lambda x: x[0] != "stop") t = FromEventStream("event", ("data", ), source, principle=True) n = ToEventStream(t) p = n.pluck(0).sink_to_list() d = n.pluck(1).sink_to_list() RE.subscribe(unstar(source.emit)) RE.subscribe(print) RE(scan([hw.motor], hw.motor, 0, 9, 10)) RE(scan([hw.motor], hw.motor, 0, 9, 10)) assert set(p) == {"start", "stop", "event", "descriptor"} assert d[1]["hints"] == { "analyzer": { "fields": ["motor", "motor_setpoint"] } } assert d[2]["data"] == {"motor_setpoint": 0, "motor": 0}
def test_align_buffering(RE, hw): zz = {"data": False} a = Stream() b = FromEventStream( "event", ("data", "motor"), a.filter(lambda x: zz["data"]), principle=True, ).map(op.add, 1) c = ToEventStream(b, ("out", )) z = move_to_first(a.AlignEventStreams(c)) sl = z.sink_to_list() RE.subscribe(lambda *x: a.emit(x)) RE(scan([hw.img], hw.motor, 0, 10, 10, md={"hello": "world"})) zz["data"] = True sl.clear() RE(scan([hw.img], hw.motor, 0, 10, 10)) assert "hello" not in sl[0][1]
def test_align_buffering2(RE, hw): a = Stream() d = Stream() b = FromEventStream("event", ("data", "motor"), principle=True, upstream=a).map(op.add, 1) c = ToEventStream(b, ("out", )) z = c.AlignEventStreams(d) names = z.pluck(0).sink_to_list() L = [] RE.subscribe(lambda *x: L.append(x)) RE(scan([hw.img], hw.motor, 0, 10, 10, md={"hello": "world"})) for nd in L: d.emit(nd) print("hi") for nd in L: a.emit(nd) assert all(k in names for k in ["start", "descriptor", "event", "stop"])
def test_align_multi_stream(RE, hw): a = Stream() b = FromEventStream( "event", ("data", "motor"), a, principle=True, event_stream_name="primary", ).map(op.add, 1) c = ToEventStream(b, ("out", )) c.sink(print) z = a.AlignEventStreams(c, event_stream_name="primary") sl = z.sink_to_list() RE.subscribe(lambda *x: a.emit(x)) def one_1d_step(detectors, motor, step): """ Inner loop of a 1D step scan This is the default function for ``per_step`` param in 1D plans. """ yield from checkpoint() yield from abs_set(motor, step, wait=True) yield from trigger_and_read(list(detectors) + [motor], name="dark") return (yield from trigger_and_read(list(detectors) + [motor])) osu = RE(scan([hw.img], hw.motor, 0, 10, 10, per_step=one_1d_step)) assert len(sl) == 10 + 3 for n, d in sl: if n == "start": assert d["original_start_uid"] == osu[0] if n == "event": print(d) assert d["data"]["out"] == d["data"]["motor"] + 1
def test_to_event_model_dict(RE, hw): source = Stream() t = FromEventStream("event", ("data", ), source, principle=True) n = ToEventStream(t) p = n.pluck(0).sink_to_list() d = n.pluck(1).sink_to_list() n.sink(print) RE.subscribe(unstar(source.emit)) RE.subscribe(print) RE(scan([hw.motor], hw.motor, 0, 9, 10)) print(d[1]["hints"]) # AAA assert set(p) == {"start", "stop", "event", "descriptor"} assert d[1]["hints"] == { "analyzer": { "fields": ["motor", "motor_setpoint"] } } assert d[2]["data"] == {"motor_setpoint": 0, "motor": 0} assert d[-1]["run_start"]