def new_data(): label = '{0}[{1}]'.format(label_base, hdu_name) data = Data(label=label) data.coords = coords groups[hdu_name] = data extension_by_shape[shape] = hdu_name return data
def test_load_data_auto_assigns_label(): factory = MagicMock() result = Data(x=[1, 2, 3], label='') factory.return_value = result d = df.load_data('test.fits', factory) factory.assert_called_once_with('test.fits') assert d.label == 'test'
def merge(self, *data, **kwargs): """ Merge two or more datasets into a single dataset. This has the following effects: All components from all datasets are added to the first argument All datasets except the first argument are removed from the collection Any component name conflicts are disambiguated The pixel and world components apart from the first argument are discarded :note: All arguments must have the same shape :param data: One or more :class:`~glue.core.data.Data` instances. :returns: self """ if len(data) < 2: raise ValueError("merge requires 2 or more arguments") shp = data[0].shape for d in data: if d.shape != shp: raise ValueError("All arguments must have the same shape") label = kwargs.get('label', data[0].label) master = Data(label=label) self.append(master) master.coords = data[0].coords for i, d in enumerate(data): if isinstance(d.coords, WCSCoordinates): master.coords = d.coords break # Find ambiguous components (ones which have labels in more than one # dataset from collections import Counter clabel_count = Counter( [c.label for d in data for c in d.visible_components]) for d in data: for c in d.components: if c in master.components: # already present (via a link) continue lbl = c.label if clabel_count[lbl] > 1: lbl = lbl + " [{0}]".format(d.label) c._label = lbl c.parent = master master.add_component(d.get_component(c), c) self.remove(d) return self
def panda_process(indf): """ Build a data set from a table using pandas. This attempts to respect categorical data input by letting pandas.read_csv infer the type """ result = Data() for name, column in indf.iteritems(): if (column.dtype == np.object) | (column.dtype == np.bool): # try to salvage numerical data coerced = column.convert_objects(convert_numeric=True) if (coerced.dtype != column.dtype) and coerced.isnull().mean() < 0.4: c = Component(coerced.values) else: # pandas has a 'special' nan implementation and this doesn't # play well with np.unique c = CategoricalComponent(column.fillna('')) else: c = Component(column.values) # convert header to string - in some cases if the first row contains # numbers, these are cast to numerical types, so we want to change that # here. if not isinstance(name, six.string_types): name = str(name) # strip off leading # name = name.strip() if name.startswith('#'): name = name[1:].strip() result.add_component(c, name) return result
def mosviz_tabular_data(*args, **kwargs): """ Build a data set from a table. We restrict ourselves to tables with 1D columns. All arguments are passed to astropy.table.Table.read(...). """ result = Data() table = astropy_table_read(*args, **kwargs) result.meta = table.meta # Loop through columns and make component list for column_name in table.columns: c = table[column_name] u = c.unit if hasattr(c, 'unit') else c.units if table.masked: # fill array for now try: c = c.filled(fill_value=np.nan) except (ValueError, TypeError): # assigning nan to integer dtype c = c.filled(fill_value=-1) dtype = c.dtype.type if dtype is np.string_ or dtype is np.str_: nc = CategoricalComponent(c, units=u) else: nc = Component.autotyped(c, units=u) result.add_component(nc, column_name) return result
def astropy_tabular_data(*args, **kwargs): """ Build a data set from a table. We restrict ourselves to tables with 1D columns. All arguments are passed to astropy.table.Table.read(...). """ result = Data() with warnings.catch_warnings(): warnings.simplefilter("ignore") table = astropy_table_read(*args, **kwargs) result.meta = table.meta # Loop through columns and make component list for column_name in table.columns: c = table[column_name] u = c.unit if hasattr(c, 'unit') else c.units if hasattr(c, 'mask'): # fill array for now try: c = c.filled(fill_value=np.nan) except (ValueError, TypeError): # assigning nan to integer dtype c = c.filled(fill_value=-1) nc = Component.autotyped(c, units=u) result.add_component(nc, column_name) return result
def _nddata_to_glue_data(ndd, data_label): if ndd.data.ndim != 2: raise ValueError(f'Imviz cannot load this NDData with ndim={ndd.data.ndim}') for attrib in ['data', 'mask', 'uncertainty']: arr = getattr(ndd, attrib) if arr is None: continue comp_label = attrib.upper() cur_label = f'{data_label}[{comp_label}]' cur_data = Data(label=cur_label) cur_data.meta.update(ndd.meta) if ndd.wcs is not None: cur_data.coords = ndd.wcs raw_arr = arr if attrib == 'data': bunit = ndd.unit or '' elif attrib == 'uncertainty': raw_arr = arr.array bunit = arr.unit or '' else: bunit = '' component = Component.autotyped(raw_arr, units=bunit) cur_data.add_component(component=component, label=comp_label) yield cur_data, cur_label
def test_subset_facet(self): # regression test for 335 act = self.app._layer_widget._actions['facet'] self.app.data_collection.append(Data(x=[1, 2, 3])) with patch('glue.dialogs.subset_facet.qt.SubsetFacet.exec_'): act._do_action()
def new_data(suffix=True): if suffix: label = '{0}[{1}]'.format(label_base, hdu_name) else: label = label_base data = Data(label=label) data.coords = coords # We need to be careful here because some header values are special # objects that we should convert to strings for key, value in hdu.header.items(): if (key == 'COMMENT' or key == 'HISTORY'): if key not in data.meta: data.meta[key] = [str(value)] else: data.meta[key].append(str(value)) elif isinstance(value, str) or isinstance(value, (int, float, bool)): data.meta[key] = value else: data.meta[key] = str(value) groups[hdu_name] = data extension_by_shape[shape] = hdu_name return data
def __init__(self, cubeviz_layout): self._cv_layout = cubeviz_layout self._cubes = cubeviz_layout.cubes ui = cubeviz_layout.ui self._overlays = Data('Overlays') # This is a list of overlay objects that are currently displayed self._active_overlays = [] # Maps overlays to the data sets they represent self._overlay_map = {} self._overlay_colorbar_axis = [] self._overlay_image_combo = ui.overlay_image_combo self._overlay_colormap_combo = ui.overlay_colormap_combo self._alpha_slider = ui.alpha_slider self._colormap_index = DEFAULT_GLUE_COLORMAP_INDEX self._overlay_image_combo.addItem("No Overlay") self._overlay_image_combo.currentIndexChanged.connect( self._on_overlay_change) self._overlay_colormap_combo.setCurrentIndex(self._colormap_index) self._overlay_colormap_combo.currentIndexChanged.connect( self._on_colormap_change) self._alpha_slider.valueChanged.connect(self._on_alpha_change)
def test_new_data_defaults(self): from glue.config import qt_client with patch('glue.app.qt.application.pick_class') as pc: pc.return_value = None d2 = Data(x=np.array([[1, 2, 3], [4, 5, 6]])) d1 = Data(x=np.array([1, 2, 3])) self.app.choose_new_data_viewer(data=d1) args, kwargs = pc.call_args assert kwargs['default'] is ScatterWidget self.app.choose_new_data_viewer(data=d2) args, kwargs = pc.call_args assert kwargs['default'] is ImageWidget
def test_image(): data = Data(label="Test Image") comp_a = Component(np.ones((25, 25))) data.add_component(comp_a, 'test_1') comp_b = Component(np.zeros((25, 25))) data.add_component(comp_b, 'test_2') return data
def _add_to_table(app, data, comp_label): """ Creates a mos table instance in the application data collection is none currently exists. Parameters ---------- app : `~jdaviz.app.Application` The Jdaviz application instance. data : array-list The set of data to added as a table (i.g. column) component. comp_label : str The label used to describe the data. Also used as the column header. """ # Add data to the mos viz table object if 'MOS Table' not in app.data_collection: table_data = Data(label='MOS Table') app.add_data(table_data, notify_done=False) mos_table = app.data_collection['MOS Table'] mos_table.add_component(data, comp_label) viewer = app.get_viewer("table-viewer") viewer.add_data(table_data) else: mos_table = app.data_collection['MOS Table'] mos_table.add_component(data, comp_label)
def setup_2d_data(self): d = Data(x=[[1, 2], [3, 4]], y=[[2, 4], [6, 8]]) self.collect.append(d) self.client.add_layer(d) self.client.xatt = d.id['x'] self.client.yatt = d.id['y'] return d
def test_histogram_data(): data = Data(label="Test Data") comp_a = Component(np.random.uniform(size=500)) comp_b = Component(np.random.normal(size=500)) data.add_component(comp_a, 'uniform') data.add_component(comp_b, 'normal') return data
def _ndarray_to_glue_data(arr, data_label): if arr.ndim != 2: raise ValueError(f'Imviz cannot load this array with ndim={arr.ndim}') data = Data(label=data_label) component = Component.autotyped(arr) data.add_component(component=component, label='DATA') yield data, data_label
def load_stacked_sequence(self, raster_data): for window, window_data in raster_data.items(): w_data = Data(label=f"{window.replace(' ', '_')}") w_data.coords = window_data.wcs w_data.add_component(Component(window_data.data), f"{window.replace(' ', '_')}") w_data.style = VisualAttributes(color='#7A617C') self.datasets.append(w_data)
def test_new_data_defaults(self): with patch('glue.app.qt.application.pick_class') as pc: pc.return_value = None d2 = Data(x=np.array([[1, 2, 3], [4, 5, 6]])) d1 = Data(x=np.array([1, 2, 3])) self.app.choose_new_data_viewer(data=d1) args, kwargs = pc.call_args assert kwargs['default'] is ScatterViewer self.app.choose_new_data_viewer(data=d2) args, kwargs = pc.call_args assert kwargs['default'] is ImageViewer pc.reset_mock()
def setup_method(self, method): self.data = Data(x=[1, 2, 3, 2, 2, 3, 1]) figure = MagicMock() self.collect = DataCollection() self.client = HistogramClient(self.collect, figure) self.axes = self.client.axes self.hub = self.collect.hub self.connect()
def setup_method(self, method): self.data = Data(x=[0, 0, 0, 1, 2, 3, 3, 10, 20], y=[-1, -1, -1, -2, -2, -2, -3, -5, -7]) self.subset = self.data.new_subset() self.collect = DataCollection(self.data) self.client = HistogramClient(self.collect, FIGURE) self.axes = self.client.axes FIGURE.canvas.draw = MagicMock() assert FIGURE.canvas.draw.call_count == 0
def setup_class(self): x = +np.arange(2520).reshape((3, 4, 5, 6, 7)) y = -np.arange(2520).reshape((3, 4, 5, 6, 7)) self.data = Data(x=x, y=y, label='Test data') self.x_id, self.y_id = self.data.main_components self.subset_state = self.x_id >= 1200
def test_edit_subset_mode_toolbar(): ga = GlueApplication() dc = ga.data_collection tbar = ga._mode_toolbar edit = ga.session.edit_subset_mode subset_combo = tbar.subset_combo mode_label = tbar._label_subset_mode dc.append(Data(x=[1, 2, 3])) assert combo_labels(subset_combo) == ('None/Create New', ['None/Create New']) assert mode_label.text() == '(the next selection will create a subset)' sg1 = dc.new_subset_group(subset_state=dc[0].id['x'] > 1, label='Subset 1') assert combo_labels(subset_combo) == ('Subset 1', ['Subset 1', 'None/Create New']) assert mode_label.text() == 'Mode:' sg2 = dc.new_subset_group(subset_state=dc[0].id['x'] < 1, label='Subset 2') assert combo_labels(subset_combo) == ('Subset 2', [ 'Subset 1', 'Subset 2', 'None/Create New' ]) assert mode_label.text() == 'Mode:' edit.edit_subset = [sg1, sg2] assert combo_labels(subset_combo) == ('Multiple subsets', [ 'Multiple subsets', 'Subset 1', 'Subset 2', 'None/Create New' ]) assert mode_label.text() == 'Mode:' edit.edit_subset = [sg1] assert combo_labels(subset_combo) == ('Subset 1', [ 'Subset 1', 'Subset 2', 'None/Create New' ]) assert mode_label.text() == 'Mode:' edit.edit_subset = [] assert combo_labels(subset_combo) == ('None/Create New', [ 'Subset 1', 'Subset 2', 'None/Create New' ]) assert mode_label.text() == '(the next selection will create a subset)' edit.edit_subset = [sg2] assert combo_labels(subset_combo) == ('Subset 2', [ 'Subset 1', 'Subset 2', 'None/Create New' ]) assert mode_label.text() == 'Mode:'
def read_header(header): out = [] for stream in header.stream_names: result = Data(label="{stream}_{uid}".format(stream=stream, uid=header.start['uid'])) tbl = header.table(stream, fill=True) for col in tbl.columns: result.add_component(tbl[col], str(col)) out.append(result) return out
def setup_method(self, method): self.data = Data(y=[-1, -1, -1, -2, -2, -2, -3, -5, -7]) self.data.add_component( CategoricalComponent(['a', 'a', 'a', 'b', 'c', 'd', 'd', 'e', 'f']), 'x') self.subset = self.data.new_subset() self.collect = DataCollection(self.data) self.client = HistogramClient(self.collect, FIGURE) self.axes = self.client.axes FIGURE.canvas.draw = MagicMock() assert FIGURE.canvas.draw.call_count == 0
def merge(self, *data, **kwargs): """ Merge two or more datasets into a single dataset. This has the following effects: All components from all datasets are added to the first argument All datasets except the first argument are removed from the collection Any component name conflicts are disambiguated The pixel and world components apart from the first argument are discarded :note: All arguments must have the same shape :param data: One or more :class:`~glue.core.data.Data` instances. :returns: self """ if len(data) < 2: raise ValueError("merge requires 2 or more arguments") shp = data[0].shape for d in data: if d.shape != shp: raise ValueError("All arguments must have the same shape") label = kwargs.get('label', data[0].label) master = Data(label=label) self.append(master) for d in data: skip = d.pixel_component_ids + d.world_component_ids for c in d.components: if c in skip: continue if c in master.components: # already present (via a link) continue taken = [_.label for _ in master.components] lbl = c.label # Special-case 'PRIMARY', rename to data label if lbl == 'PRIMARY': lbl = d.label # First-pass disambiguation, try component_data if lbl in taken: lbl = '%s_%s' % (lbl, d.label) lbl = disambiguate(lbl, taken) c._label = lbl master.add_component(d.get_component(c), c) self.remove(d) return self
def test_multi_tab(self): d = Data(label='hist', x=[[1, 2], [2, 3]]) dc = DataCollection([d]) app = GlueApplication(dc) w1 = app.new_data_viewer(HistogramWidget, data=d) app.new_tab() w2 = app.new_data_viewer(HistogramWidget, data=d) assert app.viewers == ((w1, ), (w2, )) self.check_clone(app)
def load_dendro(file): """ Load a dendrogram saved by the astrodendro package :param file: Path to a dendrogram file :returns: A list of 2 glue Data objects: the original dataset, and dendrogram. """ dg = Dendrogram.load_from(file) structs = np.arange(len(dg)) parent = np.array([ dg[i].parent.idx if dg[i].parent is not None else -1 for i in structs ]) height = np.array([dg[i].height for i in structs]) pk = np.array([dg[i].get_peak(True)[1] for i in structs]) dendro = Data(parent=parent, height=height, peak=pk, label='Dendrogram') im = Data(intensity=dg.data, structure=dg.index_map) im.join_on_key(dendro, 'structure', dendro.pixel_component_ids[0]) return [dendro, im]
def load_sunpy_map(self, sunpy_map): sunpy_map_loaded = sunpy.map.Map(sunpy_map) label = 'sunpy-map-' + sunpy_map_loaded.name data = Data(label=label) data.coords = sunpy_map_loaded.wcs # preferred way, preserves more info in some cases data.meta = sunpy_map_loaded.meta data.add_component(Component(sunpy_map_loaded.data), sunpy_map_loaded.name) data.style = VisualAttributes(color='#FDB813', preferred_cmap=sunpy_map.cmap) self.datasets.append(data)
def test_limits_inf(self): d = Data() x = Component(np.array([[1, 2], [np.infty, 4]])) y = Component(np.array([[2, 4], [-np.infty, 8]])) xid = d.add_component(x, 'x') yid = d.add_component(y, 'y') self.collect.append(d) self.client.add_layer(d) self.client.xatt = xid self.client.yatt = yid assert self.client._visible_limits(0) == (1, 4) assert self.client._visible_limits(1) == (2, 8)
def test_subset_groups_remain_synced_after_restore(self): # regrssion test for 352 d = Data(label='hist', x=[[1, 2], [2, 3]]) dc = DataCollection([d]) dc.new_subset_group() app = GlueApplication(dc) app2 = clone(app) sg = app2.data_collection.subset_groups[0] assert sg.style.parent is sg sg.style.color = '#112233' assert sg.subsets[0].style.color == '#112233'