-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshuffle_noise.py
67 lines (54 loc) · 2.42 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
import numpy as np
import h5py
from create_noise import generate_checkerboard_pattern
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 = checker_size // 2 # 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")
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")