grainmorph 0.2.0__py3-none-any.whl

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.
@@ -0,0 +1,208 @@
1
+ Metadata-Version: 2.4
2
+ Name: grainmorph
3
+ Version: 0.2.0
4
+ Summary: Particle shape analysis and visualization library.
5
+ Author: GrainMorph Contributors
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/phykn/grainmorph
8
+ Project-URL: Repository, https://github.com/phykn/grainmorph
9
+ Project-URL: Issues, https://github.com/phykn/grainmorph/issues
10
+ Requires-Python: >=3.12
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: numpy
14
+ Requires-Dist: opencv-python
15
+ Requires-Dist: matplotlib
16
+ Provides-Extra: dev
17
+ Requires-Dist: pytest; extra == "dev"
18
+ Requires-Dist: ruff; extra == "dev"
19
+ Dynamic: license-file
20
+
21
+ # GrainMorph
22
+
23
+ GrainMorph analyzes a single 2D binary particle mask and returns geometric shape metrics.
24
+
25
+ | Metric | Meaning |
26
+ |---|---|
27
+ | Wadell roundness | Corner curvature-based roundness |
28
+ | Circularity | `4 * pi * area / perimeter^2` |
29
+ | Sphericity | Inscribed circle radius divided by enclosing circle radius |
30
+ | Aspect ratio | Fitted ellipse major axis divided by minor axis |
31
+
32
+ Roundness, circularity, and sphericity are clipped to `0..1`. Aspect ratio is `>= 1`.
33
+
34
+ ## Installation
35
+
36
+ GrainMorph requires Python `3.12+`.
37
+
38
+ ```bash
39
+ pip install grainmorph
40
+ ```
41
+
42
+ For local development:
43
+
44
+ ```bash
45
+ pip install -e ".[dev]"
46
+ ```
47
+
48
+ Run the tests and code checks with the same Python environment:
49
+
50
+ ```bash
51
+ python -m pytest -q
52
+ python -m ruff check --isolated .
53
+ python -m ruff format --isolated --check src tests
54
+ ```
55
+
56
+ ## Quick Start
57
+
58
+ ```python
59
+ import grainmorph as pm
60
+
61
+ mask = pm.utils.create_circle_mask((256, 256), (128, 128), 60)
62
+ results = pm.analyze_mask(mask)
63
+
64
+ if results is None:
65
+ raise ValueError("mask has no foreground pixels")
66
+
67
+ print(results["roundness"]["val"])
68
+ print(results["circularity"]["val"])
69
+ print(results["sphericity"]["val"])
70
+ print(results["aspect_ratio"]["val"])
71
+ ```
72
+
73
+ Visualize the results:
74
+
75
+ ```python
76
+ pm.utils.plot_analysis_results(mask, results)
77
+ ```
78
+
79
+ ## API
80
+
81
+ ```python
82
+ pm.analyze_mask(
83
+ mask,
84
+ use_aspect_ratio=True,
85
+ use_roundness=True,
86
+ use_circularity=True,
87
+ use_sphericity=True,
88
+ roundness_params=None,
89
+ eps=0.001,
90
+ target_dim=384,
91
+ )
92
+ ```
93
+
94
+ | Parameter | Description |
95
+ |---|---|
96
+ | `mask` | 2D `numpy.ndarray` with `bool` or `{0, 1}` values |
97
+ | `use_*` | Toggle individual metrics on/off |
98
+ | `roundness_params` | Optional dict to override Wadell roundness parameters |
99
+ | `eps` | Tolerance for geometric computations |
100
+ | `target_dim` | Particle bounds larger than this are downscaled for speed, then coordinates are restored |
101
+
102
+ `roundness_params` accepts these Wadell options. Defaults are defined in
103
+ `wadell/roundness.py` and also apply to synthetic particle calibration:
104
+
105
+ | Option | Default | Stage |
106
+ |---|---|---|
107
+ | `max_dev_thresh` | `0.3` | Boundary discretization |
108
+ | `circle_fit_thresh` | `0.98` | Corner circle fitting |
109
+ | `alpha_ratio` | `0.05` | Boundary smoothing |
110
+ | `beta_ratio` | `0.001` | Boundary smoothing |
111
+
112
+ Returns `None` for an empty mask. Otherwise, returns an `AnalysisResult` dict. Disabled metrics are omitted; enabled metrics are present and may be `None` when the geometry cannot be measured.
113
+
114
+ ## Result Shape
115
+
116
+ ```python
117
+ {
118
+ "roundness": {"val": float} | None,
119
+ "circularity": {"val": float} | None,
120
+ "sphericity": {
121
+ "val": float,
122
+ "inscribed": {"x": float, "y": float, "r": float},
123
+ "enclosing": {"x": float, "y": float, "r": float},
124
+ } | None,
125
+ "aspect_ratio": {
126
+ "val": float,
127
+ "ellipse": {
128
+ "major": float, "minor": float,
129
+ "x": float, "y": float, "angle": float,
130
+ "w": float, "h": float, "bbox": list[list[float]],
131
+ },
132
+ } | None,
133
+ }
134
+ ```
135
+
136
+ ## Input Rules
137
+
138
+ - `mask` must be a 2D `numpy.ndarray`
139
+ - Allowed values are `bool` or numeric `{0, 1}`
140
+ - Floating masks must not contain `NaN` or `inf`
141
+ - Empty mask returns `None`
142
+ - The largest 4-connected foreground component is selected and holes are filled
143
+ - Surrounding zero padding does not change the measured shape
144
+ - Downscaling is skipped if its output would have a one-pixel axis
145
+ - Shape, center, and rectangle corner arguments use image order: `(height, width)` and `(y, x)`
146
+ - Triangle vertices use Cartesian order `(x, y)`, matching polygon coordinates
147
+
148
+ ## Project Layout
149
+
150
+ - `analyzer.py`: public analysis workflow and metric selection
151
+ - `validation.py`: public input normalization and analysis-option validation
152
+ - `mask_ops.py`: component selection, contours, and `PreparedMask` crop/resize transforms with result-coordinate restoration
153
+ - `metrics.py`: metric formulas and public result payloads
154
+ - `fitting.py`: circle and ellipse fitting plus oriented bounding boxes
155
+ - `wadell/roundness.py`: roundness options and the boundary-to-radius workflow
156
+ - `wadell/boundary.py` and `smoothing.py`: closed boundary extraction and periodic smoothing
157
+ - `wadell/discretize.py`: boundary approximation with `(x, y)` point arrays
158
+ - `wadell/corner.py`: convex corner selection, circle fitting, and accepted corner radii
159
+ - `utils/`: basic mask construction, parametric particle calibration and reporting, and visualization
160
+
161
+ ## Utilities
162
+
163
+ Mask constructors:
164
+
165
+ - `pm.utils.create_circle_mask(...)`
166
+ - `pm.utils.create_ellipse_mask(...)`
167
+ - `pm.utils.create_rectangle_mask(...)`
168
+ - `pm.utils.create_square_mask(...)`
169
+ - `pm.utils.create_triangle_mask(...)`
170
+ - `pm.utils.create_pentagon_mask(...)`
171
+ - `pm.utils.create_star_mask(...)`
172
+ - `pm.utils.create_particle_mask(...)`
173
+
174
+ For example, a triangle in a non-square image uses `(x, y)` vertices:
175
+
176
+ ```python
177
+ mask = pm.utils.create_triangle_mask((20, 60), (30, 2), (40, 2), (30, 8))
178
+ ```
179
+
180
+ `create_particle_mask` attempts to reach the requested roundness by calibrating
181
+ surface roughness. Set `seed` for reproducible masks and `return_info=True` to
182
+ receive `(mask, info)`. The report measures the returned mask, including any
183
+ clipping at the image bounds; `target_met` compares its roundness error with
184
+ `report_tol`. A zero-radius particle reports roundness `0.0`.
185
+
186
+ Visualization:
187
+
188
+ - `pm.utils.plot_analysis_results(mask, results)`
189
+
190
+ ## Changes in 0.2.0
191
+
192
+ - Renamed the package from `partimorph` to `grainmorph`: install with `pip install grainmorph` and import with `import grainmorph`. The API is otherwise unchanged from 0.1.9.
193
+
194
+ ## Changes in 0.1.9
195
+
196
+ - Wadell roundness is several times faster: corner circle fits are vectorized and boundary discretization does less per-step work. Results are unchanged.
197
+ - `create_particle_mask` calibrates roughness with 12 bisection steps by default instead of 20, about 1.5x faster. Generated masks can differ slightly; on a 108-case grid the roundness error changed by at most 0.012 and its mean was unchanged.
198
+ - Component selection, hole filling, and the Crofton perimeter now use OpenCV and NumPy, so `scipy` and `scikit-image` are no longer dependencies. Results are unchanged.
199
+
200
+ ## Changes in 0.1.8
201
+
202
+ - Corrected the fitted ellipse overlay orientation for rotated particles.
203
+ - Unified inscribed-circle fitting used by sphericity and Wadell roundness.
204
+ - Simplified ellipse fitting and bounding-box coordinate transforms while preserving the public API and analysis results.
205
+
206
+ ## License
207
+
208
+ MIT
@@ -0,0 +1,23 @@
1
+ grainmorph/__init__.py,sha256=Ob2KXdh83wK6SWKuP-JOMAg0aZsARx1bbg9NGLHaIlY,114
2
+ grainmorph/analyzer.py,sha256=VSuLlvp_TE8bvwbmn_M3Hz_5fowGI726mp1do6am0PY,1438
3
+ grainmorph/fitting.py,sha256=T3WJGBU4a8g5YMoVb_mAo630loO75KPQFZ8O7H1dpwQ,2482
4
+ grainmorph/mask_ops.py,sha256=Qh9VyPgingBUHZEV7YcnW6rRb_UTXWI6OtmAoj4m84Q,4353
5
+ grainmorph/metrics.py,sha256=h-pcaTCH-ss6e5g-Z8-GvyolgWxGVFQHAdsQmH9yh2k,2809
6
+ grainmorph/schema.py,sha256=WU4tRDt9F2d97HU_kPHzx35Z-HuCBtT-GtQnoxHyY6Y,967
7
+ grainmorph/validation.py,sha256=Bif5Z6xhuNqInIjh17JWkHxXguRhLsG_q8ymS3g8Pds,1557
8
+ grainmorph/utils/__init__.py,sha256=TBA8NX0HdmWajVQUIC-wGC23b1vYe5ggoqhUS0z2xww,682
9
+ grainmorph/utils/create_mask.py,sha256=doHSKLx2JBU53-ACqOsglR1QSLPtoyJOTUhwMAe6VyQ,3470
10
+ grainmorph/utils/geometry.py,sha256=n7NTGdaNTIx-dvH-Qt7Mdrthu0FBDFsfjyFtN-LcTAc,585
11
+ grainmorph/utils/parametric_mask.py,sha256=PLIaLk7hbgYqWUPBKyldskGoSsy5T1KXNR6_7Rv1IT0,6953
12
+ grainmorph/utils/plot.py,sha256=82ApLtTC45PgQaMqQzRYqKTfinx4rDK6ua8keNRUzTk,2679
13
+ grainmorph/wadell/__init__.py,sha256=Oh-N2HJ1ADHBodPtGtkPZzRKfOAd68RtVMCdfXRfIQs,95
14
+ grainmorph/wadell/boundary.py,sha256=pOTT6-H2nBpxSpot7TpFG1lyfafNqzY_BBlAAgpzNqc,412
15
+ grainmorph/wadell/corner.py,sha256=ztcjunNzw0DXJtoJZpj0P_Y1L9B1IEu37ccl3mK60Gc,5069
16
+ grainmorph/wadell/discretize.py,sha256=Z4B9jpHwlP39paQ6WuVYlKwZs5f5ZlEK2RIbFxlgXG4,1728
17
+ grainmorph/wadell/roundness.py,sha256=Ix6EZwUrdS3KBv3BQ238sKC0v8CIWD2mtbSk6WnUak4,1559
18
+ grainmorph/wadell/smoothing.py,sha256=KtBpD-EMdHFKbPJ7SOCMwotUDsXMaNZ8jwrcQtwUYAo,1379
19
+ grainmorph-0.2.0.dist-info/licenses/LICENSE,sha256=eA5UrriTqsFtKTRrib3YOnVweRpfrfjtl5y2_VROq-s,1080
20
+ grainmorph-0.2.0.dist-info/METADATA,sha256=XjjjhI34rq3jYs2VbwdqtxAfpat37Wg87vfTMAgLKbk,7034
21
+ grainmorph-0.2.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
22
+ grainmorph-0.2.0.dist-info/top_level.txt,sha256=fpf26GVH4a7vVo1zVJjQWK6ONytdLk5AXqGoiafDAuk,11
23
+ grainmorph-0.2.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 GrainMorph Contributors
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.
@@ -0,0 +1 @@
1
+ grainmorph