Masking¶
Coalition masking codecs, views, and absence policies.
@domain masking @see [[knowledge/architecture/masking]]
Player partition plugins: whitespace tokens and whole messages.
@domain masking @see [[knowledge/architecture/masking]] @tested-by [[tests/masking/test_partitions.py]]
- class mllm_shap.masking.partitions.RolesOptions(*, roles: list[Literal['user', 'assistant', 'system']] = ['user', 'assistant', 'system'])[source]¶
Bases:
PluginOptionsOptions of the
tokensandmessagespartitions.- roles: list[Literal['user', 'assistant', 'system']]¶
Roles whose text becomes players; other messages are always present.
- model_config = {'extra': 'forbid', 'frozen': True, 'strict': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- mllm_shap.masking.partitions.MessagesOptions¶
Options of the
messagespartition (same fields astokens).
- class mllm_shap.masking.partitions.TokenPartitioner(context: PluginContext | None = None)[source]¶
Bases:
objectPartition a snapshot into whitespace-delimited text tokens.
- options_model¶
Options accepted from
explanation.plugin_options.alias of
RolesOptions
- roles¶
Roles whose tokens become players.
- partition(snapshot: ConversationSnapshot) PlayerSet[source]¶
Derive ordered token players and their spans from the snapshot text.
Tokens of messages whose role is not in
rolesare no players and stay present in every coalition; player ids do not depend on the filter.
- mllm_shap.masking.partitions.LABEL_PREVIEW_CHARS: Final[int] = 30¶
Characters of the message text shown in a message player’s label.
- class mllm_shap.masking.partitions.MessagePartitioner(context: PluginContext | None = None)[source]¶
Bases:
objectPartition a snapshot into one player per non-empty message.
- options_model¶
Options accepted from
explanation.plugin_options.alias of
RolesOptions
- roles¶
Roles whose messages become players.
- partition(snapshot: ConversationSnapshot) PlayerSet[source]¶
Make each selected message a player whose span is its whole text.
Player ids are
{snapshot_id}:{turn}:{message}; labels read"<role>[/<speaker>] <n>: <first 30 characters>"withnthe 1-based message position.
Whitespace token spans and span-based snapshot rendering.
@domain masking @see [[knowledge/architecture/masking]] @tested-by [[tests/masking/test_tokens.py]]
- mllm_shap.masking.tokens.token_spans(snapshot: ConversationSnapshot) tuple[tuple[str, PlayerSpan], ...][source]¶
Return
(player_id, span)for every whitespace token ofsnapshot.Player ids have the form
{snapshot_id}:{turn}:{message}:{token}.
- mllm_shap.masking.tokens.render_spans(snapshot: ConversationSnapshot, spans: Sequence[PlayerSpan], replacements: Sequence[str | None]) ConversationSnapshot[source]¶
Rebuild
snapshotwith each span replaced by its replacement text.Nonedeletes a span together with the whitespace that separated it from its neighbours, so deletingbfrom"a b c"gives"a c". Text no span covers is kept as is.- Raises:
ValueError – If
spansandreplacementsdiffer in length, or a span does not match the snapshot text.
Absence policies for rendering masked conversation snapshots.
@domain masking @see [[knowledge/architecture/masking]] @tested-by [[tests/masking/test_policies.py]]
- class mllm_shap.masking.policies.PadOptions(*, placeholder: str = '[MASK]')[source]¶
Bases:
PluginOptionsOptions of the
padabsence policy.- model_config = {'extra': 'forbid', 'frozen': True, 'strict': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class mllm_shap.masking.policies.NeutralOptions(*, fill_char: str = '_')[source]¶
Bases:
PluginOptionsOptions of the
neutralabsence policy.- model_config = {'extra': 'forbid', 'frozen': True, 'strict': True}¶
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class mllm_shap.masking.policies.DeletePolicy(context: PluginContext | None = None)[source]¶
Bases:
objectRemove absent players from the rendered conversation.
- options_model¶
Accepts no
explanation.plugin_options.alias of
PluginOptions
- apply(snapshot: ConversationSnapshot, players: PlayerSet, mask: CoalitionMask) ConversationSnapshot[source]¶
Drop absent players (text, and audio samples) keeping the message structure.
The empty coalition keeps every message with empty player text (v(empty)).
- class mllm_shap.masking.policies.PadPolicy(context: PluginContext | None = None, placeholder: str = '[MASK]')[source]¶
Bases:
objectReplace absent players with a fixed mask placeholder.
- options_model¶
Options accepted from
explanation.plugin_options.alias of
PadOptions
- placeholder¶
Placeholder text inserted for absent players.
- apply(snapshot: ConversationSnapshot, players: PlayerSet, mask: CoalitionMask) ConversationSnapshot[source]¶
Substitute absent players with
placeholder; silence their audio.
- class mllm_shap.masking.policies.NeutralPolicy(context: PluginContext | None = None, fill_char: str = '_')[source]¶
Bases:
objectReplace absent players with neutral fillers that preserve their width.
- options_model¶
Options accepted from
explanation.plugin_options.alias of
NeutralOptions
- fill_char¶
Single character repeated to match each absent player’s length.
- apply(snapshot: ConversationSnapshot, players: PlayerSet, mask: CoalitionMask) ConversationSnapshot[source]¶
Substitute width-matched fillers for absent players; silence their audio.
Render audio players: cut or silence absent samples, hide alignment transcripts.
@domain masking @see [[knowledge/architecture/masking]] @tested-by [[tests/alignment/test_sgpa_players.py]]
- mllm_shap.masking.audio.render_audio(snapshot: ConversationSnapshot, spans: Sequence[PlayerSpan], present: Sequence[bool], cut: bool) ConversationSnapshot[source]¶
Remove (
cut) or silence the samples of absent players withsamples.Silencing keeps the audio’s length and timing; cutting joins what is left. Spans without
samplesand messages without absent audio are untouched.
- mllm_shap.masking.audio.hide_text(snapshot: ConversationSnapshot, message_indices: Sequence[int]) ConversationSnapshot[source]¶
Drop the text parts of the messages at
message_indices(turns flattened).
View-based masked snapshots with structural sharing.
@domain masking @see [[knowledge/architecture/masking]] @tested-by [[tests/masking/test_builder.py]]
- class mllm_shap.masking.builder.MaskedSnapshot(base: ConversationSnapshot, players: PlayerSet, mask: CoalitionMask, policy_name: str)[source]¶
Bases:
objectShared view over a conversation snapshot and coalition mask.
- base: ConversationSnapshot¶
Underlying snapshot shared across coalition evaluations.
- mask: CoalitionMask¶
Presence mask selecting which players remain in the coalition.
- class mllm_shap.masking.builder.MaskBuilder(policy: AbsencePolicy)[source]¶
Bases:
objectBuild masked snapshot views without copying the base conversation.
- view(snapshot: ConversationSnapshot, players: PlayerSet, mask: CoalitionMask) MaskedSnapshot[source]¶
Create a masked view that shares
snapshotacross coalitions.
- render(masked: MaskedSnapshot) ConversationSnapshot[source]¶
Materialize a coalition-specific conversation snapshot.
Packed bitset encoding and stable hashing for coalition masks.
@domain masking @see [[knowledge/architecture/masking]] @tested-by [[tests/masking/test_codec.py]]
- class mllm_shap.masking.codec.PackedMask(words: bytes, n_bits: int)[source]¶
Bases:
objectPacked bit representation of one boolean mask.
- class mllm_shap.masking.codec.MaskCodec[source]¶
Bases:
objectEncode and decode boolean masks to packed bytes.
- static normalize(mask: Sequence[bool] | NDArray[bool]) NDArray[bool][source]¶
Normalize incoming mask to a 1D boolean array.
- static pack(mask: Sequence[bool] | NDArray[bool]) PackedMask[source]¶
Pack a mask into little-endian bytes.
- static unpack(packed: PackedMask) tuple[bool, ...][source]¶
Unpack bytes back to a boolean tuple.
Mask-space utilities for explainable feature indexing.
@domain masking @see [[knowledge/architecture/masking]] @tested-by [[tests/masking/test_space.py]]