You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A public function that performs some sanity checks on a user object to check if it is a valid basis. In practice, passing this checks with:
check_io=True should guarantee that the basis can be composed.
require_sklearn=True should check the availability of the estimator machinery get_params and set_params.
Below a draft of the function, in this PR we should make sure that if the checks are true, then the basis can be used with sklearn.
defis_valid_basis(object: Any, check_io: bool|NDArray|Sequence[NDArray]=True, require_sklearn: bool=False):
""" Check if the object is valid basis. This method checks if an object is a valid basis. - If `require_sklearn` and `check_io` are False the methods checks for the availability of the `compute_features` method. - If `check_io` is set to True, it will check that it can call `compute_features` on a linspace between 0 and 1 as a minimal example and checks for the output structure. - If `check_io` is one or more numpy array, it calls `compute features` on them and check the output structure. - If `require_sklearn`, it checks that the `get_params` and `set_params` methods exist. """ifrequire_sklearn:
sklearn_interface=all(hasattr(object, val) forvalin ("get_params", "set_params")):
ifnotsklearn_interface:
print("Not compatible with scikit-learn.")
returnFalsetry:
new=copy.deepcopy(object)
new.set_params(**get_params())
exceptExceptionase:
print(f"``get_params`` and ``set_param`` did not work as expected. Error was {e}")
returnFalsetry:
TransformerBasis(object)
exceptExceptionase:
print(f"Cannot convert to a transformer with error:\n{e}")
returnFalseifcheck_ioisFalse:
ifhasattr(object, "compute_features"):
returnTrueelse:
print(f"Does not implement ``compute_features``.")
returnFalseifcheck_ioisTrue:
inp= (np.linspace(0, 1, 5), )
elifhasattr(check_io, "shape"):
inp= (check_io,)
else:
inp=check_ioforxininp:
try:
out=object.compute_features(x)
exceptExceptionase:
print(f"Cannot call `compute_features` over {x} with error:\n{e}"f"If your basis require a specific input structure (N-dimensional or more than one input, "f"please pass the input directly, i.e. call ``is_valid_basis(my_basis, check_io=my_input)``.")
returnFalseifnotall(hasattr(out, val) forvalin ("shape", "ndim")):
print("The output of compute features is not an array.")
returnFalseelifout.shape[0] !=x.shape[0]:
print(f"The output of ``compute_features`` do not preserve the first axis. The shape""of the input first array was {x.shape[0}, that of the output was {out.shape[0]} instead!")
returnFalseelifout.ndim!=2:
print(f"The output of ``compute_features`` is not 2-dimensional. The output dimensionality is {out.ndim}")
returnFalse
The text was updated successfully, but these errors were encountered:
A public function that performs some sanity checks on a user object to check if it is a valid basis. In practice, passing this checks with:
check_io=True
should guarantee that the basis can be composed.require_sklearn=True
should check the availability of the estimator machineryget_params
andset_params
.Below a draft of the function, in this PR we should make sure that if the checks are true, then the basis can be used with sklearn.
The text was updated successfully, but these errors were encountered: