def image_complexity(self, X, masks): # 복잡도 기준 threshold = 25.0 # reshape #masks = cp.reshape(masks, masks.shape[:-1]) R, G, B = X[:, :, :, 0:1], X[:, :, :, 1:2], X[:, :, :, 2:] # compute rg = R - G rg = cp.absolute(R - G) # compute yb = 0.5 * (R + G) - B yb = cp.absolute(0.5 * (R + G) - B) _masks = cp.logical_not(masks) # compute the mean and standard deviation of both `rg` and `yb` () # no masked_where on cupy rb_masked = np.ma.masked_where(_masks, rg) (rb_mean, rb_std) = (cp.mean(rb_masked, axis=(1, 2, 3)), cp.std(rb_masked, axis=(1, 2, 3))) yb_masked = np.ma.masked_where(_masks, yb) (yb_mean, yb_std) = (cp.mean(yb_masked, axis=(1, 2, 3)), cp.std(yb_masked, axis=(1, 2, 3))) # combine the mean and standard deviations std_root = cp.sqrt((rb_std**2) + (yb_std**2)) mean_root = cp.sqrt((rb_mean**2) + (yb_mean**2)) # derive the "colorfulness" metric and return it complexity = std_root + (0.3 * mean_root) # 수치 수정 print('image_complexity Done.') return (complexity >= threshold).astype(cp.uint8)
def test_asum(self): x = self._make_random_vector() ref = cupy.sum(cupy.absolute(x.real) + cupy.absolute(x.imag)) out = self._make_out(self.dtype.char.lower()) res = cublas.asum(x, out=out) self._check_pointer(res, out) cupy.testing.assert_allclose(res, ref, rtol=self.tol, atol=self.tol)
def test_iamin(self): x = self._make_random_vector() ref = cupy.argmin(cupy.absolute(x.real) + cupy.absolute(x.imag)) out = self._make_out('i') res = cublas.iamin(x, out=out) self._check_pointer(res, out) # Note: iamin returns 1-based index cupy.testing.assert_array_equal(res - 1, ref)
def scf_per_batch(Np, L, xs): # xs shape (bs,1024,2) B = Np//2 s = scd_fam(xs, Np, L) # shape (batch, alpha, f_k) f = cp.absolute(s) alpha = cp.amax(cp.absolute(s), axis=-1) freq = cp.amax(cp.absolute(s), axis=-2) (bs, my, mx) = f.shape freq = freq[:, (mx//2-B):(mx//2 + B)] return alpha, freq # should be (bs,Np) (bs,Np)
def yangVectorDistance(negativeVector, positiveVector, p=1): x = xp.array(negativeVector).reshape((-1, 1)) y = xp.array(positiveVector).reshape((-1, 1)) pExp = int(p) assert x.shape == y.shape, "x ({}) and y ({}) must be of the same shape".format( x.shape, y.shape) assert pExp > 0, "p must be an integer greater than 0" numerator = vectorPDistance(x, y, pExp) max_X_Y = xp.maximum(xp.absolute(x), xp.absolute(y)) maxes = xp.maximum(max_X_Y, xp.absolute(x - y)) return numerator / xp.sum(maxes)
def centerCBED(data4D_flat, x_cen, y_cen, chunks=8): stops = np.zeros(chunks + 1, dtype=np.int) stops[0:chunks] = np.arange(0, data4D_flat.shape[0], (data4D_flat.shape[0] / chunks)) stops[chunks] = data4D_flat.shape[0] max_size = int(np.amax(np.diff(stops))) centered4D = np.zeros_like(data4D_flat) image_size = np.asarray(data4D_flat.shape[1:3]) fourier_cal_y = (cp.linspace( (-image_size[0] / 2), ((image_size[0] / 2) - 1), image_size[0])) / image_size[0] fourier_cal_x = (cp.linspace( (-image_size[1] / 2), ((image_size[1] / 2) - 1), image_size[1])) / image_size[1] [fourier_mesh_x, fourier_mesh_y] = cp.meshgrid(fourier_cal_x, fourier_cal_y) move_pixels = np.flip(image_size / 2) - np.asarray((x_cen, y_cen)) move_phase = cp.exp( (-2) * np.pi * 1j * ((fourier_mesh_x * move_pixels[0]) + (fourier_mesh_y * move_pixels[1]))) for ii in range(chunks): startval = stops[ii] stop_val = stops[ii + 1] gpu_4Dchunk = cp.asarray(data4D_flat[startval:stop_val, :, :]) FFT_4D = cp.fft.fftshift(cp.fft.fft2(gpu_4Dchunk, axes=(-1, -2)), axes=(-1, -2)) FFT_4Dmove = cp.absolute( cp.fft.ifft2(cp.multiply(FFT_4D, move_phase), axes=(-1, -2))) centered4D[startval:stop_val, :, :] = cp.asnumpy(FFT_4Dmove) del FFT_4D, gpu_4Dchunk, FFT_4Dmove, move_phase, fourier_cal_y, fourier_cal_x, fourier_mesh_x, fourier_mesh_y return centered4D
def predict(self, X): for column in self.feature: if self.discrete_column[column]: new_data = [] for value in X[column]: idx = list(self.inmapping[column].keys())[list( self.inmapping[column].values()).index(column + "::" + value)] new_data.append(self.class_array[column][idx]) X.drop(columns=column, inplace=True) NewData = pd.DataFrame(new_data, columns=[column], index=X.index) X = pd.concat([X, NewData], axis=1) output = self.feedforward(X) result = [] for i in range(len(X)): row_result = [] for key, column in enumerate(self.target): idx = cp.argmax(1 - cp.absolute(self.class_array[column] - output[i, key])) row_result.append( self.outmapping[column][int(idx)].split('::')[1]) result.append(row_result) return result
def process_2D(Volume_spectra: cp.ndarray, coordinates: cp.ndarray, dispersion: cp.array) -> np.array: """ This function process 2D array of spectrum to return adjusted Bscan. :param Volume_spectra: 2nd order tensor containing spectras raw data. Last dimension is depth encoding. :type Volume_spectra: cp.ndarray :param coordinates: 2D array containing coordinates for k-linearization interpolation. :type coordinates: cp.ndarray :param dispersion: Array with value for dispersion compensation. :type dispersion: cp.array """ if Arguments.dimension[1] == 1: Volume_spectra = cp.expand_dims(Volume_spectra, axis=0) coordinates = cp.array([coordinates[1]]) Volume_spectra = detrend_1D(Volume_spectra) Volume_spectra = compensate_dispersion_2D(Volume_spectra, dispersion) Volume_spectra = linearize_spectra_1D(Volume_spectra, coordinates) else: Volume_spectra = detrend_2D(Volume_spectra) Volume_spectra = compensate_dispersion_2D(Volume_spectra, dispersion) Volume_spectra = linearize_spectra_2D(Volume_spectra, coordinates) if Arguments.shift: Volume_spectra = spectrum_shift_2D(Volume_spectra) Volume_spectra = fftpack.fft( Volume_spectra, axis=1, overwrite_x=True)[:, :Arguments.dimension[2] // 2] return cp.asnumpy(cp.absolute(Volume_spectra))
def forward(self, x): self.mask = (cp.absolute(x) > 1) out = cp.ones_like(x, dtype=np.float32) out[x < 0.5] = 0 out[x < -0.5] = -1 return out
def process_3D(Volume_spectra: np.ndarray, calibration: dict) -> np.array: """ GPU accelerated """ Volume_spectra = detrend_3D(Volume_spectra) Volume_spectra = linearize_spectra_3D(cp.asnumpy(Volume_spectra), calibration) temp = cp.array(Volume_spectra) temp = compensate_dispersion_3D(temp, calibration) if Arguments.shift: temp = spectrum_shift(temp) temp = cp.fft.rfft(temp, axis=2)[:, :, :Arguments.dimension[2] // 2] temp = cp.absolute(temp) cp.cuda.Device().synchronize() return cp.asnumpy(temp)
def ssb_kernel(processed4D, real_calibration, aperture, voltage): data_size = processed4D.shape wavelength = e_lambda(voltage) cutoff = aperture / wavelength four_y = np.fft.fftshift(np.fft.fftfreq(data_size[0], real_calibration)) four_x = np.fft.fftshift(np.fft.fftfreq(data_size[1], real_calibration)) Four_Y, Four_X = np.meshgrid(four_y, four_x) FourXY = np.sqrt((Four_Y**2) + (Four_X**2)) Left_Lobe = np.zeros(data_size, dtype=processed4D.dtype) RightLobe = np.zeros_like(Left_Lobe) #convert to CuPy arrays Four_Y = cp.asarray(Four_Y) Four_X = cp.asarray(Four_X) FourXY = cp.asarray(FourXY) Left_Lobe = cp.asarray(Left_Lobe) RightLobe = cp.asarray(RightLobe) lobe_calc(Left_Lobe, RightLobe, Four_Y, Four_X, FourXY, cutoff) data_phase = cp.angle(processed4D) data_ampli = cp.absolute(processed4D) left_trotter = cp.multiply( cp.multiply(data_ampli, Left_Lobe), cp.exp((1j) * cp.multiply(data_phase, Left_Lobe))) left_image = cp.sum(left_trotter, axis=(0, 1)) righttrotter = cp.multiply( cp.multiply(data_ampli, RightLobe), cp.exp((1j) * cp.multiply(data_phase, RightLobe))) rightimage = cp.sum(righttrotter, axis=(0, 1)) return left_image, rightimage
def absolute(inp) -> 'Tensor': _check_tensors(inp) engine = _get_engine(inp) return _create_tensor( inp, data=engine.absolute(inp.data), func=wrapped_partial(absolute_backward, inp=inp) )
def Minkowski(self,usu1,usu2,r): list_items1 = self.rating_avg.loc[self.rating_avg.loc[:,'userId'] == usu1 ] list_items2 = self.rating_avg.loc[self.rating_avg.loc[:,'userId'] == usu2 ] mezclar = pd.merge(list_items1, list_items2, how='inner', on='itemId') rating_x = mezclar[['adg_rating_x']].to_numpy().transpose()[0] rating_y = mezclar[['adg_rating_y']].to_numpy().transpose()[0] car=mezclar.shape[0] if(car==0 or r == 0): return 0 Mink = np.sum(pow(np.absolute(np.subtract(rating_x,rating_y)),r)) return pow(Mink,1/r)
def loop_body(grid): center = grid[1:-1, 1:-1] north = grid[0:-2, 1:-1] east = grid[1:-1, 2:] west = grid[1:-1, 0:-2] south = grid[2:, 1:-1] work = 0.2 * (center + north + east + west + south) delta = np.sum(np.absolute(work - center)) center[:] = work bench.plot_surface(grid, "2d", 0, 200, -200) return delta > epsilon
def Manhattan(self,usu1,usu2): list_items1 = self.rating_avg.loc[self.rating_avg.loc[:,'userId'] == usu1 ] list_items2 = self.rating_avg.loc[self.rating_avg.loc[:,'userId'] == usu2 ] mezclar = pd.merge(list_items1, list_items2, how='inner', on='itemId') rating_x = mezclar[['adg_rating_x']].to_numpy().transpose()[0] rating_y = mezclar[['adg_rating_y']].to_numpy().transpose()[0] car=mezclar.shape[0] if(car==0): return 0 Man = np.sum(np.absolute(np.subtract(rating_x,rating_y))) return Man
def test_silhouette_samples_batched(metric, chunk_divider, labeled_clusters): X, labels = labeled_clusters cuml_scores = cu_silhouette_samples(X, labels, metric=metric, chunksize=int(X.shape[0] / chunk_divider)) sk_scores = sk_silhouette_samples(X, labels, metric=metric) cu_trunc = cp.around(cuml_scores, decimals=3) sk_trunc = cp.around(sk_scores, decimals=3) diff = cp.absolute(cu_trunc - sk_trunc) > 0 over_diff = cp.all(diff) # 0.5% elements allowed to be different if len(over_diff.shape) > 0: assert over_diff.shape[0] <= 0.005 * X.shape[0] # different elements should not differ more than 1e-1 tolerance_diff = cp.absolute(cu_trunc[diff] - sk_trunc[diff]) > 1e-1 diff_change = cp.all(tolerance_diff) if len(diff_change.shape) > 0: assert False
def subpixel_pad4D(data4D_flat, final_size, cut_radius, chunks=10): stops = np.zeros(chunks + 1, dtype=np.int) stops[0:chunks] = np.arange(0, data4D_flat.shape[0], (data4D_flat.shape[0] / chunks)) stops[chunks] = data4D_flat.shape[0] max_size = int(np.amax(np.diff(stops))) final_size = (np.asarray(final_size)).astype(int) move_pixels = cp.asarray( np.flip(0.5 * (final_size - np.asarray(data4D_flat.shape[1:3])))) yy, xx = np.mgrid[0:final_size[0], 0:final_size[1]] rad = ((yy - final_size[0] / 2)**2) + ((xx - final_size[1] / 2)**2) cutoff = cp.asarray((rad < ((1.1 * cut_radius)**2)).astype(data4D_flat.dtype)) cbed = cp.zeros(final_size, dtype=data4D_flat.dtype) fourier_cal_y = (cp.linspace( (-final_size[0] / 2), ((final_size[0] / 2) - 1), final_size[0])) / final_size[0] fourier_cal_x = (cp.linspace( (-final_size[1] / 2), ((final_size[1] / 2) - 1), final_size[1])) / final_size[1] [fourier_mesh_x, fourier_mesh_y] = cp.meshgrid(fourier_cal_x, fourier_cal_y) move_phase = cp.exp( (-2) * np.pi * (1j) * ((fourier_mesh_x * move_pixels[0]) + (fourier_mesh_y * move_pixels[1]))) padded_4D = np.zeros((data4D_flat.shape[0], final_size[0], final_size[1]), dtype=data4D_flat.dtype) padded_on_gpu = cp.zeros((max_size, final_size[0], final_size[1]), dtype=data4D_flat.dtype) for cc in range(chunks): startval = stops[cc] stop_val = stops[cc + 1] gpu_4Dchunk = cp.asarray(data4D_flat[startval:stop_val, :, :]) for ii in range(gpu_4Dchunk.shape[0]): cbed[0:data4D_flat.shape[1], 0:data4D_flat.shape[2]] = gpu_4Dchunk[ii, :, :] FFT_cbd = cp.fft.fftshift(cp.fft.fft2(cbed)) moved_cbed = (cp.absolute( cp.fft.ifft2(cp.multiply(FFT_cbd, move_phase)))).astype( data4D_flat.dtype) padded_on_gpu[ii, :, :] = moved_cbed * cutoff padded_4D[startval:stop_val, :, :] = cp.asnumpy( padded_on_gpu[0:gpu_4Dchunk.shape[0], :, :]) del padded_on_gpu, moved_cbed, cbed, FFT_cbd, move_phase, gpu_4Dchunk, move_pixels, cutoff return padded_4D
def gpu_resize(dPhi, dAmp, src_x, src_y, nx, ny): ratio_x = nx/src_x ratio_y = ny/src_y # print(ratio_x , ratio_y) dPhi[cupy.isnan(dPhi)] = 0 dPhi[cupy.isinf(dPhi)] = 0 dAmp[cupy.isnan(dAmp)] = 1 dAmp[cupy.isinf(dAmp)] = 1 dAmp[cupy.equal(dAmp,0.0)] = 0.01; dAmp = cupy.absolute(dAmp) dPhi = cupyx.scipy.ndimage.zoom(dPhi, (ratio_y,ratio_x)) dAmp = cupyx.scipy.ndimage.zoom(dAmp, (ratio_y,ratio_x)) dField = cupy.log(dAmp) + 1j*(dPhi) return dField
def _eigsh_solve_ritz(alpha, beta, beta_k, k, which): t = cupy.diag(alpha) t = t + cupy.diag(beta[:-1], k=1) t = t + cupy.diag(beta[:-1], k=-1) if beta_k is not None: t[k, :k] = beta_k t[:k, k] = beta_k w, s = cupy.linalg.eigh(t) # Pick-up k ritz-values and ritz-vectors if which == 'LA': idx = cupy.argsort(w) elif which == 'LM': idx = cupy.argsort(cupy.absolute(w)) wk = w[idx[-k:]] sk = s[:, idx[-k:]] return wk, sk
def lobe_calc(data4DF, Four_Y, Four_X, FourXY, rsize, cutoff, chunks): stops = np.zeros(chunks + 1, dtype=np.int) stops[0:chunks] = np.arange(0, data4DF.shape[-1], (data4DF.shape[-1] / chunks)) stops[chunks] = data4DF.shape[-1] left_image = cp.zeros_like(FourXY, dtype=np.complex64) rightimage = cp.zeros_like(FourXY, dtype=np.complex64) d_zero = FourXY < cutoff for cc in range(chunks): startval = stops[cc] stop_val = stops[cc + 1] gpu_4Dchunk = cp.asarray(data4DF[:, :, startval:stop_val]) rcalc = rsize[startval:stop_val, :] for pp in range(rcalc.shape[0]): ii, jj = rcalc[pp, :] xq = Four_X[ii, jj] yq = Four_Y[ii, jj] cbd = gpu_4Dchunk[:, :, pp] cbd_phase = cp.angle(cbd) cbd_ampli = cp.absolute(cbd) d_plus = (((Four_X + xq)**2) + ((Four_Y + yq)**2))**0.5 d_minu = (((Four_X - xq)**2) + ((Four_Y - yq)**2))**0.5 ll = cp.logical_and((d_plus < cutoff), (d_minu > cutoff)) ll = cp.logical_and(ll, d_zero) rr = cp.logical_and((d_plus > cutoff), (d_minu < cutoff)) rr = cp.logical_and(rr, d_zero) left_trotter = cp.multiply(cbd_ampli[ll], cp.exp((1j) * cbd_phase[ll])) righttrotter = cp.multiply(cbd_ampli[rr], cp.exp((1j) * cbd_phase[rr])) left_image[ii, jj] = cp.sum(left_trotter) rightimage[ii, jj] = cp.sum(righttrotter) del gpu_4Dchunk, d_plus, d_minu, ll, rr, left_trotter, righttrotter, cbd, cbd_phase, cbd_ampli, d_zero, rcalc return left_image, rightimage
def real_if_close(a, tol=100): """If input is complex with all imaginary parts close to zero, return real parts. "Close to zero" is defined as `tol` * (machine epsilon of the type for `a`). .. warning:: This function may synchronize the device. .. seealso:: :func:`numpy.real_if_close` """ if not issubclass(a.dtype.type, cupy.complexfloating): return a if tol > 1: f = numpy.finfo(a.dtype.type) tol = f.eps * tol if cupy.all(cupy.absolute(a.imag) < tol): a = a.real return a
def to_periodogram(signal): """ Returns periodogram of signal for finding frequencies that have high energy. :param signal: signal (time domain) :type signal: cudf.Series :return: CuPy array representing periodogram :rtype: cupy.core.core.ndarray """ # convert cudf series to cupy array signal_cp = cp.fromDlpack(signal.to_dlpack()) # standardize the signal signal_cp_std = (signal_cp - cp.mean(signal_cp)) / cp.std(signal_cp) # take fourier transform of signal FFT_data = cp.fft.fft(signal_cp_std) # create periodogram prdg = (1 / len(signal)) * ((cp.absolute(FFT_data))**2) return prdg
def phasecorr_gpu(X, cfRefImg, lcorr): ''' not being used - no speed up - may be faster with cuda.jit''' nimg, Ly, Lx = X.shape ly, lx = cfRefImg.shape[-2:] lyhalf = int(np.floor(ly / 2)) lxhalf = int(np.floor(lx / 2)) # put on GPU ref_gpu = cp.asarray(cfRefImg) x_gpu = cp.asarray(X) # phasecorrelation x_gpu = fftn(x_gpu, axes=(1, 2), overwrite_x=True) * np.sqrt(Ly - 1) * np.sqrt(Lx - 1) for t in range(x_gpu.shape[0]): tmp = x_gpu[t, :, :] tmp = cp.multiply(tmp, ref_gpu) tmp = cp.divide(tmp, cp.absolute(tmp) + 1e-5) x_gpu[t, :, :] = tmp x_gpu = ifftn(x_gpu, axes=(1, 2), overwrite_x=True) * np.sqrt(Ly - 1) * np.sqrt(Lx - 1) x_gpu = cp.fft.fftshift(cp.real(x_gpu), axes=(1, 2)) # get max index x_gpu = x_gpu[cp.ix_(np.arange(0, nimg, 1, int), np.arange(lyhalf - lcorr, lyhalf + lcorr + 1, 1, int), np.arange(lxhalf - lcorr, lxhalf + lcorr + 1, 1, int))] ix = cp.argmax(cp.reshape(x_gpu, (nimg, -1)), axis=1) cmax = x_gpu[np.arange(0, nimg, 1, int), ix] ymax, xmax = cp.unravel_index(ix, (2 * lcorr + 1, 2 * lcorr + 1)) cmax = cp.asnumpy(cmax).flatten() ymax = cp.asnumpy(ymax) xmax = cp.asnumpy(xmax) ymax, xmax = ymax - lcorr, xmax - lcorr return ymax, xmax, cmax
def transform_dimacs_clause_to_cugen_clause( dimacs_clause: str, number_of_literals: int) -> cupy.array: """ Transform a clause line from the DIMACS format to a clause in the cugen SAT format :param dimacs_clause: Line representing a clause in a DIMACS file :param number_of_literals: The number of literals in the formula :return: The clause in the cugen SAT format """ cugen_clause = cupy.array([cupy.nan] * number_of_literals) signed_literals = dimacs_clause.split()[:-1] for literal in signed_literals: literal_as_integer = int(literal) literal_as_index = int(cupy.absolute(literal_as_integer) - 1) if cupy.isnan(cugen_clause[literal_as_index]): cugen_clause[literal_as_index] = 0 if literal_as_integer < 0 else 1 elif literal_as_integer != cugen_clause[literal_as_index]: cugen_clause[literal_as_index] = -2 cugen_clause[cugen_clause == -2] = cupy.nan return cugen_clause
def loss(self, X, Y): """ Compute the loss over the input X and the expected classes Y """ if self.l1 == 0: l1 = 0 else: l1 = cp.sum(cp.absolute(self.w1)) + cp.sum(cp.absolute( self.b1)) + cp.sum(cp.absolute(self.w2)) + cp.sum( cp.absolute(self.b2)) + cp.sum(cp.absolute( self.w3)) + cp.sum(cp.absolute(self.b3)) if self.l2 == 0: l2 = 0 else: l2 = cp.sum(cp.power(self.w1, 2)) + cp.sum(cp.power( self.b1, 2)) + cp.sum(cp.power(self.w2, 2)) + cp.sum( cp.power(self.b2, 2)) + cp.sum(cp.power( self.w3, 2)) + cp.sum(cp.power(self.b3, 2)) return categorical_cross_entropy( self.fprop(X).T, onehot(Y, self.output_size)) + float( self.l1 * l1) + float(self.l2 * l2)
def polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False): """Returns the least squares fit of polynomial of degree deg to the data y sampled at x. Args: x (cupy.ndarray): x-coordinates of the sample points of shape (M,). y (cupy.ndarray): y-coordinates of the sample points of shape (M,) or (M, K). deg (int): degree of the fitting polynomial. rcond (float, optional): relative condition number of the fit. The default value is ``len(x) * eps``. full (bool, optional): indicator of the return value nature. When False (default), only the coefficients are returned. When True, diagnostic information is also returned. w (cupy.ndarray, optional): weights applied to the y-coordinates of the sample points of shape (M,). cov (bool or str, optional): if given, returns the coefficients along with the covariance matrix. Returns: cupy.ndarray: of shape (deg + 1,) or (deg + 1, K). Polynomial coefficients from highest to lowest degree tuple (cupy.ndarray, int, cupy.ndarray, float): Present only if ``full=True``. Sum of squared residuals of the least-squares fit, rank of the scaled Vandermonde coefficient matrix, its singular values, and the specified value of ``rcond``. cupy.ndarray: of shape (M, M) or (M, M, K). Present only if ``full=False`` and ``cov=True``. The covariance matrix of the polynomial coefficient estimates. .. warning:: numpy.RankWarning: The rank of the coefficient matrix in the least-squares fit is deficient. It is raised if ``full=False``. .. seealso:: :func:`numpy.polyfit` """ if x.dtype.char == 'e' and y.dtype.kind == 'b': raise NotImplementedError('float16 x and bool y are not' ' currently supported') if y.dtype == numpy.float16: raise TypeError('float16 y are not supported') x = _polyfit_typecast(x) y = _polyfit_typecast(y) deg = int(deg) if deg < 0: raise ValueError('expected deg >= 0') if x.ndim != 1: raise TypeError('expected 1D vector for x') if x.size == 0: raise TypeError('expected non-empty vector for x') if y.ndim < 1 or y.ndim > 2: raise TypeError('expected 1D or 2D array for y') if x.size != y.shape[0]: raise TypeError('expected x and y to have same length') lhs = cupy.polynomial.polynomial.polyvander(x, deg)[:, ::-1] rhs = y if w is not None: w = _polyfit_typecast(w) if w.ndim != 1: raise TypeError('expected a 1-d array for weights') if w.size != x.size: raise TypeError('expected w and y to have the same length') lhs *= w[:, None] if rhs.ndim == 2: w = w[:, None] rhs *= w if rcond is None: rcond = x.size * cupy.finfo(x.dtype).eps scale = cupy.sqrt((cupy.square(lhs)).sum(axis=0)) lhs /= scale c, resids, rank, s = cupy.linalg.lstsq(lhs, rhs, rcond) if y.ndim > 1: scale = scale.reshape(-1, 1) c /= scale order = deg + 1 if rank != order and not full: msg = 'Polyfit may be poorly conditioned' warnings.warn(msg, numpy.RankWarning, stacklevel=4) if full: if resids.dtype.kind == 'c': resids = cupy.absolute(resids) return c, resids, rank, s, rcond if cov: base = cupy.linalg.inv(cupy.dot(lhs.T, lhs)) base /= cupy.outer(scale, scale) if cov == 'unscaled': factor = 1 elif x.size > order: factor = resids / (x.size - order) else: raise ValueError('the number of data points must exceed order' ' to scale the covariance matrix') if y.ndim != 1: base = base[..., None] return c, base * factor return c
def qmf(hk): """ Return high-pass qmf filter from low-pass Parameters ---------- hk : array_like Coefficients of high-pass filter. """ N = len(hk) - 1 asgn = [{0: 1, 1: -1}[k % 2] for k in range(N + 1)] return hk[::-1] * cp.array(asgn) """ Return (x, phi, psi) at dyadic points ``K/2**J`` from filter coefficients. Parameters ---------- hk : array_like Coefficients of low-pass filter. J : int, optional Values will be computed at grid points ``K/2**J``. Default is 7. Returns ------- x : ndarray The dyadic points ``K/2**J`` for ``K=0...N * (2**J)-1`` where ``len(hk) = len(gk) = N+1``. phi : ndarray The scaling function ``phi(x)`` at `x`: ``phi(x) = sum(hk * phi(2x-k))``, where k is from 0 to N. psi : ndarray, optional The wavelet function ``psi(x)`` at `x`: ``phi(x) = sum(gk * phi(2x-k))``, where k is from 0 to N. `psi` is only returned if `gk` is not None. Notes ----- The algorithm uses the vector cascade algorithm described by Strang and Nguyen in "Wavelets and Filter Banks". It builds a dictionary of values and slices for quick reuse. Then inserts vectors into final vector at the end. """ N = len(hk) - 1 if (J > 30 - cp.log2(N + 1)): raise ValueError("Too many levels.") if (J < 1): raise ValueError("Too few levels.") # construct matrices needed nn, kk = cp.ogrid[:N, :N] s2 = cp.sqrt(2) # append a zero so that take works thk = cp.r_[hk, 0] gk = qmf(hk) tgk = cp.r_[gk, 0] indx1 = cp.clip(2 * nn - kk, -1, N + 1) indx2 = cp.clip(2 * nn - kk + 1, -1, N + 1) m = cp.zeros((2, 2, N, N), 'd') m[0, 0] = cp.take(thk, indx1, 0) m[0, 1] = cp.take(thk, indx2, 0) m[1, 0] = cp.take(tgk, indx1, 0) m[1, 1] = cp.take(tgk, indx2, 0) m *= s2 # construct the grid of points x = cp.arange(0, N * (1 << J), dtype=float) / (1 << J) phi = 0 * x psi = 0 * x # find phi0, and phi1 lam, v = eig(m[0, 0]) ind = cp.argmin(cp.absolute(lam - 1)) # a dictionary with a binary representation of the # evaluation points x < 1 -- i.e. position is 0.xxxx v = cp.real(v[:, ind]) # need scaling function to integrate to 1 so find # eigenvector normalized to sum(v,axis=0)=1 sm = cp.sum(v) if sm < 0: # need scaling function to integrate to 1 v = -v sm = -sm bitdic = {'0': v / sm} bitdic['1'] = cp.dot(m[0, 1], bitdic['0']) step = 1 << J phi[::step] = bitdic['0'] phi[(1 << (J - 1))::step] = bitdic['1'] psi[::step] = cp.dot(m[1, 0], bitdic['0']) psi[(1 << (J - 1))::step] = cp.dot(m[1, 1], bitdic['0']) # descend down the levels inserting more and more values # into bitdic -- store the values in the correct location once we # have computed them -- stored in the dictionary # for quicker use later. prevkeys = ['1'] for level in range(2, J + 1): newkeys = ['%d%s' % (xx, yy) for xx in [0, 1] for yy in prevkeys] fac = 1 << (J - level) for key in newkeys: # convert key to number num = 0 for pos in range(level): if key[pos] == '1': num += (1 << (level - 1 - pos)) pastphi = bitdic[key[1:]] ii = int(key[0]) temp = cp.dot(m[0, ii], pastphi) bitdic[key] = temp phi[num * fac::step] = temp psi[num * fac::step] = cp.dot(m[1, ii], pastphi) prevkeys = newkeys return x, phi, psi
cp_diff_amp = cp.asarray(np_diff_amp, dtype="float32") cp_sup = cp.asarray(np_sup, dtype="float32") cp_initial_dens = cp.asarray(np_initial_dens, dtype="float32") cp_dens = cp.asarray(np_dens) print("iteration scale_factor Rfactor OS_ratio gamma") with open(log_path, mode='a') as log: log.write("iteration scale_factor Rfactor OS_ratio gamma") for i in range(int(iteration) + int(additional_iteration)): cp_structure_factor = cp.fft.fft2(cp_dens, norm="ortho") #【フーリエ変換】 cp_structure_factor = cp.fft.fftshift( cp_structure_factor) #fftshiftを使ってシフト cp_amp = cp.absolute(cp_structure_factor) #絶対値をとる #逆空間拘束 # np_amp = cp.asnumpy(cp.square(cp_amp))#cupy配列 ⇒ numpy配列に変換 # tifffile.imsave('diff.tif' ,np_amp) # exit() cp_structure_factor[ (cp_diff_amp % 2 > 0.0) & (cp_amp % 2 != 0.0)] = cp_structure_factor[ (cp_diff_amp % 2 > 0.0) & (cp_amp % 2 != 0.0)] * cp_diff_amp[ (cp_diff_amp % 2 > 0.0) & (cp_amp % 2 != 0.0)] / cp_amp[ (cp_diff_amp % 2 > 0.0) & (cp_amp % 2 != 0.0)] cp_structure_factor_bk = cp_structure_factor
def admittanceMatrixE(self): shapeParams = self.shapeFunctionParameters() whereIsZero = (cp.absolute(shapeParams) - 1e-12 < 0) indexZero = cp.where(whereIsZero) isConst = indexZero[2] == 2 # 1 for const x, 0 for const y indicesConstX = cp.where(isConst)[0] indicesConstY = cp.where(~isConst)[0] sortedElNodeIndices = cp.sort(self.nodeisElectrode[self.isValid], axis=1) admittanceMatrixE = cp.zeros((self.n_pts, self.ne)) shapeMatrix = cp.zeros((shapeParams.shape[0], shapeParams.shape[1], 2)) integratingMatrix = cp.zeros((shapeParams.shape[0], 2)) shapeMatrix[indicesConstY, :, 0] = shapeParams[indicesConstY, :, 0] + shapeParams[indicesConstY, :, 2] * self.pts[sortedElNodeIndices, :][indicesConstY, 1, 1][:, None] shapeMatrix[indicesConstY, :, 1] = shapeParams[indicesConstY, :, 1] shapeMatrix[indicesConstX, :, 0] = shapeParams[indicesConstX, :, 0] + shapeParams[indicesConstX, :, 1] * self.pts[sortedElNodeIndices, :][indicesConstX, 1, 0][:, None] shapeMatrix[indicesConstX, :, 1] = shapeParams[indicesConstX, :, 2] integratingMatrix[indicesConstY, 0] = self.pts[sortedElNodeIndices, :][indicesConstY, 1, 0] - self.pts[sortedElNodeIndices, :][indicesConstY, 0, 0] integratingMatrix[indicesConstY, 1] = 0.5 * (cp.power(self.pts[sortedElNodeIndices, :][indicesConstY, 1, 0], 2) - cp.power(self.pts[sortedElNodeIndices, :][indicesConstY, 0, 0], 2)) integratingMatrix[indicesConstX, 0] = self.pts[sortedElNodeIndices, :][indicesConstX, 1, 1] - self.pts[sortedElNodeIndices, :][indicesConstX, 0, 1] integratingMatrix[indicesConstX, 1] = 0.5 * (cp.power(self.pts[sortedElNodeIndices, :][indicesConstX, 1, 1], 2) - cp.power(self.pts[sortedElNodeIndices, :][indicesConstX, 0, 1], 2)) #print(integratingMatrix.shape) integrals = cp.einsum('ijk, ik -> ij', shapeMatrix, integratingMatrix) integrals[:] = cp.absolute(integrals) #integr = cp.sum(cp.multiply(shapeMatrix, integratingMatrix[:, None]), axis=2) #print(cp.sum(cp.round_(integrals, 16) == cp.round_(integr, 16))) indexElectrode = sortedElNodeIndices[:, 0] // self.n_per_el #print(indexElectrode) integrals = - integrals / self.z[indexElectrode][:, None, None] integrals = integrals.ravel() indexElectrode = cp.tile(indexElectrode, (self.n_per_el, 1)).T.ravel() #print(self.tri[twoFromElectrode][isValid]) indexNode = self.tri[self.twoFromElectrode][self.isValid].ravel() #admittanceMatrixE [self.tri[twoFromElectrode][isValid].ravel(), indexElectrode] += integrals.ravel() indSort = cp.argsort(indexNode) indexNode = indexNode[indSort] indexElectrode = indexElectrode[indSort] integrals = integrals[indSort] unique, counts = cp.unique(indexNode, return_counts=True) #print("number of unique entries", unique.shape) #print("counts \n", counts) index_pointer = cp.zeros(self.n_pts + 1) sum_count = cp.cumsum(counts) #print(sum_count) index_pointer[unique[:]+1] = sum_count[:] #print(index_pointer) nonzeroes = cp.nonzero(index_pointer)[0] #print(nonzeroes) mask = cp.zeros(index_pointer.shape[0], dtype='b1') mask[nonzeroes] = True mask[0] = True zeroes = cp.where(~mask)[0] #time_loop = time() while (index_pointer[1:]==0).any(): index_pointer[zeroes] = index_pointer[zeroes - 1] '''for i in range(index_pointer.shape[0]): if i == 0: continue elif index_pointer[i] == 0: index_pointer[i] = index_pointer[i-1]''' #print('time for loop ',time()-time_loop) index_pointer2 = cp.arange(self.n_pts + 1) #print('indexEl', indexElectrode) #print(index_pointer.shape) admittanceMatrixE = sp.csr_matrix((integrals, indexElectrode, index_pointer), shape=(self.n_pts, self.ne), dtype=integrals.dtype) adm = admittanceMatrixE.toarray(); #print(integrals) #print(indexNode) #print(indexElectrode) #a = (sortedElNodeIndices[0,0]) #print(adm[4]) # print(adm[:,1]) #print('sum zeroes ',cp.sum(adm>0)) return adm;
def admittanceMatrixC2(self): ''' compute matrix to calculate integral of two shape functions over the length of the electrode (assuming they are non-zero) - shape ((n_el * n_per_el - 1), 3, 3, 3) ''' shapeParams = self.shapeFunctionParameters() whereIsZero = (cp.absolute(shapeParams) - 1e-12 < 0) indexZero = cp.where(whereIsZero) isConst = indexZero[2] == 2 # 1 for const x, 0 for const y zeroShapeFunc = cp.array(indexZero[1]) indicesShapeFunctions = cp.outer(cp.ones(shapeParams.shape[0]), cp.arange(3)) indicesShapeFunctions[:, ~zeroShapeFunc] = 0 #print(indexZero) outerOfShapeFunc = cp.einsum('ijk, ipq -> ijpkq', shapeParams, shapeParams) #print(outerOfShapeFunc[0,0,0]) integratingMatrix = cp.empty((outerOfShapeFunc.shape[0], outerOfShapeFunc.shape[3], outerOfShapeFunc.shape[4])) ''' for i in range(20): #print(self.pts[nodeisElectrode[isValid], :][i]) print(nodeisElectrode[isValid][i]) print(self.tri[twoFromElectrode][isValid][i]) #print(nodeisElectrode[isValid])''' sortedElNodeIndices = cp.sort(self.nodeisElectrode[self.isValid], axis=1) #print(sortedElNodeIndices) firstOrderY = cp.empty((outerOfShapeFunc.shape[0])) secondOrderY = cp.empty((outerOfShapeFunc.shape[0])) thirdOrderY = cp.empty((outerOfShapeFunc.shape[0])) constX = cp.ones((outerOfShapeFunc.shape[0], 3)) firstOrderY[:] = self.pts[sortedElNodeIndices, :][:, 1, 1] - self.pts[sortedElNodeIndices, :][:, 0, 1] # y2 - y1 secondOrderY[:] = 0.5 * (cp.power(self.pts[sortedElNodeIndices, :][:, 1, 1], 2) - cp.power(self.pts[sortedElNodeIndices, :][:, 0, 1], 2)) # 1/2 (y2^2 - y1^2) thirdOrderY[:] = 1./3. * (cp.power(self.pts[sortedElNodeIndices, :][:, 1, 1], 3) - cp.power(self.pts[sortedElNodeIndices, :][:, 0, 1], 3)) # 1/3 (y2^3 - y1^3) constX[:, 1] = self.pts[sortedElNodeIndices, :][:, 1, 0] constX = cp.einsum('ij, ik -> ijk', constX, constX) integratingMatrix[:, 0, 0] = firstOrderY[:] integratingMatrix[:, 0, 1] = firstOrderY[:] integratingMatrix[:, 1, 0] = firstOrderY[:] integratingMatrix[:, 0, 2] = secondOrderY[:] integratingMatrix[:, 2, 0] = secondOrderY[:] integratingMatrix[:, 1, 1] = firstOrderY[:] integratingMatrix[:, 1, 2] = secondOrderY[:] integratingMatrix[:, 2, 1] = secondOrderY[:] integratingMatrix[:, 2, 2] = thirdOrderY[:] integratingMatrix[:] = integratingMatrix * isConst[:, None, None] #print(integratingMatrix) #intm = cp.array(integratingMatrix) #print(constX) firstOrderX = cp.empty((outerOfShapeFunc.shape[0])) secondOrderX = cp.empty((outerOfShapeFunc.shape[0])) thirdOrderX = cp.empty((outerOfShapeFunc.shape[0])) constY = cp.ones((outerOfShapeFunc.shape[0], 3)) firstOrderX[:] = self.pts[sortedElNodeIndices, :][:, 1, 0] - self.pts[sortedElNodeIndices, :][:, 0, 0] # x2 - x1 secondOrderX[:] = 0.5 * (cp.power(self.pts[sortedElNodeIndices, :][:, 1, 0], 2) - cp.power(self.pts[sortedElNodeIndices, :][:, 0, 0], 2)) # 1/2 (x2^2 - x1^2) thirdOrderX[:] = 1./3. * (cp.power(self.pts[sortedElNodeIndices, :][:, 1, 0], 3) - cp.power(self.pts[sortedElNodeIndices, :][:, 0, 0], 3)) # 1/3 (x2^3 - x1^3) constY[:, 2] = self.pts[sortedElNodeIndices, :][:, 1, 1] constY = cp.einsum('ij, ik -> ijk', constY, constY) #print(constY) indicesConstX = cp.where(isConst)[0] indicesConstY = cp.where(~isConst)[0] #print(indicesConstY) integratingMatrix[indicesConstY, 0, 0] = firstOrderX[indicesConstY] integratingMatrix[indicesConstY, 0, 1] = secondOrderX[indicesConstY] integratingMatrix[indicesConstY, 1, 0] = secondOrderX[indicesConstY] integratingMatrix[indicesConstY, 0, 2] = firstOrderX[indicesConstY] integratingMatrix[indicesConstY, 2, 0] = firstOrderX[indicesConstY] integratingMatrix[indicesConstY, 1, 1] = thirdOrderX[indicesConstY] integratingMatrix[indicesConstY, 1, 2] = secondOrderX[indicesConstY] integratingMatrix[indicesConstY, 2, 1] = secondOrderX[indicesConstY] integratingMatrix[indicesConstY, 2, 2] = firstOrderX[indicesConstY] ''' for i in range(40): print(intm[i]) print(integratingMatrix[i]) ''' integratingMatrix[indicesConstX] = cp.multiply(integratingMatrix[indicesConstX], constX[indicesConstX]) integratingMatrix[indicesConstY] = cp.multiply(integratingMatrix[indicesConstY], constY[indicesConstY]) admittanceMatrix = cp.einsum('ijklm, ilm -> ijk', outerOfShapeFunc, integratingMatrix) admittanceMatrix[:] = cp.absolute(admittanceMatrix) admittanceMatrix[admittanceMatrix < 1e-18] = 0 #admittanceMatrix2 = cp.sum(cp.multiply(outerOfShapeFunc, integratingMatrix[:, None, None, :, :]), axis = [3,4]) #print(admittanceMatrix[:50,:50]) #number_of_equal = cp.sum(cp.equal(cp.round_(admittanceMatrix, 16), cp.round_(admittanceMatrix2, 16))) #print(number_of_equal) #print(number_of_equal == admittanceMatrix.shape[0] * admittanceMatrix.shape[1] * admittanceMatrix.shape[2]) return admittanceMatrix