Exemplo n.º 1
0
def main():
    from instamatic.utils import high_precision_timers
    high_precision_timers.enable()  # sleep timers with 1 ms resolution

    # enable faster switching between threads
    sys.setswitchinterval(0.001)  # seconds

    import psutil
    p = psutil.Process(os.getpid())
    p.nice(
        psutil.REALTIME_PRIORITY_CLASS)  # set python process as high priority

    from instamatic import config

    date = datetime.datetime.now().strftime("%Y-%m-%d")
    logfile = config.logs_drc / f"instamatic_{date}.log"

    logging.basicConfig(
        format=
        "%(asctime)s | %(module)s:%(lineno)s | %(levelname)s | %(message)s",
        filename=logfile,
        level=logging.DEBUG)

    logging.captureWarnings(True)
    log = logging.getLogger(__name__)
    log.info("Instamatic-stem.gui started")

    # Work-around for race condition (errors) that occurs when
    # DataCollectionController tries to access them

    from instamatic.gui.gui import MainFrame, DataCollectionController
    from instamatic.camera import camera

    cam = camera.Camera(config.cfg.camera, as_stream=True)

    root = Tk()

    gui = MainFrame(root, cam=cam, modules=MODULES)

    from ..settings import default as settings
    from ..beam_control import BeamCtrl
    beam_ctrl = BeamCtrl(**settings)

    experiment_ctrl = DataCollectionController(ctrl=None,
                                               stream=cam,
                                               beam_ctrl=beam_ctrl,
                                               app=gui.app,
                                               log=log)
    experiment_ctrl.start()

    root.mainloop()
Exemplo n.º 2
0
def main():
    import argparse

    description = """Start instamatic with various functions (see below). If no arguments are given, start the instamatic GUI. The GUI is modular and can be defined using the config system. The GUI can be used to control the microscope and run the experiments. The GUI itself is further described on the GUI page."""

    parser = argparse.ArgumentParser(description=description,
                                     formatter_class=argparse.RawDescriptionHelpFormatter)

    parser.add_argument('-s', '--script',
                        action='store', type=str, dest='script',
                        help='Run the script given')

    parser.add_argument('-n', '--nav',
                        action='store', type=str, dest='nav_file',
                        help='Load the given .nav file')

    parser.add_argument('-a', '--acquire_at_items',
                        action='store_true', dest='acquire_at_items',
                        help='Run the script file `--script` at every point marked with `Acquire` in the nav file `--nav`.')

    parser.add_argument('-l', '--locate',
                        action='store', type=str, dest='locate',
                        help="Locate a requested directory and exit, i.e. `config`, `data`, `scripts`, `base`, 'work`, `logs`")

    parser.add_argument('-o', '--open',
                        action='store', type=str, dest='show',
                        help='Open the requested directory and exit, see `--locate`.')

    parser.add_argument('-i', '--info',
                        action='store_true', dest='info',
                        help='Show info about the current instamatic installation.')

    parser.set_defaults(script=None,
                        acquire_at_items=False,
                        nav_file=None,
                        start_gui=True,
                        locate=None,
                        show=False,
                        info=False,
                        )

    options = parser.parse_args()

    if options.locate:
        locate(options.locate)
        exit()
    if options.show:
        locate(options.show, show=True)
        exit()
    if options.info:
        show_info()
        exit()

    from instamatic.utils import high_precision_timers
    high_precision_timers.enable()  # sleep timers with 1 ms resolution

    # enable faster switching between threads
    sys.setswitchinterval(0.001)  # seconds

    from instamatic import banner
    banner.register_thank_you_message()

    from instamatic import config

    date = datetime.datetime.now().strftime('%Y-%m-%d')
    logfile = config.locations['logs'] / f'instamatic_{date}.log'

    logging.basicConfig(format='%(asctime)s | %(module)s:%(lineno)s | %(levelname)s | %(message)s',
                        filename=logfile,
                        level=logging.DEBUG)

    logging.captureWarnings(True)
    log = logging.getLogger(__name__)
    log.info(f'Instamatic started: {repr(options.__dict__)}')

    from instamatic import TEMController
    ctrl = TEMController.initialize(stream=True)

    if options.nav_file:
        from pyserialem import read_nav_file
        nav_items = read_nav_file(options.nav_file, acquire_only=True)

    if options.acquire_at_items:
        ctrl.run_script_at_items(nav_items=nav_items, script=options.script)
    elif options.script:
        ctrl.run_script(options.script)
    elif options.start_gui:
        from instamatic.gui import start_gui
        start_gui(ctrl, log=log)
Exemplo n.º 3
0
def main():
    import argparse

    description = ""
    parser = argparse.ArgumentParser(
        description=description,
        formatter_class=argparse.RawDescriptionHelpFormatter)

    parser.add_argument("-s",
                        "--script",
                        action="store",
                        type=str,
                        dest="script",
                        help="Run the script given")

    parser.add_argument("-n",
                        "--nav",
                        action="store",
                        type=str,
                        dest="nav_file",
                        help="Load the given .nav file")

    parser.add_argument(
        "-a",
        "--acquire_at_items",
        action="store_true",
        dest="acquire_at_items",
        help=
        "Run the script file `--script` at every point marked with `Acquire` in the nav file `--nav`."
    )

    parser.add_argument(
        "-l",
        "--locate",
        action="store",
        type=str,
        dest="locate",
        help=
        "Locate a requested directory and exit, i.e. `config`, `data`, `scripts`, `base`, 'work`, `logs`"
    )

    parser.add_argument(
        "-o",
        "--open",
        action="store",
        type=str,
        dest="open",
        help="Open the requested directory and exit, see `--locate`.")

    parser.set_defaults(
        script=None,
        acquire_at_items=False,
        nav_file=None,
        start_gui=True,
        locate=None,
        open=False,
    )

    options = parser.parse_args()

    if options.locate:
        locate(options.locate)
        exit()
    if options.open:
        locate(options.open, open=True)
        exit()

    from instamatic.utils import high_precision_timers
    high_precision_timers.enable()  # sleep timers with 1 ms resolution

    # enable faster switching between threads
    sys.setswitchinterval(0.001)  # seconds

    from instamatic import version
    version.register_thank_you_message()

    from instamatic import config

    date = datetime.datetime.now().strftime("%Y-%m-%d")
    logfile = config.logs_drc / f"instamatic_{date}.log"

    logging.basicConfig(
        format=
        "%(asctime)s | %(module)s:%(lineno)s | %(levelname)s | %(message)s",
        filename=logfile,
        level=logging.DEBUG)

    logging.captureWarnings(True)
    log = logging.getLogger(__name__)
    log.info(f"Instamatic started: {repr(options.__dict__)}")

    from instamatic import TEMController
    ctrl = TEMController.initialize(stream=True)

    if options.nav_file:
        from instamatic.serialem import read_nav_file
        nav_items = read_nav_file(options.nav_file, acquire_only=True)

    if options.acquire_at_items:
        ctrl.run_script_at_items(nav_items=nav_items, script=options.script)
    elif options.script:
        ctrl.run_script(options.script)
    elif options.start_gui:
        from instamatic.gui import start_gui
        start_gui(ctrl, log=log)
Exemplo n.º 4
0
import sounddevice as sd
import numpy as np
from math import ceil
from collections import defaultdict

from pprint import pprint

from instamatic.utils import high_precision_timers

high_precision_timers.enable()


def get_device_id(device, hostapi, kind="output"):
    if kind == "output":
        key = "max_output_channels"
    elif key == "input":
        key = "max_input_channels"
    else:
        raise ValueError(
            "Invalid value for 'kind', must be 'input' or 'output'")

    dct = defaultdict(dict)
    for i, api in enumerate(sd.query_hostapis()):
        for dev in api["devices"]:
            dev_name = sd.query_devices(dev)["name"]
            if sd.query_devices(dev)["max_output_channels"]:
                dct[dev_name][i] = dev

    return dct[device][hostapi]