File size: 1,373 Bytes
549c270
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# src/utils/index_utils.py

import faiss
import json
import numpy as np
from typing import Union
from pathlib import Path

def normalize(embeddings):
    """Normalize the embeddings to unit vectors."""
    norms = np.linalg.norm(embeddings, axis=1, keepdims=True)
    return embeddings / np.clip(norms, a_min=1e-10, a_max=None)

def save_faiss_index(features: np.ndarray, file_path):
    """
    Builds a FAISS index from the given feature matrix and saves it to disk.
    """
    # Ensure features is float32
    features = np.asarray(features).astype(np.float32)
    
    # Create the index
    dim = features.shape[1]
    index = faiss.IndexFlatL2(dim)
    index.add(features)

    # Save the index
    faiss.write_index(index, str(file_path))
    print(f"[βœ“] FAISS index saved to {file_path}")
    
def load_features(path: Path) -> np.ndarray:
    assert path.exists(), f"Feature file not found at: {path}"
    return np.load(path, allow_pickle=True)

def save_default_config(dataset: str, weights: dict, filename: str = "default_weights.json") -> None:
    """
    Saves the default fusion weights to a JSON file.
    """
    path = Path(f"defaults/{dataset}")
    path.mkdir(parents=True, exist_ok=True)
    filepath = path / filename
    with open(filepath, "w") as f:
        json.dump(weights, f, indent=2)
    print(f"[βœ“] Saved default weights to {filepath}")