This skill should be used when the user asks to "modify code structure", "add new modules", "understand the codebase", "follow project architecture", "maintain code consistency", or mentions creating...
This skill defines the standard code architecture for machine learning projects based on the template structure. When modifying or extending code, follow these patterns to maintain consistency.
The project follows a modular, extensible architecture with clear separation of concerns. Each module (data, model, trainer, analysis) is independently organized using factory and registry patterns for maximum flexibility.
Use this skill when:
@register_dataset@register_model__init__.py factory wiringDo not use this skill when:
Key indicator: if the task does not require a @register_* decorator or a Factory pattern, skip this skill.
Each module uses a factory to create instances dynamically:
# Example from data_module/dataset/__init__.py
DATASET_FACTORY: Dict = {}
def DatasetFactory(data_name: str):
dataset = DATASET_FACTORY.get(data_name, None)
if dataset is None:
print(f"{data_name} dataset is not implementation, use simple dataset")
dataset = DATASET_FACTORY.get('simple')
return dataset
For detailed guidance, refer to references/factory_pattern.md.
Components register themselves via decorators:
# Example from data_module/dataset/simple_dataset.py
@register_dataset("simple")
class SimpleDataset(Dataset):
def __init__(self, data):
self.data = data
For detailed guidance, refer to references/registry_pattern.md.
Modules automatically discover and import submodules:
# Example from data_module/dataset/__init__.py
models_dir = os.path.dirname(__file__)
import_modules(models_dir, "src.data_module.dataset")
For detailed guidance, refer to references/auto_import.md.
project/
āāā run/
ā āāā pipeline/ # Main workflow scripts
ā ā āāā training/ # Training pipelines
ā ā āāā prepare_data/ # Data preparation pipelines
ā ā āāā analysis/ # Analysis pipelines
ā āāā conf/ # Hydra configuration files
ā āāā training/ # Training configs
ā āāā dataset/ # Dataset configs
ā āāā model/ # Model configs
ā āāā prepare_data/ # Data prep configs
ā āāā analysis/ # Analysis configs
ā
āāā src/
ā āāā data_module/ # Data processing module
ā ā āāā dataset/ # Dataset implementations
ā ā āāā augmentation/ # Data augmentation
ā ā āāā collate_fn/ # Collate functions
ā ā āāā compute_metrics/ # Metrics computation
ā ā āāā prepare_data/ # Data preparation logic
ā ā āāā data_func/ # Data utility functions
ā ā āāā utils.py # Module-specific utilities
ā ā
ā āāā model_module/ # Model implementations
ā ā āāā brain_decoder/ # Brain decoder models
ā ā āāā model/ # Alternative model location
ā ā
ā āāā trainer_module/ # Training logic
ā āāā analysis_module/ # Analysis and evaluation
ā āāā llm/ # LLM-related code
ā āāā utils/ # Shared utilities
ā
āāā data/
ā āāā raw/ # Original, immutable data
ā āāā processed/ # Cleaned, transformed data
ā āāā external/ # Third-party data
ā
āāā outputs/
ā āāā logs/ # Training and evaluation logs
ā āāā checkpoints/ # Model checkpoints
ā āāā tables/ # Result tables
ā āāā figures/ # Plots and visualizations
ā
āāā pyproject.toml # Project configuration
āāā uv.lock # Dependency lock file
āāā TODO.md # Task tracking
āāā README.md # Project documentation
āāā .gitignore # Git ignore rules
For detailed directory structure with file descriptions, refer to references/structure.md.
When adding a new dataset:
src/data_module/dataset/@register_dataset("name") decoratortorch.utils.data.Dataset__init__, __len__, __getitem__from torch.utils.data import Dataset
from typing import Dict
import torch
from src.data_module.dataset import register_dataset
@register_dataset("custom")
class CustomDataset(Dataset):
def __init__(self, data):
self.data = data
def __len__(self):
return len(self.data)
def __getitem__(self, i: int) -> Dict[str, torch.Tensor]:
return self.data[i]
CRITICAL: Models use config-driven pattern
When adding a new model:
src/model_module/model/ or appropriate module subdirectory@register_model('ModelName') decorator__init__ accepts ONLY cfg parameter - all hyperparameters come from configforward() returns dict: {"loss": loss, "labels": labels, "logits": logits}self.trainingfrom src.model_module.brain_decoder import register_model
@register_model('MyModel')
class MyModel(nn.Module):
def __init__(self, cfg):
super().__init__()
self.cfg = cfg
self.task = cfg.dataset.task
# ALL parameters from cfg
self.hidden_dim = cfg.model.hidden_dim
self.output_dim = cfg.dataset.target_size[cfg.dataset.task]
def forward(self, x, labels=None, **kwargs):
if self.training:
# Training logic
pass
else:
# Inference logic
pass
return {"loss": loss, "labels": labels, "logits": logits}
When adding augmentation:
src/data_module/augmentation/For comprehensive style guidelines, refer to references/code_style.md.
Key principles:
__init__.py files contain factory/registry logicThe project uses Hydra for configuration management:
run/conf/ organize by moduleFor detailed information, consult:
references/structure.md - Detailed directory structure with file descriptionsreferences/factory_pattern.md - Factory pattern in-depth explanationreferences/registry_pattern.md - Registry pattern in-depth explanationreferences/auto_import.md - Auto-import pattern in-depth explanationreferences/code_style.md - Comprehensive code style guidelinesWorking examples in examples/:
examples/custom_dataset.py - Custom dataset implementationexamples/custom_model.py - Custom model implementationexamples/augmentation_example.py - Data augmentation exampleexamples/config_example.yaml - Configuration file exampleexamples/pipeline_example.sh - Pipeline script example