6565 from array import array as _array
6666 from mmap import mmap as _mmap
6767
68-
69- _NUMPY_AVAILABLE = False
70- try :
7168 import numpy as np
7269 import numpy .typing as npt
7370
74- _NUMPY_AVAILABLE = True
75- except ImportError :
76- np = None # type: ignore
77-
7871
7972class UuidRepresentation :
8073 UNSPECIFIED = 0
@@ -492,9 +485,7 @@ def from_vector(
492485 )
493486 metadata = struct .pack ("<sB" , dtype .value , padding )
494487
495- if _NUMPY_AVAILABLE and isinstance (vector , np .ndarray ):
496- data = _numpy_vector_to_bytes (vector , dtype )
497- else :
488+ if isinstance (vector , list ):
498489 if dtype == BinaryVectorDtype .INT8 : # pack ints in [-128, 127] as signed int8
499490 format_str = "b"
500491 if padding :
@@ -511,7 +502,36 @@ def from_vector(
511502 raise ValueError (f"padding does not apply to { dtype = } " )
512503 else :
513504 raise NotImplementedError ("%s not yet supported" % dtype )
514- data = struct .pack (f"<{ len (vector )} { format_str } " , * vector ) # type: ignore
505+ data = struct .pack (f"<{ len (vector )} { format_str } " , * vector )
506+ else : # vector is numpy array or incorrect type.
507+ try :
508+ import numpy as np
509+ except ImportError as exc :
510+ raise ImportError (
511+ "Failed to create binary from vector. Check type. If numpy array, numpy must be installed."
512+ ) from exc
513+ if not isinstance (vector , np .ndarray ):
514+ raise TypeError ("Vector must be a numpy array." )
515+ if vector .ndim != 1 :
516+ raise ValueError (
517+ "from_numpy_vector only supports 1D arrays as it creates a single vector."
518+ )
519+
520+ if dtype == BinaryVectorDtype .FLOAT32 :
521+ vector = vector .astype (np .dtype ("float32" ), copy = False )
522+ elif dtype == BinaryVectorDtype .INT8 :
523+ if vector .min () >= - 128 and vector .max () <= 127 :
524+ vector = vector .astype (np .dtype ("int8" ), copy = False )
525+ else :
526+ raise ValueError ("Values found outside INT8 range." )
527+ elif dtype == BinaryVectorDtype .PACKED_BIT :
528+ if vector .min () >= 0 and vector .max () <= 127 :
529+ vector = vector .astype (np .dtype ("uint8" ), copy = False )
530+ else :
531+ raise ValueError ("Values found outside UINT8 range." )
532+ else :
533+ raise NotImplementedError ("%s not yet supported" % dtype )
534+ data = vector .tobytes ()
515535
516536 if padding and len (vector ) and not (data [- 1 ] & ((1 << padding ) - 1 )) == 0 :
517537 raise ValueError (
@@ -596,8 +616,13 @@ def as_numpy_vector(self) -> BinaryVector:
596616 """
597617 if self .subtype != VECTOR_SUBTYPE :
598618 raise ValueError (f"Cannot decode subtype { self .subtype } as a vector" )
599- if not _NUMPY_AVAILABLE :
600- raise ImportError ("Converting binary to numpy.ndarray requires numpy to be installed." )
619+ try :
620+ import numpy as np
621+ except ImportError as exc :
622+ raise ImportError (
623+ "Converting binary to numpy.ndarray requires numpy to be installed."
624+ ) from exc
625+
601626 dtype , padding = struct .unpack_from ("<sB" , self , 0 )
602627 dtype = BinaryVectorDtype (dtype )
603628
@@ -637,32 +662,3 @@ def __repr__(self) -> str:
637662 return f"<Binary(REDACTED, { self .__subtype } )>"
638663 else :
639664 return f"Binary({ bytes .__repr__ (self )} , { self .__subtype } )"
640-
641-
642- def _numpy_vector_to_bytes (
643- vector : npt .NDArray [np .number ],
644- dtype : BinaryVectorDtype ,
645- ) -> bytes :
646- if not _NUMPY_AVAILABLE :
647- raise ImportError ("Converting numpy.ndarray to binary requires numpy to be installed." )
648-
649- if not isinstance (vector , np .ndarray ):
650- raise TypeError ("Vector must be a numpy array." )
651- if vector .ndim != 1 :
652- raise ValueError ("from_numpy_vector only supports 1D arrays as it creates a single vector." )
653-
654- if dtype == BinaryVectorDtype .FLOAT32 :
655- vector = vector .astype (np .dtype ("float32" ), copy = False )
656- elif dtype == BinaryVectorDtype .INT8 :
657- if vector .min () >= - 128 and vector .max () <= 127 :
658- vector = vector .astype (np .dtype ("int8" ), copy = False )
659- else :
660- raise ValueError ("Values found outside INT8 range." )
661- elif dtype == BinaryVectorDtype .PACKED_BIT :
662- if vector .min () >= 0 and vector .max () <= 127 :
663- vector = vector .astype (np .dtype ("uint8" ), copy = False )
664- else :
665- raise ValueError ("Values found outside UINT8 range." )
666- else :
667- raise NotImplementedError ("%s not yet supported" % dtype )
668- return vector .tobytes ()
0 commit comments