Skip to content

Commit

Permalink
lint: fix pep8-naming errors
Browse files Browse the repository at this point in the history
To reproduce the errors:
```
ruff check --select "N" black_it tests examples scripts
```

Some of the errors were fixed; other were ignored using the directives '# noqa: NXXX'.

We decided to ignore those errors that would have required invasive changes throughout the codebase.

For example, the model functions in examples/models have a positional parameter named 'N', which should be lowercase, but in other parts of the code the model functions were called with "N" as keyworded parameter. Hence, renaming N to n would have required a change to the function calls, replacing 'N=' with 'n='.
The same strategy was applied for function names that were used in many parts of the project.

Instead, many narrow-scoped variables were modified properly as suggested by the Ruff errors.
  • Loading branch information
marcofavorito authored and marcofavoritobi committed Sep 1, 2023
1 parent e1675cf commit 7568789
Show file tree
Hide file tree
Showing 27 changed files with 207 additions and 164 deletions.
6 changes: 3 additions & 3 deletions black_it/calibrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ def restore_from_checkpoint(
parameters_precision,
real_data,
ensemble_size,
N,
_D,
sim_length,
_d,
convergence_precision,
verbose,
saving_file,
Expand Down Expand Up @@ -290,7 +290,7 @@ def restore_from_checkpoint(
parameters_precision,
ensemble_size,
scheduler=scheduler,
sim_length=N,
sim_length=sim_length,
convergence_precision=convergence_precision,
verbose=verbose,
saving_folder=saving_file,
Expand Down
4 changes: 2 additions & 2 deletions black_it/loss_functions/fourier.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def compute_loss_1d(
The computed loss over the specific coordinate.
"""
f_real_data = np.fft.rfft(real_data, axis=0)
N = f_real_data.shape[0]
ts_length = f_real_data.shape[0]
f_real_data = self.frequency_filter(f_real_data, self.f)
# computer mean fft transform of simulated ensemble
f_sim_data = []
Expand All @@ -143,6 +143,6 @@ def compute_loss_1d(

f_sim_data = np.array(f_sim_data).mean(0)

loss_1d = np.sqrt(np.sum((abs(f_sim_data - f_real_data)) ** 2) / N)
loss_1d = np.sqrt(np.sum((abs(f_sim_data - f_real_data)) ** 2) / ts_length)

return loss_1d
14 changes: 7 additions & 7 deletions black_it/loss_functions/gsl_div.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,16 @@ def compute_loss_1d(
Returns:
the GSL loss
"""
N = len(real_data)
ts_length = len(real_data)
ensemble_size = sim_data_ensemble.shape[0]

if self.nb_values is None:
nb_values = int((N - 1) / 2.0)
nb_values = int((ts_length - 1) / 2.0)
else:
nb_values = self.nb_values

if self.nb_word_lengths is None:
nb_word_lengths = int((N - 1) / 2.0)
nb_word_lengths = int((ts_length - 1) / 2.0)
else:
nb_word_lengths = self.nb_word_lengths

Expand All @@ -149,7 +149,7 @@ def compute_loss_1d(
)

loss = self.gsl_div_1d_1_sample(
sim_xd, obs_xd, nb_word_lengths, nb_values, N
sim_xd, obs_xd, nb_word_lengths, nb_values, ts_length
)

gsl_loss += loss
Expand All @@ -162,7 +162,7 @@ def gsl_div_1d_1_sample(
obs_xd: NDArray,
nb_word_lengths: int,
nb_values: int,
N: int,
ts_length: int,
) -> float:
"""Compute the GSL-div for a single realisation of the simulated data.
Expand All @@ -171,7 +171,7 @@ def gsl_div_1d_1_sample(
obs_xd: discretised real series
nb_word_lengths: the number of word length to consider
nb_values: number of values the digitised series can take
N: the length of real and simulated series
ts_length: the length of real and simulated series
Returns:
the computed loss
Expand All @@ -197,7 +197,7 @@ def gsl_div_1d_1_sample(
weight = weight + 2 / (nb_word_lengths * (nb_word_lengths + 1))

# correction
corr = ((len(m_xp) - 1) - (len(sim_xp) - 1)) / (2 * N)
corr = ((len(m_xp) - 1) - (len(sim_xp) - 1)) / (2 * ts_length)

# add to measure
gsl_divl = 2 * m_entr - sim_entr + corr
Expand Down
36 changes: 18 additions & 18 deletions black_it/loss_functions/likelihood.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
from black_it.loss_functions.base import BaseLoss


def kernel(sq_dist: NDArray[np.float64], h: float, D: int) -> NDArray[np.float64]:
def kernel(sq_dist: NDArray[np.float64], h: float, d: int) -> NDArray[np.float64]:
"""Compute a kernel density estimation using a Gaussian kernel."""
k = np.exp(-(sq_dist / (2 * h**2))) / (h**D * (2 * np.pi) ** (D / 2.0))
k = np.exp(-(sq_dist / (2 * h**2))) / (h**d * (2 * np.pi) ** (d / 2.0))
return k


Expand Down Expand Up @@ -57,15 +57,15 @@ def __init__(
self.h = h

@staticmethod
def _get_bandwidth_silverman(N: int, D: int) -> float:
def _get_bandwidth_silverman(n: int, d: int) -> float:
"""Return a reasonable bandwidth value computed using the Silverman's rule of thumb."""
h = ((N * (D + 2)) / 4) ** (-1 / (D + 4))
h = ((n * (d + 2)) / 4) ** (-1 / (d + 4))
return h

@staticmethod
def _get_bandwidth_scott(N: int, D: int) -> float:
def _get_bandwidth_scott(n: int, d: int) -> float:
"""Return a reasonable bandwidth value computed using the Scott's rule of thumb."""
h = N ** (-1 / (D + 4))
h = n ** (-1 / (d + 4))
return h

def compute_loss(
Expand All @@ -81,9 +81,9 @@ def compute_loss(
Returns:
The loss value.
"""
R = sim_data_ensemble.shape[0] # number of repetitions
S = sim_data_ensemble.shape[1] # simulation length
D = sim_data_ensemble.shape[2] # number of dimensions
r = sim_data_ensemble.shape[0] # number of repetitions
s = sim_data_ensemble.shape[1] # simulation length
d = sim_data_ensemble.shape[2] # number of dimensions

if self.coordinate_weights is not None:
warnings.warn( # noqa: B028
Expand All @@ -92,35 +92,35 @@ def compute_loss(
RuntimeWarning,
)

filters = self._check_coordinate_filters(D)
filters = self._check_coordinate_filters(d)
filtered_data = self._filter_data(filters, sim_data_ensemble)
sim_data_ensemble = np.transpose(filtered_data, (1, 2, 0))

h = self._check_bandwidth(S, D)
h = self._check_bandwidth(s, d)
sq_dists_r_t_s = (
1.0
/ D
/ d
* np.sum(
(sim_data_ensemble[:, None, :, :] - real_data[None, :, None, :]) ** 2,
axis=3,
)
)

kernel_r_t_s = kernel(sq_dists_r_t_s, h, D)
lik_real_series_r_t = np.sum(kernel_r_t_s, axis=2) / S
kernel_r_t_s = kernel(sq_dists_r_t_s, h, d)
lik_real_series_r_t = np.sum(kernel_r_t_s, axis=2) / s
log_lik_real_series_r = np.sum(np.log(lik_real_series_r_t), axis=1)
log_lik_real_series = np.sum(log_lik_real_series_r, axis=0) / R
log_lik_real_series = np.sum(log_lik_real_series_r, axis=0) / r
return -log_lik_real_series

def _check_bandwidth(self, S: int, D: int) -> float:
def _check_bandwidth(self, s: int, d: int) -> float:
"""Check the bandwidth self.h and return a usable one."""
h: float

if isinstance(self.h, str):
if self.h == "silverman":
h = self._get_bandwidth_silverman(S, D)
h = self._get_bandwidth_silverman(s, d)
elif self.h == "scott":
h = self._get_bandwidth_scott(S, D)
h = self._get_bandwidth_scott(s, d)
else:
raise KeyError(
"Select a valid rule of thumb (either 'silverman' or 'scott') "
Expand Down
4 changes: 2 additions & 2 deletions black_it/loss_functions/msm.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ def compute_loss_1d(
loss_1d = g.dot(g)
return loss_1d
if self._covariance_mat == _CovarianceMatrixType.INVERSE_VARIANCE.value:
W = np.diag(
W = np.diag( # noqa: N806
1.0 / np.mean((real_mom_1d[None, :] - ensemble_sim_mom_1d) ** 2, axis=0)
)
else:
self._covariance_mat = cast(NDArray[np.float64], self._covariance_mat)
W = self._covariance_mat
W = self._covariance_mat # noqa: N806
try:
loss_1d = g.dot(W).dot(g)
except ValueError as e:
Expand Down
52 changes: 26 additions & 26 deletions black_it/plot/plot_descriptive_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,72 +23,72 @@
import statsmodels.api as sm


def ts_stats(ts: List[float]) -> None:
def ts_stats(time_series_raw: List[float]) -> None:
"""Show TS graphical descriptive statistics."""
color = "darkslateblue"
alpha = 0.8

Ts = np.array(ts, dtype="double")
SqDiff = np.append(
np.absolute(np.diff(Ts)), 0
ts = np.array(time_series_raw, dtype="double")
sq_diff = np.append(
np.absolute(np.diff(ts)), 0
) # not precise! shouldn't append '0'!

TsAcf = sm.tsa.acf(Ts, nlags=20)
SqDiffAcf = sm.tsa.acf(SqDiff, nlags=20)
ts_acf = sm.tsa.acf(ts, nlags=20)
sq_diff_acf = sm.tsa.acf(sq_diff, nlags=20)

TsPAcf = sm.tsa.pacf(Ts, nlags=20)
SqDiffPAcf = sm.tsa.pacf(SqDiff, nlags=20)
ts_p_acf = sm.tsa.pacf(ts, nlags=20)
sq_diff_p_acf = sm.tsa.pacf(sq_diff, nlags=20)

WINDOW_W = 20
window_w = 20

fig, _ax = plt.subplots(3, 4, figsize=(WINDOW_W, 15), sharex=False, sharey=False)
fig, _ax = plt.subplots(3, 4, figsize=(window_w, 15), sharex=False, sharey=False)

sp1 = plt.subplot(3, 4, 1)
plt.hist(Ts, 50, facecolor=color, alpha=alpha)
plt.hist(ts, 50, facecolor=color, alpha=alpha)
sp1.set_title("Ts hist")

sp2 = plt.subplot(3, 4, 2)
plt.plot(TsAcf, marker="o", linestyle="None", c=color, alpha=alpha)
plt.plot(ts_acf, marker="o", linestyle="None", c=color, alpha=alpha)
sp2.set_title("Ts acf")

sp3 = plt.subplot(3, 4, 3)
plt.plot(TsPAcf, marker="o", linestyle="None", c=color, alpha=alpha)
plt.plot(ts_p_acf, marker="o", linestyle="None", c=color, alpha=alpha)
sp3.set_title("Ts pacf")

sp4 = plt.subplot(3, 4, 4)
plt.plot(Ts[0:1000], c=color, alpha=alpha)
plt.plot(ts[0:1000], c=color, alpha=alpha)
sp4.set_title("Ts sample")

sp5 = plt.subplot(3, 4, 5)
plt.hist(SqDiff, 50, facecolor=color, alpha=alpha)
plt.hist(sq_diff, 50, facecolor=color, alpha=alpha)
sp5.set_title("Abs 1st diff hist")

sp6 = plt.subplot(3, 4, 6)
plt.plot(SqDiffAcf, marker="o", linestyle="None", c=color, alpha=alpha)
plt.plot(sq_diff_acf, marker="o", linestyle="None", c=color, alpha=alpha)
sp6.set_title("Abs 1st diff acf")

sp7 = plt.subplot(3, 4, 7)
plt.plot(SqDiffPAcf, marker="o", linestyle="None", c=color, alpha=alpha)
plt.plot(sq_diff_p_acf, marker="o", linestyle="None", c=color, alpha=alpha)
sp7.set_title("Abs 1st diff pacf")

sp8 = plt.subplot(3, 4, 8)
plt.plot(SqDiff[0:1000], c=color, alpha=alpha)
plt.plot(sq_diff[0:1000], c=color, alpha=alpha)
sp8.set_title("Abs 1st diff sample")

sp9 = plt.subplot(3, 4, 9)
for i in range(len(Ts) - 3):
plt.plot(Ts[i + 1 : i + 3], Ts[i : i + 2], alpha=0.10, c=color)
for i in range(len(ts) - 3):
plt.plot(ts[i + 1 : i + 3], ts[i : i + 2], alpha=0.10, c=color)
sp9.set_title("Ts X(t) vs X(t-1) traj")

sp10 = plt.subplot(3, 4, 10)
for i in range(len(Ts) - 4):
plt.plot(SqDiff[i + 1 : i + 3], SqDiff[i : i + 2], alpha=0.10, c=color)
for i in range(len(ts) - 4):
plt.plot(sq_diff[i + 1 : i + 3], sq_diff[i : i + 2], alpha=0.10, c=color)
sp10.set_title("Abs 1st diff X(t) vs X(t-1) traj")

sp11 = plt.subplot(3, 4, 11)
plt.plot(
Ts[1 : len(Ts)],
Ts[0 : len(Ts) - 1],
ts[1 : len(ts)],
ts[0 : len(ts) - 1],
marker="o",
linestyle="None",
alpha=0.10,
Expand All @@ -98,8 +98,8 @@ def ts_stats(ts: List[float]) -> None:

sp12 = plt.subplot(3, 4, 12)
plt.plot(
SqDiff[1 : len(Ts)],
SqDiff[0 : len(Ts) - 1],
sq_diff[1 : len(ts)],
sq_diff[0 : len(ts) - 1],
marker="o",
linestyle="None",
alpha=0.10,
Expand Down
18 changes: 11 additions & 7 deletions black_it/samplers/cors.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ def volume_d_dimensional_ball_radius_1(dims: int) -> float:
)


def cubetobox(X: NDArray[np.float64], space_bounds: NDArray) -> NDArray[np.float64]:
def cubetobox(
X: NDArray[np.float64], space_bounds: NDArray # noqa: N803
) -> NDArray[np.float64]:
"""Go from normalized values (unit cube) to absolute values (box)."""
box_points = space_bounds[0] + X * (space_bounds[1] - space_bounds[0])
return box_points


def boxtocube(X: NDArray[np.float64], space_bounds: NDArray) -> NDArray[np.float64]:
def boxtocube(
X: NDArray[np.float64], space_bounds: NDArray # noqa: N803
) -> NDArray[np.float64]:
"""Go from absolute values (box) to normalized values (unit cube)."""
cube_points = (X - space_bounds[0]) / (space_bounds[1] - space_bounds[0])
return cube_points
Expand All @@ -79,18 +83,18 @@ def phi(r: float) -> float:
"""Compute phi."""
return r * r * r

Phi = [
phis = [
[phi(np.linalg.norm(np.subtract(points[i], points[j]))) for j in range(n)] # type: ignore
for i in range(n)
]

P = np.ones((n, d + 1))
P = np.ones((n, d + 1)) # noqa: N806
P[:, 0:-1] = points

F = losses
F = losses # noqa: N806

M = np.zeros((n + d + 1, n + d + 1))
M[0:n, 0:n] = Phi
M = np.zeros((n + d + 1, n + d + 1)) # noqa: N806
M[0:n, 0:n] = phis
M[0:n, n : n + d + 1] = P
M[n : n + d + 1, 0:n] = np.transpose(P)

Expand Down
Loading

0 comments on commit 7568789

Please sign in to comment.