1111import ptwt
1212
1313
14- def get_freq_order (level : int ):
15- """Get the frequency order for a given packet decomposition level.
16- Adapted from:
17- https://github.com/PyWavelets/pywt/blob/master/pywt/_wavelet_packets.py
18- The code elements denote the filter application order. The filters
19- are named following the pywt convention as:
20- a - LL, low-low coefficients
21- h - LH, low-high coefficients
22- v - HL, high-low coefficients
23- d - HH, high-high coefficients
24- """
25- wp_natural_path = list (product (["a" , "h" , "v" , "d" ], repeat = level ))
26-
27- def _get_graycode_order (level , x = "a" , y = "d" ):
28- graycode_order = [x , y ]
29- for _ in range (level - 1 ):
30- graycode_order = [x + path for path in graycode_order ] + [
31- y + path for path in graycode_order [::- 1 ]
32- ]
33- return graycode_order
34-
35- def _expand_2d_path (path ):
36- expanded_paths = {"d" : "hh" , "h" : "hl" , "v" : "lh" , "a" : "ll" }
37- return (
38- "" .join ([expanded_paths [p ][0 ] for p in path ]),
39- "" .join ([expanded_paths [p ][1 ] for p in path ]),
40- )
41-
42- nodes : dict = {}
43- for (row_path , col_path ), node in [
44- (_expand_2d_path (node ), node ) for node in wp_natural_path
45- ]:
46- nodes .setdefault (row_path , {})[col_path ] = node
47- graycode_order = _get_graycode_order (level , x = "l" , y = "h" )
48- nodes_list : list = [nodes [path ] for path in graycode_order if path in nodes ]
49- wp_frequency_path = []
50- for row in nodes_list :
51- wp_frequency_path .append ([row [path ] for path in graycode_order if path in row ])
52- return wp_frequency_path , wp_natural_path
53-
54-
5514def generate_frequency_packet_image (packet_array : np .ndarray , degree : int ):
5615 """Create a ready-to-polt image with frequency-order packages.
5716 Given a packet array in natural order, creat an image which is
@@ -63,7 +22,8 @@ def generate_frequency_packet_image(packet_array: np.ndarray, degree: int):
6322 Returns:
6423 [np.ndarray]: The image of shape [original_height, original_width]
6524 """
66- wp_freq_path , wp_natural_path = get_freq_order (degree )
25+ wp_freq_path = ptwt .WaveletPacket2D .get_freq_order (degree )
26+ wp_natural_path = ptwt .WaveletPacket2D .get_natural_order (degree )
6727
6828 image = []
6929 # go through the rows.
@@ -107,7 +67,8 @@ def load_images(path: str) -> list:
10767
10868
10969if __name__ == "__main__" :
110- frequency_path , natural_path = get_freq_order (level = 3 )
70+ freq_path = ptwt .WaveletPacket2D .get_freq_order (level = 3 )
71+ frequency_path = ptwt .WaveletPacket2D .get_natural_order (level = 3 )
11172 print ("Loading ffhq images:" )
11273 ffhq_images = load_images ("./ffhq_style_gan/source_data/A_ffhq" )
11374 print ("processing ffhq" )
0 commit comments