Source code for upxo.pxtal.images1

"""
Composable 3D labelled-image stack (ops / props / viz plugins).

Experimental redesign of :class:`~upxo.pxtal.images.IMAGE_3D` with
abstract bases for morphological, topological, and spatial operations,
property calculators, visualisers, and :class:`IMAGE_3D_New` as the
orchestrating container. Prefer the mature ``IMAGE_3D`` / MCGS paths for
production until this stack is fully implemented.
"""

from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from typing import Tuple, Dict, Any, Optional, List, Set
import numpy as np
import networkx as nx

# Base Classes for Operations
[docs] class BaseOperations(ABC): """Abstract partitioning ops interface for 3D label images."""
[docs] @abstractmethod def part(self, img: np.ndarray) -> Tuple[np.ndarray, int]: """Partition an image into features""" pass
[docs] class BaseProperties(ABC): """Abstract per-feature property calculator interface."""
[docs] @abstractmethod def calculate(self, feature_id: int) -> Dict[str, Any]: """Calculate properties for a given feature""" pass
[docs] class BaseVisualizer(ABC): """Abstract visualisation interface for 3D image features."""
[docs] @abstractmethod def show(self, data: np.ndarray) -> None: """Display the data""" pass
# Configuration Classes
[docs] @dataclass class VoxelConfig: """Voxel size and anisotropy for physical scaling of IMAGE_3D_New.""" size: float = 1.0 anisotropy: Tuple[float, float, float] = (1.0, 1.0, 1.0)
[docs] @dataclass class ImageConfig: """Top-level config for :class:`IMAGE_3D_New` (voxel, connectivity, debug).""" voxel: VoxelConfig = field(default_factory=VoxelConfig) connectivity: int = 1 debug_mode: bool = False
# Concrete Operation Classes
[docs] class MorphologicalOps(BaseOperations): """Connected-component partitioning via scipy.ndimage label structures.""" def __init__(self, config: ImageConfig): """Initialise with an ``ImageConfig`` and build the structuring element.""" self.config = config self.structure = self._create_structure() def _create_structure(self) -> np.ndarray: """Create structuring element based on connectivity""" from scipy.ndimage import generate_binary_structure return generate_binary_structure(3, self.config.connectivity)
[docs] def part(self, img: np.ndarray) -> Tuple[np.ndarray, int]: """Partition image into features using connected components""" from scipy.ndimage import label return label(img, structure=self.structure)
[docs] class TopologicalOps(BaseOperations): """Topological partitioning of 3D features (not implemented yet).""" def __init__(self, config: ImageConfig): """Initialise with an ``ImageConfig``.""" self.config = config
[docs] def part(self, img: np.ndarray) -> Tuple[np.ndarray, int]: """Partition based on topological features""" # Implementation specific to topological partitioning raise NotImplementedError("Topological partitioning not implemented")
[docs] class SpatialOps(BaseOperations): """Spatial-feature partitioning of 3D volumes (not implemented yet).""" def __init__(self, config: ImageConfig): """Initialise with an ``ImageConfig``.""" self.config = config
[docs] def part(self, img: np.ndarray) -> Tuple[np.ndarray, int]: """Partition based on spatial features""" raise NotImplementedError("Spatial partitioning not implemented")
# Property Calculator Implementations
[docs] class MorphologicalProps(BaseProperties): """Volume, centre, and bbox morphological props for one feature ID.""" def __init__(self, image3d: 'IMAGE_3D_New'): """Initialise with a reference to the parent ``IMAGE_3D_New`` object.""" self.image3d = image3d
[docs] def calculate(self, feature_id: int) -> Dict[str, Any]: """Calculate morphological properties for a feature""" mask = self.image3d.img == feature_id props = { 'volume': np.sum(mask), 'center': np.mean(np.where(mask), axis=1), 'bbox': self._calculate_bbox(mask) } return props
def _calculate_bbox(self, mask: np.ndarray) -> Tuple[Tuple[int, int], ...]: """Calculate bounding box for a feature mask""" return tuple(slice(start, stop) for start, stop in zip(np.min(np.where(mask), axis=1), np.max(np.where(mask), axis=1) + 1))
[docs] class TopologicalProps(BaseProperties): """Neighbour-graph topological props (degree, betweenness) per feature.""" def __init__(self, image3d: 'IMAGE_3D_New'): """Initialise with a reference to the parent ``IMAGE_3D_New`` object.""" self.image3d = image3d self.graph = None
[docs] def calculate(self, feature_id: int) -> Dict[str, Any]: """Calculate topological properties for a feature""" if self.graph is None: self._build_graph() props = { 'neighbors': list(self.graph.neighbors(feature_id)), 'degree': self.graph.degree(feature_id), 'betweenness': nx.betweenness_centrality(self.graph)[feature_id] } return props
def _build_graph(self): """Build adjacency graph of features""" self.graph = nx.Graph()
# Implementation details for graph construction
[docs] class SpatialProps(BaseProperties): """Position, extent, and anisotropy spatial props for one feature ID.""" def __init__(self, image3d: 'IMAGE_3D_New'): """Initialise with a reference to the parent ``IMAGE_3D_New`` object.""" self.image3d = image3d
[docs] def calculate(self, feature_id: int) -> Dict[str, Any]: """Calculate spatial properties for a feature""" mask = self.image3d.img == feature_id props = { 'position': np.mean(np.where(mask), axis=1), 'extent': np.ptp(np.where(mask), axis=1), 'anisotropy': self._calculate_anisotropy(mask) } return props
def _calculate_anisotropy(self, mask: np.ndarray) -> float: """Calculate anisotropy of a feature""" # Implementation for anisotropy calculation return 0.0
# Visualizer Implementations
[docs] class MorphologicalViz(BaseVisualizer): """Max-projection matplotlib views of morphological 3D fields.""" def __init__(self, image3d: 'IMAGE_3D_New'): """Initialise with a reference to the parent ``IMAGE_3D_New`` object.""" self.image3d = image3d
[docs] def show(self, data: np.ndarray) -> None: """Display morphological features""" import matplotlib.pyplot as plt fig, axes = plt.subplots(1, 3, figsize=(15, 5)) for ax, idx in zip(axes, [0, 1, 2]): ax.imshow(np.max(data, axis=idx)) ax.set_title(f'Max projection {["X", "Y", "Z"][idx]}') plt.show()
[docs] class TopologicalViz(BaseVisualizer): """NetworkX graph visualisation of feature topology (stub layout).""" def __init__(self, image3d: 'IMAGE_3D_New'): """Initialise with a reference to the parent ``IMAGE_3D_New`` object.""" self.image3d = image3d
[docs] def show(self, data: np.ndarray) -> None: """Display topological features""" import networkx as nx import matplotlib.pyplot as plt G = nx.Graph() # Build and display graph pos = nx.spring_layout(G) nx.draw(G, pos, with_labels=True) plt.show()
# Main Image Class
[docs] class IMAGE_3D_New: """ Plugin-style 3D labelled image: config + ops/props/viz bundles. Holds a working copy of a 3D integer volume, :class:`ImageConfig`, and dictionaries of operation / property / visualisation plugins. Prefer :class:`~upxo.pxtal.images.IMAGE_3D` for production until this redesign is complete. """ __slots__ = ('base', 'img', 'config', 'ops', 'props', 'viz', '_feature_cache') def __init__(self, image: np.ndarray, config: Optional[ImageConfig] = None): """Initialize with image data and optional configuration""" if not isinstance(image, np.ndarray): raise TypeError("Image must be a numpy array") if image.ndim != 3: raise ValueError("Image must be 3-dimensional") self.base = image self.img = image.copy() self.config = config or ImageConfig() self._feature_cache = {} # Initialize operation subsystems self.ops = { 'morphological': MorphologicalOps(self.config), 'topological': TopologicalOps(self.config), 'spatial': SpatialOps(self.config) } # Initialize property calculators self.props = { 'morphological': self._create_mprops(), 'topological': self._create_tprops(), 'spatial': self._create_sprops() } # Initialize visualizers self.viz = { 'morphological': self._create_mviz(), 'topological': self._create_tviz() } def _create_mprops(self) -> BaseProperties: """Create morphological property calculator""" return MorphologicalProps(self) def _create_tprops(self) -> BaseProperties: """Create topological property calculator""" return TopologicalProps(self) def _create_sprops(self) -> BaseProperties: """Create spatial property calculator""" return SpatialProps(self) def _create_mviz(self) -> BaseVisualizer: """Create morphological visualizer""" return MorphologicalViz(self) def _create_tviz(self) -> BaseVisualizer: """Create topological visualizer""" return TopologicalViz(self)
[docs] def partition(self, method: str = 'morphological') -> Tuple[np.ndarray, int]: """Partition the image using specified method""" if method not in self.ops: raise ValueError(f"Unknown partition method: {method}") return self.ops[method].part(self.img)
[docs] def clear_cache(self): """Clear the feature cache""" self._feature_cache.clear()
def __repr__(self) -> str: """Return ``IMAGE_3D_New(shape=..., features=...)`` summary string.""" return f"IMAGE_3D_New(shape={self.img.shape}, features={self.count_features()})"
[docs] def count_features(self) -> int: """Count unique features in the image""" return len(np.unique(self.img)) - 1 # Excluding background