Skip to content

Add methods baysor#29

Merged
rcannood merged 22 commits into
mainfrom
add-methods-baysor
May 19, 2026
Merged

Add methods baysor#29
rcannood merged 22 commits into
mainfrom
add-methods-baysor

Conversation

@heylf
Copy link
Copy Markdown
Contributor

@heylf heylf commented May 18, 2026

Describe your changes

Adding the cpp version of bayor to the pipeline.

Need the initial segmentation now to finalize it.

Checklist before requesting a review

  • I have performed a self-review of my code

  • Check the correct box. Does this PR contain:

    • Breaking changes
    • New functionality
    • Major changes
    • Minor changes
    • Bug fixes
  • Proposed changes are described in the CHANGELOG.md

  • CI Tests succeed and look good!

@heylf heylf closed this May 18, 2026
@heylf heylf reopened this May 18, 2026
Comment thread src/methods/baysor/config.vsh.yaml Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR integrates a new baysor method component into the spatial segmentation benchmark workflow, intending to run the Baysor (C++/cpp-branch) pipeline as part of run_benchmark.

Changes:

  • Added baysor to the list of benchmarked methods and included it as a workflow dependency.
  • Introduced a new Viash component at src/methods/baysor/ (config + Python entry script) that runs Baysor and writes a SpatialData Zarr output.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.

File Description
src/workflows/run_benchmark/main.nf Adds baysor to the default methods list executed by the benchmark workflow.
src/workflows/run_benchmark/config.vsh.yaml Adds the methods/baysor dependency so the workflow can run the new component.
src/methods/baysor/script.py New method implementation: reads SpatialData, attempts to use a prior segmentation, runs Baysor, and writes a prediction Zarr.
src/methods/baysor/config.vsh.yaml New Viash component definition, including arguments and Docker build steps to install Baysor C++.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/workflows/run_benchmark/main.nf
Comment on lines +24 to +25
description: The key for the prior segmentation stored within the spatial data
default: nucleus_labels
Comment on lines +50 to +65

print(sdata)

# Check if coordinate system is available in input data
transcripts_coord_systems = sd.transformations.get_transformation(sdata[par["transcripts_key"]], get_all=True).keys()
assert par['coordinate_system'] in transcripts_coord_systems, f"Coordinate system '{par['coordinate_system']}' not found in input data."
segmentation_coord_systems = sd.transformations.get_transformation(sdata[par['prior_segmentation_key']], get_all=True).keys()
assert par['coordinate_system'] in segmentation_coord_systems, f"Coordinate system '{par['coordinate_system']}' not found in input data."

# Transform transcript coordinates to the coordinate system
print('Transforming transcripts coordinates', flush=True)
transcripts = sd.transform(sdata[par['transcripts_key']], to_coordinate_system=par['coordinate_system'])

# In case of a translation transformation of the segmentation (e.g. crop of the data), we need to adjust the transcript coordinates
trans = sd.transformations.get_transformation(sdata[par['prior_segmentation_key']], get_all=True)[par['coordinate_system']].inverse()
transcripts = sd.transform(transcripts, trans, par['coordinate_system'])
Comment on lines +81 to +86
toml_str = f"""[data]
x = "x"
y = "y"
z = "z"
gene = "feature_name"
force_2d = {par['force_2d']}
Comment on lines +99 to +107
# Run Baysor
n_threads = meta['cpus'] or os.cpu_count()
n_threads = max(n_threads-2, 1)
os.environ['JULIA_NUM_THREADS'] = str(n_threads)
print('Running Baysor', flush=True)
baysor_cmd = f"baysor run --output-style legacy -c {CONFIG_TOML} -o {BAYSOR_OUTPUT_DIR} {TRANSCRIPTS_CSV} {SEGMENTATION_TIF}"
print("\t" + baysor_cmd, flush=True)
os.system(baysor_cmd)

Comment on lines +152 to +161
),
index=sdata[par['transcripts_key']].index
)
sdata[par['transcripts_key']]["cell_id"] = cell_id_dask_series


# Create objects for cells table
print('Creating objects for cells table', flush=True)
#create new .obs for cells based on the segmentation output (corresponding with the transcripts 'cell_id')
unique_cells = np.unique(cell_id_dask_series)
Comment on lines +67 to +86
# Write transcripts to csv
print('Writing transcripts to csv', flush=True)
transcripts[['x', 'y', 'z', 'feature_name']].compute().to_csv(TRANSCRIPTS_CSV)

# Write segmentation to tif
print('Writing segmentation to tif', flush=True)
if isinstance(sdata[par['prior_segmentation_key']], xr.DataTree):
label_image = sdata[par['prior_segmentation_key']]["scale0"].image.to_numpy()
else:
label_image = sdata[par['prior_segmentation_key']].to_numpy()
imwrite(SEGMENTATION_TIF, label_image)

# Write config to toml
print('Writing config to toml', flush=True)
toml_str = f"""[data]
x = "x"
y = "y"
z = "z"
gene = "feature_name"
force_2d = {par['force_2d']}
Comment thread src/methods/baysor/script.py Outdated
rcannood and others added 4 commits May 19, 2026 11:53
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Robrecht Cannoodt <rcannood@gmail.com>
…_spatial_segmentation into add-methods-baysor
@rcannood rcannood merged commit 006e6b0 into main May 19, 2026
1 check passed
@rcannood rcannood deleted the add-methods-baysor branch May 19, 2026 09:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants