2nd Edition

Python for Experimental Psychologists A fun way of learning how to code your experiments and analyses

By Edwin S. Dalmaijer Copyright 2025
    152 Pages 20 B/W Illustrations
    by Routledge

    152 Pages 20 B/W Illustrations
    by Routledge

    Python for Experimental Psychologists equips researchers with no prior programming experience with the essential knowledge to independently script experiments and analyses, swiftly mastering Python.

    This updated edition navigates through programming fundamentals before delving into experimental scripting and data analysis. Aligned with the latest Python patch (Python 3), it illuminates techniques for displaying stimuli on computer screens, obtaining input from peripherals (e.g., keyboard, mouse), and interfacing with specialised equipment (e.g., eye trackers). Readers learn data logging and timing control alongside principles of basic and advanced data analysis, including trace data (e.g., pupil size) and gaze data analysis.

    Access to a companion website enriches the learning experience with colour figures, example stimuli, datasets, scripts, and a portable Windows installation of Python. The book’s informal and accessible tone deliberately targets aspects of Python pertinent to experimental psychologists and cognitive neuroscientists. It stands as a pivotal resource not only for researchers in these fields but also for lecturers instructing on experimentation and foundational data analysis.

    Python for Experimental Psychologists demystifies programming complexities, empowering researchers to proficiently execute experiments and analyse results, thus catalysing advancements in experimental psychology and cognitive neuroscience.

    About this book II
    About Python 3
    About the authors 3
    1. Python 1
    1.1 Installation 3
    1.1.1 Versions 3
    1.1.2 Dependencies 4
    1.1.3 Anaconda 5
    1.1.4 WinPython 6
    1.1.5 Installing packages with pip 6
    1.2 Terminal and interpreter 8
    1.3 Editor 11
    2. Variable Types 13
    2.1 Numbers 13
    2.1.1 Integers 14
    2.1.2 Floats 16
    2.2 Assigning Variables 19
    2.3 Booleans 22
    2.4 Letters 26
    2.4.1 Strings 26
    2.4.2 String Functions 27
    2.4.3 String formatting 29
    2.5 Sets 32
    2.5.1 Lists 33
    2.5.2 Tuples 40
    2.5.3 Arrays 41
    2.5.4 NumPy Arrays 43
    2.5.5 Dictionaries 45
    2.6 Classes 48
    2.7 Functions 49
    2.7.1 Arguments 53
    2.7.2 Keyword arguments 54
    2.7.3 Local and Global Variables 56
    2.7.4 Create Your First Function 57
    2.7.5 Create Your Second Function 58
    2.7.6 Create Your Third Function 59
    Make Some Noise 62
    Random Numbers 62
    Noise 63
    Matplotlib 65
    3. Creating and Presenting Stimuli 67
    3.1 Scripts 68
    3.2 The PsychoPy Coder View 69
    3.3 The Window 70
    3.3.1 Units and Colours in PsychoPy Windows 72
    3.4 Comments 75
    3.5 Make some text 76
    3.4.1 Understanding win.flip() 77
    3.5 Throwing Shapes 80
    3.6 Presenting Images 81
    3.6.1 Adding masks to images using Numpy arrays 82
    3.6.2 Blurring images using Python Imaging Library 84
    3.6 Making many stimuli 85
    3.6.1 Creating stimuli in random locations 86
    3.6.2 For Loops 87
    3.6.3 Indentation 89
    3.6.4 While Loops 92
    3.6.5 Using Loops to generate many stimuli 95
    3.6.5 Element Array Stim 96
    3.7 Timing visual stimuli by frames 98
    3.8 Making dynamic stimuli 100
    3.9 Comments (again) 102
    4. Processing Responses 103
    4.1 Are Peripherals Accurate Enough? 104
    4.2 Keyboard input 106
    4.2.1 Waiting for Keyboard input 107
    4.2.3 Understanding the “zero point” of keyboard response times 110
    4.2.2 Fetching Keyboard input without waiting 112
    4.3 If Statements 114
    4.4 Typed responses 116
    4.5 Mouse Responses 118
    4.5.1 Fetching the current mouse position 119
    4.5.2 Check the status of the mouse buttons 120
    4.5.4 Check for mouse scroll behaviour 122
    4.6 Using while loops with the mouse 123
    Make Some Noise 125
    What Is Sound? 125
    Sinusoid and Noise 125
    Numbers To Sound 131
    5. Scripting An Experiment 137
    5.1 Make a single trial 138
    5.1.1 Presenting stimuli in our experiment 139
    5.1.2 Using functions in experiments 142
    5.1.3 Gathering responses in our experiment 144
    5.2 Presenting several trials 145
    5.2.1 using variable timing trial-by-trial 147
    5.3 Importing spreadsheets and saving data 148
    5.3.1 Importing a spreadsheet of trials 149
    5.3.2 The TrialHandler 151
    5.3.3 The Experiment Handler 152
    5.4 Gathering participant information 153
    5.4.1 Constants 153
    5.4.2 Presenting a dialogue box 154
    5.5 Storing keyboard accuracy 156
    5.6 Block designs and counterbalancing 158
    5.6.1 Randomised block designs 158
    5.6.2 Counterbalanced block designs 160
    5.7 Understanding the data 161
    Make Some Noise 163
    Do You Have A Joystick? 163
    Using a Joystick 165
    Using a Keyboard 168
    Playing Sounds On Button Presses 171
    Unexpected Instrument 175
    6. Analysing Behavioural Data 178
    6.1 Analysis Plan 178
    6.2 Extracting Data 180
    6.3 Selecting Data 187
    6.4 Averaging Data 190
    6.5 Plotting Data 195
    6.6 Combining Datasets 201
    6.7 Statistical Testing 216
    7. Analysing Traces 219
    7.0.1 Pupil Size 219
    7.0.2 PyGaze Analyser 220
    7.1 Read Eye-Tracker Data 220
    7.2 Plot Your First Trace 223
    7.3 Averaging Traces 227
    8. Eye Tracking 237
    8.0.1 The Basics 238
    8.1 Creating An Eye-Tracking Experiment 240
    8.1.1 Materials 240
    8.1.2 constants.py 241
    8.1.3 Screens 244
    8.1.4 The EyeTracker Class 246
    8.1.5 Single Trial 249
    8.1.6 Whole Experiment 254
    8.2 Processing Gaze Data 256
    8.2.1 Extracting Gaze Data 258
    8.2.2 Processing Gaze Data 263
    8.2.3 Analysing Fixations 265
    9. Common Statistical Tests 273
    9.1 UGH BORING 273
    9.1.1 Variance and Error 273
    9.1.2 When is something surprising? 276
    9.2 T-tests 278
    9.2.1 What is a p-value? 279
    9.2.2 One-sample t-test 280
    9.2.3 Related-samples t-test 282
    9.2.4 Independent-samples t-test 284
    9.3 Correlation 285
    9.3.1 Covariance 286
    9.3.2 Pearson's R coefficient 287
    9.3.3 Spearman's ρ coefficient 288
    9.3.4 Side-note: Who are these tests named after? 289
    9.4 Linear regression 289
    9.4.1 Univariable linear regression 291
    4.9.2 Multivariable linear regression 292
    Getting Help 298
    Stack Overflow 298
    Documentation Websites 298
    PyGaze Forum 298
    Acknowledgements 300
    References 302

    Biography

    Edwin S. Dalmaijer is a Lecturer (Assistant Professor) at the University of Bristol. He has authored several Python libraries, standalone software packages, teaching materials, and research articles.

    An easy-to-read introduction. In a humorous style it introduces the reader to programming in Python, picking them up where they are as experimental psychologists. Very useful indeed!

    Ulrich von Hecker, Senior Lecturer, Cardiff University, UK

    Edwin Dalmaijer provides an intuitive introduction into psychological experimentation using Python libraries co-developed by the author himself. Well-structured and easy to read, this book will quickly have you programming, running and analysing your own experiments using open, modern tools. 

    Tomas Knapen, Assistant Professor Cognitive Neuroscience, Vrije Universiteit Amsterdam, The Netherlands

    Python is fast becoming the lingua franca of scientific research, and this excellent and highly accessible book fills a much-needed gap for experimental psychologists. The strong focus on developing genuinely useful experimental code, and the comprehensive examples included, make it an outstandingly useful resource not just for students, but for seasoned researchers as well. Certainly the best (and funniest!) introduction to Python I’ve come across.

    Matt Wall, Imaging Scientist, Imperial College London, UK