def create_background_mask(self, expansion=6): """ Creates a mask that only includes background pixels of this segment. TODO: explain parameter """ if self.threshold_timepoint is None: self._background_mask = np.logical_not( expand_mask(self.mask, width=int(1.6 * expansion)) + np.logical_not(self.speckle_mask)) else: self._background_mask = np.logical_not( expand_mask(self.mask, width=expansion) + np.logical_not(self.speckle_mask))
def create_colony_intensity(self): bg_intensity = np.empty(self.n_times) col_intensity = np.empty(self.n_times) self._colony_intensity = np.empty(self.n_times) # Loop is necessary due to mask destroying structure for t in range(self.n_times): col_intensity[t] = np.mean( color_sum(self.images[t, :, :])[self.mask]) bg_intensity[t] = np.median( color_sum(self.images[t, :, :])[self.background_mask]) intensity = (col_intensity - bg_intensity) self._colony_intensity = intensity if (np.mean(intensity[:6]) < 0): factor = 1.5 if (np.mean(intensity[:6]) < -20) else 1.8 background_px = color_sum(self.images[0])[self.background_mask] bright_px = np.multiply( color_sum(self.images[0]), self.background_mask ) > np.mean(background_px) + factor * np.std(background_px) bright_px = expand_mask(bright_px, width=3) self._background_mask &= np.logical_not(bright_px) try: self.create_colony_intensity() except (RecursionError): bg_intensity = np.empty(self.n_times) col_intensity = np.empty(self.n_times) # Loop is necessary due to mask destroying structure for t in range(self.n_times): col_intensity[t] = np.mean( color_sum(self.images[t, :, :])[self.mask]) bg_intensity[t] = np.median( color_sum(self.images[t, :, :])[self.background_mask]) intensity = (col_intensity - bg_intensity)
def test_point(self, apply_mask, needs_expansion): point = tuple([np.random.randint(x) for x in size]) colour = np.random.randint(ncolours) background = np.zeros((*size, ncolours), dtype=np.uint8) background[(*point, colour)] = 1 dummy_plate = Plate( images=np.zeros([times, *size, ncolours]), bg=background, ) mask = apply_mask(dummy_plate) if needs_expansion: mask = expand_mask(mask, 1) assert mask[point] if needs_expansion: far_from_point = np.logical_not( expand_mask(background[:, :, colour] > 0, 2)) assert not np.any(mask[far_from_point]) else: assert np.sum(mask) == 1
def create_speckle_mask( self, gradient_threshold=1000, intensity_factor=4, temporal_threshold=1200, expansion=3, ): """ Tries to automatically detect speckles. TODO: explain parameters """ self._speckle_mask = np.logical_not( expand_mask(self._gradient_mask(gradient_threshold) | self._intensity_mask(intensity_factor) | self._temporal_mask(temporal_threshold), width=expansion))
def test_points(self): size = (100,80) points = np.vstack([ np.random.randint(n,size=5) for n in size ]).T radius = 13 mask = np.zeros(size,dtype=bool) for point in points: mask[tuple(point)] = True expanded_mask = expand_mask(mask,radius) for i in range(size[0]): for j in range(size[1]): dist = min( np.linalg.norm(point-[i,j]) for point in points ) assert (dist<=radius) == expanded_mask[i,j]
def test_zero_radius(self): random_mask = np.random.choice([True,False],size=(100,80)) assert_array_equal( expand_mask(random_mask,0), random_mask )