scripts/rreb1_tm1b_exp_0008_zeiss_annotations_postprocessing_v8_no_correction.py
def process_annotations(annotation_files_list, overwrite_aggregated_annotation_file=False, create_symlink=False):
    """
    Helper function to process a list of JSON files with annotations.
    :param annotation_files_list: list of JSON filenames containing annotations.
    :return:
    """

    for annotation_file in annotation_files_list:

        print('File: ' + os.path.basename(annotation_file))

        # name of the file that we are going to save the aggregated annotations to
        aggregated_annotation_file = annotation_file.replace('.json', '_aggregated.json')

        # name of the original histo file
        histo_file = os.path.basename(annotation_file).replace(auto_filename_suffix, '.tif')
        histo_file = os.path.join(histology_dir, histo_file)

        im = openslide.OpenSlide(histo_file)
        xres = 1e-2 / float(im.properties['tiff.XResolution']) * 1e6  # um
        yres = 1e-2 / float(im.properties['tiff.YResolution']) * 1e6  # um

        # aggregate cells from all blocks and write/overwrite a file with them
        if not os.path.isfile(aggregated_annotation_file) or overwrite_aggregated_annotation_file:

            # load contours and their confidence measure from annotation file
            cells, props = cytometer.data.aida_get_contours(annotation_file, layer_name='White adipocyte.*', return_props=True)

            # compute cell measures
            areas = []
            inv_compactnesses = []
            for cell in cells:
                poly_cell = shapely.geometry.Polygon(cell)
                area = poly_cell.area
                if area > 0:
                    inv_compactness = poly_cell.length ** 2 / (4 * np.pi * area)
                else:
                    inv_compactness = np.nan
                areas.append(area)
                inv_compactnesses.append(inv_compactness)

            if DEBUG:
                plt.clf()
                plt.hist(np.array(areas) * 1e-3 * xres * yres, bins=500, density=True)
                plt.xlim(0, 29)
                plt.xlabel('Area ($\mu m^2$)')

            # prepare for removal objects that are too large or too small
            idx = (np.array(areas) >= min_area) * (np.array(areas) <= max_area)

            # prepare for removal objects that are not compact enough
            idx *= np.array(inv_compactnesses) <= max_inv_compactness

            if DEBUG:
                plt.clf()
                plt.hist(np.array(props['cell_prob']), bins=500, density=True)
                plt.xlim(0, 29)
                plt.xlabel('Area ($\mu m^2$)')

            # prepare for removal objects unlikely to be cells
            idx *= np.array(props['cell_prob']) >= cell_prob_thr

            # execute the removal of objects
            cells = list(np.array(cells)[idx])
            props['cell_prob'] = list(np.array(props['cell_prob'])[idx])
            # areas = list(np.array(areas)[idx])

            # create AIDA items to contain contours
            items = cytometer.data.aida_contour_items(cells, f_area2quantile_m, cm='quantiles_aida',
                                                      xres=xres, yres=yres, cell_prob=props['cell_prob'])

            # write contours to single layer AIDA file (one to visualise, one to correct manually)
            cytometer.data.aida_write_new_items(aggregated_annotation_file, items, mode='w', indent=0)

        if create_symlink:

            # name expected by AIDA for annotations
            symlink_name = os.path.basename(histo_file).replace('.tif', '.json')
            symlink_name = os.path.join(annotations_dir, symlink_name)

            # create symlink to the aggregated annotation file from the name expected by AIDA
            if os.path.isfile(symlink_name):
                os.remove(symlink_name)
            os.symlink(os.path.basename(aggregated_annotation_file), symlink_name)

    return

    Helper function to process a list of JSON files with annotations.
    :param annotation_files_list: list of JSON filenames containing annotations.
    :return:
    
Helper function to process a list of JSON files with annotations.
Helper function to process a list of JSON files with annotations .
def process_annotations(annotation_files_list, overwrite_aggregated_annotation_file=False, create_symlink=False):
    for annotation_file in annotation_files_list:
        print('File: ' + os.path.basename(annotation_file))
        aggregated_annotation_file = annotation_file.replace('.json', '_aggregated.json')
        histo_file = os.path.basename(annotation_file).replace(auto_filename_suffix, '.tif')
        histo_file = os.path.join(histology_dir, histo_file)
        im = openslide.OpenSlide(histo_file)
        xres = 1e-2 / float(im.properties['tiff.XResolution']) * 1e6  
        yres = 1e-2 / float(im.properties['tiff.YResolution']) * 1e6  
        if not os.path.isfile(aggregated_annotation_file) or overwrite_aggregated_annotation_file:
            cells, props = cytometer.data.aida_get_contours(annotation_file, layer_name='White adipocyte.*', return_props=True)
            areas = []
            inv_compactnesses = []
            for cell in cells:
                poly_cell = shapely.geometry.Polygon(cell)
                area = poly_cell.area
                if area > 0:
                    inv_compactness = poly_cell.length ** 2 / (4 * np.pi * area)
                else:
                    inv_compactness = np.nan
                areas.append(area)
                inv_compactnesses.append(inv_compactness)
            if DEBUG:
                plt.clf()
                plt.hist(np.array(areas) * 1e-3 * xres * yres, bins=500, density=True)
                plt.xlim(0, 29)
                plt.xlabel('Area ($\mu m^2$)')
            idx = (np.array(areas) >= min_area) * (np.array(areas) <= max_area)
            idx *= np.array(inv_compactnesses) <= max_inv_compactness
            if DEBUG:
                plt.clf()
                plt.hist(np.array(props['cell_prob']), bins=500, density=True)
                plt.xlim(0, 29)
                plt.xlabel('Area ($\mu m^2$)')
            idx *= np.array(props['cell_prob']) >= cell_prob_thr
            cells = list(np.array(cells)[idx])
            props['cell_prob'] = list(np.array(props['cell_prob'])[idx])
            items = cytometer.data.aida_contour_items(cells, f_area2quantile_m, cm='quantiles_aida',
                                                      xres=xres, yres=yres, cell_prob=props['cell_prob'])
            cytometer.data.aida_write_new_items(aggregated_annotation_file, items, mode='w', indent=0)
        if create_symlink:
            symlink_name = os.path.basename(histo_file).replace('.tif', '.json')
            symlink_name = os.path.join(annotations_dir, symlink_name)
            if os.path.isfile(symlink_name):
                os.remove(symlink_name)
            os.symlink(os.path.basename(aggregated_annotation_file), symlink_name)
    return
def process_annotations ( annotation_files_list , overwrite_aggregated_annotation_file = False , create_symlink = False ) : for annotation_file in annotation_files_list : print ( 'File: ' + os . path . basename ( annotation_file ) ) aggregated_annotation_file = annotation_file . replace ( '.json' , '_aggregated.json' ) histo_file = os . path . basename ( annotation_file ) . replace ( auto_filename_suffix , '.tif' ) histo_file = os . path . join ( histology_dir , histo_file ) im = openslide . OpenSlide ( histo_file ) xres = 1e-2 / float ( im . properties [ 'tiff.XResolution' ] ) * 1e6 yres = 1e-2 / float ( im . properties [ 'tiff.YResolution' ] ) * 1e6 if not os . path . isfile ( aggregated_annotation_file ) or overwrite_aggregated_annotation_file : cells , props = cytometer . data . aida_get_contours ( annotation_file , layer_name = 'White adipocyte.*' , return_props = True ) areas = [ ] inv_compactnesses = [ ] for cell in cells : poly_cell = shapely . geometry . Polygon ( cell ) area = poly_cell . area if area > 0 : inv_compactness = poly_cell . length ** 2 / ( 4 * np . pi * area ) else : inv_compactness = np . nan areas . append ( area ) inv_compactnesses . append ( inv_compactness ) if DEBUG : plt . clf ( ) plt . hist ( np . array ( areas ) * 1e-3 * xres * yres , bins = 500 , density = True ) plt . xlim ( 0 , 29 ) plt . xlabel ( 'Area ($\mu m^2$)' ) idx = ( np . array ( areas ) >= min_area ) * ( np . array ( areas ) <= max_area ) idx *= np . array ( inv_compactnesses ) <= max_inv_compactness if DEBUG : plt . clf ( ) plt . hist ( np . array ( props [ 'cell_prob' ] ) , bins = 500 , density = True ) plt . xlim ( 0 , 29 ) plt . xlabel ( 'Area ($\mu m^2$)' ) idx *= np . array ( props [ 'cell_prob' ] ) >= cell_prob_thr cells = list ( np . array ( cells ) [ idx ] ) props [ 'cell_prob' ] = list ( np . array ( props [ 'cell_prob' ] ) [ idx ] ) items = cytometer . data . aida_contour_items ( cells , f_area2quantile_m , cm = 'quantiles_aida' , xres = xres , yres = yres , cell_prob = props [ 'cell_prob' ] ) cytometer . data . aida_write_new_items ( aggregated_annotation_file , items , mode = 'w' , indent = 0 ) if create_symlink : symlink_name = os . path . basename ( histo_file ) . replace ( '.tif' , '.json' ) symlink_name = os . path . join ( annotations_dir , symlink_name ) if os . path . isfile ( symlink_name ) : os . remove ( symlink_name ) os . symlink ( os . path . basename ( aggregated_annotation_file ) , symlink_name ) return
Helper function to process a list of JSON files with annotations.
Helper function to process a list of JSON files with annotations .
"""
    Helper function to process a list of JSON files with annotations.
    :param annotation_files_list: list of JSON filenames containing annotations.
    :return:
    """ # name of the file that we are going to save the aggregated annotations to # name of the original histo file # um # um # aggregate cells from all blocks and write/overwrite a file with them # load contours and their confidence measure from annotation file # compute cell measures # prepare for removal objects that are too large or too small # prepare for removal objects that are not compact enough # prepare for removal objects unlikely to be cells # execute the removal of objects # areas = list(np.array(areas)[idx]) # create AIDA items to contain contours # write contours to single layer AIDA file (one to visualise, one to correct manually) # name expected by AIDA for annotations # create symlink to the aggregated annotation file from the name expected by AIDA
None
annotation_files_list
list of JSON filenames containing annotations.
list of JSON filenames containing annotations .
overwrite_aggregated_annotation_file
create_symlink