Skip to content

Support arbitrary patch configurations with dynamic cut handling#39

Draft
Copilot wants to merge 6 commits intomainfrom
copilot/extend-template-logic-for-patches
Draft

Support arbitrary patch configurations with dynamic cut handling#39
Copilot wants to merge 6 commits intomainfrom
copilot/extend-template-logic-for-patches

Conversation

Copy link

Copilot AI commented Dec 15, 2025

The template system hardcoded expectations for exactly 5 cuts with specific anatomical names (calcarine, medial1-3, temporal) and required a medial wall. This prevented creating patches for arbitrary brain regions like isolated occipital or temporal patches.

Changes

Dynamic cut extraction

  • Core functions (ensure_continuous_cuts, refine_cuts_with_geodesic) now iterate over all keys in vertex_dict except reserved mwall key
  • Removed hardcoded cut_names = ["calcarine", "medial1", "medial2", "medial3", "temporal"] in favor of [k for k in vertex_dict.keys() if k != "mwall"]

Flexible template processing

  • merge_small_components(): Added optional max_cuts parameter (default None = keep all cuts)
  • classify_cuts_anatomically(): Falls back to generic names (cut1, cut2, etc.) when anatomical classification not applicable
  • identify_surface_components(): Added max_cuts and classify_anatomically parameters

Topology validation

  • New validate_patch_topology() function checks single connected component, disk topology (one boundary loop), and reasonable patch size
  • Returns validation status, issues list, and topology metrics

API exports

  • Exported key functions in __init__.py for programmatic use: validate_patch_topology, ensure_continuous_cuts, refine_cuts_with_geodesic, etc.

Usage

from autoflatten.utils import save_json
from autoflatten import validate_patch_topology

# Create template with arbitrary cut names
template = {
    "lh_mwall": [100, 101, 102],  # optional, can be empty
    "lh_occipital_boundary": [1, 2, 3, 4, 5],
    "lh_ventral_cut": [10, 11, 12],
}
save_json("occipital_patch.json", template)

# Validate before flattening
is_valid, issues, info = validate_patch_topology(
    vertex_dict, subject="sub-01", hemi="lh"
)

Use custom template: autoflatten subject --template-file occipital_patch.json

See examples/custom_patch_example.py for comprehensive examples.

Original prompt

This section details on the original issue you should resolve

<issue_title>Extend template logic to allow other patches/regions</issue_title>
<issue_description>Currently we expect the template/patch to have the medial wall removed + 5 cuts as in the fully flattened cortical surface. These are three medial cuts, one temporal, and one occipital. However, this is not strictly necessary. We could be more flexible with the templates to allow arbitrary patches to be generated. For example, we could have a template for the occipital pole, a template for the temporal pole, etc. This would also allow users to create a single patch on fsaverage, which can then be applied to any subject. (We would just need to add/expose a subcommand/function to generate a json template from a patch.)

Implementing this shouldn't require too much work: just making sure that we don't hardcode expected cuts/medial walls. Plus some additional checks on the patch topology.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 5 commits December 15, 2025 20:58
- Updated core.py to extract cut names dynamically from vertex_dict
- Made merge_small_components flexible with optional max_cuts parameter
- Made classify_cuts_anatomically fall back to generic naming for non-standard patches
- Updated identify_surface_components to support arbitrary patch configurations
- Updated docstrings to reflect flexibility
- Updated tests to pass with new flexible behavior

Co-authored-by: mvdoc <6150554+mvdoc@users.noreply.github.com>
- Added validate_patch_topology() function to check patch topology
- Exported key functions in __init__.py for programmatic use
- Created comprehensive examples demonstrating flexible patch usage
- Added test for topology validation
- Added examples/README.md with usage guide
- All tests pass (151 passed, 6 skipped)

Co-authored-by: mvdoc <6150554+mvdoc@users.noreply.github.com>
- Added section on custom patches and arbitrary regions
- Documented how to create custom templates
- Added example code for validation
- Referenced examples directory for detailed usage

Co-authored-by: mvdoc <6150554+mvdoc@users.noreply.github.com>
- Format function calls with multiple parameters across lines
- Improve code readability per review suggestions

Co-authored-by: mvdoc <6150554+mvdoc@users.noreply.github.com>
Copilot AI changed the title [WIP] Extend template logic to allow other patches and regions Support arbitrary patch configurations with dynamic cut handling Dec 15, 2025
Copilot AI requested a review from mvdoc December 15, 2025 21:11
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.

Extend template logic to allow other patches/regions

2 participants