Skip to content

save_segmentation_results failing for some FOVs #174

@sam-mick

Description

@sam-mick

Hi there,

I'm running Baysor on CosMx data, using Mesmer segmentation as a prior.

Some FOVs are failing to get saved after segmentation with Baysor, but the error only seems to occur in the actual saving step.

#150 (comment) had the same error, but the issue isn't just with transcripts at -1 - it can occur at any value.

Here's an example, if the transcript file (tx in CosMx) has very few transcripts at a Z value of -1, then it will fail.


# A tibble: 9 × 2
      z count
  <int> <int>
1    -1     1
2     0   440
3     1  1149
4     2  1188
5     3   977
6     4   867
7     5   742
8     6   602
9     7   520

Likewise for transcripts at a Z value of 8:

# A tibble: 9 × 2
      z count
  <int> <int>
1     0   920
2     1  2274
3     2  2579
4     3  2270
5     4  2061
6     5  2104
7     6  1912
8     7  1906
9     8     2

And here's the output of running Baysor:

baysor/bin/baysor run temp_tx_file.csv FOV_1_mesmer_mask.tif -c baysor/cosmx.toml --count-matrix-format 'tsv'

[12:14:03] Info: Run R97afc05f1
[12:14:03] Info: (2025-12-04) Run Baysor v0.7.1
[12:14:03] Info: Loading data...
[12:14:06] Info: Excluding genes: Negative1, Negative10, Negative3, Negative4, Negative5, Negative6, Negative7, Negative8, Negative9, SystemControl10, SystemControl100, SystemControl101, SystemControl104, SystemControl111, SystemControl113, SystemControl118, SystemControl121, SystemControl126, SystemControl127, SystemControl128, SystemControl129, SystemControl130, SystemControl131, SystemControl132, SystemControl133, SystemControl136, SystemControl138, SystemControl139, SystemControl14, SystemControl141, SystemControl142, SystemControl144, SystemControl145, SystemControl146, SystemControl148, SystemControl149, SystemControl15, SystemControl150, SystemControl151, SystemControl152, SystemControl155, SystemControl159, SystemControl16, SystemControl160, SystemControl161, SystemControl164, SystemControl168, SystemControl169, SystemControl17, SystemControl171, SystemControl172, SystemControl174, SystemControl177, SystemControl18, SystemControl180, SystemControl182, SystemControl183, SystemControl184, SystemControl185, SystemControl187, SystemControl188, SystemControl190, SystemControl193, SystemControl197, SystemControl2, SystemControl21, SystemControl22, SystemControl23, SystemControl24, SystemControl26, SystemControl27, SystemControl3, SystemControl30, SystemControl34, SystemControl4, SystemControl43, SystemControl47, SystemControl48, SystemControl5, SystemControl50, SystemControl51, SystemControl52, SystemControl54, SystemControl55, SystemControl57, SystemControl58, SystemControl59, SystemControl61, SystemControl62, SystemControl65, SystemControl66, SystemControl67, SystemControl7, SystemControl71, SystemControl72, SystemControl73, SystemControl74, SystemControl75, SystemControl78, SystemControl8, SystemControl80, SystemControl81, SystemControl85, SystemControl86, SystemControl89, SystemControl9, SystemControl90, SystemControl94, SystemControl95, SystemControl96, SystemControl97
[12:14:06] Info: Loaded 48992 transcripts, 991 genes.
[12:14:06] Info: Loading segmentation mask...
[12:14:08] Warning: Minimum transcript coordinates are < 1: (1, 0). Filling it with 0.
└ Baysor.DataLoading /home/viktor_petukhov/.julia/dev/Baysor/src/data_loading/prior_segmentation.jl:30
[12:14:08] Info: Done
[12:14:09] Info: Estimating noise level
[12:14:10] Info: Done
[12:14:14] Info: Clustering molecules...
Progress: 100%|████████████████████████████████████████████████████████████████████████| Time: 0:00:01
  Iteration:                  262
  Max. difference:            0.00729
  Fraction of probs changed:  0.651
[12:14:16] Info: Algorithm stopped after 262 iterations. Max. probability difference: 0.00729. Converged: true.
[12:14:16] Info: Done
[12:14:16] Info: Initializing algorithm. Scale: 36.116947733455746, scale std: 8.876190755101101, initial #components: 32660, #molecules: 48992.
[12:14:17] Info: Using the following additional information about molecules: [:confidence, :cluster, :prior_segmentation]
[12:14:17] Info: Using 3D coordinates
Progress: 100%|████████████████████████████████████████████████████████████████████████| Time: 0:00:14
  Iteration:        500
  Noise level, %:   7.22
  Num. components:  1593
[12:14:32] Info: Processing complete.
[12:14:32] Info: Estimating local colors
[12:14:54] Info: Estimating boundary polygons
Progress: 100%|████████████████████████████████████████████████████████████████████████| Time: 0:00:00
[12:14:55] Info: Saving results to test/segmentation.csv
ERROR: MethodError: no method matching save_segmentation_results(::DataFrames.DataFrame, ::DataFrames.DataFrame, ::SparseArrays.SparseMatrixCSC{Int64, Int64}, ::OrderedCollections.OrderedDict{String, Dict{K, Matrix{Float64}} where K}, ::Baysor.Utils.OutputPaths; matrix_format::Symbol, gene_names::Vector{String}, polygon_format::String)

Closest candidates are:
  save_segmentation_results(::DataFrames.DataFrame, ::DataFrames.DataFrame, ::SparseArrays.SparseMatrixCSC{<:Real, Int64}, ::Union{Nothing, AbstractDict{TI, Matrix{TV}} where {TV<:Real, TI<:Union{Integer, String}}, AbstractDict{String, <:AbstractDict{TI, Matrix{TV}}} where {TV<:Real, TI<:Union{Integer, String}}}, ::Baysor.Utils.OutputPaths; polygon_format, gene_names, matrix_format)
   @ Baysor /home/viktor_petukhov/.julia/dev/Baysor/src/data_loading/cli_wrappers.jl:224

Stacktrace:
 [1] run(coordinates::String, prior_segmentation::String; config::Baysor.Utils.RunOptions, x_column::Symbol, y_column::Symbol, z_column::Symbol, gene_column::Symbol, min_molecules_per_cell::Int64, scale::Float64, scale_std::String, n_clusters::Int64, prior_segmentation_confidence::Float64, output::String, plot::Bool, polygon_format::String, count_matrix_format::String)
   @ Baysor.CommandLine /home/viktor_petukhov/.julia/dev/Baysor/src/cli/main.jl:136
 [2] command_main(ARGS::Vector{String})
   @ Baysor /home/viktor_petukhov/.julia/packages/Comonicon/F3QqZ/src/codegen/julia.jl:343
 [3] command_main
   @ /home/viktor_petukhov/.julia/packages/Comonicon/F3QqZ/src/codegen/julia.jl:90 [inlined]
 [4] julia_main()
   @ Baysor /home/viktor_petukhov/.julia/packages/Comonicon/F3QqZ/src/frontend/cast.jl:481
 [5] top-level scope
   @ none:1

The error seems to be from save_segmentation_results, where few transcripts in one Z level results in ::SparseArrays.SparseMatrixCSC{Int64, Int64}, where the expected input is ::SparseArrays.SparseMatrixCSC{<:Real, Int64}

Would appreciate any help to get this working with all data!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions