Skip to content

Commit 5e8ca99

Browse files
committed
Tidy up and docs
1 parent 153676b commit 5e8ca99

3 files changed

Lines changed: 28 additions & 15 deletions

File tree

paramax/wrappers.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,29 +161,36 @@ def unwrap(self) -> T:
161161

162162

163163
class RealToIncreasingOnInterval(AbstractUnwrappable[Array]):
164-
"""Unconstrained vector to increasing on a fixed interval.
164+
"""Map an unconstrained vector to increasing points on a fixed interval.
165165
166-
Unconstrained vector is passed into softmax to obtain widths,
167-
which are cumulatively summed and scaled to fit the interval
168-
(or the remainder not used by min_width).
166+
The input vector is transformed via a softmax into positive widths, to fill
167+
the interval after adding minimum width. The cumulative sum of the widths
168+
produces the incresing points.
169169
170-
Note an array of size d parameterizes widths, so maps to an array
171-
of size d+1 if both endpoints are included, d if one is included,
172-
and d-1 if neither are included.
170+
If an array of size d is used, the result has size d+1 if both
171+
endpoints are included, d if one is included, and d-1 if neither
172+
are included.
173+
174+
Args:
175+
arr: Unconstrained vector parameterizing the widths.
176+
interval: (lower, upper) bounds of the interval.
177+
min_width: Minimum spacing between consecutive points.
178+
include_endpoints: Which endpoints to include: "both", "neither",
179+
"lower", or "upper".
173180
"""
174181

175182
arr: Array
176183
interval: tuple[float | int, float | int]
177184
min_width: float
178-
include_ends: Literal["both", "neither", "lower", "upper"]
185+
include_endpoints: Literal["both", "neither", "lower", "upper"]
179186

180187
def __init__(
181188
self,
182189
arr: Array,
183190
interval: tuple[float | int, float | int],
184191
*,
185192
min_width: float,
186-
include_ends: Literal["both", "neither", "lower", "upper"],
193+
include_endpoints: Literal["both", "neither", "lower", "upper"],
187194
):
188195
scale = interval[1] - interval[0]
189196
n_widths = arr.shape[-1]
@@ -199,10 +206,16 @@ def __init__(
199206
"min_width*n_widths is greater than the interval width, so cannot be "
200207
"satisfied."
201208
)
209+
210+
if include_endpoints not in ["both", "neither", "lower", "upper"]:
211+
raise ValueError(
212+
"include_endpoints must be one of 'both', 'neither', 'lower', or 'upper'",
213+
)
214+
202215
self.arr = arr
203216
self.interval = interval
204217
self.min_width = min_width
205-
self.include_ends = include_ends
218+
self.include_endpoints = include_endpoints
206219

207220
def unwrap(self) -> Array:
208221
scale = self.interval[1] - self.interval[0]
@@ -216,9 +229,9 @@ def _unwrap(arr):
216229
lower, upper = jnp.array([self.interval[0]]), jnp.array([self.interval[1]])
217230
return jnp.concatenate(
218231
[
219-
*([lower] if self.include_ends in ("lower", "both") else []),
232+
*([lower] if self.include_endpoints in ("lower", "both") else []),
220233
jnp.cumsum(widths, axis=-1)[:-1] + lower,
221-
*([upper] if self.include_ends in ("upper", "both") else []),
234+
*([upper] if self.include_endpoints in ("upper", "both") else []),
222235
]
223236
)
224237

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ license = { file = "LICENSE" }
1717
name = "paramax"
1818
readme = "README.md"
1919
requires-python = ">=3.10"
20-
version = "0.0.3"
20+
version = "0.0.4"
2121

2222
[project.urls]
2323
repository = "https://github.com/danielward27/paramax"

tests/test_wrappers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_RealToIncreasingOnInterval(case):
5454
case["arr"],
5555
(-1, 2),
5656
min_width=0.1,
57-
include_ends=case["include_ends"],
57+
include_endpoints=case["include_ends"],
5858
)
5959
result = unwrap(real_to_inc)
6060
assert pytest.approx(case["expected"]) == result
@@ -102,7 +102,7 @@ def test_WeightNormalization():
102102
"Parameterize-exp": lambda key: Parameterize(jnp.exp, jr.normal(key, (10,))),
103103
"WeightNormalization": lambda key: WeightNormalization(jr.normal(key, (10, 2))),
104104
"RealToIncreasingOnInterval": lambda key: RealToIncreasingOnInterval(
105-
jnp.zeros(10), (-7, 5), min_width=0.2, include_ends="upper"
105+
jnp.zeros(10), (-7, 5), min_width=0.2, include_endpoints="upper"
106106
),
107107
}
108108

0 commit comments

Comments
 (0)