-
Notifications
You must be signed in to change notification settings - Fork 1
/
shift_bitmaps.py
executable file
·77 lines (53 loc) · 2.68 KB
/
shift_bitmaps.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
import pathlib
import glob
import numpy as np
print("Loading input data...")
data_dir = pathlib.Path('input_data')
imputed_anom_3D = np.load(data_dir / "imputed_anom_3D-v2.npy")
master_mask_2D = np.load(data_dir / "master_mask_2D-v2.npy")
nan_mask_3D = np.load(data_dir / "nan_mask_3D-v2.npy")
evaluation_mask_3D = np.load(data_dir / "evaluation_mask_3D-v2.npy")
duration, height, width = imputed_anom_3D.shape
row_idxs = np.load(data_dir / "row_idxs-v2.npy")
col_idxs = np.load(data_dir / "col_idxs-v2.npy")
def col_idx_shift(row, col):
new_col = col-(row - 12)//2+3
return row, new_col
shifted_width = 96
print("Shifting colon indexes...")
shifted_col_idxs = [col_idx_shift(row, col)[1] for row, col in zip(row_idxs, col_idxs)]
np.save(data_dir / "shifted_col_idxs-v2", shifted_col_idxs)
def shift_2D_bitmap(bitmap):
assert bitmap.shape == (height, width)
shifted_bitmap = np.zeros((height, shifted_width), dtype=bitmap.dtype)
shifted_bitmap[row_idxs, shifted_col_idxs] = bitmap[row_idxs, col_idxs]
return shifted_bitmap
def shift_3D_bitmap(bitmap):
assert bitmap.shape[1:] == (height, width)
shifted_bitmap = np.zeros((bitmap.shape[0], height, shifted_width), dtype=bitmap.dtype)
shifted_bitmap[:, row_idxs, shifted_col_idxs] = bitmap[:, row_idxs, col_idxs]
return shifted_bitmap
print("Shifting master mask...")
master_mask_2D_shifted = shift_2D_bitmap(master_mask_2D)
np.save(data_dir / "master_mask_2D_shifted-v2", master_mask_2D_shifted)
print("Shifting training masks...")
base_input_file_name = "training_mask_3D-v2"
base_output_file_name = "training_mask_3D_shifted-v2"
for input_file_name in glob.glob(str(data_dir / (base_input_file_name + "*"))):
print("..." + input_file_name)
input_file_name_suffix_position = input_file_name.find("training_mask_3D-v2") + len(base_input_file_name)
input_file_name_suffix = str(pathlib.Path(input_file_name[input_file_name_suffix_position:]).stem)
output_file_name = data_dir / (base_output_file_name + input_file_name_suffix)
training_mask_3D = np.load(input_file_name)
training_mask_3D_shifted = shift_3D_bitmap(training_mask_3D)
np.save(output_file_name, training_mask_3D_shifted)
print("Shifting NaN mask...")
nan_mask_3D_shifted = shift_3D_bitmap(nan_mask_3D)
np.save(data_dir / "nan_mask_3D_shifted-v2", nan_mask_3D_shifted)
print("Shifting imputed anomaly...")
imputed_anom_3D_shifted = shift_3D_bitmap(imputed_anom_3D)
np.save(data_dir / "imputed_anom_3D_shifted-v2", imputed_anom_3D_shifted)
print("Shifting evaluation mask...")
evaluation_mask_3D_shifted = shift_3D_bitmap(evaluation_mask_3D)
np.save(data_dir / "evaluation_mask_3D_shifted-v2", evaluation_mask_3D_shifted)