# Subsystem Loop

This function block repeats an internal workflow for each element of a list (or other sized collection). Use it when you want to process a series of items one-by-one inside a contained subsystem and gather results as lists.

## 📥 Inputs <a href="#inputs" id="inputs"></a>

This function block does not have any input sockets at the top level.\
To provide data to the repeated workflow, use internal blocks inside the subsystem (for example `Subsystem In`).

## 📤 Outputs <a href="#outputs" id="outputs"></a>

This function block does not expose direct output sockets at the top level.\
Results produced inside the repeated workflow are returned via internal blocks (for example `Subsystem Out`) and are collected as lists by this block.

## 🕹️ Controls <a href="#controls" id="controls"></a>

There are no visible parameter controls on the block itself. Interaction and configuration happen inside the contained subsystem workspace where you place input/output helper blocks:

* `Subsystem In` Use inside the subsystem to receive the current item (or shared inputs) for each loop step.
* `Subsystem Out` Use inside the subsystem to emit the per-step result that will be collected by the loop.

## 🎨 Features <a href="#features" id="features"></a>

* Iterative processing: runs the internal workflow once per element of the first sized collection you provide.
* Multi-list support: if multiple collections with the same length are supplied, they will be iterated in parallel. Use pairing helpers if you need explicit pairing.
* Aggregation: outputs produced by the internal workflow are collected as lists (one list per output) and returned after the loop finishes.
* Safe stop and status propagation: internal errors or invalid states are propagated so the parent scenario can react.

## ⚙️ Running mechanism (plain language) <a href="#how-it-works" id="how-it-works"></a>

* The block looks for the first collection-type input you provide (for example a list of images or numbers).
* For each element in that collection it opens the subsystem workspace and runs the contained workflow once using the current element.
* Any values you send outside the subsystem with `Subsystem Out` are appended to lists. After the loop finishes, you get one collected list per output.
* If you need to iterate multiple lists together, ensure they have the same length or pair them before feeding into the subsystem.

## 📝 How to use <a href="#usage" id="usage"></a>

1. Place the `Subsystem Loop` block in your scenario.
2. Double-click (or open) the subsystem workspace and add the following inside:
   * `Subsystem In` blocks to receive the per-step inputs you want to work on.
   * Your processing blocks (filters, detectors, calculations) to operate on each item.
   * `Subsystem Out` blocks to send results back to the loop aggregator.
3. If you want to iterate multiple lists together, prepare them using a list helper (for example `List Operations`) before feeding into the subsystem.
4. Run the scenario. The loop will run the internal workflow for every element and return aggregated lists of the outputs.

## 💡 Tips and Tricks <a href="#tips-and-tricks" id="tips-and-tricks"></a>

* To iterate multiple related lists in sync, prepare them with the `List Operations` block (use the pairing option if available) before feeding into the subsystem.
* Use `Get Element` inside the subsystem only if you need direct indexed access to lists supplied as shared inputs.
* For large image batches, combine `Batch Processing` and `Debatch` to reduce memory usage and keep per-step processing efficient.
* Use `Data Read Local` / `Data Write Local` (or Global variants) to share persistent data between the main scenario and the subsystem when a simple input/output connection is not convenient.
* If you need to run the same processing on individual images and then visualize results, connect a `Show Image` block after the loop outputs to preview collected images.
* When working with lists that may contain empty or missing entries, add an `Is None` check inside the subsystem to avoid unexpected errors.

## 🛠️ Troubleshooting <a href="#troubleshooting" id="troubleshooting"></a>

* No iterations occur: Verify you supplied at least one collection-type input inside the subsystem (via `Subsystem In`).
* Outputs come back wrapped in single-element lists: Check how many `Subsystem Out` blocks are present and confirm they return the intended values per step.
* Different list lengths: If you expect multiple lists to iterate together, ensure they are the same length or pair them beforehand with `List Operations`.
* High memory usage: Prefer `Batch Processing` and `Debatch` or process smaller chunks inside the subsystem to reduce peak memory.
