Using the file hash to verify an image stimulus

Available in post-0.4.4 releases of MWorks, including nightly builds since January 25, 2011

Whenever MWorks displays an image stimulus, information about the image is logged to the data file in a #stimDisplayUpdate event. The event data contains two fields that identify the image file used: filename, which contains the full filesystem path to the image file, and file_hash, which contains the SHA-1 hash of the image file.

Unlike the file name (which can be changed easily), the file hash provides a robust means of determining whether a given image file was in fact displayed in an experiment. The MWorks Python package includes a convenient function, file_hash(), that computes the hash of a file:

>>> from mworks import file_hash
>>> file_hash('image.png')
'e2e97203031d6c5c018aa8bfb268566a6e2d5b6a'

Alternatively, the openssl command-line tool can be used to obtain the file hash:

$ openssl dgst -sha1 image.png 
SHA1(image.png)= e2e97203031d6c5c018aa8bfb268566a6e2d5b6a

Once you have the hash for a file, you can compare it to the file_hash field in a #stimDisplayUpdate event. If the two hashes are identical, then you can be confident that the given file is indeed the image that was displayed in your experiment.