Skip to content

Commit a994aa3

Browse files
authored
Merge pull request #251 from rsagroup/cython
Cython port of core similarity computation
2 parents b176e20 + 6e0e928 commit a994aa3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1679
-428
lines changed

.github/workflows/pull_request.yml

Lines changed: 80 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,96 @@
1-
name: pull_request_tests
1+
name: pull_request_builds
22

33
on:
44
pull_request:
55
types: [opened, synchronize, reopened]
66

77
jobs:
8+
89
tests:
10+
runs-on: ubuntu-22.04
11+
steps:
12+
- uses: actions/checkout@v3
13+
- uses: actions/setup-python@v4
14+
with:
15+
python-version: '3.10'
16+
- name: Python Version
17+
run: python --version
18+
- name: Update Pip
19+
run: python -m pip install --upgrade pip
20+
- name: Install dependencies
21+
run: pip install -r requirements.txt
22+
- name: Install rsatoolbox
23+
run: pip install .
24+
- name: Install test dependencies
25+
run: pip install -r tests/requirements.txt
26+
- name: Unit tests
27+
run: pytest
928

10-
runs-on: ubuntu-latest
29+
source:
30+
needs: tests
31+
runs-on: ${{ matrix.os }}
1132
strategy:
1233
matrix:
13-
python-version: [3.7, 3.8, 3.9]
14-
34+
python-version: ['3.7', '3.8', '3.9', '3.10']
35+
os: [ubuntu-latest, macos-latest, windows-latest]
1536
steps:
16-
- uses: actions/checkout@v2
17-
- name: Setup Python
18-
uses: actions/setup-python@v2
37+
- uses: actions/checkout@v3
38+
- uses: actions/setup-python@v4
1939
with:
2040
python-version: ${{ matrix.python-version }}
21-
- name: Update pip
41+
- name: Python Version
42+
run: python --version
43+
- name: Update Pip
2244
run: python -m pip install --upgrade pip
23-
- name: Install dependencies
45+
- name: Install Build
46+
run: pip install build
47+
- name: Build package
48+
run: python -m build --sdist
49+
- name: Install rsatoolbox (Linux, Mac)
50+
run: pip install dist/*
51+
if: matrix.os != 'windows-latest'
52+
- name: Install rsatoolbox (Windows)
2453
run: |
25-
pip install -r requirements.txt
26-
pip install -r tests/requirements.txt
54+
$sdistfname = Get-ChildItem dist -Name
55+
pip install dist/$sdistfname
56+
if: matrix.os == 'windows-latest'
57+
- name: Install test dependencies
58+
run: pip install -r tests/requirements.txt
2759
- name: Unit tests
28-
run: pytest tests
60+
run: pytest
61+
- name: Store artifact
62+
uses: actions/upload-artifact@v3
63+
with:
64+
name: source
65+
path: dist/*
66+
if-no-files-found: error
67+
retention-days: 1
68+
69+
binaries:
70+
needs: tests
71+
runs-on: ${{ matrix.os }}
72+
strategy:
73+
matrix:
74+
os: [ubuntu-latest, macos-latest, windows-latest]
75+
steps:
76+
- uses: actions/checkout@v3
77+
- uses: actions/setup-python@v4
78+
with:
79+
python-version: '3.10'
80+
- name: Python Version
81+
run: python --version
82+
- name: Update Pip
83+
run: python -m pip install --upgrade pip
84+
- name: Build package
85+
uses: pypa/[email protected]
86+
with:
87+
output-dir: dist
88+
- name: Install test dependencies
89+
run: pip install -r tests/requirements.txt
90+
- name: Store artifact
91+
uses: actions/upload-artifact@v3
92+
with:
93+
name: wheels-${{ matrix.os }}
94+
path: dist/*.whl
95+
if-no-files-found: error
96+
retention-days: 1

.gitignore

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1+
# Debug and profile files
2+
debug*.py
3+
*.Result
4+
5+
# c files resulting from cython
6+
src/rsatoolbox/cengine/*.c
7+
src/rsatoolbox/cengine/*.html
8+
9+
# data files from tutorials & demos
10+
cadena_ploscb_data.pkl
111
fmri_data
2-
allendata
12+
demos/allendata
13+
14+
# stats files
15+
stats/**/*.npz
16+
stats/**/*.hdf5
17+
stats/**/*.npy
18+
stats/**/*.txt
19+
stats/**/*.csv
20+
stats/boc
21+
stats/illustrations
22+
stats/figures
323

424
# IDEs
525
.vscode/
@@ -18,8 +38,10 @@ __pycache__/
1838

1939
# C extensions
2040
*.so
41+
src/rsatoolbox/*.html
2142

2243
# Distribution / packaging
44+
wheelhouse/
2345
.Python
2446
build/
2547
develop-eggs/

MANIFEST.in

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
recursive-include rsatoolbox *.mplstyle
1+
recursive-include src/rsatoolbox *.mplstyle
22
include requirements.txt
3+
include tests/requirements.txt
4+
recursive-exclude *.yml
5+
prune .github
6+
prune demos
7+
prune docs
8+
prune tests
9+
prune wiki

consistencytests.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from rsatoolbox.model import ModelInterpolate, ModelWeighted
2+
from rsatoolbox.model.fitter import fit_regress, fit_optimize_positive
3+
from rsatoolbox.rdm import concat, compare
4+
from rsatoolbox.data import Dataset
5+
from rsatoolbox.rdm import calc_rdm
6+
from rsatoolbox.rdm import concat
7+
import numpy as np
8+
9+
10+
11+
rdms = []
12+
for _ in range(5):
13+
data = np.random.rand(6, 20)
14+
data_s = Dataset(data)
15+
rdms.append(calc_rdm(data_s))
16+
rdms = concat(rdms)
17+
18+
#def test_two_rdms_nan
19+
20+
for i_method in ['corr_cov']: #['cosine', 'corr', 'cosine_cov', 'corr_cov']:
21+
22+
for i in range(200):
23+
rdms = []
24+
for _ in range(5):
25+
data = np.random.rand(6, 20)
26+
data_s = Dataset(data)
27+
rdms.append(calc_rdm(data_s))
28+
rdms = concat(rdms)
29+
rdms = rdms.subsample_pattern('index', [0, 1, 1, 3, 4, 5])
30+
model_rdms = concat([rdms[0], rdms[1]])
31+
model_weighted = ModelWeighted(
32+
'm_weighted',
33+
model_rdms)
34+
model_interpolate = ModelInterpolate(
35+
'm_interpolate',
36+
model_rdms)
37+
# theta_m_i = model_interpolate.fit(rdms, method=i_method)
38+
theta_m_w = model_weighted.fit(rdms, method=i_method)
39+
# theta_m_w_pos = fit_optimize_positive(
40+
# model_weighted, rdms, method=i_method)
41+
theta_m_w_linear = fit_regress(model_weighted, rdms, method=i_method)
42+
# eval_m_i = np.mean(compare(model_weighted.predict_rdm(
43+
# theta_m_i), rdms, method=i_method))
44+
eval_m_w = np.mean(compare(
45+
model_weighted.predict_rdm(theta_m_w),
46+
rdms,
47+
method=i_method
48+
))
49+
# eval_m_w_pos = np.mean(compare(model_weighted.predict_rdm(
50+
# theta_m_w_pos), rdms, method=i_method))
51+
eval_m_w_linear = np.mean(compare(
52+
model_weighted.predict_rdm(theta_m_w_linear),
53+
rdms,
54+
method=i_method
55+
))
56+
#rdiff_wei_int = (eval_m_i - eval_m_w_pos) / eval_m_w_pos
57+
rdiff_reg_opt = (eval_m_w - eval_m_w_linear) / eval_m_w_linear
58+
assert np.all(theta_m_w > 0)
59+
if np.all(theta_m_w > 0):
60+
# does it only happen with negative theta?
61+
assert abs(rdiff_reg_opt) < 0.002
62+
# assert False
63+
#assert abs(rdiff_reg_opt) < 0.02
64+

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# The short X.Y version
2727
version = '0.0'
2828
# The full version, including alpha/beta/rc tags
29-
release = '0.0.4'
29+
release = '0.0.5'
3030

3131

3232
# -- General configuration ---------------------------------------------------

pyproject.toml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
[build-system]
2+
requires = [
3+
"setuptools~=65.3",
4+
"setuptools-scm",
5+
"wheel",
6+
"numpy>=1.21.2",
7+
"scipy",
8+
"cython~=3.0.0a11"
9+
]
10+
build-backend = "setuptools.build_meta"
11+
12+
[project]
13+
name = "rsatoolbox"
14+
description = "Representational Similarity Analysis (RSA) in Python"
15+
requires-python = ">=3.7,<3.11"
16+
version = "0.0.5"
17+
authors = [
18+
{name="rsatoolbox authors"},
19+
]
20+
keywords = ["neuroscience "]
21+
license = {file = "LICENSE"}
22+
classifiers = [
23+
'Programming Language :: Python',
24+
'License :: OSI Approved :: MIT License',
25+
'Operating System :: OS Independent',
26+
'Development Status :: 4 - Beta',
27+
'Topic :: Scientific/Engineering',
28+
'Intended Audience :: Science/Research',
29+
'Programming Language :: Python :: 3.7',
30+
'Programming Language :: Python :: 3.8',
31+
'Programming Language :: Python :: 3.9',
32+
'Programming Language :: Python :: 3.10',
33+
]
34+
dynamic = ["readme", "dependencies"]
35+
36+
[tool.setuptools]
37+
packages = ["rsatoolbox"]
38+
package-dir = {"" = "src"}
39+
40+
[tool.setuptools.dynamic]
41+
readme = {file = "README.md"}
42+
dependencies = {file = "requirements.txt"}
43+
44+
[tool.pytest.ini_options]
45+
testpaths = [
46+
"tests"
47+
]
48+
python_files = "*.py"
49+
50+
[tool.cibuildwheel]
51+
test-requires = "pytest"
52+
test-command = "pytest {project}/tests"
53+
before-test = "pip install -r tests/requirements.txt"
54+
skip = ["*-win32", "*-manylinux_i686", "*-musllinux_*", "pp*"]

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,4 @@ tqdm
77
h5py
88
matplotlib
99
joblib
10-
petname==2.2
1110
pandas

setup.py

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,20 @@
1-
from setuptools import setup, find_packages
2-
3-
requires = []
4-
with open('requirements.txt') as reqfile:
5-
requires = reqfile.read().splitlines()
1+
from setuptools import setup, Extension
2+
from Cython.Build import build_ext ## missing dev time req
3+
from os.path import isfile
64

75
test_requires = []
8-
with open('tests/requirements.txt') as reqfile:
9-
test_requires = reqfile.read().splitlines()
10-
11-
with open('README.md', encoding='utf-8') as readmefile:
12-
long_description = readmefile.read()
13-
6+
if isfile('tests/requirements.txt'):
7+
with open('tests/requirements.txt') as reqfile:
8+
test_requires = reqfile.read().splitlines()
149

1510
setup(
16-
name='rsatoolbox',
17-
version='0.0.4',
18-
description='Representational Similarity Analysis (RSA) in Python',
19-
url='https://github.com/rsagroup/rsatoolbox',
20-
long_description=long_description,
21-
long_description_content_type='text/markdown',
22-
classifiers=[
23-
'Programming Language :: Python',
24-
'License :: OSI Approved :: MIT License',
25-
'Operating System :: OS Independent',
26-
'Development Status :: 4 - Beta',
27-
'Topic :: Scientific/Engineering',
28-
'Intended Audience :: Science/Research',
29-
'Programming Language :: Python :: 3.7',
30-
'Programming Language :: Python :: 3.8',
31-
'Programming Language :: Python :: 3.9',
32-
],
33-
author='rsatoolbox authors',
34-
author_email='',
35-
keywords='neuroscience ',
36-
packages=find_packages(),
37-
include_package_data=True,
38-
zip_safe=False,
39-
install_requires=requires,
4011
tests_require=test_requires,
4112
test_suite='tests',
13+
url = "https://github.com/rsagroup/rsatoolbox",
14+
zip_safe = False,
15+
ext_modules = [
16+
Extension(
17+
"rsatoolbox.cengine.similarity",
18+
["src/rsatoolbox/cengine/similarity.pyx"])],
19+
cmdclass={'build_ext': build_ext}
4220
)

rsatoolbox/__init__.py renamed to src/rsatoolbox/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
""" Top level package: Only imports and organisation
44
"""
55

6+
from . import cengine
67
from . import data
78
from . import inference
89
from . import model

src/rsatoolbox/cengine/__init__.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .similarity cimport similarity

src/rsatoolbox/cengine/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .similarity import similarity, calc_one

0 commit comments

Comments
 (0)