Chart Selection¶
A chart is three anchor trees embedded in a plane, and a survey runs genes on it. A gene charted on a poor triangle is measured through a badly conditioned coordinate frame, so the choice of charts matters.
Measurement is separated from policy. chart_table measures the whole candidate
space once: the pairwise anchor graph, the quality of every triple, and how flat
each gene sits on each triple. ChartTable.select is one policy over that
table, and a caller who wants another may filter Q, read a fit row, or hand
a chosen set straight to assign_genes.
Measurement needs no survey. Locating a gene's own tree on a candidate chart and reading its out-of-plane residual says how flat that chart is for that gene, and that is a distance calculation.
reconstruction_error scores a candidate set by re-embedding the pairs its
triangles cover and measuring the result against every pair, including the
ones no chart covers. Scoring only the covered pairs would reward a thin set for
having fewer terms.
The selection thresholds default to the values AnchorTriangle.from_anchors
enforces, so a chart this policy chooses is one the survey will accept.
Leeuw & Mair (2009), verified in
docs/theory/algebra/deleeuw2009smacof-majorization.py.
chart_table
¶
Measure every chart a pool of anchors admits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trees
|
sequence of HifukuTree
|
The anchor pool, on one namespace with |
required |
labels
|
sequence of str
|
One label per anchor. |
required |
metric
|
TreeMetric
|
The metric the charts will use. |
required |
genes
|
sequence of str
|
Gene labels to fit. Defaults to the anchor labels, which is the usual case: each alignment supplies both an anchor and a gene to survey. |
None
|
gene_trees
|
sequence of HifukuTree
|
The tree of each gene. Defaults to the anchor trees. |
None
|
Returns:
| Type | Description |
|---|---|
ChartTable
|
|
Source code in src/hifuku/charts.py
ChartTable
dataclass
¶
Every candidate chart a pool of anchors admits, measured.
Attributes:
| Name | Type | Description |
|---|---|---|
labels |
list[str]
|
The |
D |
ndarray
|
|
B |
ndarray
|
|
triples |
ndarray
|
|
Q |
ndarray
|
|
genes |
list[str]
|
The |
fit |
ndarray
|
|
Notes
The triples are canonical rather than a dense (N, N, N) tensor. The
dense form is symmetric under permutation of its three axes and undefined on
the diagonal, so it is six times redundant. That is irrelevant at N = 10
and decides feasibility later: at N = M = 100 the fit array is
800 MB dense against 130 MB canonical. :meth:as_tensor builds the dense
view when N is small enough to slice interactively.
Source code in src/hifuku/charts.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
as_tensor
¶
The dense (N, N, N) view of Q, for interactive slicing.
Symmetric in its three axes and NaN wherever two indices repeat. Build
this only for a small pool: it holds N ** 3 floats where the
canonical form holds T.
Source code in src/hifuku/charts.py
containing
¶
Indices of the triples that use one anchor, by label or index.
covered_pairs
¶
Which anchor pairs a candidate set of charts measures.
Each chart is a triangle and measures its own three pairs. A pair that no chosen chart names carries no distance in the stitched frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
triples
|
array-like of int
|
Row indices into :attr: |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
|
Source code in src/hifuku/charts.py
frame_holds
¶
Whether a candidate set of charts fixes where every anchor sits.
The charts stitch into one frame by re-embedding the anchors from the pairs the charts measure. That frame means something only when those pairs hold the anchors still. Charts that join every anchor to the rest but leave some part free to turn give a frame that changes with nothing but the starting configuration.
The pair count does not answer this. A set can measure the 2n-3 pairs a plane configuration needs, spend a spare one bracing a part that is already held, and leave an anchor swinging.
Laman (1970), jacobs1997pebble.
Source code in src/hifuku/charts.py
|
reconstruction_error
¶
How much of the anchor geometry a candidate set of charts keeps.
A set of triangles covers some pairs of anchors and leaves others uncovered. This re-embeds the anchors from the covered pairs alone, then scores that configuration against every pairwise distance, including the ones no chart covers.
Scoring over every pair is the point. Stress sums only over pairs with
a positive weight, so dropping a pair deletes a non-negative term and a
thinner candidate set scores lower for free. That decrease measures the
edge count, not the embedding. The check is in
docs/theory/algebra/deleeuw2009smacof-majorization.py.
This is a set function, not a table column. What a triangle adds
depends on which triangles are already chosen, so it cannot live in
Q or fit.
The score is not monotone in the candidate set. Adding a triangle
usually lowers it and sometimes raises it, by a small amount: the
re-embedding minimizes stress over the retained pairs, while the score
measures stress over every pair, so a new pair changes the function
being minimized and can move the result to a different local minimum.
Measured on a pool of seven anchors, a rise appeared in four of six
random orders, the largest being 4e-3 against scores near 0.1.
:meth:select therefore takes the best candidate each round and stops
on the size of the improvement, rather than assuming each step helps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
triples
|
array-like of int
|
Row indices into :attr: |
required |
Returns:
| Type | Description |
|---|---|
float
|
Normalized error over all pairs, zero when the retained set reproduces the whole geometry. |
Source code in src/hifuku/charts.py
select
¶
Choose a covering set of charts, then add while it pays.
The policy has two stages. Cover every anchor, so that each one can
host a chart: at each step, take the triangle that covers the most
anchors not yet covered, and among those the one with the lowest
reconstruction error. Then keep adding the triangle that lowers the
error most, while each removes more than tol of the error.
This is one policy over an inspectable table. A caller who wants
another may filter :attr:Q, read a :attr:fit row, or pass a chosen
set straight to :func:assign_genes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tol
|
float
|
The least error a further chart must remove to earn its place. |
0.01
|
q_min
|
float
|
Discard a triple whose triangle quality falls below this. A triple with no quality at all is always discarded. Defaults to the threshold the survey itself enforces. |
None
|
b_frac_min
|
float
|
Discard a triple with a pair whose length overlap falls below this. Defaults to the threshold the survey itself enforces. |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Row indices into :attr: |
Notes
The two defaults are the thresholds AnchorTriangle.from_anchors
applies, so a chart this policy chooses is one the survey will accept.
A looser setting here emits a plan the survey then refuses.
Source code in src/hifuku/charts.py
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
assign_genes
¶
Put every gene on the chart it sits flattest on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
ChartTable
|
|
required |
triples
|
array-like of int
|
The chosen charts, as row indices into |
required |
Returns:
| Name | Type | Description |
|---|---|---|
anchors |
list[tuple[str, str, str]]
|
The chosen charts, as anchor triples, ready for a
:class: |
genes |
list[list[str]]
|
The genes assigned to each of those charts, in the same order, ready for
a :class: |
The two are returned separately because they belong to different objects: an
|
|
|
anchor triple defines a coordinate system, and the genes put on it are work
|
|
|
done inside one. A chart that no gene chose is left out of both, so no
|
|
|
chart is built with nothing to survey.
|
|
Source code in src/hifuku/charts.py
smacof
¶
smacof(D, W, dim: int = 2, *, seed: int = 0, iters: int = _SMACOF_ITERS, tol: float = _SMACOF_TOL, history: bool = False)
Weighted multidimensional scaling by majorization.
Minimizes sum_{i<j} w_ij (delta_ij - d_ij(X))^2. A weight of zero drops
a pair, which is the missing-value structure the paper describes and the way
chart selection expresses a candidate set: a pair of anchors that no chosen
triangle covers is simply not fitted.
Weighted stress has no closed form, so each step builds a quadratic
surrogate that sits above stress and touches it at the current
configuration, then jumps to the surrogate's minimum. That minimum is the
Guttman transform X = V^+ B(Y) Y. The sandwich inequality makes every
step non-increasing in stress.
Leeuw & Mair (2009), equations 4 to 14, verified in
docs/theory/algebra/deleeuw2009smacof-majorization.py.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
D
|
ndarray
|
|
required |
W
|
ndarray
|
|
required |
dim
|
int
|
Dimensions of the configuration. |
2
|
seed
|
int
|
Seed for the starting configuration, used when classical scaling cannot supply one. |
0
|
iters
|
int
|
Maximum majorization steps. |
_SMACOF_ITERS
|
tol
|
float
|
Stop when a step lowers stress by less than this. |
_SMACOF_TOL
|
history
|
bool
|
Also return the stress after each step. |
False
|
Returns:
| Type | Description |
|---|---|
ndarray or tuple
|
|
Source code in src/hifuku/charts.py
|