def process_one_nwb_for_multi_thread(inputs):

    nwb_path, params, columns, save_folder, t0 = inputs
    nwb_fn = os.path.splitext(os.path.split(nwb_path)[1])[0]
    save_path = os.path.join(save_folder, nwb_fn + '.xlsx')

    if os.path.isfile(save_path):
        print('\tt: {:5.0f} minutes, {} , excel table already exists, skip.'.
              format((time.time() - t0) / 60., nwb_fn))
        return None
    else:
        nwb_f = h5py.File(nwb_path, 'r')

        plane_ns = [
            k for k in nwb_f['processing'].keys()
            if k[0:16] == 'rois_and_traces_'
        ]
        plane_ns = [k[16:] for k in plane_ns]
        plane_ns.sort()
        # print('total plane number: {}'.format(len(plane_ns)))

        total_roi_num = 10000
        big_array = np.zeros((total_roi_num, len(columns)), dtype=np.float64)
        big_array[:] = np.nan
        df = pd.DataFrame(data=big_array, columns=columns)

        curr_row_ind = 0
        for plane_n in plane_ns:
            print('\tt: {:5.0f} minutes, {} / {}, processing ...'.format(
                (time.time() - t0) / 60., nwb_fn, plane_n))
            roi_ns = nwb_f[
                'processing/rois_and_traces_{}/ImageSegmentation/imaging_plane/roi_list'
                .format(plane_n)].value
            roi_ns = [r.encode('utf-8') for r in roi_ns if r[0:4] == 'roi_']
            roi_ns.sort()

            for roi_i, roi_n in enumerate(roi_ns):
                # print('\t\t\troi: {} / {}'.format(roi_i+1, len(roi_ns)))
                roi_properties, _, _, _, _, _, _, _, _, _, _, _, _, _ = \
                    dt.get_everything_from_roi(nwb_f=nwb_f, plane_n=plane_n, roi_n=roi_n, params=params)
                for rp_name, rp_value in roi_properties.items():
                    df.loc[curr_row_ind, rp_name] = rp_value

                curr_row_ind += 1

        df = df[0:curr_row_ind]

        save_path = os.path.join(save_folder, nwb_fn + '.xlsx')
        if os.path.isfile(save_path):
            os.remove(save_path)

        with pd.ExcelWriter(save_path, mode='w') as writer:
            df.to_excel(writer, sheet_name='sheet1')
def process_one_nwb_for_multi_thread(inputs):

    nwb_path, params, columns, save_folder, t0, nwb_i, nwb_f_num, is_overwrite = inputs
    nwb_fn = os.path.splitext(os.path.split(nwb_path)[1])[0]

    nwb_f = h5py.File(nwb_path, 'r')

    plane_ns = [
        k for k in nwb_f['processing'].keys() if k[0:16] == 'rois_and_traces_'
    ]
    plane_ns = [k[16:] for k in plane_ns]
    plane_ns.sort()
    # print('total plane number: {}'.format(len(plane_ns)))

    for plane_n in plane_ns:
        print('\tt: {:5.0f} minutes, processing {}, {} / {}, {} ...'.format(
            (time.time() - t0) / 60., nwb_fn, nwb_i + 1, nwb_f_num, plane_n))

        save_fn = '_'.join(nwb_fn.split('_')[0:2]) + '_' + plane_n + '.xlsx'
        save_path = os.path.join(save_folder, save_fn)
        if os.path.isfile(save_path):

            if is_overwrite:  # overwrite existing xlsx files
                print('\t{}, file already exists. Overwirite.'.format(
                    os.path.split(save_path)[1]))
                os.remove(save_path)

            else:  # do not overwrite existing xlsx files
                print('\t{}, file already exists. Skip.'.format(
                    os.path.split(save_path)[1]))
                return

        roi_ns = nwb_f[
            'processing/rois_and_traces_{}/ImageSegmentation/imaging_plane/roi_list'
            .format(plane_n)].value
        roi_ns = [r.encode('utf-8') for r in roi_ns if r[0:4] == 'roi_']
        roi_ns.sort()

        df = pd.DataFrame(np.nan, index=range(len(roi_ns)), columns=columns)

        for roi_i, roi_n in enumerate(roi_ns):
            # print('\t\t\troi: {} / {}'.format(roi_i+1, len(roi_ns)))
            roi_properties, _, _, _, _, _, _, _, _, _, _, _, _, _ = \
                dt.get_everything_from_roi(nwb_f=nwb_f, plane_n=plane_n, roi_n=roi_n, params=params)
            for rp_name, rp_value in roi_properties.items():
                df.loc[roi_i, rp_name] = rp_value

        with pd.ExcelWriter(save_path, mode='w') as writer:
            df.to_excel(writer, sheet_name='sheet1')
    # positive spatial receptive fields
    srf_on, srf_off = strf_dff.get_zscore_receptive_field(timeWindow=[0., 0.5])

    srf = srf_on.gaussian_filter(sigma=1.)
    srf = srf.interpolate(ratio=10.)

    print(np.max(srf.weights))


f_o = h5py.File(fn_original, 'r')
strf_o = sca.SpatialTemporalReceptiveField.from_h5_group(
    f_o['analysis/STRFs/plane0/strf_roi_{:04d}'.format(roi_ind)])
print_peak_z(strf_o)
# print(strf_o.data['traces'][0][5])
# print(strf_o.time)
f_o.close()

f_r = h5py.File(os.path.join('repacked', fn_repacked), 'r')
strf_r = dt.get_strf(f_r,
                     plane_n='plane0',
                     roi_ind=0,
                     trace_type='sta_f_center_subtracted')
print_peak_z(strf_r)
# print(strf_r.data['traces'][0][5])
# print(strf_r.time)

roi_properties, _, _, _, _, _, _, _, _, _, _, _, _, _ = \
                        dt.get_everything_from_roi(nwb_f=f_r, plane_n='plane0', roi_n=roi_n, params=params)
print(roi_properties['rf_pos_on_peak_z'])

f_r.close()
            s2_grp = save_f.create_group(table_fn[0:-5] +
                                         '_{}_ONOFF'.format(response_dir))
            s1_on_grp = save_f.create_group(table_fn[0:-5] +
                                            '_{}_ON'.format(response_dir))
            s1_off_grp = save_f.create_group(table_fn[0:-5] +
                                             '_{}_OFF'.format(response_dir))

            for roi_i, roi_row in s2_df.iterrows():

                print('\t s2 receptive fields, {}, {} / {} ...'.format(
                    roi_row['roi_n'], roi_i + 1, len(s2_df)))

                if response_dir == 'pos':
                    _, _, _, srf_on, srf_off, _, _, _, _, _, _, _, _, \
                    _ = dt.get_everything_from_roi(nwb_f=nwb_f,
                                                   plane_n=plane_n,
                                                   roi_n=roi_row['roi_n'],
                                                   params=analysis_params)

                    _, rf_on_new = dt.get_rf_properties(
                        srf=srf_on,
                        polarity='positive',
                        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'])

                    _, rf_off_new = dt.get_rf_properties(
                        srf=srf_off,
                        polarity='positive',
                        sigma=analysis_params['gaussian_filter_sigma_rf'],