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