Links¶
Terms¶
- Link
A record of
link_width(L) endpoints:L = 2is an edge,L = 3a triangle face,L = 1a single-vertex reference. Endpoints may sit in different chunks and, whendelta != 0, at different levels.- Source chunk
The chunk of the endpoint a record is filed under. It is the array cell that holds the record.
- Relative offset (
o_k) The chunk-grid displacement from the source chunk to endpoint
k, a signedsid_ndim-tuple.o_0 = 0and is not stored, so a record carriesL - 1offsets.- Intra-chunk link
A link whose offsets are all zero. It lives in the all-zero
<offsets>array of the same family.- Level delta (
<delta>) A signed path segment: how many levels a record spans.
0means all endpoints are at the owning level;+Nmeans endpoints1..L-1areNlevels coarser;-NmeansNlevels finer. Written"0","+1","-1","+2", ….- Link attribute
Per-record data at
link_attributes/<name>/<delta>/<offsets>/, mirroring the link array cell for cell.
Introduction¶
Each level stores its connectivity in one family per level delta:
links/<delta>/ GROUP family-wide policy
links/<delta>/<offsets>/ ARRAY rank-D vlen; cell = source chunk
link_attributes/<name>/<delta>/ GROUP
link_attributes/<name>/<delta>/<offsets>/ ARRAY mirrors the link array
The <offsets> segment says where the other endpoints sit. Each array
is a plain vlen array over the level’s chunk grid, one cell per source
chunk. Each stored vi_k is a vertex index local to chunk src + o_k.
An intra-chunk link and a cross-chunk link differ only in their
<offsets> directory.
Worked example:
examples/07_multiscale_links.ipynb.
Technical reference¶
Offsets grammar¶
The segment holds link_width - 1 offsets, joined by _. Each offset
is sid_ndim signed components joined by ., written like <delta>
(0, +1, -1).
Segment |
|
Meaning |
|---|---|---|
|
2 |
intra-chunk edge |
|
2 |
edge to the neighbour along |
|
2 |
edge to the neighbour along |
|
3 |
triangle spanning the source, |
|
3 |
intra-chunk triangle |
|
1 |
no other endpoint; an empty segment would make |
Build and parse segments with the helpers in
zarr_vectors/core/paths.py:
format_delta / parse_delta, format_offsets / parse_offsets,
intra_offsets, is_intra, links_group_path, links_path,
link_attributes_group_path, link_attributes_path.
parse_offsets rejects a segment unless:
it has exactly
link_width - 1offsets, or isself;every offset has exactly
sid_ndimcomponents;selfappears if and only iflink_width == 1.
Placement¶
_cell_placements in
zarr_vectors/spatial/boundary.py
gives the permutations sigma a record is filed under
(partition_records_by_offset applies it to records,
partition_link_arrays to arrays). For each sigma:
Item |
Value |
|---|---|
Cell |
chunk of endpoint |
Offsets |
|
Stored vertex indices |
|
|
|
To decode, endpoint k is in chunk anchor(src) + o_k with the k-th
stored vi.
perm_idx¶
A record is L ints, or 1 + L ([perm_idx, vi_0 … vi_{L-1}]) when
the array has a permutation column. perm_idx is a Lehmer code in
[0, L!) for sigma. Readers use it to restore input endpoint order
(face winding, edge direction).
links_has_perm decides it, and
each array records the result as has_perm:
def links_has_perm(offsets, *, delta, directed, store):
if is_intra(offsets): return False # identity placement
if delta != 0: return False # source is always endpoint 0
if store == "duplicate": return True # each copy leads with a different endpoint
return not directed # undirected sorts; directed does not
Intra-chunk records keep input order, since polyline traversal order is data.
Cell encoding: two branches, one condition¶
write_chunk_links picks the
encoding on delta == 0 and is_intra(offsets):
Condition |
Encoding |
Sidecar |
|---|---|---|
|
flat rows ( |
|
otherwise |
self-describing ragged blob ( |
none |
link_fragments/<chunk> holds the
flat branch’s per-group row ranges. Link groups need not match the
chunk’s vertex fragments. The sidecar is keyed by chunk alone, so only
this branch writes it.
directed and store¶
Both are family-wide, set on the <delta> group.
write_links raises ArrayError
if a write would change either while other offsets arrays exist under
the group.
Field |
Value |
Meaning |
|---|---|---|
|
|
Endpoints are sorted by |
|
Input order is data (for example fine→coarse pyramid links). No sort: |
|
|
|
Each record is in one cell. Finding all links touching chunk |
|
One copy per distinct incident chunk, each led by that chunk. All links touching |
|
|
On disk |
|---|---|---|
|
|
one cell, sorted, |
|
|
one copy per incident chunk, |
|
|
one cell, input order, no |
|
|
one copy per incident chunk, |
Endpoint order of skeleton links¶
Writer |
Family policy |
Intra-chunk rows |
Cross-chunk records |
|---|---|---|---|
|
|
|
sorted with |
|
|
|
|
Cross-level placement: the anchor¶
For delta != 0 the source is always input endpoint 0, at the owning
level. Cross-level records are never sorted.
Levels may have different chunk grids: LevelMetadata.chunk_shape is an
optional per-level override, which
coarsen_level sets to
source × chunk_scale_factor (default factor 1). Chunk coordinates from
two such grids cannot be subtracted directly.
Example: root chunk_shape = 100, level-1 chunk_shape = 200. Both
rows have the target in the level-1 chunk after the one holding the
source:
Source (level 0) |
Target (level 1) |
Naive |
Anchored |
|---|---|---|---|
chunk 3 = |
chunk 2 = |
|
|
chunk 1 = |
chunk 1 = |
|
|
anchor_chunk maps the
source into the target level’s grid first:
anchor = floor(c_src * r_src / r_trg)
o = c_trg - anchor # decode: c_trg = anchor + o
r_L is level L’s chunk_shape as an integer multiple of the root
chunk_shape
(chunk_scale_from_root).
This r is relative to the root; the chunk_scale_factor= argument of
coarsen_level is relative to the parent level. When r_src == r_trg, anchor == c_src. Floor division rounds toward
-inf, as negative chunk coordinates need.
All endpoints of a store share sid_ndim; a mismatch raises
ArrayError. One <delta> covers all non-source endpoints, so a record
spanning three levels (such as a triangle at (N, N, N+1)) cannot be
stored.
Enumeration order¶
Records are read in order of offsets segment (ASCII sort), then cell,
then write order within a cell. + and - sort before 0, so the
intra segment comes last:
sorted(['0.0.0', '0.0.+1', '0.0.-1', '+1.0.0', '-1.0.0'])
# ['+1.0.0', '-1.0.0', '0.0.+1', '0.0.-1', '0.0.0']
read_links and
read_link_attributes use the same order; it is what aligns attribute
rows with link records. write_link_attributes without a
LinkPartition expects attr_data in this order. Pass the
LinkPartition returned by write_links to give data in input order.
Under store="duplicate" the partition is required.
Metadata¶
Each node below stores these keys in its zarr_vectors attribute, beside
"zarr_conventions": [{"uuid": "66268a61-7833-4995-bbcb-233bb417dae5"}].
links/<delta>/ group (family policy):
{
"kind": "links_family",
"level_delta": 0,
"link_width": 2,
"directed": false,
"store": "canonical",
"sid_ndim": 3,
"num_links": 12, // logical record count
"num_physical_records": 12 // on-disk rows; > num_links under "duplicate"
}
links/<delta>/<offsets>/ array:
{
"kind": "links",
"nonempty_chunks": ["0.0.0", "1.0.0"],
"dtype": "int64",
"offsets": [[0, 0, 1]], // parsed path segment
"has_perm": true, // rows are 1 + L wide
"link_width": 2,
"level_delta": 0
}
read_links uses the stored has_perm, and computes it from the family
policy only when it is missing.
link_attributes/<name>/<delta>/ group:
{ "kind": "link_attribute_family", "name": "weight", "level_delta": 0, "num_links": 8 }
link_attributes/<name>/<delta>/<offsets>/ array:
{
"kind": "link_attribute",
"nonempty_chunks": ["0.0.0", "1.0.0"],
"name": "weight",
"dtype": "float32",
"offsets": [[1, 0, 0]],
"level_delta": 0,
"row_shape": [] // per-record shape; [] for a scalar
}
Parallel writes and counts¶
write_links keeps num_links and num_physical_records up to date.
The per-cell writer write_link_cells does not; one
finalize_links call sets them
afterwards and rebuilds nonempty_chunks. Under duplicate it counts
logical records by deduplicating decoded records.
In an unsharded array each cell is its own object. Workers that own
disjoint sets of source chunks can write in parallel; two workers must
not write the same cell. Workers pass record_presence=False, because
nonempty_chunks is shared by the whole array. create_links_family
sets the family policy without creating an offsets array.
Reading¶
from zarr_vectors.core.store import get_resolution_level, open_store
from zarr_vectors.building import (
read_links, read_link_attributes, read_links_for_tuple,
read_chunk_links, list_link_deltas, list_link_offsets,
)
root = open_store("graph.zarrvectors")
lg = get_resolution_level(root, 0)
records = read_links(lg, delta=0) # whole family
weights = read_link_attributes(lg, "weight", delta=0) # aligned to records
print(list_link_deltas(lg)) # e.g. [0, 1]
print(list_link_offsets(lg, 0)) # intra sorts last
read_links returns [] when the family is absent or has no policy.
read_chunk_links reads one cell. read_links_for_tuple reads only the
records spanning an exact L-chunk tuple. For an undirected canonical
family at delta == 0 the tuple may be in any order; for any other
family it must lead with input endpoint 0’s chunk.
Validation¶
From zarr_vectors/validate/consistency.py
(see L3 consistency). L1 lists the
<delta> segments under links/ and checks nothing else.
L3, for every <delta> family with a sid_ndim:
Check |
Applies to |
|---|---|
Every |
all families |
No offset is lexicographically negative; offsets are non-decreasing across the segment |
undirected, |
|
all families |
Every endpoint chunk exists in the level’s chunk grid |
|
The source-side endpoint chunk exists in the level’s chunk grid |
|