feat(num_feat): add NumericalFeature.feature_matrix (#337)#347
Draft
breimanntools wants to merge 1 commit into
Draft
feat(num_feat): add NumericalFeature.feature_matrix (#337)#347breimanntools wants to merge 1 commit into
breimanntools wants to merge 1 commit into
Conversation
840b3af to
a2d28f9
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #347 +/- ##
==========================================
+ Coverage 94.83% 94.84% +0.01%
==========================================
Files 196 197 +1
Lines 18767 18871 +104
Branches 3175 3198 +23
==========================================
+ Hits 17797 17898 +101
- Misses 633 636 +3
Partials 337 337
... and 13 files with indirect coverage changes
🚀 New features to boost your workflow:
|
breimanntools
commented
Jul 5, 2026
breimanntools
left a comment
Owner
Author
There was a problem hiding this comment.
Is this really necesary? dict_num_parts goes into cpp_run__num and we then get X and not dict_num_parst to X. Why did you add this here? Was there any use caes that required this
Add NumericalFeature.feature_matrix(features, dict_num_parts, df_parts, df_scales=..., n_jobs=1), the numerical analog of SequenceFeature.feature_matrix: it turns CPP.run_num-selected features back into a model matrix X while preserving the per-residue context that per-AA-averaged sequence features discard. Values are reconstructed exactly the way CPP.run_num does — the SPLIT in each feature id is re-applied to the part's 0-based residue axis (arange(L_part)), the SCALE selects the D column, and the selected residues are nanmean-averaged (round 5). Crucially, the per-part real length L_part comes from df_parts via the SAME helper run_num uses internally (_derive_dict_part_lens, non-gap character count) rather than being inferred from the tensor's NaN padding, so X is byte-identical to run_num's value reconstruction in every case — including when a genuine residue is all-NaN across D (an unresolved structure position or masked embedding), which NaN-inference would have mis-counted as padding and shifted the split boundaries. Verified against recompute_feature_matrix for uniform, ragged, and all-NaN-real-residue inputs. The df_feat 'positions' column is a JMD-offset display numbering (e.g. 21..30 for a TMD), NOT a tensor index, so it is deliberately not used for value lookup; this is documented in the method Notes. The frontend validates df_parts (row count, part coverage, real length <= padded tensor length) before dispatch. Heavy lifting lives in NumericalFeature's own _backend/num_feat/feature_matrix.py (reusing the shared cpp split/parse helpers). Ripple: numpydoc docstring with named Returns / Raises / Examples include; executed examples notebook nf_feature_matrix.ipynb (every public parameter, display_df tables); unit tests (per-parameter positive+negative, golden hand-computed means, run_num consistency incl. the all-NaN-real-residue case, ragged parts); release-notes Unreleased entry. No __init__.py change (method on an already-exported class). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
a2d28f9 to
38366b9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #337.
Summary
Adds
NumericalFeature.feature_matrix(features, dict_num_parts, df_parts, df_scales=..., n_jobs=1),the numerical analog of
SequenceFeature.feature_matrix: it turnsCPP.run_num-selected features backinto a model matrix
X, preserving the per-residue context that per-AA-averaged sequence featuresdiscard.
run_numreturnsdf_feat(selected feature ids + stats), neverX; this is the missingmaterializer that turns those ids into the model matrix — the numerical-mode counterpart of the
sequence-mode
SequenceFeature.feature_matrixstep used throughout theaapred_*/seqopt_*examples.Details
CPP.run_numdoes — the SPLIT in each feature id isre-applied to the part's 0-based residue axis (
arange(L_part)), the SCALE selects the column, andthe selected residues are nanmean-averaged (round 5).
L_partcomes fromdf_partsvia the same helperrun_numuses internally(
_derive_dict_part_lens, non-gap character count), not inferred from the tensor's NaN padding. SoXis byte-identical torun_num's value reconstruction in every case — including when agenuine residue is all-NaN across
D(an unresolved structure position or masked embedding), whicha NaN-based length would have mis-counted as padding and shifted the split boundaries.
_cpp.pyisuntouched, so
run_numitself is unaffected.df_featpositionscolumn is a JMD-offset display numbering, not a tensor index, so it isdeliberately not used for value lookup (documented in the method Notes).
df_parts(row count, part-column coverage, real length ≤ padded tensorlength) before dispatch.
_backend/num_feat/feature_matrix.py.@staticmethod, no__init__.pychange.Verification
Byte-identical to
run_num's engine (recompute_feature_matrix) for uniform, ragged/variable-length,different-
D, and the previously-divergent all-NaN-real-residue inputs.Ripple
examples/nf_feature_matrix.ipynb(every public parameter,display_dftables)df_partsarg, golden hand-computedmeans,
run_numconsistency incl. all-NaN-real-residue parity, ragged parts)Part of epic #336.
🤖 Generated with Claude Code