Browse Source

Filter annotation importation by recording file names from already imported annotation file

William N. Havard 2 years ago
parent
commit
1d88c23075
1 changed files with 16 additions and 6 deletions
  1. 16 6
      import_data/import_annotations.py

+ 16 - 6
import_data/import_annotations.py

@@ -155,7 +155,7 @@ def _build_raw_filename(input, annotation_format, filename='', extension=''):
     return input
 
 
-def _import_annotation(project, am, annotation_type, annotation_file, recording):
+def _import_annotation(project, am, annotation_type, annotation_file, recordings_from_annotation_file, recording):
     annotation_set = annotation_type.lower()
     annotation_format = ANNOTATION_TYPES.asdict()[annotation_set.upper()][0]
 
@@ -165,6 +165,10 @@ def _import_annotation(project, am, annotation_type, annotation_file, recording)
     # Build raw file names
     input = _build_raw_filename(input, annotation_format, annotation_file)
 
+    if recordings_from_annotation_file:
+        anns_from_annotation_file = am.annotations[am.annotations['raw_filename'] == recordings_from_annotation_file]
+        input = input[input['recording_filename'].isin(anns_from_annotation_file['recording_filename'])]
+
     # Filter out rows for which we do not find the matching annotation file
     input = _filter_missing_annotation_files(project.path, input)
 
@@ -193,7 +197,7 @@ def _import_annotation(project, am, annotation_type, annotation_file, recording)
 
 
 
-def import_annotations(data_path, annotation_type, annotation_file, recording):
+def import_annotations(data_path, annotation_type, annotation_file, recordings_from_annotation_file, recording):
     """
     Imports all the new annotations files that are found in the `annotations` directory. This directory will
     be recursively explored.
@@ -208,10 +212,10 @@ def import_annotations(data_path, annotation_type, annotation_file, recording):
     am = AnnotationManager(project)
     am.read()
 
-    _import_annotation(project, am, annotation_type, annotation_file, recording)
+    _import_annotation(project, am, annotation_type, annotation_file, recordings_from_annotation_file, recording)
 
 
-def main(annotation_type, annotation_file, recording=None):
+def main(annotation_type, annotation_file, recordings_from_annotation_file= None,recording=None):
     data_path = '.'
 
     # Check if running the script from the root of the data set
@@ -221,8 +225,11 @@ def main(annotation_type, annotation_file, recording=None):
         ValueError('Expected annotation ({}) path not found. Are you sure to be running this '
                    'command from the root of the data set?'.format(expected_annotation_path))
 
+    assert not(recording and recordings_from_annotation_file), \
+        ValueError('--recording and  --recordings-from-annotation-file cannot be used together!')
+
 
-    import_annotations(data_path, annotation_type, annotation_file, recording)
+    import_annotations(data_path, annotation_type, annotation_file, recordings_from_annotation_file, recording)
 
 
 def _parse_args(argv):
@@ -234,8 +241,11 @@ def _parse_args(argv):
     parser.add_argument('--annotation-file', required=True,
                         help='Annotation file to be imported.')
     parser.add_argument('--recording', required=False,
-                        help='If only the annotations for a specific recording should be imported, specify the name '
+                        help='If only the annotations for one specific recording should be imported, specify the name '
                              'here. Otherwise, all recordings will be considered.')
+    parser.add_argument('--recordings-from-annotation-file', required=False,
+                        help='Only import annotations of --annotation-file for all the recordings that were annotated '
+                             'in the file --recordings-from-annotation-file')
     args = parser.parse_args(argv)
 
     return vars(args)