"""Construct versioned models from self-contained checkpoint configuration.""" from torch import nn from src.models.refiners import ProfileCascade, ZeroAtomNet from src.models.unet import ( AxialBoundaryNet, BoundaryNet, GlobalBoundaryNet, SpatialBoundaryNet, ) class BoundaryEnsemble(nn.Module): def __init__(self, components): super().__init__() self.nets = nn.ModuleList(components) def forward(self, *args, **kwargs): raise ValueError("An ensemble must combine original-coordinate predictions") def make_model(config): kwargs = { "boundary_keys": config["boundary_keys"], "base": config["base"], "in_channels": config.get("in_channels", 1), } architecture = config.get("architecture", "boundary_unet_v1") if architecture == "boundary_volume_refine_v1": from src.models.volume import VolumeRefiner return VolumeRefiner(config) if architecture == "boundary_profile_refine_v1": return ProfileCascade(config) if architecture == "boundary_zero_atom_v1": return ZeroAtomNet(config) if architecture == "boundary_ensemble_v1": components = config["components"] if any( c["config"].get("architecture") == "boundary_ensemble_v1" for c in components ): raise ValueError("Nested ensembles are not supported") return BoundaryEnsemble([make_model(c["config"]) for c in components]) if architecture == "boundary_unet_spatial_v1": return SpatialBoundaryNet( **kwargs, layers=config.get("axial_layers", 2), lateral_layers=config.get("lateral_layers", 2), heads=config.get("axial_attention_heads", 6), ) if architecture == "boundary_unet_v1": return BoundaryNet(**kwargs) if architecture == "boundary_unet_axial_v1": return AxialBoundaryNet( **kwargs, layers=config.get("axial_layers", 2), heads=config.get("axial_attention_heads", 6), ) if architecture == "boundary_unet_global_v1": return GlobalBoundaryNet( **kwargs, layers=config.get("axial_layers", 0), heads=config.get("axial_attention_heads", 6), ) raise ValueError("Unsupported checkpoint architecture")