The modern web is good for cognitive science

Todd M. Gureckis · ·  11 minutes to read

I've been taking stock of several lab practices in the hopes of making our research more reliable, bug-free, and reproducible. When it comes to designing those experiments, we often turn to web experiments. And when it comes to web development, an honest assessment is that my lab was probably living 5, probably 10, years in the past.

I noticed this becoming an issue when several people in the lab became stuck on debugging and developing their web experiment code. I feel it is my responsibility as PI to try to figure out some general solutions that help people get unstuck. One advantage of being the PI is that I can often see the same problems cropping up in several different projects and also a solution might improve things for everyone.

So what is going on? Why is a lab that is pretty tech-savvy wasting so much time and effort?

Basically, the technology we are using (and developed) sucks.

How past successes can make present-day challenges ​

My lab developed this psiTurk package starting back in 2011 (version 1.0 was released officially in 2013). Amazingly, this ten-year-old code base is still in use! At the time we were developing our front-end code using a mixture of pure Javascript, HTML, CSS and two particular libraries: d3.js and jQuery. In 2011, this was very cutting edge[1]. This general approach worked well for us and we published some interesting and dynamic experiments online that I'm quite proud of. Most new projects build upon these old examples. Open source FTW!



I should clarify for those unfamiliar with my lab's work that our experiments are often a bit more complex than traditional trial-by-trial stimulus-response-based cognitive psychology experiments. As just one example, we had a line of experiments where people were presented with a virtual microchip and were tasked with performing "tests" to debug how it worked... much more like a simple video game than a traditional stimulus-response type paradigm. Many of our current experiments ask people to play real video games including 3D first-person games. We also run experiments where multiple participants play games together in the same virtual environment.

With these types of experiments, the following issues arise:

  1. Developing good instructions is hard: For complex experiments, online participants often need a lot of help with the instructions for such rich and dynamic tasks. Often the instructions for our tasks take longer to develop than the main experiment! Essentially we program what the main task should look like and then deconstruct it into smaller pieces to explain the various interface elements, task goal, incentives, etc... So our instructions aren't just something the subject reads but might include interactive elements, quizzes, and other logic. This is in contrast to say a simple memory experiment where you just tell the subject "memorize these words" and some of the mechanics.

  2. Bugs creep in everywhere: Let's say you design a little interactive element that is beautiful and dynamic and then you implement instructions that use elements of that code to explain the big picture. Oops, then you go back and change the main experiment. Now you have to be careful to propagate all those changes back into the instruction pages which can be a real headache and introduce bugs. Version 2.0 of our experiments tends to have equal development time to the first one even when it basically just means incremental modifications.

  3. Lack of testing: The code can get complex for the main experiment when you start adding treatment conditions on top of the basic code. It is easy to introduce errors. In 2011 the concept of "test-driven development" was uncommon even for scientific codebases written in things like Python. For instance, early versions of psiTurk lacked any real testing infrastructure (which does exist now!). Javascript testing in 2011? In 2015 even? Unlikely outside of Node.js. How do you check your experiment? Run it? Print things to console.log() and hope for the best?

  4. Javascript doesn't work like R/Python data science languages: Javascript is not an easy language for people to dive into if you are used to languages like Python or R (which are more commonly taught in Psychology departments). Perhaps the primary reason is that many methods in Javascript in the browser are asynchronous. This means that unlike R or Python the code doesn't run one line after another such that you can count on the result of one operation to be complete before the next line is run. Understanding when a function is asynchronous (non-blocking) and when it is synchronous (blocking) is often confusing for beginners and experts alike. This introduces hours of debugging. Students don't typically want to learn Javascript in some deeper sense because, well, honestly there aren't many professors around to teach them, esp. in Psychology! In addition, you currently can't use Javascript for much else in science besides web experiments, so the gains made in mastering one language don't get flexed in other parts of a project (e.g., data analysis). This can lead to a lot of frustration and bugs, at least in my experience.

  5. Unwieldy development: Adding to all this is that Javascript 10 years ago wasn't well laid out for complex application development. Ten years ago you couldn't even import submodules in Javascript! Most of our past experiments are programmed in a single file (which becomes unmanageably large). Worse, the default examples for front-end tasks in psiTurk use the very confusing nested call-back method to advance from one part of the task to the next, making it hard to isolate and test individual bits of code. There is very little reuse or modularity across experiments in the lab. Most libraries like jQuery provide relatively low-level functions for modifying the DOM.

  6. Storing data is a hassle: In any experiment, you need a persistent storage location for your data. Most people use some type of database although clever solutions exist like encoding your data into a field inside Mechanical Turk or something. In some ways, psiTurk was developed in part to help with this issue. However, psiTurk has a pretty messy data organization and requires learning a bunch of other features and details. The current recommended solution, using psiTurk on Heroku, requires 15 steps to set up with multiple caveats and options. This can be pretty difficult even for a seasoned UNIX user (even though technically, what these steps do is quite powerful and simple).

Students and "Outside" Developers ​

Some students feel that this all becomes a bit more than they signed up for getting a Ph.D. in Psychology. Some labs have relatively sophisticated software for online research but hire external companies to develop and maintain these designs. Unfortunately, the funding model for cognitive science simply doesn't include that. If I look at my recent NSF grant budgets there is not much room for hiring a programmer or outside developer to implement our experiments for us. But also, I'm not sure this is that desirable from a reproducibility perspective in part because perhaps the hired company might prevent open source sharing of the code, and also we might not understand exactly what they did[2]. I don't think it is too much to ask a graduate student or postdoc to implement a web experiment, given students in other labs have to learn a wet-lab method or train an animal on a task, but also we want to keep that process as streamlined, effective, fun, and trustworthy as possible.

So what can be done about all this? In the rest of this post let me share what I have learned poking around about the current state of the art in web development.


1. The Javascript language changed for the better ​

The first thing is that the Javascript of 10 or 15 years ago, which was designed for little "applets" running on an HTML page, is not the Javascript of today. In particular, the language continues to evolve with new features every few years. Some of these features, for example, modules, proxies, and promises, are major enhancements to the core language which really help make development much saner. These features attempt to help give novice programmers more core language features for dealing with the asynchronous nature of web development.



Going through all of it is too complicated for this post but there is a pretty outstanding online tutorial called The Modern Javascript Tutorial.

2. Modularity and compositionality are the norm ​

Another major change is the development of Javascript frameworks[3] for UI development. Examples include React, Angular, and Vue (my personal favorite having only learned one). These provide a lot of the common features you need for developing complex interactive websites.

The key idea in these systems is the notion of "components." Components can be as simple as the HTML and logic of a button or dropdown list or as complicated as an entire webpage.



It is very familiar for people who come from object-oriented programming (OOP). The key is this allows more modular reuse. A good example of the value here is that someone makes a text box element for a free response but has some validation logic on it (a certain number of words typed or something). Once someone implements that element it makes sense to share it broadly with the community. (Similarly, you can think of widgets that allow subjects to sign their consent form with their finger or mouse, etc...). Components allow this kind of modular reuse and a huge set of libraries are available for dealing with this.



For example, FormKit (try the link—it has some cool interactive demos on the landing page!) is a library of components for Vue that helps you develop complex forms quickly. The syntax is so simple and the form-building process so easy that it rivals products like Qualtrics once you get over the initial learning curve of development in the Vue framework.

3. Testing is possible! ​

The testing ecosystem for Javascript and the web has gotten quite good. Perhaps the most widely used system for unit tests is Jest which is pretty user-friendly. Testing for the web is complicated because the code runs in a browser but most testing systems (like Jest) test code in isolation without rendering it in a browser. Things like the visibility of an element (due to CSS styles) become issues when you look at a page with your own eyes. Fortunately, an outstanding tool has come along called Cypress that lets you easily test your web experiment (or any webpage) exactly as a real participant/user would. (I might write a separate post about this.)

4. Package management aids reproducibility ​

In the past, you included lots of functionality one library at a time via <script src> tags. There are a few recognized problems with this:

  1. Each <script src> tag that references another js library via the HTTP protocol requires a network request to load. This can put additional load on your webserver if a lot of users are accessing your experiment at the same time because instead of 1 request your task is making 10-20 background requests to download supporting libraries.

  2. These supporting libraries are often large monolithic javascript files so you are increasing the amount of data your participants need to download to make the page interactive. On slow network connections, this can take a long time, degrading user experience and causing people with bad connections to selectively bail on your task (very hard to detect the sampling consequences of things like that).

Various "build tools" exist now that take complex Javascript projects that depend on many packages and modules and "bundle" them together. Webpack is one of the most popular but I'm impressed with Vite which is super fast and easy to configure.

5. The Javascript ecosystem is incredibly rich ​

In our scientific code, we are used to the advantages that come from libraries. In Python we use PyTorch or TensorFlow for neural networks, Jupyter for notebooks, NumPy and SciPy for scientific programming, random number generators, etc... In R we use the tidy ecosystem or any number of advanced statistical packages.

The Javascript ecosystem is equally modular and equally diverse in part due to the widespread adoption of Node.js. Although Node is primarily a tool for running javascript as an interpreter (similar to how you run Python or R on your system), it can also be used for front-end development. You set up a node project with a special file (package.json) which is similar to the role that requirements.txt plays in a Python project. This lets you track which versions of packages your project depends on (aiding reproducibility).

Packages include ones you might have heard of (jsPsych) but also advanced tools which help you get things done. For example, Axios for making HTTP requests inside a Javascript application, Lodash for utility functions, or even TensorFlow. The number of packages and diversity is overwhelming.

6. Declarative programming and reactivity are big wins for more reliable scientific web development ​

The general approach for programming dynamic web experiments is to write code that alters what is known as the DOM (Document Object Model). This is a pretty familiar way of doing graphics programming where there is a concept of a "canvas" or "video buffer" which is written on using certain commands/functions (albeit in this case it is a highly structured, symbolic language which is being written/modified). For example, when a user clicks a button on a webpage, a function might run which selects the relevant part of the DOM to modify then adds or deletes information from that object. This style of programming is known as "imperative" because the programmer is writing the definitions of actions to be taken when certain things happen.

An alternative approach is declarative programming. Almost everyone is unknowingly aware of this difference if you have ever used Excel or Google Sheets to compute an equation. In Excel you "declare" a particular cell to be the average of the others (e.g., =average(A1:A3)). The key point is that the code you write doesn't actually include the steps needed to compute this average (e.g., first accessing the value of A1, then A2, then A3 and then summing and dividing by 3). And, even more, it doesn't include all the downstream effects that Excel handles for you such as if you change A1 then the value of the average is automatically recomputed. Often in declarative language systems, the programming language does some of this bookkeeping for you and the programmer is mostly concerned with making the relevant definitions/declarations.

Several modern Javascript frameworks adopt a more declarative programming model, making web development a bit more like Excel than traditional "raw" Javascript. This is very helpful for developing web experiments because it reduces the amount of code you write (and the ways in which you can create bugs).

7. For free or a little bit of money, the data storage issue is solved ​

One of the major features, and purposes, of psiTurk was that experiments need highly reliable ways to save data since you can't save data reliably in the browser. Some people get around this using tricks within Mechanical Turk (posting data back as a payload to the main task) or otherwise developing some other infrastructure. However, often these solutions loop in additional technologies. For example, psiTurk requires you to configure a MySQL or Postgres database. This is somewhat automated for you but also it's a concept you now need to understand to debug or analyze your data. Similarly, these types of database systems can be fairly complex to deal with, requiring a server layer/API between the web application and data.

Recently two advances have made this process much easier. First is the development of NoSQL databases. NoSQL databases take a more document-based approach to data. They have limited ability for complex queries across relational tables. However, they are perfect for experiments where each subject is basically a unique "document" and there are relatively simple grouping factors across documents (e.g. these subjects took part in the same condition or experiment).

The second advance is simple Javascript web APIs to cloud database solutions. Perhaps the most interesting of these is Google's Firebase/Firestore product. This is a NoSQL database that requires no server code to implement a data storage API.

Conclusions ​

To conclude, I fully admit I'm very late to this party. The Node ecosystem has been fabulous for years and was founded in 2009. Google Firebase was launched in 2011 (right around the time of the first psiTurk development, and was purchased by Google in 2014, one year after psiTurk first launched). Vue.js was started in 2014. In addition, I should be clear, my lab is not "just" waking up to these advances. Different students have pursued these technologies already in various configurations over the years. However, it is only recently that I've felt the stability of this stuff has clearly reached a level, for research, that it is reliable and also rich enough to fully get behind and also worth changing up our processes as well as teaching.

I guess the main observation is that the modern web development environment is among the most heavily invested open-source projects in human history. It is comparable to UNIX/R/Python in terms of richness, complexity, and support. And this is all great for scientists!

Footnotes ​


  1. jsPsych (an outstanding tool we now sometimes use) was not yet on the scene, and even a few years later when it did appear it was not as slick as it is now. ↩︎

  2. There's probably some deeper principle about "don't outsource your measurement" if doing empirical science, but maybe that's getting a little over the top. ↩︎

  3. There's a very real sense in which jsPsych, perhaps one of the most popular packages for developing web-based experiments in psychology, is an attempt at developing a web framework similar to React/Angular/Vue, but which is a bit more low-level. ↩︎

How to cite this post

You are welcome to cite this post in your own work. Use the citation or BibTeX entry below.

Citation

Gureckis, T. M. (2024, June 28). The modern web is good for cognitive science. https://todd.gureckislab.org/2024/06/28/modern-web

BibTeX
@misc{gureckis2024modernweb,
  author       = {Todd M. Gureckis},
  title        = {The modern web is good for cognitive science},
  year         = {2024},
  month        = jun,
  howpublished = {Blog post},
  url          = {https://todd.gureckislab.org/2024/06/28/modern-web}
}