loop-common 0.0.1__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 (66) hide show
  1. loop_common-0.0.1/LICENSE +21 -0
  2. loop_common-0.0.1/PKG-INFO +35 -0
  3. loop_common-0.0.1/README.md +5 -0
  4. loop_common-0.0.1/pyproject.toml +33 -0
  5. loop_common-0.0.1/setup.cfg +4 -0
  6. loop_common-0.0.1/src/loop_common/__init__.py +6 -0
  7. loop_common-0.0.1/src/loop_common/base.py +98 -0
  8. loop_common-0.0.1/src/loop_common/geometry/__init__.py +19 -0
  9. loop_common-0.0.1/src/loop_common/geometry/_aabb.py +37 -0
  10. loop_common-0.0.1/src/loop_common/geometry/_bounding_box.py +792 -0
  11. loop_common-0.0.1/src/loop_common/geometry/_face_table.py +42 -0
  12. loop_common-0.0.1/src/loop_common/geometry/_point.py +238 -0
  13. loop_common-0.0.1/src/loop_common/geometry/_structured_grid.py +114 -0
  14. loop_common-0.0.1/src/loop_common/geometry/_structured_grid_2d.py +146 -0
  15. loop_common-0.0.1/src/loop_common/geometry/_structured_grid_3d.py +243 -0
  16. loop_common-0.0.1/src/loop_common/geometry/_surface.py +288 -0
  17. loop_common-0.0.1/src/loop_common/geometry/_unstructured_mesh.py +208 -0
  18. loop_common-0.0.1/src/loop_common/interfaces/__init__.py +0 -0
  19. loop_common-0.0.1/src/loop_common/interfaces/representation.py +45 -0
  20. loop_common-0.0.1/src/loop_common/io/__init__.py +0 -0
  21. loop_common-0.0.1/src/loop_common/logging/__init__.py +14 -0
  22. loop_common-0.0.1/src/loop_common/logging/logger.py +123 -0
  23. loop_common-0.0.1/src/loop_common/logging/sinks.py +221 -0
  24. loop_common-0.0.1/src/loop_common/logging/timing.py +113 -0
  25. loop_common-0.0.1/src/loop_common/math/__init__.py +5 -0
  26. loop_common-0.0.1/src/loop_common/math/_maths.py +441 -0
  27. loop_common-0.0.1/src/loop_common/math/_transformation.py +178 -0
  28. loop_common-0.0.1/src/loop_common/math/finite_difference_stencil.py +38 -0
  29. loop_common-0.0.1/src/loop_common/observations/__init__.py +3 -0
  30. loop_common-0.0.1/src/loop_common/observations/lineset.py +41 -0
  31. loop_common-0.0.1/src/loop_common/observations/orientation.py +122 -0
  32. loop_common-0.0.1/src/loop_common/observations/pointset.py +8 -0
  33. loop_common-0.0.1/src/loop_common/observer.py +284 -0
  34. loop_common-0.0.1/src/loop_common/supports/_2d_base_unstructured.py +361 -0
  35. loop_common-0.0.1/src/loop_common/supports/_2d_p1_unstructured.py +131 -0
  36. loop_common-0.0.1/src/loop_common/supports/_2d_p2_unstructured.py +349 -0
  37. loop_common-0.0.1/src/loop_common/supports/_2d_structured_grid.py +545 -0
  38. loop_common-0.0.1/src/loop_common/supports/_3d_base_structured.py +546 -0
  39. loop_common-0.0.1/src/loop_common/supports/_3d_p2_tetra.py +374 -0
  40. loop_common-0.0.1/src/loop_common/supports/_3d_rectilinear_grid.py +315 -0
  41. loop_common-0.0.1/src/loop_common/supports/_3d_structured_grid.py +510 -0
  42. loop_common-0.0.1/src/loop_common/supports/_3d_structured_tetra.py +764 -0
  43. loop_common-0.0.1/src/loop_common/supports/_3d_unstructured_tetra.py +656 -0
  44. loop_common-0.0.1/src/loop_common/supports/__init__.py +69 -0
  45. loop_common-0.0.1/src/loop_common/supports/_aabb.py +77 -0
  46. loop_common-0.0.1/src/loop_common/supports/_base_support.py +118 -0
  47. loop_common-0.0.1/src/loop_common/supports/_face_table.py +70 -0
  48. loop_common-0.0.1/src/loop_common/supports/_p2_structured_tetra.py +627 -0
  49. loop_common-0.0.1/src/loop_common/supports/_support_factory.py +78 -0
  50. loop_common-0.0.1/src/loop_common/utils.py +74 -0
  51. loop_common-0.0.1/src/loop_common.egg-info/PKG-INFO +35 -0
  52. loop_common-0.0.1/src/loop_common.egg-info/SOURCES.txt +64 -0
  53. loop_common-0.0.1/src/loop_common.egg-info/dependency_links.txt +1 -0
  54. loop_common-0.0.1/src/loop_common.egg-info/requires.txt +9 -0
  55. loop_common-0.0.1/src/loop_common.egg-info/top_level.txt +1 -0
  56. loop_common-0.0.1/tests/test_2d_discrete_support.py +61 -0
  57. loop_common-0.0.1/tests/test_base.py +172 -0
  58. loop_common-0.0.1/tests/test_bounding_box.py +130 -0
  59. loop_common-0.0.1/tests/test_discrete_supports.py +163 -0
  60. loop_common-0.0.1/tests/test_imports.py +36 -0
  61. loop_common-0.0.1/tests/test_observations.py +72 -0
  62. loop_common-0.0.1/tests/test_p0_pointset_serialization.py +75 -0
  63. loop_common-0.0.1/tests/test_p2_structured_tetra.py +358 -0
  64. loop_common-0.0.1/tests/test_rectilinear_grid.py +311 -0
  65. loop_common-0.0.1/tests/test_structured_grid_boundary_eval.py +41 -0
  66. loop_common-0.0.1/tests/test_unstructured_supports.py +126 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Lachlan Grose
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,35 @@
1
+ Metadata-Version: 2.4
2
+ Name: loop-common
3
+ Version: 0.0.1
4
+ Summary: Common utilities for LoopStructural
5
+ Author-email: Lachlan Grose <lachlan.grose@monash.edu>
6
+ License: MIT
7
+ Classifier: Development Status :: 5 - Production/Stable
8
+ Classifier: Intended Audience :: Science/Research
9
+ Classifier: Topic :: Scientific/Engineering :: Information Analysis
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: Microsoft :: Windows
12
+ Classifier: Operating System :: POSIX
13
+ Classifier: Operating System :: MacOS
14
+ Classifier: Programming Language :: Python :: 3.9
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Requires-Python: >=3.9
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ Requires-Dist: numpy
22
+ Requires-Dist: pandas
23
+ Requires-Dist: pydantic
24
+ Requires-Dist: scipy
25
+ Requires-Dist: pyvista
26
+ Requires-Dist: pyyaml
27
+ Provides-Extra: tests
28
+ Requires-Dist: pytest; extra == "tests"
29
+ Dynamic: license-file
30
+
31
+ # loop-common
32
+
33
+ Common utilities for LoopStructural, including bounding box geometry helpers,
34
+ mesh/grid support types, and shared math/logging utilities used across the
35
+ LoopStructural workspace packages.
@@ -0,0 +1,5 @@
1
+ # loop-common
2
+
3
+ Common utilities for LoopStructural, including bounding box geometry helpers,
4
+ mesh/grid support types, and shared math/logging utilities used across the
5
+ LoopStructural workspace packages.
@@ -0,0 +1,33 @@
1
+ [build-system]
2
+ requires = ["setuptools"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "loop-common"
7
+ description = "Common utilities for LoopStructural"
8
+ version = "0.0.1"
9
+ requires-python = ">=3.9"
10
+ authors = [{ name = "Lachlan Grose", email = "lachlan.grose@monash.edu" }]
11
+ readme = "README.md"
12
+ license = { text = "MIT" }
13
+ classifiers = [
14
+ "Development Status :: 5 - Production/Stable",
15
+ "Intended Audience :: Science/Research",
16
+ "Topic :: Scientific/Engineering :: Information Analysis",
17
+ "License :: OSI Approved :: MIT License",
18
+ "Operating System :: Microsoft :: Windows",
19
+ "Operating System :: POSIX",
20
+ "Operating System :: MacOS",
21
+ "Programming Language :: Python :: 3.9",
22
+ "Programming Language :: Python :: 3.10",
23
+ "Programming Language :: Python :: 3.11",
24
+ "Programming Language :: Python :: 3.12",
25
+ ]
26
+ dependencies = ["numpy", "pandas", "pydantic", "scipy", "pyvista", "pyyaml"]
27
+
28
+ [project.optional-dependencies]
29
+ tests = ["pytest"]
30
+
31
+ [tool.setuptools.packages.find]
32
+ where = ["src"]
33
+ include = ["loop_common", "loop_common.*"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,6 @@
1
+ # Make submodules available for import
2
+
3
+ from . import geometry, io, logging, math, supports
4
+
5
+ # Expose get_logger at the package level
6
+ from .logging.logger import get_logger
@@ -0,0 +1,98 @@
1
+ from __future__ import annotations
2
+
3
+ import uuid
4
+ from datetime import datetime, timezone
5
+ from pathlib import Path
6
+ from typing import Annotated, Any
7
+
8
+ import numpy as np
9
+ from pydantic import BaseModel, BeforeValidator, ConfigDict, Field, PlainSerializer
10
+
11
+ from loop_common.logging import get_logger as getLogger
12
+
13
+ logger = getLogger(__name__)
14
+ # --- 1. The NumPy Type Logic ---
15
+
16
+
17
+ def validate_numpy(v: Any) -> np.ndarray:
18
+ """Ensures input is converted to a numpy array."""
19
+ if isinstance(v, np.ndarray):
20
+ return v
21
+ try:
22
+ return np.array(v)
23
+ except Exception as e:
24
+ raise ValueError(f"Could not convert {type(v)} to numpy array") from e
25
+
26
+
27
+ # Define a 'NumpyArray' type that:
28
+ # - Converts lists/tuples to arrays during input (BeforeValidator)
29
+ # - Converts arrays to lists during JSON export (PlainSerializer)
30
+ NumpyArray = Annotated[
31
+ np.ndarray,
32
+ BeforeValidator(validate_numpy),
33
+ PlainSerializer(lambda x: x.tolist(), return_type=list),
34
+ ]
35
+
36
+ # --- 2. The Base Entity ---
37
+
38
+
39
+ class LoopEntity(BaseModel):
40
+ """
41
+ The atomic building block for all Loop objects.
42
+ Provides identity, validation, and serialization.
43
+ """
44
+
45
+ # Allow Pydantic to handle non-pydantic types (like numpy arrays)
46
+ model_config = ConfigDict(
47
+ arbitrary_types_allowed=True,
48
+ validate_assignment=True, # Validate if user changes a value later
49
+ extra="forbid", # Prevent accidental typos from creating new fields
50
+ )
51
+
52
+ uuid: str = Field(
53
+ default_factory=lambda: str(uuid.uuid4()), description="Permanent unique identifier"
54
+ )
55
+
56
+ name: str | None = Field(default=None, description="Human-readable label")
57
+
58
+ last_modified: str = Field(
59
+ default_factory=lambda: datetime.now(tz=timezone.utc).isoformat(),
60
+ description="ISO timestamp of last change",
61
+ )
62
+
63
+ def mark_modified(self):
64
+ """Manually trigger a timestamp update."""
65
+ self.last_modified = datetime.now(tz=timezone.utc).isoformat()
66
+
67
+ @classmethod
68
+ def from_json(cls, json_str: str):
69
+ """Helper to reconstruct the object from a JSON string."""
70
+ return cls.model_validate_json(json_str)
71
+
72
+ def to_json(self, indent: int = 2) -> str:
73
+ """Helper to export to JSON string."""
74
+ return self.model_dump_json(indent=indent)
75
+
76
+ def to_yaml(self) -> str:
77
+ """Export to YAML string."""
78
+ try:
79
+ import yaml
80
+ except ImportError as exc:
81
+ raise ImportError("PyYAML is required for YAML export: pip install pyyaml") from exc
82
+ return yaml.dump(self.model_dump(mode="json"), sort_keys=False, allow_unicode=True)
83
+
84
+ def to_dict(self) -> dict:
85
+ """Export to a plain dict (JSON-safe: numpy arrays converted to lists)."""
86
+ return self.model_dump(mode="json")
87
+
88
+ def save(self, filename: Path):
89
+ filename = Path(filename)
90
+ filetype = filename.suffix.lstrip(".")
91
+ if filetype == "json":
92
+ with open(filename,'w') as f:
93
+ f.write(self.to_json())
94
+ elif filetype == 'yaml':
95
+ with open(filename,'w') as f:
96
+ f.write(self.to_yaml())
97
+ else:
98
+ logger.warning(f"Unknown filetype {filetype}. Not saving.")
@@ -0,0 +1,19 @@
1
+ from ._bounding_box import BoundingBox
2
+ from ._point import ValuePoints, VectorPoints
3
+ from ._structured_grid import StructuredGrid
4
+ from ._structured_grid_2d import StructuredGrid2DGeometry
5
+ from ._structured_grid_3d import StructuredGrid3DGeometry
6
+ from ._surface import Surface
7
+ from ._unstructured_mesh import UnstructuredMesh2DGeometry, UnstructuredMeshGeometry
8
+
9
+ __all__ = [
10
+ "BoundingBox",
11
+ "StructuredGrid",
12
+ "StructuredGrid2DGeometry",
13
+ "StructuredGrid3DGeometry",
14
+ "Surface",
15
+ "UnstructuredMesh2DGeometry",
16
+ "UnstructuredMeshGeometry",
17
+ "ValuePoints",
18
+ "VectorPoints",
19
+ ]
@@ -0,0 +1,37 @@
1
+ import numpy as np
2
+ from scipy import sparse
3
+
4
+
5
+ def _initialise_aabb(grid):
6
+ minx = np.min(grid.nodes[grid.elements[:, :4], 0], axis=1)
7
+ maxx = np.max(grid.nodes[grid.elements[:, :4], 0], axis=1)
8
+ miny = np.min(grid.nodes[grid.elements[:, :4], 1], axis=1)
9
+ maxy = np.max(grid.nodes[grid.elements[:, :4], 1], axis=1)
10
+
11
+ cell_indexes = grid.aabb_grid.global_index_to_cell_index(np.arange(grid.aabb_grid.n_elements))
12
+ corners = grid.aabb_grid.cell_corner_indexes(cell_indexes)
13
+ positions = grid.aabb_grid.node_indexes_to_position(corners)
14
+ x_boundary = positions[:, [0, 1], 0]
15
+ y_boundary = positions[:, [0, 2], 1]
16
+ a = np.logical_and(minx[None, :] > x_boundary[:, None, 0], minx[None, :] < x_boundary[:, None, 1])
17
+ b = np.logical_and(maxx[None, :] < x_boundary[:, None, 1], maxx[None, :] > x_boundary[:, None, 0])
18
+ c = np.logical_and(minx[None, :] < x_boundary[:, None, 0], maxx[None, :] > x_boundary[:, None, 0])
19
+ x_logic = np.logical_or(np.logical_or(a, b), c)
20
+
21
+ a = np.logical_and(miny[None, :] > y_boundary[:, None, 0], miny[None, :] < y_boundary[:, None, 1])
22
+ b = np.logical_and(maxy[None, :] < y_boundary[:, None, 1], maxy[None, :] > y_boundary[:, None, 0])
23
+ c = np.logical_and(miny[None, :] < y_boundary[:, None, 0], maxy[None, :] > y_boundary[:, None, 0])
24
+ y_logic = np.logical_or(np.logical_or(a, b), c)
25
+ logic = np.logical_and(x_logic, y_logic)
26
+
27
+ if grid.dimension == 3:
28
+ z_boundary = positions[:, [0, 6], 2]
29
+ minz = np.min(grid.nodes[grid.elements[:, :4], 2], axis=1)
30
+ maxz = np.max(grid.nodes[grid.elements[:, :4], 2], axis=1)
31
+ a = np.logical_and(minz[None, :] > z_boundary[:, None, 0], minz[None, :] < z_boundary[:, None, 1])
32
+ b = np.logical_and(maxz[None, :] < z_boundary[:, None, 1], maxz[None, :] > z_boundary[:, None, 0])
33
+ c = np.logical_and(minz[None, :] < z_boundary[:, None, 0], maxz[None, :] > z_boundary[:, None, 0])
34
+ z_logic = np.logical_or(np.logical_or(a, b), c)
35
+ logic = np.logical_and(logic, z_logic)
36
+
37
+ grid._aabb_table = sparse.csr_matrix(logic)