Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ruff NPY002 #162

Merged
merged 3 commits into from
Jun 22, 2024
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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ default_install_hook_types: [pre-commit, commit-msg]

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.9
rev: v0.4.10
hooks:
- id: ruff
args: [--fix]
Expand Down Expand Up @@ -87,6 +87,6 @@ repos:
- typescript-eslint

- repo: https://github.com/RobertCraigie/pyright-python
rev: v1.1.367
rev: v1.1.368
hooks:
- id: pyright
5 changes: 3 additions & 2 deletions examples/make_assets/correlation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
# i.e. the correlation matrix contains no significant correlations
# beyond the spurious correlation that occurs randomly
n_rows, n_cols = 500, 1000
rand_wide_mat = np.random.normal(0, 1, size=(n_rows, n_cols))
np_rng = np.random.default_rng(seed=0)
rand_wide_mat = np_rng.normal(0, 1, size=(n_rows, n_cols))

corr_mat = np.corrcoef(rand_wide_mat)

Expand All @@ -29,7 +30,7 @@

# Plot eigenvalue distribution of a rank-deficient correlation matrix
n_rows, n_cols = 600, 500
rand_tall_mat = np.random.normal(0, 1, size=(n_rows, n_cols))
rand_tall_mat = np_rng.normal(0, 1, size=(n_rows, n_cols))

corr_mat_rank_deficient = np.corrcoef(rand_tall_mat)

Expand Down
7 changes: 4 additions & 3 deletions examples/make_assets/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@

# Random regression data
rand_regression_size = 500
y_true = np.random.normal(5, 4, rand_regression_size)
y_pred = 1.2 * y_true - 2 * np.random.normal(0, 1, rand_regression_size)
y_std = (y_true - y_pred) * 10 * np.random.normal(0, 0.1, rand_regression_size)
np_rng = np.random.default_rng(seed=0)
y_true = np_rng.normal(5, 4, rand_regression_size)
y_pred = 1.2 * y_true - 2 * np_rng.normal(0, 1, rand_regression_size)
y_std = (y_true - y_pred) * 10 * np_rng.normal(0, 0.1, rand_regression_size)


# %% Histogram Plots
Expand Down
17 changes: 8 additions & 9 deletions examples/make_assets/ptable.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@


# %%
np_rng = np.random.default_rng(seed=0)
df_expt_gap = load_dataset("matbench_expt_gap")
df_steels = load_dataset("matbench_steels")

Expand Down Expand Up @@ -86,9 +87,7 @@

# %% Histograms laid out in as a periodic table
# Generate random parity data with y \approx x with some noise
data_dict = {
elem.symbol: np.random.randn(100) + np.random.randn(100) for elem in Element
}
data_dict = {elem.symbol: np_rng.randn(100) + np_rng.randn(100) for elem in Element}
Copy link
Collaborator

@DanielYang59 DanielYang59 Jun 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janosh It's so interesting, as I started working on fixing NPY002 violations in pymatgen materialsproject/pymatgen#3892 without looking at this PR (really!) almost at the same time.

But this legacy to new Generator replacement is not just replacing np.random with rng (the API is not exactly the same, please refer to https://numpy.org/doc/stable/reference/random/generator.html), this should be np_rng.standard_normal(100) instead.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch! thanks for letting me know. i should really take the time to make these scripts run in CI to avoid such mistakes...

will submit a fix now

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem at all :) Yes we need to include them into CI tests, mind letting me (learn and) do this?

fig = ptable_hists(
data_dict,
colormap="coolwarm",
Expand All @@ -104,9 +103,9 @@
# %% Scatter plots laid out as a periodic table
data_dict = { # random parity data with y = x + noise
elem.symbol: [
np.arange(10) + np.random.normal(0, 1, 10),
np.arange(10) + np.random.normal(0, 1, 10),
np.arange(10) + np.random.normal(0, 1, 10),
np.arange(10) + np_rng.normal(0, 1, 10),
np.arange(10) + np_rng.normal(0, 1, 10),
np.arange(10) + np_rng.normal(0, 1, 10),
]
for elem in Element
}
Expand All @@ -126,8 +125,8 @@
data_dict = { # random parabola data with y = x^2 + noise
elem.symbol: [
np.arange(10),
(np.arange(10) - 4) ** 2 + np.random.normal(0, 1, 10),
np.arange(10) + np.random.normal(0, 10, 10),
(np.arange(10) - 4) ** 2 + np_rng.normal(0, 1, 10),
np.arange(10) + np_rng.normal(0, 10, 10),
]
for elem in Element
}
Expand All @@ -148,7 +147,7 @@
data_dict = {
elem.symbol: [
np.linspace(0, 10, 10),
np.sin(2 * np.pi * np.linspace(0, 10, 10)) + np.random.normal(0, 0.2, 10),
np.sin(2 * np.pi * np.linspace(0, 10, 10)) + np_rng.normal(0, 0.2, 10),
]
for elem in Element
}
Expand Down
8 changes: 3 additions & 5 deletions examples/make_assets/relevance.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@


# Random classification data
np.random.seed(42)
np_rng = np.random.default_rng(seed=0)
rand_clf_size = 100
y_binary = np.random.choice([0, 1], size=rand_clf_size)
y_proba = np.clip(
y_binary - 0.1 * np.random.normal(scale=5, size=rand_clf_size), 0.2, 0.9
)
y_binary = np_rng.choice([0, 1], size=rand_clf_size)
y_proba = np.clip(y_binary - 0.1 * np_rng.normal(scale=5, size=rand_clf_size), 0.2, 0.9)


# %% Relevance Plots
Expand Down
5 changes: 3 additions & 2 deletions examples/make_assets/sankey.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@

# %% Sankey diagram of random integers
cols = ["col_a", "col_b"]
df_rand_ints = pd.DataFrame(np.random.randint(1, 6, size=(100, 2)), columns=cols)
np_rng = np.random.default_rng(seed=0)
df_rand_ints = pd.DataFrame(np_rng.randint(1, 6, size=(100, 2)), columns=cols)
Copy link
Collaborator

@DanielYang59 DanielYang59 Jun 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be np_rng.integers().

fig = sankey_from_2_df_cols(df_rand_ints, cols, labels_with_counts="percent")
rand_int_title = "Two sets of 100 random integers from 1 to 5"
fig.update_layout(title=dict(text=rand_int_title, x=0.5, y=0.87))
code_anno = dict(
x=0.5,
y=-0.2,
text="<span style='font-family: monospace;'>df = pd.DataFrame("
"np.random.randint(1, 6, size=(100, 2)), columns=['col_a','col_b'])<br>"
"np_rng.randint(1, 6, size=(100, 2)), columns=['col_a','col_b'])<br>"
"fig = sankey_from_2_df_cols(df, df.columns)</span>",
font_size=12,
showarrow=False,
Expand Down
7 changes: 4 additions & 3 deletions examples/make_assets/scatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@

# Random regression data
rand_regression_size = 500
y_true = np.random.normal(5, 4, rand_regression_size)
y_pred = 1.2 * y_true - 2 * np.random.normal(0, 1, rand_regression_size)
y_std = (y_true - y_pred) * 10 * np.random.normal(0, 0.1, rand_regression_size)
np_rng = np.random.default_rng(seed=0)
y_true = np_rng.normal(5, 4, rand_regression_size)
y_pred = 1.2 * y_true - 2 * np_rng.normal(0, 1, rand_regression_size)
y_std = (y_true - y_pred) * 10 * np_rng.normal(0, 0.1, rand_regression_size)


# %% density scatter plotly
Expand Down
9 changes: 5 additions & 4 deletions examples/make_assets/uncertainty.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

# %% Random regression data
rand_regression_size = 500
y_true = np.random.normal(5, 4, rand_regression_size)
y_pred = 1.2 * y_true - 2 * np.random.normal(0, 1, rand_regression_size)
y_std = (y_true - y_pred) * 10 * np.random.normal(0, 0.1, rand_regression_size)
np_rng = np.random.default_rng(seed=0)
y_true = np_rng.normal(5, 4, rand_regression_size)
y_pred = 1.2 * y_true - 2 * np_rng.normal(0, 1, rand_regression_size)
y_std = (y_true - y_pred) * 10 * np_rng.normal(0, 0.1, rand_regression_size)


# %% Uncertainty Plots
Expand All @@ -27,7 +28,7 @@
ax = error_decay_with_uncert(y_true, y_pred, y_std)
save_and_compress_svg(ax, "error-decay-with-uncert")

eps = 0.2 * np.random.randn(*y_std.shape)
eps = 0.2 * np_rng.randn(*y_std.shape)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, should be rng.standard_normal()


ax = error_decay_with_uncert(y_true, y_pred, {"better": y_std, "worse": y_std + eps})
save_and_compress_svg(ax, "error-decay-with-uncert-multiple")
Expand Down
8 changes: 4 additions & 4 deletions pymatviz/uncertainty.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ def get_err_decay(
decay_by_err = np.sort(abs_err).cumsum() / n_inc

# error decay for random exclusion of samples
ae_tile = np.tile(abs_err, [n_rand, 1])
abs_err_tile = np.tile(abs_err, [n_rand, 1])

for row in ae_tile:
np.random.shuffle(row) # shuffle rows of ae_tile in place
for row in abs_err_tile:
np.random.default_rng(seed=0).shuffle(row) # shuffle rows of ae_tile in place

rand = ae_tile.cumsum(1) / n_inc
rand = abs_err_tile.cumsum(1) / n_inc

return decay_by_err, rand.std(0)

Expand Down
19 changes: 9 additions & 10 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,32 +84,31 @@ target-version = "py39"
[tool.ruff.lint]
select = ["ALL"]
ignore = [
"ANN101",
"ANN102",
"ANN401",
"ANN101", # missing-type-self
"ANN102", # missing-type-cls
"ANN401", # any-type
"C408", # unnecessary-collection-call
"C901",
"C901", # complex-structure
"COM812", # trailing comma missing
"D205", # 1 blank line required between summary line and description
"E731", # do not assign a lambda expression, use a def
"EM101",
"EM102",
"ERA001",
"EM101", # raw-string-in-exception
"EM102", # f-string-in-exception
"ERA001", # dead code
"FIX002",
"ISC001",
"N806", # non-lowercase-variable-in-function
"NPY002",
"PLR0911", # too-many-return-statements
"PLR0912", # too-many-branches
"PLR0913", # function-with-too-many-arguments
"PLR0915", # too-many-statements
"PLR2004", # magic-number-comparison
"PT006", # pytest-parametrize-names-wrong-type
"PTH",
"PTH", # prefer pathlib over os.path
"S311",
"SIM105", # Use contextlib.suppress() instead of try-except-pass
"TD",
"TRY003",
"TRY003", # raise-vanilla-args
]
pydocstyle.convention = "google"
isort.lines-after-imports = 2
Expand Down
42 changes: 0 additions & 42 deletions site/src/routes/[slug]/+page.server.ts

This file was deleted.

12 changes: 0 additions & 12 deletions site/src/routes/[slug]/+page.svelte

This file was deleted.

12 changes: 0 additions & 12 deletions site/src/routes/[slug]/histograms/1.elements-hist.py

This file was deleted.

13 changes: 0 additions & 13 deletions site/src/routes/[slug]/parity/1.density-scatter.py

This file was deleted.

15 changes: 0 additions & 15 deletions site/src/routes/[slug]/parity/2.density-scatter-with-hist.py

This file was deleted.

14 changes: 0 additions & 14 deletions site/src/routes/[slug]/parity/5.scatter-with-err-bar.py

This file was deleted.

14 changes: 0 additions & 14 deletions site/src/routes/[slug]/parity/6.scatter-with-err-bar.py

This file was deleted.

13 changes: 0 additions & 13 deletions site/src/routes/[slug]/parity/_3.density-scatter-hex.py

This file was deleted.

14 changes: 0 additions & 14 deletions site/src/routes/[slug]/parity/_4.density-scatter-hex-with-hist.py

This file was deleted.

Loading