def test_mergeROIs():
    roi1 = corticalmapping.core.ImageAnalysis.WeightedROI(
        np.arange(9).reshape((3, 3)))
    roi2 = corticalmapping.core.ImageAnalysis.WeightedROI(
        np.arange(1, 10).reshape((3, 3)))

    merged_ROI = sca.merge_weighted_rois(roi1, roi2)
    merged_ROI2 = sca.merge_binary_rois(roi1, roi2)

    assert (np.array_equal(merged_ROI.get_weighted_mask(),
                           np.arange(1, 18, 2).reshape((3, 3))))
    assert (np.array_equal(merged_ROI2.get_binary_mask(), np.ones((3, 3))))
def test_getSparseNoiseOnsetIndex():
    allOnsetInd, onsetIndWithLocationSign = sca.get_sparse_noise_onset_index(
        ft.loadFile(sparseNoiseDisplayLogPath))
    # print list(allOnsetInd[0:10])
    # print onsetIndWithLocationSign[2][0]
    assert (list(allOnsetInd[0:10]) == [0, 6, 12, 18, 24, 30, 36, 42, 48, 54])
    assert (np.array_equal(onsetIndWithLocationSign[2][0], np.array([0.,
                                                                     70.])))
def test_SpatialTemporalReceptiveField():
    locations = [[3.0, 4.0], [3.0, 5.0], [2.0, 4.0], [2.0, 5.0], [3.0, 4.0],
                 [3.0, 5.0], [2.0, 4.0], [2.0, 5.0]]
    signs = [1, 1, 1, 1, -1, -1, -1, -1]
    traces = [[np.arange(4)], [np.arange(1, 5)], [np.arange(2, 6)],
              [np.arange(3, 7)], [np.arange(5, 9)], [np.arange(6, 10)],
              [np.arange(7, 11)], [np.arange(8, 12)]]
    traces = [np.array(t) for t in traces]
    time = np.arange(4, 8)
    STRF = sca.SpatialTemporalReceptiveField(locations, signs, traces, time)
    assert (STRF.data['traces'][0][0][1] == 8)
    assert (STRF.data['sign'][4] == 1)
    assert (np.array_equal(STRF.get_locations()[2], np.array([3., 4., -1.])))
    newLocations = [[location[0] + 1, location[1] + 1]
                    for location in locations[0:4]]
    newSigns = [1, 1, 1, 1]
    STRF.add_traces(newLocations, newSigns, traces[0:4])
    assert (STRF.data['traces'][7][1][2] == 4)
def test_SpatialTemporalReceptiveField_IO():
    locations = [[3.0, 4.0], [3.0, 5.0], [2.0, 4.0], [2.0, 5.0], [3.0, 4.0],
                 [3.0, 5.0], [2.0, 4.0], [2.0, 5.0]]
    signs = [1, 1, 1, 1, -1, -1, -1, -1]
    traces = [[np.arange(4)], [np.arange(1, 5)], [np.arange(2, 6)],
              [np.arange(3, 7)], [np.arange(5, 9)], [np.arange(6, 10)],
              [np.arange(7, 11)], [np.arange(8, 12)]]
    time = np.arange(4, 8)

    STRF = sca.SpatialTemporalReceptiveField(locations, signs, traces, time)
    if os.path.isfile(testH5Path): os.remove(testH5Path)
    testFile = h5py.File(testH5Path)
    STRFGroup = testFile.create_group('spatial_temporal_receptive_field')
    STRF.to_h5_group(STRFGroup)
    testFile.close()

    h5File = h5py.File(testH5Path)
    STRF = sca.SpatialTemporalReceptiveField.from_h5_group(
        h5File['spatial_temporal_receptive_field'])
    h5File.close()
    assert (STRF.data['traces'][3][0][1] == 7)
def test_SpatialReceptiveField_interpolate():
    SRF = sca.SpatialReceptiveField(np.random.rand(5, 5),
                                    np.arange(5)[::-1], np.arange(5))
    SRF.interpolate(5)
    assert (SRF.get_weighted_mask().shape == (20, 20))
def test_SpatialReceptiveField_thresholdReceptiveField():
    SRF = sca.SpatialReceptiveField(
        np.arange(9).reshape((3, 3)), np.arange(3), np.arange(3))
    thresholdedSRF = SRF.threshold_receptive_field(4)
    assert (np.array_equal(thresholdedSRF.weights, np.arange(4, 9)))
def test_SpatialReceptiveField():
    SRF = sca.SpatialReceptiveField(
        np.arange(9).reshape((3, 3)), np.arange(3), np.arange(3))
    assert (np.array_equal(SRF.weights, np.arange(1, 9)))
    roi_lst = [r for r in roi_lst if r[:4] == 'roi_']
    roi_lst.sort()

    for roi_ind, roi_n in enumerate(roi_lst):
        print('roi: {} / {}'.format(roi_ind + 1, len(roi_lst)))

        curr_trace, _ = dt.get_single_trace(nwb_f=nwb_f,
                                            plane_n=plane_n,
                                            roi_n=roi_n)
        if np.min(curr_trace) < bias:
            add_to_trace = -np.min(curr_trace) + bias
        else:
            add_to_trace = 0.

        curr_strf = sca.get_strf_from_nwb(h5_grp=strf_grp[plane_n],
                                          roi_ind=roi_ind,
                                          trace_type=trace_type)
        curr_strf_dff = curr_strf.get_local_dff_strf(
            is_collaps_before_normalize=True, add_to_trace=add_to_trace)
        # v_min, v_max = curr_strf_dff.get_data_range()

        rf_on, rf_off = curr_strf_dff.get_zscore_receptive_field(
            timeWindow=t_window)
        f = plt.figure(figsize=(15, 4))
        f.suptitle('{}: t_window: {}'.format(roi_n, t_window))
        ax_on = f.add_subplot(121)
        rf_on.plot_rf(plot_axis=ax_on,
                      is_colorbar=True,
                      cmap='RdBu_r',
                      vmin=zscore_range[0],
                      vmax=zscore_range[1])
예제 #9
0
                    plane_traces[:, curr_frame_start:curr_frame_end])
                probe_trigger_ts.append(curr_probe_ts)

        plane_roi_traces.append(np.array(probe_traces))
        trigger_ts.append(probe_trigger_ts)
        print('probe: {} / {}; shape: {}'.format(probe_ind + 1, len(probe_ns),
                                                 np.array(probe_traces).shape))

    # plane_roi_traces = np.array(plane_roi_traces)

    print('saving ...')
    for roi_ind in range(roi_num):

        print "roi: {} / {}".format(roi_ind + 1, roi_num)
        curr_unit_traces = [pt[:, roi_ind, :] for pt in plane_roi_traces]
        curr_unit_traces = [list(t) for t in curr_unit_traces]
        curr_strf = sca.SpatialTemporalReceptiveField2(
            locations=probe_locations,
            signs=probe_signs,
            traces=curr_unit_traces,
            trigger_ts=trigger_ts,
            time=plane_t,
            name='roi_{:04d}'.format(roi_ind),
            trace_data_type=trace_source)

        curr_strf_grp = plane_strf_grp.create_group(
            'strf_roi_{:04d}'.format(roi_ind))
        curr_strf.to_h5_group(curr_strf_grp)

nwb_f.close()
                                                    z_thr_rel=analysis_params['rf_z_thr_rel'])

                _, rf_off_new = dt.get_rf_properties(srf=srf_off,
                                                     polarity='negative',
                                                     sigma=analysis_params['gaussian_filter_sigma_rf'],
                                                     interpolate_rate=analysis_params['interpolate_rate_rf'],
                                                     z_thr_abs=analysis_params['rf_z_thr_abs'],
                                                     z_thr_rel=analysis_params['rf_z_thr_rel'])
            else:
                raise ValueError

            rf_on_mask = rf_on_new.get_weighted_mask()
            rf_off_mask = rf_off_new.get_weighted_mask()
            rf_onoff_new = sca.SpatialReceptiveField(mask=np.max([rf_on_mask, rf_off_mask], axis=0),
                                                     altPos=rf_on_new.altPos,
                                                     aziPos=rf_on_new.aziPos,
                                                     sign='ON_OFF',
                                                     thr=analysis_params['rf_z_thr_abs'])

            curr_s2_grp = s2_grp.create_group(roi_row['roi_n'])
            rf_onoff_new.to_h5_group(curr_s2_grp)

            curr_s1_on_grp = s1_on_grp.create_group(roi_row['roi_n'])
            rf_on_new.to_h5_group(curr_s1_on_grp)

            curr_s1_off_grp = s1_off_grp.create_group(roi_row['roi_n'])
            rf_off_new.to_h5_group(curr_s1_off_grp)

    # positive S1 ON
    s1_on_df = subdf[(subdf['rf_{}_on_peak_z'.format(response_dir)] >= analysis_params['rf_z_thr_abs']) &
                     (subdf['rf_{}_off_peak_z'.format(response_dir)] < analysis_params['rf_z_thr_abs'])].reset_index()
os_diff_off = []
for roi_i, roi_row in osdf.iterrows():
    date = int(roi_row['date'])
    mid = roi_row['mouse_id']
    plane_n = roi_row['plane_n']
    roi_n = roi_row['roi_n']

    map_fn = '{}_{}_{}_{}'.format(date, mid, plane_n, response_dir)
    map_f = h5py.File(os.path.join(rf_maps_folder, map_fn + '.hdf5'), 'r')

    on_grp = map_f['{}_ON'.format(map_fn)]
    off_grp = map_f['{}_OFF'.format(map_fn)]

    dire = roi_row['dgc_{}_{}_{}_{}'.format(response_dir, dire_type, dire_pp,
                                            response_type)]
    ori = sca.dire2ori(dire)

    if roi_n in on_grp.keys() and roi_n in off_grp.keys():
        rf_on = sca.SpatialReceptiveField.from_h5_group(on_grp[roi_n])
        rf_off = sca.SpatialReceptiveField.from_h5_group(off_grp[roi_n])
        c_alt_on, c_azi_on = rf_on.get_weighted_rf_center()
        c_alt_off, c_azi_off = rf_off.get_weighted_rf_center()

        onoff_ang = np.arctan((c_alt_on - c_alt_off) / (c_azi_on - c_azi_off))
        onoff_ang = onoff_ang * 180. / np.pi
        onoff_ang = sca.dire2ori(onoff_ang)

        curr_diff = abs(onoff_ang - ori)
        if curr_diff > 90.:
            curr_diff = 180 - curr_diff
예제 #12
0
            curr_frame_end = curr_frame_start + plane_chunk_frame_dur
            if curr_frame_start >= 0 and curr_frame_end <= len(plane_trace_ts):
                probe_traces.append(
                    plane_traces[:, curr_frame_start:curr_frame_end])

        plane_roi_traces.append(np.array(probe_traces))
        print('probe: {} / {}; shape: {}'.format(probe_ind + 1, len(probe_ns),
                                                 np.array(probe_traces).shape))

    # plane_roi_traces = np.array(plane_roi_traces)

    print('saving ...')
    for roi_ind in range(roi_num):

        print "roi: {} / {}".format(roi_ind + 1, roi_num)
        curr_unit_traces = [pt[:, roi_ind, :] for pt in plane_roi_traces]
        curr_unit_traces = [list(t) for t in curr_unit_traces]
        curr_strf = sca.SpatialTemporalReceptiveField(
            probe_locations,
            probe_signs,
            curr_unit_traces,
            plane_t,
            name='roi_{:04d}'.format(roi_ind),
            trace_data_type=trace_source)

        curr_strf_grp = plane_strf_grp.create_group(
            'strf_roi_{:04d}'.format(roi_ind))
        curr_strf.to_h5_group(curr_strf_grp)

nwb_f.close()