This is the repository for the Sound Communication and Behaviour Group's Python workshop 2025. You will find all teaching materials in this repository.
https://docs.google.com/presentation/d/1bH0J_6equk9mno7ffnL0q0_LL3T7o7mY8hCRrttJ4Bg/edit?usp=sharing
This exercise will teach you the basics of Python.
This exercise is mainly in the terminal. You learn how to make and share virtual environments.
- Make a virtual environment (venv) with conda and python version 3.12.
- Verify that your environment was created by listing all available environments.
- Activate it and use
conda infoand/orconda config --show channelscommands to display information about the environment’s package channels (i.e. repositories from which packages will be installed) - Add the conda-forge channel (more up-to-date and community-run). Use
conda config -h(or--help) or https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-channels.html to learn how to do that . Once added, display the environment’s channel priority information, and make sure it is set to 'strict'.
- Install the following packages:
spyderandspyder-notebooklibrosaScipyversion 1.15.1pip-chillasciimatics,pycowsayand/orcoolname(check out their PyPI sites)
- Export the environment to an
<environment>.ymlfile. The file should work across platforms.- Try creating an environment file for your operating system and compare the two .yml files.
- Share the file with someone in class and take their
<environment>.ymlfile in return. - Make another venv using that environment file, activate it, check its info and package list. Deactivate and remove that environment.
Here is the solution to exercise 2.
Here is the a template for exercise 3: my_module.py and my_script.py.
- Make a project folder called "exercise-3".
- Write a Python module called
my_module.pyby opening a script in your favorite editor.- Define at least 3 functions in
my_module.py. - Each of the functions should have a variation of positional and keyword arguments.
- Write docstrings for your functions.
- Define at least 3 functions in
- Create a second script
my_script.pyin the same directory and import the module. - Execute all of your functions in
my_script.py. Use a variation of arguments to test your functions. You should also print the docstring to the terminal using thehelp(my_module.<your_function>)command. - Now move the
my_module.pyfile into a different directory and adjust the imports accordingly.- Move the file into a new directory within "exercise-3".
- Move the file into a different directory outside of "exercise-3". Use the sys module to append the new path/location of
my_module.py.
Here is the solution to exercise 3.
Here is the template for exercise 4.
- Make a new directory and unzip the zip-folder "find_the_wavs.zip" into it.
- Make a new venv for this exercise and install the packages
scipy,matplotlib,soundfileandsounddevice. - There are 15 audio files hidden in "find_the_wavs". Find all and put them into a single list. Sort the list based on the filenames.
- Read a random audiofile into an array.
- Extract the following parameters: sampling rate, number of channels, total number of samples, duration, bit depth.
- Compare the output array from soundfile and scipy. What is the difference and how can you convert one to the other?
- Play the audio file at an appropriate sampling rate.
- [Optional] Loop over all recordings: which has the most channels? Which is the longest recording (duration in seconds)?
- Pick the recording #5 to continue this exercise. Plot the oscillogram of the recording with an appropriate time scale.
- Select a call segment to continue your analysis with. The segment should only contain a single vocalization. This segment will be used from here onwards.
- Spyder: to get inline plots working you need to unselect "Mute inline plotting" in the Plots pane menu (right side)
- Now the plots are inline in the consol. To make it interactive you need to run %matplotlib qt in the consol. Then figures are opened in a separate window. Run the figure code again after setting matplotlib to qt. To move back to inline plots run %matplotlib inline.
- Default handling of plots can be changed in Tools > Preferences > IPython console > Graphics.
- Generate a plot that indicates which part of the recording segment contains 95% of the energy. You can use the cumulative energy to find the correct window.
- [Optional] Extract the following basic parameters from the time domain signal: peak, peak-to-peak and RMS pressure, energy.
- Transform the signal segment into the frequency domain. Additionally, estimate the power spectral density with Welch's method. Plot both into the same figure. Make sure you normalize both correctly to display [dB/Hz] on the y-axis.
- [Optional] Extract the following basic parameters from the frequency domain signal: peak frequency, bandwidth and centroid frequency.
- Plot a spectrogram of the whole recording.
- [Optional] Convert the analysis pipeline into a series of functions such that you can loop over all 15 recordings and display all plots and extract all parameters where possible.
Here is the solution to exercise 4.
| topic | tutorial |
|---|---|
| Packaging 101 - make you own installable package | https://www.pyopensci.org/python-package-guide/tutorials/intro.html |
| Pandas | https://pandas.pydata.org/docs/getting_started/intro_tutorials/index.html |
| Matplotlib | https://matplotlib.org/stable/users/explain/quick_start.html#quick-start |
| Numpy | https://numpy.org/devdocs/user/quickstart.html |
| Seaborn | https://seaborn.pydata.org/tutorial/introduction.html |
| Sklearn (PCA example) | https://scikit-learn.org/stable/auto_examples/decomposition/plot_pca_iris.html#sphx-glr-auto-examples-decomposition-plot-pca-iris-py |
