-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
93 lines (69 loc) · 2.96 KB
/
Copy pathutils.py
File metadata and controls
93 lines (69 loc) · 2.96 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import math, random, string, yaml, numpy as np
from datetime import datetime
from graph.shape import EmptyShape
###
# !!! PATH TO CONFIGURATION !!!
with open('../config/generation/custom.yaml', 'r') as file:
config = yaml.safe_load(file)
SUBTGRAPH_PATH = config["repository_path"] # Repository Path
SAVE_MESH = config["generation_save_mesh"] # Allow saving of the mesh
LOAD_MATRIX = config["generation_load_matrix"] # Allow loading of the topological matrix
SAVE_MATRIX = config["generation_save_matrix"] # Allow saving of the generation matrix
# Obtain limits of grid as square root of the world length
grid_rows = grid_columns = int(math.sqrt(np.random.randint(low=config["world_min_length"][0], high=config["world_min_length"][1]+1, size=1)[0]))
# Check empty shape
isEmptyShape = lambda obj: isinstance(obj, EmptyShape)
# Check openings of the asset based on direction array
isOpenWest = lambda openings: "w" in openings
isOpenNorth = lambda openings: "n" in openings
isOpenSouth = lambda openings: "s" in openings
isOpenEast = lambda openings: "e" in openings
# Check the limits of the grid based on coordinates
isLimitNorth = lambda idx: idx == 0
isLimitWest = lambda jdx: jdx == 0
isLimitSouth = lambda idx: idx == grid_rows - 1
isLimitEast = lambda jdx: jdx == grid_columns - 1
isLimitNorthWest = lambda idx, jdx: isLimitNorth(idx) and isLimitWest(jdx)
isLimitNorthEast = lambda idx, jdx: isLimitNorth(idx) and isLimitEast(jdx)
isLimitSouthWest = lambda idx, jdx: isLimitSouth(idx) and isLimitWest(jdx)
isLimitSouthEast = lambda idx, jdx: isLimitSouth(idx) and isLimitEast(jdx)
# Get values at specified directions
getValueNorth = lambda matrix, idx, jdx: matrix[idx-1][jdx]
getValueSouth = lambda matrix, idx, jdx: matrix[idx+1][jdx]
getValueWest = lambda matrix, idx, jdx: matrix[idx][jdx-1]
getValueEast = lambda matrix, idx, jdx: matrix[idx][jdx+1]
###
def get_random_id() -> str:
"""
Generate random 32 length string.
Returns
-------
out : str
Random 32 length string
"""
return ''.join(random.choice(string.ascii_letters) for i in range(32))
def get_datetime_id(prefix="subtgraph") -> str:
"""
Generate date and time string.
Parameters
-------
prefix : str, optional
Prefix of the string
Returns
-------
out : str
Date and Time string
"""
# Get current date and time
current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
# Combine prefix with the formatted date and time
return f"{prefix}_{current_time}"
def intersection_over_union(mat1, mat2):
"""Compute IoU for binary occupancy matrices (1 = occupied, 0 = free)."""
mat1 = (mat1 > 0).float()
mat2 = (mat2 > 0).float()
intersection = torch.sum((mat1 * mat2))
union = torch.sum((mat1 + mat2) > 0)
if union == 0:
return torch.tensor(1.0) # both empty, treat as identical
return intersection / union