Ejemplo n.º 1
0
def main(model_name='parameter_sweep', exp_name='default', azimuth=12, snr=0.2, freq_bands=128, max_freq=20000, elevations=25, mean_subtracted_map=True, ear='ipsi', normalization_type='sum_1', clean=False):
    """ TODO
    """
    logger = logging.getLogger(__name__)
    logger.info('Parameter Sweep Experiment.')

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################
    # participant_numbers = np.array([1, 2, 3, 8, 9, 10, 11,
    #                                 12, 15, 17, 18, 19, 20, 21, 27, 28, 33, 40])

    participant_numbers = np.array([1, 2, 3, 8, 9, 10, 11,
                                    12, 15, 17, 18, 19, 20,
                                    21, 27, 28, 33, 40, 44,
                                    48, 50, 51, 58, 59, 60,
                                    61, 65, 119, 124, 126,
                                    127, 131, 133, 134, 135,
                                    137, 147, 148, 152, 153,
                                    154, 155, 156, 158, 162,
                                    163, 165])
    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)

    sigma_smoothing_vals = np.arange(1, 3.0, 0.1)
    sigma_gauss_norm_vals = np.arange(1, 3.0, 0.1)
    ########################################################################
    ########################################################################

    # create unique experiment name
    exp_name_str = hp.create_exp_name([exp_name, normalization_type, mean_subtracted_map, time_window, int(
        snr * 100), freq_bands, max_freq, (azimuth - 12) * 10, normalize, len(elevations), ear])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load
    if not clean and exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with exp_file.open('rb') as f:
            logger.info('Reading model data from file')
            [scores, sigma_smoothing_vals, sigma_gauss_norm_vals] = pickle.load(f)
    else:

        scores = np.zeros((sigma_smoothing_vals.shape[0], sigma_gauss_norm_vals.shape[0], 3))

        for i_par, par in enumerate(participant_numbers):

            # create or read the data
            psd_all_c, psd_all_i = generateData.create_data(
                freq_bands, par, snr, normalize, azimuth, time_window, max_freq=max_freq, diff_noise=False)

            # Take only given elevations
            psd_all_c = psd_all_c[:, elevations, :]
            psd_all_i = psd_all_i[:, elevations, :]

            ### Get different noise data ###
            psd_all_c_diff_noise, psd_all_i_diff_noise = generateData.create_data(
                freq_bands, par, snr, normalize, azimuth, time_window, max_freq=max_freq, diff_noise=True)

            # Take only given elevations
            psd_all_c_diff_noise = psd_all_c_diff_noise[:, elevations, :]
            psd_all_i_diff_noise = psd_all_i_diff_noise[:, elevations, :]

            for i_smooth, sigma_smooth in enumerate(sigma_smoothing_vals):
                for i_gauss, sigma_gauss in enumerate(sigma_gauss_norm_vals):

                    # filter data and integrate it
                    psd_mono, psd_mono_mean, psd_binaural, psd_binaural_mean = hp.process_inputs(
                        psd_all_i, psd_all_c, ear, normalization_type, sigma_smooth, sigma_gauss)

                    # create map from defined processed data
                    if mean_subtracted_map:
                        learned_map = psd_binaural_mean.mean(0)
                    else:
                        learned_map = psd_binaural.mean(0)

                    # filter data and integrate it
                    psd_mono_diff_noise, psd_mono_mean_diff_noise, psd_binaural_diff_noise, psd_binaural_mean_diff_noise = hp.process_inputs(
                        psd_all_i_diff_noise, psd_all_c_diff_noise, ear, normalization_type, sigma_smooth, sigma_gauss)

                    # # localize the sounds and save the results
                    # x_mono[i_par, :, :, :], y_mono[i_par, :] = hp.localize_sound(psd_mono, learned_map)
                    #
                    # # localize the sounds and save the results
                    # x_mono_mean[i_par, :, :, :], y_mono_mean[i_par, :, :] = hp.localize_sound(psd_mono_mean, learned_map)
                    #
                    # # localize the sounds and save the results
                    # x_bin[i_par, :, :, :], y_bin[i_par, :, :] = hp.localize_sound(psd_binaural, learned_map)

                    # localize the sounds and save the results
                    x_test, y_test = hp.localize_sound(psd_binaural_diff_noise, learned_map)
                    x_test, y_test = hp_vis.scale_v(x_test, y_test, len(elevations))
                    scores[i_smooth, i_gauss, :] += hp.get_localization_coefficients_score(x_test, y_test)
        # get the mean scores over participants
        scores = scores / len(participant_numbers)

        # create Path
        exp_path.mkdir(parents=True, exist_ok=True)
        with exp_file.open('wb') as f:
            logger.info('Creating model file')
            pickle.dump([scores, sigma_smoothing_vals, sigma_gauss_norm_vals], f)
Ejemplo n.º 2
0
def main(save_figs=False,
         save_type='svg',
         model_name='localize_all_participant',
         exp_name='all_participants_default',
         azimuth=12,
         snr=0.2,
         freq_bands=24,
         max_freq=20000,
         elevations=25,
         mean_subtracted_map=True,
         ear='ipsi',
         normalization_type='sum_1',
         sigma_smoothing=0,
         sigma_gauss_norm=1):

    logger = logging.getLogger(__name__)
    logger.info('Showing localization results for a single participant')

    # make sure save type is given
    if not save_type or len(save_type) == 0:
        save_type = 'svg'

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################
    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)

    ########################################################################
    ########################################################################

    exp_name_str = hp.create_exp_name([
        exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm,
        mean_subtracted_map, time_window,
        int(snr * 100), freq_bands, max_freq, (azimuth - 12) * 10, normalize,
        len(elevations), ear
    ])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load
    if exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with open(exp_file.as_posix(), 'rb') as f:
            logger.info('Reading model data from file')
            [
                localization_results_binaural, localization_results_monaural,
                q_ele_all, r_ipsi_all
            ] = pickle.load(f)

        fig = plt.figure(figsize=(20, 5))
        # plt.suptitle('Single Participant')
        # Monoaural Data (Ipsilateral), No Mean Subtracted
        ax1 = fig.add_subplot(1, 4, 1)
        ax2 = fig.add_subplot(1, 4, 2)
        ax3 = fig.add_subplot(1, 4, 3)
        ax4 = fig.add_subplot(1, 4, 4)

        # get the right values
        x_mono = localization_results_monaural[:, :, :, 0]
        y_mono = localization_results_monaural[:, :, :, 1]
        x_mono_mean = localization_results_monaural[:, :, :, 0]
        y_mono_mean = localization_results_monaural[:, :, :, 2]

        x_bin = localization_results_binaural[:, :, :, 0]
        y_bin = localization_results_binaural[:, :, :, 1]
        x_bin_mean = localization_results_binaural[:, :, :, 0]
        y_bin_mean = localization_results_binaural[:, :, :, 2]

        # plot regression line for each participant
        for i_par in range(x_mono.shape[0]):

            hp_vis.plot_localization_result(x_mono[i_par],
                                            y_mono[i_par],
                                            ax1,
                                            SOUND_FILES,
                                            scale_values=True,
                                            linear_reg=True,
                                            scatter_data=False)
            ax1.set_title('Monoaural')
            hp_vis.set_axis(ax1, len(elevations))
            ax1.set_ylabel('Estimated Elevation [deg]')
            ax1.set_xlabel('True Elevation [deg]')

            # Monoaural Data (Ipsilateral), Mean Subtracted
            hp_vis.plot_localization_result(x_mono_mean[i_par],
                                            y_mono_mean[i_par],
                                            ax2,
                                            SOUND_FILES,
                                            scale_values=True,
                                            linear_reg=True,
                                            scatter_data=False)
            ax2.set_title('Mono - Prior')
            hp_vis.set_axis(ax2, len(elevations))
            ax2.set_xlabel('True Elevation [deg]')

            # Binaural Data (Ipsilateral), No Mean Subtracted

            hp_vis.plot_localization_result(x_bin[i_par],
                                            y_bin[i_par],
                                            ax3,
                                            SOUND_FILES,
                                            scale_values=True,
                                            linear_reg=True,
                                            scatter_data=False)
            ax3.set_title('Binaural')
            hp_vis.set_axis(ax3, len(elevations))
            ax3.set_xlabel('True Elevation [deg]')

            # Binaural Data (Ipsilateral), Mean Subtracted

            hp_vis.plot_localization_result(x_bin_mean[i_par],
                                            y_bin_mean[i_par],
                                            ax4,
                                            SOUND_FILES,
                                            scale_values=True,
                                            linear_reg=True,
                                            scatter_data=False)
            ax4.set_title('Bin - Prior')
            hp_vis.set_axis(ax4, len(elevations))
            ax4.set_xlabel('True Elevation [deg]')

        # plot a common regression line
        x_mono_ = np.reshape(
            x_mono, (x_mono.shape[0] * x_mono.shape[1], x_mono.shape[2]))
        y_mono_ = np.reshape(
            y_mono, (y_mono.shape[0] * y_mono.shape[1], y_mono.shape[2]))

        x_mono_mean_ = np.reshape(x_mono_mean,
                                  (x_mono_mean.shape[0] * x_mono_mean.shape[1],
                                   x_mono_mean.shape[2]))
        y_mono_mean_ = np.reshape(y_mono_mean,
                                  (y_mono_mean.shape[0] * y_mono_mean.shape[1],
                                   y_mono_mean.shape[2]))

        x_bin_ = np.reshape(x_bin,
                            (x_bin.shape[0] * x_bin.shape[1], x_bin.shape[2]))
        y_bin_ = np.reshape(y_bin,
                            (y_bin.shape[0] * y_bin.shape[1], y_bin.shape[2]))

        x_bin_mean_ = np.reshape(
            x_bin_mean,
            (x_bin_mean.shape[0] * x_bin_mean.shape[1], x_bin_mean.shape[2]))
        y_bin_mean_ = np.reshape(
            y_bin_mean,
            (y_bin_mean.shape[0] * y_bin_mean.shape[1], y_bin_mean.shape[2]))

        hp_vis.plot_localization_result(x_mono_,
                                        y_mono_,
                                        ax1,
                                        SOUND_FILES,
                                        scale_values=True,
                                        linear_reg=True,
                                        disp_values=True,
                                        scatter_data=False,
                                        reg_color="black")
        hp_vis.plot_localization_result(x_mono_mean_,
                                        y_mono_mean_,
                                        ax2,
                                        SOUND_FILES,
                                        scale_values=True,
                                        linear_reg=True,
                                        disp_values=True,
                                        scatter_data=False,
                                        reg_color="black")
        hp_vis.plot_localization_result(x_bin_,
                                        y_bin_,
                                        ax3,
                                        SOUND_FILES,
                                        scale_values=True,
                                        linear_reg=True,
                                        disp_values=True,
                                        scatter_data=False,
                                        reg_color="black")
        hp_vis.plot_localization_result(x_bin_mean_,
                                        y_bin_mean_,
                                        ax4,
                                        SOUND_FILES,
                                        scale_values=True,
                                        linear_reg=True,
                                        disp_values=True,
                                        scatter_data=False,
                                        reg_color="black")

        plt.tight_layout()

        if save_figs:
            fig_save_path = ROOT / 'reports' / 'figures' / exp_name_str / model_name
            if not fig_save_path.exists():
                fig_save_path.mkdir(parents=True, exist_ok=True)
            plt.savefig(
                (fig_save_path / (model_name + '_' + exp_name +
                                  '_localization.' + save_type)).as_posix(),
                dpi=300)

        else:
            plt.show()
    else:
        logger.error('No data set found. Run model first!')
        logger.error(exp_file)
Ejemplo n.º 3
0
def main(save_figs=False, save_type='svg', model_name='parameter_sweep', exp_name='default', azimuth=12, snr=0.2, freq_bands=128, max_freq=20000, elevations=25, mean_subtracted_map=True, ear='ipsi', normalization_type='sum_1', clean=False):

    logger = logging.getLogger(__name__)
    logger.info('Showing parameter sweep results')

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################

    # make sure save type is given
    if not save_type or len(save_type) == 0:
        save_type = 'svg'

    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)


    ########################################################################
    ########################################################################

    # create unique experiment name
    exp_name_str = hp.create_exp_name([exp_name, normalization_type, mean_subtracted_map, time_window, int(
        snr * 100), freq_bands, max_freq, (azimuth - 12) * 10, normalize, len(elevations), ear])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load
    if exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with open(exp_file.as_posix(), 'rb') as f:
            logger.info('Reading model data from file')
            [scores,sigma_smoothing_vals,sigma_gauss_norm_vals] = pickle.load(f)

        fig = plt.figure(figsize=(20, 5))
        axes = fig.subplots(1,3,sharex=True,sharey=True)
        ax = axes[0]
        p = ax.pcolormesh(sigma_smoothing_vals, sigma_gauss_norm_vals, np.squeeze(scores[:, :, 0]),linewidth=0,rasterized=True)
        p.set_edgecolor('face')
        ax.set_xlabel(r'Gauss Normalization $\sigma$')
        ax.set_ylabel(r'Smoothing $\sigma$')
        # ax.set_title('Gain')
        cbar = plt.colorbar(p,ax=ax)
        cbar.set_label('Gain', rotation=270)
        cbar.ax.get_yaxis().labelpad = 15

        ax = axes[1]
        p = ax.pcolormesh(sigma_smoothing_vals, sigma_gauss_norm_vals, np.squeeze(scores[:, :, 1]),linewidth=0,rasterized=True)
        p.set_edgecolor('face')
        ax.set_xlabel(r'Gauss Normalization $\sigma$')
        # ax.set_ylabel('Smoothing Sigma')
        # ax.set_title('Bias')
        cbar = plt.colorbar(p,ax=ax)
        cbar.set_label('Bias', rotation=270)
        cbar.ax.get_yaxis().labelpad = 15

        ax = axes[2]
        p = ax.pcolormesh(sigma_smoothing_vals, sigma_gauss_norm_vals, np.squeeze(scores[:, :, 2]),linewidth=0,rasterized=True)
        p.set_edgecolor('face')
        ax.set_xlabel(r'Gauss Normalization $\sigma$')
        # ax.set_ylabel('Smoothing Sigma')
        # ax.set_title('Score')
        cbar = plt.colorbar(p,ax=ax)
        cbar.set_label('Score', rotation=270)
        cbar.ax.get_yaxis().labelpad = 15

        plt.tight_layout()

        if save_figs:
            exp_name_str = hp.create_exp_name([exp_name, normalization_type, 0, 1, mean_subtracted_map, time_window, int(
            snr * 100), freq_bands, max_freq, (azimuth - 12) * 10, normalize, len(elevations), ear])
            fig_save_path = ROOT / 'reports' / 'figures' / exp_name_str / model_name
            if not fig_save_path.exists():
                fig_save_path.mkdir(parents=True, exist_ok=True)
            logger.info('Saving figures to ' + fig_save_path.as_posix())
            plt.savefig((fig_save_path / (model_name + '_' + exp_name + '_sweep.' + save_type)).as_posix(), dpi=300)
        else:
            plt.show()
    else:
        logger.error('No data set found. Run model first!')
        logger.error(exp_file)
Ejemplo n.º 4
0
def main(save_figs=False,
         save_type='svg',
         model_name='elevation_spectra_maps',
         exp_name='unfiltered',
         azimuth=12,
         participant_numbers=None,
         snr=0.2,
         freq_bands=24,
         max_freq=20000,
         elevations=25,
         clean=False):

    logger = logging.getLogger(__name__)
    logger.info('Plotting elevation spectra map for different sounds')

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################
    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)

    # make sure save type is given
    if not save_type or len(save_type) == 0:
        save_type = 'svg'

    # if participant_numbers is not given we use all of them
    if not participant_numbers:
        participant_numbers = np.array([
            1, 2, 3, 8, 9, 10, 11, 12, 15, 17, 18, 19, 20, 21, 27, 28, 33, 40,
            44, 48, 50, 51, 58, 59, 60, 61, 65, 119, 124, 126, 127, 131, 133,
            134, 135, 137, 147, 148, 152, 153, 154, 155, 156, 158, 162, 163,
            165
        ])

        exp_name_str = hp.create_exp_name([
            exp_name, time_window,
            int(snr * 100), freq_bands, max_freq,
            len(participant_numbers), (azimuth - 12) * 10, normalize,
            len(elevations)
        ])
        exp_path = ROOT / 'models' / model_name
        exp_file = exp_path / exp_name_str
    else:
        # participant_numbers are given. need to be cast to int array
        participant_numbers = np.array(
            [int(i) for i in participant_numbers.split(',')])
        print(participant_numbers)

        exp_name_str = hp.create_exp_name([
            exp_name, time_window,
            int(snr * 100), freq_bands, max_freq, participant_numbers,
            (azimuth - 12) * 10, normalize,
            len(elevations)
        ])
        exp_path = ROOT / 'models' / model_name
        exp_file = exp_path / exp_name_str

    ########################################################################
    ########################################################################

    fig_size = (7, 5)
    # fig_size = (20, 14)

    formatter = hp_vis.ERBFormatter(20, max_freq, unit='', places=0)

    # check if model results exist already and load
    if exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with open(exp_file.as_posix(), 'rb') as f:
            logger.info('Reading model data from file')
            [ipsi_maps, contra_maps] = pickle.load(f)

        ipsi_maps = ipsi_maps[:, :, elevations, :]
        contra_maps = contra_maps[:, :, elevations, :]

        for i_par, par in enumerate(participant_numbers):

            for i_sound, sound in enumerate(SOUND_FILES):
                sound = sound.name.split('.')[0]
                # IPSI
                fig = plt.figure(figsize=fig_size)
                ax = fig.add_subplot(1, 1, 1)
                ax.set_title(sound)
                # ax.imshow(np.squeeze(ipsi_maps[i_par, i_sound]),interpolation = 'bilinear')
                data = np.squeeze(ipsi_maps[i_par, i_sound])
                # ax.pcolormesh(np.squeeze(ipsi_maps[i_par, i_sound]),shading='gouraud',linewidth=0,rasterized=True)
                c = ax.pcolormesh(np.linspace(0, 1, data.shape[1]),
                                  np.linspace(-45, 90, data.shape[0]),
                                  data,
                                  shading='gouraud',
                                  linewidth=0,
                                  rasterized=True)
                plt.colorbar(c)
                ax.xaxis.set_major_formatter(formatter)
                ax.set_xlabel('Frequency [Hz]')
                ax.set_ylabel('Elevations [deg]')
                # ax.set_yticklabels(t[1:-1])

                if save_figs:
                    fig_save_path = ROOT / 'reports' / 'figures' / exp_name_str / model_name / (
                        'participant_' + str(par))
                    if not fig_save_path.exists():
                        fig_save_path.mkdir(parents=True, exist_ok=True)
                    path_final = (
                        fig_save_path /
                        (model_name + '_' + exp_name + '_raw_maps_ipsi_' +
                         str(sound) + '.' + save_type)).as_posix()
                    plt.savefig(path_final, dpi=300, transparent=True)
                    logger.info('Writing File :' + path_final)
                    plt.close()
                else:
                    plt.show()

                # CONTRA
                fig = plt.figure(figsize=fig_size)
                ax = fig.add_subplot(1, 1, 1)
                ax.set_title(sound)
                # ax.pcolormesh(np.squeeze(contra_maps[i_par, i_sound]), shading='gouraud', linewidth=0, rasterized=True)
                data = np.squeeze(contra_maps[i_par, i_sound])
                # ax.pcolormesh(np.squeeze(ipsi_maps[i_par, i_sound]),shading='gouraud',linewidth=0,rasterized=True)
                c = ax.pcolormesh(np.linspace(0, 1, data.shape[1]),
                                  np.linspace(-45, 90, data.shape[0]),
                                  data,
                                  shading='gouraud',
                                  linewidth=0,
                                  rasterized=True)
                plt.colorbar(c)
                ax.xaxis.set_major_formatter(formatter)
                ax.set_xlabel('Frequency [Hz]')
                ax.set_ylabel('Elevations [deg]')
                # ax.set_yticklabels(t[1:-1])

                if save_figs:
                    fig_save_path = ROOT / 'reports' / 'figures' / exp_name_str / model_name / (
                        'participant_' + str(par))
                    if not fig_save_path.exists():
                        fig_save_path.mkdir(parents=True, exist_ok=True)
                    path_final = (
                        fig_save_path /
                        (model_name + '_' + exp_name + '_raw_maps_contra_' +
                         str(sound) + '.' + save_type)).as_posix()
                    plt.savefig(path_final, dpi=300, transparent=True)
                    logger.info('Writing File :' + path_final)
                    plt.close()
                else:
                    plt.show()

    else:
        logger.error('No data set found. Run model first!')
        logger.error(exp_file)
Ejemplo n.º 5
0
def main(save_figs=False,
         save_type='svg',
         model_name='map_learning',
         exp_name='localization_all_maps',
         azimuth=12,
         snr=0.2,
         n_trials=100,
         freq_bands=24,
         max_freq=20000,
         elevations=25,
         mean_subtracted_map=True,
         ear='ipsi',
         normalization_type='sum_1',
         sigma_smoothing=0,
         sigma_gauss_norm=1,
         clean=False):

    logger = logging.getLogger(__name__)
    logger.info(
        'Showing map learning results for all participants over differently learned maps'
    )

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################

    # make sure save type is given
    if not save_type or len(save_type) == 0:
        save_type = 'svg'

    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)

    ########################################################################
    ########################################################################

    # create unique experiment name
    exp_name_str = hp.create_exp_name([
        exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm,
        mean_subtracted_map, time_window,
        int(snr * 100), freq_bands, max_freq, (azimuth - 12) * 10, normalize,
        len(elevations), ear, n_trials
    ])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load
    if exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with open(exp_file.as_posix(), 'rb') as f:
            logger.info('Reading model data from file', )
            [mono_res, mono_mean_res, bin_res, bin_mean_res,
             trial_used_ss] = pickle.load(f)

        # ALL participant
        fig = plt.figure(figsize=(20, 10))
        axes = fig.subplots(4, 3)

        # walk over maps
        for i_maps in range(4):
            # walk over linear regression values (gain,bias,score)
            for i in range(3):
                ax = axes[i_maps, i]

                trials = np.reshape(trial_used_ss[i_maps],
                                    (trial_used_ss[i_maps].shape[0] *
                                     trial_used_ss[i_maps].shape[1]))
                x = trials
                bins = 25

                data = np.reshape(
                    mono_res[i_maps],
                    (mono_res[i_maps].shape[0] * mono_res[i_maps].shape[1],
                     mono_res[i_maps].shape[2]))
                y = data[:, i]
                # sns.regplot(x=x, y=y, x_bins=bins, x_estimator=np.mean, fit_reg=True, logx=True, ax=ax, x_ci='ci', truncate=True, label='Monoaural')
                sns.regplot(x=x,
                            y=y,
                            x_bins=bins,
                            x_estimator=np.mean,
                            fit_reg=True,
                            logx=False,
                            order=7,
                            ax=ax,
                            x_ci='ci',
                            truncate=True,
                            label='Monoaural')

                data = np.reshape(mono_mean_res[i_maps],
                                  (mono_mean_res[i_maps].shape[0] *
                                   mono_mean_res[i_maps].shape[1],
                                   mono_mean_res[i_maps].shape[2]))
                y = data[:, i]
                # sns.regplot(x=x, y=y, x_bins=bins, x_estimator=np.mean, fit_reg=True, logx=True, ax=ax, x_ci='ci', truncate=True, label='Mono - Prior')
                sns.regplot(x=x,
                            y=y,
                            x_bins=bins,
                            x_estimator=np.mean,
                            fit_reg=True,
                            logx=False,
                            order=7,
                            ax=ax,
                            x_ci='ci',
                            truncate=True,
                            label='Mono - Prior')

                data = np.reshape(
                    bin_res[i_maps],
                    (bin_res[i_maps].shape[0] * bin_res[i_maps].shape[1],
                     bin_res[i_maps].shape[2]))
                y = data[:, i]
                # sns.regplot(x=x, y=y, x_bins=bins, x_estimator=np.mean, fit_reg=True, logx=True, ax=ax, x_ci='ci', truncate=True, label='Binaural')
                sns.regplot(x=x,
                            y=y,
                            x_bins=bins,
                            x_estimator=np.mean,
                            fit_reg=True,
                            logx=False,
                            order=7,
                            ax=ax,
                            x_ci='ci',
                            truncate=True,
                            label='Binaural')

                data = np.reshape(bin_mean_res[i_maps],
                                  (bin_mean_res[i_maps].shape[0] *
                                   bin_mean_res[i_maps].shape[1],
                                   bin_mean_res[i_maps].shape[2]))
                y = data[:, i]
                # sns.regplot(x=x, y=y, x_bins=bins, x_estimator=np.mean, fit_reg=True, logx=True, ax=ax, x_ci='ci', truncate=True, label='Bin - Prior')
                sns.regplot(x=x,
                            y=y,
                            x_bins=bins,
                            x_estimator=np.mean,
                            fit_reg=True,
                            logx=False,
                            order=7,
                            ax=ax,
                            x_ci='ci',
                            truncate=True,
                            label='Bin - Prior')
                ax.set_xlim([0, 200])

                if i == 2:
                    lgd = ax.legend(loc=(1.03, 0.04))

                if i == 0 or i == 2:
                    ax.set_ylim([-0.1, 1.3])

                if i == 1:
                    ax.set_ylim([-0.0, 12])

                # ax.scatter(trials, data[:, i])

                # do the labeling
                if i == 0:
                    ax.set_ylabel('Gain')
                if i == 1:
                    ax.set_ylabel('Bias')
                if i == 2:
                    ax.set_ylabel('Score')

                if i_maps == 3:
                    ax.set_xlabel('# of Presented Sounds')

        if save_figs:
            exp_name_str = hp.create_exp_name([
                exp_name, normalization_type, sigma_smoothing,
                sigma_gauss_norm, mean_subtracted_map, time_window,
                int(snr * 100), freq_bands, max_freq, (azimuth - 12) * 10,
                normalize,
                len(elevations), ear
            ])
            fig_save_path = ROOT / 'reports' / 'figures' / exp_name_str / model_name
            if not fig_save_path.exists():
                fig_save_path.mkdir(parents=True, exist_ok=True)
            logger.info('Saving figures to ' + fig_save_path.as_posix())
            plt.savefig(
                (fig_save_path / (model_name + '_' + exp_name +
                                  '_map_learning.' + save_type)).as_posix(),
                dpi=300,
                bbox_extra_artists=(lgd, ),
                bbox_inches='tight')

        else:
            plt.show()
    else:
        logger.error('No data set found. Run model first!')
        logger.error(exp_file)
Ejemplo n.º 6
0
def main(save_figs=False, save_type='svg', model_name='hrtf_creation', exp_name='single_participant', azimuth=12, participant_number=9, snr=0.2, freq_bands=24, max_freq=20000, elevations=25, mean_subtracted_map=True, ear='ipsi', normalization_type='sum_1', sigma_smoothing=0, sigma_gauss_norm=1):

    logger = logging.getLogger(__name__)
    logger.info('Showing HRTFs')

    # make sure save type is given
    if not save_type or len(save_type) == 0:
        save_type = 'svg'

    ### Set Parameters of Input Files ###

    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)

    ######################################

    # create unique experiment name
    exp_name_str = hp.create_exp_name([exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm, mean_subtracted_map, time_window, int(
        snr * 100), freq_bands, max_freq, participant_number, (azimuth - 12) * 10, normalize, len(elevations), ear])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load

    # set formatter for this plot
    formatter = hp_vis.ERBFormatter(20, max_freq, unit='', places=0)

    if exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with open(exp_file.as_posix(), 'rb') as f:
            logger.info('Reading model data from file')
            [hrtfs_i, hrtfs_c, freqs] = pickle.load(f)

        fig = plt.figure(figsize=(20, 5))
        plt.suptitle('Participant: ' + str(participant_number))

        # make sure we plot the correct range of elevations
        end_el = -45  + 5.625*(len(elevations)-1)

        # Monoaural Data (Ipsilateral), No Mean Subtracted
        ax = fig.add_subplot(1, 2, 1)
        # a = ax.pcolormesh(np.squeeze(hrtfs_i[:,:-5]))
        data = hrtfs_i[:, 5:]
        print(data.shape)
        a = ax.pcolormesh(np.linspace(0, 1, data.shape[1]), np.linspace(-45, end_el, data.shape[0]),
                          data, shading='gouraud', linewidth=0, rasterized=True)

        ax.xaxis.set_major_formatter(formatter)
        # d
        cbar = plt.colorbar(a)
        cbar.ax.get_yaxis().labelpad = 20
        cbar.set_label('Decibels [dB]', rotation=270)
        ax.set_title('Ipsilateral')
        ax.set_xlabel('Frequency [Hz]')
        ax.set_ylabel('Elevations [deg]')

        ax = fig.add_subplot(1, 2, 2)
        data = hrtfs_c[:, 5:]

        a = ax.pcolormesh(np.linspace(0, 1, data.shape[1]), np.linspace(-45, end_el, data.shape[0]),
                          data, shading='gouraud', linewidth=0, rasterized=True)
        ax.xaxis.set_major_formatter(formatter)
        # ax.set_xticklabels(np.linspace(0,max_freq/1000,len(ax.get_xticks())))
        ax.set_title('Contralateral')
        ax.set_xlabel('Frequency [Hz]')
        ax.set_ylabel('Elevations [deg]')

        cbar = plt.colorbar(a)
        cbar.ax.get_yaxis().labelpad = 20
        cbar.set_label('Decibels [dB]', rotation=270)


        if save_figs:
            exp_name_str = hp.create_exp_name([exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm, mean_subtracted_map, time_window, int(
            snr * 100), freq_bands, max_freq, (azimuth - 12) * 10, normalize, len(elevations), ear])
            fig_save_path = ROOT / 'reports' / 'figures' / exp_name_str / model_name
            if not fig_save_path.exists():
                fig_save_path.mkdir(parents=True, exist_ok=True)
            logger.info('Saving figures to ' + fig_save_path.as_posix())
            plt.savefig((fig_save_path / (model_name + '_' + exp_name + '_'+str(participant_number)+ '_raw_hrtfs.' + save_type)).as_posix(), dpi=300, transparent=True)
            plt.close()

        else:
            plt.show()

    else:
        logger.error('No data set found. Run model first!')
        logger.error(exp_file)
Ejemplo n.º 7
0
def main(model_name='snr_experiment',
         exp_name='default',
         azimuth=12,
         freq_bands=128,
         max_freq=20000,
         elevations=25,
         mean_subtracted_map=True,
         ear='ipsi',
         normalization_type='sum_1',
         sigma_smoothing=0,
         sigma_gauss_norm=1,
         clean=False):
    """ This script takes the filtered data and tries to localize sounds with a learned map
        for all participants.
    """
    logger = logging.getLogger(__name__)
    logger.info('Testing localization performance for different SNRs')

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################

    participant_numbers = np.array([
        1, 2, 3, 8, 9, 10, 11, 12, 15, 17, 18, 19, 20, 21, 27, 28, 33, 40, 44,
        48, 50, 51, 58, 59, 60, 61, 65, 119, 124, 126, 127, 131, 133, 134, 135,
        137, 147, 148, 152, 153, 154, 155, 156, 158, 162, 163, 165
    ])

    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)

    snrs = np.arange(0.0, 1.1, 0.1)

    #snrs = snrs[::-1]
    #participant_numbers = participant_numbers[::-1]

    ########################################################################
    ########################################################################

    # create unique experiment name
    exp_name_str = hp.create_exp_name([
        exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm,
        mean_subtracted_map, time_window, freq_bands, max_freq,
        (azimuth - 12) * 10, normalize,
        len(elevations), ear
    ])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load
    if not clean and exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with exp_file.open('rb') as f:
            logger.info('Reading model data from file')
            [scores] = pickle.load(f)
    else:
        # scores per participant, per snr, for 4 different learned maps, (gain,bias,score)
        scores = np.zeros((len(participant_numbers), len(snrs), 4, 3))

        for i_par, par in enumerate(participant_numbers):
            for i_snr, snr in enumerate(snrs):
                # create or read the data
                psd_all_c, psd_all_i = generateData.create_data(
                    freq_bands,
                    par,
                    snr,
                    normalize,
                    azimuth,
                    time_window,
                    max_freq=max_freq,
                    diff_noise=False)

                # Take only given elevations
                psd_all_c = psd_all_c[:, elevations, :]
                psd_all_i = psd_all_i[:, elevations, :]

                # filter data and integrate it
                psd_mono, psd_mono_mean, psd_binaural, psd_binaural_mean = hp.process_inputs(
                    psd_all_i, psd_all_c, ear, normalization_type,
                    sigma_smoothing, sigma_gauss_norm)

                # create map from defined processed data
                if mean_subtracted_map:
                    learned_map = psd_binaural_mean.mean(0)
                else:
                    learned_map = psd_binaural.mean(0)

                ### Different noise data ####
                # create or read the data
                psd_all_c, psd_all_i = generateData.create_data(
                    freq_bands,
                    par,
                    snr,
                    normalize,
                    azimuth,
                    time_window,
                    max_freq=max_freq,
                    diff_noise=True)

                # Take only given elevations
                psd_all_c = psd_all_c[:, elevations, :]
                psd_all_i = psd_all_i[:, elevations, :]

                # filter data and integrate it
                psd_mono, psd_mono_mean, psd_binaural, psd_binaural_mean = hp.process_inputs(
                    psd_all_i, psd_all_c, ear, normalization_type,
                    sigma_smoothing, sigma_gauss_norm)

                # localize the sounds and save the results
                x_test, y_test = hp.localize_sound(psd_mono, learned_map)
                x_test, y_test = hp_vis.scale_v(x_test, y_test,
                                                len(elevations))
                scores[i_par, i_snr,
                       0, :] = hp.get_localization_coefficients_score(
                           x_test, y_test)

                # localize the sounds and save the results
                x_test, y_test = hp.localize_sound(psd_mono_mean, learned_map)
                x_test, y_test = hp_vis.scale_v(x_test, y_test,
                                                len(elevations))
                scores[i_par, i_snr,
                       1, :] = hp.get_localization_coefficients_score(
                           x_test, y_test)

                # localize the sounds and save the results
                x_test, y_test = hp.localize_sound(psd_binaural, learned_map)
                x_test, y_test = hp_vis.scale_v(x_test, y_test,
                                                len(elevations))
                scores[i_par, i_snr,
                       2, :] = hp.get_localization_coefficients_score(
                           x_test, y_test)

                # localize the sounds and save the results
                x_test, y_test = hp.localize_sound(psd_binaural_mean,
                                                   learned_map)
                x_test, y_test = hp_vis.scale_v(x_test, y_test,
                                                len(elevations))
                scores[i_par, i_snr,
                       3, :] = hp.get_localization_coefficients_score(
                           x_test, y_test)

        # create Path
        exp_path.mkdir(parents=True, exist_ok=True)
        with exp_file.open('wb') as f:
            logger.info('Creating model file')
            pickle.dump([scores], f)
Ejemplo n.º 8
0
def main(save_figs=False,
         save_type='svg',
         model_name='hrtf_comparison',
         exp_name='single_participant',
         azimuth=12,
         participant_number=9,
         snr=0.2,
         freq_bands=24,
         max_freq=20000,
         elevations=25,
         mean_subtracted_map=True,
         ear='ipsi',
         normalization_type='sum_1',
         sigma_smoothing=0,
         sigma_gauss_norm=1):
    logger = logging.getLogger(__name__)
    logger.info(
        'Showing Correlation Coefficient Maps between HRTFs and differntly learned Maps'
    )

    # make sure save type is given
    if not save_type or len(save_type) == 0:
        save_type = 'svg'

    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)

    ######################################

    # create unique experiment name
    exp_name_str = hp.create_exp_name([
        exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm,
        mean_subtracted_map, time_window,
        int(snr * 100), freq_bands, max_freq, participant_number,
        (azimuth - 12) * 10, normalize,
        len(elevations), ear
    ])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load
    if exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with open(exp_file.as_posix(), 'rb') as f:
            logger.info('Reading model data from file')
            [
                hrtfs_i, hrtfs_c, learned_map_mono, learned_map_mono_mean,
                learned_map_bin, learned_map_bin_mean
            ] = pickle.load(f)

        fig = plt.figure(figsize=(20, 10))
        ax = fig.add_subplot(2, 2, 1)
        ax.set_title('Mono Map')
        tmp = np.concatenate((hrtfs_c, hrtfs_i, learned_map_mono))
        tmp = np.corrcoef(tmp)
        plot_corrcoeff(tmp, ax)

        ax = fig.add_subplot(2, 2, 2)
        ax.set_title('Mono-Mean Map')
        tmp = np.concatenate((hrtfs_c, hrtfs_i, learned_map_mono_mean))
        tmp = np.corrcoef(tmp)
        plot_corrcoeff(tmp, ax)

        ax = fig.add_subplot(2, 2, 3)
        ax.set_title('Bin Map')
        tmp = np.concatenate((hrtfs_c, hrtfs_i, learned_map_bin))
        tmp = np.corrcoef(tmp)
        plot_corrcoeff(tmp, ax)

        ax = fig.add_subplot(2, 2, 4)
        # ax = fig.add_subplot(1, 1, 1)
        ax.set_title('Bin-Mean Map')
        tmp = np.concatenate((hrtfs_c, hrtfs_i, learned_map_bin_mean))
        tmp = np.corrcoef(tmp)
        plot_corrcoeff(tmp, ax)

        if save_figs:
            exp_name_str = hp.create_exp_name([
                exp_name, normalization_type, 0, 1, mean_subtracted_map,
                time_window,
                int(snr * 100), freq_bands, max_freq, (azimuth - 12) * 10,
                normalize,
                len(elevations), ear
            ])
            fig_save_path = ROOT / 'reports' / 'figures' / exp_name_str / model_name
            if not fig_save_path.exists():
                fig_save_path.mkdir(parents=True, exist_ok=True)
            logger.info('Saving figures to ' + fig_save_path.as_posix())
            plt.savefig(
                (fig_save_path / (model_name + '_' + exp_name +
                                  '_hrtfs_xcorr.' + save_type)).as_posix(),
                dpi=300)

        else:
            plt.show()

    else:
        logger.error('No data set found. Run model first!')
        logger.error(exp_file)
Ejemplo n.º 9
0
def main(model_name='elevation_spectra_maps', exp_name='unfiltered', azimuth=12, participant_numbers=None, snr=0.2, freq_bands=24, max_freq=20000, elevations=25, clean=False):
    """ TODO
    """
    logger = logging.getLogger(__name__)
    logger.info('Creating maps for all participants and sounds')

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################

    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)
    # if participant_numbers is not given we use all of them
    if not participant_numbers:
        participant_numbers = np.array([1, 2, 3, 8, 9, 10, 11,
                                        12, 15, 17, 18, 19, 20,
                                        21, 27, 28, 33, 40, 44,
                                        48, 50, 51, 58, 59, 60,
                                        61, 65, 119, 124, 126,
                                        127, 131, 133, 134, 135,
                                        137, 147, 148, 152, 153,
                                        154, 155, 156, 158, 162,
                                        163, 165])

        exp_name_str = hp.create_exp_name([exp_name, time_window, int(snr * 100), freq_bands, max_freq,
                                           len(participant_numbers), (azimuth - 12) * 10, normalize, len(elevations)])
        exp_path = ROOT / 'models' / model_name
        exp_file = exp_path / exp_name_str
    else:
        # participant_numbers are given. need to be cast to int array
        participant_numbers = np.array([int(i) for i in participant_numbers.split(',')])
        print(participant_numbers)

        exp_name_str = hp.create_exp_name([exp_name, time_window, int(snr * 100), freq_bands, max_freq,
                                           participant_numbers, (azimuth - 12) * 10, normalize, len(elevations)])
        exp_path = ROOT / 'models' / model_name
        exp_file = exp_path / exp_name_str

    ########################################################################
    ########################################################################

    # check if model results exist already and load
    if not clean and exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with exp_file.open('rb') as f:
            logger.info('Reading model data from file')
            [ipsi_maps,contra_maps] = pickle.load(f)
    else:

        ipsi_maps = np.zeros((len(participant_numbers), len(SOUND_FILES), len(elevations), freq_bands))
        contra_maps = np.zeros((len(participant_numbers), len(SOUND_FILES), len(elevations), freq_bands))

        for i_par, par in enumerate(participant_numbers):

            # create or read the data
            psd_all_c, psd_all_i = generateData.create_data(
                freq_bands, par, snr, normalize, azimuth, time_window,max_freq=max_freq)

            # Take only given elevations
            psd_all_c = psd_all_c[:, elevations, :]
            psd_all_i = psd_all_i[:, elevations, :]

            ipsi_maps[i_par, :, :, :] = psd_all_i
            contra_maps[i_par, :, :, :] = psd_all_c

        # create Path
        exp_path.mkdir(parents=True, exist_ok=True)
        with exp_file.open('wb') as f:
            logger.info('Creating model file')
            pickle.dump([ipsi_maps,contra_maps], f)
Ejemplo n.º 10
0
def main(save_figs=False,
         save_type='svg',
         model_name='different_learned_maps',
         exp_name='localization_default',
         azimuth=12,
         snr=0.2,
         freq_bands=24,
         max_freq=20000,
         elevations=25,
         mean_subtracted_map=True,
         ear='ipsi',
         normalization_type='sum_1',
         sigma_smoothing=0,
         sigma_gauss_norm=1,
         clean=False):
    logger = logging.getLogger(__name__)
    logger.info(
        'Showing localization results for all participants over different learned maps'
    )

    # make sure save type is given
    if not save_type or len(save_type) == 0:
        save_type = 'svg'
    ########################################################################
    ######################## Set parameters ################################
    ########################################################################

    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)

    ########################################################################
    ########################################################################

    # create unique experiment name
    exp_name_str = hp.create_exp_name([
        exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm,
        mean_subtracted_map, time_window,
        int(snr * 100), freq_bands, max_freq, (azimuth - 12) * 10, normalize,
        len(elevations), ear
    ])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load
    if exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with open(exp_file.as_posix(), 'rb') as f:
            logger.info('Reading model data from file')
            [
                x_mono, y_mono, x_mono_mean, y_mono_mean, x_bin, y_bin,
                x_bin_mean, y_bin_mean
            ] = pickle.load(f)

        # define which elevations should be used
        x_mono_all = x_mono[:, :, :, elevations, :]
        y_mono_all = y_mono[:, :, :, elevations]
        x_mono_mean_all = x_mono_mean[:, :, :, elevations, :]
        y_mono_mean_all = y_mono_mean[:, :, :, elevations]
        x_bin_all = x_bin[:, :, :, elevations, :]
        y_bin_all = y_bin[:, :, :, elevations]
        x_bin_mean_all = x_bin_mean[:, :, :, elevations, :]
        y_bin_mean_all = y_bin_mean[:, :, :, elevations]

        fig = plt.figure(figsize=(20, 20))
        axes = fig.subplots(4, 4, sharex=True, sharey=True)
        # plt.suptitle('Single Participant')

        # plot regression line for each participant
        for i_map in range(4):
            # get right axis
            ax1 = axes[i_map, 0]
            ax2 = axes[i_map, 1]
            ax3 = axes[i_map, 2]
            ax4 = axes[i_map, 3]

            # get the data for each map
            x_mono = x_mono_all[i_map]
            y_mono = y_mono_all[i_map]
            x_mono_mean = x_mono_mean_all[i_map]
            y_mono_mean = y_mono_mean_all[i_map]
            x_bin = x_bin_all[i_map]
            y_bin = y_bin_all[i_map]
            x_bin_mean = x_bin_mean_all[i_map]
            y_bin_mean = y_bin_mean_all[i_map]

            for i_par in range(x_mono_all.shape[1]):

                hp_vis.plot_localization_result(x_mono[i_par],
                                                y_mono[i_par],
                                                ax1,
                                                SOUND_FILES,
                                                scale_values=True,
                                                linear_reg=True,
                                                scatter_data=False)
                if i_map == 0:
                    ax1.set_title('Monoaural')
                hp_vis.set_axis(ax1, len(elevations))
                ax1.set_ylabel('Estimated Elevation \n [deg]')
                if i_map == 3:
                    ax1.set_xlabel('True Elevation [deg]')

                # Monoaural Data (Ipsilateral), Mean Subtracted
                hp_vis.plot_localization_result(x_mono_mean[i_par],
                                                y_mono_mean[i_par],
                                                ax2,
                                                SOUND_FILES,
                                                scale_values=True,
                                                linear_reg=True,
                                                scatter_data=False)

                if i_map == 0:
                    ax2.set_title('Mono - Prior')
                hp_vis.set_axis(ax2, len(elevations))
                if i_map == 3:
                    ax2.set_xlabel('True Elevation [deg]')

                # Binaural Data (Ipsilateral), No Mean Subtracted

                hp_vis.plot_localization_result(x_bin[i_par],
                                                y_bin[i_par],
                                                ax3,
                                                SOUND_FILES,
                                                scale_values=True,
                                                linear_reg=True,
                                                scatter_data=False)
                if i_map == 0:
                    ax3.set_title('Binaural')
                hp_vis.set_axis(ax3, len(elevations))
                if i_map == 3:
                    ax3.set_xlabel('True Elevation [deg]')

                # Binaural Data (Ipsilateral), Mean Subtracted

                hp_vis.plot_localization_result(x_bin_mean[i_par],
                                                y_bin_mean[i_par],
                                                ax4,
                                                SOUND_FILES,
                                                scale_values=True,
                                                linear_reg=True,
                                                scatter_data=False)
                if i_map == 0:
                    ax4.set_title('Bin - Prior')
                hp_vis.set_axis(ax4, len(elevations))
                if i_map == 3:
                    ax4.set_xlabel('True Elevation [deg]')

            # plot a common regression line
            x_mono_ = np.reshape(x_mono, (x_mono.shape[0] * x_mono.shape[1],
                                          x_mono.shape[2], x_mono.shape[3]))
            y_mono_ = np.reshape(
                y_mono, (y_mono.shape[0] * y_mono.shape[1], y_mono.shape[2]))

            x_mono_mean_ = np.reshape(
                x_mono_mean, (x_mono_mean.shape[0] * x_mono_mean.shape[1],
                              x_mono_mean.shape[2], x_mono_mean.shape[3]))
            y_mono_mean_ = np.reshape(
                y_mono_mean, (y_mono_mean.shape[0] * y_mono_mean.shape[1],
                              y_mono_mean.shape[2]))

            x_bin_ = np.reshape(x_bin, (x_bin.shape[0] * x_bin.shape[1],
                                        x_bin.shape[2], x_bin.shape[3]))
            y_bin_ = np.reshape(
                y_bin, (y_bin.shape[0] * y_bin.shape[1], y_bin.shape[2]))

            x_bin_mean_ = np.reshape(
                x_bin_mean, (x_bin_mean.shape[0] * x_bin_mean.shape[1],
                             x_bin_mean.shape[2], x_bin_mean.shape[3]))
            y_bin_mean_ = np.reshape(
                y_bin_mean, (y_bin_mean.shape[0] * y_bin_mean.shape[1],
                             y_bin_mean.shape[2]))

            hp_vis.plot_localization_result(x_mono_,
                                            y_mono_,
                                            ax1,
                                            SOUND_FILES,
                                            scale_values=False,
                                            linear_reg=True,
                                            disp_values=True,
                                            scatter_data=False,
                                            reg_color="black")
            hp_vis.plot_localization_result(x_mono_mean_,
                                            y_mono_mean_,
                                            ax2,
                                            SOUND_FILES,
                                            scale_values=False,
                                            linear_reg=True,
                                            disp_values=True,
                                            scatter_data=False,
                                            reg_color="black")
            hp_vis.plot_localization_result(x_bin_,
                                            y_bin_,
                                            ax3,
                                            SOUND_FILES,
                                            scale_values=False,
                                            linear_reg=True,
                                            disp_values=True,
                                            scatter_data=False,
                                            reg_color="black")
            hp_vis.plot_localization_result(x_bin_mean_,
                                            y_bin_mean_,
                                            ax4,
                                            SOUND_FILES,
                                            scale_values=False,
                                            linear_reg=True,
                                            disp_values=True,
                                            scatter_data=False,
                                            reg_color="black")

        if save_figs:
            fig_save_path = ROOT / 'reports' / 'figures' / exp_name_str / model_name
            if not fig_save_path.exists():
                fig_save_path.mkdir(parents=True, exist_ok=True)
            logger.info('Saving figures to ' + fig_save_path.as_posix())
            plt.savefig(
                (fig_save_path / (model_name + '_' + exp_name +
                                  '_localization.' + save_type)).as_posix(),
                dpi=300)

        else:
            plt.show()

    else:
        logger.error('No data set found. Run model first!')
        logger.error(exp_file)
Ejemplo n.º 11
0
def main(model_name='single_participant',
         exp_name='single_participant_default',
         azimuth=12,
         participant_number=9,
         snr=0.2,
         freq_bands=24,
         max_freq=20000,
         elevations=25,
         mean_subtracted_map=True,
         ear='ipsi',
         normalization_type='sum_1',
         sigma_smoothing=0,
         sigma_gauss_norm=1,
         clean=False):
    """ This script takes the filtered data and tries to localize sounds with a learned map
        for a single participant.
    """
    logger = logging.getLogger(__name__)
    logger.info('Localizing sounds for a single participant')

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################
    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)
    ########################################################################
    ########################################################################

    # create unique experiment name
    exp_name_str = hp.create_exp_name([
        exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm,
        mean_subtracted_map, time_window,
        int(snr * 100), freq_bands, max_freq, participant_number,
        (azimuth - 12) * 10, normalize,
        len(elevations), ear
    ])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str

    # check if model results exist already and load
    if not clean and exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with exp_file.open('rb') as f:
            logger.info('Reading model data from file')
            [
                x_mono, y_mono, x_mono_mean, y_mono_mean, x_bin, y_bin,
                x_bin_mean, y_bin_mean
            ] = pickle.load(f)
    else:
        # create Path
        exp_path.mkdir(parents=True, exist_ok=True)
        # create or read the data
        psd_all_c, psd_all_i = generateData.create_data(freq_bands,
                                                        participant_number,
                                                        snr,
                                                        normalize,
                                                        azimuth,
                                                        time_window,
                                                        max_freq=max_freq,
                                                        diff_noise=False)

        # Take only given elevations
        psd_all_c = psd_all_c[:, elevations, :]
        psd_all_i = psd_all_i[:, elevations, :]

        # filter data and integrate it
        psd_mono, psd_mono_mean, psd_binaural, psd_binaural_mean = hp.process_inputs(
            psd_all_i, psd_all_c, ear, normalization_type, sigma_smoothing,
            sigma_gauss_norm)

        # create map from defined processed data
        if mean_subtracted_map:
            learned_map = psd_binaural_mean.mean(0)
        else:
            learned_map = psd_binaural.mean(0)

        # create or read the data
        psd_all_c, psd_all_i = generateData.create_data(freq_bands,
                                                        participant_number,
                                                        snr,
                                                        normalize,
                                                        azimuth,
                                                        time_window,
                                                        max_freq=max_freq,
                                                        diff_noise=True)

        # Take only given elevations
        psd_all_c = psd_all_c[:, elevations, :]
        psd_all_i = psd_all_i[:, elevations, :]

        # filter data and integrate it
        psd_mono, psd_mono_mean, psd_binaural, psd_binaural_mean = hp.process_inputs(
            psd_all_i, psd_all_c, ear, normalization_type, sigma_smoothing,
            sigma_gauss_norm)

        # localize the sounds and save the results
        x_mono, y_mono = hp.localize_sound(psd_mono, learned_map)

        # localize the sounds and save the results
        x_mono_mean, y_mono_mean = hp.localize_sound(psd_mono_mean,
                                                     learned_map)

        # localize the sounds and save the results
        x_bin, y_bin = hp.localize_sound(psd_binaural, learned_map)

        # localize the sounds and save the results
        x_bin_mean, y_bin_mean = hp.localize_sound(psd_binaural_mean,
                                                   learned_map)

        with exp_file.open('wb') as f:
            logger.info('Creating model file')
            pickle.dump([
                x_mono, y_mono, x_mono_mean, y_mono_mean, x_bin, y_bin,
                x_bin_mean, y_bin_mean
            ], f)
def main(model_name='hrtf_creation',
         exp_name='single_participant',
         azimuth=12,
         participant_number=9,
         snr=0.0,
         freq_bands=128,
         max_freq=20000,
         elevations=25,
         mean_subtracted_map=True,
         ear='ipsi',
         normalization_type='sum_1',
         sigma_smoothing=0,
         sigma_gauss_norm=1,
         clean=False):
    """ This script calculates the correlation coefficient between the ipsi- and contralateral HRTF and the learned maps for a single participant.
    """
    logger = logging.getLogger(__name__)
    logger.info('Just get the HRTFs of the given participant')

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################
    normalize = False
    time_window = 0.1  # time window in sec

    ### IMPORTANT ###
    # Use 128 freq_bands for better displaying
    freq_bands = 128
    logger.info('############## IMPORTANT ###############')
    logger.info(
        '############## Using 128 frequency by default! ###############')

    elevations = np.arange(0, elevations, 1)
    ########################################################################
    ########################################################################

    # create unique experiment name
    exp_name_str = hp.create_exp_name([
        exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm,
        mean_subtracted_map, time_window,
        int(snr * 100), freq_bands, max_freq, participant_number,
        (azimuth - 12) * 10, normalize,
        len(elevations), ear
    ])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load
    if not clean and exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with exp_file.open('rb') as f:
            logger.info('Reading model data from file')
            [hrtfs_i, hrtfs_c, freqs] = pickle.load(f)
    else:

        hrtfs_c, hrtfs_i, freqs = generateHRTFs.create_data(freq_bands,
                                                            participant_number,
                                                            snr,
                                                            normalize,
                                                            azimuth,
                                                            time_window,
                                                            max_freq=max_freq,
                                                            clean=clean)

        # # remove mean for later comparison
        hrtfs_c = np.squeeze(hrtfs_c[0, elevations, :])
        # hrtfs_c -= hrtfs_c.mean()
        hrtfs_i = np.squeeze(hrtfs_i[0, elevations, :])
        # hrtfs_i -= hrtfs_i.mean()

        # create Path
        exp_path.mkdir(parents=True, exist_ok=True)
        with exp_file.open('wb') as f:
            logger.info('Creating model file')
            pickle.dump([hrtfs_i, hrtfs_c, freqs], f)
Ejemplo n.º 13
0
def main(save_figs=False,
         save_type='svg',
         model_name='all_participants',
         exp_name='localization_default',
         azimuth=12,
         snr=0.2,
         freq_bands=24,
         max_freq=20000,
         elevations=25,
         mean_subtracted_map=True,
         ear='ipsi',
         normalization_type='sum_1',
         sigma_smoothing=0,
         sigma_gauss_norm=1,
         clean=False):

    logger = logging.getLogger(__name__)
    logger.info('Showing localization results for all participants')

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################

    # make sure save type is given
    if not save_type or len(save_type) == 0:
        save_type = 'svg'

    participant_numbers = np.array([
        1, 2, 3, 8, 9, 10, 11, 12, 15, 17, 18, 19, 20, 21, 27, 28, 33, 40, 44,
        48, 50, 51, 58, 59, 60, 61, 65, 119, 124, 126, 127, 131, 133, 134, 135,
        137, 147, 148, 152, 153, 154, 155, 156, 158, 162, 163, 165
    ])

    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)

    ########################################################################
    ########################################################################

    exp_name_str = hp.create_exp_name([
        exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm,
        mean_subtracted_map, time_window,
        int(snr * 100), freq_bands, max_freq, (azimuth - 12) * 10, normalize,
        len(elevations), ear
    ])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load
    if not clean and exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with open(exp_file.as_posix(), 'rb') as f:
            logger.info('Reading model data from file')
            [
                x_mono, y_mono, x_mono_mean, y_mono_mean, x_bin, y_bin,
                x_bin_mean, y_bin_mean
            ] = pickle.load(f)

        # define which elevations should be used
        x_mono = x_mono[:, :, elevations, :]
        y_mono = y_mono[:, :, elevations]
        x_mono_mean = x_mono_mean[:, :, elevations, :]
        y_mono_mean = y_mono_mean[:, :, elevations]
        x_bin = x_bin[:, :, elevations, :]
        y_bin = y_bin[:, :, elevations]
        x_bin_mean = x_bin_mean[:, :, elevations, :]
        y_bin_mean = y_bin_mean[:, :, elevations]

        # save regression values for later usage
        coeff_ms = np.zeros((4, participant_numbers.shape[0]))
        coeff_bs = np.zeros((4, participant_numbers.shape[0]))
        scores = np.zeros((4, participant_numbers.shape[0]))

        # plot regression line for each participant
        for i_par, par in enumerate(participant_numbers):
            coeff_ms[0, i_par], coeff_bs[0, i_par], scores[
                0, i_par] = get_regression_values(x_mono[i_par], y_mono[i_par])
            coeff_ms[1, i_par], coeff_bs[1, i_par], scores[
                1, i_par] = get_regression_values(x_mono_mean[i_par],
                                                  y_mono_mean[i_par])
            coeff_ms[2, i_par], coeff_bs[2, i_par], scores[
                2, i_par] = get_regression_values(x_bin[i_par], y_bin[i_par])
            coeff_ms[3, i_par], coeff_bs[3, i_par], scores[
                3, i_par] = get_regression_values(x_bin_mean[i_par],
                                                  y_bin_mean[i_par])

            # sns.set_palette('muted')
            # my_pal = sns.color_palette("hls", 8)
        fig = plt.figure(figsize=(20, 5))
        my_pal = [(31 / 255, 119 / 255, 180 / 255),
                  (0.3333333333333333, 0.6588235294117647,
                   0.40784313725490196),
                  (0.7686274509803922, 0.3058823529411765, 0.3215686274509804),
                  (0.5058823529411764, 0.4470588235294118, 0.6980392156862745)]

        ax = fig.add_subplot(1, 3, 1)
        ax.set_ylabel('Gain')
        sns.boxplot(data=coeff_ms.T,
                    showfliers=True,
                    palette=hp_vis.MY_COLORS,
                    ax=ax,
                    linewidth=3)
        ax.set_xticklabels(['Mono', 'Mono\n-Prior', 'Bin', 'Bin\n-Prior'])

        ax = fig.add_subplot(1, 3, 2)
        ax.set_ylabel('Bias')
        sns.boxplot(data=coeff_bs.T,
                    showfliers=True,
                    palette=hp_vis.MY_COLORS,
                    ax=ax,
                    linewidth=3)
        ax.set_xticklabels(['Mono', 'Mono\n-Prior', 'Bin', 'Bin\n-Prior'])

        ax = fig.add_subplot(1, 3, 3)
        ax.set_ylabel('Score')
        sns.boxplot(data=scores.T,
                    showfliers=True,
                    palette=hp_vis.MY_COLORS,
                    ax=ax,
                    linewidth=3)
        ax.set_xticklabels(['Mono', 'Mono\n-Prior', 'Bin', 'Bin\n-Prior'])

        if save_figs:
            fig_save_path = ROOT / 'reports' / 'figures' / exp_name_str / model_name
            if not fig_save_path.exists():
                fig_save_path.mkdir(parents=True, exist_ok=True)
            logger.info('Saving figures to ' + fig_save_path.as_posix())
            plt.savefig((fig_save_path /
                         (model_name + '_' + exp_name + '_regression_values.' +
                          save_type)).as_posix(),
                        dpi=300)
        else:
            plt.show()

    else:
        logger.error('No data set found. Run model first!')
        logger.error(exp_file)
Ejemplo n.º 14
0
def main(save_figs=False,
         save_type='svg',
         model_name='all_participants',
         exp_name='localization_default',
         azimuth=12,
         snr=0.2,
         freq_bands=24,
         max_freq=20000,
         elevations=25,
         mean_subtracted_map=True,
         ear='ipsi',
         normalization_type='sum_1',
         sigma_smoothing=0,
         sigma_gauss_norm=1,
         clean=False):
    """ This script plots the localization quality for all participants over sounds
    """
    logger = logging.getLogger(__name__)
    logger.info('Showing localization results for all sounds')

    # make sure save type is given
    if not save_type or len(save_type) == 0:
        save_type = 'svg'

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################
    participant_numbers = np.array([
        1, 2, 3, 8, 9, 10, 11, 12, 15, 17, 18, 19, 20, 21, 27, 28, 33, 40, 44,
        48, 50, 51, 58, 59, 60, 61, 65, 119, 124, 126, 127, 131, 133, 134, 135,
        137, 147, 148, 152, 153, 154, 155, 156, 158, 162, 163, 165
    ])

    sounds = np.array(
        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19])

    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)
    ########################################################################
    ########################################################################

    # create unique experiment name
    exp_name_str = hp.create_exp_name([
        exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm,
        mean_subtracted_map, time_window,
        int(snr * 100), freq_bands, max_freq, (azimuth - 12) * 10, normalize,
        len(elevations), ear
    ])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load
    if exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with open(exp_file.as_posix(), 'rb') as f:
            logger.info('Reading model data from file')
            [
                x_mono, y_mono, x_mono_mean, y_mono_mean, x_bin, y_bin,
                x_bin_mean, y_bin_mean
            ] = pickle.load(f)

        # define which elevations should be used
        x_mono = x_mono[:, :, elevations, :]
        y_mono = y_mono[:, :, elevations]
        x_mono_mean = x_mono_mean[:, :, elevations, :]
        y_mono_mean = y_mono_mean[:, :, elevations]
        x_bin = x_bin[:, :, elevations, :]
        y_bin = y_bin[:, :, elevations]
        x_bin_mean = x_bin_mean[:, :, elevations, :]
        y_bin_mean = y_bin_mean[:, :, elevations]

        fig = plt.figure(figsize=(20, 5))
        # plt.suptitle('Single Participant')
        # Monoaural Data (Ipsilateral), No Mean Subtracted
        ax1 = fig.add_subplot(1, 4, 1)
        ax2 = fig.add_subplot(1, 4, 2)
        ax3 = fig.add_subplot(1, 4, 3)
        ax4 = fig.add_subplot(1, 4, 4)

        # plot regression line for each sound
        for i_sound, sound in enumerate(sounds):
            hp_vis.plot_localization_result(x_mono[:, i_sound],
                                            y_mono[:, i_sound],
                                            ax1,
                                            SOUND_FILES,
                                            scale_values=True,
                                            linear_reg=True,
                                            scatter_data=False)
            ax1.set_title('Monoaural')
            hp_vis.set_axis(ax1, len(elevations))
            ax1.set_ylabel('Estimated Elevation [deg]')
            ax1.set_xlabel('True Elevation [deg]')

            # Monoaural Data (Ipsilateral), Mean Subtracted
            hp_vis.plot_localization_result(x_mono_mean[:, i_sound],
                                            y_mono_mean[:, i_sound],
                                            ax2,
                                            SOUND_FILES,
                                            scale_values=True,
                                            linear_reg=True,
                                            scatter_data=False)
            ax2.set_title('Mono - Prior')
            hp_vis.set_axis(ax2, len(elevations))
            ax2.set_xlabel('True Elevation [deg]')

            # Binaural Data (Ipsilateral), No Mean Subtracted

            hp_vis.plot_localization_result(x_bin[:, i_sound],
                                            y_bin[:, i_sound],
                                            ax3,
                                            SOUND_FILES,
                                            scale_values=True,
                                            linear_reg=True,
                                            scatter_data=False)
            ax3.set_title('Binaural')
            hp_vis.set_axis(ax3, len(elevations))
            ax3.set_xlabel('True Elevation [deg]')

            # Binaural Data (Ipsilateral), Mean Subtracted

            hp_vis.plot_localization_result(x_bin_mean[:, i_sound],
                                            y_bin_mean[:, i_sound],
                                            ax4,
                                            SOUND_FILES,
                                            scale_values=True,
                                            linear_reg=True,
                                            scatter_data=False)
            ax4.set_title('Bin - Prior')
            hp_vis.set_axis(ax4, len(elevations))
            ax4.set_xlabel('True Elevation [deg]')

        # plot a common regression line
        x_mono_ = np.reshape(x_mono, (x_mono.shape[0] * x_mono.shape[1],
                                      x_mono.shape[2], x_mono.shape[3]))
        y_mono_ = np.reshape(
            y_mono, (y_mono.shape[0] * y_mono.shape[1], y_mono.shape[2]))

        x_mono_mean_ = np.reshape(x_mono_mean,
                                  (x_mono_mean.shape[0] * x_mono_mean.shape[1],
                                   x_mono_mean.shape[2], x_mono_mean.shape[3]))
        y_mono_mean_ = np.reshape(y_mono_mean,
                                  (y_mono_mean.shape[0] * y_mono_mean.shape[1],
                                   y_mono_mean.shape[2]))

        x_bin_ = np.reshape(
            x_bin,
            (x_bin.shape[0] * x_bin.shape[1], x_bin.shape[2], x_bin.shape[3]))
        y_bin_ = np.reshape(y_bin,
                            (y_bin.shape[0] * y_bin.shape[1], y_bin.shape[2]))

        x_bin_mean_ = np.reshape(x_bin_mean,
                                 (x_bin_mean.shape[0] * x_bin_mean.shape[1],
                                  x_bin_mean.shape[2], x_bin_mean.shape[3]))
        y_bin_mean_ = np.reshape(
            y_bin_mean,
            (y_bin_mean.shape[0] * y_bin_mean.shape[1], y_bin_mean.shape[2]))

        hp_vis.plot_localization_result(x_mono_,
                                        y_mono_,
                                        ax1,
                                        SOUND_FILES,
                                        scale_values=False,
                                        linear_reg=True,
                                        disp_values=True,
                                        scatter_data=False,
                                        reg_color="black")
        hp_vis.plot_localization_result(x_mono_mean_,
                                        y_mono_mean_,
                                        ax2,
                                        SOUND_FILES,
                                        scale_values=False,
                                        linear_reg=True,
                                        disp_values=True,
                                        scatter_data=False,
                                        reg_color="black")
        hp_vis.plot_localization_result(x_bin_,
                                        y_bin_,
                                        ax3,
                                        SOUND_FILES,
                                        scale_values=False,
                                        linear_reg=True,
                                        disp_values=True,
                                        scatter_data=False,
                                        reg_color="black")
        hp_vis.plot_localization_result(x_bin_mean_,
                                        y_bin_mean_,
                                        ax4,
                                        SOUND_FILES,
                                        scale_values=False,
                                        linear_reg=True,
                                        disp_values=True,
                                        scatter_data=False,
                                        reg_color="black")

        # get the name of the sounds
        sound_names = np.array([i.name.split('.')[0] for i in SOUND_FILES])

        lgd = ax4.legend(sound_names, loc=(1.04, 0))
        # plt.tight_layout()
        if save_figs:
            fig_save_path = ROOT / 'reports' / 'figures' / exp_name_str / model_name
            if not fig_save_path.exists():
                fig_save_path.mkdir(parents=True, exist_ok=True)
            logger.info('Saving figures to ' + fig_save_path.as_posix())
            plt.savefig((fig_save_path /
                         (model_name + '_' + exp_name +
                          '_localization_sounds.' + save_type)).as_posix(),
                        dpi=300,
                        bbox_extra_artists=(lgd, ),
                        bbox_inches='tight')

        else:
            plt.show()
    else:
        logger.error('No data set found. Run model first!')
        logger.error(exp_file)
def main(model_name='single_participant',
         exp_name='single_participant_different_azis'):
    """ Localizes sounds at azimuth 'azimuth' with a learned map at azimuth 0.
    """
    logger = logging.getLogger(__name__)
    logger.info(
        'Localizing sounds for a single participant at different azimuths')

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################
    azimuth = 12
    snr = 0.2
    freq_bands = 128
    max_freq = 20000
    participant_number = 9

    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, 25, 1)

    # filtering parameters
    normalization_type = 'sum_1'
    sigma_smoothing = 0
    sigma_gauss_norm = 1

    # use the mean subtracted map as the learned map
    mean_subtracted_map = True

    # choose which ear to use 'contra' or 'ipsi'
    ear = 'ipsi'

    ########################################################################
    ########################################################################

    # create unique experiment name
    exp_name_str = hp.create_exp_name([
        exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm,
        mean_subtracted_map, time_window,
        int(snr * 100), freq_bands, max_freq, participant_number,
        (azimuth - 12) * 10, normalize,
        len(elevations), ear
    ])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load
    if exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with exp_file.open('rb') as f:
            logger.info('Reading model data from file')
            [
                x_mono, y_mono, x_mono_mean, y_mono_mean, x_bin, y_bin,
                x_bin_mean, y_bin_mean
            ] = pickle.load(f)
    else:
        # create Path
        exp_path.mkdir(parents=True, exist_ok=True)
        # create or read the data
        psd_all_c, psd_all_i = generateData.create_data(freq_bands,
                                                        participant_number,
                                                        snr,
                                                        normalize,
                                                        12,
                                                        time_window,
                                                        max_freq=max_freq)

        # Take only given elevations
        psd_all_c = psd_all_c[:, elevations, :]
        psd_all_i = psd_all_i[:, elevations, :]

        ####### Map Learning #######
        # filter data and integrate it for map learning
        psd_mono, psd_mono_mean, psd_binaural, psd_binaural_mean = hp.process_inputs(
            psd_all_i, psd_all_c, ear, normalization_type, sigma_smoothing,
            sigma_gauss_norm)

        # create map from defined processed data
        if mean_subtracted_map:
            learned_map = psd_binaural_mean.mean(0)
        else:
            learned_map = psd_binaural.mean(0)

        ####### Input Processing #######
        # process data for actual input
        psd_all_c, psd_all_i = generateData.create_data(
            freq_bands, participant_number, snr, normalize, azimuth,
            time_window)

        # filter data and integrate it
        psd_mono, psd_mono_mean, psd_binaural, psd_binaural_mean = hp.process_inputs(
            psd_all_i, psd_all_c, ear, normalization_type, sigma_smoothing,
            sigma_gauss_norm)

        ####### Localization #######
        # localize the sounds and save the results
        x_mono, y_mono = hp.localize_sound(psd_mono, learned_map)

        # localize the sounds and save the results
        x_mono_mean, y_mono_mean = hp.localize_sound(psd_mono_mean,
                                                     learned_map)

        # localize the sounds and save the results
        x_bin, y_bin = hp.localize_sound(psd_binaural, learned_map)

        # localize the sounds and save the results
        x_bin_mean, y_bin_mean = hp.localize_sound(psd_binaural_mean,
                                                   learned_map)

        with exp_file.open('wb') as f:
            logger.info('Creating model file')
            pickle.dump([
                x_mono, y_mono, x_mono_mean, y_mono_mean, x_bin, y_bin,
                x_bin_mean, y_bin_mean
            ], f)
Ejemplo n.º 16
0
def main(model_name='different_learned_maps',
         exp_name='localization_default',
         azimuth=12,
         snr=0.2,
         freq_bands=24,
         max_freq=20000,
         elevations=25,
         mean_subtracted_map=True,
         ear='ipsi',
         normalization_type='sum_1',
         sigma_smoothing=0,
         sigma_gauss_norm=1,
         clean=False):
    """ This script takes the filtered data and tries to localize sounds with different, learned map
        for all participants.
    """
    logger = logging.getLogger(__name__)
    logger.info('Localizing sounds for all participants, different maps')

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################
    participant_numbers = np.array([
        1, 2, 3, 8, 9, 10, 11, 12, 15, 17, 18, 19, 20, 21, 27, 28, 33, 40, 44,
        48, 50, 51, 58, 59, 60, 61, 65, 119, 124, 126, 127, 131, 133, 134, 135,
        137, 147, 148, 152, 153, 154, 155, 156, 158, 162, 163, 165
    ])

    # participant_numbers = np.array([1, 2, 3, 8, 9, 10, 11,
    #                                 12, 15, 17, 18, 19, 20,
    #                                 21, 27, 28, 33, 40])

    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)
    ########################################################################
    ########################################################################

    # create unique experiment name
    exp_name_str = hp.create_exp_name([
        exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm,
        mean_subtracted_map, time_window,
        int(snr * 100), freq_bands, max_freq, (azimuth - 12) * 10, normalize,
        len(elevations), ear
    ])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load
    if not clean and exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with exp_file.open('rb') as f:
            logger.info('Reading model data from file' + exp_file.as_posix())
            [
                x_mono, y_mono, x_mono_mean, y_mono_mean, x_bin, y_bin,
                x_bin_mean, y_bin_mean
            ] = pickle.load(f)
    else:

        x_mono = np.zeros((4, len(participant_numbers), len(SOUND_FILES),
                           len(elevations), 2))
        y_mono = np.zeros(
            (4, len(participant_numbers), len(SOUND_FILES), len(elevations)))
        x_mono_mean = np.zeros((4, len(participant_numbers), len(SOUND_FILES),
                                len(elevations), 2))
        y_mono_mean = np.zeros(
            (4, len(participant_numbers), len(SOUND_FILES), len(elevations)))
        x_bin = np.zeros((4, len(participant_numbers), len(SOUND_FILES),
                          len(elevations), 2))
        y_bin = np.zeros(
            (4, len(participant_numbers), len(SOUND_FILES), len(elevations)))
        x_bin_mean = np.zeros((4, len(participant_numbers), len(SOUND_FILES),
                               len(elevations), 2))
        y_bin_mean = np.zeros(
            (4, len(participant_numbers), len(SOUND_FILES), len(elevations)))
        for i_par, par in enumerate(participant_numbers):

            # create or read the data
            psd_all_c, psd_all_i = generateData.create_data(freq_bands,
                                                            par,
                                                            snr,
                                                            normalize,
                                                            azimuth,
                                                            time_window,
                                                            max_freq=max_freq,
                                                            diff_noise=False)

            # Take only given elevations
            psd_all_c = psd_all_c[:, elevations, :]
            psd_all_i = psd_all_i[:, elevations, :]

            # filter data and integrate it
            psd_mono, psd_mono_mean, psd_binaural, psd_binaural_mean = hp.process_inputs(
                psd_all_i, psd_all_c, ear, normalization_type, sigma_smoothing,
                sigma_gauss_norm)

            ### Read different noise data ###
            # create or read the data
            psd_all_c_diff_noise, psd_all_i_diff_noise = generateData.create_data(
                freq_bands,
                par,
                snr,
                normalize,
                azimuth,
                time_window,
                max_freq=max_freq,
                diff_noise=True)

            # Take only given elevations
            psd_all_c_diff_noise = psd_all_c_diff_noise[:, elevations, :]
            psd_all_i_diff_noise = psd_all_i_diff_noise[:, elevations, :]

            # filter data and integrate it
            psd_mono_diff_noise, psd_mono_mean_diff_noise, psd_binaural_diff_noise, psd_binaural_mean_diff_noise = hp.process_inputs(
                psd_all_i_diff_noise, psd_all_c_diff_noise, ear,
                normalization_type, sigma_smoothing, sigma_gauss_norm)

            # walk over the 4 different maps: mono, mono_mean, bina, bina_mean
            for i_map in range(4):
                # create map from defined processed data

                if i_map == 0:
                    learned_map = psd_mono.mean(0)
                elif i_map == 1:
                    learned_map = psd_mono_mean.mean(0)
                elif i_map == 2:
                    learned_map = psd_binaural.mean(0)
                elif i_map == 3:
                    # bina_mean
                    learned_map = psd_binaural_mean.mean(0)
                else:
                    logger.error('Something went wrong in if i_map statement')

                # localize the sounds and save the results
                x_mono[i_map,
                       i_par, :, :, :], y_mono[i_map,
                                               i_par, :] = hp.localize_sound(
                                                   psd_mono_diff_noise,
                                                   learned_map)

                # localize the sounds and save the results
                x_mono_mean[i_map, i_par, :, :, :], y_mono_mean[
                    i_map,
                    i_par, :, :] = hp.localize_sound(psd_mono_mean_diff_noise,
                                                     learned_map)

                # localize the sounds and save the results
                x_bin[i_map,
                      i_par, :, :, :], y_bin[i_map,
                                             i_par, :, :] = hp.localize_sound(
                                                 psd_binaural_diff_noise,
                                                 learned_map)

                # localize the sounds and save the results
                x_bin_mean[i_map, i_par, :, :, :], y_bin_mean[
                    i_map, i_par, :, :] = hp.localize_sound(
                        psd_binaural_mean_diff_noise, learned_map)

        # create Path
        exp_path.mkdir(parents=True, exist_ok=True)
        with exp_file.open('wb') as f:
            logger.info('Creating model file')
            pickle.dump([
                x_mono, y_mono, x_mono_mean, y_mono_mean, x_bin, y_bin,
                x_bin_mean, y_bin_mean
            ], f)
Ejemplo n.º 17
0
def main(model_name='map_learning',
         exp_name='localization_all_maps',
         azimuth=12,
         snr=0.2,
         freq_bands=24,
         max_freq=20000,
         elevations=25,
         mean_subtracted_map=True,
         ear='ipsi',
         n_trials=100,
         normalization_type='sum_1',
         sigma_smoothing=0,
         sigma_gauss_norm=1,
         clean=False):
    """ Learns the elevation spectra map gradually over presented sounds and saves the localization quality for each trial
    """
    logger = logging.getLogger(__name__)
    logger.info('Learning different maps for all participants')

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################
    # participant_numbers = np.array([1, 2, 3, 8, 9, 10, 11,
    #                                 12, 15, 17, 18, 19, 20,
    #                                 21, 27, 28, 33, 40, 44,
    #                                 48, 50, 51, 58, 59, 60,
    #                                 61, 65, 119, 124, 126,
    #                                 127, 131, 133, 134, 135,
    #                                 137, 147, 148, 152, 153,
    #                                 154, 155, 156, 158, 162,
    #                                 163, 165])

    participant_numbers = np.array([
        127,
        131,
        133,
        134,
        135,
    ])

    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)
    ########################################################################
    ########################################################################

    # create unique experiment name
    exp_name_str = hp.create_exp_name([
        exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm,
        mean_subtracted_map, time_window,
        int(snr * 100), freq_bands, max_freq, (azimuth - 12) * 10, normalize,
        len(elevations), ear, n_trials
    ])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load
    if not clean and exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with exp_file.open('rb') as f:
            logger.info('Reading model data from file')
            [mono_res, mono_mean_res, bin_res, bin_mean_res,
             trial_used_ss] = pickle.load(f)
    else:

        # store only the localization coefficeints (gain,bias,score)
        mono_res = np.zeros((4, len(participant_numbers), n_trials, 3))
        mono_mean_res = np.zeros((4, len(participant_numbers), n_trials, 3))
        bin_res = np.zeros((4, len(participant_numbers), n_trials, 3))
        bin_mean_res = np.zeros((4, len(participant_numbers), n_trials, 3))
        trial_used_ss = np.zeros((4, len(participant_numbers), n_trials))

        for i_par, par in enumerate(participant_numbers):
            logger.info(
                'Localizing {0:d} trials for participant {1:d}. \n'.format(
                    n_trials, par))

            # create or read the data. psd_all_c = (sounds,elevations,frequency bands)
            psd_all_c, psd_all_i = generateData.create_data(freq_bands,
                                                            par,
                                                            snr,
                                                            normalize,
                                                            azimuth,
                                                            time_window,
                                                            max_freq=max_freq,
                                                            diff_noise=False)

            # Take only given elevations
            psd_all_c = psd_all_c[:, elevations, :]
            psd_all_i = psd_all_i[:, elevations, :]

            # filter data and integrate it
            psd_mono, psd_mono_mean, psd_binaural, psd_binaural_mean = hp.process_inputs(
                psd_all_i, psd_all_c, ear, normalization_type, sigma_smoothing,
                sigma_gauss_norm)

            ### Load different noise data ###
            # create or read the data. psd_all_c = (sounds,elevations,frequency bands)
            psd_all_c_diff_noise, psd_all_i_diff_noise = generateData.create_data(
                freq_bands,
                par,
                snr,
                normalize,
                azimuth,
                time_window,
                max_freq=max_freq,
                diff_noise=True)

            # Take only given elevations
            psd_all_c_diff_noise = psd_all_c_diff_noise[:, elevations, :]
            psd_all_i_diff_noise = psd_all_i_diff_noise[:, elevations, :]

            # filter data and integrate it
            psd_mono_diff_noise, psd_mono_mean_diff_noise, psd_binaural_diff_noise, psd_binaural_mean_diff_noise = hp.process_inputs(
                psd_all_i_diff_noise, psd_all_c_diff_noise, ear,
                normalization_type, sigma_smoothing, sigma_gauss_norm)

            # walk over test n_trials, in this case the number of sound samples
            for i_trials in range(n_trials):

                # decide how many sound samples should be used for the map. this is between 1 and number_of_sounds * number_of_elevations
                number_of_ss = np.random.randint(
                    1, psd_all_c.shape[0] * psd_all_c.shape[1])
                # choose the sound samples to learn the map
                ind = np.random.randint(0,
                                        high=(psd_all_c.shape[0] *
                                              psd_all_c.shape[1]),
                                        size=number_of_ss)
                # get the indices for the sound_inds
                sounds_ind = np.unravel_index(
                    ind, (psd_all_c.shape[0], psd_all_c.shape[1]))

                # decide which type of map is learned_map
                for i_maps in range(4):

                    # monaural condition
                    if i_maps == 0:
                        # get only the defined sounds and elevations
                        tmp_data = np.zeros(psd_mono.shape)
                        tmp_data[sounds_ind[0],
                                 sounds_ind[1], :] = psd_mono[sounds_ind[0],
                                                              sounds_ind[1], :]
                        # create learned_map
                        learned_map = tmp_data.mean(0)
                    elif i_maps == 1:
                        # get only the defined sounds and elevations
                        tmp_data = np.zeros(psd_mono_mean.shape)
                        tmp_data[sounds_ind[0],
                                 sounds_ind[1], :] = psd_mono_mean[
                                     sounds_ind[0], sounds_ind[1], :]
                        # create learned_map
                        learned_map = tmp_data.mean(0)
                    elif i_maps == 2:
                        # get only the defined sounds and elevations
                        tmp_data = np.zeros(psd_binaural.shape)
                        tmp_data[sounds_ind[0],
                                 sounds_ind[1], :] = psd_binaural[
                                     sounds_ind[0], sounds_ind[1], :]
                        # create learned_map
                        learned_map = tmp_data.mean(0)
                    elif i_maps == 3:
                        # get only the defined sounds and elevations
                        tmp_data = np.zeros(psd_binaural_mean.shape)
                        tmp_data[sounds_ind[0],
                                 sounds_ind[1], :] = psd_binaural_mean[
                                     sounds_ind[0], sounds_ind[1], :]
                        # create learned_map
                        learned_map = tmp_data.mean(0)

                    # store the map
                    # learned_maps_participants[i_par, :, :] = learned_map
                    # store the number of sounds used
                    trial_used_ss[i_maps, i_par, i_trials] = number_of_ss

                    # localize the sounds and save the results
                    x, y = hp.localize_sound(psd_mono_diff_noise, learned_map)
                    mono_res[
                        i_maps, i_par,
                        i_trials, :] = hp.get_localization_coefficients_score(
                            x, y)
                    # localize the sounds and save the results
                    x, y = hp.localize_sound(psd_mono_mean_diff_noise,
                                             learned_map)
                    mono_mean_res[
                        i_maps, i_par,
                        i_trials, :] = hp.get_localization_coefficients_score(
                            x, y)

                    # localize the sounds and save the results
                    x, y = hp.localize_sound(psd_binaural_diff_noise,
                                             learned_map)
                    bin_res[
                        i_maps, i_par,
                        i_trials, :] = hp.get_localization_coefficients_score(
                            x, y)

                    # localize the sounds and save the results
                    x, y = hp.localize_sound(psd_binaural_mean_diff_noise,
                                             learned_map)
                    bin_mean_res[
                        i_maps, i_par,
                        i_trials, :] = hp.get_localization_coefficients_score(
                            x, y)

        # create Path
        exp_path.mkdir(parents=True, exist_ok=True)
        with exp_file.open('wb') as f:
            logger.info('Creating model file')
            pickle.dump([
                mono_res, mono_mean_res, bin_res, bin_mean_res, trial_used_ss
            ], f)
Ejemplo n.º 18
0
def main(model_name='hrtf_comparison',
         exp_name='single_participant',
         azimuth=12,
         participant_number=9,
         snr=0.0,
         freq_bands=24,
         max_freq=20000,
         elevations=25,
         mean_subtracted_map=True,
         ear='ipsi',
         normalization_type='sum_1',
         sigma_smoothing=0,
         sigma_gauss_norm=1,
         clean=False):
    """ This script calculates the correlation coefficient between the ipsi- and contralateral HRTF and the learned maps for a single participant.
    """
    logger = logging.getLogger(__name__)
    logger.info(
        'Comparing learned HRTF maps with the actual HRTF of a participant')

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################
    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)
    ########################################################################
    ########################################################################

    # create unique experiment name
    exp_name_str = hp.create_exp_name([
        exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm,
        mean_subtracted_map, time_window,
        int(snr * 100), freq_bands, max_freq, participant_number,
        (azimuth - 12) * 10, normalize,
        len(elevations), ear
    ])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load
    if not clean and exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with exp_file.open('rb') as f:
            logger.info('Reading model data from file')
            [
                hrtfs_i, hrtfs_c, learned_map_mono, learned_map_mono_mean,
                learned_map_bin, learned_map_bin_mean
            ] = pickle.load(f)
    else:

        # create or read the data
        psd_all_c, psd_all_i = generateData.create_data(freq_bands,
                                                        participant_number,
                                                        snr,
                                                        normalize,
                                                        azimuth,
                                                        time_window,
                                                        max_freq=max_freq)

        # filter data and integrate it
        psd_mono, psd_mono_mean, psd_binaural, psd_binaural_mean = hp.process_inputs(
            psd_all_i, psd_all_c, ear, normalization_type, sigma_smoothing,
            sigma_gauss_norm)

        # create map from defined processed data
        learned_map_mono = psd_mono.mean(0)
        learned_map_mono_mean = psd_mono_mean.mean(0)
        learned_map_bin = psd_binaural.mean(0)
        learned_map_bin_mean = psd_binaural_mean.mean(0)
        # learned_map = hp.create_map(psd_mono, False)
        # Get the actual HRTF
        hrtfs_c, hrtfs_i, _ = generateHRTFs.create_data(freq_bands,
                                                        participant_number,
                                                        snr,
                                                        normalize,
                                                        azimuth,
                                                        time_window,
                                                        max_freq=max_freq,
                                                        clean=clean)

        # filter data and integrate it
        # hrtfs_c = hp.filter_dataset(hrtfs_c, normalization_type=normalization_type,
        #                                sigma_smoothing=0, sigma_gauss_norm=0)
        hrtfs_c, psd_mono_mean, psd_binaural, psd_binaural_mean = hp.process_inputs(
            hrtfs_i, hrtfs_c, 'contra', normalization_type, sigma_smoothing,
            sigma_gauss_norm)

        hrtfs_i, psd_mono_mean, psd_binaural, psd_binaural_mean = hp.process_inputs(
            hrtfs_i, hrtfs_c, 'ipsi', normalization_type, sigma_smoothing,
            sigma_gauss_norm)

        # remove mean for later comparison
        hrtfs_c = np.squeeze(hrtfs_c[0, elevations, :])
        hrtfs_c -= hrtfs_c.mean()
        hrtfs_i = np.squeeze(hrtfs_i[0, elevations, :])
        hrtfs_i -= hrtfs_i.mean()

        # remove unwanted elevations
        learned_map_mono = learned_map_mono[elevations, :]
        learned_map_mono_mean = learned_map_mono_mean[elevations, :]
        learned_map_bin = learned_map_bin[elevations, :]
        learned_map_bin_mean = learned_map_bin_mean[elevations, :]

        learned_map_mono -= learned_map_mono.mean()
        learned_map_mono_mean -= learned_map_mono_mean.mean()
        learned_map_bin -= learned_map_bin.mean()
        learned_map_bin_mean -= learned_map_bin_mean.mean()

        # ## calculate pearson index
        # correlations[i_par, 0, 0] = pearson2d(learned_map_mono, hrtfs_i)
        # correlations[i_par, 0, 1] = pearson2d(learned_map_mono, hrtfs_c)
        #
        # correlations[i_par, 1, 0] = pearson2d(
        #     learned_map_mono_mean, hrtfs_i)
        # correlations[i_par, 1, 1] = pearson2d(
        #     learned_map_mono_mean, hrtfs_c)
        #
        # correlations[i_par, 2, 0] = pearson2d(learned_map_bin, hrtfs_i)
        # correlations[i_par, 2, 1] = pearson2d(learned_map_bin, hrtfs_c)
        #
        # correlations[i_par, 3, 0] = pearson2d(
        #     learned_map_bin_mean, hrtfs_i)
        # correlations[i_par, 3, 1] = pearson2d(
        #     learned_map_bin_mean, hrtfs_c)

        # create Path
        exp_path.mkdir(parents=True, exist_ok=True)
        with exp_file.open('wb') as f:
            logger.info('Creating model file')
            pickle.dump([
                hrtfs_i, hrtfs_c, learned_map_mono, learned_map_mono_mean,
                learned_map_bin, learned_map_bin_mean
            ], f)
Ejemplo n.º 19
0
def main(save_figs=False,
         save_type='svg',
         model_name='single_participant',
         exp_name='single_participant_default',
         azimuth=12,
         participant_number=9,
         snr=0.2,
         freq_bands=24,
         max_freq=20000,
         elevations=25,
         mean_subtracted_map=True,
         ear='ipsi',
         normalization_type='sum_1',
         sigma_smoothing=0,
         sigma_gauss_norm=1):

    logger = logging.getLogger(__name__)
    logger.info('Showing localization results for a single participant')

    # make sure save type is given
    if not save_type or len(save_type) == 0:
        save_type = 'svg'

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################
    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)

    ########################################################################
    ########################################################################

    exp_name_str = hp.create_exp_name([
        exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm,
        mean_subtracted_map, time_window,
        int(snr * 100), freq_bands, max_freq, participant_number,
        (azimuth - 12) * 10, normalize,
        len(elevations), ear
    ])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load
    if exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with open(exp_file.as_posix(), 'rb') as f:
            logger.info('Reading model data from file')
            [
                localization_results_binaural, localization_results_monaural,
                q_ele_all, r_ipsi_all
            ] = pickle.load(f)

        fig = plt.figure(figsize=(20, 5))

        axes = fig.subplots(1, 4, squeeze=False, sharex=False, sharey=False)

        ax1 = axes[0, 0]
        ax1.set_title('Monaural')

        hp_vis.plot_localization_result(localization_results_monaural[:, :, 0],
                                        localization_results_monaural[:, :, 1],
                                        ax=ax1,
                                        sound_files=SOUND_FILES,
                                        scale_values=False,
                                        disp_values=True)
        # ax1.set_ylim([0,25])
        # ax1.set_xlim([0,25])

        ax1 = axes[0, 1]
        ax1.set_title('Monaural - Prior ')

        hp_vis.plot_localization_result(localization_results_monaural[:, :, 0],
                                        localization_results_monaural[:, :, 2],
                                        ax=ax1,
                                        sound_files=SOUND_FILES,
                                        scale_values=True,
                                        disp_values=True)
        # ax1.set_ylim([0,25])
        # ax1.set_xlim([0,25])

        ax1 = axes[0, 2]
        ax1.set_title('Binaural')
        hp_vis.plot_localization_result(localization_results_binaural[:, :, 0],
                                        localization_results_binaural[:, :, 1],
                                        ax=ax1,
                                        sound_files=SOUND_FILES,
                                        scale_values=True,
                                        disp_values=True)
        # ax1.set_ylim([0,25])
        # ax1.set_xlim([0,25])

        ax1 = axes[0, 3]
        ax1.set_title('Binaural - Prior')
        hp_vis.plot_localization_result(localization_results_binaural[:, :, 0],
                                        localization_results_binaural[:, :, 2],
                                        ax=ax1,
                                        sound_files=SOUND_FILES,
                                        scale_values=True,
                                        disp_values=True)
        # ax1.set_ylim([0,25])
        # ax1.set_xlim([0,25])

        plt.tight_layout()

        if save_figs:
            fig_save_path = ROOT / 'reports' / 'figures' / exp_name_str / model_name
            if not fig_save_path.exists():
                fig_save_path.mkdir(parents=True, exist_ok=True)
            plt.savefig(
                (fig_save_path / (model_name + '_' + exp_name +
                                  '_localization.' + save_type)).as_posix(),
                dpi=300)

        else:
            plt.show()
    else:
        logger.error('No data set found. Run model first!')
        logger.error(exp_file)
Ejemplo n.º 20
0
def main(save_figs=False, save_type='svg', model_name='all_participants', exp_name='localization_default', azimuth=12, freq_bands=24, max_freq=20000, elevations=25, snr=0.2, mean_subtracted_map=True, ear='ipsi', normalization_type='sum_1', sigma_smoothing=0, sigma_gauss_norm=1, clean=False):

    logger = logging.getLogger(__name__)
    logger.info('Showing localization results for all participants')

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################

    # make sure save type is given
    if not save_type or len(save_type) == 0:
        save_type = 'svg'

    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)

    snrs = np.arange(0.0, 1.1, 0.1)
    ########################################################################
    ########################################################################

    # create unique experiment name
    exp_name_str = hp.create_exp_name([exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm, mean_subtracted_map,
                                       time_window, freq_bands, max_freq, (azimuth - 12) * 10, normalize, len(elevations), ear])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load
    if exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with open(exp_file.as_posix(), 'rb') as f:
            logger.info('Reading model data from file')
            [scores] = pickle.load(f)

        snrs_all = np.tile(snrs, (scores.shape[0], 1))
        snrs_all = np.reshape(snrs_all, (snrs_all.shape[0] * snrs_all.shape[1]))
        scores_tmp = np.reshape(scores, (scores.shape[0] * scores.shape[1], scores.shape[2], scores.shape[3]))
        fig = plt.figure(figsize=(20, 7))

        ax = fig.add_subplot(1, 3, 1)
        y = scores_tmp[:, 0, 0]
        sns.regplot(x=snrs_all, y=y, x_estimator=np.mean, order=2, ax=ax, color=hp_vis.C0, label='Mono')

        y = scores_tmp[:, 1, 0]
        sns.regplot(x=snrs_all, y=y, x_estimator=np.mean, order=2, ax=ax, color=hp_vis.C1, label='Mono-Mean')

        y = scores_tmp[:, 2, 0]
        sns.regplot(x=snrs_all, y=y, x_estimator=np.mean, order=2, ax=ax, color=hp_vis.C2, label='Bin')

        y = scores_tmp[:, 3, 0]
        sns.regplot(x=snrs_all, y=y, x_estimator=np.mean, order=2, ax=ax, color=hp_vis.C3, label='Bin-Mean')
        ax.set_ylabel('Gain')
        ax.set_xlabel('SNR')
        ax.set_ylim([0.0, 1.1])

        ax = fig.add_subplot(1, 3, 2)
        y = scores_tmp[:, 0, 1]
        sns.regplot(x=snrs_all, y=y, x_estimator=np.mean, order=2, ax=ax, color=hp_vis.C0, label='Mono')

        y = scores_tmp[:, 1, 1]
        sns.regplot(x=snrs_all, y=y, x_estimator=np.mean, order=2, ax=ax, color=hp_vis.C1, label='Mono-Mean')

        y = scores_tmp[:, 2, 1]
        sns.regplot(x=snrs_all, y=y, x_estimator=np.mean, order=2, ax=ax, color=hp_vis.C2, label='Bin')

        y = scores_tmp[:, 3, 1]
        sns.regplot(x=snrs_all, y=y, x_estimator=np.mean, order=2, ax=ax, color=hp_vis.C3, label='Bin-Mean')
        ax.set_ylabel('Bias')
        ax.set_xlabel('SNR')
        ax.set_ylim([0.0, 30])

        ax = fig.add_subplot(1, 3, 3)
        y = scores_tmp[:, 0, 2]
        sns.regplot(x=snrs_all, y=y, x_estimator=np.mean, order=2, ax=ax, color=hp_vis.C0, label='Monaural')

        y = scores_tmp[:, 1, 2]
        sns.regplot(x=snrs_all, y=y, x_estimator=np.mean, order=2, ax=ax, color=hp_vis.C1, label='Mono-Mean')

        y = scores_tmp[:, 2, 2]
        sns.regplot(x=snrs_all, y=y, x_estimator=np.mean, order=2, ax=ax, color=hp_vis.C2, label='Binaural')

        y = scores_tmp[:, 3, 2]
        sns.regplot(x=snrs_all, y=y, x_estimator=np.mean, order=2, ax=ax, color=hp_vis.C3, label='Bin-Mean')
        ax.set_ylabel('Score')
        ax.set_xlabel('SNR')
        ax.set_ylim([0.0, 1.4])

        lgd = ax.legend(loc='upper center', bbox_to_anchor=(1.35, 0.8))

        plt.tight_layout()

        if save_figs:
            exp_name_str = hp.create_exp_name([exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm, mean_subtracted_map, time_window, int(
            snr * 100), freq_bands, max_freq, (azimuth - 12) * 10, normalize, len(elevations), ear])
            fig_save_path = ROOT / 'reports' / 'figures' / exp_name_str / model_name
            if not fig_save_path.exists():
                fig_save_path.mkdir(parents=True, exist_ok=True)
            logger.info('Saving figures to ' + fig_save_path.as_posix())
            plt.savefig((fig_save_path / (model_name + '_' + exp_name + '_localization.' + save_type)).as_posix(), dpi=300)
        else:
            plt.show()
    else:
        logger.error('No data set found. Run model first!')
        logger.error(exp_file)
Ejemplo n.º 21
0
def main(model_name='train_network_single_participant',
         exp_name='single_participant_default',
         azimuth=12,
         participant_number=9,
         snr=0.0,
         freq_bands=24,
         max_freq=20000,
         elevations=25,
         mean_subtracted_map=True,
         ear='ipsi',
         normalization_type='sum_1',
         sigma_smoothing=0,
         sigma_gauss_norm=1,
         clean=False,
         steady_state=False):
    """ This script takes the filtered data and tries to localize sounds with a learned map
        for a single participant.
    """
    logger = logging.getLogger(__name__)
    logger.info('Localizing sounds for a single participant')

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################
    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)
    ########################################################################
    ########################################################################

    # create unique experiment name
    exp_name_str = hp.create_exp_name([
        exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm,
        mean_subtracted_map, time_window,
        int(snr * 100), freq_bands, max_freq, participant_number,
        (azimuth - 12) * 10, normalize,
        len(elevations), ear
    ])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / (exp_name_str + '_weights')

    # check if model results exist already and load
    if not clean and exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with exp_file.open('rb') as f:
            logger.info('Reading model data from file')
            [w] = pickle.load(f)
    else:

        # create Path
        exp_path.mkdir(parents=True, exist_ok=True)
        # create or read the data
        psd_all_c, psd_all_i = generateData.create_data(freq_bands,
                                                        participant_number,
                                                        snr,
                                                        normalize,
                                                        azimuth,
                                                        time_window,
                                                        max_freq=max_freq)

        # Take only given elevations
        input_c = psd_all_c[:, elevations, :]
        input_i = psd_all_i[:, elevations, :]

        # normalize inputs over frequencies
        input_c = input_c / input_c.sum(2)[:, :, np.newaxis]
        input_i = input_i / input_i.sum(2)[:, :, np.newaxis]

        # initialize network. if steady_state is True run do not use euler but calculate the response immediatley
        net = network.Network(steady_state=steady_state)

        # if we use the steady state response to learn, we need more trials
        if steady_state:
            trials = 1500 * 10
        else:
            trials = 25

        for ele in range(trials):
            # for i_ele, ele in enumerate(elevations):
            ele = np.random.randint(0, len(elevations))
            sound = np.random.randint(0, len(SOUND_FILES))
            # sound = 1
            # ele = 1
            in_i = input_i[sound, ele]
            in_c = input_c[sound, ele]

            q_ele, r_ipsi, w, w_sounds_i, w_sounds_c = net.run(in_i,
                                                               in_c,
                                                               ele,
                                                               sound,
                                                               train=True,
                                                               prior_info=True)

            # logger.info('Sound No: ' + str(sound + 1) + ' of ' + str(len(SOUND_FILES)) +
            #             '.  -> Elevation : ' + str(ele + 1) + ' of ' + str(len(elevations)))

        with exp_file.open('wb') as f:
            logger.info('Creating model file')
            pickle.dump([w, w_sounds_i, w_sounds_c], f)
Ejemplo n.º 22
0
def main(model_name='localize_single_participant',
         exp_name='single_participant_default',
         azimuth=12,
         participant_number=9,
         snr=0.0,
         freq_bands=24,
         max_freq=20000,
         elevations=25,
         mean_subtracted_map=True,
         ear='ipsi',
         normalization_type='sum_1',
         sigma_smoothing=0,
         sigma_gauss_norm=1,
         clean=False,
         steady_state=False):
    """ This script takes the filtered data and tries to localize sounds with a learned map
        for a single participant.
    """
    logger = logging.getLogger(__name__)
    logger.info('Localizing sounds for a single participant')

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################
    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)
    ########################################################################
    ########################################################################

    # create unique experiment name
    exp_name_str = hp.create_exp_name([
        exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm,
        mean_subtracted_map, time_window,
        int(snr * 100), freq_bands, max_freq, participant_number,
        (azimuth - 12) * 10, normalize,
        len(elevations), ear
    ])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str

    # store responses
    r_ipsi_all = np.zeros((len(SOUND_FILES), len(elevations), freq_bands))
    q_ele_all = np.zeros((len(SOUND_FILES), len(elevations), len(elevations)))
    localization_results_binaural = np.zeros(
        (len(SOUND_FILES), len(elevations), 3))
    localization_results_monaural = np.zeros(
        (len(SOUND_FILES), len(elevations), 3))

    # check if model results exist already and load
    if not clean and exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with exp_file.open('rb') as f:
            logger.info('Reading model data from file')
            [
                localization_results_binaural, localization_results_monaural,
                q_ele_all, r_ipsi_all
            ] = pickle.load(f)
    else:
        # create Path
        exp_path.mkdir(parents=True, exist_ok=True)
        # create or read the data
        psd_all_c, psd_all_i = generateData.create_data(freq_bands,
                                                        participant_number,
                                                        snr,
                                                        normalize,
                                                        azimuth,
                                                        time_window,
                                                        max_freq=max_freq)

        # Take only given elevations
        input_c = psd_all_c[:, elevations, :]
        input_i = psd_all_i[:, elevations, :]

        # normalize inputs over frequencies
        input_c = input_c / input_c.sum(2)[:, :, np.newaxis]
        input_i = input_i / input_i.sum(2)[:, :, np.newaxis]

        # initialize network. if steady_state is True run do not use euler but calculate the response immediatley
        net = network.Network(steady_state=steady_state)

        # read previously read network weights
        exp_file_weights = Path(exp_file.as_posix() + '_weights')
        with exp_file_weights.open('rb') as f:
            logger.info('Reading model data from file')
            [w, w_sounds_i, w_sounds_c] = pickle.load(f)

        # normalize weights
        net.w = net.normalize_weights(w)
        net.w_sounds_i = w_sounds_i
        net.w_sounds_c = w_sounds_c

        ############## MONAURAL #################
        # walk over sounds
        for sound, _ in enumerate(SOUND_FILES):
            for i_ele, ele in enumerate(elevations):

                in_i = input_i[sound, ele]
                in_c = np.zeros(input_c[sound, ele].shape) + 0.1

                # NO PRIOR
                q_ele, r_ipsi, w, w_sounds_i, w_sounds_c = net.run(
                    in_i, in_c, ele, sound, train=False, prior_info=False)
                # localize and save results
                localization_results_monaural[sound, i_ele, 0] = ele
                localization_results_monaural[sound, i_ele,
                                              1] = q_ele[-1, :].argmax()

                # PRIOR
                q_ele, r_ipsi, w, w_sounds_i, w_sounds_c = net.run(
                    in_i, in_c, ele, sound, train=False, prior_info=True)
                # localize and save results
                localization_results_monaural[sound, i_ele,
                                              2] = q_ele[-1, :].argmax()

        ############## BINAURAL #################
        # walk over sounds
        for sound, _ in enumerate(SOUND_FILES):
            for i_ele, ele in enumerate(elevations):

                in_i = input_i[sound, ele]
                in_c = input_c[sound, ele]

                # NO PRIOR
                q_ele, r_ipsi, w, w_sounds_i, w_sounds_c = net.run(
                    in_i, in_c, ele, sound, train=False, prior_info=False)
                # localize and save results
                localization_results_binaural[sound, i_ele, 0] = ele
                localization_results_binaural[sound, i_ele,
                                              1] = q_ele[-1, :].argmax()

                # PRIOR
                q_ele, r_ipsi, w, w_sounds_i, w_sounds_c = net.run(
                    in_i, in_c, ele, sound, train=False, prior_info=True)
                # localize and save results
                localization_results_binaural[sound, i_ele,
                                              2] = q_ele[-1, :].argmax()

        with exp_file.open('wb') as f:
            logger.info('Creating model file')
            pickle.dump([
                localization_results_binaural, localization_results_monaural,
                q_ele_all, r_ipsi_all
            ], f)
Ejemplo n.º 23
0
def main(save_figs=False,
         save_type='svg',
         model_name='single_participant',
         exp_name='single_participant_default',
         azimuth=12,
         participant_number=9,
         snr=0.2,
         freq_bands=24,
         max_freq=20000,
         elevations=25,
         mean_subtracted_map=True,
         ear='ipsi',
         normalization_type='sum_1',
         sigma_smoothing=0,
         sigma_gauss_norm=1):

    logger = logging.getLogger(__name__)
    logger.info('Showing localization results for a single participant')

    # make sure save type is given
    if not save_type or len(save_type) == 0:
        save_type = 'svg'

    ########################################################################
    ######################## Set parameters ################################
    ########################################################################
    normalize = False
    time_window = 0.1  # time window in sec

    elevations = np.arange(0, elevations, 1)

    ########################################################################
    ########################################################################

    exp_name_str = hp.create_exp_name([
        exp_name, normalization_type, sigma_smoothing, sigma_gauss_norm,
        mean_subtracted_map, time_window,
        int(snr * 100), freq_bands, max_freq, participant_number,
        (azimuth - 12) * 10, normalize,
        len(elevations), ear
    ])

    exp_path = ROOT / 'models' / model_name
    exp_file = exp_path / exp_name_str
    # check if model results exist already and load
    if exp_path.exists() and exp_file.is_file():
        # try to load the model files
        with open(exp_file.as_posix(), 'rb') as f:
            logger.info('Reading model data from file')
            [
                x_mono, y_mono, x_mono_mean, y_mono_mean, x_bin, y_bin,
                x_bin_mean, y_bin_mean
            ] = pickle.load(f)

        # define which elevations should be used
        print(y_mono.shape)
        x_mono = x_mono[:, elevations, :]
        y_mono = y_mono[:, elevations]
        x_mono_mean = x_mono_mean[:, elevations, :]
        y_mono_mean = y_mono_mean[:, elevations]
        x_bin = x_bin[:, elevations, :]
        y_bin = y_bin[:, elevations]
        x_bin_mean = x_bin_mean[:, elevations, :]
        y_bin_mean = y_bin_mean[:, elevations]

        fig = plt.figure(figsize=(20, 5))
        # plt.suptitle('Single Participant')
        # Monoaural Data (Ipsilateral), No Mean Subtracted
        ax = fig.add_subplot(1, 4, 1)
        hp_vis.plot_localization_result(x_mono,
                                        y_mono,
                                        ax,
                                        SOUND_FILES,
                                        scale_values=True,
                                        linear_reg=True,
                                        disp_values=True)
        ax.set_title('Monoaural')
        hp_vis.set_axis(ax, len(elevations))
        ax.set_ylabel('Estimated Elevation [deg]')
        ax.set_xlabel('True Elevation [deg]')

        # Monoaural Data (Ipsilateral),Mean Subtracted
        ax = fig.add_subplot(1, 4, 2)
        hp_vis.plot_localization_result(x_mono_mean,
                                        y_mono_mean,
                                        ax,
                                        SOUND_FILES,
                                        scale_values=True,
                                        linear_reg=True,
                                        disp_values=True)
        ax.set_title('Mono - Prior')
        hp_vis.set_axis(ax, len(elevations))
        ax.set_xlabel('True Elevation [deg]')

        # Binaural Data (Ipsilateral), No Mean Subtracted
        ax = fig.add_subplot(1, 4, 3)
        hp_vis.plot_localization_result(x_bin,
                                        y_bin,
                                        ax,
                                        SOUND_FILES,
                                        scale_values=True,
                                        linear_reg=True,
                                        disp_values=True)
        ax.set_title('Binaural')
        hp_vis.set_axis(ax, len(elevations))
        ax.set_xlabel('True Elevation [deg]')

        # Binaural Data (Ipsilateral), Mean Subtracted
        ax = fig.add_subplot(1, 4, 4)
        hp_vis.plot_localization_result(x_bin_mean,
                                        y_bin_mean,
                                        ax,
                                        SOUND_FILES,
                                        scale_values=True,
                                        linear_reg=True,
                                        disp_values=True)
        ax.set_title('Bin - Prior')
        hp_vis.set_axis(ax, len(elevations))
        ax.set_xlabel('True Elevation [deg]')

        plt.tight_layout()

        if save_figs:
            exp_name_str = hp.create_exp_name([
                exp_name, normalization_type, sigma_smoothing,
                sigma_gauss_norm, mean_subtracted_map, time_window,
                int(snr * 100), freq_bands, max_freq, (azimuth - 12) * 10,
                normalize,
                len(elevations), ear
            ])
            fig_save_path = ROOT / 'reports' / 'figures' / exp_name_str / model_name
            if not fig_save_path.exists():
                fig_save_path.mkdir(parents=True, exist_ok=True)
            plt.savefig(
                (fig_save_path / (model_name + '_' + exp_name +
                                  '_participant_' + str(participant_number) +
                                  '_localization.' + save_type)).as_posix(),
                dpi=300)

        else:
            plt.show()
    else:
        logger.error('No data set found. Run model first!')
        logger.error(exp_file)