Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions packages/essimaging/src/ess/imaging/tools/resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ def maximum_resolution_achievable(
events: sc.DataArray,
coarse_x_bin_edges: sc.Variable,
coarse_y_bin_edges: sc.Variable,
time_bin_edges: sc.Variable,
max_tries: int = 10,
max_pixels_x: int = 2048,
max_pixels_y: int = 2048,
Expand All @@ -31,8 +30,6 @@ def maximum_resolution_achievable(
Minimum acceptable resolution in ``x``.
coarse_y_bin_edges:
Minimum acceptable resolution in ``y``.
time_bin_edges:
Desired resolution in ``t``.
max_tries:
The maximum number of iterations before giving up.
max_pixels_x:
Expand All @@ -59,7 +56,17 @@ def maximum_resolution_achievable(

nx = int(2**0.5 * lower_nx) + 1
ny = int(2**0.5 * lower_ny) + 1
events = events.bin({time_bin_edges.dim: time_bin_edges})

events = events.copy(deep=False)

if events.bins is not None:
image_dims = (coarse_x_bin_edges.dim, coarse_y_bin_edges.dim)
for c in image_dims:
if c not in events.bins.coords:
events.bins.coords[c] = sc.bins_like(
events, sc.midpoints(events.coords[c])
)
events = events.bins.concat(image_dims)
Copy link
Copy Markdown
Member

@YooSunYoung YooSunYoung Apr 13, 2026

Choose a reason for hiding this comment

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

Why do we have to concatenate it? Is it faster if we do?

Suggested change
events = events.bins.concat(image_dims)

Copy link
Copy Markdown
Contributor Author

@jokasimr jokasimr Apr 13, 2026

Choose a reason for hiding this comment

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

The problem is that if we don't concatenate the bins then when we bin it again with new bin-edges in x and y we might run out of memory because of scipp/scipp#3872.

Does that answer the question?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes it does...!
So it'll be not necessary if the bug is fixed right...?

Neverthless, I'm okay with it. It shoudn't be blocked by the bug fix...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes if the bug is fixed we should be fine without it.


for _ in range(max_tries):
xbins = sc.linspace(
Expand Down Expand Up @@ -88,7 +95,7 @@ def maximum_resolution_achievable(
upper_nx = nx
upper_ny = ny
nx = min(round((lower_nx * nx) ** 0.5), upper_nx - 1)
ny = min(round((lower_ny * ny) ** 0.5), upper_nx - 1)
ny = min(round((lower_ny * ny) ** 0.5), upper_ny - 1)

if upper_nx - lower_nx < 2 and upper_ny - lower_ny < 2:
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ def test_finds_maximum_resolution():
events,
sc.linspace('x', 0, 1, 2),
sc.linspace('x', 0, 1, 2),
sc.linspace('t', 0, 2, 2),
)
assert len(x_be) == 11
assert len(y_be) == 11
Expand All @@ -51,7 +50,6 @@ def test_finds_maximum_resolution_random(seed):
events,
sc.linspace('x', 0, 1, 2),
sc.linspace('y', 0, 1, 2),
sc.linspace('t', 0, 2, 2),
# Need enough tries to be sure we find the optimum
max_tries=100,
)
Expand All @@ -70,3 +68,42 @@ def test_finds_maximum_resolution_random(seed):
.value
== 0
)


def test_finds_maximum_resolution_binned_input():
np.random.seed(0)
n = np.random.randint(1000, 100_000)
events = sc.DataArray(
sc.ones(dims=['events'], shape=(n,)),
coords={
'x': sc.array(dims=['events'], values=np.random.random(n)),
'y': sc.array(dims=['events'], values=np.random.random(n)),
't': sc.array(dims=['events'], values=np.random.random(n)),
},
)
events = events.bin(x=100, y=100, t=500)
del events.bins.coords['x']
del events.bins.coords['y']
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it because they are usually dropped by reduction workflows?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's to make it look like typical detector event data in our workflows.

Typically we don't have pixel positions on the event data.

x_be, y_be = maximum_resolution_achievable(
events,
sc.linspace('x', 0, 1, 2),
sc.linspace('y', 0, 1, 2),
# Need enough tries to be sure we find the optimum
max_tries=100,
)

events.bins.coords['x'] = sc.bins_like(events, sc.midpoints(events.coords['x']))
events.bins.coords['y'] = sc.bins_like(events, sc.midpoints(events.coords['y']))
events = events.bins.concat(['x', 'y'])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
events = events.bins.concat(['x', 'y'])

Here too. I think putting the event coordinate should be enough.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Same issue here, if we bin again in x and y when there are already x and y dimensions then we might run out of memory.


assert events.bin(x=x_be, y=y_be).bins.size().min().value > 0
assert (
events.bin(
x=sc.linspace('x', 0, 1, len(x_be) + 1),
y=sc.linspace('y', 0, 1, len(y_be) + 1),
)
.bins.size()
.min()
.value
== 0
)
Loading