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

Major Refactor: Add Ruff Linting, Nox, and Pre-Commit Hook #266

Open
wants to merge 119 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
119 commits
Select commit Hold shift + click to select a range
58d7c2b
added ruff and nox.
BalzaniEdoardo Feb 21, 2024
720dcd4
config nox for the py3.12
BalzaniEdoardo Feb 21, 2024
cdda670
add `nox`, `ruff` and a section on managing multiple python versions
BalzaniEdoardo Feb 21, 2024
2b94867
adds src to ruff config
billbrod Feb 28, 2024
d25dea2
updates ruff version (to work with src config)
billbrod Feb 28, 2024
fbb55c9
added ruff and pre-commit configurations
hmd101 Jul 6, 2024
e5adecc
basic precommit confi
hmd101 Jul 6, 2024
870d2d6
ruff linter added to ci
hmd101 Jul 6, 2024
dc40707
formatter added
hmd101 Jul 9, 2024
c802033
default check added to ci
hmd101 Jul 9, 2024
5f61b94
assigned separate github actions to ruff code formatter and linters
hmd101 Jul 9, 2024
b0a392c
test
hmd101 Jul 9, 2024
48a571d
test
hmd101 Jul 9, 2024
4302b55
ruff badge added to readme
hmd101 Jul 10, 2024
8d44d45
noxfile added for automated testing across environments after commits
hmd101 Jul 12, 2024
83a3e41
ruff linters, both ignore and select, specified
hmd101 Jul 12, 2024
1badbad
ruff check runs
hmd101 Jul 12, 2024
3fe5fec
linting merge
hmd101 Jul 18, 2024
0c50992
toml formatting
hmd101 Jul 18, 2024
7f8aec4
added addtional pre-commit hooks
hmd101 Jul 18, 2024
3e78c47
pre-commit edits
hmd101 Jul 18, 2024
089497c
default for max line length is 88
hmd101 Jul 19, 2024
1ae3d11
error in deploy file: pypi environment not defined
hmd101 Jul 19, 2024
1e9fc9c
link changed to previous one in ci.yml
hmd101 Jul 20, 2024
1d8a57f
ruff linting and formatting combined in 1 action in CI and added to n…
hmd101 Aug 4, 2024
e0678b8
ci -- calling ruff directly instead of via action
hmd101 Aug 5, 2024
8272ba2
test
hmd101 Aug 5, 2024
34524f5
.
hmd101 Aug 5, 2024
593fd23
Merge branch 'main' into code-formatting
hmd101 Aug 5, 2024
9b5fb54
null changes in notebook reverted
hmd101 Aug 5, 2024
38a4150
in ci, specifying to use ruff config from pyproject.toml
hmd101 Aug 5, 2024
c1fd8bc
updating some deprecated imports, isinstance for union of types, unso…
hmd101 Aug 8, 2024
4bb4333
notebooks 00-quickstart and 02-eigendistortions line-length correction
hmd101 Aug 8, 2024
7786cad
Revert "notebooks 00-quickstart and 02-eigendistortions line-length c…
hmd101 Aug 8, 2024
79e93d3
Revert "updating some deprecated imports, isinstance for union of typ…
hmd101 Aug 8, 2024
3bb9ce1
additional line length and double quote fixes in 02-eigendistortions.…
hmd101 Aug 8, 2024
633d489
all notebooks in experiments refactored to meet pydocstyle and pyflak…
hmd101 Aug 8, 2024
47acd99
all files in metric refactored to meet pydocstyle and pyflakes criteria
hmd101 Aug 8, 2024
618ef87
all files in simulate and models refactored to meet pydocstyle and p…
hmd101 Aug 8, 2024
43937bc
all files in src refactored to meet pydocstyle and pyflakes criteria
hmd101 Aug 8, 2024
01bbaf9
running pyupgrade linter to upgrade syntac for newer versions
hmd101 Aug 9, 2024
bb714bf
running flake8 simplify on entire codebase
hmd101 Aug 9, 2024
8724a2b
removing # noqa: UP035 tag
hmd101 Aug 9, 2024
fa29088
test coverage session added to noxfile
hmd101 Aug 13, 2024
13f3db2
pytest ini_options adjustments to accomodate module not implemented e…
hmd101 Aug 13, 2024
d3e825c
updating test session in nox file to install all dependencies as list…
hmd101 Aug 13, 2024
4aa2765
pytest can now be run with nox including test coverage
hmd101 Aug 13, 2024
180a17e
resolving some too long lines
hmd101 Aug 14, 2024
5c16f7e
resolving merge conflict
hmd101 Aug 14, 2024
cec2844
too long lines in validate.py corrected
hmd101 Aug 14, 2024
1230ef6
formatting with line-length set to 88
hmd101 Aug 14, 2024
d367c22
too long lines shortened to 88 characters in data and metric
hmd101 Aug 14, 2024
5b76b6a
.
hmd101 Aug 15, 2024
f3eb287
some more fixes
hmd101 Aug 15, 2024
46bc834
removed .keys() from dictionary iteration, replaced if-else with 1-l…
hmd101 Aug 15, 2024
e10f8dd
ignored SIM105 or do we want to use contextlib package instead of try…
hmd101 Sep 13, 2024
d375f32
ambigious variable name in external refactored
hmd101 Sep 17, 2024
07fd3ef
tests test_metric and test_models fail
hmd101 Sep 17, 2024
bda1de7
replacing if-else block with ternary conditional operator
hmd101 Sep 19, 2024
45a9890
removing too long lines
hmd101 Sep 19, 2024
d7fbd6e
replacing if-else block by returning boolean expression directly
hmd101 Sep 19, 2024
e623302
returning boolean expression as opposed to if-if-else block
hmd101 Sep 19, 2024
4d29fd9
replacing nested if-else blocks with elif for readability
hmd101 Sep 19, 2024
4abbc02
too long lines fixed
hmd101 Sep 27, 2024
309e040
in optimizer_step in metamer.py condensed if statements and added che…
hmd101 Sep 27, 2024
302e97e
simplified decision tree conditions in check_convergence
hmd101 Sep 27, 2024
3fba75b
metamers.py refactoring finnished and all tests in test_metamers.py pass
hmd101 Sep 27, 2024
73db7cf
checking if module is available without importing it unnecessarily
hmd101 Sep 27, 2024
0c720a6
ignoring import not being at top of file for fetch.py
hmd101 Sep 27, 2024
d8e1f6a
updating union syntax to python 3.10 bar version, unrelated 420 sha-e…
hmd101 Sep 27, 2024
7d77488
ignoring unused imports linting error F401 in tools init file
hmd101 Sep 27, 2024
8555474
ignoring unused imports linting error F401 in metric init file
hmd101 Sep 27, 2024
bd9b0ec
ignoring wildcard imports linting error F403 in canonical computati…
hmd101 Sep 27, 2024
d1ab319
ignoring wildcard imports linting error F403 and unused imports F401…
hmd101 Sep 27, 2024
d028c67
added predicate ignore-init-module-imports to tool.ruff.lint in pypro…
hmd101 Sep 27, 2024
eabaf30
removed predicate ignore-init-module-imports since deprecated
hmd101 Sep 27, 2024
1952d40
ignoring unused imports F401 and wildcard imports F403
hmd101 Sep 27, 2024
5f54512
ignoring unused imports F401 in synthesize init file
hmd101 Sep 27, 2024
daa38ee
ignoring wildcard imports F401 in simlute init file
hmd101 Sep 27, 2024
1203e89
resolving linting error E402 imports not at top of cell
hmd101 Sep 27, 2024
4e0fa0d
replacing union with pipe operator which resolves UP007 in steerable_…
hmd101 Sep 27, 2024
f08c142
cutting line lenght
hmd101 Sep 27, 2024
a5fb657
making if-blocks more readable and cutting long lines in mad_compteti…
hmd101 Sep 27, 2024
4150226
placing imports to top of cell and shortening too long lines
hmd101 Sep 27, 2024
b2c5a18
fixing too long lines and placing imports at top of cell in notebookd…
hmd101 Sep 27, 2024
718440d
reordering imports and fixing too long lines in notebook geodesics
hmd101 Sep 27, 2024
47aa63d
.
hmd101 Sep 27, 2024
07ff382
shortening lines in eigendistortions.py
hmd101 Sep 27, 2024
12daada
too long lines fixed in notebook metamer-portilla-simoncelli
hmd101 Sep 27, 2024
bd34ed0
too long lines fixed in notebook metamer-portilla-simoncelli
hmd101 Sep 27, 2024
bfda72c
too long lines fixed in notebooks
hmd101 Sep 27, 2024
3db6e8b
too long lines fixed in notebooks
hmd101 Sep 27, 2024
6413588
too long lines fixed in notebooks
hmd101 Sep 27, 2024
bb9c581
too long lines fixed in notebooks
hmd101 Sep 27, 2024
b1e771b
too long lines fixed in notebooks
hmd101 Sep 27, 2024
cca5220
replacing misleading characters l
hmd101 Sep 27, 2024
aec45b7
replacing ambigious l with layer
hmd101 Sep 27, 2024
f47db30
shortening lines
hmd101 Sep 27, 2024
1d34294
replacing ambigious variable names
hmd101 Sep 27, 2024
380112f
.
hmd101 Sep 27, 2024
eb56114
fixing too long lines
hmd101 Sep 27, 2024
e6f10f2
ignoring too long lines in notebook 05 does not work for curl statement
hmd101 Sep 27, 2024
4e62eae
added isort linter and fixed 44 isort errors
hmd101 Sep 27, 2024
1bc6ed1
reformatting
hmd101 Sep 28, 2024
b2c81cd
resolving merge conflict
hmd101 Sep 28, 2024
a4e2f57
tests run again and circular import error resolved
hmd101 Sep 28, 2024
244948d
adding missing import data to init file
hmd101 Sep 28, 2024
9d572e7
fixing linting error
hmd101 Sep 28, 2024
3c1b3db
ruff version updated to 0.6.8, same as on cluster
hmd101 Sep 28, 2024
9975ef0
adding missing import conv to init in tools
hmd101 Sep 28, 2024
a159b1d
formatting fix
hmd101 Sep 28, 2024
5c9e28d
editing string to mitigate notebook error for versions 3.10 and 3.11
hmd101 Sep 28, 2024
6050344
updated 3 cns links from http to https
hmd101 Oct 1, 2024
fb2f5a0
imports sorted in init
hmd101 Oct 1, 2024
3951c79
isort ignored in init file, otherwise errors due to circulr inputs
hmd101 Oct 1, 2024
c2912b0
unused imports uncommented
hmd101 Oct 1, 2024
e13a7ad
when unused imports are removed, tests fail, so included them again
hmd101 Oct 1, 2024
54ebe85
reformatting tests and updating contributing file
hmd101 Oct 1, 2024
a777133
contributing file update completed
hmd101 Oct 1, 2024
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
36 changes: 36 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,40 @@ jobs:
print_all: false
timeout: 5
retry_count: 3
ruff_linting:
runs-on: ubuntu-latest
name: Run Ruff linter
steps:
- uses: actions/checkout@v4
- name: Install Python 3
uses: actions/setup-python@v5
with:
python-version: 3.12
cache: pip
cache-dependency-path: setup.py
- name: Install dependencies
run: |
pip install --upgrade --upgrade-strategy eager .[dev]
- name: Run ruff linter
run: |
ruff check --config=pyproject.toml
ruff_formatting:
runs-on: ubuntu-latest
name: Run Ruff code formatting check
steps:
- uses: actions/checkout@v4
- name: Install Python 3
uses: actions/setup-python@v5
with:
python-version: 3.12
cache: pip
cache-dependency-path: setup.py
- name: Install dependencies
run: |
pip install --upgrade --upgrade-strategy eager .[dev]
- name: Run ruff formatter
run: |
ruff format --check --config=pyproject.toml

check:
if: always()
Expand All @@ -130,6 +164,8 @@ jobs:
- all_tutorials_in_docs
- no_extra_nblinks
- check_urls
- ruff_linting
- ruff_formatting
runs-on: ubuntu-latest
steps:
- name: Decide whether all tests and notebooks succeeded
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ jobs:
publish:
name: Upload release to Test PyPI
needs: [build]
environment: pypi
runs-on: ubuntu-latest
environment: pypi
permissions:
Copy link
Collaborator

Choose a reason for hiding this comment

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

why this change? did this come from one of the linters or elsewhere?

id-token: write # IMPORTANT: this permission is mandatory for trusted publishing
steps:
Expand Down
31 changes: 31 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.8
hooks:
# Run the formatter.
- id: ruff-format
args: [--config=pyproject.toml]
# Run the linter.
- id: ruff
args: [--config=pyproject.toml]


- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.3.0
# note: pre-commit runs top-to-bottom, so put the hooks that modify content first,
# followed by checks that might be more likely to pass after the modifactaion hooks (like flake8)
hooks:
# Checks for large files added to the repository, typically to prevent accidental inclusion of large binaries or datasets.
- id: check-added-large-files
# Detects potential filename conflicts due to case-insensitive filesystems (e.g., Windows) where File.txt and file.txt would be considered the same.
- id: check-case-conflict
# Checks for files that contain merge conflict strings (e.g., <<<<<<<, =======, >>>>>>>).
- id: check-merge-conflict
# Validates YAML files for syntax errors.
- id: check-yaml
# Detects debug statments (e.g., print, console.log, etc.) left in code.
- id: debug-statements
# Ensures files have a newline at the end.
- id: end-of-file-fixer
# Removes trailing whitespace characters from files.
- id: trailing-whitespace
195 changes: 185 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ git pull upstream main
# update your fork's main branch with any changes from upstream
git push origin main
# create and switch to the branch
git checkout -b my_cool_branch
git checkout -b my_cool_branch
```

Then, create new changes on this branch and, when you're ready, add and commit them:
Expand All @@ -109,24 +109,71 @@ Then, create new changes on this branch and, when you're ready, add and commit t
# stage the changes
git add src/plenoptic/the_file_you_changed.py
# commit your changes
git commit -m "A helpful message explaining my changes"
git commit -m "A helpful message explaining my changes"
# push to the origin remote
git push origin my_cool_branch
git push origin my_cool_branch
```

If you aren't comfortable with `git add`, `git commit`, `git push`, I recommend the [Software Carpentry git lesson](https://swcarpentry.github.io/git-novice/).

#### Contributing your change back to plenoptic

You can make any number of changes on your branch. Once you're happy with your changes, [add tests](#adding-tests) to check that they run correctly and [add documentation](#adding-documentation), then make sure the existing [tests](#testing) all run successfully, that your branch is up-to-date with main, and then open a pull request by clicking on the big `Compare & pull request` button that appears at the top of your fork after pushing to your branch (see [here](https://intersect-training.org/collaborative-git/03-pr/index.html) for a tutorial).
You can make any number of changes on your branch. Once you're happy with your changes, [add tests](#adding-tests) to check that they run correctly and [add documentation](#adding-documentation), then make sure the existing [tests](#testing) all run successfully, that your branch is up-to-date with main, and then open a pull request by clicking on the big `Compare & pull request` button that appears at the top of your fork after pushing to your branch (see [here](https://intersect-training.org/collaborative-git/03-pr/index.html) for a tutorial).

Your pull request should include information on what you changed and why, referencing any relevant issues or discussions, and highlighting any portion of your changes where you have lingering questions (e.g., "was this the right way to implement this?") or want reviewers to pay special attention. You can look at previous closed pull requests to see what this looks like.

At this point, we will be notified of the pull request and will read it over. We will try to give an initial response quickly, and then do a longer in-depth review, at which point you will probably need to respond to our comments, making changes as appropriate. We'll then respond again, and proceed in an iterative fashion until everyone is happy with the proposed changes. This process can take a while! (The more focused your pull request, the less time it will take.)

If your changes are integrated, you will be added as a Github contributor and as one of the authors of the package. Thank you for being part of `plenoptic`!

### Style guide
### Code Quality and Linting
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
### Code Quality and Linting
### Code Style and Linting

We use [Ruff](https://docs.astral.sh/ruff/) for linting and formatting our Python code to maintain a consistent code style and catch potential errors early. To ensure your contributions meet these standards, please follow the guidelines below:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
We use [Ruff](https://docs.astral.sh/ruff/) for linting and formatting our Python code to maintain a consistent code style and catch potential errors early. To ensure your contributions meet these standards, please follow the guidelines below:
We use [Ruff](https://docs.astral.sh/ruff/) for linting and formatting our Python code to maintain a consistent code style and catch potential errors early. We run ruff as part of our CI and non-compliant code will not be merged!


#### Using Ruff

Ruff is a fast and comprehensive Python formatter and linter that checks for common style and code quality issues. It combines multiple tools, like Pyflakes, pycodestyle, isort, and other linting rules into one efficient tool, which are specified in `pyproject.toml`. Before submitting your code, make sure to run Ruff to catch any issues.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Ruff is a fast and comprehensive Python formatter and linter that checks for common style and code quality issues. It combines multiple tools, like Pyflakes, pycodestyle, isort, and other linting rules into one efficient tool, which are specified in `pyproject.toml`. Before submitting your code, make sure to run Ruff to catch any issues.
Ruff is a fast and comprehensive Python formatter and linter that checks for common style and code quality issues. It combines multiple tools, like black, Pyflakes, pycodestyle, isort, and other linting rules into one efficient tool, which are specified in `pyproject.toml`. Before submitting your code, make sure to run Ruff to catch any issues. See other sections of this document for how to use `nox` and `pre-commit` to simplify this process.


**Using Ruff for [Formatting](https://docs.astral.sh/ruff/formatter/#philosophy):**
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
**Using Ruff for [Formatting](https://docs.astral.sh/ruff/formatter/#philosophy):**
Ruff has two components, a [formatter](https://docs.astral.sh/ruff/formatter/) and a [linter](https://docs.astral.sh/ruff/linter/). Formatters and linters are both static analysis tools, but formatters "quickly check and reformat your code for stylistic consistency without changing the runtime behavior of the code", while linters "detect not just stylistic inconsistency but also potential logical bugs, and often suggest code fixes" (per [GitHub's readme project](https://github.com/readme/guides/formatters-linters-compilers)). There are many choices of formatters and linters in python; ruff aims to combine the features of many of them while being very fast.
For both the formatter and the linter, you can run ruff without any additional arguments; our configuration option are stored in the `pyproject.toml` file and so don't need to be specified explicitly.
##### Formatting:


`ruff format` is the primary entrypoint to the formatter. It accepts a list of files or directories, and formats all discovered Python files:
```bash
ruff format # Format all files in the current directory.
ruff format path/to/code/ # Format all files in `path/to/code` (and any subdirectories).
ruff format path/to/file.py # Format a single file.
```
For the full list of supported options, run `ruff format --help`.

**Using Ruff for [Linting](https://docs.astral.sh/ruff/linter/):**
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
**Using Ruff for [Linting](https://docs.astral.sh/ruff/linter/):**
##### Using Ruff for Linting:


To run Ruff on your code:
```bash
ruff check .
```
It'll then tell you which lines are violating linting rules and may suggest that some errors are automatically fixable.

To automatically fix lintint errors, run:

```bash
ruff --fix .
```

Be careful with **unsafe fixes**, safe fixes are symbolized with the tools emoji and are listed [here](https://docs.astral.sh/ruff/rules/)!

#### Ignoring Ruff Linting
You may want to suppress lint errors, for example when too long lines (code `E501`) are desired because otherwise the url might not be readable anymore.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
You may want to suppress lint errors, for example when too long lines (code `E501`) are desired because otherwise the url might not be readable anymore.
In some cases, it may be acceptable to suppress lint errors, for example when too long lines (code `E501`) are desired because otherwise the url might not be readable anymore. These ignores will be evaluated on a case-by-case basis.

I'm thinking it might be helpful to maintain a list of what we consider acceptable uses of ignore here. Currently you've documented them in this PR, but should they live in this file or on some dedicated issue? or on the discussions board?

You can do this by adding the following to the end of the line:

```bash
# noqa: E501
```
If you want to suppress an error across an entire file, do this:
```bash
# ruff: noqa: E501
```
Comment on lines +166 to +172
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you put some example python code in both of these cases? for example, do these go above the relevant line, on the same line, etc.


For more details, refer to the [documentation](https://docs.astral.sh/ruff/linter/#error-suppression).

#### General Style Guide Recommendations:

- Longer, descriptive names are preferred (e.g., `x` is not an appropriate name
for a variable), especially for anything user-facing, such as methods,
Expand All @@ -135,6 +182,17 @@ If your changes are integrated, you will be added as a Github contributor and as
(see [below](#docstrings) for details). Hidden ones do not *need* to have
complete docstrings, but they probably should.

#### Pre-Commit Hooks: Identifying simple issues before submission to code review (and how to ignore those)
Pre-commit hooks are useful for the developer to check if all the linting and formatting rules (see Ruff above) are honored _before_ committing. That is, when you commit, pre-commit hooks are run and auto-fixed where applicable (e.g., trailing whitespace). You then need to add _again_ if you want these changes to be included in your commit.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Pre-commit hooks are useful for the developer to check if all the linting and formatting rules (see Ruff above) are honored _before_ committing. That is, when you commit, pre-commit hooks are run and auto-fixed where applicable (e.g., trailing whitespace). You then need to add _again_ if you want these changes to be included in your commit.
Pre-commit hooks are useful for the developer to check if all the linting and formatting rules (see Ruff above) are honored _before_ committing. That is, when you commit, pre-commit hooks are run and auto-fixed where possible (e.g., trailing whitespace). You then need to add _again_ if you want these changes to be included in your commit. If the problem is not automatically fixable, you will need to manually update your code before you are able to commit.


Should you want to ignore pre-commit hooks, you can add `--no-verify` to your commit message like this:
```bash
git commit -m <my commit message> --no-verify
```
Comment on lines +188 to +191
Copy link
Collaborator

Choose a reason for hiding this comment

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

what's the use case here? pre-commit is optional anyway, but if people have installed it, why would they ignore it?


All of the above only applies, if you have the pre-commit package manager installed using
`pip install pre-commit`.
Comment on lines +193 to +194
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of this line, can we have the setup instructions (and link) for pre-commit? The user will need to install pre-commit in the relevant environment and then run pre-commit install right? So this should look like (I can't get the suggestion to render correctly with the code-block inside the suggestion, so this will need to be edited manually; the last line about pre-commit docs is supposed to be included in the suggestion):

Suggested change
All of the above only applies, if you have the pre-commit package manager installed using
`pip install pre-commit`.
In order to use pre-commit, you must install the `pre-commit` package into your development environment, and then install the hooks:
```bash
pip install pre-commit
pre-commit install

See pre-commit docs for more details.


### Adding models or synthesis methods

In addition to the above, see the documentation for a description of
Expand Down Expand Up @@ -199,7 +257,124 @@ several choices for how to run a subset of the tests:
View the [pytest documentation](https://doc.pytest.org/en/latest/usage.html) for
more info.

### Adding tests
### Using nox to simplify testing and linting
This section is optional but if you want to easily run tests in an isolated environment
using the [nox](https://nox.thea.codes/en/stable/) command-line tool.

`nox` is installed automatically as a `[dev]` dependency of plenoptic.

To run all tests and linters through `nox`, from the root folder of the
plenoptic package, execute the following command,

```bash
nox
```

`nox` will read the configuration from the `noxfile.py` script.

If you want to run just the tests, add the following option,

```bash
nox -s tests
```

for running only the linters,

```bash
nox -s linters
```

and for testing only the coverage, run:

```bash
nox -s coverage
```
Comment on lines +277 to +291
Copy link
Collaborator

Choose a reason for hiding this comment

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

instead of this, I think we should tell them to run nox -l to see the possible sessions, so we don't have to try and keep this up-to-date.


`nox` offers a variety of configuration options, you can learn more about it from their
[documentation](https://nox.thea.codes/en/stable/config.html).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Note that nox works particularly well with pyenv, discussed later in this file, which makes it easy to install the multiple python versions used in testing.

#### Multi-python version testing with pyenv
Sometimes, before opening a pull-request that will trigger the `.github/workflow/ci.yml` continuous
integration workflow, you may want to test your changes over all the supported python versions locally.

Handling multiple installed python versions on the same machine can be challenging and confusing.
[`pyenv`](https://github.com/pyenv/pyenv) is a great tool that really comes to the rescue.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
[`pyenv`](https://github.com/pyenv/pyenv) is a great tool that really comes to the rescue.
[`pyenv`](https://github.com/pyenv/pyenv) is a great tool that really comes to the rescue. Note that `pyenv` just handles python versions --- virtual environments have to be handled separately, using [`pyenv-virtualenv`](https://github.com/pyenv/pyenv-virtualenv)!


This tool doesn't come with the package dependencies and has to be installed separately. Installation instructions
are system specific but the package readme is very details, see
[here](https://github.com/pyenv/pyenv?tab=readme-ov-file#installation).

Follow carefully the instructions to configure pyenv after installation.

Once you have tha package installed and configured, you can install multiple python version through it.
First get a list of the available versions with the command,

```bash
pyenv install -l
```

Install the python version you need. For this example, let's assume we want `python 3.10.11` and `python 3.11.8`,

```bash
pyenv install 3.10.11
pyenv install 3.11.8
```

You can check which python version is currently set as default, by typing,

```bash
pyenv which python
```

And you can list all available versions of python with,

```bash
pyenv versions
```
If you want to run `nox` on multiple python versions, all you need to do is:

1. Set your desired versions as `global`.
```bash
pyenv global 3.11.8 3.10.11
```
This will make both version available, and the default python will be set to the first one listed
(`3.11.8` in this case).
2. Run nox specifying the python version as an option.
```bash
nox -p 3.10
```

Note that `noxfile.py` lists the available option as keyword arguments in a session specific manner.

If you have multiple python version installed, we recommend to manage your virtual environments
through `pyenv`. For that you'll need to install the extension
[`pyenv-virtualenv`](https://github.com/pyenv/pyenv-virtualenv).
Comment on lines +349 to +351
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
If you have multiple python version installed, we recommend to manage your virtual environments
through `pyenv`. For that you'll need to install the extension
[`pyenv-virtualenv`](https://github.com/pyenv/pyenv-virtualenv).
As mentioned earlier, if you have multiple python version installed, we recommend you manage your virtual environments through `pyenv` using the [`pyenv-virtualenv`](https://github.com/pyenv/pyenv-virtualenv) extension.


This tool works with most of the environment managers including (`venv` and `conda`).
Creating an environment with it is as simple as calling,

```bash
pyenv virtualenv my-python my-enviroment
```

Here, `my-python` is the python version, one between `pyenv versions`, and `my-environment` is your
new environment name.

If `my-python` has `conda` installed, it will create a conda environment, if not, it will use `venv`.

You can list the virtual environment only with,

```bash
pyenv virtualenvs
```

And you can uninstall an environment with,

```bash
pyenv uninstall my-environment
```

### Adding tests

New tests can be added in any of the existing `tests/test_*.py` scripts. Tests
should be functions, contained within classes. The class contains a bunch of
Expand Down Expand Up @@ -261,7 +436,7 @@ metamer instances run for. We do this using
`vgg16_synth_max_iter 10`; note that you need a `-p` for each parameter and
you should change nothing else about that line). See the block with `if: ${{
matrix.notebook == 'examples/Demo_Eigendistortion.ipynb' }}` for an example.

A similar procedure could be used to reduce the size of an image or other steps
that could similarly reduce the total time necessary to run a notebook.

Expand Down Expand Up @@ -395,7 +570,7 @@ header (you can have as many sub-headers as you'd like).

You should [build the docs yourself](#build-the-documentation) to ensure it
looks correct before pushing.

#### Images and plots

You can include images in `.rst` files in the documentation as well. Simply
Expand All @@ -405,9 +580,9 @@ place them in the `docs/images/` folder and use the `figure` role, e.g.,:
.. figure:: images/path_to_my_image.svg
:figwidth: 100%
:alt: Alt-text describing my image.

Caption describing my image.

```

To refer to it directly, you may want to use the [numref
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
[![codecov](https://codecov.io/gh/plenoptic-org/plenoptic/branch/main/graph/badge.svg?token=EDtl5kqXKA)](https://codecov.io/gh/plenoptic-org/plenoptic)
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/plenoptic-org/plenoptic/1.1.0?filepath=examples)
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![Code style: Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/format.json)](https://github.com/astral-sh/ruff)

![](docs/images/plenoptic_logo_wide.svg)

Expand Down
Loading
Loading