Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create mantis-friendly directory to evaluate clustering accuracy #563

Closed
ngreenwald opened this issue May 25, 2022 · 0 comments · Fixed by #641
Closed

Create mantis-friendly directory to evaluate clustering accuracy #563

ngreenwald opened this issue May 25, 2022 · 0 comments · Fixed by #641
Assignees
Labels
enhancement New feature or request

Comments

@ngreenwald
Copy link
Member

Is your feature request related to a problem? Please describe.
Mantis is super useful in order to evaluate the quality of the clustering reuslts, but requires a specific formatting

Describe the solution you'd like
Add the option to automatically create the required structure at the end of the pixel and cell clustering notebooks. Here's a quick function I wrote. This can get cleaned up, tested, etc

def create_mantis_project(mantis_project_path, img_data_path, mask_output_dir, mask_suffix, mapping_path, 
                          seg_dir, img_sub_folder='normalized'):
    
    if not os.path.exists(mantis_project_path):
        os.makedirs(mantis_project_path)
        
    # create key from cluster number to cluster name
    map_df = pd.read_csv(mapping_path)
    map_df = map_df.loc[:, ['metacluster', 'mc_name']]

    # remove duplicates from df
    map_df = map_df.drop_duplicates()
    map_df = map_df.sort_values(by=['metacluster'])
    
    # rename for mantis names
    map_df = map_df.rename({'metacluster': 'region_id', 'mn_name': 'region_name'}, axis=1)
    
    # get names of fovs with masks
    mask_names = io_utils.list_files(mask_output_dir, mask_suffix)
    fov_names = io_utils.extract_delimited_names(mask_names, delimiter=mask_suffix)
    
    # create a folder with image data, pixel masks, and segmentation mask
    for idx, val in enumerate(fov_names):
        
        # set up paths
        img_source_dir = os.path.join(img_data_path, val, img_sub_folder)
        output_dir = os.path.join(mantis_project_path, val)
        
        # copy image data if not already copied in from previous round of clustering
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)

            # copy all channels into new folder
            chans = io_utils.list_files(img_source_dir, '.tiff')
            for chan in chans:
                shutil.copy(os.path.join(img_source_dir, chan), os.path.join(output_dir, chan))

        # copy mask into new folder
        mask_name = mask_names[idx]
        shutil.copy(os.path.join(mask_output_dir, mask_name), os.path.join(output_dir, 'population{}.tiff'.format(mask_suffix)))
        
        # copy segmentations into directory
        seg_name = val + '_feature_0.tif'
        shutil.copy(os.path.join(seg_dir, seg_name), os.path.join(output_dir, 'cell_segmentation.tiff'))
        
        # copy mapping into directory
        map_df.to_csv(os.path.join(output_dir, 'population{}.csv'.format(mask_suffix)), index=False)

Here's what it looks like when run from the cell clustering nb

tiff_dir = os.path.join(base_dir, “compensated_data_1”)
create_mantis_project(mantis_project_path=os.path.join(base_dir, cell_output_dir, ‘mantis’),
                      img_data_path=tiff_dir,
                      mask_output_dir=os.path.join(base_dir, cell_output_dir),
                      mask_suffix=‘_cell_mask’,
                      mapping_path = os.path.join(base_dir, cell_output_dir, cell_cluster_prefix + ‘_cell_meta_cluster_mapping.csv’),
                     seg_dir=segmentation_dir)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants