def __init__(self, xx, yy, minimum=xp.nan, maximum=xp.nan, name=None, latex_label=None, unit=None, boundary=None): self.xx = xp.asarray(xx) self.min_limit = float(xp.amin(self.xx)) self.max_limit = float(xp.amax(self.xx)) # In order to use np/cp.interp, we need to make sure that xx is ordered sorted_idxs = xp.argsort(self.xx) self.xx = self.xx[sorted_idxs] self._yy = xp.asarray(yy)[sorted_idxs] if self._yy.ndim != 1: raise TypeError("yy must be 1D. A {}-D array given.".format( self.yy.dim)) self.YY = None self.probability_density = None self.cumulative_distribution = None self.inverse_cumulative_distribution = None self.__all_interpolated = Interp(self.xx, self._yy) minimum = float(xp.nanmax(xp.array([self.min_limit, minimum]))) maximum = float(xp.nanmin(xp.array([self.max_limit, maximum]))) bilby.core.prior.Prior.__init__(self, name=name, latex_label=latex_label, unit=unit, minimum=minimum, maximum=maximum, boundary=boundary) self._update_instance()
def edges_plot(self, canvas, nodes, name=None): """ plot edges(lines) """ aggregator, cmap = _compute_datashader_assets( self.connected_edges, self.node_x, self.edge_aggregate_col, self.edge_aggregate_fn, self.edge_color_palette, ) agg = canvas.line(self.connected_edges, self.node_x, self.node_y, aggregator) if (self.constant_limit_edges is None or self.edge_aggregate_fn == "count"): self.constant_limit_edges = [ float(cp.nanmin(agg.data)), float(cp.nanmax(agg.data)), ] span = {"span": self.constant_limit_nodes} return getattr(tf, self.node_pixel_spread)( tf.shade( agg, name=name, how="linear", alpha=255 - 255 * self.edge_transparency, **cmap, **span, ), max_px=1, )
def calc_stc(cum, gpu=False): """ Calculate STC (spatio-temporal consistensy; Hanssen et al., 2008, Terrafirma) of time series of displacement. Note that isolated pixels (which have no surrounding pixel) have nan of STC. Input: cum : Cumulative displacement (n_im, length, width) gpu : GPU flag Return: stc : STC (length, width) """ if gpu: import cupy as xp cum = xp.asarray(cum) else: xp = np n_im, length, width = cum.shape ### Add 1 pixel margin to cum data filled with nan cum1 = xp.ones((n_im, length + 2, width + 2), dtype=xp.float32) * xp.nan cum1[:, 1:length + 1, 1:width + 1] = cum ### Calc STC for surrounding 8 pixels _stc = xp.ones((length, width, 8), dtype=xp.float32) * xp.nan pixels = [[0, 0], [0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1], [2, 2]] ## Left Top = [0, 0], Rigth Bottmon = [2, 2], Center = [1, 1] for i, pixel in enumerate(pixels): ### Spatial difference (surrounding pixel-center) d_cum = cum1[:, pixel[0]:length + pixel[0], pixel[1]:width + pixel[1]] - cum1[:, 1:length + 1, 1:width + 1] ### Temporal difference (double difference) dd_cum = d_cum[:-1, :, :] - d_cum[1:, :, :] ### STC (i.e., RMS of DD) sumsq_dd_cum = xp.nansum(dd_cum**2, axis=0) n_dd_cum = (xp.sum(~xp.isnan(dd_cum), axis=0)).astype(xp.float32) #nof non-nan n_dd_cum[n_dd_cum == 0] = xp.nan #to avoid 0 division _stc[:, :, i] = xp.sqrt(sumsq_dd_cum / n_dd_cum) ### Strange but some adjacent pixels can have identical time series, ### resulting in 0 of stc. To avoid this, replace 0 with nan. _stc[_stc == 0] = xp.nan ### Identify minimum value as final STC with warnings.catch_warnings(): ## To silence warning by All-Nan slice warnings.simplefilter('ignore', RuntimeWarning) stc = xp.nanmin(_stc, axis=2) if gpu: stc = xp.asnumpy(stc) del cum, cum1, _stc, d_cum, dd_cum, sumsq_dd_cum, n_dd_cum return stc
def _run_cupy_equal_interval(data, k): max_data = cupy.nanmax(data) min_data = cupy.nanmin(data) width = (max_data - min_data) / k cuts = cupy.arange(min_data.get() + width.get(), max_data.get() + width.get(), width.get()) l_cuts = cuts.shape[0] if l_cuts > k: # handle overshooting cuts = cuts[0:k] cuts[-1] = max_data out = _bin(data, cuts, cupy.arange(l_cuts)) return out
def viewInteractiveImage(x_range, y_range, w, h, data_source, **kwargs): dd = data_source[[self.x, self.y, self.aggregate_col]] dd[self.x] = self._to_xaxis_type(dd[self.x]) dd[self.y] = self._to_yaxis_type(dd[self.y]) x_range = self._to_xaxis_type(x_range) y_range = self._to_yaxis_type(y_range) cvs = ds.Canvas(plot_width=w, plot_height=h, x_range=x_range, y_range=y_range) aggregator, cmap = _compute_datashader_assets( dd, self.x, self.aggregate_col, self.aggregate_fn, self.color_palette, ) agg = cvs.points( dd, self.x, self.y, aggregator, ) if self.constant_limit is None or self.aggregate_fn == "count": self.constant_limit = [ float(cp.nanmin(agg.data)), float(cp.nanmax(agg.data)), ] self.render_legend() span = {"span": self.constant_limit} if self.pixel_shade_type == "eq_hist": span = {} img = tf.shade(agg, how=self.pixel_shade_type, **cmap, **span) if self.pixel_spread == "dynspread": return tf.dynspread( img, threshold=self.pixel_density, max_px=self.point_size, shape=self.point_shape, ) else: return tf.spread(img, px=self.point_size, shape=self.point_shape)
def Algorithm(MO, ML, TOLERANCE, detectionPercentage=None, haversine=True): # global to analise global resultsMin # MatrizOnibus = cp.copy(MO) MatrizLinhas = cp.copy(ML) MatrizLinhas = cp.dsplit(MatrizLinhas, 2) MatrizOnibus = cp.dsplit(MatrizOnibus, 2) infVector = cp.squeeze(cp.sum(cp.isnan(MatrizLinhas[0]), axis=1), axis=-1) MatrizLinhas[0] = cp.expand_dims(MatrizLinhas[0], axis=-1) MatrizLinhas[1] = cp.expand_dims(MatrizLinhas[1], axis=-1) MatrizOnibus[0] = cp.expand_dims(MatrizOnibus[0], axis=-1) MatrizOnibus[1] = cp.expand_dims(MatrizOnibus[1], axis=-1) MatrizOnibus[0] *= cp.pi / 180 MatrizOnibus[1] *= cp.pi / 180 MatrizLinhas[1] = cp.transpose(MatrizLinhas[1], [2, 3, 0, 1]) * cp.pi / 180 MatrizLinhas[0] = cp.transpose(MatrizLinhas[0], [2, 3, 0, 1]) * cp.pi / 180 # Haversine or euclidian, based on <haversine> if haversine: results = 1000*2*6371.0088*cp.arcsin( cp.sqrt( (cp.sin((MatrizOnibus[0] - MatrizLinhas[0])*0.5)**2 + \ cp.cos(MatrizOnibus[0])* cp.cos(MatrizLinhas[0]) * cp.sin((MatrizOnibus[1] - MatrizLinhas[1])*0.5)**2) )) else: results = cp.sqrt((MatrizOnibus[0] - MatrizLinhas[0])**2 + (MatrizOnibus[1] - MatrizLinhas[1])**2) # Matriz D^[min] resultsMin = cp.nanmin(results, axis=1) #resultsGroup = cp.pad(resultsMin,[(0,0),(0,0),(0,resultsMin.shape[2]%groupSize)],constant_values=cp.NaN) #resultsGroup = cp.reshape(resultsGroup,(resultsGroup.shape[0],resultsGroup.shape[1],-1,groupSize)) sizeLine = resultsMin.shape[2] #below = cp.sum(resultsMin,axis=2) below = cp.sum(resultsMin < TOLERANCE, axis=2) resultsPerc = below / (sizeLine - infVector) if detectionPercentage: return resultsPerc > cp.array(detectionPercentage) return resultsPerc
def nodes_plot(self, canvas, nodes, name=None): """ plot nodes(scatter) """ aggregator, cmap = _compute_datashader_assets( nodes, self.node_id, self.node_aggregate_col, self.node_aggregate_fn, self.node_color_palette, ) agg = canvas.points(nodes.sort_index(), self.node_x, self.node_y, aggregator) if (self.constant_limit_nodes is None or self.node_aggregate_fn == "count"): self.constant_limit_nodes = [ float(cp.nanmin(agg.data)), float(cp.nanmax(agg.data)), ] self.render_legend() span = {"span": self.constant_limit_nodes} if self.node_pixel_shade_type == "eq_hist": span = {} return getattr(tf, self.node_pixel_spread)( tf.shade(agg, how=self.node_pixel_shade_type, name=name, **cmap, **span), threshold=self.node_pixel_density, max_px=self.node_point_size, shape=self.node_point_shape, )