Пример #1
0
def eid2path(eid: Union[str, Iter], one=None, offline: bool = False) -> Union[Path, List]:
    """
    Returns a local path from an eid, regardless of whether the path exists locally
    :param eid: An experiment uuid
    :param one: An instance of ONE
    :param offline: If True, do not connect to database (not implemented)
    :return: a Path instance

    Examples:
    >>> base = 'https://test.alyx.internationalbrainlab.org'
    >>> one = ONE(username='******', password='******', base_url=base)
    Connected to...
    >>> eid = '4e0b3320-47b7-416e-b842-c34dc9004cf8'
    >>> eid2path(eid, one=one)
    WindowsPath('E:/FlatIron/zadorlab/Subjects/flowers/2018-07-13/001')
    >>> eid2path([eid, '7dc3c44b-225f-4083-be3d-07b8562885f4'], one=one)
    [WindowsPath('E:/FlatIron/zadorlab/Subjects/flowers/2018-07-13/001'),
     WindowsPath('E:/FlatIron/cortexlab/Subjects/KS005/2019-04-11/001')]
    """
    if not one:
        one = ONE()
    if offline:
        raise NotImplementedError
        # path = one.path_from_eid(eid, offline=True)
    else:
        d = one.get_details(eid)
        path = d.get('local_path')
        if not path:
            root = Path(one._get_cache_dir(None)) / d['lab'] / 'Subjects'
            path = root / d['subject'] / d['start_time'][:10] / ('%03d' % d['number'])
    return path
Пример #2
0
def eid2ref(eid: Union[str, Iter], one=None, as_dict=True, parse=True) \
        -> Union[str, Mapping, List]:
    """
    Get human-readable session ref from path
    :param eid: The experiment uuid to find reference for
    :param one: An ONE instance
    :param as_dict: If false a string is returned in the form 'subject_sequence_yyyy-mm-dd'
    :param parse: If true, the reference date and sequence are parsed from strings to their
    respective data types
    :return: one or more objects with keys ('subject', 'date', 'sequence'), or strings with the
    form yyyy-mm-dd_n_subject

    Examples:
    >>> base = 'https://test.alyx.internationalbrainlab.org'
    >>> one = ONE(username='******', password='******', base_url=base)
    Connected to...
    >>> eid = '4e0b3320-47b7-416e-b842-c34dc9004cf8'
    >>> eid2ref(eid, one=one)
    {'subject': 'flowers', 'date': datetime.date(2018, 7, 13), 'sequence': 1}
    >>> eid2ref(eid, parse=False, one=one)
    {'subject': 'flowers', 'date': '2018-07-13', 'sequence': '001'}
    >>> eid2ref(eid, as_dict=False, one=one)
    '2018-07-13_1_flowers'
    >>> eid2ref(eid, as_dict=False, parse=False, one=one)
    '2018-07-13_001_flowers'
    >>> eid2ref([eid, '7dc3c44b-225f-4083-be3d-07b8562885f4'], one=one)
    [{'subject': 'flowers', 'date': datetime.date(2018, 7, 13), 'sequence': 1},
     {'subject': 'KS005', 'date': datetime.date(2019, 4, 11), 'sequence': 1}]
    """
    if not one:
        one = ONE()

    d = one.get_details(eid)
    if parse:
        date = datetime.fromisoformat(d['start_time']).date()
        ref = {'subject': d['subject'], 'date': date, 'sequence': d['number']}
        format_str = '{date:%Y-%m-%d}_{sequence:d}_{subject:s}'
    else:
        date = d['start_time'][:10]
        ref = {'subject': d['subject'], 'date': date, 'sequence': '%03d' % d['number']}
        format_str = '{date:s}_{sequence:s}_{subject:s}'
    return Bunch(ref) if as_dict else format_str.format(**ref)
Пример #3
0
def criteria_opto_eids(eids,
                       max_lapse=0.2,
                       max_bias=0.3,
                       min_trials=200,
                       one=None):
    if one is None:
        one = ONE()
    use_eids = []
    for j, eid in enumerate(eids):
        try:
            trials = load_trials(eid, laser_stimulation=True)
            lapse_l = 1 - (
                np.sum(trials.loc[trials['signed_contrast'] == -1,
                                  'choice'] == 1) /
                trials.loc[trials['signed_contrast'] == -1, 'choice'].shape[0])
            lapse_r = 1 - (
                np.sum(trials.loc[trials['signed_contrast'] == 1,
                                  'choice'] == -1) /
                trials.loc[trials['signed_contrast'] == 1, 'choice'].shape[0])
            bias = np.abs(0.5 -
                          (np.sum(trials.loc[trials['signed_contrast'] == 0,
                                             'choice'] == 1) /
                           np.shape(trials.loc[trials['signed_contrast'] == 0,
                                               'choice'] == 1)[0]))
            details = one.get_details(eid)
            if ((lapse_l < max_lapse) & (lapse_r < max_lapse) &
                (trials.shape[0] > min_trials)
                    & (bias < max_bias) &
                ('laser_stimulation' in trials.columns)):
                use_eids.append(eid)
            elif 'laser_stimulation' not in trials.columns:
                print('No laser_stimulation data for %s %s' %
                      (details['subject'], details['start_time'][:10]))
            else:
                print(
                    '%s %s excluded (n_trials: %d, lapse_l: %.2f, lapse_r: %.2f, bias: %.2f)'
                    % (details['subject'], details['start_time'][:10],
                       trials.shape[0], lapse_l, lapse_r, bias))
        except Exception:
            print('Could not load session %s' % eid)
    return use_eids
    eid = eids[i]
    try:
        spikes, clusters, channels = bbone.load_spike_sorting_with_channel(
                                                                    eid, aligned=True, one=one)
        ses_path = one.path_from_eid(eid)
        trials = load_trials(eid)
    except Exception as error_message:
        print(error_message)
        continue

    # Check data integrity
    if check_trials(trials) is False:
        continue

    # Extract session data
    ses_info = one.get_details(eid)
    subject = ses_info['subject']
    date = ses_info['start_time'][:10]
    probes_to_use = probes[i]

    # Process per probe
    for p, probe in enumerate(probes_to_use):
        print('Processing %s (%d of %d)' % (probe, p + 1, len(probes_to_use)))

        # Check if data is available for this probe
        if probe not in clusters.keys():
            continue

        # Check if histology is available for this probe
        if not hasattr(clusters[probe], 'acronym'):
            continue
Пример #5
0
def plot_all(eid):
    matplotlib.rcParams.update({'font.size': 10})
    # report eid =  '4a45c8ba-db6f-4f11-9403-56e06a33dfa4'

    panels = {
        'plot_paw_on_image': plot_paw_on_image,
        'plot_wheel_position': plot_wheel_position,
        'paw_speed_PSTH': paw_speed_PSTH,
        'plot_licks': plot_licks,
        'lick_raster': lick_raster,
        'nose_speed_PSTH': nose_speed_PSTH,
        'pupil_diameter_PSTH': pupil_diameter_PSTH,
        'motion_energy_PSTH': motion_energy_PSTH
    }

    nrows = 2
    ncols = 4

    #plt.ioff()

    plt.figure(figsize=(15, 10))

    k = 1
    for panel in panels:
        plt.subplot(nrows, ncols, k)
        add_panel_letter(k)
        try:
            panels[panel](eid)
            #continue
        except:
            ax = plt.gca()
            plt.text(.5,
                     .5,
                     f'error in \n {panel}',
                     color='r',
                     fontweight='bold',
                     bbox=dict(facecolor='white', alpha=0.5),
                     fontsize=10,
                     transform=ax.transAxes)
        k += 1

    plt.tight_layout()

    # print QC outcome in title and DLC task version
    one = ONE()
    task = one.alyx.rest('tasks', 'list', session=eid, name='EphysDLC')[0]
    det = one.get_details(eid, True)['extended_qc']
    p = one.path_from_eid(eid)
    s1 = ' '.join([str(p).split('/')[i] for i in [4, 6, 7, 8]])

    dlc_qcs = [
        'time_trace_length_match', 'trace_all_nan', 'mean_in_bbox',
        'pupil_blocked', 'lick_detection'
    ]

    qcs = [
        'task', 'behavior', 'videoLeft', 'videoRight', 'videoBody', 'dlcLeft',
        'dlcRight', 'dlcBody'
    ]

    l = []
    for q in qcs:
        try:
            if det[q] == 'FAIL':

                if 'dlc' in q:
                    l.append('\n')
                    l.append(q + ':' + str(det[q]) + '-->')
                    video_type = q[3:]
                    for dlc_qc in dlc_qcs:
                        w = f'_dlc{video_type}_{dlc_qc}'
                        if not det[w]:
                            l.append(w + ':' + str(det[w]) + ',')
                else:
                    l.append(q + ':' + str(det[q]) + ',')
        except:
            continue

    s2 = ' '.join(l)

    plt.suptitle(s1 + ', DLC version: ' + str(task['version']) + ' \n ' + s2,
                 backgroundcolor='white',
                 fontsize=6)
    plt.tight_layout(rect=[0, 0.03, 1, 0.95])
    #plt.tight_layout()
    plt.savefig(f'/home/mic/reproducible_dlc/overviewJune/{eid}.png')
    plt.close()