Keystroke Dynamics

Typing Speed

typing_speed(df=None, traces=None, per_traces=True)

Calculate the average typing speed in characters per minute (CPM).

Parameters:
  • df (pd.DataFrame) – DataFrame containing interaction data with ‘event_type’, ‘timestamp’, and ‘key’ columns.

  • traces (dict[str, list[pd.DataFrame]]) – optional Pre-extracted keystroke traces by session.

  • per_traces (bool) – optional Whether to calculate speed per trace. Default is True. if False, mind that the df must only contain typing data in order to obtain the correct CPM calculation.

Returns:

A dictionary with session IDs as keys and lists of typing speeds (CPM) per trace as values, or a float representing the typing speed if per_traces is False.

Return type:

dict (dict[list[float]] | float)

Practical Example

from pywib import typing_speed
# Pre-obtained data_framed
speed = typing_speed(data_frame, per_traces=True)

for session_id, speed_value in speed.items():
   print(f"Session ID: {session_id}")
   print(f"Typing Speed (CPM): {speed_value}")

Words Per Minute

words_per_minute(df=None, traces=None, per_traces=True, word_size=5)

Calculate the average typing speed in words per minute (WPM).

Parameters:
  • df (pd.DataFrame) – DataFrame containing interaction data with ‘event_type’, ‘timestamp’, and ‘key’ columns.

  • traces (dict[str, list[pd.DataFrame]]) – optional Pre-extracted keystroke traces by session.

  • per_traces (bool) – optional Whether to calculate speed per trace. Default is True. if False, mind that the df must only contain typing data in order to obtain the correct CPM calculation.

  • word_size (int?) – Word size, by default 5.

Returns:

A dictionary with session IDs as keys and lists of typing speeds (CPM) per trace as values, or a float representing the typing speed if per_traces is False.

Return type:

dict (dict[list[float]] | float)

Practical Example

from pywib import words_per_minute
# Pre-obtained data_framed
speed = words_per_minute(data_frame, per_traces=True)

for session_id, speed_value in speed.items():
   print(f"Session ID: {session_id}")
   print(f"Typing Speed (CPM): {speed_value}")

CPM stands for Characters Per Minute, and it is calculated by taking the total number of keystrokes (characters typed) and dividing it by the total time taken (in minutes) to type those characters. The method can be either run with either per_traces=True or per_traces=False, the first one segments the data by groups of keystroke events, while the second one computes the speed for the entire DataFrame without pauses into consideration. This is important to consider for the specific given dataset, if the data contains anything else than keystroke data, then per_traces=True should be used to avoid incorrect CPM calculations. Whereas a dataset of consecutive keystroke events can be processed with per_traces=False to obtain a single CPM.

Metrics for typing speed

This method returns a collection of metrics for each user, in the form of a dict[str, dict[str, number]]. The first string corresponds to the sessionId, whereas the inner dictionary contains the following keys: - average_typing_speed - total_characters - total_time_seconds - avg_keydown_to_keyup_duration

Warning

As of version 2.1.0. avg_keydown_to_keyup_duration is experimenting a computational error and always returns .NaN

typing_speed_metrics(df=None, traces=None)

Calculate typing speed metrics including average CPM, total characters typed, and total time spent typing.

This metrics include average typing speed (average_typing_speed) in CPM, total characters typed (total_characters), and total time spent typing (total_time_seconds) in seconds.

The average keydown to keyup duration (avg_keydown_to_keyup_duration) is the average duration of a keystroke (from keydown to keyup) in milliseconds.

Parameters:
  • df (Optional[DataFrame]) – pd.DataFrame DataFrame containing interaction data with ‘event_type’, ‘timestamp’, and ‘key’ columns.

  • traces (Optional[dict[str, list[DataFrame]]]) – dict[str, list[pd.DataFrame]], optional Pre-extracted keystroke traces by session.

  • per_trace – bool, optional Whether to calculate metrics per trace. Default is True.

Returns:

A dictionary with session IDs as keys and their corresponding typing speed metrics as values.

Return type:

dict

Typing Durations

typing_durations(df=None, traces=None, per_traces=True, single=False)

Calculate the durations of individual keystrokes.

Parameters:
  • df (pd.DataFrame) – DataFrame containing interaction data with ‘event_type’, ‘timestamp’, and ‘key’ columns.

  • traces (dict[str, list[pd.DataFrame]]) – optional Pre-extracted keystroke traces by session.

  • per_traces (bool) – True by default, whether to calculate durations per trace. Default is True.

  • single (bool) – False by default, wether to compute durations fully (all the time the user is typing) or for every single keystroke (value for every single key press and release combination)

  • Returns –

    Return type depends on per_traces and single:

    • per_traces=False, single=True -> list[float]:

      Keystroke durations in milliseconds.

    • per_traces=False, single=False -> float:

      Total typing duration in milliseconds.

    • per_traces=True, single=True -> dict[str, list[float]]:

      Session IDs mapped to per-trace keystroke durations.

    • per_traces=True, single=False -> dict[str, float]:

      Session IDs mapped to total duration per trace.

Return type:

list | float | dict[str, list[DataFrame]]

Practical Example

from pywib import typing_durations

durations = typing_durations(data_frame)
for duration in durations:
    print(f"Typing Duration: {duration} seconds")

Backspace Usage

backspace_usage(df=None, traces=None, per_trace=True)

Calculate the backspace usage rate (backspaces per 100 characters typed) for each session.

Parameters:
  • df (pd.DataFrame) – DataFrame containing interaction data with ‘event_type’, ‘timestamp’, and ‘key’ columns.

  • traces (dict[str, list[pd.DataFrame]]) – optional Pre-extracted keystroke traces by session.

  • per_trace (bool) – optional Whether to calculate usage per trace. Default is True.

Returns:

A dictionary with session IDs as keys and their corresponding backspace counts as values.

Return type:

dict

Practical Example

from pywib import backspace_usage

 backspace_counts = backspace_usage(data_frame)

 for session_id, count in backspace_counts.items():
      print(f"Session ID: {session_id}")
      print(f"Backspace Usage: {count}")