Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Test: Parameterized Tests",
"type": "debugpy",
"request": "launch",
"program": "parameterized_tests.py",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}/paramz/tests",
"justMyCode": false
}
]
}
2 changes: 1 addition & 1 deletion paramz/core/indexable.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def _raveled_index_for(self, param):
"""
from ..param import ParamConcatenation
if isinstance(param, ParamConcatenation):
return np.hstack((self._raveled_index_for(p) for p in param.params))
return np.hstack([self._raveled_index_for(p) for p in param.params])
return param._raveled_index() + self._offset_for(param)

def _raveled_index_for_transformed(self, param):
Expand Down
6 changes: 4 additions & 2 deletions paramz/core/parameter_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import re
import logging

from paramz.util import _set_mem_addr

from ..transformations import __fixed__, FIXED
from .constrainable import Constrainable
from .nameable import adjust_name_for_printing
Expand Down Expand Up @@ -287,8 +289,8 @@ def _propagate_param_grad(self, parray, garray):
self.param_array[pislice] = pi.param_array.flat # , requirements=['C', 'W']).flat
self.gradient_full[pislice] = pi.gradient_full.flat # , requirements=['C', 'W']).flat

pi.param_array.data = parray[pislice].data
pi.gradient_full.data = garray[pislice].data
_set_mem_addr(pi.param_array, parray[pislice])
_set_mem_addr(pi.gradient_full, garray[pislice])

pi._propagate_param_grad(parray[pislice], garray[pislice])
pi_old_size += pi.size
Expand Down
12 changes: 6 additions & 6 deletions paramz/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,9 @@ def _checkgrad(self, target_param=None, verbose=False, step=1e-6, tolerance=1e-3
for xind in zip(transformed_index):
xx = x.copy()
xx[xind] += step
f1 = float(self._objective(xx))
f1 = self._objective(xx)
xx[xind] -= 2.*step
f2 = float(self._objective(xx))
f2 = self._objective(xx)
#Avoid divide by zero, if any of the values are above 1e-15, otherwise both values are essentiall
#the same
if f1 > 1e-15 or f1 < -1e-15 or f2 > 1e-15 or f2 < -1e-15:
Expand All @@ -396,11 +396,11 @@ def _checkgrad(self, target_param=None, verbose=False, step=1e-6, tolerance=1e-3
if df_unstable: # pragma: no cover
formatted_name = "\033[94m {0} \033[0m".format(names[xind])

r = '%.6f' % float(ratio)
d = '%.6f' % float(difference)
r = '%.6f' % ratio
d = '%.6f' % difference
g = '%.6f' % gradient[xind]
ng = '%.6f' % float(numerical_gradient)
df = '%1.e' % float(df_ratio)
ng = '%.6f' % numerical_gradient
df = '%1.e' % df_ratio
grad_string = "{0:<{c0}}|{1:^{c1}}|{2:^{c2}}|{3:^{c3}}|{4:^{c4}}|{5:^{c5}}".format(formatted_name, r, d, g, ng, df, c0=cols[0] + 9, c1=cols[1], c2=cols[2], c3=cols[3], c4=cols[4], c5=cols[5])
print(grad_string)

Expand Down
9 changes: 6 additions & 3 deletions paramz/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#===============================================================================

import numpy; np = numpy
import ctypes
import numpy

from paramz.util import _set_mem_addr; np = numpy
from re import compile
try:
from re import _pattern_type
Expand Down Expand Up @@ -268,8 +271,8 @@ def _connect_parameters(self, ignore_added_names=False):
self.param_array[pslice] = p.param_array.flat # , requirements=['C', 'W']).ravel(order='C')
self.gradient_full[pslice] = p.gradient_full.flat # , requirements=['C', 'W']).ravel(order='C')

p.param_array.data = self.param_array[pslice].data
p.gradient_full.data = self.gradient_full[pslice].data
_set_mem_addr(p.param_array, self.param_array[pslice])
_set_mem_addr(p.gradient_full, self.gradient_full[pslice])

self._param_slices_.append(pslice)

Expand Down
8 changes: 4 additions & 4 deletions paramz/tests/model_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def test_constraints_set_direct(self):
#self.assertSequenceEqual(cache_str, str(self.testmodel), None, str)

def test_updates(self):
val = float(self.testmodel.objective_function())
val = self.testmodel.objective_function()
self.testmodel.update_toggle()
self.testmodel.kern.randomize(np.random.normal, loc=1, scale=.2)
self.testmodel.likelihood.randomize()
Expand All @@ -284,7 +284,7 @@ def test_set_gradients(self):

def test_fixing_optimize(self):
self.testmodel.kern.lengthscale.fix()
val = float(self.testmodel.kern.lengthscale)
val = self.testmodel.kern.lengthscale
self.testmodel.randomize()
self.assertEqual(val, self.testmodel.kern.lengthscale)
self.testmodel.optimize(max_iters=2)
Expand All @@ -299,7 +299,7 @@ def test_regular_expression_misc(self):
np.testing.assert_((self.testmodel[''][:2] == [10,10]).all())

self.testmodel.kern.lengthscale.fix()
val = float(self.testmodel.kern.lengthscale)
val = self.testmodel.kern.lengthscale
self.testmodel.randomize()
self.assertEqual(val, self.testmodel.kern.lengthscale)

Expand Down Expand Up @@ -354,7 +354,7 @@ def test_fix_unfix_constraints(self):


# Assert fixing works and does not randomize the - say - lengthscale:
val = float(self.testmodel.kern.lengthscale)
val = self.testmodel.kern.lengthscale
self.testmodel.randomize()
self.assertEqual(val, self.testmodel.kern.lengthscale)

Expand Down
17 changes: 10 additions & 7 deletions paramz/tests/parameterized_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ def test_num_params(self):
self.assertEqual(self.test1.add.rbf.num_params, 2)

def test_index_operations(self):
self.assertRaisesRegexp(AttributeError, "An index operation with the name constraints was already taken", self.test1.add_index_operation, 'constraints', None)
self.assertRaisesRegexp(AttributeError, "No index operation with the name", self.test1.remove_index_operation, 'not_an_index_operation')
self.assertRaisesRegex(AttributeError, "An index operation with the name constraints was already taken", self.test1.add_index_operation, 'constraints', None)
self.assertRaisesRegex(AttributeError, "No index operation with the name", self.test1.remove_index_operation, 'not_an_index_operation')

def test_names(self):
self.assertSequenceEqual(self.test1.parameter_names(adjust_for_printing=True), self.test1.parameter_names(adjust_for_printing=False))
Expand Down Expand Up @@ -182,7 +182,7 @@ def max_recursion():
from builtins import RecursionError as RE
except:
RE = RuntimeError
self.assertRaisesRegexp(RE, "aximum recursion depth", max_recursion)
self.assertRaisesRegex(RE, "aximum recursion depth", max_recursion)
# Recursion limit not reached if kernels are named individually:
sys.setrecursionlimit(1000)
p = Parameterized('add')
Expand Down Expand Up @@ -239,7 +239,7 @@ def test_remove_parameter_param_array_grad_array(self):
self.assertListEqual(self.test1.kern.param_array.tolist(), val[:2].tolist())

def test_add_parameter_already_in_hirarchy(self):
self.assertRaisesRegexp(HierarchyError, "You cannot add a parameter twice into the hierarchy", self.test1.link_parameter, self.white.parameters[0])
self.assertRaisesRegex(HierarchyError, "You cannot add a parameter twice into the hierarchy", self.test1.link_parameter, self.white.parameters[0])

def test_default_constraints(self):
self.assertIs(self.rbf.variance.constraints._param_index_ops, self.rbf.constraints._param_index_ops)
Expand Down Expand Up @@ -274,7 +274,7 @@ def test_constraints_views(self):

def test_fixing_randomize(self):
self.white.fix(warning=True)
val = float(self.white.variance)
val = self.white.variance
self.test1.randomize()
self.assertEqual(val, self.white.variance)

Expand All @@ -286,7 +286,7 @@ def test_randomize(self):

def test_fixing_randomize_parameter_handling(self):
self.rbf.fix(0.1, warning=True)
val = float(self.rbf.variance)
val = self.rbf.variance
self.test1.kern.randomize()
self.assertEqual(val, self.rbf.variance)

Expand All @@ -309,4 +309,7 @@ def test_printing(self):

if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.test_add_parameter']
unittest.main()
# unittest.main()
obj = ParameterizedTest()
obj.setUp()
obj.test_unfixed_param_array()
22 changes: 21 additions & 1 deletion paramz/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#===============================================================================
import warnings

def _inherit_doc(fromclass, done_classes = None):
inherited = ''
Expand All @@ -44,4 +45,23 @@ def _inherit_doc(fromclass, done_classes = None):
pass
inherited += _inherit_doc(c, done_classes=done_classes)

return inherited
return inherited


def _set_mem_addr(dest, src) -> None:
"""
This function serves to replace the `.data` getter/setter that existed in
`numpy<2` and got removed in `numpy>=2`.
The original behavior was setting the memory address of dest to that of src.
However, directly setting the memory address of a numpy array to the data of
another one seems to be unwanted in `numpy>=2`, which is causing some major
problems here.
"""
# with warnings.catch_warnings():
# warnings.simplefilter("ignore", DeprecationWarning)

# original
# dest.data = src.data

# take 1
dest.data = memoryview(src)
55 changes: 55 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
[project]
name = "paramz"
dynamic = ["version", "readme"]
authors = [
{name = "Max Zwiessele", email = "ibinbei@gmail.com"},
]
description = "The Parameterization Framework"
license = {text = "BSD 3-clause"}
keywords = ["machine-learning", "gaussian-processes", "kernels"]
classifiers = [
"License :: OSI Approved :: BSD License",
"Natural Language :: English",
"Operating System :: MacOS :: MacOS X",
"Operating System :: Microsoft :: Windows",
"Operating System :: POSIX :: Linux",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
]

dependencies = [
"numpy>=1.7",
"scipy",
"six",
"decorator>=4.0.10",
]

[project.optional-dependencies]
docs = ["sphinx"]
notebook = [
"jupyter_client >= 4.0.6",
"ipywidgets >= 4.0.3",
"ipykernel >= 4.1.0",
"notebook >= 4.0.5",
]
optimization = ["climin"]

[project.urls]
Homepage = "https://github.com/sods/paramz"

[tool.setuptools]
packages = ["paramz", "paramz.core", "paramz.optimization", "paramz.examples", "paramz.tests"]

[tool.setuptools.dynamic]
version = {attr = "paramz.__version__.__version__"}
readme = {file = ["README.rst"]}

[build-system]
requires = ["setuptools>=42", "wheel"]
build-backend = "setuptools.build_meta"
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
testpaths = paramz/tests
python_files = *_tests.py
Loading