-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshuffle_noise.py
145 lines (127 loc) · 4.57 KB
/
shuffle_noise.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import numpy as np
import h5py
from create_noise import (
generate_checkerboard_pattern,
generate_multicolor_checkerboard_pattern,
)
import hdf5plugin
import blosc
def shuffle_pattern(pattern, checker_size):
"""Shuffle the pattern by a random number of pixels relative to checkerboard size in x and y directions.
Parameters
----------
pattern : numpy.ndarray
The pattern to shuffle.
checker_size : int
The size of the checkerboard squares in pixels.
Returns
-------
numpy.ndarray
The shuffled pattern.
"""
max_shift = int(
checker_size - checker_size / 4
) # Calculate maximum shift relative to checker size
shifts = np.arange(0, max_shift + 1, checker_size // 4) # Get the possible shifts
# Generate random shifts for x and y from the calculated shifts
x_shift = np.random.choice(shifts)
y_shift = np.random.choice(shifts)
# Use numpy's roll function to perform the shifts
shifted_pattern = np.roll(pattern, shift=x_shift, axis=1) # Shift in x
shifted_pattern = np.roll(shifted_pattern, shift=y_shift, axis=0) # Shift in y
return shifted_pattern
def generate_and_store_3d_array(
frames, checkerboard_size, width_in_pixels, height_in_pixels, fps, name="Noise.h5"
):
"""Generate a 3D array of checkerboard patterns and store it in an HDF5 file.
Parameters
----------
frames : int
The number of frames to generate.
checkerboard_size : int
The size of the checkerboard squares in pixels.
width_in_pixels : int
The width of the pattern in pixels.
height_in_pixels : int
The height of the pattern in pixels.
fps : int
The frame rate of the pattern in Hz.
name : str
The name of the HDF5 file to store the pattern in.
"""
patterns_list = []
# Generate the checkerboard patterns with random shuffling for each frame
for _ in range(frames):
pattern = generate_checkerboard_pattern(
checkerboard_size, width_in_pixels, height_in_pixels
)
shuffled_pattern = shuffle_pattern(pattern, checkerboard_size)
patterns_list.append(shuffled_pattern)
stacked_patterns = np.stack(patterns_list, axis=0) # This creates a 3D array
with h5py.File(name, "w") as f:
f.create_dataset(
"Noise",
data=stacked_patterns,
dtype="uint8",
compression=hdf5plugin.Blosc(
cname="blosclz", clevel=9, shuffle=hdf5plugin.Blosc.NOSHUFFLE
),
)
f.create_dataset(name="Frame_Rate", data=fps, dtype="uint8")
f.create_dataset(
name="Checkerboard_Size", data=checkerboard_size, dtype="uint64"
)
f.create_dataset(name="Shuffle", data=True, dtype="bool")
def generate_and_store_3d_array_colour(
frames,
checkerboard_size,
width_in_pixels,
height_in_pixels,
fps,
num_channels,
name="Noise.h5",
):
"""Generate a 3D array of checkerboard patterns and store it in an HDF5 file.
Parameters
----------
frames : int
The number of frames to generate.
checkerboard_size : int
The size of the checkerboard squares in pixels.
width_in_pixels : int
The width of the pattern in pixels.
height_in_pixels : int
The height of the pattern in pixels.
fps : int
The frame rate of the pattern in Hz.
num_channels : int
The number of channels in the pattern.
name : str
The name of the HDF5 file to store the pattern in.
"""
patterns_list = []
# Generate the checkerboard patterns with random shuffling for each frame
for _ in range(frames):
pattern = generate_multicolor_checkerboard_pattern(
checkerboard_size,
width_in_pixels,
height_in_pixels,
num_channels=num_channels,
)
shuffled_pattern = shuffle_pattern(pattern, checkerboard_size)
patterns_list.append(shuffled_pattern)
stacked_patterns = np.stack(patterns_list, axis=0) # This creates a 3D array
with h5py.File(name, "w") as f:
f.create_dataset(
"Noise",
data=stacked_patterns,
dtype="uint8",
compression=hdf5plugin.Blosc(
cname="blosclz", clevel=9, shuffle=hdf5plugin.Blosc.NOSHUFFLE
),
)
f.create_dataset(name="Frame_Rate", data=fps, dtype="uint8")
f.create_dataset(
name="Checkerboard_Size", data=checkerboard_size, dtype="uint64"
)
f.create_dataset(name="Shuffle", data=True, dtype="bool")