pyeggp 1.0.0__cp313-cp313-win_amd64.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.
pyeggp/__init__.py ADDED
@@ -0,0 +1,125 @@
1
+ """""" # start delvewheel patch
2
+ def _delvewheel_patch_1_10_0():
3
+ import os
4
+ if os.path.isdir(libs_dir := os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir, 'pyeggp.libs'))):
5
+ os.add_dll_directory(libs_dir)
6
+
7
+
8
+ _delvewheel_patch_1_10_0()
9
+ del _delvewheel_patch_1_10_0
10
+ # end delvewheel patch
11
+
12
+ import atexit
13
+ from contextlib import contextmanager
14
+ from threading import Lock
15
+ from typing import Iterator, List
16
+ from io import StringIO
17
+ import tempfile
18
+ import csv
19
+
20
+ import numpy as np
21
+ import pandas as pd
22
+ from sklearn.base import BaseEstimator, RegressorMixin
23
+ from sklearn.utils.validation import check_X_y, check_array, check_is_fitted
24
+ from sklearn.metrics import mean_squared_error, r2_score
25
+
26
+ from ._binding import (
27
+ unsafe_hs_pyeggp_version,
28
+ unsafe_hs_pyeggp_main,
29
+ unsafe_hs_pyeggp_run,
30
+ unsafe_hs_pyeggp_init,
31
+ unsafe_hs_pyeggp_exit,
32
+ )
33
+
34
+ VERSION: str = "1.3.0"
35
+
36
+
37
+ _hs_rts_init: bool = False
38
+ _hs_rts_lock: Lock = Lock()
39
+
40
+
41
+ def hs_rts_exit() -> None:
42
+ global _hs_rts_lock
43
+ with _hs_rts_lock:
44
+ unsafe_hs_pyeggp_exit()
45
+
46
+
47
+ @contextmanager
48
+ def hs_rts_init(args: List[str] = []) -> Iterator[None]:
49
+ global _hs_rts_init
50
+ global _hs_rts_lock
51
+ with _hs_rts_lock:
52
+ if not _hs_rts_init:
53
+ _hs_rts_init = True
54
+ unsafe_hs_pyeggp_init(args)
55
+ atexit.register(hs_rts_exit)
56
+ yield None
57
+
58
+
59
+ def version() -> str:
60
+ with hs_rts_init():
61
+ return unsafe_hs_pyeggp_version()
62
+
63
+
64
+ def main(args: List[str] = []) -> int:
65
+ with hs_rts_init(args):
66
+ return unsafe_hs_pyeggp_main()
67
+
68
+ def pyeggp_run(dataset: str, gen: int, nPop: int, maxSize: int, nTournament: int, pc: float, pm: float, nonterminals: str, loss: str, optIter: int, optRepeat: int, nParams: int, split: int, simplify: int, dumpTo: str, loadFrom: str) -> str:
69
+ with hs_rts_init():
70
+ return unsafe_hs_pyeggp_run(dataset, gen, nPop, maxSize, nTournament, pc, pm, nonterminals, loss, optIter, optRepeat, nParams, split, simplify, dumpTo, loadFrom)
71
+
72
+ class PyEGGP(BaseEstimator, RegressorMixin):
73
+ def __init__(self, gen = 100, nPop = 100, maxSize = 15, nTournament = 3, pc = 0.9, pm = 0.3, nonterminals = "add,sub,mul,div", loss = "MSE", optIter = 50, optRepeat = 2, nParams = -1, split = 1, simplify = False, dumpTo = "", loadFrom = ""):
74
+ self.gen = gen
75
+ self.nPop = nPop
76
+ self.maxSize = maxSize
77
+ self.nTournament = nTournament
78
+ self.pc = pc
79
+ self.pm = pm
80
+ self.nonterminals = nonterminals
81
+ self.loss = loss
82
+ self.optIter = optIter
83
+ self.optRepeat = optRepeat
84
+ self.nParams = nParams
85
+ self.split = split
86
+ self.simplify = int(simplify)
87
+ self.dumpTo = dumpTo
88
+ self.loadFrom = loadFrom
89
+ self.is_fitted_ = False
90
+
91
+ def fit(self, X, y):
92
+ if X.ndim == 1:
93
+ X = X.reshape(-1,1)
94
+ y = y.reshape(-1, 1)
95
+ combined = np.hstack([X, y])
96
+ header = [f"x{i}" for i in range(X.shape[1])] + ["y"]
97
+ with tempfile.NamedTemporaryFile(mode='w+', newline='', delete=False, suffix='.csv') as temp_file:
98
+ writer = csv.writer(temp_file)
99
+ writer.writerow(header)
100
+ writer.writerows(combined)
101
+ dataset = temp_file.name
102
+
103
+ csv_data = pyeggp_run(dataset, self.gen, self.nPop, self.maxSize, self.nTournament, self.pc, self.pm, self.nonterminals, self.loss, self.optIter, self.optRepeat, self.nParams, self.split, self.simplify, self.dumpTo, self.loadFrom)
104
+ if len(csv_data) > 0:
105
+ csv_io = StringIO(csv_data.strip())
106
+ self.results = pd.read_csv(csv_io, header=0)
107
+ self.is_fitted_ = True
108
+ return self
109
+
110
+ def predict(self, X):
111
+ check_is_fitted(self)
112
+ return self.evaluate_best_model(self.model_, X)
113
+ def evaluate_best_model(self, x):
114
+ if x.ndim == 1:
115
+ x = x.reshape(-1,1)
116
+ t = np.array(list(map(float, self.results.iloc[-1].theta.split(";"))))
117
+ return eval(self.results.iloc[-1].Numpy)
118
+ def evaluate_model(self, ix, x):
119
+ if x.ndim == 1:
120
+ x = x.reshape(-1,1)
121
+ t = np.array(list(map(float, self.results.iloc[-1].theta.split(";"))))
122
+ return eval(self.results.iloc[i].Numpy)
123
+ def score(self, X, y):
124
+ ypred = self.evaluate_best_model(X)
125
+ return r2_score(y, ypred)
pyeggp/__main__.py ADDED
@@ -0,0 +1,11 @@
1
+ import sys
2
+ from typing import NoReturn
3
+ import pyeggp
4
+
5
+
6
+ def main() -> NoReturn:
7
+ sys.exit(pyeggp.main(sys.argv))
8
+
9
+
10
+ if __name__ == "__main__":
11
+ main()
Binary file
pyeggp/_binding.py ADDED
@@ -0,0 +1,20 @@
1
+ def __bootstrap__():
2
+ global __bootstrap__, __file__, __loader__
3
+ import sys, os, pkg_resources, importlib.util
4
+ __file__ = pkg_resources.resource_filename(__name__,'_binding.cp313-win_amd64.pyd')
5
+ del __bootstrap__
6
+ if '__loader__' in globals():
7
+ del __loader__
8
+
9
+ old_dir = os.getcwd()
10
+ try:
11
+ os.chdir(os.path.dirname(__file__))
12
+
13
+ spec = importlib.util.spec_from_file_location(
14
+ __name__, __file__)
15
+ mod = importlib.util.module_from_spec(spec)
16
+ spec.loader.exec_module(mod)
17
+ finally:
18
+
19
+ os.chdir(old_dir)
20
+ __bootstrap__()
pyeggp/_binding.pyi ADDED
@@ -0,0 +1,7 @@
1
+ from typing import List
2
+
3
+ def unsafe_hs_pyeggp_version() -> str: ...
4
+ def unsafe_hs_pyeggp_main() -> int: ...
5
+ def unsafe_hs_pyeggp_run(dataset: str, gen: int, nPop: int, maxSize: int, nTournament: int, pc: float, pm: float, nonterminals: str, loss: str, optIter: int, optRepeat: int, nParams: int, split: int, simplify: int, dumpTo: str, loadFrom: str) -> str: ...
6
+ def unsafe_hs_pyeggp_init(args: List[str]) -> None: ...
7
+ def unsafe_hs_pyeggp_exit() -> None: ...
pyeggp/binding.i ADDED
@@ -0,0 +1,64 @@
1
+ %module binding
2
+ %{
3
+ #include "Pyeggp_stub.h"
4
+
5
+ char * unsafe_hs_pyeggp_version() {
6
+ return hs_pyeggp_version();
7
+ }
8
+
9
+ int unsafe_hs_pyeggp_main() {
10
+ return hs_pyeggp_main();
11
+ }
12
+
13
+ char * unsafe_hs_pyeggp_run( char *dataset, int gens, int nPop, int maxSize, int nTournament, double pc, double pm, char *nonterminals, char *loss, int optIter, int optRepeat, int nParams, int split, int simplify, char *dumpTo, char *loadFrom ) {
14
+ return hs_pyeggp_run(dataset, gens, nPop, maxSize, nTournament, pc, pm, nonterminals, loss, optIter, optRepeat, nParams, split, simplify, dumpTo, loadFrom);
15
+ }
16
+
17
+ void unsafe_hs_pyeggp_init(int argc, char **argv) {
18
+ hs_init(&argc, &argv);
19
+ }
20
+
21
+ void unsafe_hs_pyeggp_exit() {
22
+ hs_exit();
23
+ }
24
+
25
+ void unsafe_py_write_stdout( char * str) {
26
+ PySys_FormatStdout("%s", str);
27
+ }
28
+
29
+ void unsafe_py_write_stderr( char * str) {
30
+ PySys_FormatStderr("%s", str);
31
+ }
32
+ %}
33
+
34
+ %typemap(in) (int argc, char **argv) {
35
+ /* Check if is a list */
36
+ if (PyList_Check($input)) {
37
+ int i;
38
+ $1 = PyList_Size($input);
39
+ $2 = (char **) malloc(($1+1)*sizeof(char *));
40
+ for (i = 0; i < $1; i++) {
41
+ PyObject *o = PyList_GetItem($input, i);
42
+ if (PyUnicode_Check(o)) {
43
+ $2[i] = (char *) PyUnicode_AsUTF8AndSize(o, 0);
44
+ } else {
45
+ PyErr_SetString(PyExc_TypeError, "list must contain strings");
46
+ SWIG_fail;
47
+ }
48
+ }
49
+ $2[i] = 0;
50
+ } else {
51
+ PyErr_SetString(PyExc_TypeError, "not a list");
52
+ SWIG_fail;
53
+ }
54
+ }
55
+
56
+ %typemap(freearg) (int argc, char **argv) {
57
+ free((char *) $2);
58
+ }
59
+
60
+ char * unsafe_hs_pyeggp_version();
61
+ int unsafe_hs_pyeggp_main();
62
+ char * unsafe_hs_pyeggp_run( char *dataset, int gens, int nPop, int maxSize, int nTournament, double p, double pm, char *nonterminals, char *loss, int optIter, int optRepeat, int nParams, int split, int simplify, char *dumpTo, char *loadFrom);
63
+ void unsafe_hs_pyeggp_init(int argc, char **argv);
64
+ void unsafe_hs_pyeggp_exit();
pyeggp/binding.py ADDED
@@ -0,0 +1,76 @@
1
+ # This file was automatically generated by SWIG (https://www.swig.org).
2
+ # Version 4.3.0
3
+ #
4
+ # Do not make changes to this file unless you know what you are doing - modify
5
+ # the SWIG interface file instead.
6
+
7
+ from sys import version_info as _swig_python_version_info
8
+ # Import the low-level C/C++ module
9
+ if __package__ or "." in __name__:
10
+ from . import _binding
11
+ else:
12
+ import _binding
13
+
14
+ try:
15
+ import builtins as __builtin__
16
+ except ImportError:
17
+ import __builtin__
18
+
19
+ def _swig_repr(self):
20
+ try:
21
+ strthis = "proxy of " + self.this.__repr__()
22
+ except __builtin__.Exception:
23
+ strthis = ""
24
+ return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,)
25
+
26
+
27
+ def _swig_setattr_nondynamic_instance_variable(set):
28
+ def set_instance_attr(self, name, value):
29
+ if name == "this":
30
+ set(self, name, value)
31
+ elif name == "thisown":
32
+ self.this.own(value)
33
+ elif hasattr(self, name) and isinstance(getattr(type(self), name), property):
34
+ set(self, name, value)
35
+ else:
36
+ raise AttributeError("You cannot add instance attributes to %s" % self)
37
+ return set_instance_attr
38
+
39
+
40
+ def _swig_setattr_nondynamic_class_variable(set):
41
+ def set_class_attr(cls, name, value):
42
+ if hasattr(cls, name) and not isinstance(getattr(cls, name), property):
43
+ set(cls, name, value)
44
+ else:
45
+ raise AttributeError("You cannot add class attributes to %s" % cls)
46
+ return set_class_attr
47
+
48
+
49
+ def _swig_add_metaclass(metaclass):
50
+ """Class decorator for adding a metaclass to a SWIG wrapped class - a slimmed down version of six.add_metaclass"""
51
+ def wrapper(cls):
52
+ return metaclass(cls.__name__, cls.__bases__, cls.__dict__.copy())
53
+ return wrapper
54
+
55
+
56
+ class _SwigNonDynamicMeta(type):
57
+ """Meta class to enforce nondynamic attributes (no new attributes) for a class"""
58
+ __setattr__ = _swig_setattr_nondynamic_class_variable(type.__setattr__)
59
+
60
+
61
+
62
+ def unsafe_hs_pyeggp_version():
63
+ return _binding.unsafe_hs_pyeggp_version()
64
+
65
+ def unsafe_hs_pyeggp_main():
66
+ return _binding.unsafe_hs_pyeggp_main()
67
+
68
+ def unsafe_hs_pyeggp_run(dataset, gens, nPop, maxSize, nTournament, p, pm, nonterminals, loss, optIter, optRepeat, nParams, split, simplify, dumpTo, loadFrom):
69
+ return _binding.unsafe_hs_pyeggp_run(dataset, gens, nPop, maxSize, nTournament, p, pm, nonterminals, loss, optIter, optRepeat, nParams, split, simplify, dumpTo, loadFrom)
70
+
71
+ def unsafe_hs_pyeggp_init(argc):
72
+ return _binding.unsafe_hs_pyeggp_init(argc)
73
+
74
+ def unsafe_hs_pyeggp_exit():
75
+ return _binding.unsafe_hs_pyeggp_exit()
76
+
pyeggp/typing.py ADDED
@@ -0,0 +1,14 @@
1
+ import sys
2
+
3
+ from typing import List
4
+
5
+ if sys.version_info < (3, 8):
6
+ from typing_extensions import Protocol
7
+ else:
8
+ from typing import Protocol
9
+
10
+
11
+ class Session(Protocol):
12
+ def version(self) -> str: ...
13
+ def main(self, args: List[str] = []) -> int: ...
14
+ def pyeggp_run(self, dataset: str, gen: int, nPop: int, maxSize: int, nTournament: int, pc: float, pm: float, nonterminals: str, loss: str, optIter: int, optRepeat: int, nParams: int, split: int, simplify: int, dumpTo: str, loadFrom: str) -> str: ...
@@ -0,0 +1,2 @@
1
+ Version: 1.10.0
2
+ Arguments: ['C:\\hostedtoolcache\\windows\\Python\\3.12.9\\x64\\Scripts\\delvewheel', 'repair', '-w', 'C:\\Users\\runneradmin\\AppData\\Local\\Temp\\cibw-run-08n10a1r\\cp313-win_amd64\\repaired_wheel', 'C:\\Users\\runneradmin\\AppData\\Local\\Temp\\cibw-run-08n10a1r\\cp313-win_amd64\\built_wheel\\pyeggp-1.0.0-cp313-cp313-win_amd64.whl', '--add-path', 'C:\\nlopt\\bin']