def __setitem__(self, key, data): """ Add a dataset to the data collection. This can be either a :class:`~glue.core.data.Data` object, which will then have its label set to the specified key, or another kind of object which will be automatically translated into a :class:`~glue.core.data.Data` object. """ if not isinstance(key, str): raise TypeError("item key should be a string, but got {0}".format( type(key))) if not isinstance(data, Data): handler, preferred = data_translator.get_handler_for(data) data = handler.to_data(data) data._preferred_translation = preferred data.label = key for existing_data in self._data[:]: if existing_data.label == key: self.remove(existing_data) self.append(data)
def parse_data(data, label): # First try new data translation layer from glue.config import data_translator try: handler, preferred = data_translator.get_handler_for(data) except TypeError: pass else: data = handler.to_data(data) data.label = label data._preferred_translation = preferred return [data] # Then try legacy 'qglue_parser' infrastructure for item in qglue_parser: data_class = item.data_class parser = item.parser if isinstance(data, data_class): try: return parser(data, label) except Exception as e: raise ValueError("Invalid format for data '%s'\n\n%s" % (label, e)) raise TypeError("Invalid data description: %s" % data)
def data(self, cls=None): # Grab the user's chosen statistic for collapsing data if hasattr(self.state, 'function'): statistic = self.state.function else: statistic = None data = [] for layer_state in self.state.layers: if hasattr(layer_state, 'layer'): # For raw data, just include the data itself if isinstance(layer_state.layer, BaseData): _class = cls or self.default_class if _class is not None: # If spectrum, collapse via the defined statistic if _class == Spectrum1D: layer_data = layer_state.layer.get_object( cls=_class, statistic=statistic) else: layer_data = layer_state.layer.get_object( cls=_class) data.append(layer_data) # For subsets, make sure to apply the subset mask to the # layer data first elif isinstance(layer_state.layer, Subset): layer_data = layer_state.layer if _class is not None: handler, _ = data_translator.get_handler_for(_class) try: layer_data = handler.to_object(layer_data, statistic=statistic) except IncompatibleAttribute: continue data.append(layer_data) return data
def get_data_from_viewer(self, viewer_reference, data_label=None, cls='default', include_subsets=True): """ Returns each data component currently rendered within a viewer instance. Viewers themselves store a default data type to which the Glue data components are transformed upon retrieval. This can be optionally overridden with the ``cls`` keyword. Notes ----- This is only used in cases where the viewers have been pre-defined in the configuration file. Otherwise, viewers are not stored via reference. Parameters ---------- viewer_reference : str The reference to the viewer defined with the ``reference`` key in the yaml configuration file. data_label : str, optional Optionally provide a label to retrieve a specific data set from the viewer instance. cls : class The class definition the Glue data components get transformed to when retrieved. This requires that a working set of translation functions exist in the ``glue_astronomy`` package. See https://github.com/glue-viz/glue-astronomy for more info. If this is the special string ``'default'``, the ``default_class`` attribute of the viewer referenced by ``viewer_reference`` is used. include_subsets : bool Whether to include subset layer data that exists in the viewer but has not been included in the core data collection object. Returns ------- data : dict A dict of the transformed Glue data objects, indexed to corresponding viewer data labels. """ viewer = self.get_viewer(viewer_reference) cls = viewer.default_class if cls == 'default' else cls if cls is not None and not isclass(cls): raise TypeError( "cls in get_data_from_viewer must be a class, None, or " "the 'default' string.") data = {} # If the viewer also supports collapsing, then grab the user's chosen # statistic for collapsing data if hasattr(viewer.state, 'function'): statistic = viewer.state.function else: statistic = None for layer_state in viewer.state.layers: label = layer_state.layer.label if hasattr(layer_state, 'layer') and \ (data_label is None or label == data_label): # For raw data, just include the data itself if isinstance(layer_state.layer, BaseData): layer_data = layer_state.layer if cls is not None: layer_data = layer_data.get_object(cls=cls, statistic=statistic) # If the shape of the data is 2d, then use CCDData as the # output data type elif len(layer_data.shape) == 2: layer_data = layer_data.get_object(cls=CCDData) data[label] = layer_data # For subsets, make sure to apply the subset mask to the # layer data first elif isinstance(layer_state.layer, Subset): layer_data = layer_state.layer if cls is not None: handler, _ = data_translator.get_handler_for(cls) layer_data = handler.to_object(layer_data, statistic=statistic) data[label] = layer_data # If a data label was provided, return only the data requested if data_label is not None: if data_label in data: data = {data_label: data.get(data_label)} else: data = {} return data