66from .indexer import ArrowIndexV2
77from .bucket import (
88 ResolutionGroup ,
9- MultiIndexV2 , MultiResolutionBucketIndexV2 , MultiMultiResolutionBucketIndexV2 , IndexV2Builder
9+ MultiIndexV2 ,
10+ MultiResolutionBucketIndexV2 ,
11+ MultiMultiResolutionBucketIndexV2 ,
12+ IndexV2Builder ,
1013)
1114
1215
13- def load_index (src ,
14- multireso = False ,
15- batch_size = 1 ,
16- world_size = 1 ,
17- sample_strategy = 'uniform' ,
18- probability = None ,
19- shadow_file_fn = None ,
20- seed = None ,
21- ):
16+ def load_index (
17+ src ,
18+ multireso = False ,
19+ batch_size = 1 ,
20+ world_size = 1 ,
21+ sample_strategy = "uniform" ,
22+ probability = None ,
23+ shadow_file_fn = None ,
24+ seed = None ,
25+ ):
2226 if isinstance (src , str ):
2327 src = [src ]
24- if src [0 ].endswith (' .arrow' ):
28+ if src [0 ].endswith (" .arrow" ):
2529 if multireso :
26- raise ValueError ('Arrow file does not support multiresolution. Please make base index V2 first and then'
27- 'build multiresolution index.' )
30+ raise ValueError (
31+ "Arrow file does not support multiresolution. Please make base index V2 first and then"
32+ "build multiresolution index."
33+ )
2834 idx = IndexV2Builder (src ).to_index_v2 ()
29- elif src [0 ].endswith (' .json' ):
35+ elif src [0 ].endswith (" .json" ):
3036 if multireso :
3137 if len (src ) == 1 :
32- idx = MultiResolutionBucketIndexV2 (src [0 ], batch_size = batch_size ,
33- world_size = world_size ,
34- shadow_file_fn = shadow_file_fn ,
35- )
38+ idx = MultiResolutionBucketIndexV2 (
39+ src [0 ],
40+ batch_size = batch_size ,
41+ world_size = world_size ,
42+ shadow_file_fn = shadow_file_fn ,
43+ )
3644 else :
37- idx = MultiMultiResolutionBucketIndexV2 (src , batch_size = batch_size ,
38- world_size = world_size ,
39- sample_strategy = sample_strategy , probability = probability ,
40- shadow_file_fn = shadow_file_fn , seed = seed ,
41- )
45+ idx = MultiMultiResolutionBucketIndexV2 (
46+ src ,
47+ batch_size = batch_size ,
48+ world_size = world_size ,
49+ sample_strategy = sample_strategy ,
50+ probability = probability ,
51+ shadow_file_fn = shadow_file_fn ,
52+ seed = seed ,
53+ )
4254 else :
4355 if len (src ) == 1 :
44- idx = ArrowIndexV2 (src [0 ],
45- shadow_file_fn = shadow_file_fn ,
46- )
56+ idx = ArrowIndexV2 (
57+ src [0 ],
58+ shadow_file_fn = shadow_file_fn ,
59+ )
4760 else :
48- idx = MultiIndexV2 (src ,
49- sample_strategy = sample_strategy , probability = probability ,
50- shadow_file_fn = shadow_file_fn , seed = seed ,
51- )
61+ idx = MultiIndexV2 (
62+ src ,
63+ sample_strategy = sample_strategy ,
64+ probability = probability ,
65+ shadow_file_fn = shadow_file_fn ,
66+ seed = seed ,
67+ )
5268 else :
53- raise ValueError (f' Unknown file type: { src [0 ]} ' )
69+ raise ValueError (f" Unknown file type: { src [0 ]} " )
5470 return idx
5571
5672
@@ -59,7 +75,7 @@ def get_attribute(data, attr_list):
5975 for attr in attr_list :
6076 ret_data [attr ] = data .get (attr , None )
6177 if ret_data [attr ] is None :
62- raise ValueError (f' Missing { attr } in { data } ' )
78+ raise ValueError (f" Missing { attr } in { data } " )
6379 return ret_data
6480
6581
@@ -71,27 +87,38 @@ def get_optional_attribute(data, attr_list):
7187
7288
7389def detect_index_type (data ):
74- if isinstance (data [' group_length' ], dict ):
75- return ' multireso'
90+ if isinstance (data [" group_length" ], dict ):
91+ return " multireso"
7692 else :
77- return 'base'
93+ return "base"
94+
7895
7996def show_index_info (src , only_arrow_files = False , depth = 1 ):
8097 """
8198 Show base/multireso index information.
8299 """
83100 if not Path (src ).exists ():
84- raise ValueError (f' { src } does not exist.' )
101+ raise ValueError (f" { src } does not exist." )
85102 print (f"Loading index file { src } ..." )
86- with open (src , 'r' ) as f :
103+ with open (src , "r" ) as f :
87104 src_data = json .load (f )
88105 print (f"Loaded." )
89- data = get_attribute (src_data , ['data_type' , 'indices_file' , 'arrow_files' , 'cum_length' ,
90- 'group_length' , 'indices' , 'example_indices' ])
91- opt_data = get_optional_attribute (src_data , ['config_file' ])
106+ data = get_attribute (
107+ src_data ,
108+ [
109+ "data_type" ,
110+ "indices_file" ,
111+ "arrow_files" ,
112+ "cum_length" ,
113+ "group_length" ,
114+ "indices" ,
115+ "example_indices" ,
116+ ],
117+ )
118+ opt_data = get_optional_attribute (src_data , ["config_file" ])
92119
93120 # Format arrow_files examples
94- arrow_files = data [' arrow_files' ]
121+ arrow_files = data [" arrow_files" ]
95122 if only_arrow_files :
96123 existed = set ()
97124 arrow_files_output_list = []
@@ -105,79 +132,96 @@ def show_index_info(src, only_arrow_files=False, depth=1):
105132 if depth >= len (parts ):
106133 continue
107134 else :
108- arrow_file_part = '/' .join (parts [:- depth ])
135+ arrow_file_part = "/" .join (parts [:- depth ])
109136 if arrow_file_part not in existed :
110137 arrow_files_output_list .append (arrow_file_part )
111138 existed .add (arrow_file_part )
112139 else :
113- raise ValueError (f'Depth { depth } has exceeded the limit of arrow file { arrow_file } .' )
114- arrow_files_repr = '\n ' .join (arrow_files_output_list )
140+ raise ValueError (
141+ f"Depth { depth } has exceeded the limit of arrow file { arrow_file } ."
142+ )
143+ arrow_files_repr = "\n " .join (arrow_files_output_list )
115144 print (arrow_files_repr )
116145 return None
117146
118- return_space = ' \n ' + ' ' * 21
147+ return_space = " \n " + " " * 21
119148
120149 if len (arrow_files ) <= 4 :
121150 arrow_files_repr = return_space .join ([arrow_file for arrow_file in arrow_files ])
122151 else :
123- arrow_files_repr = return_space .join ([_ for _ in arrow_files [:2 ]] + ['...' ]
124- + [_ for _ in arrow_files [- 2 :]])
152+ arrow_files_repr = return_space .join (
153+ [_ for _ in arrow_files [:2 ]] + ["..." ] + [_ for _ in arrow_files [- 2 :]]
154+ )
125155 arrow_files_length = len (arrow_files )
126156
127157 # Format data_type
128- data_type = data [' data_type' ]
158+ data_type = data [" data_type" ]
129159 if isinstance (data_type , str ):
130160 data_type = [data_type ]
131161 data_type_common = []
132162 src_files = []
133163 found_src_files = False
134164 for data_type_item in data_type :
135- if not found_src_files and data_type_item .strip () != ' src_files=' :
165+ if not found_src_files and data_type_item .strip () != " src_files=" :
136166 data_type_common .append (data_type_item .strip ())
137167 continue
138168 found_src_files = True
139- if data_type_item .endswith (' .json' ):
169+ if data_type_item .endswith (" .json" ):
140170 src_files .append (data_type_item .strip ())
141171 else :
142172 data_type_common .append (data_type_item .strip ())
143173 data_type_part2_with_ids = []
144174 max_id_len = len (str (len (src_files )))
145175 for sid , data_type_item in enumerate (src_files , start = 1 ):
146- data_type_part2_with_ids .append (f'{ str (sid ).rjust (max_id_len )} . { data_type_item } ' )
176+ data_type_part2_with_ids .append (
177+ f"{ str (sid ).rjust (max_id_len )} . { data_type_item } "
178+ )
147179 data_type = data_type_common + data_type_part2_with_ids
148180 data_repr = return_space .join (data_type )
149181
150182 # Format cum_length examples
151- cum_length = data [' cum_length' ]
183+ cum_length = data [" cum_length" ]
152184 if len (cum_length ) <= 8 :
153- cum_length_repr = ', ' .join ([str (i ) for i in cum_length ])
185+ cum_length_repr = ", " .join ([str (i ) for i in cum_length ])
154186 else :
155- cum_length_repr = ', ' .join ([str (i ) for i in cum_length [:4 ]] + ['...' ] + [str (i ) for i in cum_length [- 4 :]])
187+ cum_length_repr = ", " .join (
188+ [str (i ) for i in cum_length [:4 ]]
189+ + ["..." ]
190+ + [str (i ) for i in cum_length [- 4 :]]
191+ )
156192 cum_length_length = len (cum_length )
157193
158- if detect_index_type (data ) == ' base' :
194+ if detect_index_type (data ) == " base" :
159195 # Format group_length examples
160- group_length = data [' group_length' ]
196+ group_length = data [" group_length" ]
161197 if len (group_length ) <= 8 :
162- group_length_repr = ', ' .join ([str (i ) for i in group_length ])
198+ group_length_repr = ", " .join ([str (i ) for i in group_length ])
163199 else :
164- group_length_repr = ', ' .join ([str (i ) for i in group_length [:4 ]] + ['...' ] + [str (i ) for i in group_length [- 4 :]])
200+ group_length_repr = ", " .join (
201+ [str (i ) for i in group_length [:4 ]]
202+ + ["..." ]
203+ + [str (i ) for i in group_length [- 4 :]]
204+ )
165205 group_length_length = len (group_length )
166206
167207 # Format indices examples
168- indices = data [' indices' ]
169- if len (indices ) == 0 and data [' indices_file' ] != '' :
170- indices_file = Path (src ).parent / data [' indices_file' ]
208+ indices = data [" indices" ]
209+ if len (indices ) == 0 and data [" indices_file" ] != "" :
210+ indices_file = Path (src ).parent / data [" indices_file" ]
171211 if Path (indices_file ).exists ():
172212 print (f"Loading indices from { indices_file } ..." )
173- indices = np .load (indices_file )['x' ]
213+ indices = np .load (indices_file )["x" ]
174214 print (f"Loaded." )
175215 else :
176- raise ValueError (f'This Index file contains an extra file { indices_file } which is missed.' )
216+ raise ValueError (
217+ f"This Index file contains an extra file { indices_file } which is missed."
218+ )
177219 if len (indices ) <= 8 :
178- indices_repr = ', ' .join ([str (i ) for i in indices ])
220+ indices_repr = ", " .join ([str (i ) for i in indices ])
179221 else :
180- indices_repr = ', ' .join ([str (i ) for i in indices [:4 ]] + ['...' ] + [str (i ) for i in indices [- 4 :]])
222+ indices_repr = ", " .join (
223+ [str (i ) for i in indices [:4 ]] + ["..." ] + [str (i ) for i in indices [- 4 :]]
224+ )
181225
182226 # Calculate indices total length
183227 indices_length = len (indices )
@@ -188,7 +232,7 @@ def show_index_info(src, only_arrow_files=False, depth=1):
188232 \033 [4mdata_type:\033 [0m { data_repr } """
189233
190234 # Process optional data
191- if opt_data [' config_file' ] is not None :
235+ if opt_data [" config_file" ] is not None :
192236 print_str += f"""
193237 \033 [4mconfig_file:\033 [0m { opt_data ['config_file' ]} """
194238
@@ -205,18 +249,20 @@ def show_index_info(src, only_arrow_files=False, depth=1):
205249 Examples: { indices_repr } """
206250
207251 else :
208- group_length = data [' group_length' ]
252+ group_length = data [" group_length" ]
209253
210- indices_file = Path (src ).parent / data [' indices_file' ]
211- assert Path (indices_file ).exists (), f' indices_file { indices_file } not found'
254+ indices_file = Path (src ).parent / data [" indices_file" ]
255+ assert Path (indices_file ).exists (), f" indices_file { indices_file } not found"
212256 print (f"Loading indices from { indices_file } ..." )
213257 indices_data = np .load (indices_file )
214258 print (f"Loaded." )
215259 indices_length = sum ([len (indices ) for key , indices in indices_data .items ()])
216260 keys = [k for k in group_length .keys () if len (indices_data [k ]) > 0 ]
217261
218262 resolutions = ResolutionGroup .from_list_of_hxw (keys )
219- resolutions .attr = [f'{ len (indices ):>,d} ' for k , indices in indices_data .items ()]
263+ resolutions .attr = [
264+ f"{ len (indices ):>,d} " for k , indices in indices_data .items ()
265+ ]
220266 resolutions .prefix_space = 25
221267
222268 print_str = f"""File: { Path (src ).absolute ()}
@@ -225,7 +271,7 @@ def show_index_info(src, only_arrow_files=False, depth=1):
225271 \033 [4mdata_type:\033 [0m { data_repr } """
226272
227273 # Process optional data
228- if opt_data [' config_file' ] is not None :
274+ if opt_data [" config_file" ] is not None :
229275 print_str += f"""
230276 \033 [4mconfig_file:\033 [0m { opt_data ['config_file' ]} """
231277
@@ -236,15 +282,19 @@ def show_index_info(src, only_arrow_files=False, depth=1):
236282 if src_file .exists ():
237283 with src_file .open () as f :
238284 base_data = json .load (f )
239- if ' config_file' in base_data :
240- config_files .append (base_data [' config_file' ])
285+ if " config_file" in base_data :
286+ config_files .append (base_data [" config_file" ])
241287 else :
242- config_files .append (' Unknown' )
288+ config_files .append (" Unknown" )
243289 else :
244- config_files .append (' Missing the src file' )
290+ config_files .append (" Missing the src file" )
245291 if config_files :
246- config_file_str = return_space .join ([f'{ str (sid ).rjust (max_id_len )} . { config_file } '
247- for sid , config_file in enumerate (config_files , start = 1 )])
292+ config_file_str = return_space .join (
293+ [
294+ f"{ str (sid ).rjust (max_id_len )} . { config_file } "
295+ for sid , config_file in enumerate (config_files , start = 1 )
296+ ]
297+ )
248298 print_str += f"""
249299 \033 [4mbase config files:\033 [0m { config_file_str } """
250300
@@ -259,4 +309,4 @@ def show_index_info(src, only_arrow_files=False, depth=1):
259309 \033 [4mbuckets: Count = { len (keys )} \033 [0m
260310 { resolutions } """
261311
262- print (print_str + ' \n )\n ' )
312+ print (print_str + " \n )\n " )
0 commit comments