experiment_server 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- experiment_server-0.3.0/LICENSE.md +21 -0
- experiment_server-0.3.0/PKG-INFO +295 -0
- experiment_server-0.3.0/README.md +260 -0
- experiment_server-0.3.0/experiment_server/__init__.py +31 -0
- experiment_server-0.3.0/experiment_server/_api.py +200 -0
- experiment_server-0.3.0/experiment_server/_client.py +76 -0
- experiment_server-0.3.0/experiment_server/_participant_ordering.py +127 -0
- experiment_server-0.3.0/experiment_server/_process_config.py +348 -0
- experiment_server-0.3.0/experiment_server/_server.py +403 -0
- experiment_server-0.3.0/experiment_server/cli.py +72 -0
- experiment_server-0.3.0/experiment_server/static/css/bootstrap-5.2.3.min.css +7 -0
- experiment_server-0.3.0/experiment_server/static/index.html +217 -0
- experiment_server-0.3.0/experiment_server/static/initconfig.html +143 -0
- experiment_server-0.3.0/experiment_server/static/js/alpinejs3.min.js +5 -0
- experiment_server-0.3.0/experiment_server/static/js/bootstrap-4.5.0.min.js +7 -0
- experiment_server-0.3.0/experiment_server/static/js/fontawesome-1e694dd391.js +2 -0
- experiment_server-0.3.0/experiment_server/static/js/htmx-1.9.10.js +1 -0
- experiment_server-0.3.0/experiment_server/static/js/jquery-3.5.1.min.js +2 -0
- experiment_server-0.3.0/experiment_server/static/js/popper-1.16.0.min.js +5 -0
- experiment_server-0.3.0/experiment_server/static/js/sweetalert2-11.js +6 -0
- experiment_server-0.3.0/experiment_server/utils.py +99 -0
- experiment_server-0.3.0/pyproject.toml +67 -0
- experiment_server-0.3.0/sample_config.toml +57 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
**The MIT License (MIT)**
|
|
2
|
+
|
|
3
|
+
Copyright © 2020, Ahmed Shariff
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: experiment-server
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Server for experiments to get configuarations from
|
|
5
|
+
License: MIT
|
|
6
|
+
Keywords: experiment,study-design
|
|
7
|
+
Author: Ahmed Shariff
|
|
8
|
+
Author-email: shariff.mfa@outlook.com
|
|
9
|
+
Requires-Python: >=3.8
|
|
10
|
+
Classifier: Development Status :: 1 - Planning
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Natural Language :: English
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Requires-Dist: Jinja2 (>=3.1.2)
|
|
21
|
+
Requires-Dist: asyncio (>=3.4.3)
|
|
22
|
+
Requires-Dist: click (>=6.0)
|
|
23
|
+
Requires-Dist: click-aliases (>=1.0.4)
|
|
24
|
+
Requires-Dist: easydict (>=1)
|
|
25
|
+
Requires-Dist: importlib-metadata (>=6.8.0)
|
|
26
|
+
Requires-Dist: loguru (>=0.5,<0.6)
|
|
27
|
+
Requires-Dist: pandas (>=1)
|
|
28
|
+
Requires-Dist: requests (>=2.25.1)
|
|
29
|
+
Requires-Dist: tabulate (>=0.8.9)
|
|
30
|
+
Requires-Dist: toml (>=0.10.2)
|
|
31
|
+
Requires-Dist: tornado (>=6.2)
|
|
32
|
+
Requires-Dist: watchdog (>=2.1.9)
|
|
33
|
+
Description-Content-Type: text/markdown
|
|
34
|
+
|
|
35
|
+
# Overview
|
|
36
|
+
|
|
37
|
+
This is a Python application that allows you to create/maintain/manage study configurations away from your implementations. `experiment-server` has several different interfaces (see below) to allow using it in a range of different scenarios. I've used it with Python, js and [Unity projects](https://github.com/ahmed-shariff/experiment_server/wiki/Using-with-Unity). See the [wiki](https://github.com/ahmed-shariff/experiment_server/wiki) for examples.
|
|
38
|
+
|
|
39
|
+
# Content
|
|
40
|
+
|
|
41
|
+
- [Overview](#overview)
|
|
42
|
+
- [Installation](#installation)
|
|
43
|
+
- [Usage](#usage)
|
|
44
|
+
- [Configuration of an experiment](#configuration-of-an-experiment)
|
|
45
|
+
- [Verify config](#verify-config)
|
|
46
|
+
- [Loading experiment through server](#loading-experiment-through-server)
|
|
47
|
+
- [Loading experiment through API](#loading-experiment-through-api)
|
|
48
|
+
- [Generate expanded config files](#generate-expanded-config-files)
|
|
49
|
+
- [Function calls in config](#function-calls-in-config)
|
|
50
|
+
- [Supported functions](#supported-functions)
|
|
51
|
+
- [Example function calls](#example-function-calls)
|
|
52
|
+
|
|
53
|
+
# Installation
|
|
54
|
+
|
|
55
|
+
Install it directly into an activated virtual environment:
|
|
56
|
+
|
|
57
|
+
```text
|
|
58
|
+
$ pip install experiment-server
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
or add it to your [Poetry](https://poetry.eustace.io/) project:
|
|
62
|
+
|
|
63
|
+
```text
|
|
64
|
+
$ poetry add experiment-server
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
# Usage
|
|
68
|
+
## Configuration of an experiment
|
|
69
|
+
The configuration is defined in a [toml](https://toml.io/en/) file.
|
|
70
|
+
|
|
71
|
+
A config file can be generated as follows
|
|
72
|
+
```sh
|
|
73
|
+
$ experiment-server new-config-file new_config.toml
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
See example `.toml` below for how the configuration can be defined.
|
|
77
|
+
|
|
78
|
+
```toml
|
|
79
|
+
# The `configuration` table contains the settings of the study/experiment itself
|
|
80
|
+
[configuration]
|
|
81
|
+
# The `order` is an array of block names or an array of array of block names.
|
|
82
|
+
order = [["conditionA", "conditionB", "conditionA", "conditionB"]]
|
|
83
|
+
# The `groups` and `within_groups` are optional keys that allows you to define how the
|
|
84
|
+
# conditions specified in `order` will be managed. `groups` would dictate how the top
|
|
85
|
+
# level array of `order` will be handled. `within_groups` would dictate how the conditions
|
|
86
|
+
# in the nested arrays (if specified) would be managed. These keys can have one
|
|
87
|
+
# of the following values.
|
|
88
|
+
# - "latin_square": Apply latin square to balance the values.
|
|
89
|
+
# - "randomize": For each participant randomize the order of the values in the array.
|
|
90
|
+
# - "as_is": Use the order of the values as specified.
|
|
91
|
+
# When not specified, the default value is "as_is" for both keys.
|
|
92
|
+
groups = "latin_square"
|
|
93
|
+
within_groups= "randomize"
|
|
94
|
+
# The random seed to use for any randomization. Default seed is 0. The seed will be
|
|
95
|
+
# the value of random_seed + participant_index
|
|
96
|
+
random_seed = 0
|
|
97
|
+
|
|
98
|
+
# The subtable `variabels` are values that can be used anywhere when defining the blocks.
|
|
99
|
+
# Any variable can be used by appending "$" before the variable name in the blocks. See
|
|
100
|
+
# below for an exmaple of how variables can be used
|
|
101
|
+
[configuration.variables]
|
|
102
|
+
TRIALS_PER_ITEM = 3
|
|
103
|
+
|
|
104
|
+
# Blocks are defined as an array of tables. Each block must contain `name` and the
|
|
105
|
+
# subtable `config`. Optionally, a block can also specify `extends`, whish is a `name` of
|
|
106
|
+
# another block. See below for more explanation on how `extends` works
|
|
107
|
+
|
|
108
|
+
# Block: Condition A
|
|
109
|
+
[[blocks]]
|
|
110
|
+
name = "conditionA"
|
|
111
|
+
|
|
112
|
+
# The `config` subtable can have any key-values. Note that `name` and `participant_index`
|
|
113
|
+
# will be added to the `config` when this file is being processed. Hence, those keys
|
|
114
|
+
# will be overwritten if used in this subtable.
|
|
115
|
+
[blocks.config]
|
|
116
|
+
trialsPerItem = "$TRIALS_PER_ITEM"
|
|
117
|
+
param1 = 1
|
|
118
|
+
# The value can also be a function call. A function call is represented as a table
|
|
119
|
+
# The following function call will be replaced with a call to
|
|
120
|
+
# [random.choices](https://docs.python.org/3/library/random.html#random.choices)
|
|
121
|
+
# See `# Function calls` in README for more information.
|
|
122
|
+
param2 = { function_name = "choices", args = { population = [1 , 2 , 3 ], k = 2}}
|
|
123
|
+
param3 = { function_name = "choices", args = [[1 , 2 , 3 ]], params = { unique = true } }
|
|
124
|
+
|
|
125
|
+
# Block: Condition B
|
|
126
|
+
[[blocks]]
|
|
127
|
+
name = "conditionB"
|
|
128
|
+
extends = "conditionA"
|
|
129
|
+
|
|
130
|
+
# Since "conditionB" is extending "conditionA", the keys in the `config` subtable of
|
|
131
|
+
# the block "conditionA" not defined in the `config` subtable of "conditionB" will be copied
|
|
132
|
+
# to the `config` subtable of "conditionB". In this example, `param1`, `param2` and
|
|
133
|
+
# `trialsPerItem` will be copied over here.
|
|
134
|
+
[blocks.config]
|
|
135
|
+
param3 = [2]
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
See [toml spec](https://toml.io/en/v1.0.0) for more information on the format of a toml file.
|
|
139
|
+
|
|
140
|
+
The above config file, after being processed, would result in the following list of blocks for participant number 1:
|
|
141
|
+
```json
|
|
142
|
+
[
|
|
143
|
+
{
|
|
144
|
+
"name": "conditionB",
|
|
145
|
+
"extends": "conditionA",
|
|
146
|
+
"config": {
|
|
147
|
+
"param3": [
|
|
148
|
+
2
|
|
149
|
+
],
|
|
150
|
+
"trialsPerItem": 3,
|
|
151
|
+
"param1": 1,
|
|
152
|
+
"param2": [
|
|
153
|
+
1,
|
|
154
|
+
2
|
|
155
|
+
],
|
|
156
|
+
"participant_index": 1,
|
|
157
|
+
"name": "conditionB",
|
|
158
|
+
"block_id": 0
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
"name": "conditionA",
|
|
163
|
+
"config": {
|
|
164
|
+
"trialsPerItem": 3,
|
|
165
|
+
"param1": 1,
|
|
166
|
+
"param2": [
|
|
167
|
+
2,
|
|
168
|
+
2
|
|
169
|
+
],
|
|
170
|
+
"param3": [
|
|
171
|
+
3
|
|
172
|
+
],
|
|
173
|
+
"participant_index": 1,
|
|
174
|
+
"name": "conditionA",
|
|
175
|
+
"block_id": 1
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
"name": "conditionA",
|
|
180
|
+
"config": {
|
|
181
|
+
"trialsPerItem": 3,
|
|
182
|
+
"param1": 1,
|
|
183
|
+
"param2": [
|
|
184
|
+
1,
|
|
185
|
+
1
|
|
186
|
+
],
|
|
187
|
+
"param3": [
|
|
188
|
+
2
|
|
189
|
+
],
|
|
190
|
+
"participant_index": 1,
|
|
191
|
+
"name": "conditionA",
|
|
192
|
+
"block_id": 2
|
|
193
|
+
}
|
|
194
|
+
},
|
|
195
|
+
{
|
|
196
|
+
"name": "conditionB",
|
|
197
|
+
"extends": "conditionA",
|
|
198
|
+
"config": {
|
|
199
|
+
"param3": [
|
|
200
|
+
2
|
|
201
|
+
],
|
|
202
|
+
"trialsPerItem": 3,
|
|
203
|
+
"param1": 1,
|
|
204
|
+
"param2": [
|
|
205
|
+
3,
|
|
206
|
+
1
|
|
207
|
+
],
|
|
208
|
+
"participant_index": 1,
|
|
209
|
+
"name": "conditionB",
|
|
210
|
+
"block_id": 3
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
]
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## Verify config
|
|
217
|
+
A config file can be validated by running:
|
|
218
|
+
```sh
|
|
219
|
+
$ experiment-server verify-config-file sample_config.toml
|
|
220
|
+
```
|
|
221
|
+
This will show how the expanded config looks like for the first 5 participants.
|
|
222
|
+
|
|
223
|
+
## Loading experiment through server
|
|
224
|
+
After installation, the server can used as:
|
|
225
|
+
|
|
226
|
+
```sh
|
|
227
|
+
$ experiment-server run sample_config.toml
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
See more options with `--help`
|
|
231
|
+
|
|
232
|
+
The server exposes the following REST API:
|
|
233
|
+
- [GET] `/api/blocks-count` - Return the number of blocks in the configuration loaded.
|
|
234
|
+
- [GET] `/api/block-id` / `/api/block-id/:participant-id` - Returns the current block-id. If `participant-id` is provided, the blcok-id of the participant will be returned, if not the default participant's block-id will be returned. Note that the block-id is 0 indexed. i.e., the first block's block-id is 0.
|
|
235
|
+
- [GET] `/api/active` / `/api/active/:participant-id` - Returns the status for `participant-id`, if `participant-id` is not provided, will return the status of the default participant. Will be `false` if the participant was just initialized or the participant has gone through all blocks. To initialize the participant's status (or move to a given block), use the `move-to-next` or `move-to-block` endpoints.
|
|
236
|
+
- [GET] `/api/config` / `api/active/:participant-id` - Return the config for `participant-id`, if `participant-id` is not provided, will return the config for the default participant.
|
|
237
|
+
- [GET] `/api/summary-data` / `/api/summary-data/:participant-id` - Returns the summary of the configs for `participant-id`, if `participant-id` is not provided, returns the summary of the configs for the default participant. Currently, the summary is a JSON with the following keys
|
|
238
|
+
- "participant_index"
|
|
239
|
+
- "config_length"
|
|
240
|
+
- [GET] `/api/all-configs` / `/api/all-configs/:participant-id` - Returns all the configs as a list for the `participant-id`, if `participant-id` is not provided, returns the configs for the default participant. This is akin having the results of the `config` endpoint in one list.
|
|
241
|
+
- [GET] `/api/status-string` / `/api/status-string/:participant-id` - Returns status string for `participant-id`, if `participant-id` is not provided, returns statu string the default participant.
|
|
242
|
+
- [POST] `/api/move-to-next` / `/api/move-to-next/:participant-id` - Move `participant-id` to the next block, if `participant-id` is not provided, move the default participant to the next block. If the participant was not initialized (`active` is false), will make be marked as active (`active` will be set to true). If the block the participant was in was the last block, they will be marked as not active (`active` will be set to false).
|
|
243
|
+
- [POST] `/api/move-to-block/:block-id` / `/api/move-to-block/:participant-id/:block-id` - Move `participant-id` to the block number indicated by `block-id`, if `participant-id` is not provided, move the default participant to the block number indicated by `block-id`. If the participant was not initialized (`active` is false), will make be marked as active (`active` will be set to true). Will fail if the `block-id` is below 0 or above the length of the config.
|
|
244
|
+
- [POST] `/api/move-all-to-block/:block-id` - Move all active participants (`active` returns true) to the block number indicated by `block-id`.
|
|
245
|
+
- [POST] `/api/shutdown` - Shuts-down the server.
|
|
246
|
+
- [PUT] `/api/new-participant` - Adds a new participant and returns the new participant-id. The new participant-id will be the largest current participant-id +1.
|
|
247
|
+
- [PUT] `/api/add-participant/:participant-id` - Add a new participant with `participant-id`. If there is already a participant with the `participant-id`, this will fail.
|
|
248
|
+
|
|
249
|
+
For a Python application, `experiment_server.Client` can be used to access configs from the server. Also, the server can be launched programmatically using `experiment_server.server_process` which returns a [`Process`](https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Process) object.
|
|
250
|
+
|
|
251
|
+
**NOTE**: If the config file served is changed, the new config will be loaded, but the state of the participants will be maintained. i.e., the added participants and the block id they are at will not change. To move the block ids for all active participants, you would have to call the `move-all-to-block` endpoint.
|
|
252
|
+
|
|
253
|
+
The server also provides a simple web interface, which can be accessed at `/` or `/index`. This interface allows to manage and monitor the flow of the experiment:
|
|
254
|
+
|
|
255
|
+

|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
## Loading experiment through API
|
|
259
|
+
A configuration can be loaded and managed by importing `experiment_server.Experiment`.
|
|
260
|
+
|
|
261
|
+
## Generate expanded configs
|
|
262
|
+
A config file (i.e. `.toml` file), can be expanded to JSON with the following command
|
|
263
|
+
|
|
264
|
+
```sh
|
|
265
|
+
$ experiment-server generate-config-json sample_config.toml --participant-range 5
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
The above will generate the expanded configs for participant indices 1 to 5 as JSON output on stdout. This result can be written out to individual JSON files by setting the `--out-dir`/`-d` to a directory. See more options with `--help`
|
|
269
|
+
|
|
270
|
+
## Function calls in config
|
|
271
|
+
A function call in the config is represented by a table, with the following keys
|
|
272
|
+
- `function_name`: This should be one of the names in the supported functions list below.
|
|
273
|
+
- `args`: The arguments to be passed to the function represented by `function_name`. This can be a list or a table/dict. They should unpack with `*` or `**` respectively when called with the corresponding function.
|
|
274
|
+
- (optional) `params`: function-specific configurations to apply with the function calls.
|
|
275
|
+
- (optional) `id`: A unique identifier to group function calls.
|
|
276
|
+
|
|
277
|
+
A table that has keys other than the above keys would not be treated as a function call. Any function calls in different places of the config with the same `id` would be treated as a single group. Tables without an `id` are grouped based on their key-value pairs. Groups are used to identify how some parameters affect the results (e.g., `unique` for `choices`). Function calls can also be in `configurations.variabels`. Note that all function calls are made after the `extends` are resolved and variables from `configurations.variabels` are replaced.
|
|
278
|
+
|
|
279
|
+
### Supported functions
|
|
280
|
+
- `choices`: Calls [random.choices](https://docs.python.org/3/library/random.html#random.choices). `params` can be a table/dictionary which can have the key `unique`. The value of `unique` must be `true` or `false`. By default `unique` is `false`. If it's `true`, within a group of function calls, no value from the population passed to `random.choices` is repeated for a given participant.
|
|
281
|
+
|
|
282
|
+
### Example function calls
|
|
283
|
+
```toml
|
|
284
|
+
param = { function_name = "choices", args = [[1 , 2 , 3 , 4]], params = { unique = true } }
|
|
285
|
+
```
|
|
286
|
+
```toml
|
|
287
|
+
param = { foo = "test", bar = { function_name = "choices", args = { population = ["w", "x", "y", "z"], k = 1 } } }
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
For more on the `experiemnt-server` and how it can be used see the [wiki](https://github.com/ahmed-shariff/experiment_server/wiki)
|
|
291
|
+
|
|
292
|
+
# Wishlist (todo list?)
|
|
293
|
+
- Improved docs
|
|
294
|
+
- Add the option of using dict values in order
|
|
295
|
+
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
# Overview
|
|
2
|
+
|
|
3
|
+
This is a Python application that allows you to create/maintain/manage study configurations away from your implementations. `experiment-server` has several different interfaces (see below) to allow using it in a range of different scenarios. I've used it with Python, js and [Unity projects](https://github.com/ahmed-shariff/experiment_server/wiki/Using-with-Unity). See the [wiki](https://github.com/ahmed-shariff/experiment_server/wiki) for examples.
|
|
4
|
+
|
|
5
|
+
# Content
|
|
6
|
+
|
|
7
|
+
- [Overview](#overview)
|
|
8
|
+
- [Installation](#installation)
|
|
9
|
+
- [Usage](#usage)
|
|
10
|
+
- [Configuration of an experiment](#configuration-of-an-experiment)
|
|
11
|
+
- [Verify config](#verify-config)
|
|
12
|
+
- [Loading experiment through server](#loading-experiment-through-server)
|
|
13
|
+
- [Loading experiment through API](#loading-experiment-through-api)
|
|
14
|
+
- [Generate expanded config files](#generate-expanded-config-files)
|
|
15
|
+
- [Function calls in config](#function-calls-in-config)
|
|
16
|
+
- [Supported functions](#supported-functions)
|
|
17
|
+
- [Example function calls](#example-function-calls)
|
|
18
|
+
|
|
19
|
+
# Installation
|
|
20
|
+
|
|
21
|
+
Install it directly into an activated virtual environment:
|
|
22
|
+
|
|
23
|
+
```text
|
|
24
|
+
$ pip install experiment-server
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
or add it to your [Poetry](https://poetry.eustace.io/) project:
|
|
28
|
+
|
|
29
|
+
```text
|
|
30
|
+
$ poetry add experiment-server
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
# Usage
|
|
34
|
+
## Configuration of an experiment
|
|
35
|
+
The configuration is defined in a [toml](https://toml.io/en/) file.
|
|
36
|
+
|
|
37
|
+
A config file can be generated as follows
|
|
38
|
+
```sh
|
|
39
|
+
$ experiment-server new-config-file new_config.toml
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
See example `.toml` below for how the configuration can be defined.
|
|
43
|
+
|
|
44
|
+
```toml
|
|
45
|
+
# The `configuration` table contains the settings of the study/experiment itself
|
|
46
|
+
[configuration]
|
|
47
|
+
# The `order` is an array of block names or an array of array of block names.
|
|
48
|
+
order = [["conditionA", "conditionB", "conditionA", "conditionB"]]
|
|
49
|
+
# The `groups` and `within_groups` are optional keys that allows you to define how the
|
|
50
|
+
# conditions specified in `order` will be managed. `groups` would dictate how the top
|
|
51
|
+
# level array of `order` will be handled. `within_groups` would dictate how the conditions
|
|
52
|
+
# in the nested arrays (if specified) would be managed. These keys can have one
|
|
53
|
+
# of the following values.
|
|
54
|
+
# - "latin_square": Apply latin square to balance the values.
|
|
55
|
+
# - "randomize": For each participant randomize the order of the values in the array.
|
|
56
|
+
# - "as_is": Use the order of the values as specified.
|
|
57
|
+
# When not specified, the default value is "as_is" for both keys.
|
|
58
|
+
groups = "latin_square"
|
|
59
|
+
within_groups= "randomize"
|
|
60
|
+
# The random seed to use for any randomization. Default seed is 0. The seed will be
|
|
61
|
+
# the value of random_seed + participant_index
|
|
62
|
+
random_seed = 0
|
|
63
|
+
|
|
64
|
+
# The subtable `variabels` are values that can be used anywhere when defining the blocks.
|
|
65
|
+
# Any variable can be used by appending "$" before the variable name in the blocks. See
|
|
66
|
+
# below for an exmaple of how variables can be used
|
|
67
|
+
[configuration.variables]
|
|
68
|
+
TRIALS_PER_ITEM = 3
|
|
69
|
+
|
|
70
|
+
# Blocks are defined as an array of tables. Each block must contain `name` and the
|
|
71
|
+
# subtable `config`. Optionally, a block can also specify `extends`, whish is a `name` of
|
|
72
|
+
# another block. See below for more explanation on how `extends` works
|
|
73
|
+
|
|
74
|
+
# Block: Condition A
|
|
75
|
+
[[blocks]]
|
|
76
|
+
name = "conditionA"
|
|
77
|
+
|
|
78
|
+
# The `config` subtable can have any key-values. Note that `name` and `participant_index`
|
|
79
|
+
# will be added to the `config` when this file is being processed. Hence, those keys
|
|
80
|
+
# will be overwritten if used in this subtable.
|
|
81
|
+
[blocks.config]
|
|
82
|
+
trialsPerItem = "$TRIALS_PER_ITEM"
|
|
83
|
+
param1 = 1
|
|
84
|
+
# The value can also be a function call. A function call is represented as a table
|
|
85
|
+
# The following function call will be replaced with a call to
|
|
86
|
+
# [random.choices](https://docs.python.org/3/library/random.html#random.choices)
|
|
87
|
+
# See `# Function calls` in README for more information.
|
|
88
|
+
param2 = { function_name = "choices", args = { population = [1 , 2 , 3 ], k = 2}}
|
|
89
|
+
param3 = { function_name = "choices", args = [[1 , 2 , 3 ]], params = { unique = true } }
|
|
90
|
+
|
|
91
|
+
# Block: Condition B
|
|
92
|
+
[[blocks]]
|
|
93
|
+
name = "conditionB"
|
|
94
|
+
extends = "conditionA"
|
|
95
|
+
|
|
96
|
+
# Since "conditionB" is extending "conditionA", the keys in the `config` subtable of
|
|
97
|
+
# the block "conditionA" not defined in the `config` subtable of "conditionB" will be copied
|
|
98
|
+
# to the `config` subtable of "conditionB". In this example, `param1`, `param2` and
|
|
99
|
+
# `trialsPerItem` will be copied over here.
|
|
100
|
+
[blocks.config]
|
|
101
|
+
param3 = [2]
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
See [toml spec](https://toml.io/en/v1.0.0) for more information on the format of a toml file.
|
|
105
|
+
|
|
106
|
+
The above config file, after being processed, would result in the following list of blocks for participant number 1:
|
|
107
|
+
```json
|
|
108
|
+
[
|
|
109
|
+
{
|
|
110
|
+
"name": "conditionB",
|
|
111
|
+
"extends": "conditionA",
|
|
112
|
+
"config": {
|
|
113
|
+
"param3": [
|
|
114
|
+
2
|
|
115
|
+
],
|
|
116
|
+
"trialsPerItem": 3,
|
|
117
|
+
"param1": 1,
|
|
118
|
+
"param2": [
|
|
119
|
+
1,
|
|
120
|
+
2
|
|
121
|
+
],
|
|
122
|
+
"participant_index": 1,
|
|
123
|
+
"name": "conditionB",
|
|
124
|
+
"block_id": 0
|
|
125
|
+
}
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
"name": "conditionA",
|
|
129
|
+
"config": {
|
|
130
|
+
"trialsPerItem": 3,
|
|
131
|
+
"param1": 1,
|
|
132
|
+
"param2": [
|
|
133
|
+
2,
|
|
134
|
+
2
|
|
135
|
+
],
|
|
136
|
+
"param3": [
|
|
137
|
+
3
|
|
138
|
+
],
|
|
139
|
+
"participant_index": 1,
|
|
140
|
+
"name": "conditionA",
|
|
141
|
+
"block_id": 1
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
"name": "conditionA",
|
|
146
|
+
"config": {
|
|
147
|
+
"trialsPerItem": 3,
|
|
148
|
+
"param1": 1,
|
|
149
|
+
"param2": [
|
|
150
|
+
1,
|
|
151
|
+
1
|
|
152
|
+
],
|
|
153
|
+
"param3": [
|
|
154
|
+
2
|
|
155
|
+
],
|
|
156
|
+
"participant_index": 1,
|
|
157
|
+
"name": "conditionA",
|
|
158
|
+
"block_id": 2
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
{
|
|
162
|
+
"name": "conditionB",
|
|
163
|
+
"extends": "conditionA",
|
|
164
|
+
"config": {
|
|
165
|
+
"param3": [
|
|
166
|
+
2
|
|
167
|
+
],
|
|
168
|
+
"trialsPerItem": 3,
|
|
169
|
+
"param1": 1,
|
|
170
|
+
"param2": [
|
|
171
|
+
3,
|
|
172
|
+
1
|
|
173
|
+
],
|
|
174
|
+
"participant_index": 1,
|
|
175
|
+
"name": "conditionB",
|
|
176
|
+
"block_id": 3
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
]
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Verify config
|
|
183
|
+
A config file can be validated by running:
|
|
184
|
+
```sh
|
|
185
|
+
$ experiment-server verify-config-file sample_config.toml
|
|
186
|
+
```
|
|
187
|
+
This will show how the expanded config looks like for the first 5 participants.
|
|
188
|
+
|
|
189
|
+
## Loading experiment through server
|
|
190
|
+
After installation, the server can used as:
|
|
191
|
+
|
|
192
|
+
```sh
|
|
193
|
+
$ experiment-server run sample_config.toml
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
See more options with `--help`
|
|
197
|
+
|
|
198
|
+
The server exposes the following REST API:
|
|
199
|
+
- [GET] `/api/blocks-count` - Return the number of blocks in the configuration loaded.
|
|
200
|
+
- [GET] `/api/block-id` / `/api/block-id/:participant-id` - Returns the current block-id. If `participant-id` is provided, the blcok-id of the participant will be returned, if not the default participant's block-id will be returned. Note that the block-id is 0 indexed. i.e., the first block's block-id is 0.
|
|
201
|
+
- [GET] `/api/active` / `/api/active/:participant-id` - Returns the status for `participant-id`, if `participant-id` is not provided, will return the status of the default participant. Will be `false` if the participant was just initialized or the participant has gone through all blocks. To initialize the participant's status (or move to a given block), use the `move-to-next` or `move-to-block` endpoints.
|
|
202
|
+
- [GET] `/api/config` / `api/active/:participant-id` - Return the config for `participant-id`, if `participant-id` is not provided, will return the config for the default participant.
|
|
203
|
+
- [GET] `/api/summary-data` / `/api/summary-data/:participant-id` - Returns the summary of the configs for `participant-id`, if `participant-id` is not provided, returns the summary of the configs for the default participant. Currently, the summary is a JSON with the following keys
|
|
204
|
+
- "participant_index"
|
|
205
|
+
- "config_length"
|
|
206
|
+
- [GET] `/api/all-configs` / `/api/all-configs/:participant-id` - Returns all the configs as a list for the `participant-id`, if `participant-id` is not provided, returns the configs for the default participant. This is akin having the results of the `config` endpoint in one list.
|
|
207
|
+
- [GET] `/api/status-string` / `/api/status-string/:participant-id` - Returns status string for `participant-id`, if `participant-id` is not provided, returns statu string the default participant.
|
|
208
|
+
- [POST] `/api/move-to-next` / `/api/move-to-next/:participant-id` - Move `participant-id` to the next block, if `participant-id` is not provided, move the default participant to the next block. If the participant was not initialized (`active` is false), will make be marked as active (`active` will be set to true). If the block the participant was in was the last block, they will be marked as not active (`active` will be set to false).
|
|
209
|
+
- [POST] `/api/move-to-block/:block-id` / `/api/move-to-block/:participant-id/:block-id` - Move `participant-id` to the block number indicated by `block-id`, if `participant-id` is not provided, move the default participant to the block number indicated by `block-id`. If the participant was not initialized (`active` is false), will make be marked as active (`active` will be set to true). Will fail if the `block-id` is below 0 or above the length of the config.
|
|
210
|
+
- [POST] `/api/move-all-to-block/:block-id` - Move all active participants (`active` returns true) to the block number indicated by `block-id`.
|
|
211
|
+
- [POST] `/api/shutdown` - Shuts-down the server.
|
|
212
|
+
- [PUT] `/api/new-participant` - Adds a new participant and returns the new participant-id. The new participant-id will be the largest current participant-id +1.
|
|
213
|
+
- [PUT] `/api/add-participant/:participant-id` - Add a new participant with `participant-id`. If there is already a participant with the `participant-id`, this will fail.
|
|
214
|
+
|
|
215
|
+
For a Python application, `experiment_server.Client` can be used to access configs from the server. Also, the server can be launched programmatically using `experiment_server.server_process` which returns a [`Process`](https://docs.python.org/3/library/multiprocessing.html#multiprocessing.Process) object.
|
|
216
|
+
|
|
217
|
+
**NOTE**: If the config file served is changed, the new config will be loaded, but the state of the participants will be maintained. i.e., the added participants and the block id they are at will not change. To move the block ids for all active participants, you would have to call the `move-all-to-block` endpoint.
|
|
218
|
+
|
|
219
|
+
The server also provides a simple web interface, which can be accessed at `/` or `/index`. This interface allows to manage and monitor the flow of the experiment:
|
|
220
|
+
|
|
221
|
+

|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
## Loading experiment through API
|
|
225
|
+
A configuration can be loaded and managed by importing `experiment_server.Experiment`.
|
|
226
|
+
|
|
227
|
+
## Generate expanded configs
|
|
228
|
+
A config file (i.e. `.toml` file), can be expanded to JSON with the following command
|
|
229
|
+
|
|
230
|
+
```sh
|
|
231
|
+
$ experiment-server generate-config-json sample_config.toml --participant-range 5
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
The above will generate the expanded configs for participant indices 1 to 5 as JSON output on stdout. This result can be written out to individual JSON files by setting the `--out-dir`/`-d` to a directory. See more options with `--help`
|
|
235
|
+
|
|
236
|
+
## Function calls in config
|
|
237
|
+
A function call in the config is represented by a table, with the following keys
|
|
238
|
+
- `function_name`: This should be one of the names in the supported functions list below.
|
|
239
|
+
- `args`: The arguments to be passed to the function represented by `function_name`. This can be a list or a table/dict. They should unpack with `*` or `**` respectively when called with the corresponding function.
|
|
240
|
+
- (optional) `params`: function-specific configurations to apply with the function calls.
|
|
241
|
+
- (optional) `id`: A unique identifier to group function calls.
|
|
242
|
+
|
|
243
|
+
A table that has keys other than the above keys would not be treated as a function call. Any function calls in different places of the config with the same `id` would be treated as a single group. Tables without an `id` are grouped based on their key-value pairs. Groups are used to identify how some parameters affect the results (e.g., `unique` for `choices`). Function calls can also be in `configurations.variabels`. Note that all function calls are made after the `extends` are resolved and variables from `configurations.variabels` are replaced.
|
|
244
|
+
|
|
245
|
+
### Supported functions
|
|
246
|
+
- `choices`: Calls [random.choices](https://docs.python.org/3/library/random.html#random.choices). `params` can be a table/dictionary which can have the key `unique`. The value of `unique` must be `true` or `false`. By default `unique` is `false`. If it's `true`, within a group of function calls, no value from the population passed to `random.choices` is repeated for a given participant.
|
|
247
|
+
|
|
248
|
+
### Example function calls
|
|
249
|
+
```toml
|
|
250
|
+
param = { function_name = "choices", args = [[1 , 2 , 3 , 4]], params = { unique = true } }
|
|
251
|
+
```
|
|
252
|
+
```toml
|
|
253
|
+
param = { foo = "test", bar = { function_name = "choices", args = { population = ["w", "x", "y", "z"], k = 1 } } }
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
For more on the `experiemnt-server` and how it can be used see the [wiki](https://github.com/ahmed-shariff/experiment_server/wiki)
|
|
257
|
+
|
|
258
|
+
# Wishlist (todo list?)
|
|
259
|
+
- Improved docs
|
|
260
|
+
- Add the option of using dict values in order
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from importlib_metadata import version
|
|
2
|
+
import logging
|
|
3
|
+
from loguru import logger
|
|
4
|
+
|
|
5
|
+
__version__ = version(__package__)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class __InterceptHandler(logging.Handler):
|
|
9
|
+
def emit(self, record):
|
|
10
|
+
# Get corresponding Loguru level if it exists
|
|
11
|
+
try:
|
|
12
|
+
level = logger.level(record.levelname).name
|
|
13
|
+
except ValueError:
|
|
14
|
+
level = record.levelno
|
|
15
|
+
|
|
16
|
+
# Find caller from where originated the logged message
|
|
17
|
+
frame, depth = logging.currentframe(), 2
|
|
18
|
+
while frame.f_code.co_filename == logging.__file__:
|
|
19
|
+
frame = frame.f_back
|
|
20
|
+
depth += 1
|
|
21
|
+
|
|
22
|
+
logger.opt(depth=depth, exception=record.exc_info).log(level, record.getMessage())
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
logging.basicConfig(handlers=[__InterceptHandler()], level=0)
|
|
26
|
+
|
|
27
|
+
from experiment_server._server import server_process
|
|
28
|
+
from experiment_server._client import Client
|
|
29
|
+
from experiment_server._api import Experiment
|
|
30
|
+
|
|
31
|
+
__all__ = [server_process, Client, Experiment]
|