propagation_gan/libs/util.py

66 lines
1.5 KiB
Python

#!/usr/bin/python
import numpy as np
from libs.consts import NOISE_FLOOR
def touch(filepath):
try:
open(filepath, 'a').close()
return True
except BaseException:
pass
return False
def convert_mat_to_vector(
mat: np.ndarray,
block_size: float = 0.1
):
'''
'''
rx_locs = []
rx_rsses = []
width, length = mat.shape
for i in range(width):
for j in range(length):
if mat[i, j] <= NOISE_FLOOR + 5:
continue
rx_locs.append([(i + 0.5) * block_size, (j + 0.5) * block_size])
rx_rsses.append(mat[i, j])
return np.array(rx_locs), np.array(rx_rsses)
def convert_vector_to_mat(
rx_locs: np.ndarray,
rx_rsses: np.ndarray,
shape: tuple
):
'''
'''
result = np.ones(shape) * -85.0
for i in range(rx_locs.shape[0]):
# if offset is None:
# x, y = rx_locs[i, :] / block_size
# else:
# x, y = (rx_locs[i, :] + offset) / block_size
# x, y = int(round(x)), int(round(y))
result[i % shape[0], int(i / shape[0])] = rx_rsses[i]
return result
def stats(data):
'''
'''
data = data.reshape(-1,)
return {
"5perc": np.percentile(data, 5),
"25perc": np.percentile(data, 25),
"median": np.median(data),
"75perc": np.percentile(data, 75),
"95perc": np.percentile(data, 95),
"mean": np.mean(data),
"std": np.std(data),
}