def send_data(self): data, graph_sel = self.data, self.graph.get_selection() selected_data, ann_data = None, None if data: group_sel = np.zeros(len(data), dtype=int) if len(graph_sel): # we get selection by region ids so we have to map it to points for id, s in zip(self.region_ids, graph_sel): if s == 0: continue id_indices = np.where(self.data_ids == id)[0] group_sel[id_indices] = s else: graph_sel = [0] if np.sum(graph_sel) > 0: selected_data = create_groups_table(data, group_sel, False, "Group") if data is not None: if np.max(graph_sel) > 1: ann_data = create_groups_table(data, group_sel) else: ann_data = create_annotated_table(data, group_sel.astype(bool)) self.output_changed.emit(selected_data) self.Outputs.selected_data.send(selected_data) self.Outputs.annotated_data.send(ann_data)
def commit(self): if self.embedding is not None: names = get_unique_names( [v.name for v in self.data.domain.variables], ["tsne-x", "tsne-y"]) output = embedding = Orange.data.Table.from_numpy( Orange.data.Domain([ ContinuousVariable(names[0]), ContinuousVariable(names[1]) ]), self.embedding) else: output = embedding = None if self.embedding is not None and self.data is not None: domain = self.data.domain domain = Orange.data.Domain( domain.attributes, domain.class_vars, domain.metas + embedding.domain.attributes) output = self.data.transform(domain) output.metas[:, -2:] = embedding.X selection = self.graph.get_selection() if output is not None and len(selection) > 0: selected = create_groups_table(output, self.graph.selection, False, "Group") else: selected = None if self.graph.selection is not None and np.max( self.graph.selection) > 1: annotated = create_groups_table(output, self.graph.selection) else: annotated = create_annotated_table(output, selection) self.Outputs.selected_data.send(selected) self.Outputs.annotated_data.send(annotated)
def update_output(self): if self.data is None: self.Outputs.selected_data.send(None) self.Outputs.annotated_data.send(None) self._set_output_summary(None) return indices = np.zeros(len(self.data), dtype=int) if self.selection is not None and np.any(self.selection): for y in range(self.size_y): for x in range(self.size_x): rows = self.get_member_indices(x, y) indices[rows] = self.selection[x, y] if np.any(indices): sel_data = create_groups_table(self.data, indices, False, "Group") self.Outputs.selected_data.send(sel_data) self._set_output_summary(sel_data) else: self.Outputs.selected_data.send(None) self._set_output_summary(None) if np.max(indices) > 1: annotated = create_groups_table(self.data, indices) else: annotated = create_annotated_table(self.data, np.flatnonzero(indices)) self.Outputs.annotated_data.send(annotated)
def apply(self): data = self.data selected_data = annotated_data = histogram_data = None if self.is_valid: if self.var.is_discrete: group_indices, values = self._get_output_indices_disc() else: group_indices, values = self._get_output_indices_cont() selected = np.nonzero(group_indices)[0] if selected.size: selected_data = create_groups_table( data, group_indices, include_unselected=False, values=values) annotated_data = create_annotated_table(data, selected) if self.var.is_continuous: # annotate with bins hist_indices, hist_values = self._get_histogram_indices() annotated_data = create_groups_table( annotated_data, hist_indices, var_name="Bin", values=hist_values) histogram_data = self._get_histogram_table() summary = len(selected_data) if selected_data else self.info.NoOutput details = format_summary_details(selected_data) if selected_data else "" self.info.set_output_summary(summary, details) self.Outputs.selected_data.send(selected_data) self.Outputs.annotated_data.send(annotated_data) self.Outputs.histogram_data.send(histogram_data)
def commit(self): if self.embedding is not None: names = get_unique_names([v.name for v in self.data.domain.variables], ["mds-x", "mds-y"]) output = embedding = Orange.data.Table.from_numpy( Orange.data.Domain([ContinuousVariable(names[0]), ContinuousVariable(names[1])]), self.embedding ) else: output = embedding = None if self.embedding is not None and self.data is not None: domain = self.data.domain domain = Orange.data.Domain(domain.attributes, domain.class_vars, domain.metas + embedding.domain.attributes) output = self.data.transform(domain) output.metas[:, -2:] = embedding.X selection = self.graph.get_selection() if output is not None and len(selection) > 0: selected = output[selection] else: selected = None if self.graph.selection is not None and np.max(self.graph.selection) > 1: annotated = create_groups_table(output, self.graph.selection) else: annotated = create_annotated_table(output, selection) self.Outputs.selected_data.send(selected) self.Outputs.annotated_data.send(annotated)
def test_create_groups_table_set_values(self): group_indices = random.sample(range(0, len(self.zoo)), 20) selection = np.zeros(len(self.zoo), dtype=np.uint8) selection[group_indices[:10]] = 1 selection[group_indices[10:]] = 2 values = ("this", "that", "rest") table = create_groups_table(self.zoo, selection, values=values) self.assertEqual(tuple(table.domain["Selected"].values), values)
def commit(self): def prepare_components(): if self.placement in [self.Placement.Circular, self.Placement.LDA]: attrs = [a for a in self.model_selected[:]] axes = self.plotdata.axes elif self.placement == self.Placement.PCA: axes = self._pca.components_.T attrs = [a for a in self._pca.orig_domain.attributes] if self.placement != self.Placement.Projection: domain = Domain( [ ContinuousVariable(a.name, compute_value=lambda _: None) for a in attrs ], metas=[StringVariable(name="component")], ) metas = np.array( [[ "{}{}".format(self.Component_name[self.placement], i + 1) for i in range(axes.shape[1]) ]], dtype=object, ).T components = Table(domain, axes.T, metas=metas) components.name = "components" else: components = self.projection return components selected = annotated = components = None if self.data is not None and self.plotdata.data is not None: components = prepare_components() graph = self.graph mask = self.plotdata.valid_mask.astype(int) mask[mask == 1] = (graph.selection if graph.selection is not None else [False * len(mask)]) selection = (np.array([], dtype=np.uint8) if mask is None else np.flatnonzero(mask)) name = self.data.name data = self.plotdata.data if len(selection): selected = data[selection] selected.name = name + ": selected" selected.attributes = self.data.attributes if graph.selection is not None and np.max(graph.selection) > 1: annotated = create_groups_table(data, mask) else: annotated = create_annotated_table(data, selection) annotated.attributes = self.data.attributes annotated.name = name + ": annotated" self.Outputs.selected_data.send(selected) self.Outputs.annotated_data.send(annotated) self.Outputs.components.send(components)
def test_create_groups_table_include_unselected(self): group_indices = random.sample(range(0, len(self.zoo)), 20) selection = np.zeros(len(self.zoo), dtype=np.uint8) selection[group_indices[:10]] = 1 selection[group_indices[10:]] = 2 table = create_groups_table(self.zoo, selection) self.assertEqual( len(SameValue(table.domain["Selected"], "Unselected")(table)), len(self.zoo) - len(group_indices))
def test_create_groups_table_include_unselected(self): group_indices = random.sample(range(0, len(self.zoo)), 20) selection = np.zeros(len(self.zoo), dtype=np.uint8) selection[group_indices[:10]] = 1 selection[group_indices[10:]] = 2 table = create_groups_table(self.zoo, selection) self.assertEqual( len(SameValue(table.domain["Selected"], "Unselected")(table)), len(self.zoo) - len(group_indices) )
def _get_annotated_data(data, group_sel, graph_sel): if data is None: return None if graph_sel is not None and np.max(graph_sel) > 1: return create_groups_table(data, group_sel) else: if group_sel is None: mask = np.full((len(data), ), False) else: mask = np.nonzero(group_sel)[0] return create_annotated_table(data, mask)
def groups_or_annotated_table(data, selection): """ Use either Orange's create_annotated_table (for at most 1 selected class or create_groups_table (for more selected classes) :param data: Orange data table :param selection: classes for selected indices (0 for unselected) :return: Orange data table with an added column """ if len(selection) and np.max(selection) > 1: return create_groups_table(data, selection) else: return create_annotated_table(data, np.flatnonzero(selection))
def commit(self): if self.data: # add Group column (group number) self.Outputs.selected_data.send( create_groups_table(self.image_grid.image_list, self.selection, False, "Group")) # filter out empty cells - keep only indices of cells that contain images # add Selected column (Yes/No if one group, else Unselected or group number) if self.selection is not None and np.max(self.selection) > 1: out_data = create_groups_table( self.image_grid.image_list[self.nonempty], self.selection[self.nonempty]) else: out_data = create_annotated_table( self.image_grid.image_list[self.nonempty], self.selection[self.nonempty]) self.Outputs.data.send(out_data) else: self.Outputs.data.send(None) self.Outputs.selected_data.send(None)
def send_data(self): data, graph_sel = self.data, self.graph.get_selection() selected_data, ann_data = None, None if data: group_sel = np.zeros(len(data), dtype=int) if len(graph_sel): # we get selection by region ids so we have to map it to points for id, s in zip(self.region_ids, graph_sel): if s == 0: continue id_indices = np.where(self.data_ids == id)[0] group_sel[id_indices] = s else: graph_sel = [0] if np.sum(graph_sel) > 0: selected_data = create_groups_table(data, group_sel, False, "Group") if data is not None: if np.max(graph_sel) > 1: ann_data = create_groups_table(data, group_sel) else: ann_data = create_annotated_table(data, group_sel.astype(bool)) self.output_changed.emit(selected_data) self.Outputs.selected_data.send(selected_data) self.Outputs.annotated_data.send(ann_data) # Added by Jean 2020/06/20, output aggdata for future usage agg_data = self.agg_data # type: Optional[np.ndarray] region_ids = self.region_ids # type: Optional[np.ndarray] if agg_data is not None: agg_data = agg_data.reshape(agg_data.shape[0], 1) region_ids = region_ids.reshape(region_ids.shape[0], 1) agg_data = Table.from_numpy(None, agg_data, None, region_ids) self.Outputs.agg_data.send(agg_data)
def apply(self): data = self.data selected_data = annotated_data = histogram_data = None if self.is_valid: if self.var.is_discrete: group_indices, values = self._get_output_indices_disc() else: group_indices, values = self._get_output_indices_cont() hist_indices, hist_values = self._get_histogram_indices() histogram_data = create_groups_table(data, hist_indices, values=hist_values) selected = np.nonzero(group_indices)[0] if selected.size: selected_data = create_groups_table(data, group_indices, include_unselected=False, values=values) annotated_data = create_annotated_table(data, selected) self.Outputs.selected_data.send(selected_data) self.Outputs.annotated_data.send(annotated_data) self.Outputs.histogram_data.send(histogram_data)
def commit(self): if self.data: # add Group column (group number) self.Outputs.selected_data.send( create_groups_table(self.image_grid.image_list, self.selection, False, "Group")) # filter out empty cells - keep indices of cells that contain images # add Selected column # (Yes/No if one group, else Unselected or group number) if self.selection is not None and np.max(self.selection) > 1: out_data = create_groups_table( self.image_grid.image_list[self.nonempty], self.selection[self.nonempty]) else: out_data = create_annotated_table( self.image_grid.image_list[self.nonempty], np.nonzero(self.selection[self.nonempty])) self.Outputs.data.send(out_data) else: self.Outputs.data.send(None) self.Outputs.selected_data.send(None)
def commit(self): def prepare_components(): if self.placement in [self.Placement.Circular, self.Placement.LDA]: attrs = [a for a in self.model_selected[:]] axes = self.plotdata.axes elif self.placement == self.Placement.PCA: axes = self._pca.components_.T attrs = [a for a in self._pca.orig_domain.attributes] if self.placement != self.Placement.Projection: domain = Domain([ContinuousVariable(a.name, compute_value=lambda _: None) for a in attrs], metas=[StringVariable(name='component')]) metas = np.array([["{}{}".format(self.Component_name[self.placement], i + 1) for i in range(axes.shape[1])]], dtype=object).T components = Table(domain, axes.T, metas=metas) components.name = 'components' else: components = self.projection return components selected = annotated = components = None if self.data is not None and self.plotdata.data is not None: components = prepare_components() graph = self.graph mask = self.plotdata.valid_mask.astype(int) mask[mask == 1] = graph.selection if graph.selection is not None \ else [False * len(mask)] selection = np.array([], dtype=np.uint8) if mask is None else np.flatnonzero(mask) name = self.data.name data = self.plotdata.data if len(selection): selected = data[selection] selected.name = name + ": selected" selected.attributes = self.data.attributes if graph.selection is not None and np.max(graph.selection) > 1: annotated = create_groups_table(data, mask) else: annotated = create_annotated_table(data, selection) annotated.attributes = self.data.attributes annotated.name = name + ": annotated" self.Outputs.selected_data.send(selected) self.Outputs.annotated_data.send(annotated) self.Outputs.components.send(components)
def _send_selection(self, data, selection_group, no_group=False): annotated_data = groups_or_annotated_table(data, selection_group) self.Outputs.annotated_data.send(annotated_data) selected = None if data: if no_group and data: # compatibility mode, the output used to lack the group column selection_indices = np.flatnonzero(selection_group) selected = data[selection_indices] else: selected = create_groups_table(data, selection_group, False, "Group") selected = selected if selected else None self.Outputs.selected_data.send(selected if selected else None) return annotated_data, selected
def commit(self): selected = annotated = components = None graph = self.graph if self.data is not None and self.plotdata.validmask is not None: name = self.data.name metas = () + self.data.domain.metas + (self.variable_x, self.variable_y) domain = Domain( attributes=self.data.domain.attributes, class_vars=self.data.domain.class_vars, metas=metas, ) data = self.plotdata.data.transform(domain) validmask = self.plotdata.validmask mask = np.array(validmask, dtype=int) mask[mask == 1] = (graph.selection if graph.selection is not None else [False * len(mask)]) selection = (np.array([], dtype=np.uint8) if mask is None else np.flatnonzero(mask)) if len(selection): selected = data[selection] selected.name = name + ": selected" selected.attributes = self.data.attributes if graph.selection is not None and np.max(graph.selection) > 1: annotated = create_groups_table(data, mask) else: annotated = create_annotated_table(data, selection) annotated.attributes = self.data.attributes annotated.name = name + ": annotated" comp_domain = Domain(self.data.domain.attributes, metas=[StringVariable(name="component")]) metas = np.array([["FreeViz 1"], ["FreeViz 2"]]) components = Table.from_numpy(comp_domain, X=self.plotdata.anchors.T, metas=metas) components.name = name + ": components" self.Outputs.selected_data.send(selected) self.Outputs.annotated_data.send(annotated) self.Outputs.components.send(components)
def commit(self): selected = annotated = components = None if self.data is not None and np.sum(self.valid_data): name = self.data.name domain = self.data.domain metas = domain.metas + (self.variable_x, self.variable_y) domain = Domain(domain.attributes, domain.class_vars, metas) embedding_coords = np.zeros((len(self.data), 2), dtype=np.float) embedding_coords[self.valid_data] = self._embedding_coords data = self.data.transform(domain) data[:, self.variable_x] = embedding_coords[:, 0][:, None] data[:, self.variable_y] = embedding_coords[:, 1][:, None] selection = self.graph.get_selection() if len(selection): selected = data[selection] selected.name = name + ": selected" selected.attributes = self.data.attributes if self.graph.selection is not None and \ np.max(self.graph.selection) > 1: annotated = create_groups_table(data, self.graph.selection) else: annotated = create_annotated_table(data, selection) annotated.attributes = self.data.attributes annotated.name = name + ": annotated" points = self.graph.get_points() comp_domain = Domain(points[:, 2], metas=[StringVariable(name='component')]) metas = np.array([["RX"], ["RY"], ["angle"]]) angle = np.arctan2(np.array(points[:, 1].T, dtype=float), np.array(points[:, 0].T, dtype=float)) components = Table.from_numpy(comp_domain, X=np.row_stack( (points[:, :2].T, angle)), metas=metas) components.name = name + ": components" self.Outputs.selected_data.send(selected) self.Outputs.annotated_data.send(annotated) self.Outputs.components.send(components)
def commit(self): selected = annotated = components = None graph = self.graph if self.plotdata.data is not None: name = self.data.name data = self.plotdata.data mask = self.plotdata.valid_mask.astype(int) mask[mask == 1] = ( graph.selection if graph.selection is not None else [False * len(mask)] ) selection = ( np.array([], dtype=np.uint8) if mask is None else np.flatnonzero(mask) ) if len(selection): selected = data[selection] selected.name = name + ": selected" selected.attributes = self.data.attributes if graph.selection is not None and np.max(graph.selection) > 1: annotated = create_groups_table(data, mask) else: annotated = create_annotated_table(data, selection) annotated.attributes = self.data.attributes annotated.name = name + ": annotated" comp_domain = Domain( self.plotdata.points[:, 2], metas=[StringVariable(name="component")] ) metas = np.array([["RX"], ["RY"], ["angle"]]) angle = np.arctan2( np.array(self.plotdata.points[:, 1].T, dtype=float), np.array(self.plotdata.points[:, 0].T, dtype=float), ) components = Table.from_numpy( comp_domain, X=np.row_stack((self.plotdata.points[:, :2].T, angle)), metas=metas, ) components.name = name + ": components" self.Outputs.selected_data.send(selected) self.Outputs.annotated_data.send(annotated) self.Outputs.components.send(components)
def output_image_selection(self): if not self.data: self.Outputs.selected_data.send(None) self.Outputs.annotated_data.send(None) self.curveplot.set_data(None) return indices = np.flatnonzero(self.imageplot.selection_group) annotated_data = create_groups_table(self.data, self.imageplot.selection_group) if annotated_data is not None: annotated_data.X = self.data.X # workaround for Orange's copying on domain conversio self.Outputs.annotated_data.send(annotated_data) selected = self.data[indices] self.Outputs.selected_data.send(selected if selected else None) if selected: self.curveplot.set_data(selected) else: self.curveplot.set_data(self.data)
def commit(self): selected = annotated = components = None graph = self.graph if self.data is not None and self.plotdata.validmask is not None: name = self.data.name metas = () + self.data.domain.metas + (self.variable_x, self.variable_y) domain = Domain(attributes=self.data.domain.attributes, class_vars=self.data.domain.class_vars, metas=metas) data = self.plotdata.data.transform(domain) validmask = self.plotdata.validmask mask = np.array(validmask, dtype=int) mask[mask == 1] = graph.selection if graph.selection is not None \ else [False * len(mask)] selection = np.array([], dtype=np.uint8) if mask is None else np.flatnonzero(mask) if len(selection): selected = data[selection] selected.name = name + ": selected" selected.attributes = self.data.attributes if graph.selection is not None and np.max(graph.selection) > 1: annotated = create_groups_table(data, mask) else: annotated = create_annotated_table(data, selection) annotated.attributes = self.data.attributes annotated.name = name + ": annotated" comp_domain = Domain( self.data.domain.attributes, metas=[StringVariable(name='component')]) metas = np.array([["FreeViz 1"], ["FreeViz 2"]]) components = Table.from_numpy( comp_domain, X=self.plotdata.anchors.T, metas=metas) components.name = name + ": components" self.Outputs.selected_data.send(selected) self.Outputs.annotated_data.send(annotated) self.Outputs.components.send(components)
def commit(self): selected = annotated = components = None graph = self.graph if self.plotdata.data is not None: name = self.data.name data = self.plotdata.data mask = self.plotdata.valid_mask.astype(int) mask[mask == 1] = graph.selection if graph.selection is not None \ else [False * len(mask)] selection = np.array([], dtype=np.uint8) if mask is None else np.flatnonzero(mask) if len(selection): selected = data[selection] selected.name = name + ": selected" selected.attributes = self.data.attributes if graph.selection is not None and np.max(graph.selection) > 1: annotated = create_groups_table(data, mask) else: annotated = create_annotated_table(data, selection) annotated.attributes = self.data.attributes annotated.name = name + ": annotated" comp_domain = Domain( self.plotdata.points[:, 2], metas=[StringVariable(name='component')]) metas = np.array([["RX"], ["RY"], ["angle"]]) angle = np.arctan2(np.array(self.plotdata.points[:, 1].T, dtype=float), np.array(self.plotdata.points[:, 0].T, dtype=float)) components = Table.from_numpy( comp_domain, X=np.row_stack((self.plotdata.points[:, :2].T, angle)), metas=metas) components.name = name + ": components" self.Outputs.selected_data.send(selected) self.Outputs.annotated_data.send(annotated) self.Outputs.components.send(components)
def send_data(self): selected = None selection = None # TODO: Implement selection for sql data graph = self.graph if isinstance(self.data, SqlTable): selected = self.data elif self.data is not None: selection = graph.get_selection() if len(selection) > 0: selected = self.data[selection] if graph.selection is not None and np.max(graph.selection) > 1: annotated = create_groups_table(self.data, graph.selection) else: annotated = create_annotated_table(self.data, selection) self.Outputs.selected_data.send(selected) self.Outputs.annotated_data.send(annotated) # Store current selection in a setting that is stored in workflow if selection is not None and len(selection): self.selection_group = list( zip(selection, graph.selection[selection])) else: self.selection_group = None
def _get_selected(): if not len(selection): return None return create_groups_table(data, graph.selection, False, "Group")
def _get_annotated(): if graph.selection is not None and np.max(graph.selection) > 1: return create_groups_table(data, graph.selection) else: return create_annotated_table(data, selection)
def _get_selected_data(data, selection, group_sel): return create_groups_table(data, group_sel, False, "Group") \ if len(selection) else None
def _get_annotated_data(data, group_sel, graph_sel): if graph_sel is not None and np.max(graph_sel) > 1: return create_groups_table(data, group_sel) else: return create_annotated_table(data, np.nonzero(group_sel)[0])
def _get_annotated_data(data, selection, group_sel, graph_sel): if graph_sel is not None and np.max(graph_sel) > 1: return create_groups_table(data, group_sel) else: return create_annotated_table(data, selection)
def _get_selected_data(data, selection, group_sel): return create_groups_table(data, group_sel, False, "Group") \ if len(selection) else None
def _get_annotated(): if graph.selection is not None and np.max(graph.selection) > 1: return create_groups_table(data, graph.selection) else: return create_annotated_table(data, selection)
def _get_selected(): if not len(selection): return None return create_groups_table(data, graph.selection, False, "Group")