예제 #1
0
    def display_growth_curve(self,
                             fit_interval_length=0.7,
                             min_lower_bound=1.8,
                             smooth_width=7):  # New intensity measure
        N_t = self.n_times
        time = np.linspace(0, (N_t - 1) * 0.25, N_t)

        pl = np.copy(self.colony_intensity())

        array_mask = pl > 0
        pl = pl[array_mask]
        time = time[array_mask]

        smooth_log = smoothen(np.log10(pl), smooth_width)
        smooth_time = time
        # Changed minimum to min of pl instead of min of smooth log because of smoothing function
        lower_bound = (np.max(smooth_log) + np.mean(np.sort(np.log10(pl))[:3])
                       - fit_interval_length) / 2

        if lower_bound < min_lower_bound:
            lower_bound = min_lower_bound

        upper_bound = lower_bound + fit_interval_length
        print(f"Lower bound is {lower_bound}, upper bound is {upper_bound}.")

        for k in range(len(smooth_log)):
            if smooth_log[k] > lower_bound:
                i_0 = k
                break

        for k in range(len(smooth_log)):
            if smooth_log[k] > upper_bound:
                i_f = k
                break

        if all(smooth_log < upper_bound):
            i_f = len(smooth_log) - 1

        a = np.polyfit(time[i_0:i_f], np.log10(pl[i_0:i_f]), 1)

        gen_time = np.log10(2) / a[0]

        print('Calculated generation time in hours is')
        print(gen_time)

        plt.figure(figsize=(12, 8))
        plt.semilogy(time, 10**(smooth_log), label='Smoothened curve')
        plt.semilogy(time, pl, '.', label='Measurement')
        plt.semilogy(time[i_0:i_f],
                     pl[i_0:i_f],
                     '.',
                     label='Timepoints included in fit')
        plt.semilogy(time[i_0:i_f],
                     10**(a[0] * time[i_0:i_f] + a[1]),
                     label='Fit')
        #plt.semilogy(time, pl, '.') # Just for now, for showing Andreas some data
        plt.xlabel('Time [h]')
        plt.ylabel('Intensity')
        plt.legend()
        plt.show()
예제 #2
0
 def plot_segment_intensity(self,
                            smooth_width=10,
                            seg_intensity_threshold=1000):
     plt.plot(self.segment_intensity(), label='Segment intensity')
     plt.plot(smoothen(self.segment_intensity(), smooth_width),
              label='Smoothened segment intensity')
     plt.plot(seg_intensity_threshold * np.ones(self.n_times),
              '--',
              label='Threshold')
     plt.legend()
     plt.show()
예제 #3
0
 def create_threshold_timepoint(self,
                                seg_intensity_threshold=1000,
                                smooth_width=10,
                                growth_threshold=600):
     a = self.segment_intensity()
     a = smoothen(a, smooth_width)
     #self._threshold_timepoint = None
     #"""
     try:
         self._threshold_timepoint = np.where(
             a > seg_intensity_threshold)[0][0]
     except (IndexError):
         self._threshold_timepoint = self.n_times - 1
         warn(
             "Segment intensity threshold was not reached. Colony area mask will be created from last picture in time lapse."
         )
         if not np.any(a > growth_threshold):
             self._threshold_timepoint = None
예제 #4
0
    def generation_time(self,
                        fit_interval_length=0.7,
                        min_lower_bound=1.8,
                        smooth_width=7):  # New intensity measure
        N_t = self.n_times
        time = np.linspace(0, (N_t - 1) * 0.25, N_t)
        pl = np.empty((3, N_t))

        pl = self.colony_intensity()

        if np.min(pl) < 0:
            pl = pl + 1.05 * abs(np.min(pl))

        smooth_log = smoothen(
            np.log10(pl)[np.logical_not(np.isnan(np.log10(pl)))], smooth_width)
        smooth_time = time[np.logical_not(np.isnan(np.log10(pl)))]
        n_nan = np.sum(np.isnan(np.log10(pl)))
        # Changed minimum to min of pl instead of min of smooth log because of smoothing function
        lower_bound = (np.max(smooth_log) + np.min(np.log10(pl)) -
                       fit_interval_length) / 2

        if lower_bound < min_lower_bound:
            lower_bound = min_lower_bound
        upper_bound = lower_bound + fit_interval_length

        for k in range(len(smooth_log)):
            if smooth_log[k] > lower_bound:
                i_0 = k + n_nan
                break

        for k in range(len(smooth_log)):
            if smooth_log[k] > upper_bound:
                i_f = k + n_nan
                break

        a = np.polyfit(time[i_0:i_f], np.log10(pl[i_0:i_f]), 1)

        gen_time = np.log10(2) / a[0]
        return gen_time
예제 #5
0
    def segment(self):
        """
		Tries to automatically segment the images into individual colonies.
		"""
        intensities = color_distance(self.temp_mean, self.background)
        profiles = [np.average(intensities, axis=i) for i in (1, 0)]

        for smooth_width in range(1, int(self.resolution[0] / self.layout[0])):
            self._coordinates = [None, None]
            for i in (0, 1):
                smooth_profile = smoothen(profiles[i], smooth_width)
                self._coordinates[i] = argrelmax(smooth_profile)[0]
                if len(self._coordinates[i]) != self.layout[i]:
                    # bad smooth width → continue outer loop
                    break
            else:
                # good smooth width → break outer loop
                break
        else:
            # no good smooth width at all:
            raise ColonyscopyFailedHeuristic(
                "Could not detect colony coordinates. Check if layout in right order."
            )
예제 #6
0
    def test_growth_curve_computation(self,
                                      fit_interval_length=0.7,
                                      min_lower_bound=1.8,
                                      smooth_width=7):
        N_t = self.n_times
        time = np.linspace(0, (N_t - 1) * 0.25, N_t)
        pl = np.copy(self.colony_intensity)

        array_mask = pl > 0
        pl = pl[array_mask]
        time = time[array_mask]

        smooth_log = smoothen(np.log10(pl), smooth_width)
        smooth_time = time
        # 		Changed minimum to mean of 3 smallest values of pl, bc smoothed function weird and min(pl) random
        lower_bound = (np.max(smooth_log) + np.mean(np.sort(np.log10(pl))[:3])
                       - fit_interval_length) / 2

        if lower_bound < min_lower_bound:
            lower_bound = min_lower_bound

        upper_bound = lower_bound + fit_interval_length

        for k in range(len(smooth_log)):
            if smooth_log[k] > lower_bound:
                i_0 = k
                break

        for k in range(len(smooth_log)):
            if smooth_log[k] > upper_bound:
                i_f = k
                break

        if all(smooth_log < upper_bound):
            i_f = len(smooth_log) - 1

        a = np.polyfit(time[i_0:i_f], np.log10(pl[i_0:i_f]), 1)

        gen_time = np.log10(2) / a[0]

        print(f"Calculated generation time is {gen_time} hours.")
        print(f"Starting point of fit is at {time[i_0]} hours.")

        plt.figure(figsize=(12, 8))
        plt.semilogy(time, pl, '.', label='Measurement')
        plt.semilogy(time[i_0:i_f],
                     pl[i_0:i_f],
                     '.',
                     label='Timepoints included in fit')
        plt.semilogy(time[i_0:i_f],
                     10**(a[0] * time[i_0:i_f] + a[1]),
                     label='Fit')
        plt.ylim(bottom=5)
        plt.xlabel('Time [h]')
        plt.ylabel('Intensity')
        plt.legend()
        plt.show()

        plt.imshow(self.speckle_mask, cmap='gray')
        plt.title('Speckle mask')
        plt.show()

        plt.imshow(self.background_mask, cmap='gray')
        plt.title('Background mask')
        plt.show()

        plt.imshow(self.mask, cmap='gray')
        plt.title('Colony area mask')
        plt.show()

        k = 0
        for image in color_sum(self.images)[::10, :, :]:
            print(f"Time {time[k]} hours.")
            k += 10
            plt.imshow(image, cmap='gray')
            plt.show()

        plt.imshow(color_sum(self.temp_mean))
        plt.colorbar()
        plt.show()

        plt.imshow(np.multiply(color_sum(self.temp_mean), self.mask))
        plt.clim(np.min(color_sum(self.temp_mean)[self.mask]),
                 np.max(color_sum(self.temp_mean)[self.mask]))
        plt.colorbar()
        plt.show()

        print(
            f"Standard deviation of temporal mean intensity in colony area mask is {np.std(color_sum(self.temp_mean)[self.mask].ravel())/np.mean(color_sum(self.temp_mean)[self.mask].ravel())}"
        )