|
1 | 1 | # Developer's Guide |
2 | 2 |
|
3 | | -Depends on Python 3.9+ |
| 3 | +Depends on Python 3.10+ |
4 | 4 |
|
5 | 5 | ## Getting started |
6 | 6 |
|
@@ -28,12 +28,12 @@ Depends on Python 3.9+ |
28 | 28 |
|
29 | 29 | 4. Check or confirm your settings using `git remote -v` |
30 | 30 |
|
31 | | - ```text |
| 31 | +```bash |
32 | 32 | origin git@github.com:[your user name]/DeepForest.git (fetch) |
33 | 33 | origin git@github.com:[your user name]/DeepForest.git (push) |
34 | 34 | upstream https://github.com/weecology/DeepForest.git (fetch) |
35 | 35 | upstream https://github.com/weecology/DeepForest.git (push) |
36 | | - ``` |
| 36 | +``` |
37 | 37 |
|
38 | 38 | 5. Install the package from the main directory. |
39 | 39 |
|
@@ -70,26 +70,85 @@ $ pip install . --upgrade # or python setup.py install |
70 | 70 | $ pytest -v |
71 | 71 | ``` |
72 | 72 |
|
73 | | -### Checking and fixing code style |
| 73 | +### Code Quality and Style |
| 74 | + |
| 75 | +We use [pre-commit](https://pre-commit.com/) to ensure consistent code quality and style across the project. Pre-commit runs automated checks and fixes before each commit to catch issues early. |
| 76 | + |
| 77 | +#### Setting up pre-commit |
| 78 | + |
| 79 | +1. **Install pre-commit** (if not already installed): |
| 80 | + ```bash |
| 81 | + pip install pre-commit |
| 82 | + ``` |
| 83 | + |
| 84 | +2. **Install the pre-commit hooks** in your local repository: |
| 85 | + ```bash |
| 86 | + pre-commit install |
| 87 | + ``` |
| 88 | + |
| 89 | +3. **Run pre-commit on all files** (optional, for initial setup): |
| 90 | + ```bash |
| 91 | + pre-commit run --all-files |
| 92 | + ``` |
| 93 | + |
| 94 | +#### What pre-commit does |
| 95 | + |
| 96 | +Our pre-commit configuration (`.pre-commit-config.yaml`) includes: |
| 97 | + |
| 98 | +- **Code formatting**: Uses [Ruff](https://docs.astral.sh/ruff/) for fast Python linting and formatting |
| 99 | +- **Import sorting**: Automatically sorts and organizes imports |
| 100 | +- **Docstring formatting**: Uses [docformatter](https://github.com/PyCQA/docformatter) to format docstrings |
| 101 | +- **Notebook formatting**: Formats Jupyter notebooks in the documentation |
| 102 | +- **File checks**: Ensures files end with newlines, removes trailing whitespace, checks YAML/JSON syntax |
| 103 | +- **Large file detection**: Prevents accidentally committing large files |
| 104 | + |
| 105 | +#### Running pre-commit manually |
| 106 | + |
| 107 | +You can run pre-commit checks manually at any time: |
| 108 | + |
| 109 | +```bash |
| 110 | +# Run on staged files only |
| 111 | +pre-commit run |
| 112 | +
|
| 113 | +# Run on all files |
| 114 | +pre-commit run --all-files |
| 115 | +
|
| 116 | +# Run a specific hook |
| 117 | +pre-commit run ruff |
| 118 | +``` |
| 119 | + |
| 120 | +#### Fixing pre-commit issues |
74 | 121 |
|
75 | | -#### Using Yapf |
| 122 | +Most pre-commit hooks will automatically fix issues for you. If a hook fails: |
76 | 123 |
|
77 | | -We use [yapf](https://github.com/google/yapf) for code formatting and style checking. |
| 124 | +1. **Check the output** - pre-commit will show you what needs to be fixed |
| 125 | +2. **Re-run the hook** - many hooks can auto-fix issues: |
| 126 | + ```bash |
| 127 | + pre-commit run --all-files |
| 128 | + ``` |
| 129 | +3. **Stage the fixed files** and commit again: |
| 130 | + ```bash |
| 131 | + git add . |
| 132 | + git commit -m "Your commit message" |
| 133 | + ``` |
78 | 134 |
|
79 | | -The easiest way to make sure your code is formatted correctly is to integrate it into your editor. |
80 | | -See [EDITOR SUPPORT](https://github.com/google/yapf/blob/main/EDITOR%20SUPPORT.md). |
| 135 | +#### Bypassing pre-commit (not recommended) |
81 | 136 |
|
82 | | -You can also run yapf from the command line to cleanup the style in your changes: |
| 137 | +If you need to bypass pre-commit for a specific commit (not recommended for regular development): |
83 | 138 |
|
84 | 139 | ```bash |
85 | | -yapf -i --recursive src/deepforest/ --style=.style.yapf |
| 140 | +git commit --no-verify -m "Your commit message" |
86 | 141 | ``` |
87 | 142 |
|
88 | | -If the style tests fail on a pull request, running the above command is the easiest way to fix this. |
| 143 | +#### Editor integration |
| 144 | + |
| 145 | +For the best development experience, consider integrating these tools directly into your editor: |
89 | 146 |
|
90 | | -#### Using pre-commit |
| 147 | +- **VS Code**: Install the Ruff extension for real-time linting and formatting |
| 148 | +- **PyCharm**: Configure Ruff as an external tool |
| 149 | +- **Vim/Neovim**: Use plugins like `nvim-lspconfig` with the Ruff language server |
91 | 150 |
|
92 | | -We configure all our checks using the `.pre-commit-config.yaml` file. To verify your code styling before committing, you should run `pre-commit install` to set up the hooks, followed by `pre-commit run` to execute them. This will apply the formatting rules specified in the `.style.yapf` file. For additional information, please refer to the [pre-commit documentation](https://pre-commit.com/index.html). |
| 151 | +For more information, see the [pre-commit documentation](https://pre-commit.com/). |
93 | 152 |
|
94 | 153 | ## Documentation |
95 | 154 |
|
@@ -175,14 +234,15 @@ The model will be uploaded to [https://huggingface.co/weecology/[model-name]](ht |
175 | 234 |
|
176 | 235 | ### CropModel |
177 | 236 |
|
178 | | -```python``` |
| 237 | +```python |
179 | 238 | from deepforest.model import CropModel |
180 | 239 |
|
181 | 240 | crop_model = CropModel() |
182 | 241 | crop_model.push_to_hub("weecology/cropmodel-deadtrees") |
183 | 242 |
|
184 | 243 | # Reload it later |
185 | 244 | crop_model.from_pretrained("Weecology/cropmodel-deadtrees") |
| 245 | +
|
186 | 246 | ``` |
187 | 247 |
|
188 | 248 | Please name the cropmodel based on what is being classified. |
|
0 commit comments