-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
59 lines (50 loc) · 1.83 KB
/
utils.py
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
56
57
58
59
import numpy
import numpy.typing
from typing import Optional
PIXELS_IN_BIG_FILE = 50 * 1000 * 1000 # 50 megapixels
def to_hsv_matrix(
matrix: numpy.typing.NDArray[numpy.uint8],
hues: Optional[numpy.typing.NDArray[numpy.uint8]],
) -> numpy.typing.NDArray[numpy.uint8]:
"""
The matrix is a 2D array of uint8's. The hues are either None or another 2D
array of the same shape.
We return a 3D array representing an HSV image of the matrix, optionally
colored by the hues.
"""
result = numpy.zeros([*matrix.shape, 3], numpy.uint8)
result[:, :, 2] = matrix * 255
if hues is not None:
result[:, :, 0] = hues
result[:, :, 1] = 255 # Saturation
return result
def make_matrix(
tokens_a: numpy.typing.NDArray[numpy.str_],
tokens_b: numpy.typing.NDArray[numpy.str_]
) -> numpy.typing.NDArray[numpy.uint8]:
matrix = numpy.zeros([len(tokens_a), len(tokens_b)], dtype=numpy.uint8)
for i, value in enumerate(tokens_a):
matrix[i, :] = (tokens_b == value)
return matrix
def guess_language(filename: str) -> str:
file_type = filename.split(".")[-1]
known_types = { # Sorted by language (sorted by value, not key!)
"c": "c",
"h": "cpp", # Might be C or C++, err on the side of caution
"cc": "cpp",
"hh": "cpp",
"cpp": "cpp",
"hpp": "cpp",
"go": "go",
"hs": "haskell",
"js": "javascript",
"py": "python",
"rs": "rust",
"svelte": "svelte",
"ts": "typescript",
}
expected_language = known_types.get(file_type)
if expected_language is not None:
return expected_language
raise ValueError(f"Cannot infer language for unknown file extension "
f"'.{file_type}'. Set language explicitly")