Skip to content

Step-by-Step Refactoring Plan for NTSim

Phase 1: Preparation and Git Organization

  1. Set Up Git Branching and Initial State

    • Create Release Tag: Tag the current stable version of ntsim_2.py (e.g., v0.0.9) so we can always reference or revert to it if needed.
    • Create Feature Branches:
      • For each feature, create a new branch. The naming convention could be: dvn/feature/<feature-name> for user dvn and updated appropriately for other users.
      • Example branches:
        • dvn/feature/config-refactor
        • dvn/feature/argument-parser-modularization
        • dvn/feature/simulation-split
  2. Set Up Continuous Integration (CI)

    • Use GitHub Actions (or another CI platform) to run unit tests and style checks (flake8, black, anything else?).
    • Create a simple workflow to run tests on push or pull requests to any feature branch.

Phase 2: Refactor Argument Handling and Configuration

  1. Refactor Argument Parsing (Feature Branch: dvn/feature/argument-parser-modularization)

    • Create a New Script: Create ntsim.py as a fresh starting point.
    • Move Argument Parsing to ntsim_arguments.py:
      • Split argument parsing into a standalone module.
      • Use configargparse but enhance it with config file support. Allow users to provide a YAML/JSON configuration.
    • Introduce a Configuration File:
      • Provide a default configuration file (ntsim.yaml) that can be customized.
      • Modify ntsim.py to load settings from the configuration file first, then override with command-line arguments if provided.
  2. Refactor Initialization Logic (Feature Branch: dvn/feature/config-refactor)

    • Create a configuration management class (NTSimConfig) to handle all the configurations.
    • Goals:
      • Centralize all configuration in one place.
      • Reduce the use of direct references to opts across the script.
      • Ensure the command-line interface can override configuration settings.

Phase 3: Refactor NTSim Class and Split Responsibilities

  1. Split NTSim Responsibilities (Feature Branch: dvn/feature/simulation-split)

    • Break down the large responsibilities of the NTSim class into multiple methods and smaller classes:
      • NTSimArgumentParser: Handle all argument parsing.
      • NTSimConfigurator: Responsible for reading the configuration, setting up dependencies, and initializing objects.
      • NTSimSimulator: Focus purely on the simulation logic.
      • NTSimWriter: Handle all file writing and logging.
    • Action Steps:
      • Extract methods from process().
      • Move configure() logic to NTSimConfigurator.
      • Create clean, readable methods for event initialization, particle simulation, photon propagation, etc.
    • Deliverables:
      • Each class or function has a single responsibility.
      • Each method is easily testable in isolation.
  2. Introduce Modular Factories (Feature Branch: dvn/feature/factory-improvements)

    • Refactor Factories: Update the current Factory classes so that dependencies are instantiated dynamically.
    • Blueprint Handling: Avoid the dual usage of terms like "Blueprint." Consider using clearer terms like ConfigTemplate vs. Instance.

Phase 4: Simplify Package Management & Installation

  1. Package and Module Management (Feature Branch: dvn/feature/package-management)

    • Optional Installation:
      • Update pyproject.toml to include optional installations:
         [project.optional_dependencies]
         generator-neutrino-ava = "nupropagator>=0.0.2"
         # or generator-neutrino-ava = ["nupropagator @ git@git.jinr.ru:ava/nupropogator.git"] 
        
         telescope-baikal = ["baikal-model @ git@git.jinr.ru:Baikal/bgvd-model.git"]
        
        Users should be able to run pip install ntsim[telescope-baikal] or to pip install ntsim[generator-neutrino-ava] install optional dependencies.
    • Interactive Welcome Mode:
      • Implement a welcome feature in ntsim.py to display available configurations based on installed packages.
      • Optional Dependency Installer: Add a utility that informs the user which optional dependencies are missing and offers the necessary pip install command.
  2. Installation Verification (Feature Branch: dvn/feature/installation-check)

    • Add Diagnostic Script (ntsim_check.py):
      • This script checks the current environment for missing dependencies.
      • Print a summary with steps to install any missing components.
    • Post-Installation Report:
      • Add a post-installation success message summarizing the status of optional components.

Phase 5: Testing & Verification

  1. Implement Unit Tests for Core Components (Feature Branch: dvn/feature/unit-testing)

    • Create tests for individual classes:
      • Configuration Parsing: Verify NTSimConfig correctly reads and overrides settings.
      • Factory Modules: Test each Factory’s get_blueprint() and configure() methods.
      • Simulation Methods: Test components of process(), especially the refactored methods like simulate_particles(), simulate_photons().
    • Add test cases for:
      • Edge Scenarios: Such as missing dependencies or misconfigured inputs.
      • Optional Dependencies: Test welcome mode behavior based on installed components.
  2. End-to-End Testing (Feature Branch: dvn/feature/e2e-testing)

    • Set up tests to run complete simulations, validate the output, and check for correctness and consistency.
    • Use the tqdm simulation loops and validate progress and logging consistency.

Phase 6: Documentation & User Interface

  1. Update Documentation (Feature Branch: dvn/feature/documentation)

    • Update the README.md to reflect new setup methods and modular installations.
    • Write user guides for:
      • Installing Different Modes
        • welcome (minimal possible informing the user what is available and what could be installed)
        • baikal-gvd (install all data and models for GVD),
        • generator-neutrino (ava, nugen, proposal, all),
        • generator-muon (Corsika, ...)
        • ...
      • Using the New Configuration System: Include examples of YAML/JSON config.
    • Write a Developer Guide on contributing to ntsim, focusing on how to use feature branches and how to run tests.
  2. User-Friendly Logging & CLI Improvements (Feature Branch: dvn/feature/logging-improvements)

    • Improve logging:
      • Refactor logging configurations to be more modular (ntsim_logging.py).
      • Add detailed log levels to track progress in different parts of the simulation (INFO, DEBUG, WARNING).
    • Command-Line Interface:
      • Shorten the command-line usage by leveraging the configuration file.
      • Provide meaningful error messages if required arguments are missing or incorrectly formatted.

Phase 7: Final Release & Feedback Collection

  1. Prepare for Release (Feature Branch: dvn/feature/release-preparation)

    • Ensure all feature branches are merged into main.
    • Tag a new release version (v0.1-beta).
    • Include release notes summarizing major refactoring steps, new features, and breaking changes.
  2. User Feedback & Iteration

    • Release to a subset of users for beta testing.
    • Gather feedback on:
      • Ease of Use: Are the new configurations and installations user-friendly?
      • Performance: Does the refactored code maintain or improve simulation times?
    • Prioritize bug fixes and minor enhancements based on feedback.