Beispiel #1
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
            os.mkdir(os.path.join(folder, 'MLimages_{}'.format(round_num)))

        for EachRound in RoundNumberList:

            cells_counted_in_round = 0

            if EachRound == round_num:

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

                for EachCoord in CoordinatesList:

                    # =============================================================================
                    #             For tag fluorescence:
                    # =============================================================================
                    print(EachCoord)
                    #-------------- readin image---------------
                    for Eachfilename in enumerate(fileNameList):
                        if EachCoord in Eachfilename[
                                1] and EachRound in Eachfilename[1]:
                            if '0Zmax' in Eachfilename[1]:
                                ImgNameInfor = Eachfilename[1][
                                    0:len(Eachfilename[1]) -
                                    14]  # get rid of '_PMT_0Zmax.tif' in the name.
                            elif '0Zfocus' in Eachfilename[1]:
                                ImgNameInfor = Eachfilename[1][
                                    0:len(Eachfilename[1]) -
                                    16]  # 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)

                    #                    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

                    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)
                    else:
                        Cell_Data_new, self.cell_counted_inRound, total_cells_counted_in_coord = \
                            ProcessImage.retrieveDataFromML(Rawimage, MLresults, str(ImgNameInfor), self.cell_counted_inRound)
                        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
Beispiel #2
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