BNMPy.model_compressor

The model_compressor module provides tools for simplifying Boolean Networks before optimization.

class BNMPy.model_compressor.ModelCompressor(network, measured_nodes: Set[str] | None = None, perturbed_nodes: Set[str] | None = None)[source]

Bases: object

Model compression for Boolean Networks.

Provides methods to compress models by removing non-observable/non-controllable nodes and collapsing linear paths to simplify the network structure.

Methods

collapse_paths(paths)

Collapse linear paths by removing intermediate nodes and creating direct connections.

compress([remove_non_observable, ...])

Compress the model by removing non-observable/non-controllable nodes and collapsing linear paths.

find_collapsible_paths()

Find linear paths that can be collapsed.

find_non_controllable_nodes()

Find nodes that are non-controllable (no path from perturbed species).

find_non_observable_nodes()

Find nodes that are non-observable (no path to measured species).

get_compression_summary(compression_info)

Generate a summary of the compression results.

remove_nodes(nodes_to_remove)

Remove specified nodes from the network.

visualize_compression(original_network[, ...])

Visualize the compression results showing removed nodes and edges.

__init__(network, measured_nodes: Set[str] | None = None, perturbed_nodes: Set[str] | None = None)[source]

Initialize the model compressor.

find_non_observable_nodes() Set[str][source]

Find nodes that are non-observable (no path to measured species).

find_non_controllable_nodes() Set[str][source]

Find nodes that are non-controllable (no path from perturbed species).

find_collapsible_paths() List[List[str]][source]

Find linear paths that can be collapsed.

A collapsible path is a series of nodes that form a linear cascade, where intermediate nodes can be removed without losing connectivity.

collapse_paths(paths: List[List[str]]) None[source]

Collapse linear paths by removing intermediate nodes and creating direct connections. For each path, update all references to any relay node in the path to the ultimate source.

remove_nodes(nodes_to_remove: Set[str]) None[source]

Remove specified nodes from the network.

compress(remove_non_observable: bool = True, remove_non_controllable: bool = True, collapse_linear_paths: bool = True) Dict[str, Set[str]][source]

Compress the model by removing non-observable/non-controllable nodes and collapsing linear paths.

get_compression_summary(compression_info: Dict) str[source]

Generate a summary of the compression results.

visualize_compression(original_network, output_html='compression_visualization.html', interactive=False)[source]

Visualize the compression results showing removed nodes and edges.

BNMPy.model_compressor.compress_model(network, measured_nodes: Set[str] | None = None, perturbed_nodes: Set[str] | None = None, remove_non_observable: bool = True, remove_non_controllable: bool = True, collapse_linear_paths: bool = True)[source]

Convenience function to compress a model in one step.

Overview

Model compression simplifies networks while preserving relevant behavior. Currently supports Boolean Networks only.

Features

The compressor can:

  1. Remove non-observable nodes: Nodes without paths to measured species

  2. Remove non-controllable nodes: Nodes not influenced by perturbed species

  3. Collapse linear paths: Simplify cascades of intermediate nodes

  4. Visualize results: Show which nodes and edges were removed

Functions

compress_model

BNMPy.model_compressor.compress_model(network, measured_nodes: Set[str] | None = None, perturbed_nodes: Set[str] | None = None, remove_non_observable: bool = True, remove_non_controllable: bool = True, collapse_linear_paths: bool = True)[source]

Convenience function to compress a model in one step.

Main compression function with automatic processing.

ModelCompressor Class

class BNMPy.model_compressor.ModelCompressor(network, measured_nodes: Set[str] | None = None, perturbed_nodes: Set[str] | None = None)[source]

Bases: object

Model compression for Boolean Networks.

Provides methods to compress models by removing non-observable/non-controllable nodes and collapsing linear paths to simplify the network structure.

Methods

collapse_paths(paths)

Collapse linear paths by removing intermediate nodes and creating direct connections.

compress([remove_non_observable, ...])

Compress the model by removing non-observable/non-controllable nodes and collapsing linear paths.

find_collapsible_paths()

Find linear paths that can be collapsed.

find_non_controllable_nodes()

Find nodes that are non-controllable (no path from perturbed species).

find_non_observable_nodes()

Find nodes that are non-observable (no path to measured species).

get_compression_summary(compression_info)

Generate a summary of the compression results.

remove_nodes(nodes_to_remove)

Remove specified nodes from the network.

visualize_compression(original_network[, ...])

Visualize the compression results showing removed nodes and edges.

__init__(network, measured_nodes: Set[str] | None = None, perturbed_nodes: Set[str] | None = None)[source]

Initialize the model compressor.

find_non_observable_nodes() Set[str][source]

Find nodes that are non-observable (no path to measured species).

find_non_controllable_nodes() Set[str][source]

Find nodes that are non-controllable (no path from perturbed species).

find_collapsible_paths() List[List[str]][source]

Find linear paths that can be collapsed.

A collapsible path is a series of nodes that form a linear cascade, where intermediate nodes can be removed without losing connectivity.

collapse_paths(paths: List[List[str]]) None[source]

Collapse linear paths by removing intermediate nodes and creating direct connections. For each path, update all references to any relay node in the path to the ultimate source.

remove_nodes(nodes_to_remove: Set[str]) None[source]

Remove specified nodes from the network.

compress(remove_non_observable: bool = True, remove_non_controllable: bool = True, collapse_linear_paths: bool = True) Dict[str, Set[str]][source]

Compress the model by removing non-observable/non-controllable nodes and collapsing linear paths.

get_compression_summary(compression_info: Dict) str[source]

Generate a summary of the compression results.

visualize_compression(original_network, output_html='compression_visualization.html', interactive=False)[source]

Visualize the compression results showing removed nodes and edges.

For advanced compression with step-by-step control.

Basic Usage

import BNMPy

# Load network
network = BNMPy.load_network_from_file("network.txt")

# Define nodes
measured_nodes = {'Output1', 'Output2', 'Biomarker'}
perturbed_nodes = {'Drug1', 'Drug2', 'Input'}

# Compress
compressed_network, compression_info = BNMPy.compress_model(
    network,
    measured_nodes=measured_nodes,
    perturbed_nodes=perturbed_nodes,
    verbose=True
)

print(f"Original: {len(network.nodeDict)} nodes")
print(f"Compressed: {compressed_network.N} nodes")

Extract nodes directly from experimental data:

import BNMPy

# Load network
network = BNMPy.load_network_from_file("network.txt")

# Extract nodes from experiments
measured_nodes, perturbed_nodes = BNMPy.extract_experiment_nodes("experiments.csv")

# Compress using experimental information
compressed_network, compression_info = BNMPy.compress_model(
    network,
    measured_nodes=measured_nodes,
    perturbed_nodes=perturbed_nodes
)

Step-by-Step Compression

For detailed control:

from BNMPy.model_compressor import ModelCompressor

# Initialize compressor
compressor = ModelCompressor(network, measured_nodes, perturbed_nodes)

# Analyze network
non_observable = compressor.find_non_observable_nodes()
non_controllable = compressor.find_non_controllable_nodes()
collapsible_paths = compressor.find_collapsible_paths()

print(f"Non-observable nodes: {non_observable}")
print(f"Non-controllable nodes: {non_controllable}")
print(f"Collapsible paths: {collapsible_paths}")

# Selective compression
compression_info = compressor.compress(
    remove_non_observable=True,
    remove_non_controllable=True,
    collapse_linear_paths=True
)

# Get compressed network
compressed_network = compressor.get_compressed_network()

# Get summary
summary = compressor.get_compression_summary(compression_info)
print(summary)

Visualization

The module provides visualization capabilities:

import BNMPy

# Compress network
compressed_network, compression_info = BNMPy.compress_model(
    network,
    measured_nodes=measured_nodes,
    perturbed_nodes=perturbed_nodes
)

# Visualize compression results
BNMPy.vis_compression(
    network,                    # Original network
    compressed_network,         # Compressed network
    compression_info,          # Compression information
    "compression_results.html" # Output file
)

Visualization:

  • Node colors: Source nodes (grey), sink nodes (orange), intermediate (blue)

  • Removed nodes: Shown in light grey

  • Edge styling: Different colors for inhibitory vs activating edges

  • Interactive: Draggable nodes with physics simulation

See Also