Composition & Decomposition

A compositional index folds many cells into one string. Decomposing unfolds it; composing folds it back. The two are inverses, and the order is stable — which is what everything else in the package leans on.

from itacart import index as ix
region = "NE(0001/0002(1(A1,A2,A3)))"
ix.decompose(region)
['NE(0001/0002(1(A1)))', 'NE(0001/0002(1(A2)))', 'NE(0001/0002(1(A3)))']

Three atomic indices, each a complete address in its own right. Folding them back returns what we started with:

ix.compose(ix.decompose(region)) == region
True

The order is a contract, not an accident

decompose emits cells in a fixed order, and every function that answers one value per cell answers in that same order.

The ordering decompose emits

docs/_static/f2/f2_06_decompose_order.png

That alignment is what makes the notation usable in a pipeline. Ask for the parents of a region and you get one parent per cell, positionally matched:

import itacart
list(zip(ix.decompose(region), itacart.get_parent(region)))
[('NE(0001/0002(1(A1)))', 'NE(0001/0002(1))'),
 ('NE(0001/0002(1(A2)))', 'NE(0001/0002(1))'),
 ('NE(0001/0002(1(A3)))', 'NE(0001/0002(1))')]

Without the guarantee you would have to carry a separate list of which answer belongs to which cell — which is exactly the unstructured-list problem the compositional index exists to avoid.

Counting without unfolding

A region can address a very large number of cells, and you often want the count rather than the cells:

ix.count_cells(region), ix.count_cells("SE(1400/0374)")
(3, 1)

count_cells reads the structure rather than materialising the members. When you do want them one at a time, iter_cells streams in decompose order instead of building the whole list:

next(iter(ix.iter_cells(region)))
'NE(0001/0002(1(A1)))'

Taking one address apart and putting it back

split_components and join_components are the atomic-level pair, working on a single cell’s path rather than on a set:

parts = ix.split_components("SE(1400/0374(3(C2(3))))")
parts, ix.join_components(parts)
(['SE', '1400/0374', '3', 'C2', '3'], 'SE(1400/0374(3(C2(3))))')

This is the level at which you inspect a resolution, swap a refinement code, or build an address from parts you already hold. The set-level pair and the component-level pair do not mix: decompose is about how many cells a string names, split_components is about how deep one cell’s address goes.

The parse tree underneath

Both pairs are views of one structure, which parse exposes directly:

ix.parse("NE(0001/0002(1(A1,A2)))")
{'code': None,
 'resolution': -1,
 'children': [{'code': 'NE',
   'resolution': 0,
   'children': [{'code': '0001/0002',
     'resolution': 1,
     'children': [{'code': '1',
       'resolution': 2,
       'children': [{'code': 'A1', 'resolution': 3, 'children': []},
        {'code': 'A2', 'resolution': 3, 'children': []}]}]}]}],
 'quadrant': 'NE',
 'base_cell': '0001/0002'}
The parse tree of a compositional index

docs/_static/f2/f2_05_figure7_parse_tree.png

Reaching for parse is rarely necessary — the functions above cover the ordinary cases — but it is there when you need to walk the structure rather than consume it.