FlyGym (NeuroMechFly)

repository·main·Indexed 41 days ago

https://github.com/nely-epfl/flygym

A high-performance Python library for simulating the embodied sensorimotor control of a digital twin fruit fly (Drosophila melanogaster). Version 2.1.0 features a biomechanical model based on micro-CT scans, simulated compound eyes for vision, olfactory receptors, and a hierarchical CNS model consisting of a Brain and Ventral Nerve Cord (VNC). It includes support for leg adhesion, mechanosensory feedback, and GPU-accelerated simulations via Warp/MJWarp.

Tokens
34.2K
Snippets
116
Records
153
Agent score
56%

What's inside FlyGym

  1. Overview of NeuroMechFly v2 (FlyGym)

    main

    FlyGym is a Python library providing a digital twin of the adult fruit fly Drosophila melanogaster. It is designed for simulating embodied sensorimotor control.

    Key features include:

    • Biomechanical Model: Based on micro-CT scans of a real adult female fly with adjusted body segments (e.g., antennae).
    • Vision: Simulated compound eyes with ommatidia arranged on a hexagonal lattice.
    • Olfaction: Simulated odor/chemical intensity at receptors in the antennae and maxillary palps.
    • Hierarchical Control: Supports a two-part CNS model consisting of a Brain (sensory integration and decision making) and a Ventral Nerve Cord (VNC) (motor control), connected via descending and ascending representations.
    • Leg Adhesion: Simulates specialized adhesive structures at leg tips, which can be toggled on/off during leg stance/swing to model locomotion on vertical or overhanging surfaces.
    • Mechanosensory Feedback: Provides access to joint angles, actuator forces, contact forces, and user-defined anatomical joint-site positions.
  2. Overview of Browser (WebAssembly) apps

    main

    Flygym provides in-browser applications that run the NeuroMechFly model using MuJoCo compiled to WebAssembly and Three.js for rendering. These apps require no Python installation to run in a browser.

    The wasm/ directory contains two primary applications:

    1. Viewer: An interactive posing viewer (see viewer/README.md). It allows you to pose the fly using sliders for each leg actuator and toggle overlays for contact, force, joints, and actuators.
    2. Game: The NeuroMechFly Live slalom game (see game/README.md). It allows you to pilot the fly through a slalom track at three neural abstraction levels: CPG, tripod gait, and individual legs.
  3. Overview of the Browser (WebAssembly) interactive viewer

    main
    The Browser (WebAssembly) interactive viewer is a self-contained, in-browser version of the scripts/launch_interactive_viewer.py tool. It uses MuJoCo compiled to WebAssembly and Three.js for rendering to simulate the NeuroMechFly model directly in a web browser. It is used for live simulation, manual control via sliders, and visual inspection of the fly model.
  4. Get started with FlyGym

    main

    FlyGym is a framework for simulating embodied sensorimotor control using NeuroMechFly v2. To begin using the library, you can follow these primary paths:

    1. Installation: Set up the environment and dependencies.
    2. API Migration: If you are moving from FlyGym v1 to v2, refer to the migration guide.
    3. Tutorials: Follow structured learning paths including:
      • Model Composition: Learning how to compose models and scenes (Basic and Advanced).
      • Experimental Replay: Replaying experimental recordings or using the FlyBody model.
      • Simulation: Running GPU-accelerated simulations.
      • Locomotion Controllers: Implementing CPG, rule-based, hybrid, or turning controllers.
      • Machine Learning: Implementing muscle-based imitation learning.
      • Optimization: Performance profiling.
    4. API Reference: Deep dive into specific modules like anatomy, compose (fly, physics, pose, world), rendering, simulation, and warp.
  5. Choosing between the current API and the legacy flygym-gymnasium API

    main

    FlyGym provides two different ways to interact with NeuroMechFly simulations depending on your goals:

    1. Current API (Recommended for scientific studies): This is the modern API documented on the official site. It is optimized for computational efficiency and is the standard for research and scientific studies.
    2. Legacy flygym-gymnasium API (Recommended for learning): This API is more user-friendly and offers a guided interface. It was used for the 13 detailed tutorials accompanying the NeuroMechFly v2 paper. While excellent for learning about neuromechanical simulation in general, it is less computationally efficient than the current API.

    For detailed guidance on moving from the legacy interface to the modern one, refer to the migration guide.

  6. Understand the viewer file layout

    main

    The viewer's structure consists of the following components:

    • viewer.html: Contains the control panel, stage markup, and styles.
    • viewer.js: Handles the MuJoCo-WASM simulation loop, Three.js rendering, and user controls.
    • assets/ (Generated):
      • model/fly.xml: A flattened, self-contained MJCF file including *.stl meshes.
      • model_meta.json: Contains metadata such as timestep, neutral keyframe, per-actuator slider metadata, and colors.
  7. Understand the three levels of motor control in NeuroMechFly

    main

    The game demonstrates three distinct levels of abstraction for controlling animal behavior, ranging from high-level decisions to low-level limb management:

    1. Level 1: High-level control (Central Pattern Generators - CPG)

      • Concept: Uses CPGs, which are neural oscillators that produce rhythmic movement patterns (like walking) without needing rhythmic input.
      • Control: You decide on direction (forward, backward, left, right) using a joystick or keyboard. The CPG circuits handle the complex motor coordination automatically.
    2. Level 2: Medium-level control (Fixed gait pattern)

      • Concept: Focuses on specific movement patterns, such as the "tripod gait" used by insects. In a tripod gait, three legs move together to form a stable base while the other three move.
      • Control: You control groups of three legs to move forward or backward. This requires more precision than Level 1.
    3. Level 3: Low-level control (Individual legs)

      • Concept: Mimics the conscious management of limb placement (similar to a climber on a cliff). Instead of controlling muscles directly, you control individual legs.
      • Control: You move one leg at a time using a joystick or keyboard. This is the most challenging level of control.
  8. Understand the differences between FlyGym v1 and v2

    main

    FlyGym has transitioned from a Gymnasium-compliant interface (v1.x.x) to a custom, high-performance interface (v2.x.x).

    FlyGym 1.x.x (via flygym-gymnasium)

    • Interface: Strictly follows the Gymnasium API.
    • Use Case: Best for users who need immediate compatibility with existing RL libraries and tutorials built for the Gymnasium standard.

    FlyGym 2.x.x (via flygym)

    • Interface: Custom API designed for direct and flexible interaction with the simulation.
    • Performance: Achieves approximately a 10× speed-up on CPU-based simulations compared to v1.x.x.
    • Key Feature: Supports lazy-loading of simulated states rather than exposing all pre-configured states as a single "observation" block.
    • Mathematical Model: Despite the interface change, it remains fully compatible with Partially Observable Markov Decision Process (POMDP) principles.

    Note for v2 users: If you need Gymnasium compatibility while using the faster v2 engine, it is recommended to write a lightweight wrapper around the v2 API rather than using an official compatibility layer.

  9. Understand the FlyMimic musculoskeletal model

    main

    The FlyMimic model is an experimental musculoskeletal body model where standard joint actuators are replaced by 15 Hill-type muscles on the left-front (LF) leg.

    Key differences from the default FlyGym model:

    • Actuation: Uses 15 Hill-type muscles via spatial tendons instead of joint position/torque actuators.
    • Leg Structure: The LF leg is split into LFCoxa, LFTrochanter, LFFemur, LFTibia, and LFTarsus1..5.
    • Passive Joints: Uses stiffness and per-joint spring reference angles.
    • Other Legs: Only the LF leg is muscle-driven; the RF leg is locked to 0, and LM/LH legs are passive.
    • Base: The thorax is tethered (anchored to the world) rather than free-floating.

    Warning: This model is experimental. The API may change, and features like per-leg ground-contact sensors are currently absent.

  10. Understand the WebAssembly game architecture and layout

    main

    The game architecture consists of the following components:

    • Physics Engine: MuJoCo (WebAssembly) running at dt=1e-4. It runs at a fixed playback speed (approx. 0.1x real-time) controlled by PLAYBACK_SPEED in game.js.
    • Controller (game.js): A JavaScript port of the Python control logic. It uses a Controller class to manage CPG networks (Level 1) and per-leg/tripod state machines (Levels 2-3). It uses baked tables from assets/model_meta.json to avoid needing SciPy in the browser.
    • Rendering: Three.js for 3D visualization.
    • File Structure:
      • game.html: Contains the stage, HUD (level, timer, controls), and overlay screens.
      • game.js: The core loop containing MuJoCo-WASM, ported controllers, and Three.js rendering.
      • assets/model/fly.xml: The flattened slalom-arena MJCF model.
      • assets/model_meta.json: Contains timestep, neutral keyframes, actuator/adhesion data, CPG parameters, and baked trajectory tables.
  11. Getting started with FlyGym tutorials

    main
    If you are new to FlyGym, you should begin with the core tutorials located in the "Tutorials" section of the navigation bar. These provide the foundational knowledge required to use the current NeuroMechFly v2 API for embodied sensorimotor control simulations.
  12. Play the NeuroMechFly browser game

    main

    The NeuroMechFly game is a web-based demonstration of embodied sensorimotor control. It runs the NeuroMechFly model with MuJoCo compiled to WebAssembly directly in your browser. You can play it within an iframe or in full-screen mode.

    <!-- The game is hosted at the following path relative to the documentation root -->
    <iframe src="../wasm/game/game.html" ...></iframe>