Skip to content

fix: Aggregation of fluorphores in ground truth with multiple samples#131

Merged
tlambert03 merged 1 commit intotlambert03:mainfrom
melisande-c:fix/gt_aggr_bug
Dec 16, 2025
Merged

fix: Aggregation of fluorphores in ground truth with multiple samples#131
tlambert03 merged 1 commit intotlambert03:mainfrom
melisande-c:fix/gt_aggr_bug

Conversation

@melisande-c
Copy link
Copy Markdown
Contributor

@melisande-c melisande-c commented Dec 16, 2025

I found a bug where if you have multiple FlurophoreDistribution instances in the Sample, the number of fluorophores will be multiplied by the number of samples. This was happening because the same space was being given to each distribution to render into, which would result in n copies of the space for n samples, which would then be summed together. I fixed this by creating a new space for each distribution during the ground truth generation.

Below shows a demo of the behaviour before the fix and after:

import numpy as np
import matplotlib.pyplot as plt

import microsim.schema as ms
from microsim._data_array import xrDataArray
from microsim.schema.backend import NumpyAPI
Renderable Ball class used in demo
class Ball:

    def __init__(self, centre: tuple[float, float, float], radius: float):
        """
        Renderable class that generates a ball for the given `centre` and `radius`.

        Parameters
        ----------
        centre : tuple[float, float, float]
            The centre of the ball in truth pixel units.
        radius : float
            The radius of the ball in truth pixel units.
        """
        self.centre = np.array(centre)
        self.radius = radius

    def render(self, space: xrDataArray, xp: NumpyAPI | None = None) -> xrDataArray:
        xp = xp or NumpyAPI()
        shape = np.array(space.shape)
        centre = self.centre

        ii, jj, kk = np.mgrid[: shape[0], : shape[1], : shape[2]]
        space += xp.asarray(
            (
                (ii - centre[0]) ** 2 + (jj - centre[1]) ** 2 + (kk - centre[2]) ** 2
                <= self.radius**2
            ).astype(float)
        )
        return space

Before

>>> sim = ms.Simulation(
        truth_space=ms.space.ShapeScaleSpace(shape=(24, 64, 128), scale=(0.04, 0.02, 0.02)),
        output_space=ms.space.DownscaledSpace(downscale=4),
        sample=[
            ms.FluorophoreDistribution(distribution=Ball(centre=(12, 32, 32), radius=32)),
            ms.FluorophoreDistribution(distribution=Ball(centre=(12, 32, 128-32+1), radius=32))
        ]
    )
>>> img = sim.digital_image()
... img.data.min(), img.data.max()
(28.87027282714844, 462.57768554687505)

>>> gt = sim.ground_truth()
... gt.shape
(2, 24, 64, 128)

>>> fig, axes=plt.subplots(2, 1)
... axes[0].imshow(gt.data[0, 12])
... axes[1].imshow(gt.data[1, 12]
... fig.suptitle("Behaviour before")
aacb3436-aa75-4007-bd4b-c764d74468a0

After

(simulation parameters kept the same)

>>> img.data.min(), img.data.max()
(14.435137939453126, 231.28884277343752)
e7714503-fb2d-4c3b-9d96-de120f202ae4

As you can see the min and max values before the fix are roughly double the image values after the fix because I had two distributions being simulated.

@codecov
Copy link
Copy Markdown

codecov bot commented Dec 16, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.57%. Comparing base (a1d205c) to head (37ca071).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #131      +/-   ##
==========================================
- Coverage   83.93%   83.57%   -0.37%     
==========================================
  Files          47       47              
  Lines        3094     3026      -68     
==========================================
- Hits         2597     2529      -68     
  Misses        497      497              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tlambert03
Copy link
Copy Markdown
Owner

after is better! 😂 thank you as always @melisande-c for the clear issue and effective fix 👍

@tlambert03 tlambert03 merged commit 165dd88 into tlambert03:main Dec 16, 2025
19 checks passed
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.

2 participants