py-materials 3.2.1__tar.gz → 3.3.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.
Files changed (57) hide show
  1. {py_materials-3.2.1 → py_materials-3.3.0}/PKG-INFO +1 -1
  2. {py_materials-3.2.1 → py_materials-3.3.0}/pyproject.toml +1 -1
  3. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/__init__.py +3 -1
  4. py_materials-3.3.0/src/pymat/search.py +127 -0
  5. py_materials-3.3.0/tests/test_search.py +278 -0
  6. {py_materials-3.2.1 → py_materials-3.3.0}/.gitignore +0 -0
  7. {py_materials-3.2.1 → py_materials-3.3.0}/LICENSE +0 -0
  8. {py_materials-3.2.1 → py_materials-3.3.0}/README.md +0 -0
  9. {py_materials-3.2.1 → py_materials-3.3.0}/docs/catalog/README.md +0 -0
  10. {py_materials-3.2.1 → py_materials-3.3.0}/docs/catalog/ceramics/README.md +0 -0
  11. {py_materials-3.2.1 → py_materials-3.3.0}/docs/catalog/electronics/README.md +0 -0
  12. {py_materials-3.2.1 → py_materials-3.3.0}/docs/catalog/gases/README.md +0 -0
  13. {py_materials-3.2.1 → py_materials-3.3.0}/docs/catalog/liquids/README.md +0 -0
  14. {py_materials-3.2.1 → py_materials-3.3.0}/docs/catalog/metals/README.md +0 -0
  15. {py_materials-3.2.1 → py_materials-3.3.0}/docs/catalog/plastics/README.md +0 -0
  16. {py_materials-3.2.1 → py_materials-3.3.0}/docs/catalog/scintillators/README.md +0 -0
  17. {py_materials-3.2.1 → py_materials-3.3.0}/docs/decisions/README.md +0 -0
  18. {py_materials-3.2.1 → py_materials-3.3.0}/mat-rs/data/README.md +0 -0
  19. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/core.py +0 -0
  20. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/data/ceramics.toml +0 -0
  21. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/data/electronics.toml +0 -0
  22. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/data/gases.toml +0 -0
  23. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/data/liquids.toml +0 -0
  24. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/data/metals.toml +0 -0
  25. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/data/plastics.toml +0 -0
  26. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/data/scintillators.toml +0 -0
  27. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/elements.py +0 -0
  28. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/enrichers.py +0 -0
  29. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/factories.py +0 -0
  30. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/loader.py +0 -0
  31. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/properties.py +0 -0
  32. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/registry.py +0 -0
  33. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/units.py +0 -0
  34. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/vis/__init__.py +0 -0
  35. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/vis/_model.py +0 -0
  36. {py_materials-3.2.1 → py_materials-3.3.0}/src/pymat/vis/adapters.py +0 -0
  37. {py_materials-3.2.1 → py_materials-3.3.0}/tests/__init__.py +0 -0
  38. {py_materials-3.2.1 → py_materials-3.3.0}/tests/_visual_compare.py +0 -0
  39. {py_materials-3.2.1 → py_materials-3.3.0}/tests/baselines/README.md +0 -0
  40. {py_materials-3.2.1 → py_materials-3.3.0}/tests/conftest.py +0 -0
  41. {py_materials-3.2.1 → py_materials-3.3.0}/tests/material_preview.html +0 -0
  42. {py_materials-3.2.1 → py_materials-3.3.0}/tests/test_adapters.py +0 -0
  43. {py_materials-3.2.1 → py_materials-3.3.0}/tests/test_apply_to.py +0 -0
  44. {py_materials-3.2.1 → py_materials-3.3.0}/tests/test_catalog.py +0 -0
  45. {py_materials-3.2.1 → py_materials-3.3.0}/tests/test_core.py +0 -0
  46. {py_materials-3.2.1 → py_materials-3.3.0}/tests/test_e2e_vis.py +0 -0
  47. {py_materials-3.2.1 → py_materials-3.3.0}/tests/test_enrichers.py +0 -0
  48. {py_materials-3.2.1 → py_materials-3.3.0}/tests/test_integration.py +0 -0
  49. {py_materials-3.2.1 → py_materials-3.3.0}/tests/test_loader.py +0 -0
  50. {py_materials-3.2.1 → py_materials-3.3.0}/tests/test_properties.py +0 -0
  51. {py_materials-3.2.1 → py_materials-3.3.0}/tests/test_readme_examples.py +0 -0
  52. {py_materials-3.2.1 → py_materials-3.3.0}/tests/test_toml_integrity.py +0 -0
  53. {py_materials-3.2.1 → py_materials-3.3.0}/tests/test_uncertainties.py +0 -0
  54. {py_materials-3.2.1 → py_materials-3.3.0}/tests/test_vis.py +0 -0
  55. {py_materials-3.2.1 → py_materials-3.3.0}/tests/test_visual_compare.py +0 -0
  56. {py_materials-3.2.1 → py_materials-3.3.0}/tests/test_visual_regression.py +0 -0
  57. {py_materials-3.2.1 → py_materials-3.3.0}/tests/visual_render.html +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: py-materials
3
- Version: 3.2.1
3
+ Version: 3.3.0
4
4
  Summary: Hierarchical material library for CAD applications with build123d integration
5
5
  Project-URL: Homepage, https://github.com/MorePet/py-mat
6
6
  Project-URL: Repository, https://github.com/MorePet/py-mat
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "py-materials"
7
- version = "3.2.1"
7
+ version = "3.3.0"
8
8
  description = "Hierarchical material library for CAD applications with build123d integration"
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -54,9 +54,10 @@ from .properties import (
54
54
  SourcingProperties,
55
55
  ThermalProperties,
56
56
  )
57
+ from .search import search
57
58
  from .units import ureg
58
59
 
59
- __version__ = "3.2.1" # x-release-please-version
60
+ __version__ = "3.3.0" # x-release-please-version
60
61
  __all__ = [
61
62
  "Material",
62
63
  "AllProperties",
@@ -70,6 +71,7 @@ __all__ = [
70
71
  "ureg",
71
72
  "load_toml",
72
73
  "load_category",
74
+ "search",
73
75
  "enrich_from_periodictable",
74
76
  "enrich_from_matproj",
75
77
  "enrich_all",
@@ -0,0 +1,127 @@
1
+ """
2
+ Fuzzy search over the py-mat domain library.
3
+
4
+ ``pymat.search("stainless 316")`` returns a ranked list of ``Material``
5
+ instances whose registry key, name, grade, or hierarchy path matches
6
+ the query tokens. Complements ``pymat.vis.search(...)`` (visual
7
+ catalog): two axes, same verb, no namespace collision.
8
+
9
+ Algorithm: tokenize query on whitespace; every token must match at
10
+ least one weighted target on a candidate Material for it to be
11
+ included. Score is the sum of per-token best-target weights.
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ from typing import TYPE_CHECKING
17
+
18
+ if TYPE_CHECKING:
19
+ from .core import Material
20
+
21
+
22
+ # Target weights — higher is a stronger match. Order matters: when a
23
+ # query token matches multiple targets on the same Material, the
24
+ # highest-weight hit counts.
25
+ _WEIGHT_KEY = 10 # registry key — "s316L"
26
+ _WEIGHT_NAME = 5 # Material.name — "Stainless Steel 316L"
27
+ _WEIGHT_GRADE = 5 # Material.grade — "316L"
28
+ _WEIGHT_PATH = 3 # hierarchy parent names
29
+
30
+
31
+ def _targets(key: str, material: Material) -> list[tuple[str, int]]:
32
+ """Return (lowered-target-string, weight) pairs for a Material.
33
+
34
+ Hierarchy path walks ``parent`` up; skips the material's own name
35
+ (already covered by the name target) and any None-named parents.
36
+ """
37
+ pairs: list[tuple[str, int]] = [(key.lower(), _WEIGHT_KEY)]
38
+
39
+ if material.name:
40
+ pairs.append((material.name.lower(), _WEIGHT_NAME))
41
+
42
+ grade = getattr(material, "grade", None)
43
+ if grade:
44
+ pairs.append((str(grade).lower(), _WEIGHT_GRADE))
45
+
46
+ # Walk the parent chain; skip the material itself (starts from parent).
47
+ parent = getattr(material, "parent", None)
48
+ while parent is not None:
49
+ if parent.name:
50
+ pairs.append((parent.name.lower(), _WEIGHT_PATH))
51
+ parent = getattr(parent, "parent", None)
52
+
53
+ return pairs
54
+
55
+
56
+ def _score(tokens: list[str], targets: list[tuple[str, int]]) -> int:
57
+ """Score a Material against the query tokens.
58
+
59
+ Every token must substring-match at least one target. Returns 0
60
+ (rejected) if any token misses. Otherwise returns the sum of
61
+ best-per-token weights — so a token matching the high-weight key
62
+ beats a token matching only the low-weight path.
63
+ """
64
+ total = 0
65
+ for token in tokens:
66
+ best = 0
67
+ for target, weight in targets:
68
+ if token in target and weight > best:
69
+ best = weight
70
+ if best == 0:
71
+ return 0 # token didn't match anywhere → reject
72
+ total += best
73
+ return total
74
+
75
+
76
+ def search(query: str, *, limit: int = 10) -> list[Material]:
77
+ """Fuzzy-find Materials in the loaded library by name, key, or grade.
78
+
79
+ Tokenizes ``query`` on whitespace (case-insensitive). Every token
80
+ must match somewhere — registry key, ``Material.name``,
81
+ ``Material.grade``, or a hierarchy parent name — for a Material
82
+ to be included. Ranks by summed match weight; returns at most
83
+ ``limit`` best hits.
84
+
85
+ Returns an empty list for an empty / whitespace-only query.
86
+
87
+ Examples::
88
+
89
+ pymat.search("stainless")
90
+ # → [stainless, s304, s316L, s410, ...]
91
+
92
+ pymat.search("316")
93
+ # → [s316L, s316, ...] — grades match, parent doesn't
94
+
95
+ pymat.search("stainless 316")
96
+ # → [s316L] — all tokens must match, grade wins
97
+
98
+ pymat.search("lyso ce saint")
99
+ # → [prelude420, ...] — deep hierarchy via tokenization
100
+
101
+ Triggers ``load_all()`` on first call so results are exhaustive
102
+ across categories.
103
+ """
104
+ tokens = query.lower().split()
105
+ if not tokens:
106
+ return []
107
+
108
+ # Import lazily to avoid circular import at module load: pymat's
109
+ # __init__.py hasn't finished initializing when this module loads.
110
+ from . import load_all, registry
111
+
112
+ load_all()
113
+ all_materials = registry.list_all()
114
+
115
+ scored: list[tuple[int, int, str, Material]] = []
116
+ for key, material in all_materials.items():
117
+ targets = _targets(key, material)
118
+ s = _score(tokens, targets)
119
+ if s > 0:
120
+ # Tie-break tuple: primary by score DESC (via -s), then by
121
+ # key length ASC (shorter key wins — "s316L" before
122
+ # "electropolished" when both score the same), then by key
123
+ # alphabetical for determinism.
124
+ scored.append((-s, len(key), key, material))
125
+
126
+ scored.sort()
127
+ return [m for _, _, _, m in scored[:limit]]
@@ -0,0 +1,278 @@
1
+ """Tests for pymat.search — fuzzy find over the domain library.
2
+
3
+ Covers the full contract documented in the search module docstring:
4
+
5
+ - tokenization (whitespace, case)
6
+ - target weighting (key > name/grade > hierarchy path)
7
+ - conjunctive matching (every token must hit somewhere)
8
+ - tie-breaks (shorter key wins)
9
+ - limit truncation
10
+ - load_all() side effect on first call
11
+ - empty / whitespace query
12
+ - missing optional attributes (grade=None, parent=None)
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import pymat
18
+ from pymat import Material, search
19
+ from pymat.search import _score, _targets
20
+
21
+
22
+ class TestTopLevelSurface:
23
+ """``pymat.search`` is a public top-level verb, returning Materials."""
24
+
25
+ def test_callable_from_package_root(self):
26
+ assert callable(pymat.search)
27
+
28
+ def test_in_package_all(self):
29
+ assert "search" in pymat.__all__
30
+
31
+ def test_returns_list_of_materials(self):
32
+ hits = pymat.search("stainless")
33
+ assert isinstance(hits, list)
34
+ for m in hits:
35
+ assert isinstance(m, Material)
36
+
37
+
38
+ class TestEmptyQuery:
39
+ """An empty or whitespace-only query returns [], never the whole registry."""
40
+
41
+ def test_empty_string(self):
42
+ assert search("") == []
43
+
44
+ def test_whitespace_only(self):
45
+ assert search(" \t\n ") == []
46
+
47
+
48
+ class TestExactKeyMatch:
49
+ """A query that's an exact registry key must return that Material
50
+ first — the high-weight key target outranks name / path matches."""
51
+
52
+ def test_exact_key_ranks_first(self):
53
+ hits = search("s316L")
54
+ assert hits, "exact-key query returned no hits"
55
+ # The s316L registry entry's Material.name is "Stainless Steel 316L"
56
+ assert hits[0].name == "Stainless Steel 316L"
57
+
58
+ def test_lowercase_query_matches_camelcase_key(self):
59
+ """Search is case-insensitive — "s316l" matches registry key "s316L"."""
60
+ hits = search("s316l")
61
+ assert hits and hits[0].name == "Stainless Steel 316L"
62
+
63
+
64
+ class TestNameMatch:
65
+ """Substring of ``Material.name`` must match even when the registry
66
+ key is different — users search for human names, not registry keys."""
67
+
68
+ def test_name_substring_match(self):
69
+ # 'steel' appears in name "Stainless Steel" etc., not in keys
70
+ hits = search("steel")
71
+ assert any("Steel" in m.name for m in hits)
72
+
73
+ def test_human_word_matches_name(self):
74
+ hits = search("aluminum")
75
+ assert hits
76
+ assert all("Aluminum" in m.name for m in hits)
77
+
78
+
79
+ class TestGradeMatch:
80
+ """``Material.grade`` is a separate target — a bare grade number
81
+ should find the Material even if it's not in the key or full name."""
82
+
83
+ def test_grade_number_finds_grade_materials(self):
84
+ # 'T6' is both a registry key alias AND a grade value;
85
+ # hitting either target gets s316L-style materials.
86
+ hits = search("6061")
87
+ assert hits, "grade-number query returned no hits"
88
+ assert any("6061" in m.name for m in hits)
89
+
90
+
91
+ class TestMultiTokenConjunctive:
92
+ """All tokens must match somewhere — a miss on any token rejects."""
93
+
94
+ def test_both_tokens_must_match(self):
95
+ """'stainless 316' matches only grades with both — not bare
96
+ Stainless Steel parent, not bare 'Metal063'."""
97
+ hits = search("stainless 316")
98
+ assert hits
99
+ # Every hit's name must contain BOTH 'stainless' and '316' case-insensitively
100
+ for m in hits:
101
+ lowered = m.name.lower()
102
+ assert "stainless" in lowered
103
+ assert "316" in lowered
104
+
105
+ def test_token_miss_rejects_match(self):
106
+ """A token that matches nothing rejects the Material entirely —
107
+ even if other tokens match strongly."""
108
+ hits = search("stainless unobtainium_xyz")
109
+ assert hits == []
110
+
111
+ def test_tokens_across_name_and_hierarchy(self):
112
+ """Tokens can match different targets on the same Material —
113
+ one in the name, another in the parent chain."""
114
+ hits = search("lyso saint")
115
+ assert hits
116
+ # saint_gobain parent + lyso in name → must hit Saint-Gobain LYSO
117
+ assert any("Saint-Gobain" in m.name and "LYSO" in m.name for m in hits)
118
+
119
+
120
+ class TestRanking:
121
+ """Key matches outrank name matches; ties broken by shorter key."""
122
+
123
+ def test_key_beats_name_when_both_match(self):
124
+ """If a query matches both a registry key exactly AND a name
125
+ partially elsewhere, the exact-key hit ranks higher."""
126
+ # 'T6' is a registry key (= Aluminum 6061-T6 alias), and 'T6'
127
+ # also appears in the name of every T6-ed aluminum. The T6 key
128
+ # entry should come first.
129
+ hits = search("T6")
130
+ assert hits
131
+ # First hit's key-match is higher weight than name-only matches
132
+ # lower in the list.
133
+ assert hits[0].name == "Aluminum 6061-T6"
134
+
135
+ def test_shorter_key_tiebreaks_first(self):
136
+ """With equal scores, shorter registry key wins — so parents
137
+ come before their longer-keyed descendants when both match."""
138
+ # Both 'stainless' (parent) and e.g. 's316L' match 'stainless'
139
+ # via name — but parent has a key-level hit AND shorter key.
140
+ hits = search("stainless")
141
+ assert hits[0].name == "Stainless Steel" # the parent
142
+
143
+ def test_limit_respects_ranking(self):
144
+ """``limit=`` truncates AFTER ranking, not before."""
145
+ all_hits = search("steel", limit=50)
146
+ top3 = search("steel", limit=3)
147
+ assert len(top3) == 3
148
+ # top3 must be a strict prefix of all_hits (same ranking order)
149
+ assert [m.name for m in top3] == [m.name for m in all_hits[:3]]
150
+
151
+
152
+ class TestLimitParameter:
153
+ def test_default_limit_is_ten(self):
154
+ # Registry has >10 steel-related materials; default should cap at 10.
155
+ hits = search("steel")
156
+ assert len(hits) <= 10
157
+
158
+ def test_custom_limit(self):
159
+ hits = search("steel", limit=2)
160
+ assert len(hits) <= 2
161
+
162
+ def test_limit_zero_returns_empty(self):
163
+ assert search("steel", limit=0) == []
164
+
165
+
166
+ class TestLoadingBehavior:
167
+ """``search`` triggers ``load_all`` so results are exhaustive even
168
+ when categories haven't been touched yet."""
169
+
170
+ def test_search_loads_all_categories(self):
171
+ # Any query should find materials across every category.
172
+ # 'lyso' lives in scintillators, 'fr4' in electronics, 'ptfe'
173
+ # in plastics — a query for each must return hits regardless
174
+ # of prior lazy-load state.
175
+ for q, expected in [
176
+ ("lyso", "LYSO"),
177
+ ("fr4", "FR4"),
178
+ ("ptfe", "PTFE"),
179
+ ]:
180
+ hits = search(q)
181
+ assert hits, f"no hits for {q!r}"
182
+ assert any(expected in m.name for m in hits), f"no {expected} in hits for {q!r}"
183
+
184
+
185
+ class TestInternalScoring:
186
+ """Unit tests for the scoring helpers — pinned so refactors can't
187
+ silently change weights."""
188
+
189
+ def test_targets_include_key_and_name(self):
190
+ from pymat.search import _WEIGHT_KEY, _WEIGHT_NAME
191
+
192
+ m = Material(name="Test Material", grade="42")
193
+ pairs = _targets("test_key", m)
194
+ # Tuple flattening — check by target text + weight
195
+ assert ("test_key", _WEIGHT_KEY) in pairs
196
+ assert ("test material", _WEIGHT_NAME) in pairs
197
+
198
+ def test_targets_include_grade_when_present(self):
199
+ from pymat.search import _WEIGHT_GRADE
200
+
201
+ m = Material(name="Test", grade="42")
202
+ pairs = _targets("k", m)
203
+ assert ("42", _WEIGHT_GRADE) in pairs
204
+
205
+ def test_targets_omit_grade_when_none(self):
206
+ """No grade set → ``_targets`` must not add a grade pair.
207
+ Compared by content (not weight, since name + grade share a
208
+ weight today)."""
209
+ m = Material(name="Test")
210
+ assert m.grade is None
211
+ pairs = _targets("k", m)
212
+ # Expected pairs: key + name only. No grade-valued entry.
213
+ # The grade value, if present, would be the string form of
214
+ # whatever m.grade holds — here None. So simply assert the
215
+ # set is exactly key + name.
216
+ assert pairs == [("k", 10), ("test", 5)]
217
+
218
+ def test_targets_walk_parent_chain(self):
219
+ """Hierarchy parent names must appear as path-weighted targets."""
220
+ from pymat.search import _WEIGHT_PATH
221
+
222
+ parent = Material(name="Parent Material")
223
+ child = Material(name="Child Material")
224
+ child.parent = parent # simulate hierarchy attachment
225
+
226
+ pairs = _targets("child_key", child)
227
+ path_pairs = [t for t, w in pairs if w == _WEIGHT_PATH]
228
+ assert "parent material" in path_pairs
229
+
230
+ def test_score_all_tokens_must_match(self):
231
+ targets = [("stainless steel 316l", 5), ("s316l", 10)]
232
+ # Both tokens present → positive
233
+ assert _score(["stainless", "316"], targets) > 0
234
+ # One token missing → 0 (rejected)
235
+ assert _score(["stainless", "xyz"], targets) == 0
236
+
237
+ def test_score_picks_highest_weight_per_token(self):
238
+ from pymat.search import _WEIGHT_KEY, _WEIGHT_NAME
239
+
240
+ # "316" appears in both a key target (weight 10) and a name
241
+ # target (weight 5) — the higher one counts.
242
+ targets = [
243
+ ("s316l", _WEIGHT_KEY), # has "316"
244
+ ("stainless steel 316l", _WEIGHT_NAME), # has "316"
245
+ ]
246
+ score = _score(["316"], targets)
247
+ assert score == _WEIGHT_KEY
248
+
249
+
250
+ class TestDeterminism:
251
+ """Two identical queries must return the same list in the same
252
+ order — no hash-randomization surprises leaking through."""
253
+
254
+ def test_same_query_same_order(self):
255
+ a = search("steel", limit=5)
256
+ b = search("steel", limit=5)
257
+ assert [m.name for m in a] == [m.name for m in b]
258
+
259
+
260
+ class TestRegressionGuards:
261
+ """Specific realistic queries that users are likely to type.
262
+ These pin behavior the README / docstring examples advertise."""
263
+
264
+ def test_stainless_parent_first(self):
265
+ hits = search("stainless")
266
+ assert hits[0].name == "Stainless Steel"
267
+
268
+ def test_stainless_316_returns_316L_grade(self):
269
+ hits = search("stainless 316")
270
+ assert hits[0].grade == "316L"
271
+
272
+ def test_lyso_ce_saint_hits_prelude(self):
273
+ """Deep-hierarchy query: a scintillator vendor's product."""
274
+ hits = search("lyso ce saint")
275
+ names = [m.name for m in hits]
276
+ assert any("PreLude" in n for n in names) or any(
277
+ "Saint-Gobain" in n and "LYSO" in n for n in names
278
+ )
File without changes
File without changes
File without changes