API Referencesð
This is the reference for classes (CamelCase
names) and functions
(underscore_case
names) of stimuli
grouped thematically.
Audioð
The audio module provides classes for generating standard audio stimuli and for loading existing files.
Sound generationð
Standard sounds can be generated like noise, amplitude modulated sounds or pure tones.
|
Colored noise stimulus. |
|
Amplitude modulated sound. |
|
Pure tone stimulus at a given frequency. |
Example usage:
from stimuli.audio import Noise
sound = Noise(color="pink", volume=50, duration=1)
sound.play(when=0.2) # schedule in 200 ms
Sound filesð
For more complex stimuli, the sounds can be loaded from files.
|
Auditory stimulus loaded from a file. |
Example usage:
from stimuli.audio import Sound
sound = Sound("path/to/sound.wav")
sound.play(when=0.2) # schedule in 200 ms
Backendð
The audio module uses the sounddevice library with a callback function to play the sounds.
|
Sounddevice backend for audio playback. |
Triggerð
To mark events in time during an experiment, you can use triggers. For instance, you can
mark the onset of a sound. The triggers defined in stimuli
uses one byte to encode
the trigger value, i.e. the trigger value must be between 1 and 255 (except for the
LSLTrigger
which uses a trigger value between 1 and 127).
Mock trigger class. |
|
|
Trigger sending values on an LSL outlet. |
|
Trigger using a parallel port (also called LPT port). |
Example usage:
from stimuli.trigger import ParallelPortTrigger
trigger = ParallelPortTrigger("/dev/parport0")
trigger.signal(1) # send trigger value 1
Arduino to Parallel Port converterð
Parallel ports are less and less common on computers, and are not supported by macOS. Instead, the Fondation Campus Biotech Geneva developed a USB to parallel port converter using an arduino.
The details can be found on this repository: https://github.com/fcbg-platforms/arduino-trigger
This converter can be used with the ParallelPortTrigger
class
by specifying the path to the serial port connected to the arduino or by using the
str
"arduino"
for automatic detection.
Timeð
The python time
module is not accurate enough for precise timing in experiments. The
stimuli
package provides a more accurate clock, the Clock
class, which uses time.perf_counter()
or time.monotonic_ns()
depending on
which has the highest resolution. The sleeping function stimuli.time.sleep()
replaces the standard time.sleep()
function and make use of the clock from
stimuli
to achieve a better precision.
|
Clock which keeps track of time in nanoseconds. |
|
High precision sleep function. |
Sleep performance (linux):
%timeit time.sleep(0.0005)
556 Ξs ¹ 131 ns per loop (mean ¹ std. dev. of 7 runs, 1,000 loops each)
%timeit stimuli.time.sleep(0.0005)
501 Ξs ¹ 77.5 ns per loop (mean ¹ std. dev. of 7 runs, 1,000 loops each)
Example usage:
from stimuli.audio import Tone
from stimuli.trigger import ParallelPortTrigger
from stimuli.time import sleep
sound = Tone(frequency=1000, volume=25, duratin=0.5)
trigger = ParallelPortTrigger("/dev/parport0")
sound.play(when=0.2)
sleep(0.2)
trigger.signal(1)
Regardless of the function used, stimuli
is still limited by the accuracy of the
on-board computer clock. If you have access to a more accurate clock, you can subclass
the abstract class BaseClock
to implement your own clock.
Base class for high precision clocks. |
Many objects and functions in stimuli
have a clock
argument which supports any
BaseClock
subclass.
Utilitiesð
Logging utilities are available to interact with stimuli
logger.
Add a file handler to the logger. |
|
Set the log level for the logger. |
Development utilities are available to help debug a setup.
Print the system information for debugging. |