Skip to content

Commit

Permalink
fixed attr errir
Browse files Browse the repository at this point in the history
  • Loading branch information
BalzaniEdoardo committed Oct 30, 2024
1 parent a708052 commit 4282d6a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
32 changes: 16 additions & 16 deletions src/nemos/basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ def __init__(

# these parameters are going to be set at the first call of `compute_features`
# since we cannot know a-priori how many features may be convolved
self._num_output_features = None
self._n_output_features = None
self._input_shape = None

self._label = str(label)
Expand Down Expand Up @@ -568,9 +568,9 @@ def _check_convolution_kwargs(self):
)

@property
def num_output_features(self) -> int | None:
def n_output_features(self) -> int | None:
"""Read-only property returning the number of features returned by the basis."""
return self._num_output_features
return self._n_output_features

@property
def label(self) -> str:
Expand Down Expand Up @@ -1216,7 +1216,7 @@ def _get_default_slicing(
if self._n_basis_input[0] == 1 or isinstance(self, MultiplicativeBasis):
split_dict = {
self.label: slice(
start_slice, start_slice + self._num_output_features
start_slice, start_slice + self._n_output_features
)
}
else:
Expand All @@ -1231,9 +1231,9 @@ def _get_default_slicing(
}
else:
split_dict = {
self.label: slice(start_slice, start_slice + self._num_output_features)
self.label: slice(start_slice, start_slice + self._n_output_features)
}
start_slice += self._num_output_features
start_slice += self._n_output_features
return split_dict, start_slice

def split_by_feature(
Expand Down Expand Up @@ -1352,11 +1352,11 @@ def split_by_feature(
"""

if x.shape[axis] != self.num_output_features:
if x.shape[axis] != self.n_output_features:
raise ValueError(
"`x.shape[axis]` does not match the expected number of features."
f" `x.shape[axis] == {x.shape[axis]}`, while the expected number "
f"of features is {self.num_output_features}"
f"of features is {self.n_output_features}"
)

# Get the slice dictionary based on predefined feature slicing
Expand Down Expand Up @@ -1419,7 +1419,7 @@ def _set_num_output_features(self, *xi: NDArray) -> Basis:
This function computes the number of inputs that are provided to the basis and uses
that number, and the n_basis_funcs to calculate the number of output features that
`self.compute_features` will return. These quantities and the input shape (excluding the sample axis)
are stored in `self._n_basis_input` and `self._num_output_features`, and `self._input_shape`
are stored in `self._n_basis_input` and `self._n_output_features`, and `self._input_shape`
respectively.
Parameters
Expand Down Expand Up @@ -1460,7 +1460,7 @@ def _set_num_output_features(self, *xi: NDArray) -> Basis:
n_inputs = (1,) if xi[0].ndim == 1 else (np.prod(shape),)

self._n_basis_input = n_inputs
self._num_output_features = self.n_basis_funcs * self._n_basis_input[0]
self._n_output_features = self.n_basis_funcs * self._n_basis_input[0]
return self


Expand Down Expand Up @@ -1490,7 +1490,7 @@ def __init__(self, basis1: Basis, basis2: Basis) -> None:
basis1._n_input_dimensionality + basis2._n_input_dimensionality
)
self._n_basis_input = None
self._num_output_features = None
self._n_output_features = None
self._label = "(" + basis1.label + " + " + basis2.label + ")"
self._basis1 = basis1
self._basis2 = basis2
Expand All @@ -1505,8 +1505,8 @@ def _set_num_output_features(self, *xi: NDArray) -> Basis:
*xi[self._basis1._n_input_dimensionality :]
)._n_basis_input,
)
self._num_output_features = (
self._basis1.num_output_features + self._basis2.num_output_features
self._n_output_features = (
self._basis1.n_output_features + self._basis2.n_output_features
)
return self

Expand Down Expand Up @@ -1616,7 +1616,7 @@ def __init__(self, basis1: Basis, basis2: Basis) -> None:
basis1._n_input_dimensionality + basis2._n_input_dimensionality
)
self._n_basis_input = None
self._num_output_features = None
self._n_output_features = None
self._label = "(" + basis1.label + " * " + basis2.label + ")"
self._basis1 = basis1
self._basis2 = basis2
Expand Down Expand Up @@ -1704,8 +1704,8 @@ def _set_num_output_features(self, *xi: NDArray) -> Basis:
*xi[self._basis1._n_input_dimensionality :]
)._n_basis_input,
)
self._num_output_features = (
self._basis1.num_output_features * self._basis2.num_output_features
self._n_output_features = (
self._basis1.n_output_features * self._basis2.n_output_features
)
return self

Expand Down
48 changes: 24 additions & 24 deletions tests/test_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,20 +607,20 @@ def test_init_label(self, label):
("label", None),
("label", "label"),
("n_basis_input", 1),
("num_output_features", 5),
("n_output_features", 5),
],
)
def test_attr_setter(self, attribute, value):
bas = self.cls(5)
with pytest.raises(AttributeError, match="can't set attribute"):
with pytest.raises(AttributeError, match=fr"can't set attribute|property '{attribute}' of"):
setattr(bas, attribute, value)

@pytest.mark.parametrize("n_input", [1, 2, 3])
def test_set_num_output_features(self, n_input):
bas = self.cls(5, mode="conv", window_size=10)
assert bas.num_output_features is None
assert bas.n_output_features is None
bas.compute_features(np.random.randn(20, n_input))
assert bas.num_output_features == n_input * bas.n_basis_funcs
assert bas.n_output_features == n_input * bas.n_basis_funcs

@pytest.mark.parametrize("n_input", [1, 2, 3])
def test_set_num_basis_input(self, n_input):
Expand Down Expand Up @@ -1451,20 +1451,20 @@ def test_init_label(self, label):
("label", None),
("label", "label"),
("n_basis_input", 1),
("num_output_features", 5),
("n_output_features", 5),
],
)
def test_attr_setter(self, attribute, value):
bas = self.cls(5)
with pytest.raises(AttributeError, match="can't set attribute"):
with pytest.raises(AttributeError, match=fr"can't set attribute|property '{attribute}' of"):
setattr(bas, attribute, value)

@pytest.mark.parametrize("n_input", [1, 2, 3])
def test_set_num_output_features(self, n_input):
bas = self.cls(5, mode="conv", window_size=10)
assert bas.num_output_features is None
assert bas.n_output_features is None
bas.compute_features(np.random.randn(20, n_input))
assert bas.num_output_features == n_input * bas.n_basis_funcs
assert bas.n_output_features == n_input * bas.n_basis_funcs

@pytest.mark.parametrize("n_input", [1, 2, 3])
def test_set_num_basis_input(self, n_input):
Expand Down Expand Up @@ -2244,20 +2244,20 @@ def test_init_label(self, label):
("label", None),
("label", "label"),
("n_basis_input", 1),
("num_output_features", 5),
("n_output_features", 5),
],
)
def test_attr_setter(self, attribute, value):
bas = self.cls(5)
with pytest.raises(AttributeError, match="can't set attribute"):
with pytest.raises(AttributeError, match=fr"can't set attribute|property '{attribute}' of"):
setattr(bas, attribute, value)

@pytest.mark.parametrize("n_input", [1, 2, 3])
def test_set_num_output_features(self, n_input):
bas = self.cls(5, mode="conv", window_size=10)
assert bas.num_output_features is None
assert bas.n_output_features is None
bas.compute_features(np.random.randn(20, n_input))
assert bas.num_output_features == n_input * bas.n_basis_funcs
assert bas.n_output_features == n_input * bas.n_basis_funcs

@pytest.mark.parametrize("n_input", [1, 2, 3])
def test_set_num_basis_input(self, n_input):
Expand Down Expand Up @@ -3154,20 +3154,20 @@ def test_init_label(self, label):
("label", None),
("label", "label"),
("n_basis_input", 1),
("num_output_features", 5),
("n_output_features", 5),
],
)
def test_attr_setter(self, attribute, value):
bas = self.cls(5, decay_rates=np.arange(1, 6))
with pytest.raises(AttributeError, match="can't set attribute"):
with pytest.raises(AttributeError, match=fr"can't set attribute|property '{attribute}' of"):
setattr(bas, attribute, value)

@pytest.mark.parametrize("n_input", [1, 2, 3])
def test_set_num_output_features(self, n_input):
bas = self.cls(5, mode="conv", window_size=10, decay_rates=np.arange(1, 6))
assert bas.num_output_features is None
assert bas.n_output_features is None
bas.compute_features(np.random.randn(20, n_input))
assert bas.num_output_features == n_input * bas.n_basis_funcs
assert bas.n_output_features == n_input * bas.n_basis_funcs

@pytest.mark.parametrize("n_input", [1, 2, 3])
def test_set_num_basis_input(self, n_input):
Expand Down Expand Up @@ -3925,20 +3925,20 @@ def test_init_label(self, label):
("label", None),
("label", "label"),
("n_basis_input", 1),
("num_output_features", 5),
("n_output_features", 5),
],
)
def test_attr_setter(self, attribute, value):
bas = self.cls(5)
with pytest.raises(AttributeError, match="can't set attribute"):
with pytest.raises(AttributeError, match=fr"can't set attribute|property '{attribute}' of"):
setattr(bas, attribute, value)

@pytest.mark.parametrize("n_input", [1, 2, 3])
def test_set_num_output_features(self, n_input):
bas = self.cls(5, mode="conv", window_size=10)
assert bas.num_output_features is None
assert bas.n_output_features is None
bas.compute_features(np.random.randn(20, n_input))
assert bas.num_output_features == n_input * bas.n_basis_funcs
assert bas.n_output_features == n_input * bas.n_basis_funcs

@pytest.mark.parametrize("n_input", [1, 2, 3])
def test_set_num_basis_input(self, n_input):
Expand Down Expand Up @@ -5568,11 +5568,11 @@ def test_set_num_output_features(self, n_basis_input1, n_basis_input2):
bas1 = basis.RaisedCosineBasisLinear(10, mode="conv", window_size=10)
bas2 = basis.BSplineBasis(11, mode="conv", window_size=10)
bas_add = bas1 + bas2
assert bas_add.num_output_features is None
assert bas_add.n_output_features is None
bas_add.compute_features(
np.ones((20, n_basis_input1)), np.ones((20, n_basis_input2))
)
assert bas_add.num_output_features == (
assert bas_add.n_output_features == (
n_basis_input1 * 10 + n_basis_input2 * 11
)

Expand Down Expand Up @@ -6137,11 +6137,11 @@ def test_set_num_output_features(self, n_basis_input1, n_basis_input2):
bas1 = basis.RaisedCosineBasisLinear(10, mode="conv", window_size=10)
bas2 = basis.BSplineBasis(11, mode="conv", window_size=10)
bas_add = bas1 * bas2
assert bas_add.num_output_features is None
assert bas_add.n_output_features is None
bas_add.compute_features(
np.ones((20, n_basis_input1)), np.ones((20, n_basis_input2))
)
assert bas_add.num_output_features == (
assert bas_add.n_output_features == (
n_basis_input1 * 10 * n_basis_input2 * 11
)

Expand Down

0 comments on commit 4282d6a

Please sign in to comment.