Ejemplo n.º 1
0
def start_dispatcher(host, port, logfile=None):
    """The dispatcher function
    Parameters
    ----------
    logfile : string
        string come from user command. ex --logfile=temp.log
        logfile will be "temp.log". logfile could be empty.
    """
    dispatcher = RemoteDispatcher((host, port))
    if logfile is not None:
        raise ValueError(
            "Parameter 'logfile' is deprecated and will be removed in future releases. "
            "Currently it does not have effect. Call the function with 'logfile=None' "
        )

    def log_writer(name, doc):
        """logger's wrapper function
            This function will be used to fit .subscribe() method.
            It has two arguments as .subscribe expects. Inside, it
            calls logger.* to write doc which is a dict as a str
            into logfile
        """
        if name in ('start', 'stop'):
            logger.info("%s: %r", name, doc)
        else:
            logger.debug("%s: %r", name, doc)

    dispatcher.subscribe(log_writer)  # Subscribe log writer
    dispatcher.start()
Ejemplo n.º 2
0
def start_dispatcher(host, port, logfile):
    """The dispatcher function
    Parameters
    ----------
    logfile : string
        string come from user command. ex --logfile=temp.log
        logfile will be "temp.log". logfile could be empty.
    """
    dispatcher = RemoteDispatcher((host, port))
    if logfile:
        set_handler(file=logfile)

    def log_writer(name, doc):
        """logger's wrapper function
            This function will be used to fit .subscribe() method.
            It has two arguments as .subscribe expects. Inside, it
            calls logger.* to write doc which is a dict as a str
            into logfile
        """
        if name in ('start', 'stop'):
            logger.info("%s: %r", name, doc)
        else:
            logger.debug("%s: %r", name, doc)

    dispatcher.subscribe(log_writer)  # Subscribe log writer
    dispatcher.start()
Ejemplo n.º 3
0
    def make_and_start_dispatcher(queue):
        def put_in_queue(name, doc):
            print('putting ', name, 'in queue')
            queue.put((name, doc))

        d = RemoteDispatcher('127.0.0.1:5568', deserializer=cloudpickle.loads)
        d.subscribe(put_in_queue)
        print("REMOTE IS READY TO START")
        d.loop.call_later(9, d.stop)
        d.start()
Ejemplo n.º 4
0
    def make_and_start_dispatcher(queue):
        def put_in_queue(name, doc):
            print('putting ', name, 'in queue')
            queue.put((name, doc))

        d = RemoteDispatcher('127.0.0.1:5568')
        d.subscribe(put_in_queue)
        print("REMOTE IS READY TO START")
        d.loop.call_later(9, d.stop)
        d.start()
Ejemplo n.º 5
0
    def make_and_start_dispatcher(queue):
        def put_in_queue(name, doc):
            print('putting ', name, 'in queue')
            queue.put((name, doc))

        d = RemoteDispatcher('127.0.0.1:5568')
        d.subscribe('all', put_in_queue)
        print("REMOTE IS READY TO START")
        d._loop.call_later(9, d.stop)
        d.start()
    def make_and_start_dispatcher(queue):
        def put_in_queue(name, doc):
            print("putting ", name, "in queue")
            queue.put((name, doc))

        d = RemoteDispatcher("127.0.0.1:5568")
        d.subscribe(put_in_queue)
        print("REMOTE IS READY TO START")
        d.loop.call_later(9, d.stop)
        d.start()
Ejemplo n.º 7
0
    def run(self):
        def put_in_queue(name, doc):
            print("putting ", name, "in queue")
            self._queue.put((name, doc))

        from bluesky.callbacks.zmq import RemoteDispatcher

        d = RemoteDispatcher("127.0.0.1:5568")
        d.subscribe(put_in_queue)
        print("REMOTE IS READY TO START")
        d.loop.call_later(9, d.stop)
        d.start()
Ejemplo n.º 8
0
def main():
    parser = argparse.ArgumentParser(
        description='Listen for documents over 0MQ and validate Resources.')
    parser.add_argument(
        'proxy_address', type=str,
        help="bluesky-0MQ-proxy out address, given as in localhost:5578")
    parser.add_argument(
        '--emails', required=False, nargs='*',
        help="space-separated list of email addresses")
    args = parser.parse_args()

    log_handler = logging.StreamHandler()  # stderr
    log_handler.setFormatter(LogFormatter())
    logger.setLevel('INFO')
    logger.addHandler(log_handler)

    if args.emails:
        server_name = socket.gethostname()
        smtp_handler = SMTPHandler(
            mailhost='localhost',
            fromaddr=f'Resource Health Check <noreply@{server_name}>',
            toaddrs=args.emails,
            subject=(f'Error report from resource health check on '
                     f'{server_name}')
        )
        smtp_handler.setFormatter(LogFormatter(color=False))
        smtp_handler.setLevel('WARNING')
        # Use QueueHandler in case sending email is slow. LogRecords flow
        # from QueueHandler -> Queue -> QueueListener -> SMTPHandler.
        cleanup_listener = True
        que = queue.Queue()
        queue_handler = QueueHandler(que)
        queue_listener = QueueListener(que, smtp_handler,
                                       respect_handler_level=True)
        logger.addHandler(queue_handler)
        queue_listener.start()
    else:
        cleanup_listener = False

    rr = RunRouter([validator_factory])
    rd = RemoteDispatcher(args.proxy_address)
    rd.subscribe(rr)

    logger.info(f'Listening to {args.proxy_address}')

    try:
        rd.start()  # runs forever
    finally:
        if cleanup_listener:
            queue_listener.stop()
Ejemplo n.º 9
0
class ConsumerThread(QThread):
    documents = Signal([tuple])

    def __init__(self, *args, zmq_address, **kwargs):
        super().__init__(*args, **kwargs)
        self.dispatcher = RemoteDispatcher(zmq_address)

        def callback(name, doc):
            if name == 'start':
                log.debug("New streaming Run: uid=%r", doc['uid'])
            self.documents.emit((name, doc))

        self.dispatcher.subscribe(callback)

    def run(self):
        self.dispatcher.start()
Ejemplo n.º 10
0
class ConsumerThread(QThread):
    documents = Signal([tuple])

    def __init__(self, *args, zmq_address, **kwargs):
        super().__init__(*args, **kwargs)
        self.dispatcher = RemoteDispatcher(zmq_address)

        def callback(name, doc):
            if name == 'start':
                log.debug("New streaming Run: uid=%r", doc['uid'])
            self.documents.emit((name, doc))

        self.dispatcher.subscribe(callback)

    def run(self):
        self.dispatcher.start()
def main():
    from bluesky.callbacks.zmq import Publisher, RemoteDispatcher

    parser = argparse.ArgumentParser(
        description=
        "Listen for unfilled documents over 0MQ and emit filled ones.")
    parser.add_argument(
        "receive_from",
        type=str,
        help="bluesky-0MQ-proxy out address, given as in localhost:5578",
    )
    parser.add_argument(
        "send_to",
        type=str,
        help="bluesky-0MQ-proxy in address, given as in localhost:5578",
    )
    args = parser.parse_args()

    # Data flows through:
    # * RemoteDispatcher (0MQ)
    # * Accumulator (caches until stop doc is received)
    # * EmittingFiller (fills external data)
    # * Publisher (0MQ)

    publisher = Publisher(args.send_to)

    handler_registry = discover_handlers()

    def factory(name, doc):
        filler = EmittingFiller(handler_registry,
                                inplace=False,
                                callback=publisher,
                                coerce="force_numpy")
        accumulator = Accumulator(callback=filler)
        return [accumulator], []

    rr = RunRouter([factory])
    rd = RemoteDispatcher(args.receive_from)
    rd.subscribe(rr)

    print(f"Listening to {args.receive_from}")

    try:
        rd.start()  # runs forever
    except KeyboardInterrupt:
        print("Terminated by user; exiting")
def start_callback():
    from bluesky.callbacks.zmq import RemoteDispatcher
    from SciStreams.config import config as configd

    from SciStreams.interfaces.databroker.databases import databases
    cmsdb = databases['cms:data']
    # db needed to fill events
    stream_buffer = BufferStream(db=cmsdb)
    def callback(*nds):
        nds = stream_buffer(nds)
        stream_input(*nds)
    # get the dispatcher port for bluesky zeromq process
    ipstring = "localhost:{:4d}".format(configd['bluesky']['port'])
    d = RemoteDispatcher(ipstring)
    d.subscribe(callback)
    #d.subscribe(print)

    # when done subscribing things and ready to use:
    d.start()  # runs event loop forever
Ejemplo n.º 13
0
class RunProcess(QRunnable):
    def __init__(self, *args, path, plan_name, axes, **kwargs):
        QObject.__init__(self, *args, **kwargs)

        self.axes = axes
        self.signals = ProcessLine()

    @pyqtSlot()
    def run(self):
        #self.lp = LivePlot(self.device.current.name, x=self.device.offset.name, ax=self.axes, marker='.')

        self.plan = Custom_Plan()
        self.plan.PRINT_CALLBACK = self.signals.signal.emit
        self.plan.AXIS = self.axes

        in_q = mp.Queue()
        out_q = mp.Queue()

        self.p_scan = mp.Process(target=execute_RE,
                                 args=[self.plan, out_q, in_q])
        self.p_scan.start()

        self.plan.NAMES = out_q.get()
        print(self.plan.NAMES)

        self.plan.describe_printers()
        self.plan.describe_plots()
        self.remote()
        self.p_proxy = mp.Process(target=proxy)
        self.p_proxy.start()
        time.sleep(2)
        in_q.put('start scan')

        self.p_scan.join()
        self.terminate()

    def remote(self):
        self.remote_dispatcher = RemoteDispatcher(('localhost', 5568))

        for printer in self.plan.PRINTERS:
            self.remote_dispatcher.subscribe(printer)

        for plot in self.plan.PLOTS:
            self.remote_dispatcher.subscribe(plot)

        # db = Broker.named('light')
        cat = catalog['light']
        self.remote_dispatcher.subscribe(cat.v1.insert)

        t1 = threading.Thread(target=self.remote_dispatcher.start, daemon=True)
        t1.start()

    def terminate(self):
        self.p_scan.terminate()
        self.p_proxy.terminate()

    def close_worker(self):
        self.remote_dispatcher.stop()
        self.terminate()
        print('Dinge')
Ejemplo n.º 14
0
    db,
    td.name,
    # vis=False,
    write_to_disk=False,
    # mask_setting=None
)

# a = LiveImage('pe1_image')
loop = zmq_asyncio.ZMQEventLoop()
install_qt_kicker(loop=loop)


def put_in_queue(nd):
    if nd[0] == 'event':
        nd[1]['data']['pe1_image'] = np.asarray(nd[1]['data']['pe1_image'])
    # if nd[0] == 'event':
    #     db.fill_event(nd[1])
    # print(nd)
    # source.emit(nd)
    a(*nd)
    plt.pause(.1)


disp = RemoteDispatcher('127.0.0.1:5568', loop=loop)
# disp.subscribe(istar(put_in_queue))
# disp.subscribe(a)
disp.subscribe(istar(source.emit))
print("REMOTE IS READY TO START")
# disp._loop.call_later(60, disp.stop)
disp.start()
Ejemplo n.º 15
0
        stream_source = self.descriptor_doc['data_keys'][stream_name]['source']

        if stream_source == 'pizzabox-di-file':
            data = load_trig_trace(raw_data)

        if stream_source == 'pizzabox-adc-file':
            data = load_adc_trace(raw_data)
            stream_offset = f'{stream_name} offset'
            if stream_offset in self.start_doc:
                data.iloc[:, 1] -= self.start_doc[stream_offset]
            stream_gain = f'{stream_name} gain'
            if stream_gain in self.start_doc:
                data.iloc[:, 1] /= 10**self.start_doc[stream_gain]

        if stream_source == 'pizzabox-enc-file':
            data = load_enc_trace(raw_data)
            if dev_name == 'hhm_theta':
                data.iloc[:, 1] = xas.xray.encoder2energy(
                    data['encoder'], 360000,
                    -float(self.start_doc['angle_offset']))
                dev_name = 'energy'

        self._preprocessed_data[dev_name] = data


dispatcher = RemoteDispatcher('localhost:5578', prefix=b'raw')

if __name__ == '__main__':
    dispatcher.subscribe(Interpolator(handler_registry))
    dispatcher.start()
Ejemplo n.º 16
0
)

redis_queue = RedisQueue(
    redis.StrictRedis(host=args.redis_host, port=args.redis_port, db=0))

gpopt = gp_optimizer.GPOptimizer(
    input_space_dimension=3,
    output_space_dimension=1,
    output_number=1,
    index_set_bounds=[[16, 81], [7.5, 60], [340, 460], [0, 1]],
    hyperparameter_bounds=[[0.1, 100], [0.1, 100], [0.1, 100], [0.1, 100]],
)

gpcam_recommender_run_router, _ = recommender_factory(
    gp_optimizer_obj=gpopt,
    independent_keys=[
        "ctrl_Ti", "ctrl_annealing_time", "ctrl_temp", "ctrl_thickness"
    ],
    dependent_keys=["I_00"],
    variance_keys=["I_00_variance"],
    max_count=1,
    queue=redis_queue,
)

zmq_dispatcher.subscribe(gpcam_recommender_run_router)

print(
    f"ADAPTIVE GPCAM CONSUMER LISTENING ON {args.zmq_subscribe_prefix.encode()}"
)
zmq_dispatcher.start()
Ejemplo n.º 17
0
                    '{start[institution]}_'
                    '{start[user_name]}/'
                    '{start[project_name]}/'
                    f'{formatted_date}/'
                    '{start[scan_id]}/'
                    '{start[scan_id]}-'
                    '{start[sample_name]}-'
                    #'{event[data][Beamline Energy_energy]:.2f}eV-'
                ),
                directory=USERDIR,
                flush=True,
                line_terminator='\n')
            serializer('start', start_doc)
            serializer('descriptor', descriptor_doc)
            return [serializer]
        else:
            return []

    return [], [subfactory]


import event_model
import suitcase.jsonl

handler_registry = {
    'AD_TIFF': databroker.assets.handlers.AreaDetectorTiffHandler
}
rr = RunRouter([factory], handler_registry=handler_registry)
rr_token = dispatcher.subscribe(rr)
dispatcher.start()
Ejemplo n.º 18
0
def start_analysis(
    mask_kwargs=None,
    pdf_kwargs=None,
    fq_kwargs=None,
    mask_setting=None,
    save_kwargs=None,
    # pdf_argrelmax_kwargs=None,
    # mean_argrelmax_kwargs=None
):
    """Start analysis pipeline

    Parameters
    ----------
    mask_kwargs : dict
        The kwargs passed to the masking see xpdtools.tools.mask_img
    pdf_kwargs : dict
        The kwargs passed to the pdf generator, see xpdtools.tools.pdf_getter
    fq_kwargs : dict
        The kwargs passed to the fq generator, see xpdtools.tools.fq_getter
    mask_setting : dict
        The setting of the mask
    save_kwargs : dict
        The kwargs passed to the main formatting node (mostly the filename
        template)
    """
    # if pdf_argrelmax_kwargs is None:
    #     pdf_argrelmax_kwargs = {}
    # if mean_argrelmax_kwargs is None:
    #     mean_argrelmax_kwargs = {}
    d = RemoteDispatcher(glbl_dict['proxy_address'])
    install_qt_kicker(
        loop=d.loop)  # This may need to be d._loop depending on tag
    if mask_setting is None:
        mask_setting = {}
    if fq_kwargs is None:
        fq_kwargs = {}
    if pdf_kwargs is None:
        pdf_kwargs = {}
    if mask_kwargs is None:
        mask_kwargs = {}
    if save_kwargs is None:
        save_kwargs = {}
    for a, b in zip(
        [
            mask_kwargs,
            pdf_kwargs,
            fq_kwargs,
            mask_setting,
            save_kwargs,
            # pdf_argrelmax_kwargs,
            # mean_argrelmax_kwargs
        ],
        [
            _mask_kwargs,
            _pdf_kwargs,
            _fq_kwargs,
            _mask_setting,
            _save_kwargs,
            # _pdf_argrelmax_kwargs,
            # _mean_argrelmax_kwargs
        ]):
        if a:
            b.update(a)

    d.subscribe(lambda *x: raw_source.emit(x))
    print('Starting Analysis Server')
    d.start()
Ejemplo n.º 19
0
from pymongo import MongoClient
from bluesky.callbacks.zmq import RemoteDispatcher
from suitcase.mongo_layout1 import Serializer

# This listens to a lightweight (0MQ-based) message bus for Documents
# and inserts them into MongoDB.

dispatcher = RemoteDispatcher('localhost:5578')

client = MongoClient('localhost:27017')
serializer = Serializer(client['mds'], client['assets'])
print(client.address)
dispatcher.subscribe(serializer)
dispatcher.start()
Ejemplo n.º 20
0
import sys

from bluesky.callbacks.zmq import RemoteDispatcher

# listen for 0MQ messages from xf28id2-ca1:5578
zmq_server = "xf28id2-ca1:5578"
zmq_prefix = sys.argv[1].encode()


def echo(name, doc):
    print(f"got a {name} document with 0MQ prefix {zmq_prefix}")
    if name == "start":
        print(f"  start id {doc['uid']}")
    elif name == "descriptor":
        print(f"  start id {doc['run_start']}")
        print(f"  uid {doc['uid']}")
    elif name == "event":
        print(f"  descriptor id {doc['descriptor']}")
        print(f"  uid {doc['uid']}")


d = RemoteDispatcher("xf28id2-ca1:5578", prefix=zmq_prefix)
d.subscribe(echo)
print("ZMQ ECHO CONSUMER IS RUNNING")
d.start()
Ejemplo n.º 21
0
        self.client.lpush("adaptive", json.dumps(value))


redis_queue = RedisQueue(redis.StrictRedis(host="localhost", port=6379, db=0))

step_recommender = recommendations.StepRecommender(1.5)
max_count = 15

recommender_factory, _ = per_start.recommender_factory(
    adaptive_obj=step_recommender,
    independent_keys=["ctrl_temp"],
    dependent_keys=["full_Q"],
    max_count=max_count,
    queue=redis_queue,
)
zmq_dispatcher.subscribe(recommender_factory)


def echo_factory(start_name, start_doc):
    print(
        f"echo_factory called with {start_name}\n{pprint.pformat(start_doc)}\n"
    )

    def echo(name, doc):
        print(
            f"adaptive gpcam consumer received {name}:\n{pprint.pformat(doc)}\n"
        )

    return [echo], []

Ejemplo n.º 22
0
def main():
    rr = RunRouter([factory])
    dispatcher = RemoteDispatcher('localhost:5578')
    dispatcher.subscribe(rr)
    dispatcher.start()
Ejemplo n.º 23
0
d = RemoteDispatcher("127.0.0.1:5678", prefix=b"adaptive")


class RedisQueue:
    "fake just enough of the queue.Queue API on top of redis"

    def __init__(self, client):
        self.client = client

    def put(self, value):
        print(f"pushing {value}")
        self.client.lpush("adaptive", json.dumps(value))


rq = RedisQueue(redis.StrictRedis(host="localhost", port=6379, db=0))

adaptive_obj = recommendations.StepRecommender(1.5)
independent_keys = ["motor"]
dependent_keys = ["det"]
queue = rq
max_count = 15

rr, _ = per_start.recommender_factory(
    adaptive_obj, independent_keys, dependent_keys, max_count=max_count, queue=queue
)


d.subscribe(rr)
print("REMOTE IS READY TO START")
d.start()
Ejemplo n.º 24
0
)

# publish 0MQ messages with xpdan's prefix
zmq_publishing_prefix = b"an"
zmq_analysis_publisher = ZmqPublisher(
    address=("127.0.0.1", 4567), prefix=zmq_publishing_prefix
)


def zmq_publish_from_analysis_factory(start_name, start_doc):
    print(
        f"zmq_publish_from_analysis_factory called with {start_name}:\n{pprint.pformat(start_doc)}\n"
    )

    def zmq_publish_from_analysis(name, doc):
        if name == "start":
            # add batch_count
            print("adding batch_count=1")
            doc["batch_count"] = 1
        print(f"analysis consumer publishing {name}:\n{pprint.pformat(doc)}\n")
        zmq_analysis_publisher(name, doc)

    return [zmq_publish_from_analysis], []


zmq_dispatcher.subscribe(RunRouter(factories=[zmq_publish_from_analysis_factory]))

print(f"NOT XPDAN CONSUMER IS LISTENING ON {zmq_listening_prefix}")
print(f"AND PUBLISHING ON {zmq_publishing_prefix}")
zmq_dispatcher.start()
Ejemplo n.º 25
0
Archivo: disp.py Proyecto: xpdAcq/xpdAn
db = Broker(mds=mds, reg=fs)
td = TemporaryDirectory()
source = conf_main_pipeline(db, td.name,
                            # vis=False,
                            write_to_disk=False,
                            # mask_setting=None
                            )

# a = LiveImage('pe1_image')
loop = zmq_asyncio.ZMQEventLoop()
install_qt_kicker(loop=loop)

def put_in_queue(nd):
    if nd[0] == 'event':
        nd[1]['data']['pe1_image'] = np.asarray(nd[1]['data']['pe1_image'])
    # if nd[0] == 'event':
    #     db.fill_event(nd[1])
    # print(nd)
    # source.emit(nd)
    a(*nd)
    plt.pause(.1)


disp = RemoteDispatcher('127.0.0.1:5568', loop=loop)
# disp.subscribe(istar(put_in_queue))
# disp.subscribe(a)
disp.subscribe(istar(source.emit))
print("REMOTE IS READY TO START")
# disp._loop.call_later(60, disp.stop)
disp.start()