def select_2coordinates(self, attr_x, attr_y): xat = self.data.domain[attr_x] yat = self.data.domain[attr_y] def extract_col(data, var): nd = Orange.data.Domain([var]) d = self.data.transform(nd) return d.X[:, 0] coorx = extract_col(self.data, xat) coory = extract_col(self.data, yat) lsx = values_to_linspace(coorx) lsy = values_to_linspace(coory) xindex, xnan = index_values_nan(coorx, lsx) yindex, ynan = index_values_nan(coory, lsy) # trick: # https://stackoverflow.com/questions/31878240/numpy-average-of-values-corresponding-to-unique-coordinate-positions coo = np.hstack([xindex.reshape(-1, 1), yindex.reshape(-1, 1)]) sortidx = np.lexsort(coo.T) sorted_coo = coo[sortidx] unqID_mask = np.append(True, np.any(np.diff(sorted_coo, axis=0), axis=1)) ID = unqID_mask.cumsum() - 1 unq_coo = sorted_coo[unqID_mask] unique, counts = np.unique(ID, return_counts=True) pos = 0 bins = [] for size in counts: bins.append(sortidx[pos:pos + size]) pos += size matrix = [] for indices in bins: selection = self.data.X[indices] array = self.calc_table_np(selection) matrix.append(array) table_2_coord = Orange.data.Table.concatenate(matrix, axis=0) with table_2_coord.unlocked(): table_2_coord[:, attr_x] = np.linspace(*lsx)[unq_coo[:, 0]].reshape( -1, 1) table_2_coord[:, attr_y] = np.linspace(*lsy)[unq_coo[:, 1]].reshape( -1, 1) return table_2_coord
def on_done(self, res): self.lsx, self.lsy = res.lsx, res.lsy lsx, lsy = self.lsx, self.lsy d = res.d self.fixed_levels = res.image_values_fixed_levels self.data_points = res.data_points xindex, xnan = index_values_nan(res.coorx, self.lsx) yindex, ynan = index_values_nan(res.coory, self.lsy) self.data_valid_positions = valid = np.logical_not( np.logical_or(xnan, ynan)) invalid_positions = len(d) - np.sum(valid) if invalid_positions: self.parent.Information.not_shown(invalid_positions) imdata = np.ones((lsy[2], lsx[2])) * float("nan") imdata[yindex[valid], xindex[valid]] = d[valid] self.data_values = d self.data_imagepixels = np.vstack((yindex, xindex)).T self.img.setImage(imdata, autoLevels=False) self.update_levels() self.update_color_schema() self.update_legend_visible() # shift centres of the pixels so that the axes are useful shiftx = _shift(lsx) shifty = _shift(lsy) left = lsx[0] - shiftx bottom = lsy[0] - shifty width = (lsx[1] - lsx[0]) + 2 * shiftx height = (lsy[1] - lsy[0]) + 2 * shifty self.img.setRect(QRectF(left, bottom, width, height)) self.refresh_img_selection() self.image_updated.emit()
def select_1coordinate(self, attr): at = self.data.domain[attr] def extract_col(data, var): nd = Orange.data.Domain([var]) d = self.data.transform(nd) return d.X[:, 0] coor = extract_col(self.data, at) ls = values_to_linspace(coor) index, _ = index_values_nan(coor, ls) coo = np.hstack([index.reshape(-1, 1)]) sortidx = np.lexsort(coo.T) sorted_coo = coo[sortidx] unqID_mask = np.append(True, np.any(np.diff(sorted_coo, axis=0), axis=1)) ID = unqID_mask.cumsum() - 1 unq_coo = sorted_coo[unqID_mask] unique, counts = np.unique(ID, return_counts=True) pos = 0 bins = [] for size in counts: bins.append(sortidx[pos:pos + size]) pos += size matrix = [] for indices in bins: selection = self.data.X[indices] array = self.calc_table_np(selection) matrix.append(array) table_1_coord = Orange.data.Table.concatenate(matrix, axis=0) with table_1_coord.unlocked(): table_1_coord[:, attr] = np.linspace(*ls)[unq_coo[:, 0]].reshape(-1, 1) return table_1_coord
def test_index_nan(self): a = np.array([1, 2, 3, np.nan]) v = values_to_linspace(a) iv, nans = index_values_nan(a, v) np.testing.assert_equal(iv[:-1], [0, 1, 2]) np.testing.assert_equal(nans, [0, 0, 0, 1])
def update_view(self): self.parent.Error.image_too_big.clear() self.parent.Information.not_shown.clear() self.img.clear() self.img.setSelection(None) self.legend.set_colors(None) self.lsx = None self.lsy = None self.data_points = None self.data_values = None self.data_imagepixels = None self.data_valid_positions = None if self.data and self.attr_x and self.attr_y: xat = self.data.domain[self.attr_x] yat = self.data.domain[self.attr_y] ndom = Orange.data.Domain([xat, yat]) datam = Orange.data.Table(ndom, self.data) coorx = datam.X[:, 0] coory = datam.X[:, 1] self.data_points = datam.X self.lsx = lsx = values_to_linspace(coorx) self.lsy = lsy = values_to_linspace(coory) if lsx[-1] * lsy[-1] > IMAGE_TOO_BIG: self.parent.Error.image_too_big(lsx[-1], lsy[-1]) return di = {} if self.parent.value_type == 0: # integrals imethod = self.parent.integration_methods[ self.parent.integration_method] if imethod != Integrate.PeakAt: datai = Integrate( methods=imethod, limits=[[self.parent.lowlim, self.parent.highlim]])(self.data) else: datai = Integrate( methods=imethod, limits=[[self.parent.choose, self.parent.choose]])(self.data) if np.any(self.parent.curveplot.selection_group): # curveplot can have a subset of curves on the input> match IDs ind = np.flatnonzero( self.parent.curveplot.selection_group)[0] dind = self.data_ids[self.parent.curveplot.data[ind].id] di = datai.domain.attributes[0].compute_value.draw_info( self.data[dind:dind + 1]) d = datai.X[:, 0] else: dat = self.data.domain[self.parent.attr_value] ndom = Orange.data.Domain([dat]) d = Orange.data.Table(ndom, self.data).X[:, 0] self.refresh_markings(di) xindex, xnan = index_values_nan(coorx, lsx) yindex, ynan = index_values_nan(coory, lsy) self.data_valid_positions = valid = np.logical_not( np.logical_or(xnan, ynan)) invalid_positions = len(d) - np.sum(valid) if invalid_positions: self.parent.Information.not_shown(invalid_positions) imdata = np.ones((lsy[2], lsx[2])) * float("nan") imdata[yindex[valid], xindex[valid]] = d[valid] self.data_values = d self.data_imagepixels = np.vstack((yindex, xindex)).T self.img.setImage(imdata, autoLevels=False) self.update_levels() self.update_color_schema() # shift centres of the pixels so that the axes are useful shiftx = _shift(lsx) shifty = _shift(lsy) left = lsx[0] - shiftx bottom = lsy[0] - shifty width = (lsx[1] - lsx[0]) + 2 * shiftx height = (lsy[1] - lsy[0]) + 2 * shifty self.img.setRect(QRectF(left, bottom, width, height)) self.refresh_img_selection()