(vec: Union[list, np.ndarray], dtype: np.dtype)
| 41 | |
| 42 | |
| 43 | def convert_to_numpy(vec: Union[list, np.ndarray], dtype: np.dtype) -> np.ndarray: |
| 44 | if isinstance(vec, np.ndarray): |
| 45 | if vec.dtype == dtype and vec.ndim == 1: |
| 46 | return vec |
| 47 | return np.asarray(vec, dtype=dtype).flatten() |
| 48 | |
| 49 | try: |
| 50 | arr = np.asarray(vec, dtype=dtype) |
| 51 | if arr.ndim != 1: |
| 52 | arr = arr.flatten() |
| 53 | return arr |
| 54 | except (ValueError, TypeError) as e: |
| 55 | raise TypeError( |
| 56 | f"Cannot convert input to 1D numpy array with dtype={dtype}: {type(vec)}" |
| 57 | ) from e |
| 58 | |
| 59 | |
| 60 | class QueryContext: |
no test coverage detected