Skip to content

Commit

Permalink
use rich instead of preogressbar
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi-Gau committed Feb 19, 2024
1 parent d05e906 commit ae24d9d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 21 deletions.
1 change: 1 addition & 0 deletions examples/JSS_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path

import matplotlib.pyplot as plt
from rich import print

import neurodesign

Expand Down
2 changes: 2 additions & 0 deletions examples/compare_designs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from rich import print

import neurodesign
from neurodesign import generate

Expand Down
1 change: 1 addition & 0 deletions examples/comparison_neurodesign.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"import numpy as np\n",
"import seaborn as sns\n",
"from scipy.stats import t\n",
"from rich import print\n",
"\n",
"from neurodesign import Experiment, Optimisation\n",
"\n",
Expand Down
74 changes: 57 additions & 17 deletions neurodesign/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,39 @@
from pathlib import Path

import numpy as np
import progressbar
import scipy
import scipy.linalg
import sklearn.cluster
from numpy import transpose as t
from rich import print
from rich.progress import (
BarColumn,
MofNCompleteColumn,
Progress,
SpinnerColumn,
TaskProgressColumn,
TextColumn,
TimeElapsedColumn,
TimeRemainingColumn,
)
from scipy.special import gamma

from neurodesign import generate, report


def progress_bar(text: str, color: str = "green") -> Progress:
"""Return a rich progress bar instance."""
return Progress(
TextColumn(f"[{color}]{text}"),
SpinnerColumn("dots"),
TimeElapsedColumn(),
BarColumn(),
MofNCompleteColumn(),
TaskProgressColumn(),
TimeRemainingColumn(),
)


class Design:
"""
This class represents an experimental design for an fMRI experiment.
Expand Down Expand Up @@ -982,41 +1005,58 @@ def optimise(self):
# add new designs
self.clear()
self.add_new_designs(weights=[1, 0, 0, 0])

# loop
bar = progressbar.ProgressBar()
for _ in bar(range(self.preruncycles)):
self.to_next_generation(seed=self.seed, weights=[1, 0, 0, 0])
if self.finished:
continue
with progress_bar(text="Optimizing") as progress:
task = progress.add_task(
description="optimize", total=len(range(self.preruncycles))
)
for _ in range(self.preruncycles):
self.to_next_generation(seed=self.seed, weights=[1, 0, 0, 0])
progress.update(task, advance=1)
if self.finished:
continue

self.exp.FeMax = np.max(self.bestdesign.F)

if self.exp.FdMax == 1 and self.weights[1] > 0:
self.clear()
self.add_new_designs(weights=[0, 1, 0, 0])

# loop
bar = progressbar.ProgressBar()
for _ in bar(range(self.preruncycles)):
self.to_next_generation(seed=self.seed, weights=[0, 1, 0, 0])
if self.finished:
continue
with progress_bar(text="Optimizing") as progress:
task = progress.add_task(
description="optimize", total=len(range(self.preruncycles))
)
for _ in range(self.preruncycles):
self.to_next_generation(seed=self.seed, weights=[0, 1, 0, 0])
progress.update(task, advance=1)
if self.finished:
continue

self.exp.FdMax = np.max(self.bestdesign.F)

# clear all attributes
self.clear()
self.add_new_designs()

# loop
bar = progressbar.ProgressBar()
for _ in bar(range(self.cycles)):
self.to_next_generation(seed=self.seed)
if self.finished:
continue
with progress_bar(text="Optimizing") as progress:
task = progress.add_task(
description="optimize", total=len(range(self.cycles))
)
for _ in range(self.cycles):
self.to_next_generation(seed=self.seed)
progress.update(task, advance=1)
if self.finished:
continue

return self

def evaluate(self):
# select designs: best from k-means clusters
shape = self.bestdesign.Xconv.shape
des = np.zeros([np.product(shape), len(self.designs)])
des = np.zeros([np.prod(shape), len(self.designs)])
efficiencies = np.array([x.F for x in self.designs])
for d in range(len(self.designs)):
hrf = []
Expand Down
1 change: 1 addition & 0 deletions neurodesign/msequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import math

import numpy as np
from rich import print


class Msequence:
Expand Down
4 changes: 1 addition & 3 deletions neurodesign/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
Table,
)

from neurodesign import Optimisation

plt.switch_backend("agg")


def make_report(population: Optimisation, outfile: str | Path = "NeuroDesign.pdf"):
def make_report(population, outfile: str | Path = "NeuroDesign.pdf"):
"""Create a report of a finished design optimisation."""
if not isinstance(population.cov, np.ndarray):
population.evaluate()
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ classifiers = [
]
dependencies = [
"scikit-learn>0.15.0",
"progressbar>2.0",
"rich",
"reportlab",
"pdfrw",
"matplotlib"
Expand Down

0 comments on commit ae24d9d

Please sign in to comment.