<jupyter_start><jupyter_text>The Egg data objectThis tutorial will go over the basics of the `Egg` data object, the essential quail data structure that contains all the data you need to run analyses and plot the results. An egg is made up of two primary pieces of data: 1. `pres` data - words/stimuli that were presented to a subject2. `rec` data - words/stimuli that were recalled by the subject.You cannot create an `egg` without both of these components.  Additionally, there are a few optional fields:1. `features` data - features that describe each of the stimuli (for example, the category of the word, word length, etc.). This field is required for fingerprint analysis (see the fingerprint tutorial).2. `dist_funcs` dictionary - this field allows you to control the distance functions for each of the stimulus features.  For more on this, see the fingerprint tutorial.3. `meta` dictionary - this is an optional field that allows you to store custom meta data about the dataset, such as the date collected, experiment version etc.There are also a few other fields and functions to make organizing and modifying `eggs` easier (discussed at the bottom).  Now, lets dive in and create an `egg` from scratch.  Load in the library<jupyter_code>import quail<jupyter_output><empty_output><jupyter_text>The `pres` data structureThe first piece of an `egg` is the `pres` data, or in other words the words/stimuli that were presented to the subject. For a single subject's data, the form of the input will be a list of lists, where each list is comprised of the words presented to the subject during a particular study block. Let's create a fake dataset of one subject who saw two encoding lists:<jupyter_code>presented_words = [['cat', 'bat', 'hat', 'goat'],['zoo', 'animal', 'zebra', 'horse']]<jupyter_output><empty_output><jupyter_text>The `rec` data structureThe second fundamental component of an egg is the `rec` data, or the words/stimuli that were recalled by the subject. Now, let's create the recall lists:<jupyter_code>recalled_words = [['bat', 'cat', 'goat', 'hat'],['animal', 'horse', 'zoo']]<jupyter_output><empty_output><jupyter_text>We now have the two components necessary to build an `egg`, so let's do that and then take a look at the result.<jupyter_code>egg = quail.Egg(pres=presented_words, rec=recalled_words)<jupyter_output><empty_output><jupyter_text>That's it!  We've created our first `egg`. Let's take a closer look at how the `egg` is setup. We can use the `info` method to get a quick snapshot of the `egg`:<jupyter_code>egg.info()<jupyter_output>Number of subjects: 1
Number of lists per subject: 2
Number of words per list: 4
Date created: Tue May  2 14:05:59 2017
Meta data: {}<jupyter_text>Now, let's take a closer look at how the `egg` is structured.  First, we will check out the `pres` field:<jupyter_code>egg.pres<jupyter_output><empty_output><jupyter_text>As you can see above, the `pres` field was turned into a multi-index Pandas DataFrame organized by subject and by list. This is how the `pres` data is stored within an egg, which will make more sense when we consider larger datasets with more subjects.  Next, let's take a look at the `rec` data:<jupyter_code>egg.rec<jupyter_output><empty_output><jupyter_text>The `rec` data is also stored as a DataFrame.  Notice that if the number of recalled words is shorter than the number of presented words (as it typically will be), those columns are filled with a `None` value. Now, let's create an `egg` with two subject's data and take a look at the result.  Multisubject `eggs`<jupyter_code># presented words
sub1_presented=[['cat', 'bat', 'hat', 'goat'],['zoo', 'animal', 'zebra', 'horse']]
sub2_presented=[['cat', 'bat', 'hat', 'goat'],['zoo', 'animal', 'zebra', 'horse']]

# recalled words
sub1_recalled=[['bat', 'cat', 'goat', 'hat'],['animal', 'horse', 'zoo']]
sub2_recalled=[['cat', 'goat', 'bat', 'hat'],['horse', 'zebra', 'zoo', 'animal']]

# combine subject data
presented_words = [sub1_presented, sub2_presented]
recalled_words = [sub1_recalled, sub2_recalled]

# create Egg
multisubject_egg = quail.Egg(pres=presented_words,rec=recalled_words)<jupyter_output><empty_output><jupyter_text>As you can see above, in order to create an `egg` with more than one subject's data, all you do is create a list of subjects.  Let's see how the `pres` data is organized in the egg with more than one subject:<jupyter_code>multisubject_egg.pres<jupyter_output><empty_output><jupyter_text>Looks identical to the single subject data, but now we have two unique subject identifiers in the `DataFrame`. The `rec` data is set up in the same way:<jupyter_code>multisubject_egg.rec<jupyter_output><empty_output><jupyter_text>As you add more subjects, they are simply appended to the bottom of the df with a unique subject identifier.  The `features` data structureThe `features` data structure is an optional field that can be added to an egg.  It contains features of the presented words that are required for the fingerprint analysis.  The `features` data is set up like the `pres` and `rec` data, but instead of a single word, there is a dictionary of features.  For example take the word "cat".  A `features` dictionary for this word might look something like this:<jupyter_code>cat_features = {
    'category' : 'animal',
    'word_length' : 3,
    'starting_letter' : 'c',
}<jupyter_output><empty_output><jupyter_text>You can include any stimulus feature you want in this dictionary, such as the position of the word on the screen, the color, or perhaps the font of the word. To create the `features` data structure in an `egg`, use the format of the `pres` structure, but replace the words with dictionaries:<jupyter_code># presented words
presented_words=[['cat', 'bat', 'hat', 'goat'],['zoo', 'donkey', 'zebra', 'horse']]

# presentation features
presented_words_features = [
    [
        {
            'category' : 'animal',
            'word_length' : 3,
            'starting_letter' : 'c'
        },
        {
            'category' : 'object',
            'word_length' : 3,
            'starting_letter' : 'b'
        },
        {
            'category' : 'object',
            'word_length' : 3,
            'starting_letter' : 'h'
        },
        {
            'category' : 'animal',
            'word_length' : 4,
            'starting_letter' : 'g'
        },
    ],
    [
        {
            'category' : 'place',
            'word_length' : 3,
            'starting_letter' : 'z'
        },
        {
            'category' : 'animal',
            'word_length' : 6,
            'starting_letter' : 'd'
        },
        {
            'category' : 'animal',
            'word_length' : 5,
            'starting_letter' : 'z'
        },
        {
            'category' : 'animal',
            'word_length' : 5,
            'starting_letter' : 'h'
        },
    ],
]

# recalled words
recalled_words=[['bat', 'cat', 'goat', 'hat'],['donkey', 'horse', 'zoo']]<jupyter_output><empty_output><jupyter_text>Then, simply pass the features to the `Egg` class using the `features` key word argument:<jupyter_code># create egg object
egg = quail.Egg(pres=presented_words, rec=recalled_words, features=presented_words_features)
egg.features<jupyter_output><empty_output><jupyter_text>Defining custom distance functions for the stimulus feature dimensions As described in the fingerprint tutorial, the `features` data structure is used to estimate how subjects cluster their recall responses with respect to the features of the encoded stimuli.  Briefly, these estimates are derived by computing the similarity of neighboring recall words along each feature dimension.  For example, if you recall "dog", and then the next word you recall is "cat", your clustering by category score would increase because the two recalled words are in the same category.  Similarly, if after you recall "cat" you recall the word "can", your clustering by starting letter score would increase, since both words share the first letter "c".  This logic can be extended to any number of feature dimensions.Similarity between the words can be computed in a number of ways.  By default, the distance function for all textual features (like category, starting letter) is binary.  In other words, if the words are in the same category (cat, dog), there similarity would be 1, whereas if they are in different categories (cat, can) their similarity would be 0.  For numerical features (such as word length), by default similarity between words is computed using Euclidean distance.  However, the point of this digression is that you can define your own distance functions by passing a `dist_func` dictionary to the `Egg` class.  This could be for all feature dimensions, or only a subset. Let's see an example:<jupyter_code>dist_funcs = {
    'word_length' : lambda x,y: (x-y)**2
}

egg = quail.Egg(pres=presented_words, rec=recalled_words, features=presented_words_features, dist_funcs=dist_funcs)<jupyter_output><empty_output><jupyter_text>In the example code above, similarity between words for the word_length feature dimension will now be computed using this custom distance function, while all other feature dimensions will be set to the default.  Adding meta data to an `egg` Lastly, we can add meta data to the `egg`.  We added this field to help researchers keep their eggs organized by adding custom meta data to the `egg` object. The data is added to the `egg` by passing the `meta` key word argument when creating the `egg`:<jupyter_code>meta = {
    'Researcher' : '<NAME>',
    'Study' : 'Egg Tutorial'
}

egg = quail.Egg(pres=presented_words, rec=recalled_words, meta=meta)
egg.info()<jupyter_output>Number of subjects: 1
Number of lists per subject: 2
Number of words per list: 4
Date created: Tue May  2 14:08:16 2017
Meta data: {'Researcher': '<NAME>', 'Study': 'Egg Tutorial'}<jupyter_text>Adding `listgroup` and `subjgroup` to an `egg`While the `listgroup` and `subjgroup` arguments can be used within the `analyze` function, they can also be attached directly to the `egg`, allowing you to save condition labels for easy organization and easy data sharing.To do this, simply pass one or both of the arguments when creating the `egg`:<jupyter_code># presented words
sub1_presented=[['cat', 'bat', 'hat', 'goat'],['zoo', 'animal', 'zebra', 'horse']]
sub2_presented=[['cat', 'bat', 'hat', 'goat'],['zoo', 'animal', 'zebra', 'horse']]

# recalled words
sub1_recalled=[['bat', 'cat', 'goat', 'hat'],['animal', 'horse', 'zoo']]
sub2_recalled=[['cat', 'goat', 'bat', 'hat'],['horse', 'zebra', 'zoo', 'animal']]

# combine subject data
presented_words = [sub1_presented, sub2_presented]
recalled_words = [sub1_recalled, sub2_recalled]

# create Egg
multisubject_egg = quail.Egg(pres=presented_words,rec=recalled_words, subjgroup=['condition1', 'condition2'],
                            listgroup=['early','late'])<jupyter_output><empty_output><jupyter_text>Saving an `egg`Once you have created your egg, you can save it for use later, or to share with colleagues. To do this, simply call the `save` method with a filepath:```multisubject_egg.save('myegg.p')```This will save you `egg` using the package `pickle`. The result? A pickled egg!  To load this egg later, simply call the `load_egg` function with the path of the egg:```egg = quail.load('myegg.p')```  Stacking `eggs`We now have two separate eggs, each with a single subject's data. Let's combine them by passing a `list` of `eggs` to the `stack_eggs` function:<jupyter_code># subject 1 data
sub1_presented=[['cat', 'bat', 'hat', 'goat'],['zoo', 'animal', 'zebra', 'horse']]
sub1_recalled=[['bat', 'cat', 'goat', 'hat'],['animal', 'horse', 'zoo']]

# create subject 2 egg
subject1_egg = quail.Egg(pres=sub1_presented, rec=sub1_recalled)

# subject 2 data
sub2_presented=[['cat', 'bat', 'hat', 'goat'],['zoo', 'animal', 'zebra', 'horse']]
sub2_recalled=[['cat', 'goat', 'bat', 'hat'],['horse', 'zebra', 'zoo', 'animal']]

# create subject 2 egg
subject2_egg = quail.Egg(pres=sub2_presented, rec=sub2_recalled)
stacked_eggs = quail.stack_eggs([subject1_egg, subject2_egg])
stacked_eggs.pres<jupyter_output><empty_output><jupyter_text>Cracking `eggs`You can use the `crack_egg` function to slice out a subset of subjects or lists:<jupyter_code>cracked_egg = quail.crack_egg(stacked_eggs, subjects=[1], lists=[0])
cracked_egg.pres<jupyter_output><empty_output><jupyter_text>Alternatively, you can use the `crack` method, which does the same thing:<jupyter_code>stacked_eggs.crack(subjects=[0,1], lists=[1]).pres<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10890302066772654, 'fraction_numerical': 0.006677265500794912, 'mean_word_length': 4.677346570397112, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '32349', 'license': 'MIT', 'n_tokens_mistral': 3963, 'n_tokens_neox': 3762, 'n_words': 1506, 'path': 'docs/tutorial/.ipynb_checkpoints/egg-checkpoint.ipynb', 'repo_name': 'mkclairhong/quail', 'size': '12592'}