Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ share/python-wheels/
.installed.cfg
*.egg
MANIFEST
*.DS_STORE

# PyInstaller
# Usually these files are written by a python script from a template
Expand Down Expand Up @@ -69,7 +70,9 @@ instance/
.scrapy

# Sphinx documentation
docs/_build/
doc/_build/
doc/auto_examples/
doc/generated/

# PyBuilder
target/
Expand Down Expand Up @@ -117,13 +120,13 @@ venv.bak/
# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# examples
examples/*_data/*
4 changes: 2 additions & 2 deletions doc/API/datatypes.rst
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Data Types
===================================

.. autoclass:: whobpyt.datatypes.Recording
.. autoclass:: whobpyt.datatypes.Timeseries
:members:
:undoc-members:
:special-members: __init__

.. autoclass:: whobpyt.datatypes.par
.. autoclass:: whobpyt.datatypes.Parameter
:members:
:undoc-members:
:special-members: __init__
Expand Down
8 changes: 4 additions & 4 deletions doc/API/models.rst
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Models
===================================

.. autoclass:: whobpyt.models.JansenRit.RNNJANSEN
:members: info, createIC, createDelayIC, setModelParameters, forward
.. autoclass:: whobpyt.models.jansen_rit.JansenRitModel
:members: info, createIC, createDelayIC, setModelSCParameters, forward
:undoc-members:
:special-members: __init__

.. autoclass:: whobpyt.models.RWW.RNNRWW
:members: info, createIC, setModelParameters, forward
.. autoclass:: whobpyt.models.wong_wang.ReducedWongWangModel
:members: info, createIC, createDelayIC, setModelSCParameters, forward
:undoc-members:
:special-members: __init__

Expand Down
6 changes: 3 additions & 3 deletions doc/API/run.rst
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
Run
===================================

.. autoclass:: whobpyt.run.Model_fitting
.. autoclass:: whobpyt.run.ModelFitting
:members: train, evaluate, save
:undoc-members:
:special-members: __init__

.. autoclass:: whobpyt.run.Fitting_Batch
.. autoclass:: whobpyt.run.FittingBatch
:members: train
:undoc-members:
:special-members: __init__

.. autoclass:: whobpyt.run.Fitting_FNGFPG
.. autoclass:: whobpyt.run.FittingFNGFPG
:members: train
:undoc-members:
:special-members: __init__
2 changes: 0 additions & 2 deletions doc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
#SPHINXPROJ = fooof
SPHINXPROJ = whobpyt
SOURCEDIR = .
BUILDDIR = _build
Expand Down Expand Up @@ -50,7 +49,6 @@ install:
# --no-checkout just fetches the root folder without content
# --depth 1 is a speed optimization since we don't need the history prior to the last commit
# -b gh-pages fetches only the branch for the gh-pages
#git clone -b gh-pages --single-branch --no-checkout --depth 1 https://github.com/fooof-tools/fooof _build/gh_pages
git clone -b gh-pages --single-branch --no-checkout --depth 1 git@github.com:griffithslab/whobpyt _build/gh_pages
# A .nojekyll file tells Github pages to bypass Jekyll processing
touch _build/gh_pages/.nojekyll
Expand Down
22 changes: 8 additions & 14 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,18 @@ def is_development_build():
}

html_theme = 'sphinx_rtd_theme'
#html_theme_options = {
# 'logo_only': False,#True,
# 'navigation_depth': 5,
#}
html_context = {}

if not 'READTHEDOCS' in os.environ:
html_static_path = ['_static/']
html_js_files = ['debug.js']
# if not 'READTHEDOCS' in os.environ:
# html_static_path = ['_static/']
# html_js_files = ['debug.js']

# Add fake versions for local QA of the menu
html_context['test_versions'] = list(map(
lambda x: str(x / 10),
range(1, 100)
))
# # Add fake versions for local QA of the menu
# html_context['test_versions'] = list(map(
# lambda x: str(x / 10),
# range(1, 100)
# ))

#html_logo = "demo/static/logo-wordmark-light.svg"
#html_logo = "img/eeg-notebooks_logo.png"
html_show_sourcelink = True

htmlhelp_basename = slug
Expand Down
13 changes: 7 additions & 6 deletions doc/getting_started/running_in_codespaces.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ Running whobpyt in GitHub Codespaces

GitHub Codespaces is a new feature that allows you to run a full VS Code environment in the cloud and access it from your browser.

We have added pre-made configurations for you to use `whobpyt` in GitHub Codespaces, which takes care of cloning, installing python, setting up the environment, and installing the pertinent packages for you.
We have added pre-made configurations for you to use ``whobpyt`` in GitHub Codespaces, which takes care of cloning, installing python, setting up the environment, and installing the pertinent packages for you.

Steps to use ``whobpyt`` in GitHub Codespaces:

Steps to use `whobpyt` in GitHub Codespaces:
1. Go to [the GitHub page for the repo](https://github.com/griffithslab/whobpyt) (or any specific branch you want to explore).
2. Click on the green "Code" button on the top right and select the second tab "Codespaces".
3. In front of the first option "Codespaces", click on the three-dot button.
4. Select "New with options".
5. In the new page, select your desired branch. From the second field, choose either of the three configurations we have provided:
a. `WhoBPyT-CPU` for a CPU-only environment.
b. `WhoBPyT-GPU` for a GPU-enabled environment.
c. `WhoBPyT-docs` for a CPU-only environment that also builds the `html` documentation (useful for exploring and debugging the documentation)
a. ``WhoBPyT-CPU`` for a CPU-only environment.
b. ``WhoBPyT-GPU`` for a GPU-enabled environment.
c. ``WhoBPyT-docs`` for a CPU-only environment that also builds the ``html`` documentation (useful for exploring and debugging the documentation)
6. Choose your desired region and machine type.
7. Click on "Create codespace" and wait for the environment to be created. This should take 1-2 minutes.
8. Once the environment is created, the python environment will install the necessary packages. This should take 4-5 minutes.
9. Your configured codespace should be ready. Now you get a full VS Code environment in your browser. You can now explore the code, run the examples, and even edit the code and commit your changes back to the repo.

Now you should be good to continue with the rest of the example code in `.py` files, `.ipynb` notebooks, and experiment with new ideas.
Now you should be good to continue with the rest of the example code in ``.py`` files, ``.ipynb`` notebooks, and experiment with new ideas.


**Notes**:
Expand Down
2 changes: 2 additions & 0 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ Welcome to WhoBPyT documentation!
:glob:

auto_examples/eg__replicate_Momi2023
auto_examples/eg__RWW_example
auto_examples/eg__multimodal_simulation

6 changes: 0 additions & 6 deletions doc/usage.rst

This file was deleted.

177 changes: 177 additions & 0 deletions examples/eg__RWW_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
"""

==================================
Fitting Wong_Wang model (HCP data)
==================================

"""

# sphinx_gallery_thumbnail_number = 1

# %%
# Importage
# ---------
#

# os stuff
import os
import sys
sys.path.append('..')

# whobpyt stuff
import whobpyt
from whobpyt.datatypes import Parameter, Timeseries
from whobpyt.models.wong_wang import ReducedWongWangModel, ReducedWongWangParams
from whobpyt.optimization.custom_cost_RWW import CostsRWW
from whobpyt.run import ModelFitting
from whobpyt.datasets import fetch_hcpl2k8

# array and pd stuff
import numpy as np
import pandas as pd

# viz stuff
import matplotlib.pyplot as plt

# gdown
import gdown

# %%
# define destination path and download the data


"""
des_dir = '../'
if not os.path.exists(des_dir):
os.makedirs(des_dir) # create folder if it does not exist
url = "https://drive.google.com/drive/folders/18smy3ElTd4VksoL4Z15dhwT5l3yjk6xS"
os.chdir(des_dir)
gdown.download_folder(url, remaining_ok = True)
"""

# Go to examples folder
#os.chdir('examples')


# Download the data for this example to default location ~/.whobpyt/data
data_dir = fetch_hcpl2k8()

base_dir = os.path.join(data_dir, 'HCP')

# %%
#get subject list
#base_dir = '../HCP/'

#subs =sorted([sc_file[-10:-4] for sc_file in os.listdir(base_dir) if sc_file[:8] == 'weights_'])

sub = '100307'

# %%
# define options for wong-wang model
node_size = 83
mask = np.tril_indices(node_size, -1)
num_epochs = 5
TPperWindow = 20
step_size = 0.05
input_size = 2
tr = 0.75
repeat_size = 5

# %%
# load raw data and get SC empirical BOLD and FC
sc_file = os.path.join(base_dir, 'weights_' + sub + '.txt')
ts_file = os.path.join(base_dir, sub + '_rfMRI_REST1_LR_hpc200_clean__l2k8_sc33_ts.pkl') # out_dir+'sub_'+sub+'simBOLD_idt.txt'

sc = np.loadtxt(sc_file)
SC = (sc + sc.T) * 0.5
sc = np.log1p(SC) / np.linalg.norm(np.log1p(SC))

ts_pd = pd.read_pickle(ts_file)
ts = ts_pd.values
ts = ts / np.max(ts)
fc_emp = np.corrcoef(ts.T)

# %%
# prepare data structure for model
print(ts.T.shape)
fMRIstep = tr
data_mean = Timeseries(ts.T, fMRIstep)

# %%
# get model parameters structure and define the fitted parameters by setting non-zero variance for the model
params = ReducedWongWangParams(g=Parameter(400, 400, 1/np.sqrt(10), True), g_EE=Parameter(1.5, 1.5, 1/np.sqrt(50), True), \
g_EI =Parameter(0.8, 0.8, 1/np.sqrt(50), True), \
g_IE=Parameter(0.6, 0.6, 1/np.sqrt(50), True), I_0 =Parameter(0.2), \
std_in=Parameter(np.log(0.1), np.log(0.1), 0.1, True, True, lb =0.02))

# %%
# call model want to fit
model = ReducedWongWangModel(params, node_size =node_size, TRs_per_window =TPperWindow, step_size=step_size, tr=tr, sc=sc, use_fit_gains=True)

# %%
# create objective function
ObjFun = CostsRWW(model)

# %%
# call model fit
F = ModelFitting(model, ObjFun)


# %%
# Model Training
# ---------------------------------------------------
#
F.train(u = 0, empRecs = [data_mean], num_epochs = num_epochs, TPperWindow = TPperWindow, learningrate = 0.05)

# %%
# Plots of loss over Training
plt.plot(np.arange(1,len(F.trainingStats.loss)+1), F.trainingStats.loss)
plt.title("Total Loss over Training Epochs")

# %%
# Plots of parameters values over Training
plt.plot(F.trainingStats.fit_params['g_EE'], label = "g_EE")
plt.plot(F.trainingStats.fit_params['g_EI'], label = "g_EI")
plt.plot(F.trainingStats.fit_params['g_IE'], label = "g_IE")
plt.legend()
plt.title("Local Coupling Variables Changing Over Training Epochs")

# %%
# Model Evaluation (with 20 window for warmup)
# ---------------------------------------------------
#
F.evaluate(u = 0, empRec = data_mean, TPperWindow = TPperWindow, base_window_num = 20)

# %%
# Plot SC and fitted SC

fig, ax = plt.subplots(1, 2, figsize=(5, 4))
im0 = ax[0].imshow(sc, cmap='bwr', vmin = 0.0, vmax = 0.05)
ax[0].set_title('The empirical SC')
fig.colorbar(im0, ax=ax[0], fraction=0.046, pad=0.04)
im1 = ax[1].imshow(F.model.sc_fitted.detach().numpy(), cmap='bwr', vmin = 0.0, vmax = 0.05)
ax[1].set_title('The fitted SC')
fig.colorbar(im1, ax=ax[1], fraction=0.046, pad=0.04)
plt.show()

# %%
# Plot E I and simulated BOLD
fig, ax = plt.subplots(1, 3, figsize=(12, 8))
ax[0].plot(F.lastRec['E'].npTS().T)
ax[0].set_title('Test: E')
ax[1].plot(F.lastRec['I'].npTS().T)
ax[1].set_title('Test: I')
ax[2].plot(F.lastRec['bold'].npTS().T)
ax[2].set_title('Test: BOLD')
plt.show()

# %%
# Plot the FC and the test FC
fig, ax = plt.subplots(1, 2, figsize=(5, 4))
im0 = ax[0].imshow(fc_emp, cmap='bwr')
ax[0].set_title('The empirical FC')
fig.colorbar(im0, ax=ax[0], fraction=0.046, pad=0.04)
im1 = ax[1].imshow(np.corrcoef(F.lastRec['bold'].npTS()), cmap='bwr')
ax[1].set_title('The simulated FC')
fig.colorbar(im1, ax=ax[1], fraction=0.046, pad=0.04)
plt.show()
Loading