Example #1
0
    def test_find_in_tree_gen(self):

        res = list(Utils.find_in_tree_gen(self.spectros_dir, pattern='*.png'))
        expected = [
            f"{self.spectros_dir}/AMADEC/Amaziliadecora1061880.png",
            f"{self.spectros_dir}/FORANA/SONG_XC609364-41759.png",
            f"{self.spectros_dir}/FORANA/SONG_XC253440-FORANA04.png",
            f"{self.spectros_dir}/FORANA/SONG_XC520628-passarochao.png",
            f"{self.spectros_dir}/FORANA/SONG_XC360575-BFAN.png",
            f"{self.spectros_dir}/FORANA/SONG_XC171241-Formicarius_analis.png"
        ]
        self.assertSetEqual(set(res), set(expected))
Example #2
0
 def img_generator(self, in_img_or_dir):
     
     if os.path.isfile(in_img_or_dir):
         return iter([in_img_or_dir])
     return Utils.find_in_tree_gen(in_img_or_dir, '*.png')
    def __init__(self,
                 selection_tbl_loc,
                 spectrogram_locs,
                 out_dir,
                 unittesting=False):
        '''
        Create snippet copies into out_dir 
        for all snippets that are covered
        by any of the given selection tables.
        
        :param selection_tbl_loc: path to individual selection
            table or a directory containing selection tables.
            Each tbl is a tsv file with extension .txt
        :type selection_tbl_loc: str
        :param spectrogram_locs: individual or directory of 
            spectrogram snippets.
        :type spectrogram_locs: str
        :param out_dir: destination of snippet copies
        :type out_dir: src
        :param unittesting: if True, does not initialize
            the instance, or run any operations
        :type unittesting: bool
        '''

        if unittesting:
            return

        if not os.path.exists(selection_tbl_loc):
            print(f"Cannot open {selection_tbl_loc}")
            sys.exit(1)

        if not os.path.exists(spectrogram_locs):
            print(f"Spectrogram snippets {spectrogram_locs} not found")
            sys.exit(1)

        # Is path to sel tbl an individual tsv file?
        if os.path.isfile(selection_tbl_loc):
            table_paths = iter([selection_tbl_loc])
        else:
            # Caller gave directory of .csv files.
            # Get them all recursively:
            table_paths = Utils.find_in_tree_gen(selection_tbl_loc,
                                                 pattern="*.txt")

        # Is snippets path to an individual .png snippet file?
        if os.path.isfile(spectrogram_locs):
            snippet_paths = iter([spectrogram_locs])
        else:
            # Caller gave directory of .png files.
            # Get them all recursively:
            snippet_paths = Utils.find_in_tree_gen(spectrogram_locs,
                                                   pattern="*.png")
        # If out_dir does not exist, create it,
        # and all dirs along the path:
        if not os.path.exists(out_dir):
            os.makedirs(out_dir)

        # Get dict:
        #    {<recording-id> : SelTblSnipsAssoc-instance}
        # where each SelTblSnipsAssoc instance is a generator
        # of snippet metadata from snippet that are covered in
        # the selection table that is associated with the instance.
        # In addition the absolute snippet path is added as
        # entry of key 'snip_path'.
        #
        # The generator feeds out the snippet metadata in order of
        # start time.
        #
        # For brevity, call each instance of SelTblSnipsAssoc
        # an 'assoc'

        rec_id_assocs = self.create_snips_gen_for_sel_tbls(
            snippet_paths, table_paths)

        for assoc in rec_id_assocs.values():
            # The assoc focuses on a single selection
            # table, and the snippets it covers.
            # Get the info contained in each row of
            # the sel tb. This will be a list of dicts, each with
            # the information from one selection tbl row:

            selections = Utils.read_raven_selection_table(
                assoc.raven_sel_tbl_path)

            # Go through each snippet in the association, enrich its
            # metadata with species info. Then copy the enriched
            # snippet to the target dir:

            for snip_metadata in iter(assoc):
                self.match_snippet(selections, snip_metadata, out_dir)