Skip to content

Apply tiled functionality for tiled processing of graphs#183

Merged
JoOkuma merged 17 commits intoroyerlab:mainfrom
JoOkuma:jookuma/apply-tiled
Nov 4, 2025
Merged

Apply tiled functionality for tiled processing of graphs#183
JoOkuma merged 17 commits intoroyerlab:mainfrom
JoOkuma:jookuma/apply-tiled

Conversation

@JoOkuma
Copy link
Copy Markdown
Member

@JoOkuma JoOkuma commented Oct 24, 2025

@yfukai, could you review this PR?

I have a function that cannot handle the whole graph, so I'm adding functionality to chunk the data.

Because we might have tile artifacts, it needs an overlay. The only way I thought of being able to do this properly is by providing two graphs to the MapFunc. I also considered providing the tile coordinates in case a user might want to slice the image at that position.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Oct 24, 2025

Codecov Report

❌ Patch coverage is 91.66667% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.22%. Comparing base (c3e1463) to head (8b5c14b).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/tracksdata/functional/_apply.py 91.42% 4 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #183      +/-   ##
==========================================
+ Coverage   88.02%   88.22%   +0.20%     
==========================================
  Files          50       51       +1     
  Lines        3474     3593     +119     
  Branches      601      617      +16     
==========================================
+ Hits         3058     3170     +112     
- Misses        250      254       +4     
- Partials      166      169       +3     

☔ 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.

@JoOkuma
Copy link
Copy Markdown
Member Author

JoOkuma commented Oct 24, 2025

I found it helpful to have a Tile object. I updated to include this

@yfukai
Copy link
Copy Markdown
Contributor

yfukai commented Oct 25, 2025

@JoOkuma Sure, I'll review this in a couple of days!

Copy link
Copy Markdown
Contributor

@yfukai yfukai left a comment

Choose a reason for hiding this comment

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

Almost all looks nice to me! I'm just afraid that the logic around eps might not work as expected if start and end can accept floats (since the tile size may be arbitrary small depending on the length unit). If those are ints as noted as type annotations, maybe we can simply use -1 or +1 to switch exclusive / inclusive slices.

Comment thread src/tracksdata/functional/_apply.py Outdated
Comment thread src/tracksdata/functional/_apply.py Outdated
eps = 1e-8 # adding eps because np.arange is right exclusive
tiles_corner = list(
itertools.product(
*[np.arange(s, e + eps, t).tolist() for s, e, t in zip(start, end, tiling_scheme.tile, strict=True)]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since start and end are ints, maybe we can do something like:
It may also be useful to note whether indices are inclusive or exclusive.

Suggested change
*[np.arange(s, e + eps, t).tolist() for s, e, t in zip(start, end, tiling_scheme.tile, strict=True)]
*[np.arange(s, e+1, t).tolist() for s, e, t in zip(start, end, tiling_scheme.tile, strict=True)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Or if we accept the float as start and ends, epsilon should be relative to its values? (Since this may not work as expected if e-s<1e-8)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@yfukai, np.nextafter ornp.spacing created other errors, I went for

                np.arange(s, e, t).tolist() if s != e else [s]

What do you think? The eps was a hack for when s was equal to e and np.arange would fail.

Comment thread src/tracksdata/functional/_apply.py Outdated
Comment thread src/tracksdata/functional/_apply.py Outdated
for corner in tiles_corner:
# corner considers the overlap, so right needs to be shifted by 2 * o
# -1e-8 is because tracksdata filter slicing is inclusive
slicing_without_overlap = tuple(slice(c, c + t - 1e-6) for c, t in zip(corner, tiling_scheme.tile, strict=True))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Similar comment to this applies?

@yfukai
Copy link
Copy Markdown
Contributor

yfukai commented Oct 29, 2025

I'm sorry to be late, this is actually my first requested review

@JoOkuma
Copy link
Copy Markdown
Member Author

JoOkuma commented Oct 30, 2025

Hi, @yfukai, thank you for the review, and don't need to apologize.
I understand we have other things going on besides this.
I'll also finish reviewing your PR soon.

I changed tile|overlap to {tile|overlap}_shape.

The tiling can be floats, so I changed the typing to reflect that. I also added a test to check if the scale is affecting the slicing, and I had to change the eps so it varies with the scale of the tile due to numerical errors.

The two eps are different:

  • the one in the np.arange is because in the case when the min and max value of an attr were equal, it was returning a zero-length array.
  • The slicing eps interacts with spatial-graph, which suffers from numerical error, so now it's scaled, and it works for integers and floats as covered by the new test.

Let me know if something is not clear

@JoOkuma
Copy link
Copy Markdown
Member Author

JoOkuma commented Oct 30, 2025

It seems there's an unrelated issue that broke the CI, I'll open another PR to fix it.

@yfukai
Copy link
Copy Markdown
Contributor

yfukai commented Oct 31, 2025

Hi @JoOkuma, thanks! I found out np.nextafter to swap the inclusive and exclusive ranges. Would it be safer to replace eps with this?

@JoOkuma
Copy link
Copy Markdown
Member Author

JoOkuma commented Nov 3, 2025

Hi @JoOkuma, thanks! I found out np.nextafter to swap the inclusive and exclusive ranges. Would it be safer to replace eps with this?

Hi @yfukai, thanks for the input. I didn't know this function. Using nextafter didn't work that well with np.arange but I was able to get around with an if condition, see comment above.

For the other use case, either that or np.spacing leads to issues with the RTree from spatial-graph. It behaves a bit differently than we expect, so even when I tried with np.float32 precision for the tolerance, it doesn't pass the tests.

I'm considering reverting this last commit 6988d91 and leaving it as it is. What do you think?

@yfukai
Copy link
Copy Markdown
Contributor

yfukai commented Nov 4, 2025

Hi @JoOkuma, thank you for checking how np.nextafter works! I added the dtype=np.float32 and now the tests are working. Can you check if JoOkuma#4 makes sense?

@yfukai
Copy link
Copy Markdown
Contributor

yfukai commented Nov 4, 2025

If that looks fine okey, this PR itself looks good to go for me!

This reverts commit 6988d91.
@JoOkuma JoOkuma merged commit cb4770a into royerlab:main Nov 4, 2025
7 of 8 checks passed
@yfukai
Copy link
Copy Markdown
Contributor

yfukai commented Nov 4, 2025

I still believe there's a edge case that only JoOkuma#4 can handle... I'll change the PR pointing to main.

@JoOkuma
Copy link
Copy Markdown
Member Author

JoOkuma commented Nov 4, 2025

Sounds good @yfukai, let me know when I can review #189

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