rajewsky-lab/openst

Error in openst pairwise_aligner

falsetry1514 opened this issue · 4 comments

Describe the bug

When I run this code in shell, it gave me a warning:
multiprocessing/resource_tracker.py:224: UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown
warnings.warn('resource_tracker: There appear to be %d '

This is my config:
PhysMem: 22G used (7981M wired, 0B compressor), 234G unused.
CPU: 2.7 GHz 24 cores Intel Xeon W
sonoma 14.5
GPU: AMD Radeon Pro 580X 8 GB

Steps to reproduce the bug

This is the code:
openst from_spacemake --project-id openst_demo --sample-id e13_mouse_head --run-mode openst pairwise_aligner --metadata aligner.json --only-coarse

Sample code to reproduce the bug

I ran the example data named E13_mouse_head, and ran it on mac. I figure out the problem how to run spacemake on mac.

Expected results

I hope it will run successfully.

This is the whole script:
shell:
$ openst from_spacemake --project-id openst_demo --sample-id e13_mouse_head --run-mode openst pairwise_aligner --metadata aligner.json --only-coarse
INFO 2024-07-17 20:21:57,577 - openst from_spacemake - running from spacemake directory
INFO 2024-07-17 20:21:57,577 - {'subcommand': 'from_spacemake', 'project_id': 'openst_demo', 'sample_id': 'e13_mouse_head', 'run_mode': 'openst', 'func': <function cmd_run_from_spacemake at 0x10840f370>}
INFO 2024-07-17 20:21:57,981 - openst pairwise_aligner - running with the following parameters:
INFO 2024-07-17 20:21:57,981 - {'project_id': 'openst_demo', 'sample_id': 'e13_mouse_head', 'run_mode': 'openst', 'subcommand': 'pairwise_aligner', 'h5_in': 'projects/openst_demo/processed_data/e13_mouse_head/multimodal/stitched_spots.h5ad', 'image_in': 'uns/spatial/staining_image', 'metadata': 'aligner.json', 'only_coarse': True, 'rescale_factor_coarse': 20, 'threshold_counts_coarse': 1, 'pseudoimage_size_coarse': 500, 'ransac_coarse_min_samples': 3, 'ransac_coarse_residual_threshold': 2, 'ransac_coarse_max_trials': 2, 'rescale_factor_fine': 10, 'gaussian_sigma_fine': 2, 'threshold_counts_fine': 0, 'pseudoimage_size_fine': 2000, 'ransac_fine_min_samples': 3, 'ransac_fine_residual_threshold': 2, 'ransac_fine_max_trials': 1, 'min_matches': 50, 'mask_tissue': False, 'mask_gaussian_sigma': 5, 'keep_black_background': False, 'feature_matcher': 'LoFTR', 'num_workers': 1, 'device': 'cpu', 'func': <function cmd_run_pairwise_aligner at 0x10840ee60>}
INFO 2024-07-17 20:22:00,142 - Coarse registration, 21015579 coordinates
INFO 2024-07-17 20:22:00,142 - Rescaling input image for coarse registration
INFO 2024-07-17 20:22:02,685 - Flip (1, 1), rotation 0
Segmentation fault: 11
$ miniforge3/envs/openst/lib/python3.10/multiprocessing/resource_tracker.py:224: UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown
warnings.warn('resource_tracker: There appear to be %d '

Hi @danilexn
I find the function that caused this problem.

which is in '.../miniforge3/envs/openst/lib/python3.10/site-packages/openst/alignment/feature_matching.py'

def _find_matches_loftr(im_0: np.ndarray, im_1: np.ndarray, pretrained: str = "outdoor", device: str = "cpu") -> tuple:
...
matcher = KF.LoFTR(pretrained=pretrained).to(device)
...

But I don't know how to solve it.

Hi, I report a bug in pairwise_aligner.

typed this line in bash shell
openst from_spacemake --project-id openst_demo --sample-id e13_mouse_head pairwise_aligner

argparse will pass the parameter in args, when it came to STAGE 2: fine registration per tile.

IPdb [35]: np.unique(tile_id.codes)
Out  [35]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11], dtype=int8)
IPdb [51]: np.unique(tile_id[(total_counts > args.threshold_counts_coarse)].codes)
Out  [51]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11], dtype=int8)
IPdb [52]: np.unique(tile_id[(total_counts > args.threshold_counts_coarse)].codes[_i_sts_coords_coarse_within_image_bounds])
Out  [52]: array([ 1,  2,  3,  5,  6,  7,  9, 10, 11], dtype=int8)

this will cause a zero array '_t_valid_coords'

IPdb [53]: np.isin(tile_id[(total_counts > args.threshold_counts_coarse)].codes[_i_sts_coords_coarse_within_image_bounds], tile_code,).any()
Out  [53]: False

caused that

logging.info(f"Registering tile 0 with 0 coordinates")

this will cause an error that a number divided by zero in the function create_paired_pseudoimage:

# sts_pseudoimage.max() is equal to zero
sts_pseudoimage = ((sts_pseudoimage / sts_pseudoimage.max()) * 255).astype(np.uint8)

Hi @falsetry1514!

Thanks for the very detailed issue. I will take a look - might take a while because we don't have access to Intel-based Macs, and the first issue looks like something platform-specific, but we'll try our best. Regarding the second issue, will fix it asap 😄

Hi @falsetry1514!

Thanks for the very detailed issue. I will take a look - might take a while because we don't have access to Intel-based Macs, and the first issue looks like something platform-specific, but we'll try our best. Regarding the second issue, will fix it asap 😄

Hi, @danilexn
I have work it on macos by using the example of e13_mouse_head!
This is the solution that running the whole program of openst and spacemake on macos:

  • First: modify some py files
    1.First py file
    path = </PATH/to/spacemake>/snakemake/scripts/n_intersect_sequences.py

modify this line:

improt multiprocessing as mp

into:

import multiprocess as mp

The reason is that the first module will report an error, but the second one will not.

2.Second py file
path = </PATH/to/spacemake>/snakemake/scripts/automated_analysis.py
enclosed the script by:
if name == 'main':

  • Second: modify two smk file
    main.smk & mapping.smk
    modify:
'ln -sr' or 'ln -rs'

into

ln -s

ln command on mac don't have the parameter of '-r'

main.smk
modify:
'zcat {input} | cut -f2 | head -{wildcards.n_beads} > {output}'
into
'gunzip -c {input} | cut -f2 | head -{wildcards.n_beads} > {output}'

zcat command on mac will add '.z' suffix on gz file, the 'gunzip -c' command do the same function on it.