Set Up Python Environment for CogSci 131

3 minute read

CogSci 131 (Computational Modeling of Cognition) is one of my favorite classes at Berkeley when I took it with Prof. Tom Griffiths in 2016. Now I’m teaching it in my last semester. Before getting to the fun parts (e.g., implementing recurrent nets and classic RL algorithms like SARSA using vanilla NumPy), one needs to set up the right Python environment. I hope this tutorial can save students some headaches.

Option #1: Conda Virtual Environment

To avoid nightmares down the lineπŸ‘‡, use virtual environments for your local stuff.

  1. Install Miniconda: Install the right version for your operating system from this page (I used Anaconda years ago that comes with dozens of pre-installed packages, which is more cumbersome or convenient, depending on the perspective)
Selection of latest Miniconda installers (if your machine has an Apple silicon chip, pick the third option under MacOS).

Selection of latest Miniconda installers (if your machine has an Apple silicon chip, pick the third option under MacOS).

  1. Create environment: Open a terminal πŸ‘‰ cd to where you wanna launch Jupyter (for me it’s cd /Users/apple/Documents/spring2022/cogsci131/notebooks) πŸ‘‰ run this command conda create --name cogsci131 python=3.9.5

    • “cogsci131” is the name of the environment; you can use whatever you like
    • Python 3.9.5 is the lasted version that conda installs as of writing; replace with the latest version when you do this
  2. Activate environment: In the same directory, run conda activate cogsci131

  3. Install packages: For this course (or cognitive science in general), you most likely need to conda install pandas numpy scipy matplotlib seaborn jupyterlab pandoc

    • After all the installations are completed, you should be able to launch Jupyter Lab from this environment: jupyter lab
      Jupyter Lab opens a web browser πŸ‘‰ you can start a kernel and create a notebook from there

      Jupyter Lab opens a web browser πŸ‘‰ you can start a kernel and create a notebook from there

  4. (Optional) Export environment: If for some reason you need to recreate the above environment (nuked it or changed computers), you can export the settings to a YAML file (conda env export > cogsci131.yml) and create a new environment from the saved file (conda create --name cogsci131 --file cogsci131.yml)

Option #2: Google Colab

If you wanna avoid the pain for now, you can use Google Colab, which is like Google Docs, but for Jupyter notebooks. The user interface is slightly different between the two. Colab makes it easy to share your work, but if you wanna install a package permanently on Colab, you may need to link it to Google Drive (here’s how).

Further Resources

  • The Good Research Code Handbook: Having a Python environment is sufficient for this course but is only the starting point of scientific computing. This book teaches you how to organize your computational projects from beginning to end.
  • Bayesian Modeling and Computation in Python (online version, physical copy): Exact Bayesian inference is expensive — both for us as computational modelers and for the mind to carry out computations in everyday reasoning. For Python folks, PyMC3 is perhaps the most popular package for approximate Bayesian inference using Markov chain Monte Carlo (MCMC) or variational inference (VI). This new book from the PyMC developers is a great way to learn it.
  • How to Cog Sci: Prof. Todd Gureckis at NYU has a shockingly well-produced video series introducing Jupyter, Python, stats, and data wrangling (the production quality is definitely up there with the best professional YouTubers). It’s amazing to imagine how Todd patiently teaches the basics extremely well while being an incredibly prolific scientist and open-source contributor…