def heightmap_clamp(hm: np.ndarray, mi: float, ma: float) -> None: """Clamp all values on this heightmap between ``mi`` and ``ma`` Args: hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions. mi (float): The lower bound to clamp to. ma (float): The upper bound to clamp to. .. deprecated:: 2.0 Do ``hm.clip(mi, ma)`` instead. """ hm.clip(mi, ma)
def fitness(self, data: np.ndarray, own=True) -> float: """ Calculate the fitness of an image. How good is a solution, crucial for evolution of the EA Args: data (np.ndarray): Raw pixel values own (bool): Evaluate own local model(s) Returns: fitness value (float) """ if self._own_eval and own: conf = self._evaluator.eval_own_nn(data, self._class_index) else: conf = self._evaluator.eval(data, self._class_index) image_diff = 1.0 if self._original is not None: img = data.clip(0, 1).astype(np.float32) image_diff = ssim(self._original, img, multichannel=True, data_range=1.0) if image_diff > 0.05: image_diff = image_diff * 1000 else: image_diff = 1.0 if conf == -1: image_diff = 1.0 return conf / image_diff
def eval(self, image: np.ndarray, class_index: int) -> float: """ Evaluates a given image (chromosome) for a specific class to the api. Args: image (np.ndarray): image to be evaluated class_index (int): class index to evaluate Returns: confidence value (float) """ img = image.clip(0, 1).astype(np.float32) img = np.floor(img * 255) / 255 labels, confs = self.__connector.send_query(img) if class_index == -1: max_conf = max(confs) return max_conf else: for i, label in enumerate(labels): if label == self.__index_to_label[str(class_index)]: return confs[i] return -1
def flow_to_rgb(flow: np.ndarray, maxval: Union[int, float] = 20) -> np.ndarray: """ Convert optic flow to RGB by linearly mapping X to red and Y to green 255 in the resulting image will correspond to `maxval`. 0 corresponds to -`maxval`. Parameters ---------- flow: np.ndarray. shape: (H, W, 2) optic flow. dX is the 0th channel, dy the 1st maxval: float the maximum representable value of the optic flow. Returns ------- flow_map: np.ndarray. shape: (H, W, 3) RGB image """ H, W, C = flow.shape assert (C == 2) flow = (flow + maxval) * (255 / 2 / maxval) flow = flow.clip(min=0, max=255) flow_map = np.ones((H, W, 3), dtype=np.uint8) * 127 # X -> red channel flow_map[..., 0] = flow[..., 0] # Y -> green channel flow_map[..., 1] = flow[..., 1] # Z is all ones return flow_map
def cv2_imshow( image: np.ndarray, imshow_scale: Optional[float] = None, convert_bgr_to_rgb: bool = True, ) -> None: """ A replacement for cv2.imshow() for use in Jupyter notebooks. Args: image (np.ndarray):. shape (N, M) or (N, M, 1) is an NxM grayscale image. shape (N, M, 3) is an NxM BGR color image. shape (N, M, 4) is an NxM BGRA color image. imshow_scale (Optional[float]): zoom ratio to show the image convert_bgr_to_rgb (bool): switch to convert BGR to RGB channel. """ image = image.clip(0, 255).astype('uint8') # cv2 stores colors as BGR; convert to RGB if convert_bgr_to_rgb and image.ndim == 3: if image.shape[2] == 4: image = cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA) else: image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) if imshow_scale is not None: image = cv2.resize(image, None, fx=imshow_scale, fy=imshow_scale) display(Image.fromarray(image))
def normalize_fixed( src: np.ndarray, in_min: float, in_max: float ) -> np.ndarray: """ Rescale image intensity. Rescale an grayscale image's intensity range from [min_, max_] to [0.0, 1.0]. Intensity value out of [min_, max_] will be clipped. Args: src: image to be intensity rescaled. in_min: input min value for intensity mapping in_max: input max value for intensity mapping Return: intensity rescaled image of type np.float32. """ assert in_min < in_max src = src.clip(in_min, in_max) src_float = src.astype(np.float32) a = 1. / (in_max - in_min) b = -in_min / (in_max - in_min) dst = a * src_float + b return dst
def array_to_img(x: np.ndarray, mode='RGB', data_format=None, min_val=0, max_val=255): """Convert an ndarray to PIL Image.""" x = np.squeeze(x).astype('float32') x = (x - min_val) / (max_val - min_val) x = x.clip(0, 1) * 255 if data_format not in ('channels_first', 'channels_last'): data_format = DATA_FORMAT if np.ndim(x) == 2: return Image.fromarray(x.astype('uint8'), mode='L').convert(mode) elif np.ndim(x) == 3: if data_format == 'channels_first': x = x.transpose([1, 2, 0]) return Image.fromarray(x.astype('uint8'), mode=mode) elif np.ndim(x) == 4: if data_format == 'channels_first': x = x.transpose([0, 2, 3, 1]) ret = [ Image.fromarray(np.round(i).astype('uint8'), mode=mode) for i in x ] return ret.pop() if len(ret) is 1 else ret elif np.ndim(x) >= 5: raise ValueError(f"Dimension of x must <= 4. Got {np.ndim(x)}.")
def set_focal_point(self, center_x_y: np.ndarray, zoom, barrel): """Set where we'll focus in the next frame once we can move.""" self.state.unclipped_center = center_x_y.copy() self.state.unclipped_zoom = zoom self.state.unclipped_barrel = barrel self.state.center = center_x_y.clip(self.CENTER_MIN, self.CENTER_MAX) self.state.zoom = min(max(zoom, self.ZOOM_MIN), self.ZOOM_MAX) self.state.barrel = min(max(barrel, self.BARREL_MIN), self.BARREL_MAX)
def nwrmsle(y_true: np.ndarray, y_pred: np.ndarray, weights: np.ndarray): """ Calculates and returns the Normalized Weighted Root Mean Squared Logarithmic Error Args: y_true (np.ndarray): The true labels y_pred (np.ndarray): The predicted labels weights (np.ndarray): The weights for each predictions Returns: int: The NWRMSLE """ assert y_true.shape == y_pred.shape == weights.shape, "Arguments are not of same shape" y_true = y_true.clip(min=0) y_pred = y_pred.clip(min=0) return np.sqrt( np.sum(weights * np.square(np.log1p(y_pred) - np.log1p(y_true))) / np.sum(weights))
def handle(self, frame: np.ndarray) -> np.ndarray: frame = np.array(frame, dtype=float) if self.area >= 1: frame.flat += np.random.normal(0, self.std, frame.size) elif self.area > 0: amount = int(self.area * frame.size) flat_indices = np.random.choice(frame.size, amount, False) frame.flat[flat_indices] += np.random.normal(0, self.std, amount) return frame.clip(0, 255).astype(np.uint8)
def colorize( image: np.ndarray, clipping_range: Tuple[Optional[int], Optional[int]] = (None, None), colormap: int = cv2.COLORMAP_HSV, ) -> np.ndarray: if clipping_range[0] or clipping_range[1]: img = image.clip(clipping_range[0], clipping_range[1]) else: img = image.copy() img = cv2.normalize(img, None, 0, 255, cv2.NORM_MINMAX, dtype=cv2.CV_8U) img = cv2.applyColorMap(img, colormap) return img
def preprocess_img(img: np.ndarray, new_shape): img = img.clip(10000, 50000) img = cv2.normalize(img, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F) img = BaseDataset.reshape_resize(img, new_shape) return img
def _deconvolution_extract_stain(self, image: np.ndarray) -> np.ndarray: """Perform Stain Deconvolution and return stain matrix for the image. Args: image: uint8 RGB image to perform stain deconvolution on Return: he: H&E absorbance matrix for the image (first column is H, second column is E, rows are RGB values) """ # check image type and values if not isinstance(image, np.ndarray): raise TypeError("Image must be of type numpy.ndarray.") if image.min() < 0: raise ValueError("Image should not have negative values.") if image.max() > 255: raise ValueError("Image should not have values greater than 255.") # reshape image and calculate absorbance image = image.reshape((-1, 3)) image = image.astype(np.float32, copy=False) + 1.0 absorbance = -np.log(image.clip(max=self.tli) / self.tli) # remove transparent pixels absorbance_hat = absorbance[np.all(absorbance > self.beta, axis=1)] if len(absorbance_hat) == 0: raise ValueError( "All pixels of the input image are below the absorbance threshold." ) # compute eigenvectors _, eigvecs = np.linalg.eigh( np.cov(absorbance_hat.T).astype(np.float32, copy=False)) # project on the plane spanned by the eigenvectors corresponding to the two largest eigenvalues t_hat = absorbance_hat.dot(eigvecs[:, 1:3]) # find the min and max vectors and project back to absorbance space phi = np.arctan2(t_hat[:, 1], t_hat[:, 0]) min_phi = np.percentile(phi, self.alpha) max_phi = np.percentile(phi, 100 - self.alpha) v_min = eigvecs[:, 1:3].dot( np.array([(np.cos(min_phi), np.sin(min_phi))], dtype=np.float32).T) v_max = eigvecs[:, 1:3].dot( np.array([(np.cos(max_phi), np.sin(max_phi))], dtype=np.float32).T) # a heuristic to make the vector corresponding to hematoxylin first and the one corresponding to eosin second if v_min[0] > v_max[0]: he = np.array((v_min[:, 0], v_max[:, 0]), dtype=np.float32).T else: he = np.array((v_max[:, 0], v_min[:, 0]), dtype=np.float32).T return he
def project_to_mnist_input(x: np.ndarray, preprocessing_fn: Callable): """ Map input tensor to original space """ if x.dtype not in (float, np.float32): raise ValueError("Projection assumes float input") if np.isnan(x).any(): raise ValueError("Cannot project nan values to int input") # clip first to deal with any infinite values that may arise x = (x.clip(0, 1) * 255.0).round() # skip the actual casting to uint8, as we are going to normalize again to float return preprocessing_fn(x)
def scale_chw_matrix(matrix: np.ndarray, min_percentile=1, max_percentile=99): try: c, h, w = matrix.shape except ValueError: h, w = matrix.shape c = 1 matrix = matrix.reshape((c, h * w)).astype(np.float64) # Get 2nd and 98th percentile mins = np.nanpercentile(matrix, min_percentile, axis=1) maxs = np.nanpercentile(matrix, max_percentile, axis=1) matrix = (matrix - mins[:, None]) / (maxs[:, None] - mins[:, None]) matrix = matrix.reshape((c, h, w)) matrix = matrix.clip(0., 1.) return matrix
def get_single_level_predictions(self, num_jobs: np.ndarray, resource_cap=None, runtime=None): if resource_cap is None: resource_cap = self.resource_cap # assume we are using the whole system if runtime is None: runtime = self.get_avg_runtime(num_jobs, resource_cap) sched_bottleneck = num_jobs / self.sched_rate resource_bottleneck = (num_jobs * runtime) / num_jobs.clip( max=resource_cap ) # clip(max=X) sets all values greater than X to X. equivalent to .apply(lambda x: min(x, X)) true_bottleneck = np.maximum(sched_bottleneck, resource_bottleneck) init_cost = self.sched_create_cost return true_bottleneck + init_cost
def save_images(images_file: str, images: np.ndarray, asrgb=False): """ Save the images in a file in MNIST format :param str mnist_file: file to save the images :param np.ndarray images: images to save """ def isqrt(i): return int(sqrt(float(i))) def write32(f, i): raw_int = struct.pack(">l", i) f.write(raw_int) # if the images are in the range [0,1], convert them if images.dtype != np.uint8: images = (images.clip(0, 1) * 255).astype(np.uint8) if asrgb: if len(images.shape) == 2: n, s = images.shape n *= 3 s //= 3 h = w = isqrt(s) else: n, c, h, w = images.shape n *= c else: if len(images.shape) == 2: n, s = images.shape h = w = isqrt(s) else: n, h, w = images.shape if images_file.endswith(".gz"): f = gzip.open(images_file, mode="wb") else: f = open(images_file, mode="wb") # with open(images_file, mode="wb") as f: with f: write32(f, 2051) # magick write32(f, n) # number of images write32(f, w) # width write32(f, h) # height data = images.tobytes() f.write(data)
def step(self, action: np.ndarray) -> Tuple[np.ndarray, float, bool, dict]: action = action.clip(self.action_space.low, self.action_space.high) assert action.shape == (1,) # Move time forward self._t += 1 # Update velocity with action and compute new position self._v += action[0] / self._mass self._x += self._dt * self._v done = self._t == self._max_episode_steps effort_penalty = -0.01 * np.abs(action[0]) proximity_reward = -0.1 * np.abs(self._x - self._task_target) ** 2 reward = proximity_reward + effort_penalty return self._compute_state(), reward, done, {'task_idx': self._this_task_idx}
def __call__(self, input_array_or_list, target_array: np.ndarray): if self.target_min_value is None and self.target_max_value is None: clipped_target_array = target_array else: clipped_target_array = target_array.clip(min=self.target_min_value, max=self.target_max_value) if isinstance(input_array_or_list, np.ndarray): input_array_list = [input_array_or_list] input_min_value = [self.input_min_value] input_max_value = [self.input_max_value] else: input_array_list = input_array_or_list if isinstance(self.input_min_value, int): input_min_value = [self.input_min_value ] * len(input_array_list) else: input_min_value = self.input_min_value if isinstance(self.input_max_value, int): input_max_value = [self.input_max_value ] * len(input_array_list) else: input_max_value = self.input_max_value assert len(input_array_list) == len(input_min_value) == len( input_max_value) clipped_input_array_list = [] for input_array, min_value, max_value in zip(input_array_list, input_min_value, input_max_value): if min_value is None and max_value is None: clipped_input_array = input_array else: clipped_input_array = input_array.clip(min=min_value, max=max_value) clipped_input_array_list.append(clipped_input_array) if len(clipped_input_array_list) == 1: return clipped_input_array_list[0], clipped_target_array else: return clipped_input_array_list, clipped_target_array
def __call__(self, image: np.ndarray) -> np.ndarray: """Perform stain normalization. Args: image: uint8 RGB image/patch to be stain normalized, pixel values between 0 and 255 Return: image_norm: stain normalized image/patch """ # check image type and values if not isinstance(image, np.ndarray): raise TypeError("Image must be of type numpy.ndarray.") if image.min() < 0: raise ValueError("Image should not have negative values.") if image.max() > 255: raise ValueError("Image should not have values greater than 255.") # extract stain of the image he = self.stain_extractor(image) # reshape image and calculate absorbance h, w, _ = image.shape image = image.reshape((-1, 3)) image = image.astype(np.float32) + 1.0 absorbance = -np.log(image.clip(max=self.tli) / self.tli) # rows correspond to channels (RGB), columns to absorbance values y = np.reshape(absorbance, (-1, 3)).T # determine concentrations of the individual stains conc = np.linalg.lstsq(he, y, rcond=None)[0] # normalize stain concentrations max_conc = np.asarray( [np.percentile(conc[0, :], 99), np.percentile(conc[1, :], 99)], dtype=np.float32) tmp = np.divide(max_conc, self.max_cref, dtype=np.float32) image_c = np.divide(conc, tmp[:, np.newaxis], dtype=np.float32) image_norm: np.ndarray = np.multiply( self.tli, np.exp(-self.target_he.dot(image_c)), dtype=np.float32) image_norm[image_norm > 255] = 254 image_norm = np.reshape(image_norm.T, (h, w, 3)).astype(np.uint8) return image_norm