Skip to content

Commit

Permalink
Merge branch 'master' into doc_animations
Browse files Browse the repository at this point in the history
# Conflicts:
#	brian2/sphinxext/generate_examples.py
  • Loading branch information
mstimberg committed Nov 18, 2022
2 parents 915fbc2 + 11d11e9 commit 0e28341
Show file tree
Hide file tree
Showing 167 changed files with 24,366 additions and 18,639 deletions.
4 changes: 3 additions & 1 deletion .devcontainer/dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
scipy >= 0.13.3
matplotlib >= 2.0
jupyterlab
ipympl
ipympl
pre-commit == 2.20.*
black == 22.10.0
5 changes: 3 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
"python.defaultInterpreterPath": "/usr/local/bin/python",
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.formatting.provider": "black",
"python.formatting.autopep8Path": "/usr/local/py-utils/bin/autopep8",
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
"python.formatting.blackPath": "black",
"python.formatting.yapfPath": "/usr/local/py-utils/bin/yapf",
"python.linting.banditPath": "/usr/local/py-utils/bin/bandit",
"python.linting.flake8Path": "/usr/local/py-utils/bin/flake8",
Expand All @@ -46,7 +47,7 @@

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "pip3 install --user -r requirements.txt",
"postCreateCommand": "pip3 install --user -e '.[test,doc]' && pip3 freeze > .devcontainer/frozen-requirements.txt",
"postCreateCommand": "pre-commit install && pip3 install --user -e '.[test,doc]' && pip3 freeze > .devcontainer/frozen-requirements.txt",

// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode",
Expand Down
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@

# Unify string formatting and single versus double quotes
d3ae59251c753ae0737d6ae6242b7e85b60908c4
# Reformatting with black
1e9ea598491444fe7c4ee9ece2ec94ad7c5020ec
3 changes: 1 addition & 2 deletions .github/workflows/publish_to_pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ jobs:
- name: Build source tarball (only on Linux)
run: |
python -m pip install --upgrade pip
python -m pip install "cython>=0.29" oldest-supported-numpy setuptools
python -m pip install "cython>=0.29" oldest-supported-numpy setuptools versioneer
python setup.py sdist --formats=gztar --with-cython --fail-on-error
if: ${{ startsWith(matrix.os, 'ubuntu-') }}
- name: Build wheels
uses: pypa/cibuildwheel@v2.2.2
with:
output-dir: dist
env:
CIBW_BEFORE_BUILD: pip install "cython>=0.29" oldest-supported-numpy setuptools
CIBW_PROJECT_REQUIRES_PYTHON: ">=3.7"
CIBW_ARCHS: auto64
CIBW_MANYLINUX_X86_64_IMAGE: manylinux2014
Expand Down
19 changes: 17 additions & 2 deletions .github/workflows/testsuite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,24 @@ jobs:
uses: mstimberg/github-calc-nep29@v0.5
with:
token: ${{ secrets.GITHUB_TOKEN }}


pre-commit:
name: Run linters with pre-commit
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
cache: 'pip'
cache-dependency-path: .devcontainer/dev-requirements.txt
- name: Install deps
run: pip3 install -r .devcontainer/dev-requirements.txt
- name: Run pre-commit hooks
run: pre-commit run --all-files --show-diff-on-failure

testing:
needs: [get_python_versions]
needs: [get_python_versions, pre-commit]
name: "Python ${{ matrix.python-version }} on ${{ matrix.os }} (standalone: ${{ matrix.standalone }}, 32bit: ${{ matrix.float_dtype_32 }})"
runs-on: ${{ matrix.os }}
strategy:
Expand Down
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
repos:
- repo: meta
hooks:
- id: check-hooks-apply
- id: check-useless-excludes
- repo: https://github.com/psf/black
rev: '22.10.0'
hooks:
- id: black
exclude: '^brian2/_version.py$'
files: '^brian2/.*\.pyi?$'
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ Abolfazl Ziaeemehr (@Ziaeemehr)
Étienne Mollier (@emollier)
Sebastian Schmitt (@schmitts)
Felix C. Stegerman (@obfusk)
Oleksii Leonov (@aleksejleonov)
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ Stimberg, M, Brette, R, Goodman, DFM. “Brian 2, an Intuitive and Efficient Neu
:alt: Join the chat at https://gitter.im/brian-team/brian2
:target: https://gitter.im/brian-team/brian2?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge


.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
:alt: code style: black
:target: https://github.com/psf/black

Quickstart
----------
Try out Brian on the `mybinder <https://mybinder.org/>`_ service:
Expand Down
87 changes: 57 additions & 30 deletions brian2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@
def _check_dependencies():
"""Check basic dependencies"""
import sys

missing = []
try:
import numpy
except ImportError as ex:
sys.stderr.write(f"Importing numpy failed: '{ex}'\n")
missing.append('numpy')
missing.append("numpy")
try:
import sympy
except ImportError as ex:
sys.stderr.write(f"Importing sympy failed: '{ex}'\n")
missing.append('sympy')
missing.append("sympy")
try:
import pyparsing
except ImportError as ex:
sys.stderr.write(f"Importing pyparsing failed: '{ex}'\n")
missing.append('pyparsing')
missing.append("pyparsing")
try:
import jinja2
except ImportError as ex:
sys.stderr.write(f"Importing Jinja2 failed: '{ex}'\n")
missing.append('jinja2')
missing.append("jinja2")

if len(missing):
raise ImportError(f"Some required dependencies are missing:\n{', '.join(missing)}")
raise ImportError(
f"Some required dependencies are missing:\n{', '.join(missing)}"
)


_check_dependencies()

Expand All @@ -42,28 +46,30 @@ def _check_dependencies():
from numpy.random import *
from numpy.linalg import *
import numpy.ma as ma

# don't let numpy's datetime hide stdlib
import datetime

from ._version import get_versions as _get_versions
__version__ = _get_versions()['version']
__release_date__ = _get_versions()['date']

__version__ = _get_versions()["version"]
__release_date__ = _get_versions()["date"]

if __release_date__ is not None:
__release_date__ = __release_date__[:10] #only use date part
__git_revision__ = _get_versions()['full-revisionid']
__release_date__ = __release_date__[:10] # only use date part
__git_revision__ = _get_versions()["full-revisionid"]

# Make sure that Brian's unit-aware functions are used, even when directly
# using names prefixed with numpy or np
import brian2.numpy_ as numpy
import brian2.numpy_ as np

# delete some annoying names from the namespace
if 'x' in globals():
if "x" in globals():
del x
if 'f' in globals():
if "f" in globals():
del f
if 'rate' in globals():
if "rate" in globals():
del rate

__docformat__ = "restructuredtext en"
Expand All @@ -73,30 +79,32 @@ def _check_dependencies():

# Check for outdated dependency versions
def _check_dependency_version(name, version):
from distutils.version import LooseVersion
from packaging.version import Version
from .core.preferences import prefs
from .utils.logger import get_logger
import sys

logger = get_logger(__name__)

module = sys.modules[name]
if not isinstance(module.__version__, str): # mocked module
return
if not LooseVersion(module.__version__) >= LooseVersion(version):
message = f'{name} is outdated (got version {module.__version__}, need version {version})'
if not Version(module.__version__) >= Version(version):
message = (
f"{name} is outdated (got version {module.__version__}, need version"
f" {version})"
)
if prefs.core.outdated_dependency_error:
raise ImportError(message)
else:

logger.warn(message, 'outdated_dependency')
logger.warn(message, "outdated_dependency")


def _check_dependency_versions():
for name, version in [('numpy', '1.10'),
('sympy', '1.2'),
('jinja2', '2.7')]:
for name, version in [("numpy", "1.10"), ("sympy", "1.2"), ("jinja2", "2.7")]:
_check_dependency_version(name, version)


_check_dependency_versions()

# Initialize the logging system
Expand All @@ -107,6 +115,7 @@ def _check_dependency_versions():
# Check the caches
def _get_size_recursively(dirname):
import os

total_size = 0
for dirpath, _, filenames in os.walk(dirname):
for fname in filenames:
Expand All @@ -120,20 +129,24 @@ def _get_size_recursively(dirname):
pass # ignore the file
return total_size


#: Stores the cache directory for code generation targets
_cache_dirs_and_extensions = {}


def check_cache(target):
cache_dir, _ = _cache_dirs_and_extensions.get(target, (None, None))
if cache_dir is None:
return
size = _get_size_recursively(cache_dir)
size_in_mb = int(round(size/1024./1024.))
size_in_mb = int(round(size / 1024.0 / 1024.0))
if size_in_mb > prefs.codegen.max_cache_dir_size:
logger.info(f"Cache size for target '{target}': {size_in_mb} MB.\n"
f"You can call clear_cache('{target}') to delete all "
f"files from the cache or manually delete files in the "
f"'{cache_dir}' directory.")
logger.info(
f"Cache size for target '{target}': {size_in_mb} MB.\n"
f"You can call clear_cache('{target}') to delete all "
"files from the cache or manually delete files in the "
f"'{cache_dir}' directory."
)
else:
logger.debug(f"Cache size for target '{target}': {size_in_mb} MB")

Expand All @@ -158,6 +171,7 @@ def clear_cache(target):
"""
import os
import shutil

cache_dir, extensions = _cache_dirs_and_extensions.get(target, (None, None))
if cache_dir is None:
raise ValueError(f'No cache directory registered for target "{target}".')
Expand All @@ -168,10 +182,12 @@ def clear_cache(target):
if f.endswith(ext):
break
else:
raise IOError(f"The cache directory for target '{target}' contains "
f"the file '{os.path.join(folder, f)}' of an unexpected type and "
f"will therefore not be removed. Delete files in "
f"'{cache_dir}' manually")
raise IOError(
f"The cache directory for target '{target}' contains "
f"the file '{os.path.join(folder, f)}' of an unexpected type and "
"will therefore not be removed. Delete files in "
f"'{cache_dir}' manually"
)

logger.debug(f"Clearing cache for target '{target}' (directory '{cache_dir}').")
shutil.rmtree(cache_dir)
Expand All @@ -181,9 +197,20 @@ def _check_caches():
from brian2.codegen.runtime.cython_rt.extension_manager import get_cython_cache_dir
from brian2.codegen.runtime.cython_rt.extension_manager import get_cython_extensions

for target, (dirname, extensions) in [('cython', (get_cython_cache_dir(), get_cython_extensions()))]:
for target, (dirname, extensions) in [
("cython", (get_cython_cache_dir(), get_cython_extensions()))
]:
_cache_dirs_and_extensions[target] = (dirname, extensions)
if prefs.codegen.max_cache_dir_size > 0:
check_cache(target)


_check_caches()

from . import _version

__version__ = _version.get_versions()["version"]

from . import _version

__version__ = _version.get_versions()["version"]
Loading

0 comments on commit 0e28341

Please sign in to comment.