Skip to content
Merged
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
17 changes: 9 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ repos:
rev: v2.4.1
hooks:
- id: codespell
args: ["-L", "larg"]
exclude: >
(?x)^(
.*\.txt|
.*\.svg|
.*\.ipynb
)$

# Clang format
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v20.1.7
rev: v20.1.8
hooks:
- id: clang-format
types_or: [c++, c, cuda]
Expand All @@ -55,15 +55,16 @@ repos:

# Ruff, the Python auto-correcting linter/formatter written in Rust
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.12.2
rev: v0.12.3
hooks:
- id: ruff
args: ["--fix", "--show-fixes", "--exclude=__init__.py"]
args: ["--fix", "--show-fixes"]
- id: ruff-format

# Black format Python and notebooks
- repo: https://github.com/psf/black
rev: 25.1.0
# Format docstrings
- repo: https://github.com/DanielNoord/pydocstringformatter
rev: v0.7.3
hooks:
- id: black-jupyter
- id: pydocstringformatter
args: ["--style=numpydoc"]
...
79 changes: 79 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,82 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[tool.ruff]
indent-width = 4
line-length = 88
target-version = "py39"
extend-include = ["*.ipynb"]

lint.select = [
"ANN", # flake8-annotations
"ARG", # flake8-unused-arguments
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"C90", # mccabe
# "D", # pydocstyle
"DTZ", # flake8-datetimez
"E", # pycodestyle
"EM", # flake8-errmsg
"ERA", # eradicate
"F", # Pyflakes
"I", # isort
"ICN", # flake8-import-conventions
"N", # pep8-naming
"PD", # pandas-vet
"PGH", # pygrep-hooks
"PIE", # flake8-pie
"PL", # Pylint
"PLC", # Pylint
"PLE", # Pylint
"PLR", # Pylint
"PLW", # Pylint
"PT", # flake8-pytest-style
"Q", # flake8-quotes
"RET", # flake8-return
"RUF100", # Ruff-specific
"S", # flake8-bandit
"SIM", # flake8-simplify
# "T20", # flake8-print
"TID", # flake8-tidy-imports
"UP", # pyupgrade
"W", # pycodestyle
"YTT", # flake8-2020
]

exclude = [
"__init__.py",
"setup.py",
]

lint.ignore = [
"S301", # pickle
"S311", # Standard pseudo-random generators
]

[tool.ruff.lint.pep8-naming]
extend-ignore-names = [
"X", "X1", "X_train", "X_val", "X_test", "X_predict",
]

[tool.ruff.lint.pydocstyle]
convention = "numpy"

[tool.ruff.format]
docstring-code-format = true
docstring-code-line-length = 80

[tool.ruff.lint.extend-per-file-ignores]
"test/**" = ["S101", "PLR2004", "ANN"]
"python/example_rmux.py" = ["PLR2004"]
"python/example_maze.py" = ["PLR2004"]
"python/notebooks/example_cartpole.ipynb" = ["E501", "SIM115"]
"python/notebooks/example_maze.ipynb" = ["E501", "PLR2004"]
"python/notebooks/example_regression.ipynb" = ["PLR2004"]
"python/notebooks/example_rmux.ipynb" = ["PLR2004"]
"python/notebooks/example_tuning.ipynb" = ["E501", "ANN201"]

[tool.codespell]
ignore-words-list = [
"larg",
]
6 changes: 3 additions & 3 deletions python/example_cartpole.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@


def replay(replay_size: int = 5000) -> None:
"""Performs experience replay updates"""
"""Performs experience replay updates."""
batch_size: int = min(len(memory), replay_size)
batch = random.sample(memory, batch_size)
for state, action, reward, next_state, done in batch:
Expand All @@ -133,7 +133,7 @@ def replay(replay_size: int = 5000) -> None:


def egreedy_action(state: np.ndarray) -> int:
"""Selects an action using an epsilon greedy policy"""
"""Selects an action using an epsilon greedy policy."""
if np.random.rand() < epsilon:
return random.randrange(N_ACTIONS)
prediction_array = xcs.predict(state.reshape(1, -1))[0]
Expand All @@ -143,7 +143,7 @@ def egreedy_action(state: np.ndarray) -> int:


def episode() -> tuple[float, int]:
"""Executes a single episode, saving to memory buffer"""
"""Executes a single episode, saving to memory buffer."""
episode_score: float = 0
episode_steps: int = 0
state: np.ndarray = env.reset()[0]
Expand Down
2 changes: 1 addition & 1 deletion python/example_maze.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def run_experiment(env: Maze) -> None:
bar.close()


def plot_performance(env: Maze):
def plot_performance(env: Maze) -> None:
"""Plots learning performance."""
plt.figure(figsize=(10, 6))
plt.plot(trials, steps)
Expand Down
11 changes: 5 additions & 6 deletions python/example_rmux.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,12 @@ def execute(self, act: int) -> tuple[bool, float]:
reward += 0.1
else:
reward = self.max_payoff
elif self.payoff_map:
reward = (pos - self.pos_bits) * 0.2
if answer == 1:
reward += 0.1
else:
if self.payoff_map:
reward = (pos - self.pos_bits) * 0.2
if answer == 1:
reward += 0.1
else:
reward = 0
reward = 0
return correct, reward


Expand Down
12 changes: 6 additions & 6 deletions python/notebooks/example_cartpole.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
"import random\n",
"from collections import deque\n",
"\n",
"from matplotlib import rcParams\n",
"import matplotlib.pyplot as plt\n",
"import imageio\n",
"import gymnasium as gym\n",
"import imageio\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from IPython.display import display, Image\n",
"from IPython.display import Image, display\n",
"from matplotlib import rcParams\n",
"from tqdm import tqdm\n",
"\n",
"import xcsf\n",
Expand Down Expand Up @@ -625,12 +625,12 @@
}
],
"source": [
"annotated_frames = list()\n",
"annotated_frames = []\n",
"\n",
"if SAVE_GIF:\n",
" # add score and episode nr\n",
" rcParams[\"font.family\"] = \"monospace\"\n",
" bbox = dict(boxstyle=\"round\", fc=\"0.8\")\n",
" bbox = {\"boxstyle\": \"round\", \"fc\": \"0.8\"}\n",
" bar = tqdm(total=len(frames), position=0, leave=True)\n",
" for i in range(len(frames)):\n",
" fig = plt.figure(dpi=90)\n",
Expand Down
7 changes: 5 additions & 2 deletions python/notebooks/example_classification.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from sklearn.datasets import fetch_openml\n",
"from sklearn.metrics import ConfusionMatrixDisplay\n",
"from sklearn.metrics import confusion_matrix, classification_report\n",
"from sklearn.metrics import (\n",
" ConfusionMatrixDisplay,\n",
" classification_report,\n",
" confusion_matrix,\n",
")\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.preprocessing import MinMaxScaler, OneHotEncoder\n",
"\n",
Expand Down
5 changes: 3 additions & 2 deletions python/notebooks/example_maze.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@
}
],
"source": [
"def plot_performance(env: Maze):\n",
"def plot_performance(env: Maze) -> None:\n",
" \"\"\"Plots learning performance.\"\"\"\n",
" plt.figure(figsize=(10, 6))\n",
" plt.plot(trials, steps)\n",
Expand Down Expand Up @@ -534,9 +534,10 @@
],
"source": [
"import io\n",
"from PIL import Image\n",
"from turtle import Screen, Turtle\n",
"\n",
"from IPython.display import display\n",
"from PIL import Image\n",
"\n",
"GRID_WIDTH: int = maze.x_size\n",
"GRID_HEIGHT: int = maze.y_size\n",
Expand Down
2 changes: 1 addition & 1 deletion python/notebooks/example_regression.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"import graphviz\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from IPython.display import display, Image\n",
"from IPython.display import Image, display\n",
"from sklearn.datasets import fetch_openml\n",
"from sklearn.ensemble import RandomForestRegressor\n",
"from sklearn.gaussian_process import GaussianProcessRegressor\n",
Expand Down
11 changes: 5 additions & 6 deletions python/notebooks/example_rmux.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,12 @@
" reward += 0.1\n",
" else:\n",
" reward = self.max_payoff\n",
" elif self.payoff_map:\n",
" reward = (pos - self.pos_bits) * 0.2\n",
" if answer == 1:\n",
" reward += 0.1\n",
" else:\n",
" if self.payoff_map:\n",
" reward = (pos - self.pos_bits) * 0.2\n",
" if answer == 1:\n",
" reward += 0.1\n",
" else:\n",
" reward = 0\n",
" reward = 0\n",
" return correct, reward"
]
},
Expand Down
1 change: 1 addition & 0 deletions python/notebooks/example_seeding.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"outputs": [],
"source": [
"import json\n",
"\n",
"import numpy as np\n",
"\n",
"import xcsf\n",
Expand Down
6 changes: 3 additions & 3 deletions python/notebooks/example_tuning.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -693,12 +693,12 @@
"source": [
"# General parameters can be searched in the usual way\n",
"\n",
"parameters = dict(beta=[0.1, 0.5])\n",
"parameters = {\"beta\": [0.1, 0.5]}\n",
"\n",
"# EA parameters require specifying a dict,\n",
"# but individual values can still be set because the other values are not changed.\n",
"\n",
"parameters = dict(ea=[{\"lambda\": 2}, {\"lambda\": 10}])\n",
"parameters = {\"ea\": [{\"lambda\": 2}, {\"lambda\": 10}]}\n",
"\n",
"# However, for actions, conditions, and predictions, the WHOLE dict must be specified\n",
"# for each value to try in the search. See Optuna below.\n",
Expand Down Expand Up @@ -752,7 +752,7 @@
}
],
"source": [
"def objective(trial):\n",
"def objective(trial: optuna.trial.Trial) -> float:\n",
" \"\"\"Measure a new hyperparameter combination.\"\"\"\n",
" # get new parameters to try\n",
" e0: float = trial.suggest_float(\"e0\", 0.08, 0.12, step=0.01)\n",
Expand Down
Loading
Loading