# Ichiyama 2025 - Neural Network Modeling Code

This repository contains code for neural network modeling and analysis used in Ichiyama et al. (2025). 

## Repository Structure

```
ichiyama_2025_code/
│
├── README.md                          # This file
│
├── Core Modules
│   ├── network_CADEX.py              # Main network model implementation using Brian2
│   ├── connection_matrix_gen.py      # Connection matrix generation and clustering un-used but needed for imports
│   ├── utils.py                      # General utility functions for analysis
│   └── spike_train_utils.py          # Spike train analysis utilities #deprecated
│
├── scan_code/                        # Network parameter fitting and optimization
│   ├── network_fitting.py            # Core fitting functions and error metrics
│   ├── 1_network_fitting_all_fast.py # Fast parameter fitting script
│   ├── 1_network_fitting_FF.py       # Feed-forward network fitting
│   ├── 1_network_fitting_full_space.py # Full parameter space exploration
│   ├── 1_network_fitting_slow_gaba.py # Slow GABA dynamics fitting
│   ├── 2_scan_results_parse_V2.py    # Parse optimization results
│   ├── 3_scan_results_concat.ipynb   # Concatenate results from multiple runs
│   ├── 4_selecting_a_net_fit.ipynb   # Network fit selection and comparison
│   ├── 5_net_select_v2.ipynb         # part 2 of the network fit selection
|   └── 6_network_scan_fit_rescan.py  # Rescans the best network fits for the best way to flip the network
│
└── fig_code/                         # Figure generation notebooks
    ├── ICHIYAMA_PAPER2_COMP_DATA_FIGS.ipynb           # Main figures
    ├── ICHIYAMA_PAPER2_COMP_DATA_FIGS.html            # HTML export of main figures
    ├── ICHIYAMA_PAPER2_COMP_DATA_FIGS_for_stats.ipynb # Statistical analysis figures
    ├── ICHIYAMA_PAPER2_COMP_DATA_FIGS_net_select.ipynb # Network selection figures
    └── ICHIYAMA_PAPER2_COMP_DATA_FIGS_opto.ipynb      # Optogenetics simulation figures
```

## Installation

### Prerequisites

- Python 3.8 or higher
- Anaconda or Miniconda (recommended for environment management)

### Setup

1. Clone or download this repository:
```bash
git clone https://github.com/smestern/ichiyama_2025_code
cd ichiyama_2025_code
```

2. Create a new conda environment (recommended):
```bash
conda create -n ichiyama_env python=3.9 
conda activate ichiyama_env
```

3. Install required dependencies (if env file not used):
```bash
pip install brian2 numpy scipy pandas matplotlib
pip install networkx scikit-learn joblib
pip install nevergrad torch sbi
pip install ipfx elephant neo quantities pyabf
pip install jupyter notebook
pip install POT  # for optimal transport (ot)
```

4. Install sNMO (Spike train Neuromodeling Optimizer):
This unfortunately is a custom package that I have not added to pypi yet. You will have to pull from github directly to install

```bash
pip install git+https://github.com/smestern/sNMO
```

## Usage

### Parameter Fitting

Parameter fitting should be run in a specific order:

1. The first step is to generate the scans across the parameters. This code is located within the `scan_code` folder. The scans for each network configurations (and for the full space) are found in each of the individual files labelled `1_network_fitting_[scan_type]`. These scans run in parallel accross using the ScrHammersly (a pseudo random technique). All 4 motifs should be scanned prior to proceeding.

2. Then run the `2_scan_results_parse_V2.py` script across the scan results. This code computes additional error metrics for later use. This code will output a `concat_res.csv`

3. The `concat_res.csv` is then loaded into `3_scan_results_concat.ipynb`. Here we compute the overall error metrics. Then we clean and scale the data. This script generates a `concat_res_all_clean.csv` file.

4. The `concat_res_all_clean.csv` is loaded into `4_selecting_a_net_fit.ipynb` file. This file runs some final metrics generating the file `. It also plots the parameter space. 

5. Finally `5_net_select_v2.ipynb` is run to select the top 10 fits for each network motif. This script saves the paths of the top networks.

6. Then `6_network_scan_fit_rescan.py` is used to rescan the top 10 parameters from each network motif. This rescan focuses on selecting the best parameters for flipping the network.

### Generating Figures

For generating figures, the script [1_ICHIYAMA_PAPER2_COMP_DATA_FIGS_net_select](fig_code/1_ICHIYAMA_PAPER2_COMP_DATA_FIGS_net_select.ipynb) should be run first! This script allows you to select your final parameter set for you with other experiments.

Once you have made your final selection, you can run the following scripts:
[ICHIYAMA_PAPER2_COMP_DATA_FIGS.ipynb](fig_code\ICHIYAMA_PAPER2_COMP_DATA_FIGS.ipynb) - Generates the baseline runs showing examples of the clean bursting state.  
[ICHIYAMA_PAPER2_COMP_DATA_FIGS_for_stats.ipynb](fig_code\ICHIYAMA_PAPER2_COMP_DATA_FIGS_for_stats.ipynb) - Generates the runs for computing stats. Includes a network flip to simulate the natural switching we see in in vivo baselines  
[ICHIYAMA_PAPER2_COMP_DATA_FIGS_opto.ipynb](fig_code\ICHIYAMA_PAPER2_COMP_DATA_FIGS_opto.ipynb) - Simulates the opto-pulse experiment while the networks remain in bursting state

## Data

Due to sheer size, data for fitting is avalible upon request.

The best parameter sets are included with the repo.

## Citation

[Citation information to be added]

## Contact

smestern@uwo.ca
