nilomr / pykanto

A python library for animal vocalisation analysis
https://nilomr.github.io/pykanto
MIT License
20 stars 3 forks source link

Exporting units as .wav and .json? #37

Open ArKornreich opened 1 week ago

ArKornreich commented 1 week ago

Hi Nilo!

Thank you again so much for your help earlier this year, and sorry to keep bothering you with questions!

Is there a way to take the units of songs and, before they are projected in the interactive app, save them much the way the segmenting process generates .wav files and .json files for individual songs in longer recordings? This functionality probably exists, but embarassingly I'm having a bit of trouble figuring out how to do this.

Thank you!

nilomr commented 1 week ago

Hi Ar

Happy to help!

Is there a way to take the units of songs and, before they are projected in the interactive app, save them much the way the segmenting process generates .wav files and .json files for individual songs in longer recordings? This functionality probably exists, but embarassingly I'm having a bit of trouble figuring out how to do this.

Yes, you can easily save individual files for each 'unit' by loading the file that contains it and using the existing unit onset and offset times to extract and save them one by one.

https://gist.github.com/nilomr/dc153c7c6b1b399a837355548d2ffdd7

There you can see how to get onset and offsets as well as labels if they exist, eg

key = "any_song_key"
onsets = dataset.data.loc[key, "onsets"]
offsets = dataset.data.loc[key, "offsets"]
labels = dataset.data.loc[key, "song_seq"]

Once you have that you can open the wav for that song and segment it using your favourite method. You could use the soundfile module as in this example: https://github.com/nilomr/pykanto/blob/35d2218d5ac13a13b3c1457ea68150358be295c3/pykanto/signal/segment.py#L302

Hope that helps! Nilo

ArKornreich commented 2 days ago

Hi Nilo!

I'm so sorry for my delay in getting back to you! I'm going to try to test this sometime this week and get back to you with any issues. I haven't forgot about this! Deepest apologies!

Best,

Ar K