Skip to content

Commit

Permalink
[MAINT] add tests (#17)
Browse files Browse the repository at this point in the history
* set up test notebooks

* add tests and coverage

* change path
  • Loading branch information
Remi-Gau authored Feb 17, 2024
1 parent 4cdfb2e commit 270a150
Show file tree
Hide file tree
Showing 24 changed files with 694 additions and 17,772 deletions.
11 changes: 1 addition & 10 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
[flake8]
; exclude =
; .git,
; __pycache__,
; build,
; dist,
; env,
; venv,
; doc/_build
--select = A,B,C,D,E,F,W,C90,FS
; docstring-convention = numpy
max-line-length = 100
max-line-length = 90
# For PEP8 error codes see
# http://pep8.readthedocs.org/en/latest/intro.html#error-codes
; per-file-ignores =
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run_examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
strategy:
fail-fast: false
matrix:
example: [comparedesigns.py, comparison_neurodesign.py, JSS_example.py, optimisation.py]
example: [compare_designs.py, JSS_example.py, optimisation.py]
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
31 changes: 31 additions & 0 deletions .github/workflows/test_notebook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
name: test notebook

on:
push:
branches:
- master
pull_request:
branches:
- '*'

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
test_notebook:
name: run notebooks
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup python
uses: actions/setup-python@v5
with:
python-version: '3.12'
allow-prereleases: false
- name: Install tox
run: python -m pip install --upgrade tox
- name: Run tests
run: tox run -e test_notebook
41 changes: 41 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
name: tests

on:
push:
branches:
- master
pull_request:
branches:
- '*'

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
tests:
name: Test with ${{ matrix.py }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
py: ['3.12', '3.11', '3.10', '3.9', '3.8']
os: [ubuntu-latest, macos-latest, windows-latest]
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup python
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.py }}
allow-prereleases: false
- name: Install tox
run: python -m pip install --upgrade tox
- name: Run tests
run: tox run -e tests
- name: Upload coverage to CodeCov
uses: codecov/codecov-action@v4
with:
flags: ${{ matrix.os }}_${{ matrix.py }}
if: success()
3 changes: 2 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ repos:
rev: 24.1.1
hooks:
- id: black
additional_dependencies: ['.[jupyter]']
args: [--config, pyproject.toml]

- repo: https://github.com/adamchainz/blacken-docs
Expand Down Expand Up @@ -65,4 +66,4 @@ repos:
hooks:
- id: flake8
args: [--config, .flake8, --verbose, neurodesign, examples]
additional_dependencies: [flake8-docstrings, flake8-use-fstring]
additional_dependencies: [flake8-docstrings, flake8-use-fstring, flake8-nb]
63 changes: 37 additions & 26 deletions examples/JSS_example.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import os
import os.path as op
from collections import Counter
from pathlib import Path

import matplotlib.pyplot as plt

import neurodesign

EXP = neurodesign.experiment(
output_dir = Path(__file__).parent / "output"
output_dir.mkdir(parents=True, exist_ok=True)

exp = neurodesign.experiment(
TR=1.2,
n_trials=20,
P=[0.3, 0.3, 0.4],
Expand All @@ -19,40 +21,44 @@
ITImax=4,
)

DES1 = neurodesign.design(
order=[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1], ITI=[2] * 20, experiment=EXP
design_1 = neurodesign.design(
order=[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1],
ITI=[2] * 20,
experiment=exp,
)

DES1.designmatrix()

DES1.FCalc(weights=[0.25, 0.25, 0.25, 0.25])
design_1.designmatrix()

design_1.FCalc(weights=[0.25, 0.25, 0.25, 0.25])

plt.plot(DES1.Xconv)

out_dir = "output"
if not op.isdir(out_dir):
os.makedirs(out_dir)
plt.plot(design_1.Xconv)

plt.savefig(op.join(out_dir, "example_figure_1.pdf"), format="pdf")
plt.savefig(output_dir / "example_figure_1.pdf", format="pdf")

DES2 = neurodesign.design(
order=[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1], ITI=[2] * 20, experiment=EXP
design_2 = neurodesign.design(
order=[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1],
ITI=[2] * 20,
experiment=exp,
)
DES2.designmatrix()
DES2.FCalc(weights=[0.25, 0.25, 0.25, 0.25])
print("Ff of Design 1: " + str(DES1.Ff))
print("Ff of Design 2: " + str(DES2.Ff))
print("Fd of Design 1: " + str(DES1.Fd))
print("Fd of Design 2: " + str(DES2.Fd))
design_2.designmatrix()
design_2.FCalc(weights=[0.25, 0.25, 0.25, 0.25])
print(f"Ff of Design 1: {str(design_1.Ff)}")
print(f"Ff of Design 2: {str(design_2.Ff)}")
print(f"Fd of Design 1: {str(design_1.Fd)}")
print(f"Fd of Design 2: {str(design_2.Fd)}")

DES3, DES4 = DES1.crossover(DES2, seed=2000)
print(DES3.order)
print(DES4.order)
design_3, design_4 = design_1.crossover(design_2, seed=2000)
print(design_3.order)
print(design_4.order)


order = neurodesign.generate.order(
nstim=4, ntrials=100, probabilities=[0.25, 0.25, 0.25, 0.25], ordertype="random", seed=1234
nstim=4,
ntrials=100,
probabilities=[0.25, 0.25, 0.25, 0.25],
ordertype="random",
seed=1234,
)
print(order[:10])
Counter(order)
Expand All @@ -70,6 +76,11 @@
)

POP = neurodesign.optimisation(
experiment=EXP, weights=[0, 0.5, 0.25, 0.25], preruncycles=10, cycles=100, folder="./", seed=100
experiment=exp,
weights=[0, 0.5, 0.25, 0.25],
preruncycles=10,
cycles=100,
folder="./",
seed=100,
)
POP.optimise()
87 changes: 87 additions & 0 deletions examples/compare_designs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import neurodesign
from neurodesign import generate

# define experimental setup

exp = neurodesign.experiment(
TR=2,
n_trials=20,
P=[0.3, 0.3, 0.4],
C=[[1, -1, 0], [0, 1, -1]],
n_stimuli=3,
rho=0.3,
stim_duration=1,
t_pre=0.5,
t_post=2,
ITImodel="exponential",
ITImin=2,
ITImax=4,
ITImean=2.1,
)

# define first design with a fixed ITI

design_1 = neurodesign.design(
order=[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1],
ITI=[2] * 20,
experiment=exp,
)

# expand to design matrix

design_1.designmatrix()
design_1.FCalc(weights=[0, 0.5, 0.25, 0.25])
design_1.FdCalc()
design_1.FcCalc()
design_1.FfCalc()
design_1.FeCalc()

# define second design

design_2 = neurodesign.design(
order=[0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2, 0, 1],
ITI=generate.iti(20, "exponential", min=1, mean=2, max=4, seed=1234)[0],
experiment=exp,
)

design_2.designmatrix()
design_2.FeCalc()
design_2.FdCalc()
design_2.FcCalc()
design_2.FfCalc()

# crossover to obtain design 3 and 4

design_3, design_4 = design_1.crossover(design_2, seed=2000)
design_3.order
design_4.order
design_3.designmatrix()
design_3.FeCalc()
design_3.FdCalc()
design_3.FcCalc()
design_3.FfCalc()
design_4.designmatrix()
design_4.FeCalc()
design_4.FdCalc()
design_4.FcCalc()
design_4.FfCalc()

# mutate design
DES5 = design_1.mutation(0.3, seed=2000)
DES5.designmatrix()
DES5.FeCalc()
DES5.FdCalc()
DES5.FcCalc()
DES5.FfCalc()

# compare detection power
result = (
f" RESULTS \n ======= \n"
f"DESIGN 1: Fd = {design_1.Fd} \n"
f"DESIGN 2: Fd = {design_2.Fd} \n"
f"DESIGN 3: Fd = {design_3.Fd} \n"
f"DESIGN 4: Fd = {design_4.Fd} \n"
f"DESIGN 5: Fd = {DES5.Fd} \n"
)

print(result)
86 changes: 0 additions & 86 deletions examples/comparedesigns.py

This file was deleted.

Loading

0 comments on commit 270a150

Please sign in to comment.