Skip to content

Commit 1a29509

Browse files
authored
Merge pull request #142 from geometric-intelligence/ninamiolane-lint2
Pass ruff linter on whole codebase
2 parents f367123 + ab55f2f commit 1a29509

24 files changed

+239
-11515
lines changed

.pre-commit-config.yaml

+1-6
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,4 @@ repos:
2525
types_or: [ python, pyi, jupyter ]
2626
args: [ --fix ]
2727
- id: ruff-format
28-
types_or: [ python, pyi, jupyter ]
29-
30-
- repo: https://github.com/numpy/numpydoc
31-
rev: v1.6.0
32-
hooks:
33-
- id: numpydoc-validation
28+
types_or: [ python, pyi, jupyter ]

neurometry/curvature/datasets/gridcells.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def generate_all_grids(
9696
grids : numpy.ndarray, shape=(num_cells, num_fields_per_cell = (ceil(dims[0]/lx)+1)*(ceil(dims[1]/ly)+1),2)
9797
All the grid cell lattices.
9898
"""
99-
lx = ly = 10 # TODO: FIX, these values are only placeholders.
99+
lx = ly = 10 # TODO: FIX, these values are only placeholders.
100100
# ref_lattice = create_reference_lattice(lx, ly, arena_dims, lattice_type = lattice_type)
101101
ref_lattice = structures.get_lattice(
102102
scale=grid_scale, lattice_type=lattice_type, dimensions=arena_dims
@@ -107,15 +107,16 @@ def generate_all_grids(
107107
grids_warped = np.zeros((n_cells, *np.shape(ref_lattice)))
108108

109109
arena_dims = np.array(arena_dims)
110+
rng = np.random.default_rng(seed=0)
110111

111112
for i in range(n_cells):
112-
angle_i = np.random.normal(grid_orientation_mean, grid_orientation_std) * (
113+
angle_i = rng.normal(grid_orientation_mean, grid_orientation_std) * (
113114
np.pi / 180
114115
)
115116
rot_i = np.array(
116117
[[np.cos(angle_i), -np.sin(angle_i)], [np.sin(angle_i), np.cos(angle_i)]]
117118
)
118-
phase_i = np.multiply([lx, ly], np.random.rand(2))
119+
phase_i = np.multiply([lx, ly], rng.random(2))
119120
lattice_i = np.matmul(rot_i, ref_lattice.T).T + phase_i
120121
# lattice_i = np.where(abs(lattice_i) < arena_dims / 2, lattice_i, None)
121122

neurometry/curvature/datasets/synthetic.py

+28-26
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,14 @@ def load_images(n_scalars=10, n_angles=1000, img_size=256):
7070
images = []
7171
angles = []
7272
scalars = []
73+
rng = np.random.default_rng(seed=0)
7374
for i_angle in range(n_angles):
7475
angle = 360 * i_angle / n_angles
7576
rot_image = skimage.transform.rotate(image, angle)
7677
for i_scalar in range(n_scalars):
7778
scalar = 1 + 0.2 * i_scalar
7879
blur_image = skimage.filters.gaussian(rot_image, sigma=scalar)
79-
noise = np.random.normal(loc=0.0, scale=0.05, size=blur_image.shape)
80+
noise = rng.normal(loc=0.0, scale=0.05, size=blur_image.shape)
8081
images.append((blur_image + noise).astype(np.float32))
8182
angles.append(angle)
8283
scalars.append(scalar)
@@ -240,40 +241,41 @@ def load_place_cells(n_times=10000, n_cells=40):
240241
n_firing_per_cell = int(n_times / n_cells)
241242
place_cells = []
242243
labels = []
244+
rng = np.random.default_rng(seed=0)
243245
for _ in range(n_firing_per_cell):
244246
for i_cell in range(n_cells):
245247
cell_firings = np.zeros(n_cells)
246248

247249
if i_cell == 0:
248-
cell_firings[-2] = np.random.poisson(1.0)
249-
cell_firings[-1] = np.random.poisson(2.0)
250-
cell_firings[0] = np.random.poisson(4.0)
251-
cell_firings[1] = np.random.poisson(2.0)
252-
cell_firings[2] = np.random.poisson(1.0)
250+
cell_firings[-2] = rng.poisson(1.0)
251+
cell_firings[-1] = rng.poisson(2.0)
252+
cell_firings[0] = rng.poisson(4.0)
253+
cell_firings[1] = rng.poisson(2.0)
254+
cell_firings[2] = rng.poisson(1.0)
253255
elif i_cell == 1:
254-
cell_firings[-1] = np.random.poisson(1.0)
255-
cell_firings[0] = np.random.poisson(2.0)
256-
cell_firings[1] = np.random.poisson(4.0)
257-
cell_firings[2] = np.random.poisson(2.0)
258-
cell_firings[3] = np.random.poisson(1.0)
256+
cell_firings[-1] = rng.poisson(1.0)
257+
cell_firings[0] = rng.poisson(2.0)
258+
cell_firings[1] = rng.poisson(4.0)
259+
cell_firings[2] = rng.poisson(2.0)
260+
cell_firings[3] = rng.poisson(1.0)
259261
elif i_cell == n_cells - 2:
260-
cell_firings[-4] = np.random.poisson(1.0)
261-
cell_firings[-3] = np.random.poisson(2.0)
262-
cell_firings[-2] = np.random.poisson(4.0)
263-
cell_firings[-1] = np.random.poisson(2.0)
264-
cell_firings[0] = np.random.poisson(1.0)
262+
cell_firings[-4] = rng.poisson(1.0)
263+
cell_firings[-3] = rng.poisson(2.0)
264+
cell_firings[-2] = rng.poisson(4.0)
265+
cell_firings[-1] = rng.poisson(2.0)
266+
cell_firings[0] = rng.poisson(1.0)
265267
elif i_cell == n_cells - 1:
266-
cell_firings[-3] = np.random.poisson(1.0)
267-
cell_firings[-2] = np.random.poisson(2.0)
268-
cell_firings[-1] = np.random.poisson(4.0)
269-
cell_firings[0] = np.random.poisson(2.0)
270-
cell_firings[1] = np.random.poisson(1.0)
268+
cell_firings[-3] = rng.poisson(1.0)
269+
cell_firings[-2] = rng.poisson(2.0)
270+
cell_firings[-1] = rng.poisson(4.0)
271+
cell_firings[0] = rng.poisson(2.0)
272+
cell_firings[1] = rng.poisson(1.0)
271273
else:
272-
cell_firings[i_cell - 2] = np.random.poisson(1.0)
273-
cell_firings[i_cell - 1] = np.random.poisson(2.0)
274-
cell_firings[i_cell] = np.random.poisson(4.0)
275-
cell_firings[i_cell + 1] = np.random.poisson(2.0)
276-
cell_firings[i_cell - 3] = np.random.poisson(1.0)
274+
cell_firings[i_cell - 2] = rng.poisson(1.0)
275+
cell_firings[i_cell - 1] = rng.poisson(2.0)
276+
cell_firings[i_cell] = rng.poisson(4.0)
277+
cell_firings[i_cell + 1] = rng.poisson(2.0)
278+
cell_firings[i_cell - 3] = rng.poisson(1.0)
277279
place_cells.append(cell_firings)
278280
labels.append(i_cell / n_cells * 360)
279281

neurometry/curvature/datasets/utils.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,10 @@ def load(config):
141141
indices = np.arange(len(dataset))
142142

143143
train_indices = np.arange(train_num)
144+
rng = np.random.default_rng(seed=0)
144145
if config.batch_shuffle:
145146
# Note: this breaks the temporal ordering.
146-
train_indices = np.random.choice(indices, train_num, replace=False)
147+
train_indices = rng.choice(indices, train_num, replace=False)
147148

148149
test_indices = np.delete(indices, train_indices)
149150
train_dataset = dataset[train_indices]

neurometry/curvature/plots.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
# Generate some sample data
66

7-
theta = np.random.uniform(0, 2 * np.pi, 1000)
7+
rng = np.random.default_rng(seed=0)
8+
theta = rng.uniform(0, 2 * np.pi, 1000)
89

9-
r = np.random.normal(1, 0.1, 1000)
10+
r = rng.normal(1, 0.1, 1000)
1011

1112
x = r * np.cos(theta)
1213
y = r * np.sin(theta)
13-
z = np.random.normal(0, 0.1, 1000)
14+
z = rng.normal(0, 0.1, 1000)
1415

1516

1617
# Create a 3D scatter plot

neurometry/datasets/load_rnn_grid_cells.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import os
22
import random
3-
import sys
4-
from pathlib import Path
53

64
import matplotlib.pyplot as plt
75
import numpy as np
86

9-
sys.path.append(str(Path(__file__).parent.parent))
10-
11-
from .rnn_grid_cells import config, dual_agent_activity, single_agent_activity, utils
7+
# sys.path.append(str(Path(__file__).parent.parent))
8+
from .rnn_grid_cells import (
9+
config,
10+
dual_agent_activity,
11+
single_agent_activity,
12+
utils,
13+
)
1214

1315
# Loading single agent model
1416

@@ -62,9 +64,10 @@ def load_activations(epochs, version="single", verbose=True):
6264
options, _ = parser.parse_known_args()
6365
options.run_ID = utils.generate_run_ID(options)
6466
if type == "single":
65-
activations_single_agent, rate_map_single_agent = (
66-
single_agent_activity.main(options, epoch=epoch)
67-
)
67+
(
68+
activations_single_agent,
69+
rate_map_single_agent,
70+
) = single_agent_activity.main(options, epoch=epoch)
6871
activations.append(activations_single_agent)
6972
rate_maps.append(rate_map_single_agent)
7073
elif type == "dual":
@@ -92,8 +95,9 @@ def load_activations(epochs, version="single", verbose=True):
9295

9396

9497
def plot_rate_map(indices, num_plots, activations):
98+
rng = np.random.default_rng(seed=0)
9599
if indices is None:
96-
idxs = np.random.randint(0, 4095, num_plots)
100+
idxs = rng.integers(0, 4095, num_plots)
97101
else:
98102
idxs = indices
99103
num_plots = len(indices)

neurometry/datasets/rnn_grid_cells/place_cells.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ def __init__(self, options, us=None):
1616
self.softmax = torch.nn.Softmax(dim=-1)
1717

1818
# Randomly tile place cell centers across environment
19-
np.random.seed(0)
20-
usx = np.random.uniform(-self.box_width / 2, self.box_width / 2, (self.Np,))
21-
usy = np.random.uniform(-self.box_width / 2, self.box_width / 2, (self.Np,))
19+
rng = np.random.default_rng(seed=0)
20+
usx = rng.uniform(-self.box_width / 2, self.box_width / 2, (self.Np,))
21+
usy = rng.uniform(-self.box_width / 2, self.box_width / 2, (self.Np,))
2222
self.us = torch.tensor(np.vstack([usx, usy]).T)
2323
# If using a GPU, put on GPU
2424
self.us = self.us.to(self.device)

neurometry/datasets/rnn_grid_cells/place_cells_dual_path_integration.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ def __init__(self, options, us=None):
1717
self.softmax = torch.nn.Softmax(dim=-1)
1818

1919
# Randomly tile place cell centers across environment
20-
np.random.seed(0)
21-
usx = np.random.uniform(-self.box_width / 2, self.box_width / 2, (self.Np,))
22-
usy = np.random.uniform(-self.box_width / 2, self.box_width / 2, (self.Np,))
20+
rng = np.random.default_rng(seed=0)
21+
usx = rng.uniform(-self.box_width / 2, self.box_width / 2, (self.Np,))
22+
usy = rng.uniform(-self.box_width / 2, self.box_width / 2, (self.Np,))
2323
self.us = torch.tensor(np.vstack([usx, usy]).T)
2424
# If using a GPU, put on GPU
2525
self.us = self.us.to(self.device)

neurometry/datasets/rnn_grid_cells/scores.py

+7-12
Original file line numberDiff line numberDiff line change
@@ -189,26 +189,21 @@ def get_scores(self, rate_map):
189189
max_60_ind,
190190
)
191191

192-
def plot_ratemap(self, ratemap, ax=None, title=None, *args, **kwargs): # pylint: disable=keyword-arg-before-vararg
192+
def plot_ratemap(self, ratemap, ax=None, title=None, *args, **kwargs):
193193
"""Plot ratemaps."""
194194
if ax is None:
195195
ax = plt.gca()
196-
# Plot the ratemap
197-
ax.imshow(ratemap, interpolation="none", *args, **kwargs)
198-
# ax.pcolormesh(ratemap, *args, **kwargs)
196+
ax.imshow(ratemap, *args, interpolation="none", **kwargs)
199197
ax.axis("off")
200198
if title is not None:
201199
ax.set_title(title)
202200

203-
def plot_sac(self, sac, mask_params=None, ax=None, title=None, *args, **kwargs): # pylint: disable=keyword-arg-before-vararg
201+
def plot_sac(self, sac, mask_params=None, ax=None, title=None, *args, **kwargs):
204202
"""Plot spatial autocorrelogram."""
205203
if ax is None:
206204
ax = plt.gca()
207-
# Plot the sac
208205
useful_sac = sac * self._plotting_sac_mask
209-
ax.imshow(useful_sac, interpolation="none", *args, **kwargs)
210-
# ax.pcolormesh(useful_sac, *args, **kwargs)
211-
# Plot a ring for the adequate mask
206+
ax.imshow(useful_sac, *args, interpolation="none", **kwargs)
212207
if mask_params is not None:
213208
center = self._nbins - 1
214209
ax.add_artist(
@@ -304,11 +299,11 @@ def get_sac_interp(self, cell):
304299
yy = np.linspace(-1, 1, 99)
305300
return scipy.interpolate.RegularGridInterpolator((xx, yy), sac)
306301

307-
def get_phi(
308-
self, cell, interp=None, spacing_values=np.arange(0.01, 1.0, 0.01)
309-
): # 0.15
302+
def get_phi(self, cell, interp=None, spacing_values=None): # 0.15
310303
"""Get orientation of grid cell."""
311304

305+
if spacing_values is None:
306+
spacing_values = np.arange(0.01, 1.0, 0.01)
312307
if interp is None:
313308
interp = self.get_sac_interp(cell)
314309

neurometry/datasets/rnn_grid_cells/trajectory_generator.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,18 @@ def generate_trajectory(self, box_width, box_height, batch_size):
4444
self.border_region = 0.03 # meters
4545

4646
# Initialize variables
47+
rng = np.random.default_rng(seed=0)
4748
position = np.zeros([batch_size, samples + 2, 2])
4849
head_dir = np.zeros([batch_size, samples + 2])
49-
position[:, 0, 0] = np.random.uniform(-box_width / 2, box_width / 2, batch_size)
50-
position[:, 0, 1] = np.random.uniform(
51-
-box_height / 2, box_height / 2, batch_size
52-
)
53-
head_dir[:, 0] = np.random.uniform(0, 2 * np.pi, batch_size)
50+
position[:, 0, 0] = rng.uniform(-box_width / 2, box_width / 2, batch_size)
51+
position[:, 0, 1] = rng.uniform(-box_height / 2, box_height / 2, batch_size)
52+
head_dir[:, 0] = rng.uniform(0, 2 * np.pi, batch_size)
5453
velocity = np.zeros([batch_size, samples + 2])
5554

5655
# Generate sequence of random boosts and turns
57-
random_turn = np.random.normal(mu, sigma, [batch_size, samples + 1])
58-
random_vel = np.random.rayleigh(b, [batch_size, samples + 1])
59-
v = np.abs(np.random.normal(0, b * np.pi / 2, batch_size))
56+
random_turn = rng.normal(mu, sigma, [batch_size, samples + 1])
57+
random_vel = rng.rayleigh(b, [batch_size, samples + 1])
58+
v = np.abs(rng.normal(0, b * np.pi / 2, batch_size))
6059

6160
for t in range(samples + 1):
6261
# Update velocity

neurometry/datasets/rnn_grid_cells/trajectory_generator_dual_path_integration.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,18 @@ def generate_trajectory(self, box_width, box_height, batch_size):
4444
self.border_region = 0.03 # meters
4545

4646
# Initialize variables
47+
rng = np.random.default_rng(seed=0)
4748
position = np.zeros([batch_size, samples + 2, 2])
4849
head_dir = np.zeros([batch_size, samples + 2])
49-
position[:, 0, 0] = np.random.uniform(-box_width / 2, box_width / 2, batch_size)
50-
position[:, 0, 1] = np.random.uniform(
51-
-box_height / 2, box_height / 2, batch_size
52-
)
53-
head_dir[:, 0] = np.random.uniform(0, 2 * np.pi, batch_size)
50+
position[:, 0, 0] = rng.uniform(-box_width / 2, box_width / 2, batch_size)
51+
position[:, 0, 1] = rng.uniform(-box_height / 2, box_height / 2, batch_size)
52+
head_dir[:, 0] = rng.uniform(0, 2 * np.pi, batch_size)
5453
velocity = np.zeros([batch_size, samples + 2])
5554

5655
# Generate sequence of random boosts and turns
57-
random_turn = np.random.normal(mu, sigma, [batch_size, samples + 1])
58-
random_vel = np.random.rayleigh(b, [batch_size, samples + 1])
59-
v = np.abs(np.random.normal(0, b * np.pi / 2, batch_size))
56+
random_turn = rng.normal(mu, sigma, [batch_size, samples + 1])
57+
random_vel = rng.rayleigh(b, [batch_size, samples + 1])
58+
v = np.abs(rng.normal(0, b * np.pi / 2, batch_size))
6059

6160
for t in range(samples + 1):
6261
# Update velocity

pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ repository="https://github.com/geometric-intelligence/neurometry"
8383

8484
[tool.ruff]
8585
target-version = "py311"
86-
extend-include = ["*.ipynb"]
86+
extend-exclude = ["*.ipynb"]
8787

8888
[tool.ruff.format]
8989
docstring-code-format = true
@@ -107,6 +107,7 @@ select = [
107107
ignore = [
108108
"E501", # line too long
109109
"PERF203", # allow try-except within loops
110+
"RUF012", # force typing
110111
]
111112

112113
[tool.ruff.lint.pydocstyle]

tutorials/01_methods_create_synthetic_data.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"# Synthetic Neural Manifolds"
7+
"# Create Synthetic Neural Manifolds"
88
]
99
},
1010
{

tutorials/02_methods_estimate_manifold_dimension.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"# Neural Dimensionality Estimation"
7+
"# Estimate Neural Dimensions"
88
]
99
},
1010
{

tutorials/03_methods_estimate_manifold_topology.ipynb

+9-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@
77
"tags": []
88
},
99
"source": [
10-
"## Set Up + Imports"
10+
"# Estimate Neural Topology"
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"id": "1ff5d6ed",
16+
"metadata": {},
17+
"source": [
18+
"### Set Up + Imports"
1119
]
1220
},
1321
{

tutorials/hyperbolic.ipynb renamed to tutorials/04_explore_hyperbolic_geometry.ipynb

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "2720eebc",
6+
"metadata": {},
7+
"source": [
8+
"# Explore Hyperbolic Geometry"
9+
]
10+
},
311
{
412
"cell_type": "markdown",
513
"id": "87c6f038-2b67-41ef-86a2-84d110626a3c",
614
"metadata": {},
715
"source": [
8-
"## Imports & Setup"
16+
"### Imports & Setup"
917
]
1018
},
1119
{

0 commit comments

Comments
 (0)