Add .explain() DSL predicate for node-level explanations#968
Merged
Conversation
- Added ExplainSpec dataclass to AST for explanation configuration - Implemented explain() method in QueryBuilder with validation - Created py3plex/dsl/explain.py with explanation engine for nodes - Community explanation (community_id, community_size) - Top neighbors explanation (ranked by weight or degree) - Layer footprint explanation (layers_present, n_layers_present) - Integrated explanation step into executor (runs after LIMIT) - Added expand_explanations parameter to QueryResult.to_pandas() - Explanations support per-layer grouping context Co-authored-by: SkBlaz <10035780+SkBlaz@users.noreply.github.com>
- Updated explain() to handle two modes: 1. No args: returns ExplainQuery (execution plan, backward compat) 2. With args: attaches explanations to results (new feature) - Removed duplicate explain() method - Added comprehensive tests covering all features - All 26 explain tests pass - Backward compatibility maintained (67 DSL v2 tests pass) Co-authored-by: SkBlaz <10035780+SkBlaz@users.noreply.github.com>
- Created example_dsl_explain.py demonstrating all features - Shows basic usage, custom explanation blocks, flagship pattern - Includes neighbor ranking options and exclusion patterns - All examples run successfully with correct output Co-authored-by: SkBlaz <10035780+SkBlaz@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add new DSL predicate operator
Add .explain() DSL predicate for node-level explanations
Dec 30, 2025
.explain(...)
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.
Adds
.explain()to the DSL query API for attaching explanations (community membership, top neighbors, layer footprint) to result nodes. Explanations are computed post-filtering for efficiency and expand viato_pandas(expand_explanations=True).Changes
DSL API
QueryBuilder.explain(): Dual-mode methodExplainSpecdataclass in AST for configurationExplanation Engine (
py3plex/dsl/explain.py)community_id,community_sizefrom network partitionlayers_present,n_layers_presentfor multilayer nodesResult Integration
QueryResultto_pandas(expand_explanations=True)expands complex structures to JSON stringsUsage
Testing
Original prompt
This section details on the original issue you should resolve
<issue_title>explain()</issue_title>
<issue_description>
Goal
Add a new DSL predicate/operator:
.explain(...)that can be chained before.execute(network)It attaches “explanations” to each resulting row/entity (typically nodes), enabling:
result.to_pandas(expand_explanations=True) # or expand_explanations="columns"
Must support the flagship usage:
Q.nodes()...limit(20).explain(
neighbors_top=10,
include=["community", "top_neighbors", "layer_footprint"]
).execute(network)
and then:
High-level behavior
.explain()does NOT change which rows are returned; it adds metadata per row.Deliverables
Query.explain(...)methodto_pandas(expand_explanations=True)supportTODO 0 — Locate architecture touchpoints
TODO 1 — Define
.explain()public APIImplement a method on the Query object:
Semantics
Default: ["community", "top_neighbors", "layer_footprint"]
top_neighborsexplanationexplanationsfield but exposealso as top-level columns when expand_explanations=True.
Validation rules
A) multiple calls merge includes and override config
B) raise if explain already present
Prefer A for ergonomics, but implement carefully.
Return value
TODO: follow existing patterns for other predicates (e.g., .mutate returns new query?).
TODO 2 — Add pipeline “ExplainStep” to DSL execution plan
so only final rows are explained.
Execution ordering
TODO 3 — Explanation engine (Phase 1: node explanations)
Implement in a new module, e.g.
py3plex/dsl/explain.pyorpy3plex/analysis/explain.py.Public entrypoint
def explain_rows(
network,
rows: list[dict] | pandas.DataFrame,
*...
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.