示例#1
0
def fromFile(filename, encoding='utf-8-sig'):
    """Load data from a psydat, pickle or JSON file.

    Parameters
    ----------
    encoding : str
        The encoding to use when reading a JSON file. This parameter will be
        ignored for any other file type.

    """
    filename = pathToString(filename)
    if filename.endswith('.psydat'):
        with open(filename, 'rb') as f:
            try:
                contents = pickle.load(f)
            except UnicodeDecodeError:
                f.seek(0)  # reset to start of file to try again
                contents = pickle.load(
                    f, encoding='latin1')  # python 2 data files
            # if loading an experiment file make sure we don't save further
            # copies using __del__
            if hasattr(contents, 'abort'):
                contents.abort()
            return contents
    elif filename.endswith('pickle'):
        with open(filename, 'rb') as f:
            contents = pickle.load(f)
        return contents
    elif filename.endswith('.json'):
        with codecs.open(filename, 'r', encoding=encoding) as f:
            contents = json_tricks.load(f)

        # Restore RNG if we load a TrialHandler2 object.
        # We also need to remove the 'temporary' ._rng_state attribute that
        # was saved with it.
        from psychopy.data import TrialHandler2
        if isinstance(contents, TrialHandler2):
            contents._rng = np.random.default_rng()
            contents._rng.bit_generator.state = contents._rng_state
            del contents._rng_state
            return contents

        # QuestPlus.
        if sys.version_info.major == 3 and sys.version_info.minor >= 6:
            from psychopy.data.staircase import QuestPlusHandler
            from questplus import QuestPlus
            if isinstance(contents, QuestPlusHandler):
                # Restore the questplus.QuestPlus object.
                contents._qp = QuestPlus.from_json(contents._qp_json)
                del contents._qp_json
                return contents

        # If we haven't returned anything by now, the loaded object is neither
        # a TrialHandler2 nor a QuestPlus instance. Return it unchanged.
        return contents
    else:
        msg = "Don't know how to handle this file type, aborting."
        raise ValueError(msg)
示例#2
0
lower_asymptote_prior = lower_asymptote_prior / lower_asymptote_prior.sum()

slope_prior = scipy.stats.norm.pdf(x=param['slope'], loc=3, scale=1)
slope_prior = slope_prior / slope_prior.sum()

prior = dict(threshold=np.ones(len(param['threshold'])),
             slope=slope_prior,
             lower_asymptote=lower_asymptote_prior,
             lapse_rate=np.ones(len(param['lapse_rate'])))

stim_domain = dict(intensity=intensities)
stim_selection_options = dict(n=3, max_consecutive_reps=2)
q = QuestPlus(stim_domain=stim_domain,
              func='weibull',
              stim_scale='log10',
              param_domain=param,
              prior=prior,
              outcome_domain=outcome_domain,
              stim_selection_method='min_n_entropy',
              stim_selection_options=stim_selection_options)

with np.printoptions(precision=3, suppress=True):
    print(q.stim_domain)

plot(q)
print(q.next_stim)
for trial_no in range(1, 20 + 1):
    if trial_no == 1:
        intensity = intensities[
            3]  # start with a relatively high concentration
    else:
        intensity = q.next_stim['intensity']