-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/multi file inference #96
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
e7b6a4b
now we create metadata also for InferenceDataset so we could support …
mosheman5 6ed908d
remove unused method
mosheman5 aa785e9
wip
mosheman5 3872d5b
add the class probabilities to the inference results, run on all chan…
mosheman5 be600f1
merge the probability with the emitted call
mosheman5 752a267
make filename first, would be easier to find it later
mosheman5 386e911
use sample rate instead of data sample rate
mosheman5 90046c4
script to merge multiple raven file into a single one
mosheman5 f655243
bugfix: test works now
mosheman5 b6ccec8
make sure we support multiple classes in the inference script
mosheman5 49479c4
now it works on all classes as well!
mosheman5 79c8322
Merge branch 'master' into feature/multi-file-inference
mosheman5 f0f87f1
add option to include the audio file name in the merged raven output
mosheman5 3b81863
Merge branch 'master' into feature/multi-file-inference
mosheman5 b73551b
sort by filename and begin time
mosheman5 b6cfb90
fix typo
mosheman5 82062c6
enable manually changing the max frequency of the output raven files
mosheman5 e696fa2
add to more fields to the raven outputs
mosheman5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import pandas as pd | ||
from pathlib import Path | ||
import argparse | ||
import soundfile as sf | ||
from tqdm import tqdm | ||
|
||
|
||
def make_parser(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--input-raven-folder", "-ir", | ||
help="Path to the directory containing the raven files to be merged.", type=str) | ||
parser.add_argument("--input-audio-folder", "-ia", | ||
help="Path to the directory containing the audio files that are aligned to the annotations.", | ||
type=str) | ||
parser.add_argument("--output-path", "-o", | ||
help="Path the the output path of the merged raven annotation", type=str) | ||
parser.add_argument("--include-begin-file", "-ibf", dest="include_begin_file", action="store_true") | ||
parser.add_argument("--no-begin-file", "-nbf", dest="include_begin_file", action="store_false") | ||
parser.set_defaults(include_begin_file=True) | ||
return parser | ||
|
||
|
||
def main() -> None: | ||
""" | ||
This script is used to merge multiple raven annotation files into one file. | ||
""" | ||
# configurations: | ||
args = make_parser().parse_args() | ||
raven_folder = Path(args.input_raven_folder) | ||
audio_folder = Path(args.input_audio_folder) | ||
output_path = Path(args.output_path) | ||
include_begin_file = args.include_begin_file | ||
# get the list of raven files | ||
raven_files = list(raven_folder.glob('*.txt')) | ||
# get the list of audio files | ||
audio_files = list(audio_folder.glob('*.wav')) | ||
# sort the audio files by name, should be the order by start time as well | ||
audio_files = sorted(audio_files) | ||
assert len(raven_files) == len(audio_files), "The number of raven files and audio files should be the same." | ||
# create a mapping between the raven files and the audio files | ||
adapted_files_list = [] | ||
for file in tqdm(audio_files, desc="Mapping audio files to raven files"): | ||
file_stem = file.stem | ||
adapted_raven_files = [raven_file for raven_file in raven_files if file_stem in raven_file.stem] | ||
assert len(adapted_raven_files) == 1, f"Expected one raven file for {file_stem}, found {len(adapted_raven_files)}" | ||
raven_file = adapted_raven_files[0] | ||
res = {"name": file_stem, "audio_file": file, "raven_file": raven_file} | ||
adapted_files_list.append(res) | ||
# create a list to store the dataframes | ||
df_list = [] | ||
# iterate over the raven files | ||
seconds_offset = 0 | ||
entries_offset = 0 | ||
for entry in tqdm(adapted_files_list, desc="Merging raven files"): | ||
# read the raven file | ||
df = pd.read_csv(entry["raven_file"], sep="\t") | ||
# add the offset to the begin and end time | ||
df['Begin Time (s)'] += seconds_offset | ||
df['End Time (s)'] += seconds_offset | ||
df['Selection'] += entries_offset | ||
mosheman5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if include_begin_file: | ||
df['Begin File'] = [entry["audio_file"].name] * df.shape[0] | ||
# get the audio file duration | ||
audio_file_duration = sf.info(entry["audio_file"]).duration | ||
# add the audio file duration to the offset | ||
seconds_offset += audio_file_duration | ||
entries_offset += df.shape[0] | ||
# add the dataframe to the list | ||
df_list.append(df) | ||
|
||
# concatenate the dataframes | ||
concatenated_df = pd.concat(df_list) | ||
# save the concatenated dataframe | ||
concatenated_df.to_csv(output_path, sep="\t", index=False) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it might be nice to allow the user to define the key to sort by (sort by name can be the default)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not gonna implement it now, if needed in the future we can have a user defined key ordering