Beispiel #1
0
def vaex_reader(source):
    """
    Read a vaex hdf5 file
    """

    if os.path.isdir(source):

        arrays = {}
        for filename in glob.glob(os.path.join(source, '*')):
            if is_vaex_file(filename):
                logger.info("Reading vaex data from {0}".format(filename))
                ds = vaex.open(filename)
            else:
                logger.info("Not a vaex file: {0}".format(filename))

        # If there are no vaex files, we raise an error, and if there is one
        # then we are done!

        if len(arrays) == 0:
            raise Exception(
                "No vaex files found in directory: {0}".format(source))
        elif len(arrays) == 1:
            label = list(arrays.keys())[0]
            return [Data(array=arrays[label], label=label)]

        # We now check whether all the shapes of the vaex files are the same,
        # and if so, we merge them into a single file.

        labels = sorted(arrays)
        ref_shape = arrays[labels[0]].shape

        for label in labels[1:]:

            if arrays[label].shape != ref_shape:
                break

        else:

            # Since we are here, the shapes of all the vaex files match, so
            # we can construct a higher-dimensional array.

            # Make sure arrays are sorted while constructing array
            array = np.array([arrays[label] for label in labels])

            # We flip the array here on that in most cases we expect that the
            # scan will start at the top of e.g. the body and move downwards.
            array = array[::-1]

            return [Data(array=array, label=dicom_label(source))]

        # If we are here, the shapes of the vaex files didn't match, so we
        # simply return one Data object per vaex file.
        return [Data(array=arrays[label], label=label) for label in labels]

    else:

        ds = vaex.open(source)
        data = [DataVaex(ds)]

    return data
Beispiel #2
0
def main(argv=sys.argv):

    opt, args = parse(argv[1:])

    if opt.verbose:
        logger.setLevel("INFO")

    logger.info("Input arguments: %s", sys.argv)

    # Global keywords for Glue startup.
    kwargs = {'config': opt.config, 'maximized': not opt.nofs}

    if opt.test:
        return run_tests()
    elif opt.restore:
        start_glue(gluefile=args[0], **kwargs)
    elif opt.script:
        execute_script(args[0])
    else:
        has_file = len(args) == 1
        has_files = len(args) > 1
        has_py = has_file and args[0].endswith('.py')
        has_glu = has_file and args[0].endswith('.glu')
        if has_py:
            execute_script(args[0])
        elif has_glu:
            start_glue(gluefile=args[0], **kwargs)
        elif has_file or has_files:
            start_glue(datafiles=args, **kwargs)
        else:
            start_glue(**kwargs)
Beispiel #3
0
def main(argv=sys.argv):

    opt, args = parse(argv[1:])

    if opt.verbose:
        logger.setLevel("INFO")

    logger.info("Input arguments: %s", sys.argv)

    if opt.test:
        return run_tests()
    elif opt.restore:
        start_glue(gluefile=args[0], config=opt.config)
    elif opt.script:
        execute_script(args[0])
    else:
        has_file = len(args) == 1
        has_files = len(args) > 1
        has_py = has_file and args[0].endswith('.py')
        has_glu = has_file and args[0].endswith('.glu')
        if has_py:
            execute_script(args[0])
        elif has_glu:
            start_glue(gluefile=args[0], config=opt.config)
        elif has_file or has_files:
            start_glue(datafiles=args, config=opt.config)
        else:
            start_glue(config=opt.config)
Beispiel #4
0
def main(argv=sys.argv):

    opt, args = parse(argv[1:])

    if opt.verbose:
        logger.setLevel("INFO")

    logger.info("Input arguments: %s", sys.argv)

    if opt.test:
        return run_tests()
    elif opt.restore:
        start_glue(gluefile=args[0], config=opt.config)
    elif opt.script:
        execute_script(args[0])
    else:
        has_file = len(args) == 1
        has_files = len(args) > 1
        has_py = has_file and args[0].endswith('.py')
        has_glu = has_file and args[0].endswith('.glu')
        if has_py:
            execute_script(args[0])
        elif has_glu:
            start_glue(gluefile=args[0], config=opt.config)
        elif has_file or has_files:
            start_glue(datafiles=args, config=opt.config)
        else:
            start_glue(config=opt.config)
Beispiel #5
0
def main(argv=sys.argv):

    opt, args = parse(argv[1:])

    if opt.verbose:
        logger.setLevel("INFO")

    logger.info("Input arguments: %s", sys.argv)

    # Global keywords for Glue startup.
    kwargs = {'config': opt.config,
              'maximized': not opt.nomax}

    if opt.test:
        return run_tests()
    elif opt.restore:
        start_glue(gluefile=args[0], **kwargs)
    elif opt.script:
        execute_script(args[0])
    else:
        has_file = len(args) == 1
        has_files = len(args) > 1
        has_py = has_file and args[0].endswith('.py')
        has_glu = has_file and args[0].endswith('.glu')
        if has_py:
            execute_script(args[0])
        elif has_glu:
            start_glue(gluefile=args[0], **kwargs)
        elif has_file or has_files:
            start_glue(datafiles=args, **kwargs)
        else:
            start_glue(**kwargs)
Beispiel #6
0
    def default_members(self):
        defaults = {}
        for viewer in qt_client.members:
            try:
                defaults[viewer] = viewer._get_default_tools()
            except AttributeError:
                logger.info("could not get default tools for {0}".format(viewer.__name__))
                defaults[viewer] = []

        return defaults
Beispiel #7
0
    def default_members(self):
        defaults = {}
        for viewer in qt_client.members:
            try:
                defaults[viewer] = viewer._get_default_tools()
            except AttributeError:
                logger.info("could not get default tools for {0}".format(viewer.__name__))
                defaults[viewer] = []

        return defaults
Beispiel #8
0
 def __get__(self, instance, type=None):
     # Under certain circumstances, PyQt will try and access these properties
     # while loading the ui file, so we have to be robust to failures.
     # However, we print out a warning if things fail.
     try:
         widget = reduce(getattr, [instance] + self._att)
         return self.getter(widget)
     except Exception:
         logger.info("An error occured when accessing attribute {0} of {1}. Returning None.".format('.'.join(self._att), instance))
         return None
Beispiel #9
0
 def __get__(self, instance, type=None):
     # Under certain circumstances, PyQt will try and access these properties
     # while loading the ui file, so we have to be robust to failures.
     # However, we print out a warning if things fail.
     try:
         widget = reduce(getattr, [instance] + self._att)
         return self.getter(widget)
     except Exception:
         logger.info("An error occured when accessing attribute {0} of {1}. Returning None.".format('.'.join(self._att), instance))
         return None
Beispiel #10
0
def setup():

    from glue.logger import logger
    from glue.config import tool_registry
    from glue.viewers.image.qt import ImageWidget

    from .contour_selection import ContourSelectionTool

    tool_registry.add(ContourSelectionTool, widget_cls=ImageWidget)

    logger.info("Loaded VizieR importer plugin")
Beispiel #11
0
def find_factory(filename, **kwargs):

    from glue.config import data_factory

    # We no longer try the 'default' factory first because we actually need to
    # try all identifiers and select the one to use based on the priority. This
    # allows us to define more specialized loaders take priority over more
    # general ones. For example, a FITS file that is a dendrogram should be
    # loaded as a dendrogram, not a plain FITS file.

    best_priority = None
    valid_formats = []

    # Iterating over the data factory returns the formats sorted by decreasing
    # alphabetical order then by label (alphabetically) in order to be
    # deterministic. This is implemented in DataFactoryRegistry.__iter__.

    for df in data_factory:

        logger.info('Trying data factory {0}'.format(df.label))

        # Once we've found a match, and iterated through the rest of the
        # importers with the same priority, we can exit the loop.
        if best_priority is not None and df.priority < best_priority:
            break

        if df.function is auto_data:
            continue

        try:
            is_format = df.identifier(filename, **kwargs)
        except ImportError:  # dependencies missing
            continue
        except Exception:  # any other issue
            continue

        if is_format:
            valid_formats.append(df)
            best_priority = df.priority

    logger.info('Valid formats: {0}'.format(valid_formats))

    if len(valid_formats) == 0:
        return None
    elif len(valid_formats) > 1:
        labels = ["'{0}'".format(x.label) for x in valid_formats]
        warnings.warn(
            "Multiple data factories matched the input: {0}. Choosing {1}.".
            format(', '.join(labels), labels[0]))

    func = valid_formats[0].function

    return func
Beispiel #12
0
def setup():

    from .qt_widget import QtVizierImporter
    from glue.config import importer

    @importer("Import from VizieR")
    def vizier_importer():
        wi = QtVizierImporter()
        wi.exec_()
        return wi.datasets

    from glue.logger import logger
    logger.info("Loaded VizieR importer plugin")
Beispiel #13
0
def find_factory(filename, **kwargs):

    from glue.config import data_factory

    # We no longer try the 'default' factory first because we actually need to
    # try all identifiers and select the one to use based on the priority. This
    # allows us to define more specialized loaders take priority over more
    # general ones. For example, a FITS file that is a dendrogram should be
    # loaded as a dendrogram, not a plain FITS file.

    best_priority = None
    valid_formats = []

    # Iterating over the data factory returns the formats sorted by decreasing
    # alphabetical order then by label (alphabetically) in order to be
    # deterministic. This is implemented in DataFactoryRegistry.__iter__.

    for df in data_factory:

        logger.info('Trying data factory {0}'.format(df.label))

        # Once we've found a match, and iterated through the rest of the
        # importers with the same priority, we can exit the loop.
        if best_priority is not None and df.priority < best_priority:
            break

        if df.function is auto_data:
            continue

        try:
            is_format = df.identifier(filename, **kwargs)
        except ImportError:  # dependencies missing
            continue
        except Exception:  # any other issue
            continue

        if is_format:
            valid_formats.append(df)
            best_priority = df.priority

    logger.info('Valid formats: {0}'.format(valid_formats))

    if len(valid_formats) == 0:
        return None
    elif len(valid_formats) > 1:
        labels = ["'{0}'".format(x.label) for x in valid_formats]
        warnings.warn("Multiple data factories matched the input: {0}. Choosing {1}.".format(', '.join(labels), labels[0]))

    func = valid_formats[0].function

    return func
    def set_mode(self, init=False):

        log.info("Setting mode={0} with init={1}".format(self.mode, init))

        # do not allow THIS to override "official" toolbar modes: we handle
        # those correctly already
        overwriteable_modes = ('line_panzoom', 'line_identify',
                               'line_select', 'cont_keyboard',
                               'cont_panzoom', 'cont_select',
                               'cont_exclude', 'line_keyboard', '')

        if self.mode.startswith('line'):
            if init:
                log.info("Activating fitter")
                self.spectrum.specfit.clear_all_connections()
                self.spectrum.plotter.activate_interactive_fitter()
                assert self.spectrum.plotter._active_gui is not None
            self.spectrum.specfit.debug = self.spectrum.specfit._debug = True
        elif self.mode.startswith('cont'):
            if init:
                log.info("Activating continuum fitter")
                self.spectrum.baseline.clear_all_connections()
                self.spectrum.plotter.activate_interactive_baseline_fitter(reset_selection=True)
                assert self.spectrum.plotter._active_gui is not None
            self.spectrum.baseline.debug = self.spectrum.baseline._debug = True
        else:
            raise NotImplementedError("Unknown mode: {0}".format(self.mode))

        if self.toolbar.mode in overwriteable_modes:
            self.toolbar.mode = self.mode
        log.info("Setting mode: {0}".format(self.mode))
Beispiel #15
0
def load_plugins(splash=None):

    # Search for plugins installed via entry_points. Basically, any package can
    # define plugins for glue, and needs to define an entry point using the
    # following format:
    #
    # entry_points = """
    # [glue.plugins]
    # webcam_importer=glue_exp.importers.webcam:setup
    # vizier_importer=glue_exp.importers.vizier:setup
    # dataverse_importer=glue_exp.importers.dataverse:setup
    # """
    #
    # where ``setup`` is a function that does whatever is needed to set up the
    # plugin, such as add items to various registries.

    import setuptools
    logger.info("Loading external plugins using "
                "setuptools=={0}".format(setuptools.__version__))

    from glue._plugin_helpers import iter_plugin_entry_points, PluginConfig
    config = PluginConfig.load()

    n_plugins = len(list(iter_plugin_entry_points()))

    for iplugin, item in enumerate(iter_plugin_entry_points()):

        if item.module_name not in _installed_plugins:
            _installed_plugins.add(item.name)

        if item.module_name in _loaded_plugins:
            logger.info("Plugin {0} already loaded".format(item.name))
            continue

        if not config.plugins[item.name]:
            continue

        try:
            function = item.load()
            function()
        except Exception as exc:
            logger.info("Loading plugin {0} failed "
                        "(Exception: {1})".format(item.name, exc))
        else:
            logger.info("Loading plugin {0} succeeded".format(item.name))
            _loaded_plugins.add(item.module_name)

        if splash is not None:
            splash.set_progress(100. * iplugin / float(n_plugins))

    try:
        config.save()
    except Exception as e:
        logger.warn("Failed to load plugin configuration")

    # Reload the settings now that we have loaded plugins, since some plugins
    # may have added some settings. Note that this will not re-read settings
    # that were previously read.
    from glue._settings_helpers import load_settings
    load_settings()
Beispiel #16
0
def load_plugins(splash=None):

    # Search for plugins installed via entry_points. Basically, any package can
    # define plugins for glue, and needs to define an entry point using the
    # following format:
    #
    # entry_points = """
    # [glue.plugins]
    # webcam_importer=glue_exp.importers.webcam:setup
    # vizier_importer=glue_exp.importers.vizier:setup
    # dataverse_importer=glue_exp.importers.dataverse:setup
    # """
    #
    # where ``setup`` is a function that does whatever is needed to set up the
    # plugin, such as add items to various registries.

    import setuptools
    logger.info("Loading external plugins using "
                "setuptools=={0}".format(setuptools.__version__))

    from glue._plugin_helpers import iter_plugin_entry_points, PluginConfig
    config = PluginConfig.load()

    n_plugins = len(list(iter_plugin_entry_points()))

    for iplugin, item in enumerate(iter_plugin_entry_points()):

        if item.module_name not in _installed_plugins:
            _installed_plugins.add(item.name)

        if item.module_name in _loaded_plugins:
            logger.info("Plugin {0} already loaded".format(item.name))
            continue

        if not config.plugins[item.name]:
            continue

        try:
            function = item.load()
            function()
        except Exception as exc:
            logger.info("Loading plugin {0} failed "
                        "(Exception: {1})".format(item.name, exc))
        else:
            logger.info("Loading plugin {0} succeeded".format(item.name))
            _loaded_plugins.add(item.module_name)

        if splash is not None:
            splash.set_progress(100. * iplugin / float(n_plugins))

    try:
        config.save()
    except Exception as e:
        logger.warn("Failed to load plugin configuration")

    # Reload the settings now that we have loaded plugins, since some plugins
    # may have added some settings. Note that this will not re-read settings
    # that were previously read.
    from glue._settings_helpers import load_settings
    load_settings()
Beispiel #17
0
def load_settings():

    from glue.config import settings, CFG_DIR
    settings_cfg = os.path.join(CFG_DIR, 'settings.cfg')

    config = configparser.ConfigParser()
    read = config.read(settings_cfg)

    if len(read) == 0 or not config.has_section('settings'):
        return

    for name, value in config.items('settings'):
        name = name.upper()
        if hasattr(settings, name):
            setattr(settings, name, json.loads(value))
        else:
            logger.info("Unknown setting {0} - skipping".format(name))
Beispiel #18
0
def setup():

    from glue.logger import logger
    try:
        import cv2
    except ImportError:
        logger.info("Could not load webcam importer plugin, since OpenCV is required")
        return

    from glue.config import importer
    from .qt_widget import QtWebcamImporter

    @importer("Import from webcam")
    def webcam_importer():
        wi = QtWebcamImporter()
        wi.exec_()
        return wi.data

    logger.info("Loaded webcam importer plugin")
Beispiel #19
0
def main(argv=sys.argv):

    opt, args = parse(argv[1:])

    if opt.verbose:
        logger.setLevel("INFO")

    if opt.faulthandler:
        import faulthandler
        faulthandler.enable()

    logger.info("Input arguments: %s", sys.argv)

    # Global keywords for Glue startup.
    kwargs = {
        'config': opt.config,
        'maximized': not opt.nomax,
        'auto_merge': opt.auto_merge
    }

    if opt.startup:
        kwargs['startup_actions'] = opt.startup.split(',')

    if opt.test:
        return run_tests()
    elif opt.restore:
        start_glue(gluefile=args[0], **kwargs)
    elif opt.script:
        execute_script(args[0])
    else:
        has_file = len(args) == 1
        has_files = len(args) > 1
        has_py = has_file and args[0].endswith('.py')
        has_glu = has_file and args[0].endswith('.glu')
        if has_py:
            execute_script(args[0])
        elif has_glu:
            start_glue(gluefile=args[0], **kwargs)
        elif has_file or has_files:
            start_glue(datafiles=args, **kwargs)
        else:
            start_glue(**kwargs)
Beispiel #20
0
def load_settings(force=False):
    """
    Load the settings from disk.

    By default, only settings not already defined in memory are read in, but
    by setting ``force=True``, all settings will be read in.
    """

    from glue.config import settings, CFG_DIR
    settings_cfg = os.path.join(CFG_DIR, 'settings.cfg')

    logger.info("Loading settings from {0}".format(settings_cfg))

    config = configparser.ConfigParser()
    read = config.read(settings_cfg)

    if len(read) == 0 or not config.has_section('main'):
        return

    for name, value in config.items('main'):
        name = name.upper()
        if name in settings:
            if settings.is_default(name) or force:
                setattr(settings, name, json.loads(value))
            elif not settings.is_default(name):
                logger.info("Setting {0} already initialized - skipping".format(name))
        else:
            logger.info("Unknown setting {0} - skipping".format(name))
    def set_new_data(self, data, mask=None):

        log.info("Setting new data")
        if data.ndim == 3:
            x_comp_id = data.world_component_ids[0]
            xunit = data.coords.wcs.wcs.cunit[2]
            cubedata = data[self._options_widget.y_att]
            log.info('cubedata shape: {0}'.format(cubedata.shape))
            if mask is not None:
                cubedata = np.ma.masked_array(cubedata, ~mask)
                meandata = cubedata.mean(axis=2).mean(axis=1)
            else:
                meandata = np.nanmean(cubedata, axis=(1,2))
            log.info('meandata shape: {0}'.format(meandata.shape))
            ydata = meandata
            xdata = data[x_comp_id][:,0,0]
            log.info("xdata shape: {0}".format(xdata.shape))
            xdata = u.Quantity(xdata, xunit)
        elif data.ndim == 2:
            raise ValueError("can't handle images")
        elif data.ndim == 1:
            x_comp_id = data.world_component_ids[0]
            y_comp_id = self._options_widget.y_att
            xunit = data.coords.wcs.wcs.cunit[0]
            xdata = u.Quantity(data[x_comp_id], xunit)
            ydata = data[y_comp_id]
        else:
            raise ValueError("Data have {0} dimensions.  Only 1D and 3D data"
                             " are supported".format(data.ndim))

        self._options_widget.x_att = x_comp_id.label
        log.info("Done averaging or loading 1d")

        sp = pyspeckit.Spectrum(data=ydata, xarr=xdata)
        sp.plotter(axis=self.axes, clear=False, color=data.style.color)
        sp.plotter.figure.canvas.manager.toolbar = self.toolbar
        sp.plotter.axis.figure.canvas.mpl_connect('button_press_event',
                                                  self.click_manager)
        self.spectra[data] = sp
        self.spectrum = sp
Beispiel #22
0
def load_settings(force=False):
    """
    Load the settings from disk.

    By default, only settings not already defined in memory are read in, but
    by setting ``force=True``, all settings will be read in.
    """

    from glue.config import settings, CFG_DIR
    settings_cfg = os.path.join(CFG_DIR, 'settings.cfg')

    logger.info("Loading settings from {0}".format(settings_cfg))

    config = configparser.ConfigParser()
    read = config.read(settings_cfg)

    if len(read) == 0 or not config.has_section('main'):
        return

    for name, value in config.items('main'):
        name = name.upper()
        if name in settings:
            if settings.is_default(name) or force:
                setattr(settings, name, json.loads(value))
            elif not settings.is_default(name):
                logger.info(
                    "Setting {0} already initialized - skipping".format(name))
        else:
            logger.info("Unknown setting {0} - skipping".format(name))
Beispiel #23
0
def load_plugins():

    # Search for plugins installed via entry_points. Basically, any package can
    # define plugins for glue, and needs to define an entry point using the
    # following format:
    #
    # entry_points = """
    # [glue.plugins]
    # webcam_importer=glue_exp.importers.webcam:setup
    # vizier_importer=glue_exp.importers.vizier:setup
    # dataverse_importer=glue_exp.importers.dataverse:setup
    # """
    #
    # where ``setup`` is a function that does whatever is needed to set up the
    # plugin, such as add items to various registries.

    import setuptools
    logger.info("Loading external plugins using setuptools=={0}".format(
        setuptools.__version__))

    from glue._plugin_helpers import iter_plugin_entry_points, PluginConfig
    config = PluginConfig.load()

    for item in iter_plugin_entry_points():

        if not item.module_name in _installed_plugins:
            _installed_plugins.add(item.name)

        if item.module_name in _loaded_plugins:
            logger.info("Plugin {0} already loaded".format(item.name))
            continue

        if not config.plugins[item.name]:
            continue

        try:
            function = item.load()
            function()
        except Exception as exc:
            logger.info("Loading plugin {0} failed (Exception: {1})".format(
                item.name, exc))
        else:
            logger.info("Loading plugin {0} succeeded".format(item.name))
            _loaded_plugins.add(item.module_name)

    try:
        config.save()
    except Exception as e:
        logger.warn("Failed to load plugin configuration")
def apply_mode(mode):

    if mode.viewer.toolbar.mode in ('line_select', 'cont_select'):

        assert mode.viewer.spectrum.plotter._active_gui is not None, "No active GUI tool in pyspeckit"

        roi = mode.roi()

        log.info("ROI: {0}".format(roi))

        if isinstance(roi, RectangularROI):
            x1 = roi.xmin
            x2 = roi.xmax
        elif isinstance(roi, XRangeROI):
            x1 = roi.min
            x2 = roi.max
        if x1 > x2:
            x1, x2 = x2, x1

        mode.viewer.spectrum.plotter._active_gui.selectregion(xmin=x1,
                                                              xmax=x2,
                                                              highlight=True)
Beispiel #25
0
def load_plugins():

    # Search for plugins installed via entry_points. Basically, any package can
    # define plugins for glue, and needs to define an entry point using the
    # following format:
    #
    # entry_points = """
    # [glue.plugins]
    # webcam_importer=glue_exp.importers.webcam:setup
    # vizier_importer=glue_exp.importers.vizier:setup
    # dataverse_importer=glue_exp.importers.dataverse:setup
    # """
    #
    # where ``setup`` is a function that does whatever is needed to set up the
    # plugin, such as add items to various registries.

    import setuptools
    logger.info("Loading external plugins using setuptools=={0}".format(setuptools.__version__))

    from glue._plugin_helpers import iter_plugin_entry_points, PluginConfig
    config = PluginConfig.load()

    for item in iter_plugin_entry_points():

        if not item.module_name in _installed_plugins:
            _installed_plugins.add(item.name)

        if item.module_name in _loaded_plugins:
            logger.info("Plugin {0} already loaded".format(item.name))
            continue

        if not config.plugins[item.name]:
            continue

        try:
            function = item.load()
            function()
        except Exception as exc:
            logger.info("Loading plugin {0} failed (Exception: {1})".format(item.name, exc))
        else:
            logger.info("Loading plugin {0} succeeded".format(item.name))
            _loaded_plugins.add(item.module_name)

    try:
        config.save()
    except Exception as e:
        logger.warn("Failed to load plugin configuration")
 def click_manager(self, event):
     """
     Pass events to the appropriate pyspeckit actions
     """
     if self.toolbar.mode in ('line_identify', 'line_select', 'cont_select', 'cont_exclude'):
         log.info("Toolbar: mode={0}".format(self.toolbar.mode))
         if self.mode == 'line_identify':
             self.spectrum.specfit.guesspeakwidth(event)
             self.spectrum.plotter.refresh()
         elif self.mode == 'line_select':
             self.spectrum.specfit._selectregion_interactive(event)
         elif self.mode == 'cont_select':
             self.spectrum.baseline._selectregion_interactive(event)
         elif self.mode == 'cont_exclude':
             self.spectrum.baseline._selectregion_interactive(event, mark_include=False)
         else:
             log.info("Not in line fitter mode, clicks do NOTHING.")
     else:
         log.info("Toolbar: mode={0}".format(self.toolbar.mode))
Beispiel #27
0
def load_plugins(splash=None):

    # Search for plugins installed via entry_points. Basically, any package can
    # define plugins for glue, and needs to define an entry point using the
    # following format:
    #
    # entry_points = """
    # [glue.plugins]
    # webcam_importer=glue_exp.importers.webcam:setup
    # vizier_importer=glue_exp.importers.vizier:setup
    # dataverse_importer=glue_exp.importers.dataverse:setup
    # """
    #
    # where ``setup`` is a function that does whatever is needed to set up the
    # plugin, such as add items to various registries.

    import setuptools
    logger.info("Loading external plugins using "
                "setuptools=={0}".format(setuptools.__version__))

    from glue._plugin_helpers import iter_plugin_entry_points, PluginConfig
    config = PluginConfig.load()

    n_plugins = len(list(iter_plugin_entry_points()))

    for iplugin, item in enumerate(iter_plugin_entry_points()):

        if item.module_name not in _installed_plugins:
            _installed_plugins.add(item.name)

        if item.module_name in _loaded_plugins:
            logger.info("Plugin {0} already loaded".format(item.name))
            continue

        if not config.plugins[item.name]:
            continue

        # We don't use item.load() because that then checks requirements of all
        # the imported packages, which can lead to errors like this one that
        # don't really matter:
        #
        # Exception: (pytest 2.6.0 (/Users/tom/miniconda3/envs/py27/lib/python2.7/site-packages),
        #             Requirement.parse('pytest>=2.8'), set(['astropy']))
        #
        # Just to be clear, this kind of error does indicate that there is an
        # old version of a package in the environment, but this can confuse
        # users as importing astropy directly would work (as setuptools then
        # doesn't do a stringent test of dependency versions). Often this kind
        # of error can occur if there is a conda version of a package and and
        # older pip version.

        try:
            module = import_module(item.module_name)
            function = getattr(module, item.attrs[0])
            function()
        except Exception as exc:
            # Here we check that some of the 'core' plugins load well and
            # raise an actual exception if not.
            if item.module_name in REQUIRED_PLUGINS:
                raise
            else:
                logger.info("Loading plugin {0} failed "
                            "(Exception: {1})".format(item.name, exc))
        else:
            logger.info("Loading plugin {0} succeeded".format(item.name))
            _loaded_plugins.add(item.module_name)

        if splash is not None:
            splash.set_progress(100. * iplugin / float(n_plugins))

    try:
        config.save()
    except Exception as e:
        logger.warn("Failed to load plugin configuration")

    # Reload the settings now that we have loaded plugins, since some plugins
    # may have added some settings. Note that this will not re-read settings
    # that were previously read.
    from glue._settings_helpers import load_settings
    load_settings()
Beispiel #28
0
def load_plugins(splash=None, require_qt_plugins=False):

    # Search for plugins installed via entry_points. Basically, any package can
    # define plugins for glue, and needs to define an entry point using the
    # following format:
    #
    # entry_points = """
    # [glue.plugins]
    # webcam_importer=glue_exp.importers.webcam:setup
    # vizier_importer=glue_exp.importers.vizier:setup
    # dataverse_importer=glue_exp.importers.dataverse:setup
    # """
    #
    # where ``setup`` is a function that does whatever is needed to set up the
    # plugin, such as add items to various registries.

    import setuptools
    logger.info("Loading external plugins using "
                "setuptools=={0}".format(setuptools.__version__))

    from glue._plugin_helpers import iter_plugin_entry_points, PluginConfig
    config = PluginConfig.load()

    n_plugins = len(list(iter_plugin_entry_points()))

    for iplugin, item in enumerate(iter_plugin_entry_points()):

        if item.module_name not in _installed_plugins:
            _installed_plugins.add(item.name)

        if item.module_name in _loaded_plugins:
            logger.info("Plugin {0} already loaded".format(item.name))
            continue

        if not config.plugins[item.name]:
            continue

        # We don't use item.load() because that then checks requirements of all
        # the imported packages, which can lead to errors like this one that
        # don't really matter:
        #
        # Exception: (pytest 2.6.0 (/Users/tom/miniconda3/envs/py27/lib/python2.7/site-packages),
        #             Requirement.parse('pytest>=2.8'), set(['astropy']))
        #
        # Just to be clear, this kind of error does indicate that there is an
        # old version of a package in the environment, but this can confuse
        # users as importing astropy directly would work (as setuptools then
        # doesn't do a stringent test of dependency versions). Often this kind
        # of error can occur if there is a conda version of a package and and
        # older pip version.

        try:
            module = import_module(item.module_name)
            function = getattr(module, item.attrs[0])
            function()
        except Exception as exc:
            # Here we check that some of the 'core' plugins load well and
            # raise an actual exception if not.
            if item.module_name in REQUIRED_PLUGINS:
                raise
            elif item.module_name in REQUIRED_PLUGINS_QT and require_qt_plugins:
                raise
            else:
                logger.info("Loading plugin {0} failed "
                            "(Exception: {1})".format(item.name, exc))
        else:
            logger.info("Loading plugin {0} succeeded".format(item.name))
            _loaded_plugins.add(item.module_name)

        if splash is not None:
            splash.set_progress(100. * iplugin / float(n_plugins))

    try:
        config.save()
    except Exception as e:
        logger.warn("Failed to load plugin configuration")

    # Reload the settings now that we have loaded plugins, since some plugins
    # may have added some settings. Note that this will not re-read settings
    # that were previously read.
    from glue._settings_helpers import load_settings
    load_settings()
Beispiel #29
0
    def receive_message(self, private_key, sender_id, msg_id, mtype, params,
                        extra):

        logger.info(
            'SAMP: received message - sender_id={0} msg_id={1} mtype={2} '
            'params={3} extra={4}'.format(sender_id, msg_id, mtype, params,
                                          extra))

        if mtype.startswith('table.load'):

            if self.table_id_exists(params['table-id']):
                logger.info('SAMP: table with table-id={0} has already '
                            'been read in'.format(params['table-id']))
                return

            logger.info('SAMP: loading table with table-id={0}'.format(
                params['table-id']))

            if mtype == 'table.load.votable':
                data = astropy_tabular_data_votable(params['url'])
            elif mtype == 'table.load.fits':
                data = astropy_tabular_data_fits(params['url'])
            else:
                logger.info('SAMP: unknown format {0}'.format(
                    mtype.split('.')[-1]))
                return

            data.label = params['name']
            data.meta['samp-table-id'] = params['table-id']

            self.data_collection.append(data)

        elif mtype.startswith('image.load'):

            if self.image_id_exists(params['image-id']):
                logger.info('SAMP: image with image-id={0} has already '
                            'been read in'.format(params['image-id']))
                return

            logger.info('SAMP: loading image with image-id={0}'.format(
                params['image-id']))

            if mtype == 'image.load.fits':
                data = fits_reader(params['url'])
            else:
                logger.info('SAMP: unknown format {0}'.format(
                    mtype.split('.')[-1]))
                return

            data.label = params['name']
            data.meta['samp-image-id'] = params['image-id']

            self.data_collection.append(data)

        elif self.state.highlight_is_selection and mtype == 'table.highlight.row':

            data = self.data_from_table_id(params['table-id'])
            len(self.data_collection.subset_groups)

            subset_state = ElementSubsetState(indices=[params['row']],
                                              data=data)

            mode = EditSubsetMode()
            mode.update(self.data_collection, subset_state)

        elif mtype == 'table.select.rowList':

            data = self.data_from_table_id(params['table-id'])
            len(self.data_collection.subset_groups)

            rows = np.asarray(params['row-list'], dtype=int)

            subset_state = ElementSubsetState(indices=rows, data=data)

            mode = EditSubsetMode()
            mode.update(self.data_collection, subset_state)

        elif mtype == 'samp.hub.event.register' or mtype == 'samp.hub.event.unregister':

            self.state.on_client_change()
Beispiel #30
0
from __future__ import absolute_import, division, print_function

from glue.logger import logger

try:
    from dill import dumps, loads
except ImportError:
    logger.info("Dill library not installed. Falling back to cPickle")

from glue.external.six.moves.cPickle import dumps, loads
Beispiel #31
0
def dicom_reader(source):
    """
    Read a DICOM file or a directory with DICOM files
    """

    if os.path.isdir(source):

        # We are dealing with a directory which should contain DICOM files. At
        # this point, we need to check whether the directory contains zero,
        # one, or more DICOM datasets.

        arrays = {}
        for filename in glob.glob(os.path.join(source, '*')):
            if is_dicom_file(filename):
                logger.info("Reading DICOM data from {0}".format(filename))
                ds = pydicom.read_file(filename)
                arrays[dicom_label(filename)] = ds.pixel_array
            else:
                logger.info("Not a DICOM file: {0}".format(filename))

        # If there are no DICOM files, we raise an error, and if there is one
        # then we are done!

        if len(arrays) == 0:
            raise Exception("No DICOM files found in directory: {0}".format(source))
        elif len(arrays) == 1:
            label = list(arrays.keys())[0]
            return [Data(array=arrays[label], label=label)]

        # We now check whether all the shapes of the DICOM files are the same,
        # and if so, we merge them into a single file.

        labels = sorted(arrays)
        ref_shape = arrays[labels[0]].shape

        for label in labels[1:]:

            if arrays[label].shape != ref_shape:
                break

        else:

            # Since we are here, the shapes of all the DICOM files match, so
            # we can construct a higher-dimensional array.

            # Make sure arrays are sorted while constructing array
            array = np.array([arrays[label] for label in labels])

            # We flip the array here on that in most cases we expect that the
            # scan will start at the top of e.g. the body and move downwards.
            array = array[::-1]

            return [Data(array=array, label=dicom_label(source))]

        # If we are here, the shapes of the DICOM files didn't match, so we
        # simply return one Data object per DICOM file.
        return [Data(array=arrays[label], label=label) for label in labels]

    else:

        ds = pydicom.read_file(source)
        data = [Data(array=ds.pixel_array, label=dicom_label(source))]

    return data
Beispiel #32
0
from __future__ import absolute_import, division, print_function

from glue.logger import logger

try:
    from dill import dumps, loads  # noqa
except ImportError:
    logger.info("Dill library not installed. Falling back to cPickle")
    from glue.external.six.moves.cPickle import dumps, loads  # noqa