<p>In the <a href="https://discourse.psychopy.org/t/accuracy-of-psychopy-keystroke-duration-measurement/3049/3" rel="nofollow noreferrer">Psychopy forum</a> I have received some advice:</p>

<ul>
<li>One would be to change the code: take out the print command, no
redefining of variables </li>
<li>USB, USB device, the python and system
libraries should be the newest and fastest, if possible, because
these could have an influence on the accuracy. </li>
<li>And that Psychtoolbox forum could have the solution, but so far I haven't found it.</li>
</ul>

CC BY-SA 3.0
2017-10-16T00:03:08.200Z
This is sadly a total absence of response. Referring to external sources without checking is not a good contribution to this site.
Denis Cousineau
CC BY-SA 3.0
2017-10-17T14:43:56.240Z
This is not my answer, as stated, and therefore also not checked as solving the problem. Following  Steven Jeuris advice I've asked the question in a different forum and just summarized the answer I've received here, to share this bit of information or to trigger some ideas. Perhaps a comment would have been better, but the question above had so many comments, that I thought it would be too confusing
Nathalie
CC BY-SA 3.0
2017-09-15T18:21:55.830Z
2017-10-15T20:16:36.320Z
2017-10-15T20:16:36.320Z
Nathalie
<p>This frankly should have been asked on SO as it's 99% a programming question. There are a lot of things that can contribute to the "spiky" accuracy you've seen:</p>

<ul>
<li><p>language/runtime: Python is hardly a good idea for anything requiring real-time accuracy. It's an interpreted language with <a href="https://rushter.com/blog/python-garbage-collector/" rel="nofollow noreferrer">garbage collection</a>. The [re]assignments in your loop will (eventually) trigger that.  I've written once a Python GTK interface for a real-time device, but the code controlling the device was entirely in C, and nothing time-critical was done on Python side.</p></li>
<li><p>Ultimately, the C runtime dynamic memory allocations (which <a href="https://dmalcolm.fedorapeople.org/presentations/PyCon-US-2011/MemoryUsage.pdf" rel="nofollow noreferrer">Python eventually relies on</a>) are also <a href="http://mil-embedded.com/articles/justifiably-apis-militaryaerospace-embedded-code/" rel="nofollow noreferrer">not predictable in terms of timing</a>, although this is probably not what you're seeing. For real-time software dynamic allocation is generally avoided.</p></li>
<li><p>operating system: not running at high enough process/thread priority so you get occasionally preempted by other processes/threads.</p></li>
<li><p>hardware/driver: USB sends packetized data at limited poll rate; <a href="https://books.google.com/books?id=YCslDwAAQBAJ&amp;pg=PA53" rel="nofollow noreferrer">8ms (125 Hz)</a> being the default for most input devices. Unless you can change the default, a PS/2 keyboard is typically faster; see table 2 in <a href="https://doi.org/10.3758/BF03195452" rel="nofollow noreferrer">Shimizu 2002</a>; the worse standard deviation (SD) for response time seen there for a USB keyboard was around 10ms, which seems less than what you saw, so you're probably having additional issues. Only one PS/2 keyboard exceed 3ms SD (by a little).</p></li>
<li><p>the microcontroller (or possibly additional chips, <a href="https://datasheets.maximintegrated.com/en/ds/MAX6816-MAX6818.pdf" rel="nofollow noreferrer">e.g.</a>) in your keyboard implement something called debouncing, which smoothes out the presses and releases to compensate for the mechanical characteristics of the keyboard's switches. Without this, you'll garbage presses as <a href="http://en.community.dell.com/support-forums/laptop/f/3518/t/19328066" rel="nofollow noreferrer">in the case of some Dell laptops</a> some years ago. Debounce applies both to key presses and releases. For most keyboards debounce will not be configurable or very easy to find out what the settings are. </p></li>
</ul>

<p>From an engineering perspective, the first thing to ask how good of an accuracy do you actually need. And for a science perspective, what do you need this measurement for? Your question smacks of a big <a href="https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem">xy problem</a>.</p>

CC BY-SA 3.0
2017-12-28T04:11:17.530Z
2017-12-28T04:49:21.060Z
2017-12-28T04:49:21.060Z
got trolled too much this week
<p>I am using <a href="http://psychopy.org" rel="nofollow noreferrer">Psychopy</a> and this code for measuring keystroke duration as suggested in this answer for <a href="https://cogsci.stackexchange.com/questions/17454/freeware-for-measuring-keystroke-duration">my previous question</a>: </p>

<pre><code>from psychopy.iohub import launchHubServer 
io = launchHubServer()
keyboard = io.devices.keyboard
count = 0
keyinfo = ""
duration = 0
key = ""

io.clearEvents('all')

print 'begin'

while key not in ['escape', 'esc']:
    key_info = keyboard.waitForReleases()[0] 
    count += 1
    key = key_info.key
    keyinfo = key_info
    duration = key_info.duration
    key = key_info.key
    print 'round number:' , count
    print key_info
    print 'Press time was', key_info.duration
    print 'key', key_info.key
</code></pre>

<p>The first tests look promising, but I was wondering if there is a difference in accuracy between <code>iohub</code> or <code>pyglet</code> function in Psychopy? Or if there would even be a more accurate solution than using one of theses two? And how is the measurement of the keystroke duration. I've been searching for it but haven't found any data for windows. The only thing I found are <a href="https://groups.google.com/forum/m/#!topic/psychopy-dev/u3WyDfnIYBo" rel="nofollow noreferrer">these tests</a>
Has someone stumbled across a publication regarding the accuracy?</p>

<p>Moreover I don’t know how good RAM and CPU would need to be, right know I am using an ASUS notebook with Intel Core i7 2.6 GHz and 16GB RAM, (Bluetooth, Ethernet. Wi-Fi, Mobile and other kind of connections disabled; desktop visual effects disabled; antivirus, software updates, background programs, and other kind of asynchronous events sources disabled) and the last version of PsychoPy2.</p>

<p>I tried to do preliminary tests to measure the accuracy of my test and to measure the keystroke duration with Psychopy and my keyboard simulating device. Before renting more expensive equipment, I've tried to pretest the accuracy with a 60 fps camera recording the moment of touch and without touch to the device and measuring with Psychopy at the same, to be able to compare these to values to have the accuracy. The 60 fps means, that in between frames there are 16.67 ms. My cheap camera pretest showed, that most of the processing time would be a maximum of approximately 8ms, so that I think I will achieve even lower values with better recording equipment with a higher framerate. But approximately every tenth measurement I have 24-36 ms signal processing.</p>

<p>My questions summarized:</p>

<ol>
<li>Does anyone know, what the reason might be that approximately every tenth measurement has higher signal processing times?</li>
<li>How could I make the measurement of the keystroke duration more accurate?</li>
<li>Has anyone measured the keystroke duration accuracy of psychopys2 different functions (e.g <code>iohub</code> and <code>pyglet</code>) in windows himself or stumbled upon literature about this?</li>
</ol>

CC BY-SA 3.0
2017-09-09T11:03:20.940Z
This is a really good question and most welcome here! However, in case you do not immediately get answers here, I recommend you to ask a similar question (rephrased to focus more on the specific technology) on [Stack Overflow](https://stackoverflow.com/). If you get a response there, of course you could add your own answer here!
Steven Jeuris
CC BY-SA 3.0
2017-09-09T20:26:25.060Z
What exactly is iohub or pyglet? What is the accuracy of 16.67 ms? I mean what did you measure and how do you define accuracy? And you say *...the measured data is somewhere around 8 ms* - what data? What did you measure? The question is quite unclear at this stage. Also, there are many questions buried in the post. What is it you need to know? Please be clear and explicit.
AliceD
CC BY-SA 3.0
2017-09-09T20:47:19.527Z
I've tried to rephrase the question and make the information more clear. Thank you Steven Jeuris for the suggestion, that will be my second step!
Nathalie
CC BY-SA 3.0
2017-09-09T21:39:05.387Z
Thanks for this. So you are basically asking what the reason might be that *...[A]pproximately every tenth measurement I have 24-36 ms signal processing*  ?
AliceD
CC BY-SA 3.0
2017-09-10T04:33:07.627Z
Thank you for the suggestion, the questions are now summarized at the end.
Nathalie
CC BY-SA 3.0
2017-09-10T12:45:52.223Z
What is the relation between keystroke duration and frame rate? Further, the accuracy of keystroke duration is hard to quantify, as you need to know how long it was pressed for in reality versus what you are measuring back. So it's still hard to interpret your post. Further, it's important to know how frequent the lapses occur. I mean, if it is random the underlying issue will be different than when exactly one in ten is off. But most important is: what exactly did you test?
AliceD
CC BY-SA 3.0
2017-09-10T17:38:06.400Z
the camera with this framerate are just a way to test the measurement (but the camera is not so accurate because of the low framerate), to see if what I measure is correct I use the camera, because at the moment I don't have any other reliable method and because I dont know how long it was actually pressed, thats why I am recodring the pressing of the key with the camera and comparing it to the measured values with my device. And approximately every tenth key press, there are longer times measured than the camera records.
Nathalie
<measurement><methodology><psychophysics>
Accuracy of psychopy keystroke duration measurement
Nathalie
# Accuracy of psychopy keystroke duration measurement

(Asked by: username_0 on 2017-09-09)

I am using Psychopy and this code for measuring keystroke duration as suggested in this answer for my previous question: 
from psychopy.iohub import launchHubServer 
io = launchHubServer()
keyboard = io.devices.keyboard
count = 0
keyinfo = ""
duration = 0
key = ""

io.clearEvents('all')

print 'begin'

while key not in ['escape', 'esc']:
    key_info = keyboard.waitForReleases()[0] 
    count += 1
    key = key_info.key
    keyinfo = key_info
    duration = key_info.duration
    key = key_info.key
    print 'round number:' , count
    print key_info
    print 'Press time was', key_info.duration
    print 'key', key_info.key

The first tests look promising, but I was wondering if there is a difference in accuracy between iohub or pyglet function in Psychopy? Or if there would even be a more accurate solution than using one of theses two? And how is the measurement of the keystroke duration. I've been searching for it but haven't found any data for windows. The only thing I found are these tests
Has someone stumbled across a publication regarding the accuracy?
Moreover I don’t know how good RAM and CPU would need to be, right know I am using an ASUS notebook with Intel Core i7 2.6 GHz and 16GB RAM, (Bluetooth, Ethernet. Wi-Fi, Mobile and other kind of connections disabled; desktop visual effects disabled; antivirus, software updates, background programs, and other kind of asynchronous events sources disabled) and the last version of PsychoPy2.
I tried to do preliminary tests to measure the accuracy of my test and to measure the keystroke duration with Psychopy and my keyboard simulating device. Before renting more expensive equipment, I've tried to pretest the accuracy with a 60 fps camera recording the moment of touch and without touch to the device and measuring with Psychopy at the same, to be able to compare these to values to have the accuracy. The 60 fps means, that in between frames there are 16.67 ms. My cheap camera pretest showed, that most of the processing time would be a maximum of approximately 8ms, so that I think I will achieve even lower values with better recording equipment with a higher framerate. But approximately every tenth measurement I have 24-36 ms signal processing.
My questions summarized:

Does anyone know, what the reason might be that approximately every tenth measurement has higher signal processing times?
How could I make the measurement of the keystroke duration more accurate?
Has anyone measured the keystroke duration accuracy of psychopys2 different functions (e.g iohub and pyglet) in windows himself or stumbled upon literature about this?

**Tags:** measurement, methodology, psychophysics

> **Comments:**
> **username_1**: This is a really good question and most welcome here! However, in case you do not immediately get answers here, I recommend you to ask a similar question (rephrased to focus more on the specific technology) on [Stack Overflow](https://stackoverflow.com/). If you get a response there, of course you could add your own answer here!  
> **username_2**: What exactly is iohub or pyglet? What is the accuracy of 16.67 ms? I mean what did you measure and how do you define accuracy? And you say *...the measured data is somewhere around 8 ms* - what data? What did you measure? The question is quite unclear at this stage. Also, there are many questions buried in the post. What is it you need to know? Please be clear and explicit.  
> **username_0**: I've tried to rephrase the question and make the information more clear. Thank you username_1 for the suggestion, that will be my second step!  
> **username_2**: Thanks for this. So you are basically asking what the reason might be that *...[A]pproximately every tenth measurement I have 24-36 ms signal processing*  ?  
> **username_0**: Thank you for the suggestion, the questions are now summarized at the end.  
> **username_2**: What is the relation between keystroke duration and frame rate? Further, the accuracy of keystroke duration is hard to quantify, as you need to know how long it was pressed for in reality versus what you are measuring back. So it's still hard to interpret your post. Further, it's important to know how frequent the lapses occur. I mean, if it is random the underlying issue will be different than when exactly one in ten is off. But most important is: what exactly did you test?  
> **username_0**: the camera with this framerate are just a way to test the measurement (but the camera is not so accurate because of the low framerate), to see if what I measure is correct I use the camera, because at the moment I don't have any other reliable method and because I dont know how long it was actually pressed, thats why I am recodring the pressing of the key with the camera and comparing it to the measured values with my device. And approximately every tenth key press, there are longer times measured than the camera records.  

---

## 2 answers

### Answer by username_0 on 2017-09-15. Score: 2

In the [Psychopy forum](https://discourse.psychopy.org/t/accuracy-of-psychopy-keystroke-duration-measurement/3049/3) I have received some advice:

* One would be to change the code: take out the print command, no
  redefining of variables
* USB, USB device, the python and system
  libraries should be the newest and fastest, if possible, because
  these could have an influence on the accuracy.
* And that Psychtoolbox forum could have the solution, but so far I haven't found it.

> **Comments:**
> **username_3**: This is sadly a total absence of response. Referring to external sources without checking is not a good contribution to this site.  
> **username_0**: This is not my answer, as stated, and therefore also not checked as solving the problem. Following  username_1 advice I've asked the question in a different forum and just summarized the answer I've received here, to share this bit of information or to trigger some ideas. Perhaps a comment would have been better, but the question above had so many comments, that I thought it would be too confusing  

---

### Answer by username_4 on 2017-12-28. Score: 1

This frankly should have been asked on SO as it's 99% a programming question. There are a lot of things that can contribute to the "spiky" accuracy you've seen:

* language/runtime: Python is hardly a good idea for anything requiring real-time accuracy. It's an interpreted language with [garbage collection](https://rushter.com/blog/python-garbage-collector/). The [re]assignments in your loop will (eventually) trigger that. I've written once a Python GTK interface for a real-time device, but the code controlling the device was entirely in C, and nothing time-critical was done on Python side.
* Ultimately, the C runtime dynamic memory allocations (which [Python eventually relies on](https://dmalcolm.fedorapeople.org/presentations/PyCon-US-2011/MemoryUsage.pdf)) are also [not predictable in terms of timing](http://mil-embedded.com/articles/justifiably-apis-militaryaerospace-embedded-code/), although this is probably not what you're seeing. For real-time software dynamic allocation is generally avoided.
* operating system: not running at high enough process/thread priority so you get occasionally preempted by other processes/threads.
* hardware/driver: USB sends packetized data at limited poll rate; [8ms (125 Hz)](https://books.google.com/books?id=YCslDwAAQBAJ&pg=PA53) being the default for most input devices. Unless you can change the default, a PS/2 keyboard is typically faster; see table 2 in [Shimizu 2002](https://doi.org/10.3758/BF03195452); the worse standard deviation (SD) for response time seen there for a USB keyboard was around 10ms, which seems less than what you saw, so you're probably having additional issues. Only one PS/2 keyboard exceed 3ms SD (by a little).
* the microcontroller (or possibly additional chips, [e.g.](https://datasheets.maximintegrated.com/en/ds/MAX6816-MAX6818.pdf)) in your keyboard implement something called debouncing, which smoothes out the presses and releases to compensate for the mechanical characteristics of the keyboard's switches. Without this, you'll garbage presses as [in the case of some Dell laptops](http://en.community.dell.com/support-forums/laptop/f/3518/t/19328066) some years ago. Debounce applies both to key presses and releases. For most keyboards debounce will not be configurable or very easy to find out what the settings are.

From an engineering perspective, the first thing to ask how good of an accuracy do you actually need. And for a science perspective, what do you need this measurement for? Your question smacks of a big [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem).


---

