classcounter 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- classcounter-0.1.0/.gitignore +208 -0
- classcounter-0.1.0/CHANGELOG.md +15 -0
- classcounter-0.1.0/Examples.ipynb +382 -0
- classcounter-0.1.0/LICENSE +21 -0
- classcounter-0.1.0/PKG-INFO +198 -0
- classcounter-0.1.0/README.md +160 -0
- classcounter-0.1.0/benchmark_backends.ipynb +259 -0
- classcounter-0.1.0/classcounter/__init__.py +6 -0
- classcounter-0.1.0/classcounter/_numba.py +25 -0
- classcounter-0.1.0/classcounter/core.py +284 -0
- classcounter-0.1.0/classcounter/py.typed +0 -0
- classcounter-0.1.0/images/Backend comparison.png +0 -0
- classcounter-0.1.0/pyproject.toml +79 -0
- classcounter-0.1.0/tests/__init__.py +0 -0
- classcounter-0.1.0/tests/conftest.py +89 -0
- classcounter-0.1.0/tests/install/test_all_extras.py +90 -0
- classcounter-0.1.0/tests/install/test_base_only.py +38 -0
- classcounter-0.1.0/tests/install/test_geo_only.py +94 -0
- classcounter-0.1.0/tests/install/test_numba_only.py +67 -0
- classcounter-0.1.0/tests/install/test_torch_only.py +63 -0
- classcounter-0.1.0/tests/test_backend_consistency.py +75 -0
- classcounter-0.1.0/tests/test_count_classes.py +25 -0
- classcounter-0.1.0/tests/test_edge_cases.py +76 -0
- classcounter-0.1.0/tests/test_errors.py +72 -0
- classcounter-0.1.0/tests/test_install.py +49 -0
- classcounter-0.1.0/tests/test_names_mapping.py +97 -0
- classcounter-0.1.0/tests/test_numba_backend.py +46 -0
- classcounter-0.1.0/tests/test_numpy_backend.py +50 -0
- classcounter-0.1.0/tests/test_percent.py +46 -0
- classcounter-0.1.0/tests/test_raster.py +232 -0
- classcounter-0.1.0/tests/test_torch_backend.py +62 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
.python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
#poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
#pdm.lock
|
|
116
|
+
#pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
#pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.envrc
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
#.idea/
|
|
177
|
+
|
|
178
|
+
# Abstra
|
|
179
|
+
# Abstra is an AI-powered process automation framework.
|
|
180
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
181
|
+
# Learn more at https://abstra.io/docs
|
|
182
|
+
.abstra/
|
|
183
|
+
|
|
184
|
+
# Visual Studio Code
|
|
185
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
186
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
188
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
189
|
+
# .vscode/
|
|
190
|
+
|
|
191
|
+
# Ruff stuff:
|
|
192
|
+
.ruff_cache/
|
|
193
|
+
|
|
194
|
+
# PyPI configuration file
|
|
195
|
+
.pypirc
|
|
196
|
+
|
|
197
|
+
# Cursor
|
|
198
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
199
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
200
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
201
|
+
.cursorignore
|
|
202
|
+
.cursorindexingignore
|
|
203
|
+
|
|
204
|
+
# Marimo
|
|
205
|
+
marimo/_static/
|
|
206
|
+
marimo/_lsp/
|
|
207
|
+
__marimo__/
|
|
208
|
+
uv.lock
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to ClassCounter are documented in this file.
|
|
4
|
+
|
|
5
|
+
## [0.1.0] - 2026-02-23
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
- Initial public release
|
|
9
|
+
- NumPy, Numba, and PyTorch backends for fast class counting
|
|
10
|
+
- GeoTIFF support via rasterio
|
|
11
|
+
- Name mapping for readable output
|
|
12
|
+
- Percentage calculation mode
|
|
13
|
+
- Automatic backend selection based on input type
|
|
14
|
+
- GPU acceleration (CUDA) via PyTorch
|
|
15
|
+
- Full test suite
|
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
{
|
|
2
|
+
"cells": [
|
|
3
|
+
{
|
|
4
|
+
"cell_type": "markdown",
|
|
5
|
+
"metadata": {},
|
|
6
|
+
"source": [
|
|
7
|
+
"# ClassCounter Examples\n",
|
|
8
|
+
"\n",
|
|
9
|
+
"This notebook demonstrates how to use `classcounter` to count occurrences of class values in arrays and tensors."
|
|
10
|
+
]
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
"cell_type": "code",
|
|
14
|
+
"execution_count": 1,
|
|
15
|
+
"metadata": {},
|
|
16
|
+
"outputs": [],
|
|
17
|
+
"source": [
|
|
18
|
+
"import numpy as np\n",
|
|
19
|
+
"\n",
|
|
20
|
+
"from classcounter import count_classes"
|
|
21
|
+
]
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
"cell_type": "markdown",
|
|
25
|
+
"metadata": {},
|
|
26
|
+
"source": [
|
|
27
|
+
"## Basic Usage\n",
|
|
28
|
+
"\n",
|
|
29
|
+
"Pass a numpy array to `count_classes` and get back a dict mapping each unique value to its count."
|
|
30
|
+
]
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"cell_type": "code",
|
|
34
|
+
"execution_count": 2,
|
|
35
|
+
"metadata": {},
|
|
36
|
+
"outputs": [
|
|
37
|
+
{
|
|
38
|
+
"data": {
|
|
39
|
+
"text/plain": [
|
|
40
|
+
"{0: 2022, 1: 1993, 2: 2077, 3: 1967, 4: 1941}"
|
|
41
|
+
]
|
|
42
|
+
},
|
|
43
|
+
"execution_count": 2,
|
|
44
|
+
"metadata": {},
|
|
45
|
+
"output_type": "execute_result"
|
|
46
|
+
}
|
|
47
|
+
],
|
|
48
|
+
"source": [
|
|
49
|
+
"rng = np.random.default_rng(42)\n",
|
|
50
|
+
"arr = rng.integers(0, 5, size=(100, 100), dtype=np.int32)\n",
|
|
51
|
+
"\n",
|
|
52
|
+
"count_classes(arr)"
|
|
53
|
+
]
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
"cell_type": "markdown",
|
|
57
|
+
"metadata": {},
|
|
58
|
+
"source": [
|
|
59
|
+
"## Name Mapping\n",
|
|
60
|
+
"\n",
|
|
61
|
+
"Use the `names` parameter to map integer class IDs to human-readable labels. Unmapped classes are preserved with their integer key (a warning is emitted)."
|
|
62
|
+
]
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
"cell_type": "code",
|
|
66
|
+
"execution_count": 3,
|
|
67
|
+
"metadata": {},
|
|
68
|
+
"outputs": [
|
|
69
|
+
{
|
|
70
|
+
"data": {
|
|
71
|
+
"text/plain": [
|
|
72
|
+
"{'water': 2022, 'forest': 1993, 'urban': 2077, 'crop': 1967, 'bare': 1941}"
|
|
73
|
+
]
|
|
74
|
+
},
|
|
75
|
+
"execution_count": 3,
|
|
76
|
+
"metadata": {},
|
|
77
|
+
"output_type": "execute_result"
|
|
78
|
+
}
|
|
79
|
+
],
|
|
80
|
+
"source": [
|
|
81
|
+
"names = {0: \"water\", 1: \"forest\", 2: \"urban\", 3: \"crop\", 4: \"bare\"}\n",
|
|
82
|
+
"\n",
|
|
83
|
+
"count_classes(arr, names=names)"
|
|
84
|
+
]
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
"cell_type": "markdown",
|
|
88
|
+
"metadata": {},
|
|
89
|
+
"source": [
|
|
90
|
+
"## Percentage Output\n",
|
|
91
|
+
"\n",
|
|
92
|
+
"Set `percent=True` to get results as percentages (0–100) instead of raw counts. This combines with `names` too."
|
|
93
|
+
]
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
"cell_type": "code",
|
|
97
|
+
"execution_count": 4,
|
|
98
|
+
"metadata": {},
|
|
99
|
+
"outputs": [
|
|
100
|
+
{
|
|
101
|
+
"data": {
|
|
102
|
+
"text/plain": [
|
|
103
|
+
"{'water': 20.22, 'forest': 19.93, 'urban': 20.77, 'crop': 19.67, 'bare': 19.41}"
|
|
104
|
+
]
|
|
105
|
+
},
|
|
106
|
+
"execution_count": 4,
|
|
107
|
+
"metadata": {},
|
|
108
|
+
"output_type": "execute_result"
|
|
109
|
+
}
|
|
110
|
+
],
|
|
111
|
+
"source": [
|
|
112
|
+
"count_classes(arr, names=names, percent=True)"
|
|
113
|
+
]
|
|
114
|
+
},
|
|
115
|
+
{
|
|
116
|
+
"cell_type": "markdown",
|
|
117
|
+
"metadata": {},
|
|
118
|
+
"source": [
|
|
119
|
+
"## PyTorch Tensors\n",
|
|
120
|
+
"\n",
|
|
121
|
+
"Pass a torch tensor and the torch backend is used automatically. Install with `pip install classcounter[torch]`."
|
|
122
|
+
]
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
"cell_type": "code",
|
|
126
|
+
"execution_count": 5,
|
|
127
|
+
"metadata": {},
|
|
128
|
+
"outputs": [
|
|
129
|
+
{
|
|
130
|
+
"data": {
|
|
131
|
+
"text/plain": [
|
|
132
|
+
"{0: 1944, 1: 2004, 2: 2037, 3: 2004, 4: 2011}"
|
|
133
|
+
]
|
|
134
|
+
},
|
|
135
|
+
"execution_count": 5,
|
|
136
|
+
"metadata": {},
|
|
137
|
+
"output_type": "execute_result"
|
|
138
|
+
}
|
|
139
|
+
],
|
|
140
|
+
"source": [
|
|
141
|
+
"import torch\n",
|
|
142
|
+
"\n",
|
|
143
|
+
"tensor = torch.randint(0, 5, (100, 100))\n",
|
|
144
|
+
"count_classes(tensor)"
|
|
145
|
+
]
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
"cell_type": "markdown",
|
|
149
|
+
"metadata": {},
|
|
150
|
+
"source": [
|
|
151
|
+
"## Performance\n",
|
|
152
|
+
"\n",
|
|
153
|
+
"With `classcounter[numba]` installed, numpy arrays are automatically counted with a JIT-compiled loop — no code changes needed. PyTorch is another fast option, especially on GPU."
|
|
154
|
+
]
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
"cell_type": "code",
|
|
158
|
+
"execution_count": 15,
|
|
159
|
+
"metadata": {},
|
|
160
|
+
"outputs": [
|
|
161
|
+
{
|
|
162
|
+
"name": "stdout",
|
|
163
|
+
"output_type": "stream",
|
|
164
|
+
"text": [
|
|
165
|
+
"Elements: 400,000,000\n",
|
|
166
|
+
"263 ms ± 14.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
|
|
167
|
+
]
|
|
168
|
+
}
|
|
169
|
+
],
|
|
170
|
+
"source": [
|
|
171
|
+
"big = rng.integers(0, 10, size=(20000, 20000), dtype=np.int32)\n",
|
|
172
|
+
"print(f\"Elements: {big.size:,}\")\n",
|
|
173
|
+
"\n",
|
|
174
|
+
"%timeit count_classes(big)"
|
|
175
|
+
]
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
"cell_type": "code",
|
|
179
|
+
"execution_count": 7,
|
|
180
|
+
"metadata": {},
|
|
181
|
+
"outputs": [
|
|
182
|
+
{
|
|
183
|
+
"name": "stdout",
|
|
184
|
+
"output_type": "stream",
|
|
185
|
+
"text": [
|
|
186
|
+
"Using device: cuda\n",
|
|
187
|
+
"802 ms ± 63.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n",
|
|
188
|
+
"33.2 ms ± 600 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
|
|
189
|
+
]
|
|
190
|
+
}
|
|
191
|
+
],
|
|
192
|
+
"source": [
|
|
193
|
+
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
|
|
194
|
+
"print(f\"Using device: {device}\")\n",
|
|
195
|
+
"\n",
|
|
196
|
+
"big_tensor = torch.randint(0, 10, (20000, 20000), device=device)\n",
|
|
197
|
+
"\n",
|
|
198
|
+
"# CPU baseline\n",
|
|
199
|
+
"big_cpu = big_tensor.cpu()\n",
|
|
200
|
+
"%timeit count_classes(big_cpu)\n",
|
|
201
|
+
"\n",
|
|
202
|
+
"# GPU (or same CPU if no GPU available)\n",
|
|
203
|
+
"%timeit count_classes(big_tensor)"
|
|
204
|
+
]
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
"cell_type": "markdown",
|
|
208
|
+
"metadata": {},
|
|
209
|
+
"source": [
|
|
210
|
+
"## GeoTIFF Support\n",
|
|
211
|
+
"\n",
|
|
212
|
+
"Install with `pip install classcounter[geo]`. Pass a file path directly to count classes in a raster."
|
|
213
|
+
]
|
|
214
|
+
},
|
|
215
|
+
{
|
|
216
|
+
"cell_type": "code",
|
|
217
|
+
"execution_count": 8,
|
|
218
|
+
"metadata": {},
|
|
219
|
+
"outputs": [
|
|
220
|
+
{
|
|
221
|
+
"name": "stdout",
|
|
222
|
+
"output_type": "stream",
|
|
223
|
+
"text": [
|
|
224
|
+
"Created: /tmp/tmpr0hwn1go/land_cover.tif\n"
|
|
225
|
+
]
|
|
226
|
+
}
|
|
227
|
+
],
|
|
228
|
+
"source": [
|
|
229
|
+
"import tempfile\n",
|
|
230
|
+
"from pathlib import Path\n",
|
|
231
|
+
"\n",
|
|
232
|
+
"import rasterio\n",
|
|
233
|
+
"from rasterio.transform import from_bounds\n",
|
|
234
|
+
"\n",
|
|
235
|
+
"# Create a small land-cover raster (5 classes, 100x100 pixels)\n",
|
|
236
|
+
"land_cover = rng.integers(0, 5, size=(100, 100), dtype=np.uint8)\n",
|
|
237
|
+
"sample_tif = Path(tempfile.mkdtemp()) / \"land_cover.tif\"\n",
|
|
238
|
+
"transform = from_bounds(115.0, -33.0, 116.0, -32.0, 100, 100)\n",
|
|
239
|
+
"\n",
|
|
240
|
+
"with rasterio.open(\n",
|
|
241
|
+
" sample_tif,\n",
|
|
242
|
+
" \"w\",\n",
|
|
243
|
+
" driver=\"GTiff\",\n",
|
|
244
|
+
" height=100,\n",
|
|
245
|
+
" width=100,\n",
|
|
246
|
+
" count=1,\n",
|
|
247
|
+
" dtype=\"uint8\",\n",
|
|
248
|
+
" transform=transform,\n",
|
|
249
|
+
" crs=\"EPSG:4326\",\n",
|
|
250
|
+
") as dst:\n",
|
|
251
|
+
" dst.write(land_cover, 1)\n",
|
|
252
|
+
"\n",
|
|
253
|
+
"print(f\"Created: {sample_tif}\")"
|
|
254
|
+
]
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
"cell_type": "code",
|
|
258
|
+
"execution_count": 9,
|
|
259
|
+
"metadata": {},
|
|
260
|
+
"outputs": [
|
|
261
|
+
{
|
|
262
|
+
"data": {
|
|
263
|
+
"text/plain": [
|
|
264
|
+
"{'water': 1932, 'forest': 2068, 'urban': 2024, 'crop': 1978, 'bare': 1998}"
|
|
265
|
+
]
|
|
266
|
+
},
|
|
267
|
+
"execution_count": 9,
|
|
268
|
+
"metadata": {},
|
|
269
|
+
"output_type": "execute_result"
|
|
270
|
+
}
|
|
271
|
+
],
|
|
272
|
+
"source": [
|
|
273
|
+
"count_classes(sample_tif, names=names)"
|
|
274
|
+
]
|
|
275
|
+
},
|
|
276
|
+
{
|
|
277
|
+
"cell_type": "markdown",
|
|
278
|
+
"metadata": {},
|
|
279
|
+
"source": [
|
|
280
|
+
"## Saving Results to GeoTIFF Metadata\n",
|
|
281
|
+
"\n",
|
|
282
|
+
"Use `save_metadata=True` to write class counts back into the raster's GDAL metadata tags (one tag per class, e.g. `CLASS_COUNT_0=1234`). For more control, use the standalone `save_counts_to_raster` function."
|
|
283
|
+
]
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
"cell_type": "code",
|
|
287
|
+
"execution_count": 10,
|
|
288
|
+
"metadata": {},
|
|
289
|
+
"outputs": [
|
|
290
|
+
{
|
|
291
|
+
"data": {
|
|
292
|
+
"text/plain": [
|
|
293
|
+
"{0: 1932, 1: 2068, 2: 2024, 3: 1978, 4: 1998}"
|
|
294
|
+
]
|
|
295
|
+
},
|
|
296
|
+
"execution_count": 10,
|
|
297
|
+
"metadata": {},
|
|
298
|
+
"output_type": "execute_result"
|
|
299
|
+
}
|
|
300
|
+
],
|
|
301
|
+
"source": [
|
|
302
|
+
"from classcounter import save_counts_to_raster\n",
|
|
303
|
+
"\n",
|
|
304
|
+
"# One-liner: count and write CLASS_COUNT_ tags in one step\n",
|
|
305
|
+
"count_classes(sample_tif, save_metadata=True)"
|
|
306
|
+
]
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
"cell_type": "code",
|
|
310
|
+
"execution_count": 11,
|
|
311
|
+
"metadata": {},
|
|
312
|
+
"outputs": [
|
|
313
|
+
{
|
|
314
|
+
"data": {
|
|
315
|
+
"text/plain": [
|
|
316
|
+
"{'CLASS_COUNT_0': '1932',\n",
|
|
317
|
+
" 'CLASS_COUNT_1': '2068',\n",
|
|
318
|
+
" 'CLASS_COUNT_2': '2024',\n",
|
|
319
|
+
" 'CLASS_COUNT_3': '1978',\n",
|
|
320
|
+
" 'CLASS_COUNT_4': '1998'}"
|
|
321
|
+
]
|
|
322
|
+
},
|
|
323
|
+
"execution_count": 11,
|
|
324
|
+
"metadata": {},
|
|
325
|
+
"output_type": "execute_result"
|
|
326
|
+
}
|
|
327
|
+
],
|
|
328
|
+
"source": [
|
|
329
|
+
"# Verify the tags were written\n",
|
|
330
|
+
"with rasterio.open(sample_tif) as src:\n",
|
|
331
|
+
" tags = src.tags()\n",
|
|
332
|
+
"\n",
|
|
333
|
+
"{k: v for k, v in tags.items() if k.startswith(\"CLASS_\")}"
|
|
334
|
+
]
|
|
335
|
+
},
|
|
336
|
+
{
|
|
337
|
+
"cell_type": "markdown",
|
|
338
|
+
"metadata": {},
|
|
339
|
+
"source": [
|
|
340
|
+
"With `percent=True` the prefix switches to `CLASS_PERCENT_`. You can also set a custom prefix with `metadata_prefix`, or call `save_counts_to_raster` directly:"
|
|
341
|
+
]
|
|
342
|
+
},
|
|
343
|
+
{
|
|
344
|
+
"cell_type": "code",
|
|
345
|
+
"execution_count": 12,
|
|
346
|
+
"metadata": {},
|
|
347
|
+
"outputs": [],
|
|
348
|
+
"source": [
|
|
349
|
+
"# Percent metadata\n",
|
|
350
|
+
"count_classes(sample_tif, percent=True, save_metadata=True)\n",
|
|
351
|
+
"\n",
|
|
352
|
+
"# Custom prefix\n",
|
|
353
|
+
"count_classes(sample_tif, save_metadata=True, metadata_prefix=\"LAND_\")\n",
|
|
354
|
+
"\n",
|
|
355
|
+
"# Standalone function with named classes\n",
|
|
356
|
+
"counts = count_classes(sample_tif, names=names)\n",
|
|
357
|
+
"save_counts_to_raster(sample_tif, counts, prefix=\"NAMED_\")"
|
|
358
|
+
]
|
|
359
|
+
}
|
|
360
|
+
],
|
|
361
|
+
"metadata": {
|
|
362
|
+
"kernelspec": {
|
|
363
|
+
"display_name": "classcounter",
|
|
364
|
+
"language": "python",
|
|
365
|
+
"name": "python3"
|
|
366
|
+
},
|
|
367
|
+
"language_info": {
|
|
368
|
+
"codemirror_mode": {
|
|
369
|
+
"name": "ipython",
|
|
370
|
+
"version": 3
|
|
371
|
+
},
|
|
372
|
+
"file_extension": ".py",
|
|
373
|
+
"mimetype": "text/x-python",
|
|
374
|
+
"name": "python",
|
|
375
|
+
"nbconvert_exporter": "python",
|
|
376
|
+
"pygments_lexer": "ipython3",
|
|
377
|
+
"version": "3.13.7"
|
|
378
|
+
}
|
|
379
|
+
},
|
|
380
|
+
"nbformat": 4,
|
|
381
|
+
"nbformat_minor": 4
|
|
382
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 DPIRD-DMA
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|