Welcome to the Scikit-oTree tutorial. This package aims to integrate any experiment developed on-top of oTree, with the Python Scientific-Stack; alowing the scientists to access a big collection of tools for analyse the experimental data.
In [1]:
import skotree
skotree.VERSION
Out[1]:
Scikit-oTree don't preprocess any data from the experiment. All the information are preserved exactly as any traditional export from oTree; the project only take this data and present it.
oTree uses some global configuration to make it run. Scikit-oTree don't store any global configuration alowwing to load data from different experiments without problems. All the oTree related processing always happen in an external process.
The data are always presented as a Pandas DataFrame
To install Scikit-oTree you must has Python and PIP. You can found a comprensive tutorial to install it here.
After that you only need to run
pip install -U scikit-otree
In [2]:
# this load the library
import skotree
# this load the experiment located
# in the directory tests and
experiment = skotree.oTree("./tests")
experiment
Out[2]:
The previous code make a lot of things in background:
experiment
object.Let's check the result
In [3]:
experiment.settings
Out[3]:
This is the traditional object that you obtain in any oTree experiment if you write
from django.conf import settings
Now let's check some information about the experiment, for example all the exiting oTree apps.
In [4]:
experiment.lsapps()
Out[4]:
or maybe you want to see all the sessiong configured that uses all this apps
In [5]:
experiment.lssessions()
Out[5]:
Yikes! the app and the session has the same name. Let's check the full session configuration.
In [6]:
experiment.session_config("matching_pennies")
Out[6]:
Finally you can access any content of the settings object ussing the attribute showed before. For example, maybe you want to see the "currency code"
In [7]:
experiment.settings.REAL_WORLD_CURRENCY_CODE
Out[7]:
In [8]:
all_data = experiment.all_data()
all_data
Out[8]:
In [9]:
data = experiment.app_data("matching_pennies")
data
Out[9]:
With the power of pandas.DataFrame you can easily filter the data
In [10]:
filtered = data[["participant.code", "player.penny_side", "player.payoff"]]
filtered
Out[10]:
Describe the data
In [11]:
filtered.describe()
Out[11]:
group by participant
In [12]:
group = filtered.groupby("participant.code")
group.describe()
Out[12]:
or check all the columns availables
In [13]:
data.columns
Out[13]:
In [14]:
tspent = experiment.time_spent()
tspent
Out[14]:
In [15]:
# check the available columns
tspent.columns
Out[15]:
In [16]:
# filter only the most important columns
tspent = tspent[["participant__code", "page_index", "seconds_on_page"]]
tspent
Out[16]:
In [17]:
# lets describe the time expent by page
tspent.groupby("page_index").describe()
Out[17]:
In [18]:
# and lets make a plot but grouped by participant
%matplotlib inline
tspent.groupby("participant__code")[["seconds_on_page"]].plot();
Scikit-oTree offers out of the box the posibility to run the oTree bot-based-tests and retrieve all the data generated by them.
The method bot_data()
consume two arguments:
And the return a dict like object (called CSVStorage
) whith the same attributes as application has the session in the app_sequence
key.
In [19]:
storage = experiment.bot_data("matching_pennies", 4)
storage
Out[19]:
as you can see the only available app (as we see before) is the matching_pennies
.
Lets extract the data
In [20]:
storage["matching_pennies"]
Out[20]:
also for convenience the sintax storage.matching_pennied
are available
In [21]:
storage.matching_pennies
Out[21]:
If for some reason the experiment fails, this method returns an exception. for example if we provide a invalid number of participants
In [22]:
experiment.bot_data("matching_pennies", 1)
In [23]:
remote = skotree.oTree("http://localhost:8000")
remote
Out[23]:
In [24]:
remote.lsapps()
Out[24]:
In [25]:
remote.lssessions()
Out[25]:
In [26]:
remote.app_data("matching_pennies")
Out[26]:
If you are trying to connect to server in auth level mode DEMO
or STUDY
(More Information about modes) without credentials an error will be shown:
In [3]:
skotree.oTree("http://localhost:9000")
In this cases you need to provide the parameters username
and password
In [5]:
# the credential are not stored internally
exp = skotree.oTree("http://localhost:9000", username="admin", password="skotree")
exp
Out[5]:
and now all works as before
In [7]:
exp.all_data()
Out[7]:
session_config()
bot_data()
Raises an NotImplementedError
when are called.
In [27]:
remote.bot_data("matching_pennies", 1)
Also the settings attribute always returns None
The pandas.DataFrame itself is a great tool for data analysis, but also can be easily adapted to feed a lot of libraries like: