Ejemplo n.º 1
0
def test_lag_crp_block(data):
    """Test block-CRP analysis."""
    crp = fr.lag_crp(data, lag_key='block', count_unique=True)
    # recalls: [[2, 3, NaN], [3, 1, 3]]
    # blocks:  [[2, 2, NaN], [2, 1, 2]]
    np.testing.assert_array_equal(crp['actual'], np.array([1, 1, 0]))
    np.testing.assert_array_equal(crp['possible'], np.array([2, 1, 0]))
    np.testing.assert_array_equal(crp['prob'], np.array([0.5, 1.0, np.nan]))
Ejemplo n.º 2
0
def test_lag_crp_query_output(data):
    """Test lag-CRP analysis with item output position filter."""
    crp = fr.lag_crp(data, item_query='output > 1 or not recall')

    np.testing.assert_array_equal(crp['actual'], np.zeros(5))
    np.testing.assert_array_equal(crp['possible'], np.zeros(5))
    expected = np.empty(5)
    expected.fill(np.nan)
    np.testing.assert_array_equal(crp['prob'], expected)
Ejemplo n.º 3
0
    def test_lag_crp_cat(self):
        crp = fr.lag_crp(self.data, test_key='task', test=lambda x, y: x == y)
        actual = np.array([1, 0, 0, 0, 0])
        possible = np.array([1, 0, 0, 0, 0])
        prob = np.array([1., np.nan, np.nan, np.nan, np.nan])

        np.testing.assert_array_equal(crp['actual'], actual)
        np.testing.assert_array_equal(crp['possible'], possible)
        np.testing.assert_array_equal(crp['prob'], prob)
Ejemplo n.º 4
0
    def test_lag_crp_query(self):
        crp = fr.lag_crp(self.data, item_query='input != 2')
        actual = np.array([1, 0, 0, 0, 0])
        possible = np.array([1, 0, 0, 0, 0])
        prob = np.array([1., np.nan, np.nan, np.nan, np.nan])

        np.testing.assert_array_equal(crp['actual'], actual)
        np.testing.assert_array_equal(crp['possible'], possible)
        np.testing.assert_array_equal(crp['prob'], prob)
Ejemplo n.º 5
0
    def test_lag_crp(self):
        crp = fr.lag_crp(self.data)
        actual = np.array([1, 0, 0, 1, 0])
        possible = np.array([1, 2, 0, 1, 0])
        prob = np.array([1., 0., np.nan, 1., np.nan])

        np.testing.assert_array_equal(crp['actual'], actual)
        np.testing.assert_array_equal(crp['possible'], possible)
        np.testing.assert_array_equal(crp['prob'], prob)
Ejemplo n.º 6
0
def test_lag_crp_cat(data):
    """Test lag-CRP conditional on within-category transitions."""
    crp = fr.lag_crp(data, test_key='task', test=lambda x, y: x == y)
    actual = np.array([1, 0, 0, 0, 0])
    possible = np.array([1, 0, 0, 0, 0])
    prob = np.array([1.0, np.nan, np.nan, np.nan, np.nan])

    np.testing.assert_array_equal(crp['actual'], actual)
    np.testing.assert_array_equal(crp['possible'], possible)
    np.testing.assert_array_equal(crp['prob'], prob)
Ejemplo n.º 7
0
def test_lag_crp_query_input(data):
    """Test lag-CRP analysis with item input position filter."""
    crp = fr.lag_crp(data, item_query='input != 2')
    actual = np.array([1, 0, 0, 0, 0])
    possible = np.array([1, 0, 0, 0, 0])
    prob = np.array([1.0, np.nan, np.nan, np.nan, np.nan])

    np.testing.assert_array_equal(crp['actual'], actual)
    np.testing.assert_array_equal(crp['possible'], possible)
    np.testing.assert_array_equal(crp['prob'], prob)
Ejemplo n.º 8
0
def test_lag_crp(data):
    """Test basic lag-CRP analysis."""
    crp = fr.lag_crp(data)
    actual = np.array([1, 0, 0, 1, 0])
    possible = np.array([1, 2, 0, 1, 0])
    prob = np.array([1.0, 0.0, np.nan, 1.0, np.nan])

    np.testing.assert_array_equal(crp['actual'], actual)
    np.testing.assert_array_equal(crp['possible'], possible)
    np.testing.assert_array_equal(crp['prob'], prob)
Ejemplo n.º 9
0
def test_plot_lag_crp(data):
    stat = fr.lag_crp(data)
    g = fr.plot_lag_crp(stat)
    plt.close()
    assert isinstance(g, sns.FacetGrid)
Ejemplo n.º 10
0
def test_plot_lag_crp(data):
    """Test plotting a lag-CRP curve."""
    stat = fr.lag_crp(data)
    g = fr.plot_lag_crp(stat)
    plt.close()
    assert isinstance(g, sns.FacetGrid)
Ejemplo n.º 11
0
def visualize_fit(model_class,
                  parameters,
                  data,
                  data_query=None,
                  experiment_count=1000,
                  savefig=False):
    """
    Apply organizational analyses to visually compare the behavior of the model with these parameters against
    specified dataset.
    """

    # generate simulation data from model
    model = model_class(**parameters)
    try:
        model.experience(np.eye(model.item_count, model.item_count + 1, 1))
    except ValueError:
        model.experience(np.eye(model.item_count, model.item_count))
    sim = []
    for experiment in range(experiment_count):
        sim += [[experiment, 0, 'study', i + 1, i]
                for i in range(model.item_count)]
    for experiment in range(experiment_count):
        sim += [[experiment, 0, 'recall', i + 1, o]
                for i, o in enumerate(model.free_recall())]
    sim = pd.DataFrame(
        sim, columns=['subject', 'list', 'trial_type', 'position', 'item'])
    sim_data = fr.merge_free_recall(sim)

    # generate simulation-based spc, pnr, lag_crp
    sim_spc = fr.spc(sim_data).reset_index()
    sim_pfr = fr.pnr(sim_data).query('output <= 1').reset_index()
    sim_lag_crp = fr.lag_crp(sim_data).reset_index()

    # generate data-based spc, pnr, lag_crp
    data_spc = fr.spc(data).query(data_query).reset_index()
    data_pfr = fr.pnr(data).query('output <= 1').query(
        data_query).reset_index()
    data_lag_crp = fr.lag_crp(data).query(data_query).reset_index()

    # combine representations
    data_spc['Source'] = 'Data'
    sim_spc['Source'] = model_class.__name__
    combined_spc = pd.concat([data_spc, sim_spc], axis=0)

    data_pfr['Source'] = 'Data'
    sim_pfr['Source'] = model_class.__name__
    combined_pfr = pd.concat([data_pfr, sim_pfr], axis=0)

    data_lag_crp['Source'] = 'Data'
    sim_lag_crp['Source'] = model_class.__name__
    combined_lag_crp = pd.concat([data_lag_crp, sim_lag_crp], axis=0)

    # generate plots of result
    # spc
    g = sns.FacetGrid(dropna=False, data=combined_spc)
    g.map_dataframe(sns.lineplot, x='input', y='recall', hue='Source')
    g.set_xlabels('Serial position')
    g.set_ylabels('Recall probability')
    plt.title('P(Recall) by Serial Position Curve')
    g.add_legend()
    g.set(ylim=(0, 1))
    if savefig:
        plt.savefig('figures/{}_fit_spc.jpeg'.format(model_class.__name__),
                    bbox_inches='tight')
    else:
        plt.show()

    #pdf
    h = sns.FacetGrid(dropna=False, data=combined_pfr)
    h.map_dataframe(sns.lineplot, x='input', y='prob', hue='Source')
    h.set_xlabels('Serial position')
    h.set_ylabels('Probability of First Recall')
    plt.title('P(First Recall) by Serial Position')
    h.add_legend()
    h.set(ylim=(0, 1))
    if savefig:
        plt.savefig('figures/{}_fit_pfr.jpeg'.format(model_class.__name__),
                    bbox_inches='tight')
    else:
        plt.show()

    # lag crp
    max_lag = 5
    filt_neg = f'{-max_lag} <= lag < 0'
    filt_pos = f'0 < lag <= {max_lag}'
    i = sns.FacetGrid(dropna=False, data=combined_lag_crp)
    i.map_dataframe(lambda data, **kws: sns.lineplot(
        data=data.query(filt_neg), x='lag', y='prob', hue='Source', **kws))
    i.map_dataframe(lambda data, **kws: sns.lineplot(
        data=data.query(filt_pos), x='lag', y='prob', hue='Source', **kws))
    i.set_xlabels('Lag')
    i.set_ylabels('Recall Probability')
    plt.title('Recall Probability by Item Lag')
    i.add_legend()
    i.set(ylim=(0, 1))
    if savefig:
        plt.savefig('figures/{}_fit_crp.jpeg'.format(model_class.__name__),
                    bbox_inches='tight')
    else:
        plt.show()
Ejemplo n.º 12
0
 def time_lag_crp(self):
     crp = fr.lag_crp(self.data)