def select_valid_oof(y, oof): if isinstance(oof, cupy.ndarray): if len(oof.shape) == 1: idx = cupy.argwhere(~cupy.isnan(oof[:])).ravel() elif len(oof.shape) == 2: idx = cupy.argwhere(~cupy.isnan(oof[:, 0])).ravel() elif len(oof.shape) == 3: idx = cupy.argwhere(~cupy.isnan(oof[:, 0, 0])).ravel() else: raise ValueError(f'Unsupported shape:{oof.shape}') return y.iloc[idx] if hasattr(y, 'iloc') else y[idx], oof[idx] else: return ToolBox.select_valid_oof(y, oof)
def _dense_fit(self, X, strategy, missing_values, fill_value): """Fit the transformer on dense data.""" mask = _get_mask(X, missing_values) # Mean if strategy == "mean": count_missing_values = mask.sum(axis=0) n_elems = X.shape[0] - count_missing_values mean = np.nansum(X, axis=0) mean -= (count_missing_values * missing_values) mean /= n_elems return mean # Median elif strategy == "median": count_missing_values = mask.sum(axis=0) n_elems = X.shape[0] - count_missing_values middle, is_odd = np.divmod(n_elems, 2) is_odd = is_odd.astype(np.bool) middle += count_missing_values X_sorted = X.copy() X_sorted[mask] = np.nan X_sorted = np.sort(X, axis=0) median = np.empty(X.shape[1], dtype=X.dtype) wis_odd = np.argwhere(is_odd).squeeze() wnot_odd = np.argwhere(~is_odd).squeeze() median[wis_odd] = X_sorted[middle[wis_odd], wis_odd] elm1 = X_sorted[middle[wnot_odd] - 1, wnot_odd] elm2 = X_sorted[middle[wnot_odd], wnot_odd] median[wnot_odd] = (elm1 + elm2) / 2. return median # Most frequent elif strategy == "most_frequent": n_features = X.shape[1] most_frequent = cpu_np.empty(n_features, dtype=X.dtype) for i in range(n_features): feature_mask_idxs = np.where(~mask[:, i])[0] values, counts = np.unique(X[feature_mask_idxs, i], return_counts=True) count_max = counts.max() if count_max > 0: value = values[counts == count_max].min() else: value = np.nan most_frequent[i] = value return np.array(most_frequent) # Constant elif strategy == "constant": return np.full(X.shape[1], fill_value, dtype=X.dtype)
def _check_symmetric_relations(a_matrix): """ Check if the argument matrix is symmetric. Raise a value error with details about the offending elements if it is not. This is useful for checking the instantaneously linked nodes have the same link strength. Parameters ---------- a_matrix : 2D numpy array Relationships between nodes at tau = 0. Indexed such that first index is node and second is parent, i.e. node j with parent i has strength a_matrix[j,i] """ # Check it is symmetric if not np.allclose(a_matrix, a_matrix.T, rtol=1e-10, atol=1e-10): # Store the disagreement elements bad_elems = ~np.isclose(a_matrix, a_matrix.T, rtol=1e-10, atol=1e-10) bad_idxs = np.argwhere(bad_elems) error_message = "" for node, parent in bad_idxs: # Check that we haven't already printed about this pair if bad_elems[node, parent]: error_message += \ "Parent {:d} of node {:d}".format(parent, node)+\ " has coefficient {:f}.\n".format(a_matrix[node, parent])+\ "Parent {:d} of node {:d}".format(node, parent)+\ " has coefficient {:f}.\n".format(a_matrix[parent, node]) # Check if we already printed about this one bad_elems[node, parent] = False bad_elems[parent, node] = False raise ValueError("Relationships between nodes at tau=0 are not"+\ " symmetric!\n"+error_message)
def delete_pad(image): orig_h, orig_w = image.shape[:2] mask = xp.argwhere( image[:, :, 3] > 128) # alphaチャンネルの条件、!= 0 や == 255に調整できる (min_y, min_x) = (max(min(mask[:, 0]) - 1, 0), max(min(mask[:, 1]) - 1, 0)) (max_y, max_x) = (min(max(mask[:, 0]) + 1, orig_h), min(max(mask[:, 1]) + 1, orig_w)) return image[min_y:max_y, min_x:max_x]
def getProj(self, obs, center_pixel, rz, z, ry, rx): patch = self.getPatch(obs, center_pixel, torch.zeros_like(rz)) patch = np.round(patch.cpu().numpy(), 5) patch = cp.array(patch) projections = [] size = self.patch_size zs = cp.array(z.numpy()) + cp.array( [(-size / 2 + j) * self.heightmap_resolution for j in range(size)]) zs = zs.reshape((zs.shape[0], 1, 1, zs.shape[1])) zs = zs.repeat(size, 1).repeat(size, 2) c = patch.reshape(patch.shape[0], self.patch_size, self.patch_size, 1).repeat(size, 3) ori_occupancy = c > zs # transform into points point_w_d = cp.argwhere(ori_occupancy) rz_id = (rz.expand(-1, self.num_rz) - self.rzs).abs().argmin(1) ry_id = (ry.expand(-1, self.num_ry) - self.rys).abs().argmin(1) rx_id = (rx.expand(-1, self.num_rx) - self.rxs).abs().argmin(1) dimension = point_w_d[:, 0] point = point_w_d[:, 1:4] rz_id = cp.array(rz_id) ry_id = cp.array(ry_id) rx_id = cp.array(rx_id) mapped_point = self.map[rz_id[dimension], ry_id[dimension], rx_id[dimension], point[:, 0], point[:, 1], point[:, 2]].T rotated_point = mapped_point.T[(cp.logical_and( 0 < mapped_point.T, mapped_point.T < size)).all(1)] d = dimension[(cp.logical_and( 0 < mapped_point.T, mapped_point.T < size)).all(1)].T.astype(int) for i in range(patch.shape[0]): point = rotated_point[d == i].T occupancy = cp.zeros((size, size, size)) if point.shape[0] > 0: occupancy[point[0], point[1], point[2]] = 1 occupancy = median_filter(occupancy, size=2) occupancy = cp.ceil(occupancy) projection = cp.stack( (occupancy.sum(0), occupancy.sum(1), occupancy.sum(2))) projections.append(projection) return torch.tensor(cp.stack(projections)).float().to(self.device)
def __init__( self, n_pre: int, # presynaptic neurons n_post: int, # postsynaptic neurons w_min: float, # min synaptic weight w_max: float, # max synaptic weight d_min: int, # min axonal delay d_max: int, # max axonal delay den: float, # synaptic density inh: float # inhibitory neurons ): self.n_syn = int(n_pre * n_post * den) self.n_inh = int(n_pre * inh) self.n_post = n_post self.d_max = d_max # global synaptic index -> prevent duplicates i_glob = cp.random.choice( n_pre * n_post, self.n_syn, replace=False ).astype(cp.int32) # convert to pre-post self.i_pre = i_glob % n_pre self.i_post = i_glob // n_pre # synaptic weights self.w = cp.random.uniform(w_min, w_max, self.n_syn, dtype=cp.float32) neg = cp.argwhere(self.i_pre < self.n_inh) self.w[neg] *= -1 # axonal delays self.d = cp.random.randint(d_min, d_max+1, self.n_syn, dtype=cp.int) # output matrix self.output = cp.zeros((d_max, n_post), dtype=cp.float32) self.kernel = propagate_delayed()
def sort_states(states,state_count): """Sort the states to place identical states next to each other This function sorts the states stored in a 2d numpy.ndarray so that identical states are placed next to each other. To increase speed, the states are not actually sorted since moving data around in memory can be time consuming, and usually not useful. What is returned is a sorted index and the location of unique states in the sorted index. Args: states ([numpy.ndarray]): A 2d array of compressed states. See ``compress_states`` function. state_count ([int]): The number of states (or number of rows to sort). Returns: edges ([np.ndarray]): Bin edges, or locations of unique states index ([np.ndarray]): Sorted index. This output can be used to actually sort the input states by doing ``states[index]`` """ logger.debug('sort_states') if has_cupy: logger.debug('sort_states: cupy.lexsort') states = cupy.asarray(states[:state_count]).T index = cupy.lexsort(states) states = states[:,index] uniques = cupy.argwhere(cupy.any(states[:,:-1] != states[:,1:],axis=0)) + 1 bin_edges = cupy.zeros((uniques.size+2,),dtype=np.int64) bin_edges[1:-1] = uniques.squeeze() bin_edges[-1] = states.shape[1] bin_edges = cupy.asnumpy(bin_edges) index = cupy.asnumpy(index) else: logger.debug('sort_states: tensorstate._lex_sort') bin_edges,index = ts._lex_sort(states,state_count) return bin_edges,index
def RenderingUserViewLF_AllinOne5K(LF=None, LFDisparity=None, FB=None, viewpoint=None, DIR=None): sphereW = Params.WIDTH sphereH = Params.HEIGHT CENTERx = viewpoint.lon CENTERy = viewpoint.lat # output view is 3:4 ratio new_imgW = cp.floor(viewpoint.diag * 4 / 5 + 0.5) new_imgH = cp.floor(viewpoint.diag * 3 / 5 + 0.5) new_imgW = int(new_imgW) new_imgH = int(new_imgH) OutView = cp.zeros((new_imgH, new_imgW, 3)) TYwarp, TXwarp = cp.mgrid[0:new_imgH, 0:new_imgW] TX = TXwarp TY = TYwarp TX = (TX - 0.5 - new_imgW / 2) TY = (TY - 0.5 - new_imgH / 2) #의심 TX = TX + 1 TY = TY + 1 r = (viewpoint.diag / 2) / cp.tan(viewpoint.fov / 2) R = cp.sqrt(TY**2 + r**2) # Calculate LF_n ANGy = cp.arctan(-TY / r) ANGy = ANGy + CENTERy if (FB == 1): ANGn = cp.cos(ANGy) * cp.arctan(TX / r) ANGn = ANGn + CENTERx Pn = (Params.LFU_W / 2 - viewpoint.pos_y ) * cp.tan(ANGn) + viewpoint.pos_x + (3 * Params.LFU_W / 2) elif (FB == 2): ANGn = cp.cos(ANGy) * cp.arctan(-TX / r) ANGn = ANGn - CENTERx Pn = (Params.LFU_W / 2 + viewpoint.pos_y ) * cp.tan(ANGn) + viewpoint.pos_x + (3 * Params.LFU_W / 2) X = cp.sin(ANGy) * R Y = -cp.cos(ANGy) * R Z = TX ANGx = cp.arctan2(Z, -Y) RZY = cp.sqrt(Z**2 + Y**2) ANGy = cp.arctan(X / RZY) #or ANGy = atan2(X, RZY); RATIO = 1 ANGy = ANGy * RATIO ANGx = ANGx + CENTERx ANGx[abs(ANGy) > pi / 2] = ANGx[abs(ANGy) > pi / 2] + pi ANGx[ANGx > pi] = ANGx[ANGx > pi] - 2 * pi ANGy[ANGy > pi / 2] = pi / 2 - (ANGy[ANGy > pi / 2] - pi / 2) ANGy[ANGy < -pi / 2] = -pi / 2 + (ANGy[ANGy < -pi / 2] + pi / 2) Px = (ANGx + pi) / (2 * pi) * sphereW + 0.5 Py = ((-ANGy) + pi / 2) / pi * sphereH + 0.5 if (DIR == 2): Px = Px + Params.WIDTH / 4 elif (DIR == 3): Px = Px + Params.WIDTH / 2 elif (DIR == 4): Px = Px - Params.WIDTH / 4 Px[Px < 1] = Px[Px < 1] + Params.WIDTH Px[Px > Params.WIDTH] = Px[Px > Params.WIDTH] - Params.WIDTH INDxx = cp.argwhere(Px < 1) Px[INDxx] = Px[INDxx] + sphereW Pn0 = cp.floor(Pn) Pn1 = cp.ceil(Pn) Pnr = Pn - Pn0 Px0 = cp.floor(Px) Px1 = cp.ceil(Px) Pxr = Px - Px0 Py0 = cp.floor(Py) Py1 = cp.ceil(Py) Pyr = Py - Py0 Pnr = cp.rint((Pnr * 10000)) Pnr = Pnr / 10000 Pxr = cp.rint((Pxr * 10000)) Pxr = Pxr / 10000 Pyr = cp.rint((Pyr * 10000)) Pyr = Pyr / 10000 #210->012 rgb #cv2 사용 안하면 그대로 OutView[:, :, 2] = inter8_mat5K(LF, Pnr, Pn0, Pn1, Pxr, Px0, Px1, Pyr, Py0, Py1, 1) OutView[:, :, 1] = inter8_mat5K(LF, Pnr, Pn0, Pn1, Pxr, Px0, Px1, Pyr, Py0, Py1, 2) OutView[:, :, 0] = inter8_mat5K(LF, Pnr, Pn0, Pn1, Pxr, Px0, Px1, Pyr, Py0, Py1, 3) OutFlow = inter8_mat_flow5K(LFDisparity, Pnr, Pn0, Pn1, Pxr, Px0, Px1, Pyr, Py0, Py1) Py = cp.pad(Py, [(1, 1), (0, 0)], mode='edge') Py = cp.ceil((Py * 10000)) Py = Py / 10000 My = 2 / (Py[2:cp.size(Py, 0), :] - Py[0:(cp.size(Py, 0) - 2), :]) My[0, :] = My[0, :] / 2 My[cp.size(My, 0) - 1, :] = My[cp.size(My, 0) - 1, :] / 2 OutFlow = My * OutFlow return OutView, OutFlow
def prominent_peaks(img, min_xdistance=1, min_ydistance=1, threshold=None, num_peaks=cp.inf): """Return peaks with non-maximum suppression. Identifies most prominent features separated by certain distances. Non-maximum suppression with different sizes is applied separately in the first and second dimension of the image to identify peaks. Parameters ---------- image : (M, N) ndarray Input image. min_xdistance : int Minimum distance separating features in the x dimension. min_ydistance : int Minimum distance separating features in the y dimension. threshold : float Minimum intensity of peaks. Default is `0.5 * max(image)`. num_peaks : int Maximum number of peaks. When the number of peaks exceeds `num_peaks`, return `num_peaks` coordinates based on peak intensity. Returns ------- intensity, xcoords, ycoords : tuple of array Peak intensity values, x and y indices. Notes ----- Modified from https://github.com/mritools/cupyimg _prominent_peaks method """ t00 = time.time() #img = image.copy() rows, cols = img.shape if threshold is None: threshold = 0.5 * cp.max(img) ycoords_size = 2 * min_ydistance + 1 xcoords_size = 2 * min_xdistance + 1 t0 = time.time() img_max = ndi.maximum_filter(img, size=(ycoords_size, xcoords_size), mode="constant", cval=0) te = (time.time() - t0) * 1e3 logger.debug(f"Maxfilter: {te:2.2f} ms") t0 = time.time() mask = img == img_max img *= mask mask = img > threshold te = (time.time() - t0) * 1e3 logger.debug(f"bitbash: {te:2.2f} ms") t0 = time.time() # Find array (x,y) indexes corresponding to max pixels peak_idxs = cp.argwhere(mask) # Find corresponding maximum values peak_vals = img[peak_idxs[:, 0], peak_idxs[:, 1]] # Sort peak values low to high ## Sort the list of peaks by intensity, not left-right, so larger peaks ## in Hough space cannot be arbitrarily suppressed by smaller neighbors val_sort_idx = cp.argsort(peak_vals)[::-1] # Return (x,y) coordinates corresponding to sorted max pixels coords = peak_idxs[val_sort_idx] te = (time.time() - t0) * 1e3 logger.debug(f"coord search: {te:2.2f} ms") t0 = time.time() img_peaks = [] ycoords_peaks = [] xcoords_peaks = [] # relative coordinate grid for local neighbourhood suppression ycoords_ext, xcoords_ext = cp.mgrid[-min_ydistance:min_ydistance + 1, -min_xdistance:min_xdistance + 1] for ycoords_idx, xcoords_idx in coords: accum = img_max[ycoords_idx, xcoords_idx] if accum > threshold: # absolute coordinate grid for local neighbourhood suppression ycoords_nh = ycoords_idx + ycoords_ext xcoords_nh = xcoords_idx + xcoords_ext # no reflection for distance neighbourhood ycoords_in = cp.logical_and(ycoords_nh > 0, ycoords_nh < rows) ycoords_nh = ycoords_nh[ycoords_in] xcoords_nh = xcoords_nh[ycoords_in] # reflect xcoords and assume xcoords are continuous, # e.g. for angles: # (..., 88, 89, -90, -89, ..., 89, -90, -89, ...) xcoords_low = xcoords_nh < 0 ycoords_nh[xcoords_low] = rows - ycoords_nh[xcoords_low] xcoords_nh[xcoords_low] += cols xcoords_high = xcoords_nh >= cols ycoords_nh[xcoords_high] = rows - ycoords_nh[xcoords_high] xcoords_nh[xcoords_high] -= cols # suppress neighbourhood img_max[ycoords_nh, xcoords_nh] = 0 # add current feature to peaks img_peaks.append(accum) ycoords_peaks.append(ycoords_idx) xcoords_peaks.append(xcoords_idx) img_peaks = cp.array(img_peaks) ycoords_peaks = cp.array(ycoords_peaks) xcoords_peaks = cp.array(xcoords_peaks) te = (time.time() - t0) * 1e3 logger.debug(f"crazyloop: {te:2.2f} ms") if num_peaks < len(img_peaks): idx_maxsort = cp.argsort(img_peaks)[::-1][:num_peaks] img_peaks = img_peaks[idx_maxsort] ycoords_peaks = ycoords_peaks[idx_maxsort] xcoords_peaks = xcoords_peaks[idx_maxsort] te = (time.time() - t0) * 1e3 logger.debug(f"prominent_peaks total: {te:2.2f} ms") return img_peaks, xcoords_peaks, ycoords_peaks
#!/usr/bin/env python import cupy as cp import cupyx.scipy.ndimage import numpy as np if __name__ == "__main__": data = cp.random.rand(10, 10) data[1, 2] = 30 data[4, 5] = 60 data[7, 8] = 90 threshold = 1.0 # Find maxes maxes = cupyx.scipy.ndimage.maximum_filter(data, size=(5, 5)) # Find peaks peaks = cp.logical_and(maxes == data, data >= threshold) with np.printoptions(linewidth=200): print(maxes) print(peaks) print(cp.argwhere(peaks))
def FilterData(matrizOnibusCpu, matrizLinhasCpu, busIdList, lineIdList, CONFIGS, logging): distanceTolerance = float( CONFIGS['default_correction_method']['distanceTolerance']) detectionPercentage = float( CONFIGS['default_correction_method']['detectionPercentage']) busStepSize = int(CONFIGS['default_correction_method']['busStepSize']) lineStepSize = int(CONFIGS['default_correction_method']['lineStepSize']) matrizOnibus = cp.asarray(matrizOnibusCpu) matrizLinhas = cp.asarray(matrizLinhasCpu) busesList = cp.array_split( matrizOnibus, int(matrizOnibus.shape[0] / busStepSize if matrizOnibus.shape[0] > busStepSize else 1)) for index in range(1, len(busesList)): if len(busesList[index].shape) == 2: busesList[index][None, :] busesList[index] = cp.hsplit(busesList[index], [ int(cp.argwhere(cp.isnan(busesList[index][0, :, 1]))[0]), busesList[index].shape[1] ])[0] linesList = cp.array_split( matrizLinhas, int(matrizLinhas.shape[0] / lineStepSize if matrizLinhas.shape[0] > lineStepSize else 1)) for index in range(1, len(linesList)): if len(linesList[index].shape) == 2: linesList[index][None, :] linesList[index] = cp.hsplit(linesList[index], [ int(cp.argwhere(cp.isnan(linesList[index][0, :, 1]))[0]), linesList[index].shape[1] ])[0] #busDataset = tr.utils.data.DataLoader(busesList,drop_last=True) #lineDataset = tr.utils.data.DataLoader(linesList,drop_last=True) #algorithm = Algorithm() #algorithm.cuda() fullResults = None for nowBus, busTensor in enumerate(busesList): allLinesResult = None for nowLine, lineTensor in enumerate(linesList): # Algorithm segment #algRes = algorithm.FullAlgorithm(busTensor,lineTensor,distanceTolerance,detectionPercentage) #algRes = FullAlgorithm(busTensor,lineTensor,distanceTolerance,detectionPercentage) algRes = cp.asnumpy( Algorithm(busTensor, lineTensor, TOLERANCE=distanceTolerance, detectionPercentage=detectionPercentage)) #segmentResults = algRes.numpy() # Concatenation to full results matrix if allLinesResult is None: allLinesResult = np.copy(algRes) else: allLinesResult = np.concatenate([allLinesResult, algRes], axis=1) if fullResults is None: fullResults = np.copy(allLinesResult) else: fullResults = np.concatenate([fullResults, allLinesResult], axis=0) #fullResults = fullResults > detectionPercentage lineLabel = [(i[0], str(i[1])) for i in lineIdList] busLabel = [i[0] for i in busIdList] results = pd.DataFrame(fullResults.T, index=pd.MultiIndex.from_tuples(lineLabel), columns=busLabel) return results