Survey Analysis¶
Functions that read survey files and analyze them. They make no assumption about how a study is organized.
loadreads any mix of files, directories and glob patterns, and returns a dict from each file's path to itsSurvey. It assumes no naming scheme.run_chaingives the runs of one survey in order, by the sealed parent links and not by the text of the labels.qd_historygives the variations, the QD-score and the growth exponent of one survey. It reads the(S, N, m)rows where the record holds them, and otherwise the archive stored at the end of each run. A run that holds the records of more than one gene needs thegeneargument.hypsometric_curvegives the elite log-likelihoods of an archive, best first.explored_edgegives the edge of the filled niches as Cartesian lines on the chart.kh_scalegives each elite of an archive its height below the archive's best tree, its own Kishino-Hasegawa standard error against that tree, and their ratioz. It computes one log-likelihood pass per elite on the host, so it needs the alignment and the model of the survey.shorelinegives, from that scale, the edge z of an archive (every shoreline closes at a datum at or below it), the niche that sets it, and the z at the survey's gate.datum_rangegives, from one shoreline per gene, the cap of a study's datum, the genes that set it, and a default datum.DatumRange.to_framegives one row per gene as a polars DataFrame.halt_statusgives whether and where a survey record halted and, for a survey that did not, the projected variations to the halt.
Datums and shorelines gives the reasons for the scale and the shoreline test.
The module also exports the QD-score functions of hifuku.halt,
the coordinate functions of hifuku.utils, and elite_frame, so one import
serves an analysis. The
functions read files and write nothing. The driver does not import this
module.
analysis
¶
Helpers for analyzing hifuku survey files.
The functions here take no position on how a study is organized. load
reads any mix of files, directories and glob patterns. run_chain
follows the sealed parent links of a survey, so it works for any labeling.
The other functions work on one survey or one archive at a time. Pieces
that live in other modules and serve an analysis are aliased here, so one
import serves. The functions read files and write nothing.
KhScale
dataclass
¶
Each elite's height below its archive's best tree, and its z.
The arrays align with keys, in the archive's key order. height
is L_best - L. se is the tree's own standard error against the
best tree, by Kishino and Hasegawa's eq. 12 over site patterns weighted
by their site counts. z is height / se, and the best tree has
z = 0. The best tree is selected from the same data, so z is
biased toward overconfidence.
Source code in src/hifuku/analysis.py
Shoreline
dataclass
¶
Where the shorelines of one archive stop closing, on the KH scale.
A datum d on the scale selects the filled niches with z < d.
Every empty niche is outside that region. The boundary of the region
consists only of closed loops between filled niches when no edge elite
has z < d, that is when d is at most edge_z. An edge elite
has at least one empty niche among its eight neighbors. An empty niche
inside the filled region counts. edge_key is the niche that sets
edge_z, and n_edge counts the edge elites.
gate_z is the median z of the trees whose height lies within
SHORELINE_GATE_BAND of the gate height, or None.
Source code in src/hifuku/analysis.py
DatumRange
dataclass
¶
The datums a study's archives allow, one archive per gene.
cap is the lowest edge z over the genes: every shoreline of every
archive closes at a datum at or below it. constraining names the
gene or genes whose edge z equals the cap. default is the largest of
levels strictly below the cap, or None. per_gene and gate_z map each
gene to its edge z and to the z at which its gate falls.
Source code in src/hifuku/analysis.py
to_frame
¶
One row per gene, lowest edge z first, as a polars DataFrame.
Source code in src/hifuku/analysis.py
HaltStatus
dataclass
¶
Whether and where one survey halted, and what it would still cost.
rule is "qd_score" or "rate". halted_at is the variation
count at the halt, or None. variations is the count spent. Under
the QD-score rule, beta is the last decided growth exponent and
tau the threshold. Both are None under the rate rule. beta is
also None before the guard, without QD-score rows, and while the last
row counts no elites. projections maps each fitted model that
describes the survey to its projected total variations to the halt. It
is empty when the survey halted.
Source code in src/hifuku/analysis.py
fit_models
¶
Fit each accumulation function by least squares on log Q.
Only checkpoints with positive variations and positive QD-score are
used. Of these, the fit uses the checkpoints at or past the guard of
:func:past_guard. When fewer than three such checkpoints are past
the guard, the fit uses all of them. A model whose fit fails, or that
has fewer than three points to fit, is left out of the result.
Source code in src/hifuku/halt.py
growth_exponent
¶
The growth exponent at checkpoint i, or None when undecided.
Undecided when there is no earlier checkpoint, when the half checkpoint has no variations, or when either QD-score is not positive.
Source code in src/hifuku/halt.py
qd_scores
¶
variations_to_tau
¶
Variations at which a fitted model's growth exponent reaches tau.
Source code in src/hifuku/halt.py
barycentric_to_cartesian
¶
Map barycentric (lambda1, lambda2) to Cartesian (x, y).
lambda3 = 1 - lambda1 - lambda2 is applied internally. lam1 and
lam2 broadcast against each other.
Returns:
| Type | Description |
|---|---|
(x, y) : tuple of numpy arrays
|
Cartesian coordinates in the Euclidean chart plane, so
|
Source code in src/hifuku/utils.py
cartesian_to_barycentric
¶
Map Cartesian (x, y) to barycentric (lambda1, lambda2).
The exact inverse of :func:barycentric_to_cartesian. x and y
broadcast against each other. lambda3 is 1 - lambda1 - lambda2.
Returns:
| Type | Description |
|---|---|
(lam1, lam2) : tuple of numpy arrays
|
|
Source code in src/hifuku/utils.py
elite_frame
¶
Extract an elite archive into a dataframe of one row per elite.
Columns are those of :func:elite_arrays. backend is "pandas" or
"polars". Pass anchors to add the Cartesian x, y columns.
Source code in src/hifuku/utils.py
load
¶
Read survey files, by path, directory or glob pattern.
Each argument is a str or pathlib.Path. A directory gives every
*.h5 file in it; a pattern gives every *.h5 file it matches.
Returns a dict from each file's path to its Survey, in path order.
Two spellings of one file, such as a relative and an absolute path, give
one entry, under the first spelling. Every file is read with its seals
checked. An argument that matches no survey file raises
FileNotFoundError.
Source code in src/hifuku/analysis.py
run_chain
¶
The runs of one survey, oldest first, by their parent links.
label names the last run of the chain. By default the chain ends at
the run that no other run continues. A file that holds several chains
needs label. When the parent of a run is not in the file, the chain
starts at the earliest run that is present.
The records of one run can name different parents. gene and
chart select the record whose parent links are followed, by the rule
of :func:qd_history. Only the runs that hold such a record are part
of the chain. Without them, all records of a run must name one parent,
else this raises ValueError.
Source code in src/hifuku/analysis.py
qd_history
¶
Variations, QD-score and growth exponent of one survey.
gene and chart select one record of each run. A run with one
record needs neither. A run with more than one record needs gene,
else this raises ValueError and names the genes.
The values come from the per-checkpoint (S, N, m) rows where the
selected record of the last run holds them. Otherwise they come from
the archive stored at the end of each run. A qd array with no rows
counts as absent. The rows of the last record cover the whole chain,
back to the first run that recorded rows. The variations are the
matching last rows of its history. The offset is the lowest elite at
the last row. The growth exponent is NaN where it is undecided and at
every checkpoint before the guard of :func:hifuku.halt.past_guard.
Source code in src/hifuku/analysis.py
hypsometric_curve
¶
explored_edge
¶
The edge of the filled niches, as Cartesian lines on the chart.
The niches form a grid in barycentric coordinates. Holes inside the
filled region are filled first, so the lines trace its outer edge. A
separate island of filled niches gets a line of its own. The contour of
the occupancy grid at one half runs along the cell edges between filled
and empty niches. anchors fixes the Cartesian coordinates.
Returns a list of (x, y) array pairs, one per line. An archive with
no elites gives an empty list.
Source code in src/hifuku/analysis.py
kh_scale
¶
The per-tree Kishino-Hasegawa scale of one archive.
The best tree is the elite with the highest log-likelihood computed on
the host. On a tie, it is the first in archive.keys. Each tree costs
one pass of :func:hifuku.pruning.pattern_log_likelihoods. An archive
with no elites raises ValueError. A tree with a positive height and
a zero standard error has z = inf.
Source code in src/hifuku/analysis.py
shoreline
¶
The shoreline of an archive from its KH scale, or None.
gate_height is the height of the survey's gate below its best tree,
in nats. It is None when the survey ran under an absolute floor, and
then gate_z is None. An archive of fewer than two elites has no
shoreline.
Source code in src/hifuku/analysis.py
datum_range
¶
The datum range of a study from one shoreline per gene.
shorelines maps each gene's label to its :class:Shoreline. An
empty mapping, or a gene whose shoreline is None, raises
ValueError.
Source code in src/hifuku/analysis.py
halt_status
¶
The halt status of one survey record.