File size: 2,011 Bytes
78022ff
 
d1f3499
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78022ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
import os
import pickle
import numpy as np


def find_exact_match(matrix, query_vector, decimals=9):
    """
    Finds the index of the vector in 'matrix' that is the closest match to 'query_vector'
    when considering rounding to a specified number of decimal places.

    Parameters:
    - matrix: 2D numpy array where each row is a vector.
    - query_vector: 1D numpy array representing the vector to be matched.
    - decimals: Number of decimals to round to for the match.

    Returns:
    - Index of the exact match if found, or -1 if no match is found.
    """
    # Round the matrix and query vector to the specified number of decimals
    rounded_matrix = np.round(matrix, decimals=decimals)
    rounded_query = np.round(query_vector, decimals=decimals)

    # Find the index where all elements match
    matches = np.all(rounded_matrix == rounded_query, axis=1)

    # Return the index if a match is found, otherwise return -1
    if np.any(matches):
        return np.where(matches)[0][0]  # Return the first match
    else:
        return -1

def file_cache(file_path):
    def decorator(func):
        def wrapper(*args, **kwargs):
            # Ensure the directory exists
            dir_path = os.path.dirname(file_path)
            if not os.path.exists(dir_path):
                os.makedirs(dir_path, exist_ok=True)
                print(f"Created directory {dir_path}")

            # Check if the file already exists
            if os.path.exists(file_path):
                # Load from cache
                with open(file_path, "rb") as f:
                    print(f"Loading cached data from {file_path}")
                    return pickle.load(f)
            else:
                # Compute and save to cache
                result = func(*args, **kwargs)
                with open(file_path, "wb") as f:
                    pickle.dump(result, f)
                    print(f"Saving new cache to {file_path}")
                return result
        return wrapper
    return decorator