IO Loaders
The slopit.io module provides loaders for various data formats. Loaders convert platform-specific formats into the standardized SlopitSession schema.
High-Level Functions
load_session
Load a single session from a file with automatic format detection.
slopit.io.load_session
load_session(path: str | Path) -> SlopitSession
Load a single session from a file.
Automatically detects the file format and uses the appropriate loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the session file. |
required |
Returns:
| Type | Description |
|---|---|
SlopitSession
|
The loaded session data. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |
ValueError
|
If the file format cannot be determined or is invalid. |
Examples:
>>> session = load_session("data/participant_001.json")
>>> print(f"Session {session.session_id} has {len(session.trials)} trials")
Example
from slopit import load_session
# Load native slopit format
session = load_session("data/participant_001.json")
# Load JATOS format
session = load_session("jatos_results/study_result_123.txt")
load_sessions
Load multiple sessions from a directory.
slopit.io.load_sessions
load_sessions(path: str | Path, pattern: str = '*') -> list[SlopitSession]
Load multiple sessions from a directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to directory containing session files. |
required |
pattern
|
str
|
Glob pattern for file matching. |
'*'
|
Returns:
| Type | Description |
|---|---|
list[SlopitSession]
|
List of loaded sessions. |
Examples:
>>> sessions = load_sessions("data/")
>>> print(f"Loaded {len(sessions)} sessions")
Example
from slopit import load_sessions
# Load all sessions from a directory
sessions = load_sessions("data/")
print(f"Loaded {len(sessions)} sessions")
# Filter by pattern
sessions = load_sessions("data/", pattern="*.json")
Base Loader Class
BaseLoader
Abstract base class that all format-specific loaders inherit from.
slopit.io.base.BaseLoader
Bases: ABC
Abstract base class for data loaders.
Subclasses implement loading logic for specific data formats (JATOS, Pavlovia, Gorilla, etc.).
Source code in src/slopit/io/base.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
load
abstractmethod
load(path: Path) -> SlopitSession
Load a single session from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the data file. |
required |
Returns:
| Type | Description |
|---|---|
SlopitSession
|
The loaded session data. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |
ValueError
|
If the file format is invalid. |
Source code in src/slopit/io/base.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
load_many
abstractmethod
load_many(path: Path, pattern: str = '*') -> Iterator[SlopitSession]
Load multiple sessions from a directory or archive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to directory or archive file. |
required |
pattern
|
str
|
Glob pattern for file matching. |
'*'
|
Yields:
| Type | Description |
|---|---|
SlopitSession
|
Session data for each matching file. |
Source code in src/slopit/io/base.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
can_load
abstractmethod
classmethod
can_load(path: Path) -> bool
Check if this loader can handle the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if this loader can handle the format. |
Source code in src/slopit/io/base.py
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
Native Format Loader
NativeLoader
Loader for native slopit JSON format. The native format is a JSON file that directly contains a SlopitSession object.
slopit.io.native.NativeLoader
Bases: BaseLoader
Loader for native slopit JSON format.
The native format is a JSON file that directly contains a SlopitSession object with schemaVersion field.
Examples:
>>> loader = NativeLoader()
>>> session = loader.load(Path("data/session.json"))
>>> print(f"Loaded session {session.session_id}")
Source code in src/slopit/io/native.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | |
load
load(path: Path) -> SlopitSession
Load a native slopit JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the JSON file. |
required |
Returns:
| Type | Description |
|---|---|
SlopitSession
|
The loaded session data. |
Source code in src/slopit/io/native.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
load_many
load_many(path: Path, pattern: str = '*.json') -> Iterator[SlopitSession]
Load multiple native JSON files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to directory containing JSON files. |
required |
pattern
|
str
|
Glob pattern for file matching. |
'*.json'
|
Yields:
| Type | Description |
|---|---|
SlopitSession
|
Session data for each matching file. |
Source code in src/slopit/io/native.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
can_load
classmethod
can_load(path: Path) -> bool
Check if path appears to be native slopit format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the path appears to be native format. |
Source code in src/slopit/io/native.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
Native Format Structure
{
"schemaVersion": "1.0",
"sessionId": "abc123",
"participantId": "P001",
"studyId": "study_001",
"platform": {
"name": "jspsych",
"version": "7.3.0",
"adapterVersion": "0.1.0"
},
"environment": {
"userAgent": "Mozilla/5.0...",
"screenResolution": [1920, 1080],
"viewportSize": [1200, 800],
"devicePixelRatio": 2.0,
"timezone": "America/New_York",
"language": "en-US",
"touchCapable": false
},
"timing": {
"startTime": 1705000000000,
"endTime": 1705000600000,
"duration": 600000
},
"trials": [
{
"trialId": "trial-0",
"trialIndex": 0,
"trialType": "survey-text",
"startTime": 1705000000000,
"endTime": 1705000060000,
"rt": 60000,
"stimulus": {...},
"response": {...},
"behavioral": {...}
}
],
"globalEvents": {
"focus": [],
"errors": []
}
}
Example
from slopit.io import NativeLoader
from pathlib import Path
loader = NativeLoader()
# Check if file is native format
if NativeLoader.can_load(Path("data/session.json")):
session = loader.load(Path("data/session.json"))
# Load multiple files
for session in loader.load_many(Path("data/"), pattern="*.json"):
print(f"Loaded {session.session_id}")
JATOS Loader
JATOSLoader
Loader for data exported from JATOS (Just Another Tool for Online Studies). JATOS exports data as JSON arrays or newline-delimited JSON.
slopit.io.jatos.JATOSLoader
Bases: BaseLoader
Loader for JATOS export format.
JATOS exports data as JSON, either as a single array of trials or as newline-delimited JSON (one trial per line). This loader handles both formats.
Examples:
>>> loader = JATOSLoader()
>>> session = loader.load(Path("jatos_results/study_result_123.txt"))
>>> print(f"Loaded {len(session.trials)} trials")
Source code in src/slopit/io/jatos.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | |
load
load(path: Path) -> SlopitSession
Load a JATOS result file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the JATOS result file (.txt or .json). |
required |
Returns:
| Type | Description |
|---|---|
SlopitSession
|
Converted session data. |
Source code in src/slopit/io/jatos.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
load_result
load_result(trials: list[dict[str, JsonValue]], result_id: str = 'unknown') -> SlopitSession
Load a session from raw trial data.
This method is useful for converting JATOS API results directly without writing to a file first.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trials
|
list[dict[str, JsonValue]]
|
List of raw trial dictionaries from JATOS. |
required |
result_id
|
str
|
Optional identifier for the result (for logging/debugging). |
'unknown'
|
Returns:
| Type | Description |
|---|---|
SlopitSession
|
Converted session data. |
Examples:
>>> loader = JATOSLoader()
>>> trials = [{"trial_type": "survey", "response": "..."}]
>>> session = loader.load_result(trials, result_id="api-123")
Source code in src/slopit/io/jatos.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
load_many
load_many(path: Path, pattern: str = '*.txt') -> Iterator[SlopitSession]
Load multiple JATOS result files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to directory containing result files. |
required |
pattern
|
str
|
Glob pattern for file matching. |
'*.txt'
|
Yields:
| Type | Description |
|---|---|
SlopitSession
|
Session data for each file. |
Source code in src/slopit/io/jatos.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
can_load
classmethod
can_load(path: Path) -> bool
Check if path appears to be JATOS format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the path appears to be JATOS format. |
Source code in src/slopit/io/jatos.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
JATOS Format
JATOS exports come in two formats:
Array format (single JSON array):
[
{"trial_type": "html-keyboard-response", "rt": 1234, ...},
{"trial_type": "survey-text", "response": "...", "slopit": {...}, ...}
]
Newline-delimited format (one trial per line):
{"trial_type": "html-keyboard-response", "rt": 1234, ...}
{"trial_type": "survey-text", "response": "...", "slopit": {...}, ...}
Extracted Fields
The JATOS loader extracts:
- participant_id: From
PROLIFIC_PID,workerId,participant_id, orsubject - study_id: From
STUDY_ID,study_id, orexperiment_id - environment: From standard jsPsych fields (
user_agent,screen_width, etc.) - behavioral data: From the
slopitfield added by slopit adapters
Example
from slopit.io import JATOSLoader
from pathlib import Path
loader = JATOSLoader()
# Load JATOS result file
session = loader.load(Path("jatos_results/study_result_123.txt"))
print(f"Loaded {len(session.trials)} trials")
# Load all results from a directory
for session in loader.load_many(Path("jatos_results/")):
print(f"Session: {session.session_id}")
print(f" Participant: {session.participant_id}")
print(f" Trials: {len(session.trials)}")
Writing Custom Loaders
To support a new data format, subclass BaseLoader:
from pathlib import Path
from collections.abc import Iterator
from slopit.io.base import BaseLoader
from slopit.schemas import SlopitSession
class MyCustomLoader(BaseLoader):
"""Loader for my custom format."""
def load(self, path: Path) -> SlopitSession:
"""Load a single session."""
# Parse your format
raw_data = self._parse_file(path)
# Convert to SlopitSession
return SlopitSession(
schema_version="1.0",
session_id=raw_data["id"],
# ... map other fields
)
def load_many(self, path: Path, pattern: str = "*") -> Iterator[SlopitSession]:
"""Load multiple sessions."""
if path.is_file():
yield self.load(path)
return
for file_path in sorted(path.glob(pattern)):
if self._is_my_format(file_path):
yield self.load(file_path)
@classmethod
def can_load(cls, path: Path) -> bool:
"""Check if this loader can handle the path."""
if path.is_dir():
return any(cls._is_my_format(f) for f in path.glob("*"))
return cls._is_my_format(path)
@classmethod
def _is_my_format(cls, path: Path) -> bool:
"""Check if file is in my custom format."""
# Implement format detection
return path.suffix == ".myformat"
def _parse_file(self, path: Path) -> dict:
"""Parse file contents."""
# Implement parsing logic
pass
Registering Custom Loaders
Currently, custom loaders must be used directly:
loader = MyCustomLoader()
session = loader.load(Path("data/file.myformat"))
Future versions will support registering loaders for automatic format detection.