@@ -51,8 +51,7 @@ def json_file_path(self) -> str:
51
51
@property
52
52
def input_data_id (self ) -> str :
53
53
"""
54
- JSONファイルから決まる、input_data_id.
55
- Simple(v2)版用です。
54
+ JSONファイルから決まるinput_data_id.
56
55
"""
57
56
return self .__input_data_id
58
57
@@ -201,7 +200,7 @@ class SimpleAnnotationDirParser(SimpleAnnotationParser):
201
200
Examples:
202
201
JSONファイルをパースする::
203
202
204
- p = SimpleAnnotationDirParser(Path("task_id/input_data_id.json"))
203
+ p = SimpleAnnotationDirParser(Path("annotation/ task_id/input_data_id.json"))
205
204
annotation = p.parse()
206
205
207
206
"""
@@ -311,28 +310,134 @@ def __init__(self, task_id: str):
311
310
def task_id (self ) -> str :
312
311
return self .__task_id
313
312
313
+ @property
314
+ @abc .abstractmethod
315
+ def json_file_path_list (self ) -> List [str ]:
316
+ """
317
+ パースするJSONファイルパスのリスト
318
+ """
319
+
320
+ @abc .abstractmethod
321
+ def get_parser (self , json_file_path : str ) -> SimpleAnnotationParser :
322
+ """
323
+ JSONファイルパスから、Simple Annotation parserを取得する。
324
+
325
+ Args:
326
+ json_file_path: パースするJSONファイルのパス。``json_file_path_list`` に含まれる値を指定すること。
327
+
328
+ Returns:
329
+ Simple Annotation parser
330
+
331
+ Raises:
332
+ ValueError: ``json_file_path`` の値が ``json_file_path_list`` に含まれていないとき
333
+
334
+ """
335
+
314
336
@abc .abstractmethod
315
337
def lazy_parse (self ) -> Iterator [SimpleAnnotationParser ]:
316
338
pass
317
339
318
340
319
341
class SimpleAnnotationZipParserByTask (SimpleAnnotationParserByTask ):
320
- def __init__ (self , zip_file : zipfile .ZipFile , task_id : str , json_path_list : List [str ]):
342
+ """
343
+ Simple Annotation zipのparserをタスクごとにまとめたもの。
344
+
345
+ Args:
346
+ zip_file: Simple Annotation zipのzipfileオブジェクト
347
+ task_id: タスクID
348
+ json_path_list: パースするJSONパスのリスト。
349
+ Noneの場合は、``zipfile.ZipFile.infolist()`` 関数を呼び出して、JSONパスのリストを生成します。
350
+
351
+ Examples:
352
+ JSONファイルをパースする::
353
+
354
+ with zipfile.ZipFile("simple-annotation.zip", "r") as zip_file:
355
+ p = SimpleAnnotationZipParserByTask(zip_file, "task1")
356
+
357
+ """
358
+
359
+ def __get_json_file_path_list (self , task_id : str ) -> List [str ]:
360
+ """
361
+ task_idとJSONパスリストの辞書を取得する。
362
+ """
363
+
364
+ def _match_task_id_and_contain_input_data_json (zip_info : zipfile .ZipInfo ) -> bool :
365
+ """
366
+ task_idディレクトリ配下の入力データJSONかどうか
367
+ """
368
+ paths = [p for p in zip_info .filename .split ("/" ) if len (p ) != 0 ]
369
+ if len (paths ) != 2 :
370
+ return False
371
+ if paths [0 ] != task_id :
372
+ return False
373
+ if not paths [1 ].endswith (".json" ):
374
+ return False
375
+ return True
376
+
377
+ return [
378
+ zip_info .filename
379
+ for zip_info in self .__zip_file .infolist ()
380
+ if _match_task_id_and_contain_input_data_json (zip_info )
381
+ ]
382
+
383
+ def __init__ (self , zip_file : zipfile .ZipFile , task_id : str , json_path_list : Optional [List [str ]] = None ):
321
384
self .__zip_file = zip_file
322
- self .__json_path_list = json_path_list
385
+ if json_path_list is not None :
386
+ self .__json_path_list = json_path_list
387
+ else :
388
+ self .__json_path_list = self .__get_json_file_path_list (task_id )
323
389
super ().__init__ (task_id )
324
390
325
391
def lazy_parse (self ) -> Iterator [SimpleAnnotationZipParser ]:
326
392
return (SimpleAnnotationZipParser (self .__zip_file , e ) for e in self .__json_path_list )
327
393
394
+ @property
395
+ def json_file_path_list (self ) -> List [str ]:
396
+ return self .__json_path_list
397
+
398
+ def get_parser (self , json_file_path : str ) -> SimpleAnnotationParser :
399
+ if json_file_path in self .__json_path_list :
400
+ return SimpleAnnotationZipParser (self .__zip_file , json_file_path )
401
+ else :
402
+ raise ValueError (f"json_file_path '{ json_file_path } ' は `json_file_path_list` に含まれていません。" )
403
+
328
404
329
405
class SimpleAnnotationDirParserByTask (SimpleAnnotationParserByTask ):
330
- def __init__ (self , task_id : str , json_path_list : List [Path ]):
331
- self .__json_path_list = json_path_list
406
+ """
407
+ Simple Annotation zipを展開したディレクトリのparserをタスクごとにまとめたもの。
408
+
409
+ Args:
410
+ task_id: Simple Annotation zipのzipfileオブジェクト
411
+ task_id: タスクID
412
+ json_path_list: タスク配下のJSONパスのリスト。パスにはtask_idを含む。
413
+
414
+ Examples:
415
+ JSONファイルをパースする::
416
+
417
+ with zipfile.ZipFile("simple-annotation.zip", "r") as zip_file:
418
+ p = SimpleAnnotationZipParserByTask(zip_file, "task1", ["task1/input1.json","task1/input2.json"])
419
+
420
+ """
421
+
422
+ def __init__ (self , task_dir_path : Path ):
423
+ self .__task_dir_path = task_dir_path
424
+ task_id = task_dir_path .name
332
425
super ().__init__ (task_id )
333
426
334
427
def lazy_parse (self ) -> Iterator [SimpleAnnotationDirParser ]:
335
- return (SimpleAnnotationDirParser (e ) for e in self .__json_path_list )
428
+ return (
429
+ SimpleAnnotationDirParser (e ) for e in self .__task_dir_path .iterdir () if e .is_file () and e .suffix == ".json"
430
+ )
431
+
432
+ @property
433
+ def json_file_path_list (self ) -> List [str ]:
434
+ return [str (e ) for e in self .__task_dir_path .iterdir () if e .is_file () and e .suffix == ".json" ]
435
+
436
+ def get_parser (self , json_file_path : str ) -> SimpleAnnotationParser :
437
+ if json_file_path in self .json_file_path_list :
438
+ return SimpleAnnotationDirParser (Path (json_file_path ))
439
+ else :
440
+ raise ValueError (f"json_file_path '{ json_file_path } ' は `json_file_path_list` に含まれていません。" )
336
441
337
442
338
443
def __parse_annotation_dir (annotaion_dir_path : Path , clazz ) -> Iterator [Any ]:
@@ -405,6 +510,9 @@ def is_input_data_json(zip_info: zipfile.ZipInfo) -> bool:
405
510
return True
406
511
407
512
def create_task_dict (arg_info_list : List [zipfile .ZipInfo ]) -> Dict [str , List [str ]]:
513
+ """
514
+ task_idとJSONパスリストの辞書を取得する。
515
+ """
408
516
task_dict : Dict [str , List [str ]] = {}
409
517
sorted_path_list = sorted ([e .filename for e in arg_info_list if is_input_data_json (e )])
410
518
@@ -447,9 +555,7 @@ def lazy_parse_simple_annotation_dir_by_task(annotaion_dir_path: Path) -> Iterat
447
555
if not task_dir .is_dir ():
448
556
continue
449
557
450
- task_id = task_dir .name
451
- json_path_list = [e for e in task_dir .iterdir () if e .is_file () and e .suffix == ".json" ]
452
- yield SimpleAnnotationDirParserByTask (task_id = task_id , json_path_list = json_path_list )
558
+ yield SimpleAnnotationDirParserByTask (task_dir )
453
559
454
560
455
561
def __parse_annotation_zip (zip_file_path : Path , clazz ) -> Iterator [Any ]:
0 commit comments