# cortex-lab/phylib architecture

> Architecture diagram of the GitHub repository cortex-lab/phylib, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cortex-lab/phylib
- Repository: https://github.com/cortex-lab/phylib

## Overview

##### Project type and purpose
- phylib is a domain‑specific Python library (not a full‑stack app) for electrophysiological data analysis.
- It provides core building blocks for higher‑level tools (phy, ibllib), offering I/O, electrode definitions, statistical routines, and utilities.

##### File structure
- Top‑level:
- setup.py, requirements*.txt, environment.yml → packaging and dependencies
- Makefile, .travis.yml → CI/build scripts
- phylib/ → core code; subdivided into four major packages + tests alongside each
- phylib/electrode → electrode models, probe definitions (e.g. 1x32_buzsaki.prb), measurement arrays (mea.py)
- phylib/io → reading and merging data from different formats (ALF, array, datasets, traces), mock data generators
- phylib/stats → statistical analyses (cross‑correlograms, clustering)
- phylib/utils → miscellaneous helpers (geometry, event handling, type definitions)
- Each package has a tests/ subpackage verifying its functionality.

##### README for additional insights
- Emphasizes that phylib is a supporting library for spike‑sorting visualization (phy) and behavioural‑neuro pipeline (ibllib).

##### Final advice
- Be detailed: break each phylib subpackage into its responsibilities.
- Both the static structure (packages, files) and dynamic flow (data paths, API calls).
- Don’t overthink: representing each module and its major interactions is sufficient to give stakeholders a clear view of phylib’s architecture.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Clients
    subgraph "External Clients"
        PHY["phy"]:::external
        IBL["ibllib"]:::external
    end

    %% External Libraries
    subgraph "External Libraries"
        NP["NumPy"]:::external
        SP["SciPy"]:::external
        H5["h5py"]:::external
    end

    %% Core Modules
    subgraph "phylib Core Modules"
        IO["phylib.io"]:::core
        EEL["phylib.electrode"]:::core
        STATS["phylib.stats"]:::core
        UT["phylib.utils"]:::core

        %% Tests
        subgraph "Tests" 
            TIO["io tests"]:::support
            TE["electrode tests"]:::support
            TS["stats tests"]:::support
            TU["utils tests"]:::support
        end
    end

    %% Data Stores
    Data["In-memory Data"]:::data
    Output["CCG plots, cluster labels"]:::data

    %% Packaging & CI
    subgraph "Packaging & CI"
        SetupPy["setup.py"]:::support
        SetupCfg["setup.cfg"]:::support
        Req["requirements.txt / requirements-dev.txt"]:::support
        Env["environment.yml"]:::support
        Make["Makefile"]:::support
        Travis[".travis.yml"]:::support
    end

    %% Relationships
    IO -->|"produces data"| Data
    EEL -->|"defines probe geometries"| IO
    UT -->|"shared helpers"| IO
    UT -->|"shared helpers"| STATS
    EEL -->|"enriches data"| STATS
    IO -->|"inputs for analysis"| STATS
    STATS -->|"analysis outputs"| Output

    PHY -->|imports| IO
    PHY -->|imports| EEL
    PHY -->|imports| STATS
    PHY -->|imports| UT
    IBL -->|imports| IO
    IBL -->|imports| EEL
    IBL -->|imports| STATS
    IBL -->|imports| UT

    NP -->|dependency| IO
    SP -->|dependency| IO
    H5 -->|dependency| IO
    NP -->|dependency| STATS
    SP -->|dependency| STATS

    SetupPy -->|manages packaging| IO
    SetupPy -->|manages packaging| EEL
    SetupPy -->|manages packaging| STATS
    SetupPy -->|manages packaging| UT
    Make -->|builds/tests| IO
    Travis -->|CI runs| IO

    %% Click Events
    click EEL "https://github.com/cortex-lab/phylib/tree/master/phylib/electrode/"
    click IO "https://github.com/cortex-lab/phylib/tree/master/phylib/io/"
    click STATS "https://github.com/cortex-lab/phylib/tree/master/phylib/stats/"
    click UT "https://github.com/cortex-lab/phylib/tree/master/phylib/utils/"
    click SetupPy "https://github.com/cortex-lab/phylib/blob/master/setup.py"
    click SetupCfg "https://github.com/cortex-lab/phylib/blob/master/setup.cfg"
    click Req "https://github.com/cortex-lab/phylib/blob/master/requirements.txt"
    click Env "https://github.com/cortex-lab/phylib/blob/master/environment.yml"
    click Make "https://github.com/cortex-lab/phylib/tree/master/Makefile"
    click Travis "https://github.com/cortex-lab/phylib/blob/master/.travis.yml"

    %% Styles
    classDef core fill:#e0f7fa,stroke:#006064
    classDef external fill:#f1f8e9,stroke:#33691e
    classDef support fill:#fff8e1,stroke:#ff6f00
    classDef data fill:#e8eaf6,stroke:#283593
```
