Exemple #1
0
    def analyze_images_in_folder(
        self,
        folder,
        generate_zmax=False,
        show_result=True,
        save_mask=True,
        save_excel=True,
    ):
        """
        Given the folder, perform general analysis over the images in it.

        Parameters
        ----------
        folder : str
            Path to the folder.
        generate_zmax : bool, optional
            Whether to calcaulate the z-max projection first. The default is False.
        show_result : bool, optional
            If show the machine learning segmentation results. The default is True.
        save_mask : bool, optional
            DESCRIPTION. The default is True.
        save_excel : bool, optional
            DESCRIPTION. The default is True.

        Returns
        -------
        cell_Data : pd.dataframe
            DESCRIPTION.

        """
        flat_cell_counted_in_folder = 0
        total_cells_counted_in_folder = 0
        background_substraction = False
        root_folder = folder

        # If need to do zmax projection first
        if generate_zmax == True:
            ProcessImage.cam_screening_post_processing(root_folder)
            # Here a new folder for maxProjection is generated inside, change the path
            folder = os.path.join(root_folder, "maxProjection")

        # If background images are taken
        if os.path.exists(os.path.join(root_folder, "background")):
            # If the background image is taken to substract out
            background_substraction = True
            print("Run background substraction.")

            # Get all the background files names
            background_fileNameList = []
            for file in os.listdir(os.path.join(root_folder, "background")):
                if "calculated background" not in file:
                    if "tif" in file or "TIF" in file:
                        background_fileNameList.append(
                            os.path.join(root_folder, "background", file))

            # Average over multiple images
            background_image = ProcessImage.image_stack_calculation(
                background_fileNameList, operation="mean")

            # # Smooth the image
            # background_image = ProcessImage.average_filtering(
            #     background_image, filter_side_length = 25)

            # Save the individual file.
            with skimtiff.TiffWriter(
                    os.path.join(root_folder, "background",
                                 "calculated background.tif"),
                    imagej=True,
            ) as tif:
                tif.save(background_image.astype(np.uint16), compress=0)

        # Get a list of file names
        fileNameList = []
        for file in os.listdir(folder):
            if "tif" in file and "LED" not in file:
                fileNameList.append(file)

        print(fileNameList)

        # Analyse each image
        for image_file_name in fileNameList:
            print(image_file_name)
            Rawimage = imread(os.path.join(folder, image_file_name))

            if background_substraction == True:
                Rawimage = np.abs(Rawimage - background_image).astype(
                    np.uint16)

                camera_dark_level = 100

                # # Normalize to the illumination intensity
                # Rawimage = np.uint16(Rawimage \
                #         / ((background_image - camera_dark_level)\
                #            /(np.amin(background_image) - camera_dark_level)))

            # Analyze each image
            # Run the detection on input image.
            MLresults = self.DetectionOnImage(Rawimage,
                                              axis=None,
                                              show_result=show_result)

            if save_mask == True:

                if not os.path.exists(os.path.join(folder, "ML_masks")):
                    # If the folder is not there, create the folder
                    os.mkdir(os.path.join(folder, "ML_masks"))

                fig, ax = plt.subplots()
                # Set class_names = [None,None,None,None] to mute class name display.
                visualize.display_instances(
                    Rawimage,
                    MLresults["rois"],
                    MLresults["masks"],
                    MLresults["class_ids"],
                    class_names=[None, None, None, None],
                    ax=ax,
                    centre_coors=MLresults["Centre_coor"],
                    Centre_coor_radius=2,
                    WhiteSpace=(0, 0),
                )  # MLresults['class_ids'],MLresults['scores'],
                # ax.imshow(fig)
                fig.tight_layout()
                # Save the detection Rawimage
                fig_name = os.path.join(
                    folder,
                    "ML_masks",
                    "ML_mask_{}.png".format(
                        image_file_name[0:len(image_file_name) - 4]),
                )
                plt.savefig(fname=fig_name,
                            dpi=200,
                            pad_inches=0.0,
                            bbox_inches="tight")

            if flat_cell_counted_in_folder == 0:
                (
                    cell_Data,
                    flat_cell_counted_in_folder,
                    total_cells_counted_in_coord,
                ) = ProcessImage.retrieveDataFromML(
                    Rawimage, MLresults, image_file_name,
                    flat_cell_counted_in_folder)
            else:
                (
                    Cell_Data_new,
                    flat_cell_counted_in_folder,
                    total_cells_counted_in_coord,
                ) = ProcessImage.retrieveDataFromML(
                    Rawimage, MLresults, image_file_name,
                    flat_cell_counted_in_folder)
                if len(Cell_Data_new) > 0:
                    cell_Data = cell_Data.append(Cell_Data_new)
            total_cells_counted_in_folder += total_cells_counted_in_coord

        if save_excel == True:
            # Save to excel
            cell_Data.to_excel(
                os.path.join(
                    folder,
                    "CellsProperties_{}flat_outof_{}cells.xlsx".format(
                        flat_cell_counted_in_folder,
                        total_cells_counted_in_folder),
                ))

        return cell_Data
Exemple #2
0
    def analyze_images_in_folder(self,
                                 folder,
                                 generate_zmax=False,
                                 show_result=True,
                                 save_mask=True,
                                 save_excel=True):
        """
        Given the folder, perform general analysis over the images in it.

        Parameters
        ----------
        folder : str
            Path to the folder.
        generate_zmax : bool, optional
            Whether to calcaulate the z-max projection first. The default is False.
        show_result : bool, optional
            If show the machine learning segmentation results. The default is True.
        save_mask : bool, optional
            DESCRIPTION. The default is True.
        save_excel : bool, optional
            DESCRIPTION. The default is True.

        Returns
        -------
        cell_Data : pd.dataframe
            DESCRIPTION.

        """
        flat_cell_counted_in_folder = 0
        total_cells_counted_in_folder = 0

        # If need to do zmax projection first
        if generate_zmax == True:
            ProcessImage.cam_screening_post_processing(folder)
            # Here a new folder for maxProjection is generated inside, change the path
            folder = os.path.join(folder, 'maxProjection')

        # If background images are taken
        if os.path.exists(os.path.join(folder, 'background')):
            # If the background image is taken to substract out
            background_substraction = True

            # Get all the background files names
            background_fileNameList = []
            for file in os.listdir(os.path.join(folder, 'background')):
                if "tif" in file:
                    background_fileNameList.append(
                        os.path.join(folder, 'background', file))

            background_image = ProcessImage.image_stack_calculation(
                background_fileNameList, operation="mean")

        # Get a list of file names
        fileNameList = []
        for file in os.listdir(folder):
            if "tif" in file and "LED" not in file:
                fileNameList.append(file)

        print(fileNameList)

        # Analyse each image
        for image_file_name in fileNameList:
            print(image_file_name)
            Rawimage = imread(os.path.join(folder, image_file_name))

            if background_substraction == True:
                Rawimage = np.abs(Rawimage - background_image)

            # Analyze each image
            # Run the detection on input image.
            MLresults = self.DetectionOnImage(Rawimage,
                                              axis=None,
                                              show_result=show_result)

            if save_mask == True:

                if not os.path.exists(os.path.join(folder, 'ML_masks')):
                    # If the folder is not there, create the folder
                    os.mkdir(os.path.join(folder, 'ML_masks'))

                fig, ax = plt.subplots()
                # Set class_names = [None,None,None,None] to mute class name display.
                visualize.display_instances(
                    Rawimage,
                    MLresults['rois'],
                    MLresults['masks'],
                    MLresults['class_ids'],
                    class_names=[None, None, None, None],
                    ax=ax,
                    centre_coors=MLresults['Centre_coor'],
                    Centre_coor_radius=2,
                    WhiteSpace=(
                        0, 0))  #MLresults['class_ids'],MLresults['scores'],
                # ax.imshow(fig)
                fig.tight_layout()
                # Save the detection Rawimage
                fig_name = os.path.join(
                    folder, 'ML_masks', 'ML_mask_{}.png'.format(
                        image_file_name[0:len(image_file_name) - 4]))
                plt.savefig(fname=fig_name,
                            dpi=200,
                            pad_inches=0.0,
                            bbox_inches='tight')

            if flat_cell_counted_in_folder == 0:
                cell_Data, flat_cell_counted_in_folder, total_cells_counted_in_coord = \
                    ProcessImage.retrieveDataFromML(Rawimage, MLresults, image_file_name, flat_cell_counted_in_folder)
            else:
                Cell_Data_new, flat_cell_counted_in_folder, total_cells_counted_in_coord = \
                    ProcessImage.retrieveDataFromML(Rawimage, MLresults, image_file_name, flat_cell_counted_in_folder)
                if len(Cell_Data_new) > 0:
                    cell_Data = cell_Data.append(Cell_Data_new)
            total_cells_counted_in_folder += total_cells_counted_in_coord

        if save_excel == True:
            # Save to excel
            cell_Data.to_excel(
                os.path.join(
                    folder, 'CellsProperties_{}flat_outof_{}cells.xlsx'.format(
                        flat_cell_counted_in_folder,
                        total_cells_counted_in_folder)))

        return cell_Data
Exemple #3
0
    def FluorescenceAnalysis(self, folder, round_num, save_mask=True):
        """
        # =============================================================================
        # Given the folder and round number, return a dictionary for the round
        # that contains each scanning position as key and structured array of detailed
        # information about each identified cell as content.
        #
        #   Returned structured array fields:
        #   - BoundingBox of cell ROI
        #   - Mean intensity of whole cell area
        #   - Mean intensity of cell membrane part
        #   - Contour soma ratio
        # =============================================================================

        Parameters
        ----------
        folder : string.
            The directory to folder where the screening data is stored.
        round_num : string.
            The target round number of analysis.
        save_mask: bool.
            Whether to save segmentation masks.

        Returns
        -------
        cell_Data : pd.DataFrame.
            Sum of return from func: retrieveDataFromML, for whole round.
        """
        RoundNumberList, CoordinatesList, fileNameList = self.retrive_scanning_scheme(
            folder, file_keyword="Zmax")
        # RoundNumberList, CoordinatesList, fileNameList = self.retrive_scanning_scheme(folder, file_keyword = 'Zfocus')

        if not os.path.exists(
                os.path.join(folder, "MLimages_{}".format(round_num))):
            # If the folder is not there, create the folder to store ML segmentations
            os.mkdir(os.path.join(folder, "MLimages_{}".format(round_num)))

        for EachRound in RoundNumberList:

            cells_counted_in_round = 0

            background_substraction = False
            # =============================================================================
            #             For background_substraction
            # =============================================================================
            # If background images are taken
            background_images_folder = os.path.join(
                folder, "background {}".format(EachRound))
            # print(background_images_folder)
            if os.path.exists(background_images_folder):
                # If the background image is taken to substract out
                background_substraction = True
                print("Run background substraction.")

                # Get all the background files names
                background_fileNameList = []
                for file in os.listdir(background_images_folder):
                    if "calculated background" not in file:
                        if "tif" in file or "TIF" in file:
                            background_fileNameList.append(
                                os.path.join(background_images_folder, file))

                background_image = ProcessImage.image_stack_calculation(
                    background_fileNameList, operation="mean")

                # # Smooth the background image
                # background_image = ProcessImage.average_filtering(
                #     background_image, filter_side_length = 25)

                # Save the individual file.
                with skimtiff.TiffWriter(
                        os.path.join(background_images_folder,
                                     "calculated background.tif"),
                        imagej=True,
                ) as tif:
                    tif.save(background_image.astype(np.uint16), compress=0)

            if EachRound == round_num:

                # Start numbering cells at each round
                self.cell_counted_inRound = 0

                for EachCoord in CoordinatesList:

                    # =============================================================================
                    #             For fluorescence:
                    # =============================================================================
                    print(EachCoord)
                    # -------------- readin image---------------
                    for Eachfilename in enumerate(fileNameList):
                        if (EachCoord in Eachfilename[1]
                                and EachRound in Eachfilename[1]):
                            if "Zmax" in Eachfilename[1]:
                                try:
                                    ImgNameInfor = Eachfilename[1][
                                        0:Eachfilename[1].index(
                                            "_PMT"
                                        )]  # get rid of '_PMT_0Zmax.tif' in the name.
                                except:
                                    ImgNameInfor = Eachfilename[1][
                                        0:Eachfilename[1].index(
                                            "_Cam"
                                        )]  # get rid of '_Cam_Zmax.tif' in the name.
                            elif "Zfocus" in Eachfilename[1]:
                                ImgNameInfor = Eachfilename[1][
                                    0:len(Eachfilename[1]) -
                                    16]  # get rid of '_PMT_0Zfocus.tif' in the name.
                            elif "Zpos1" in Eachfilename[1]:
                                ImgNameInfor = Eachfilename[1][0:len(
                                    Eachfilename[1]
                                )]  # get rid of '_PMT_0Zfocus.tif' in the name.
                            _imagefilename = os.path.join(
                                folder, Eachfilename[1])
                    # ------------------------------------------

                    # =========================================================================
                    #                     USING MASKRCNN...
                    # =========================================================================
                    # Imagepath      = self.Detector._fixPathName(_imagefilename)
                    Rawimage = imread(_imagefilename)

                    # Background substraction
                    if background_substraction == True:
                        Rawimage = np.abs(Rawimage - background_image)

                        camera_dark_level = 100

                        # # Normalize to the illumination intensity
                        # Rawimage = np.uint16(Rawimage \
                        #         / ((background_image - camera_dark_level)\
                        #             /(np.amin(background_image) - camera_dark_level)))

                    #                    if ClearImgBef == True:
                    #                        # Clear out junk parts to make it esaier for ML detection.
                    #                        RawimageCleared = self.preProcessMLimg(Rawimage, smallest_size=300, lowest_region_intensity=0.16)
                    #                    else:
                    #                        RawimageCleared = Rawimage.copy()

                    image = ProcessImage.convert_for_MaskRCNN(Rawimage)

                    # Run the detection on input image.
                    results = self.Detector.detect([image])

                    MLresults = results[0]

                    if save_mask == True:
                        fig, ax = plt.subplots()
                        # Set class_names = [None,None,None,None] to mute class name display.
                        visualize.display_instances(
                            image,
                            MLresults["rois"],
                            MLresults["masks"],
                            MLresults["class_ids"],
                            class_names=[None, None, None, None],
                            ax=ax,
                            centre_coors=MLresults["Centre_coor"],
                            Centre_coor_radius=2,
                            WhiteSpace=(0, 0),
                        )  # MLresults['class_ids'],MLresults['scores'],
                        # ax.imshow(fig)
                        fig.tight_layout()
                        # Save the detection image
                        fig_name = os.path.join(
                            folder, "MLimages_{}\{}.tif".format(
                                round_num, ImgNameInfor))
                        plt.savefig(fname=fig_name,
                                    dpi=200,
                                    pad_inches=0.0,
                                    bbox_inches="tight")

                    # segmentationImg = Image.fromarray(fig) #generate an image object
                    # segmentationImg.save(os.path.join(folder, 'MLimages_{}\{}.tif'.format(round_num, ImgNameInfor)))#save as tif

                    # Use retrieveDataFromML from ImageProcessing.py to extract numbers.
                    if self.cell_counted_inRound == 0:
                        (
                            cell_Data,
                            self.cell_counted_inRound,
                            total_cells_counted_in_coord,
                        ) = ProcessImage.retrieveDataFromML(
                            Rawimage,
                            MLresults,
                            str(ImgNameInfor),
                            self.cell_counted_inRound,
                            show_each_cell=False)
                    else:
                        (
                            Cell_Data_new,
                            self.cell_counted_inRound,
                            total_cells_counted_in_coord,
                        ) = ProcessImage.retrieveDataFromML(
                            Rawimage,
                            MLresults,
                            str(ImgNameInfor),
                            self.cell_counted_inRound,
                            show_each_cell=False)
                        if len(Cell_Data_new) > 0:
                            cell_Data = cell_Data.append(Cell_Data_new)

                    # Count in total how many flat and round cells are identified.
                    cells_counted_in_round += total_cells_counted_in_coord

                print("Number of round/flat cells in this round: {}".format(
                    cells_counted_in_round))

        # Save to excel
        cell_Data.to_excel(
            os.path.join(
                os.path.join(
                    folder,
                    round_num + "_" +
                    datetime.now().strftime("%Y-%m-%d_%H-%M-%S") +
                    "_CellsProperties.xlsx",
                )))

        return cell_Data