|
| 1 | +import warnings |
| 2 | +from typing import Optional, Union |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import numpy.typing as npt |
| 6 | +import sklearn.utils.validation as sklearn_utils_validation |
| 7 | +import torch |
| 8 | + |
| 9 | +import cebra |
| 10 | +import cebra.integrations.sklearn.utils as sklearn_utils |
| 11 | +import cebra.models |
| 12 | +import cebra.solvers |
| 13 | + |
| 14 | + |
| 15 | +#NOTE: Deprecated: transform is now handled in the solver but the original |
| 16 | +# method is kept here for testing. |
| 17 | +def cebra_transform_deprecated(cebra_model, |
| 18 | + X: Union[npt.NDArray, torch.Tensor], |
| 19 | + session_id: Optional[int] = None) -> npt.NDArray: |
| 20 | + """Transform an input sequence and return the embedding. |
| 21 | +
|
| 22 | + Args: |
| 23 | + cebra_model: The CEBRA model to use for the transform. |
| 24 | + X: A numpy array or torch tensor of size ``time x dimension``. |
| 25 | + session_id: The session ID, an :py:class:`int` between 0 and :py:attr:`num_sessions` for |
| 26 | + multisession, set to ``None`` for single session. |
| 27 | +
|
| 28 | + Returns: |
| 29 | + A :py:func:`numpy.array` of size ``time x output_dimension``. |
| 30 | +
|
| 31 | + Example: |
| 32 | +
|
| 33 | + >>> import cebra |
| 34 | + >>> import numpy as np |
| 35 | + >>> dataset = np.random.uniform(0, 1, (1000, 30)) |
| 36 | + >>> cebra_model = cebra.CEBRA(max_iterations=10) |
| 37 | + >>> cebra_model.fit(dataset) |
| 38 | + CEBRA(max_iterations=10) |
| 39 | + >>> embedding = cebra_model.transform(dataset) |
| 40 | +
|
| 41 | + """ |
| 42 | + warnings.warn( |
| 43 | + "The method is deprecated " |
| 44 | + "but kept for testing puroposes." |
| 45 | + "We recommend using `transform` instead.", |
| 46 | + DeprecationWarning, |
| 47 | + stacklevel=2) |
| 48 | + |
| 49 | + sklearn_utils_validation.check_is_fitted(cebra_model, "n_features_") |
| 50 | + model, offset = cebra_model._select_model(X, session_id) |
| 51 | + |
| 52 | + # Input validation |
| 53 | + X = sklearn_utils.check_input_array(X, min_samples=len(cebra_model.offset_)) |
| 54 | + input_dtype = X.dtype |
| 55 | + |
| 56 | + with torch.no_grad(): |
| 57 | + model.eval() |
| 58 | + |
| 59 | + if cebra_model.pad_before_transform: |
| 60 | + X = np.pad(X, ((offset.left, offset.right - 1), (0, 0)), |
| 61 | + mode="edge") |
| 62 | + X = torch.from_numpy(X).float().to(cebra_model.device_) |
| 63 | + |
| 64 | + if isinstance(model, cebra.models.ConvolutionalModelMixin): |
| 65 | + # Fully convolutional evaluation, switch (T, C) -> (1, C, T) |
| 66 | + X = X.transpose(1, 0).unsqueeze(0) |
| 67 | + output = model(X).cpu().numpy().squeeze(0).transpose(1, 0) |
| 68 | + else: |
| 69 | + # Standard evaluation, (T, C, dt) |
| 70 | + output = model(X).cpu().numpy() |
| 71 | + |
| 72 | + if input_dtype == "float64": |
| 73 | + return output.astype(input_dtype) |
| 74 | + |
| 75 | + return output |
| 76 | + |
| 77 | + |
| 78 | +# NOTE: Deprecated: batched transform can now be performed (more memory efficient) |
| 79 | +# using the transform method of the model, and handling padding is implemented |
| 80 | +# directly in the base Solver. This method is kept for testing purposes. |
| 81 | +@torch.no_grad() |
| 82 | +def multiobjective_transform_deprecated(solver: cebra.solvers.Solver, |
| 83 | + inputs: torch.Tensor) -> torch.Tensor: |
| 84 | + """Transform the input data using the model. |
| 85 | +
|
| 86 | + Args: |
| 87 | + solver: The solver containing the model and device. |
| 88 | + inputs: The input data to transform. |
| 89 | +
|
| 90 | + Returns: |
| 91 | + The transformed data. |
| 92 | + """ |
| 93 | + |
| 94 | + warnings.warn( |
| 95 | + "The method is deprecated " |
| 96 | + "but kept for testing puroposes." |
| 97 | + "We recommend using `transform` instead.", |
| 98 | + DeprecationWarning, |
| 99 | + stacklevel=2) |
| 100 | + |
| 101 | + offset = solver.model.get_offset() |
| 102 | + solver.model.eval() |
| 103 | + X = inputs.cpu().numpy() |
| 104 | + X = np.pad(X, ((offset.left, offset.right - 1), (0, 0)), mode="edge") |
| 105 | + X = torch.from_numpy(X).float().to(solver.device) |
| 106 | + |
| 107 | + if isinstance(solver.model.module, cebra.models.ConvolutionalModelMixin): |
| 108 | + # Fully convolutional evaluation, switch (T, C) -> (1, C, T) |
| 109 | + X = X.transpose(1, 0).unsqueeze(0) |
| 110 | + outputs = solver.model(X) |
| 111 | + |
| 112 | + # switch back from (1, C, T) -> (T, C) |
| 113 | + if isinstance(outputs, torch.Tensor): |
| 114 | + assert outputs.dim() == 3 and outputs.shape[0] == 1 |
| 115 | + outputs = outputs.squeeze(0).transpose(1, 0) |
| 116 | + elif isinstance(outputs, tuple): |
| 117 | + assert all(tensor.dim() == 3 and tensor.shape[0] == 1 |
| 118 | + for tensor in outputs) |
| 119 | + outputs = (output.squeeze(0).transpose(1, 0) for output in outputs) |
| 120 | + outputs = tuple(outputs) |
| 121 | + else: |
| 122 | + raise ValueError("Invalid condition in solver.transform") |
| 123 | + else: |
| 124 | + # Standard evaluation, (T, C, dt) |
| 125 | + outputs = solver.model(X) |
| 126 | + |
| 127 | + return outputs |
0 commit comments