MCPcopy Create free account
hub / github.com/MinishLab/vicinity / normalize_or_copy

Function normalize_or_copy

vicinity/utils.py:45–55  ·  view source on GitHub ↗

Return the original vectors if they are already normalized. Otherwise, the vectors are normalized, and a new array is returned.

(vectors: npt.NDArray)

Source from the content-addressed store, hash-verified

43
44
45def normalize_or_copy(vectors: npt.NDArray) -> npt.NDArray:
46 """
47 Return the original vectors if they are already normalized.
48
49 Otherwise, the vectors are normalized, and a new array is returned.
50 """
51 norms = np.linalg.norm(vectors, axis=-1)
52 all_unit_length = np.allclose(norms[norms != 0], 1)
53 if all_unit_length:
54 return vectors
55 return normalize(vectors, norms)
56
57
58class Metric(Enum):

Callers 5

test_normalize_or_copyFunction · 0.90
queryMethod · 0.90
thresholdMethod · 0.90
__init__Method · 0.90
insertMethod · 0.90

Calls 1

normalizeFunction · 0.85

Tested by 1

test_normalize_or_copyFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…