lf-pollywog 0.1.1__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.
docs/conf.py ADDED
@@ -0,0 +1,39 @@
1
+ # Configuration file for the Sphinx documentation builder.
2
+
3
+ import os
4
+ import sys
5
+
6
+ sys.path.insert(0, os.path.abspath(".."))
7
+ #
8
+ # For the full list of built-in configuration values, see the documentation:
9
+ # https://www.sphinx-doc.org/en/master/usage/configuration.html
10
+
11
+ # -- Project information -----------------------------------------------------
12
+ # https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
13
+
14
+ project = "pollywog"
15
+ copyright = "2025, Arthur Endlein"
16
+ author = "Arthur Endlein"
17
+ release = "0.1.0"
18
+
19
+ # -- General configuration ---------------------------------------------------
20
+ # https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
21
+
22
+ extensions = [
23
+ "sphinx.ext.autodoc",
24
+ "sphinx_autodoc_typehints",
25
+ ]
26
+ # -- Options for HTML output -------------------------------------------------
27
+ # https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
28
+
29
+ autodoc_member_order = "bysource"
30
+
31
+ templates_path = ["_templates"]
32
+ exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
33
+
34
+
35
+ # -- Options for HTML output -------------------------------------------------
36
+ # https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
37
+
38
+ html_theme = "alabaster"
39
+ html_static_path = ["_static"]
@@ -0,0 +1,166 @@
1
+ Metadata-Version: 2.4
2
+ Name: lf_pollywog
3
+ Version: 0.1.1
4
+ Summary: Python library for working with Leapfrog calculation sets (.lfcalc files)
5
+ Author: Arthur Endlein
6
+ Author-email: Arthur Endlein <endarthur@gmail.com>
7
+ License: MIT
8
+ Project-URL: Homepage, https://github.com/endarthur/pollywog
9
+ Requires-Python: >=3.7
10
+ Description-Content-Type: text/markdown
11
+ License-File: LICENSE
12
+ Provides-Extra: conversion
13
+ Requires-Dist: scikit-learn; extra == "conversion"
14
+ Provides-Extra: dev
15
+ Requires-Dist: pytest; extra == "dev"
16
+ Requires-Dist: scikit-learn; extra == "dev"
17
+ Requires-Dist: pandas; extra == "dev"
18
+ Dynamic: author
19
+ Dynamic: license-file
20
+ Dynamic: requires-python
21
+
22
+ # Automating Leapfrog Workflows with Pollywog – An Independent Open-Source Tool
23
+
24
+ [![DOI](https://zenodo.org/badge/1071742254.svg)](https://doi.org/10.5281/zenodo.17313856)
25
+
26
+ Professionals using Seequent solutions for geological modeling and resource estimation often work with .lfcalc files. These files can be repetitive to manage and prone to manual errors. This is especially true when dealing with conditional logic, domain-based dilution calculations, or predictive model integration.
27
+
28
+ Pollywog was developed to support this technical audience. It is a Python package that enables:
29
+
30
+ - Programmatic reading and writing of .lfcalc files, making calculations more standardized and reproducible
31
+ - Automation of complex workflows, including conditional equations and post-processing of results
32
+ - Integration with machine learning models via scikit-learn, allowing classifiers or regressions to be applied directly within Leapfrog calculations
33
+ - Creation of reusable scripts, which can be versioned and audited, providing greater control over modeling processes
34
+
35
+ Pollywog aims to reduce time spent on manual tasks, minimize input errors, and increase efficiency in geological modeling.
36
+
37
+ The documentation includes practical examples and tutorials to help technical teams get started quickly.
38
+
39
+ If you work with Leapfrog and are looking to optimize your workflows, Pollywog is worth exploring.
40
+
41
+ Pollywog is still very much a work in progress, so take care in its use and make sure to not have anything important open and not saved in Leapfrog while testing it out. Also, please report any issues you encounter. Suggestions and contributions are very welcome!
42
+
43
+ ## Legal Disclaimer
44
+
45
+ Pollywog is an independent open-source tool developed to support the automation of workflows involving .lfcalc files used in Leapfrog software by Seequent.
46
+ This tool does not perform reverse engineering, does not modify Leapfrog, and does not access its source code or proprietary libraries. Pollywog operates exclusively on user-generated files and is designed to complement Leapfrog through external automation.
47
+
48
+ Important:
49
+ - Pollywog is not affiliated with, endorsed by, or sponsored by Seequent or any company associated with Leapfrog
50
+ - Use of this tool does not violate Leapfrog’s license terms or Seequent’s policies
51
+ - Users are encouraged to review Leapfrog’s terms of use before integrating Pollywog into commercial or corporate environments
52
+ - The author is not responsible for any misuse of the tool that may breach Seequent’s licensing terms
53
+
54
+
55
+ ## Installation
56
+
57
+ Install from pypi (not available yet, but soon):
58
+
59
+ ```bash
60
+ pip install lf_pollywog
61
+ ```
62
+
63
+ Or install the latest development version from GitHub:
64
+
65
+ ```bash
66
+ pip install git+https://github.com/endarthur/pollywog.git
67
+ ```
68
+
69
+ ## Usage
70
+
71
+ ### Reading and Writing `.lfcalc` files
72
+
73
+ ```python
74
+ import pollywog as pw
75
+ calcset = pw.CalcSet.read_lfcalc("path/to/file.lfcalc")
76
+ calcset.to_lfcalc("output.lfcalc")
77
+ ```
78
+
79
+ ### Creating a Simple Calculation Set
80
+
81
+ ```python
82
+ from pollywog.core import Number, CalcSet
83
+ calcset = CalcSet([
84
+ Number(name="Au_final", children=["clamp([Au_est], 0)"]),
85
+ Number(name="Ag_final", children=["clamp([Ag_est], 0)"])
86
+ ])
87
+ calcset.to_lfcalc("postprocessed.lfcalc")
88
+ ```
89
+
90
+ ### Converting a scikit-learn model
91
+
92
+ Currently supports decision trees (both classification and regression) and linear models:
93
+
94
+ ```python
95
+ from pollywog.conversion.sklearn import convert_tree
96
+ from sklearn.tree import DecisionTreeRegressor
97
+ import numpy as np
98
+ X = np.array([[0.2, 1.0, 10], [0.5, 2.0, 20]])
99
+ y = np.array([0.7, 0.8])
100
+ feature_names = ["Cu_final", "Au_final", "Ag_final"]
101
+ reg = DecisionTreeRegressor(max_depth=2)
102
+ reg.fit(X, y)
103
+ recovery_calc = convert_tree(reg, feature_names, "recovery_ml")
104
+ CalcSet([recovery_calc]).to_lfcalc("recovery_ml.lfcalc")
105
+ ```
106
+
107
+ For more advanced workflows (domain dilution, conditional logic, economic value, combining CalcSets, etc.), see the Jupyter notebooks in the `examples/` folder of this repository or the documentation at https://pollywog.readthedocs.io/en/latest/.
108
+
109
+ ## Querying CalcSets
110
+
111
+ Pollywog provides a powerful query method for filtering items in a `CalcSet`, inspired by pandas' DataFrame.query. You can use Python-like expressions to select items based on their attributes and external variables.
112
+
113
+ ### Syntax
114
+ - Use item attributes (e.g., `name`, `item_type`) in expressions.
115
+ - Reference external variables using `@var` syntax (e.g., `name.startswith(@prefix)`).
116
+ - Supported helpers: `len`, `any`, `all`, `min`, `max`, `sorted`, `re`, `str`.
117
+
118
+ ### Examples
119
+
120
+ ```python
121
+ # Select items whose name starts with 'Au'
122
+ calcset.query('name.startswith("Au")')
123
+
124
+ # Select items whose name starts with an external variable 'prefix'
125
+ prefix = "Ag"
126
+ calcset.query('name.startswith(@prefix)')
127
+
128
+ # Select items with more than one child
129
+ calcset.query('len(children) > 1')
130
+
131
+ # Use regular expressions
132
+ calcset.query('re.match(r"^A", name)')
133
+ ```
134
+
135
+ ### Notes
136
+ - External variables (`@var`) are resolved from the caller's scope or passed as keyword arguments.
137
+ - Only items matching the query expression are returned in the new `CalcSet`.
138
+
139
+ ## License
140
+
141
+ MIT License
142
+
143
+ <!-- ## Authors
144
+
145
+ See `AUTHORS` file or repository contributors. -->
146
+
147
+ ## Contributions
148
+
149
+ Contributions are very welcome!
150
+ If you'd like to collaborate on Pollywog, whether through bug fixes, feature enhancements, new use cases, or documentation, please follow these steps:
151
+
152
+ - Fork the repository
153
+ - Create a feature branch (git checkout -b feature-name)
154
+ - Make your changes and commit (git commit -m 'Add new feature')
155
+ - Submit a pull request with a clear explanation of your changes
156
+
157
+ Before contributing, please:
158
+ - Ensure your changes align with the project’s goals
159
+ - Maintain consistent code style
160
+ - Test your modifications whenever possible
161
+
162
+ Feel free to open an issue if you have questions or suggestions.
163
+
164
+ ## Acknowledgements
165
+
166
+ Thanks to Debora Roldão for helping with organization of the project, documentation and design, Eduardo Takafuji for the initial discussion of the feasability of this all those years ago and Jessica da Matta for support and sanity checks along the way.
@@ -0,0 +1,22 @@
1
+ docs/conf.py,sha256=p-qpiT0_Angy1mFUQ2Lo9CEmHTYq8btI6oPK4GFw-mw,1330
2
+ lf_pollywog-0.1.1.dist-info/licenses/LICENSE,sha256=MXMNCuTUaZEm8i5kH-5700l4scXAg7OtkK1oJGxAj50,1092
3
+ pollywog/__init__.py,sha256=4eW1G9ffdPHGspw_gNWhjWz1DvJ0IHa5bosou8vR1x4,143
4
+ pollywog/core.py,sha256=NKf5s_9sq1QTP6iXCaUn3zIuekXqHDGBRzyDRqFB5KM,29165
5
+ pollywog/display.py,sha256=qnz2Qnk0nGuvgT9hpYQ4g5jf2WIlib_phLEnDJG7Bas,6861
6
+ pollywog/helpers.py,sha256=o7JHMXYZrzRpTUUObMb-IVN4VAuk_l6fKSvRUpvVIuo,9434
7
+ pollywog/run.py,sha256=N0gvWEgn7OE2pgvomLkPmeyntKUR04MnIM4hPNtmAJo,4241
8
+ pollywog/utils.py,sha256=7zY92xZ5ZuezvrZQ9n5td7FzlYIhZdMEVO5RUg7zKW0,2686
9
+ pollywog/conversion/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
+ pollywog/conversion/sklearn.py,sha256=Q7ROJSAp7EQ-gNhGqd1kRtwRtvP_srCpEeeta_tLBuc,6216
11
+ tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
12
+ tests/conftest.py,sha256=9AG2ujuE0JVQTN7FwDKmBd8sr-gTYcaiSHtdKHNygNE,109
13
+ tests/test_conversion.py,sha256=GivwmDLqvWL5X9Oi_g8H9ec1LCwnbAzGokaELx5ABn8,2165
14
+ tests/test_core.py,sha256=q-10Z2pusPiXRol55OUR93pGsZrvHbqCu0S8H-zo7wg,8762
15
+ tests/test_display.py,sha256=hDSZB-F5PsfM7KYCH8GyBZKKJog2UaYatYpCbYHwRrc,2195
16
+ tests/test_helpers.py,sha256=xjAnKbnqEVXr5kTRgbKkWHixYI2uE1SYThA7s48Z9Vg,2700
17
+ tests/test_run.py,sha256=OSvDzzswLhwXvaBZOjK5F80CXpaijVq74hLFf6ro1wU,2419
18
+ tests/test_utils.py,sha256=SyPV_dGTdhaw5c2A0H9Cn69baKzCyMx2L11fYc8HzDo,1120
19
+ lf_pollywog-0.1.1.dist-info/METADATA,sha256=wDtGmHy-9aHhCicXOPXhYcRSJ5KxzExFZAZIRZqKLvU,6924
20
+ lf_pollywog-0.1.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
21
+ lf_pollywog-0.1.1.dist-info/top_level.txt,sha256=lMfdwk0fB3NwCWtYY9MShj6useQfVBas_0dLAoVJcfw,20
22
+ lf_pollywog-0.1.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Arthur Endlein
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,3 @@
1
+ docs
2
+ pollywog
3
+ tests
pollywog/__init__.py ADDED
@@ -0,0 +1,2 @@
1
+ from .core import CalcSet, Variable, Number, Category, Filter, If, IfRow
2
+ from .helpers import Sum, Average, Product, Normalize, WeightedAverage
File without changes
@@ -0,0 +1,154 @@
1
+ from ..core import If, IfRow, Variable, Number, Category
2
+
3
+ try:
4
+ import sklearn
5
+ except ImportError:
6
+ raise ImportError(
7
+ "scikit-learn is required for conversion. Please install it via 'pip install scikit-learn'."
8
+ )
9
+
10
+
11
+ # Classification and Regression Trees
12
+
13
+ from sklearn import tree, ensemble
14
+
15
+
16
+ def convert_tree(tree_model, feature_names, target_name, flat=False, comment_equation=None, output_type=None):
17
+ tree_ = tree_model.tree_
18
+ feature_name = [
19
+ feature_names[i] if i != tree._tree.TREE_UNDEFINED else "undefined!"
20
+ for i in tree_.feature
21
+ ]
22
+
23
+ is_classifier = hasattr(tree_model, "classes_")
24
+ classes = None
25
+ if is_classifier:
26
+ classes = tree_model.classes_
27
+
28
+ if flat:
29
+ # Create a flat list of conditions and values
30
+ conditions = []
31
+ values = []
32
+
33
+ def recurse_flat(node, current_conditions):
34
+ if tree_.feature[node] != tree._tree.TREE_UNDEFINED:
35
+ name = feature_name[node]
36
+ threshold = tree_.threshold[node]
37
+ left_conditions = current_conditions + [f"[{name}] <= {threshold}"]
38
+ right_conditions = current_conditions + [f"[{name}] > {threshold}"]
39
+ recurse_flat(tree_.children_left[node], left_conditions)
40
+ recurse_flat(tree_.children_right[node], right_conditions)
41
+ else:
42
+ value = tree_.value[node][0][0] if not is_classifier else classes[tree_.value[node][0].argmax()]
43
+ value = f'"{value}"' if isinstance(value, str) else str(value)
44
+ conditions.append(" and ".join(current_conditions) if current_conditions else "True")
45
+ values.append(value)
46
+
47
+ recurse_flat(0, [])
48
+ if_rows = [IfRow([cond], [val]) for cond, val in zip(conditions, values)]
49
+ if_rows = If(if_rows, otherwise=["blank"])
50
+ else:
51
+ # Create nested If structure
52
+ def recurse(node):
53
+ if tree_.feature[node] != tree._tree.TREE_UNDEFINED:
54
+ name = feature_name[node]
55
+ threshold = tree_.threshold[node]
56
+ left = recurse(tree_.children_left[node])
57
+ right = recurse(tree_.children_right[node])
58
+ return If(IfRow(f"[{name}] <= {threshold}", left), right)
59
+ else:
60
+ value = tree_.value[node][0][0] if not is_classifier else classes[tree_.value[node][0].argmax()]
61
+ value = f'"{value}"' if isinstance(value, str) else str(value)
62
+ return value
63
+
64
+ if_rows = recurse(0)
65
+
66
+
67
+ if comment_equation is None:
68
+ comment_equation = f"Converted from {tree_model.__class__.__name__}"
69
+
70
+ if output_type is not None:
71
+ return output_type(target_name, if_rows, comment_equation=comment_equation)
72
+
73
+ if isinstance(tree_model, tree.DecisionTreeRegressor):
74
+ return Number(target_name, if_rows, comment_equation=comment_equation)
75
+ elif isinstance(tree_model, tree.DecisionTreeClassifier):
76
+ return Category(target_name, if_rows, comment_equation=comment_equation)
77
+ else:
78
+ raise ValueError("Unsupported tree model type")
79
+
80
+ def convert_forest(forest_model, feature_names, target_name, flat=False, comment_equation=None):
81
+ """Convert a RandomForestRegressor or RandomForestClassifier to a Pollywog Number or Category.
82
+
83
+ Args:
84
+ forest_model: A fitted RandomForestRegressor or RandomForestClassifier from scikit-learn
85
+ feature_names: List of feature names used in the model
86
+ target_name: Name of the target variable to create
87
+ flat: Whether to create flattened if expressions (default: False)
88
+ comment_equation: Optional comment for the generated equation
89
+
90
+ Returns:
91
+ A Pollywog Number (for regression) or Category (for classification)
92
+ """
93
+ if not isinstance(forest_model, (ensemble.RandomForestRegressor, ensemble.RandomForestClassifier)):
94
+ raise ValueError("forest_model must be a RandomForestRegressor or RandomForestClassifier")
95
+
96
+ # Convert each tree in the forest
97
+ trees = [
98
+ convert_tree(
99
+ estimator,
100
+ feature_names,
101
+ f"{target_name}_tree_{i}",
102
+ flat=flat,
103
+ comment_equation=f"Tree {i} from {forest_model.__class__.__name__}",
104
+ output_type=Variable
105
+ )
106
+ for i, estimator in enumerate(forest_model.estimators_)
107
+ ]
108
+
109
+
110
+ # Average predictions for regression or majority vote for classification
111
+ if isinstance(forest_model, ensemble.RandomForestRegressor):
112
+ # For regression, create an equation that averages the tree outputs
113
+ tree_outputs = " + ".join([f"[{t.name}]" for t in trees])
114
+ equation = f"({tree_outputs}) / {len(trees)}"
115
+ return trees + [Number(
116
+ target_name,
117
+ [equation],
118
+ comment_equation=f"Averaged output from {len(trees)} trees in {forest_model.__class__.__name__}",
119
+ )]
120
+ elif isinstance(forest_model, ensemble.RandomForestClassifier):
121
+ # For classification, create an equation that does majority voting
122
+ tree_outputs = " + ".join([f"[{t.name}]" for t in trees])
123
+ equation = f"round(({tree_outputs}) / {len(trees)})"
124
+ return trees + [Category(
125
+ target_name,
126
+ [equation],
127
+ comment_equation=f"Majority vote from {len(trees)} trees in {forest_model.__class__.__name__}",
128
+ )]
129
+
130
+ # Linear Models
131
+ from sklearn import linear_model
132
+
133
+
134
+ def convert_linear_model(lm_model, feature_names, target_name):
135
+ coefs = lm_model.coef_
136
+ intercept = lm_model.intercept_
137
+
138
+ def format_float(val):
139
+ return f"{float(val):.6f}"
140
+
141
+ terms = [format_float(intercept)] if intercept != 0 else []
142
+ for coef, feature in zip(coefs, feature_names):
143
+ if coef != 0:
144
+ terms.append(f"{format_float(coef)} * [{feature}]")
145
+
146
+ equation = " + ".join(terms) if terms else "0"
147
+ return Number(
148
+ target_name,
149
+ [equation],
150
+ comment_equation=f"Converted from {lm_model.__class__.__name__}",
151
+ )
152
+
153
+ # Pre Processing
154
+