Exemplo n.º 1
0
    def get_next_cost_dict(self, params_dict):
        # Get parameters from the provided dictionary
        params = params_dict['params']

        #wave = self.gaussian_interpolation(params=x)
        wave = sf.wave_normalization(params)

        filename = 'E:/python/documents/wave_opt/gausswave.csv'
        sf.array_to_csv(filename=filename, array=wave)

        signal = [-1]
        gate = sf.convey_params_get_cost(signal)

        filename_SQ = 'D:/shot noise/' + sf.today_str() + '/try/SQ.csv'
        filename_ASQ = 'D:/shot noise/' + sf.today_str() + '/try/ASQ.csv'
        df_SQ = pd.read_csv(filename_SQ, header=None).values
        df_ASQ = pd.read_csv(filename_ASQ, header=None).values
        res = df_SQ - df_ASQ
        res = res[np.where(np.abs(res - np.mean(res)) < 3 * gate)]
        varian = np.var(res) / np.mean(df_SQ)
        print('var is:', varian)
        cost = 100 * np.log10(varian)

        uncer = 0
        bad = False
        cost_dict = {'cost': cost, 'uncer': uncer, 'bad': bad}
        return cost_dict
    def run(self, csv_file=None, signal=[1]):
        if not csv_file:
            if self.init_wave == 'g':
                wave = self.gauss_wave()
            elif self.init_wave == 't':
                wave = self.triangle_wave()
            elif self.init_wave == 's':
                wave = self.square_wave()
        else:
            while not os.path.isfile(csv_file):
                csv_file = input('Please input the path of Arbitary Wave:')
            wave = self.AWG(csv_file)
        wave = sf.wave_normalization(wave)
        sf.array_to_csv(array=wave, filename=self.filename)
        if self.path_today:
            sf.array_to_csv(array=wave,
                            filename=self.path_today + 'initwave' + '.csv')

        for i in range(self.nums_iter):
            print('Run:', i + 1)
            self.signal = signal[0]
            cost = sf.convey_params_get_cost(params=signal)
            if self.signal < 0:
                filename_SQ = 'D:/shot noise/' + sf.today_str() + '/try/SQ.csv'
                filename_ASQ = 'D:/shot noise/' + sf.today_str(
                ) + '/try/ASQ.csv'
                df_SQ = pd.read_csv(filename_SQ, header=None).values
                df_ASQ = pd.read_csv(filename_ASQ, header=None).values
                res = df_SQ - df_ASQ
                gate = cost
                res = res[np.where(np.abs(res - np.mean(res)) < 3 * gate)]
                var_diff = 10 * np.log10(np.var(res) / np.mean(df_SQ))
                print('var_diff is:', var_diff)
            self.cost_list.append(var_diff)
            self.generate_wave(count=i + 1)

        if self.path_today:
            filename = self.path_today + 'Gain_vs_run' + '.csv'
            sf.array_to_csv(array=self.cost_list, filename=filename)
            filename = self.path_today + 'Seed intensity_vs_run' + '.csv'
            sf.array_to_csv(array=self.seed_intensity, filename=filename)

        self.plot_costs_vs_run()
    def get_next_cost_dict(self, params_dict):
        # Get parameters from the provided dictionary
        params = params_dict['params']
        #wave = self.gaussian_interpolation(params=x)
        wave = sf.wave_normalization(params)
        filename = 'E:/python/documents/wave_opt/gausswave.csv'
        sf.array_to_csv(filename=filename, array=wave)

        siganl = [1]
        gain = sf.convey_params_get_cost(siganl)

        if gain < 0.002 or np.max(wave) >= 2:
            bad = True
            cost = 1
            uncer = 0
        else:
            wave = wave / abs(gain) * self.paras_scale
            sf.array_to_csv(filename=filename, array=wave)

            signal = [-1]
            gate = sf.convey_params_get_cost(signal)

            filename_SQ = 'D:/shot noise/' + sf.today_str() + '/try/SQ.csv'
            filename_ASQ = 'D:/shot noise/' + sf.today_str() + '/try/ASQ.csv'
            df_SQ = pd.read_csv(filename_SQ, header=None).values
            df_ASQ = pd.read_csv(filename_ASQ, header=None).values

            res = df_SQ - df_ASQ

            df_SQ = df_SQ[np.where(np.abs(res - np.mean(res)) < (3 * gate))]
            res = res[np.where(np.abs(res - np.mean(res)) < (3 * gate))]

            varian = np.var(res) / np.mean(df_SQ)
            print('var is:', varian)

            cost = 10 * np.log10(varian)
            bad = False
            uncer = sf.estim_noise_uncer(res) * 10 / np.var(res) / np.log(10)

        cost_dict = {'cost': cost, 'uncer': uncer, 'bad': bad}
        return cost_dict
Exemplo n.º 4
0
def stokes_test():
    wave = generate_wave()
    wave = sf.wave_normalization(wave)

    filename = 'E:/python/documents/wave_opt/gausswave.csv'
    sf.array_to_csv(array=wave, filename=filename)
    signal = [-1]
    gate = sf.convey_params_get_cost(signal)
    if signal[0] < 0:
        filename_SQ = 'D:/shot noise/' + sf.today_str() + '/try/SQ.csv'
        filename_ASQ = 'D:/shot noise/' + sf.today_str() + '/try/ASQ.csv'
        filename_excited = 'D:/shot noise/' + sf.today_str(
        ) + '/try/excited.csv'
        df_SQ = pd.read_csv(filename_SQ, header=None).values
        df_ASQ = pd.read_csv(filename_ASQ, header=None).values
        df_excited = pd.read_csv(filename_excited, header=None).values
        df_SQ = df_SQ - np.min(df_excited) * len(df_excited)
        inten_SQ = np.mean(df_SQ)
        std_SQ = np.var(df_SQ)
        print('intensity of SQ and var of SQ are:', inten_SQ, std_SQ)
    return inten_SQ, std_SQ
Exemplo n.º 5
0
def seedlight_test():
    wave = generate_wave()
    wave = sf.wave_normalization(wave)

    filename = 'E:/python/documents/wave_opt/gausswave.csv'
    sf.array_to_csv(array=wave, filename=filename)
    signal = [-1]
    gate = sf.convey_params_get_cost(signal)
    if signal[0] < 0:
        filename_seed = 'D:/shot noise/' + sf.today_str() + '/try2/seed.csv'
        filename_everyseed = 'D:/shot noise/' + sf.today_str(
        ) + '/try2/everyseed.csv'
        df_seed = pd.read_csv(filename_seed, header=None).values
        df_everyseed = pd.read_csv(filename_everyseed, header=None).values
        df_everyseed = df_everyseed - np.min(df_seed) * len(df_seed)
        inten_seed = np.mean(df_everyseed)
        var_seed = np.std(df_everyseed)
        print('intensity of seedlight and std of seedlight are:', inten_seed,
              var_seed)

    return inten_seed, var_seed
    def generate_wave(self, count):
        filename_excited = 'D:/shot noise/' + sf.today_str(
        ) + '/try/excited.csv'
        filename_seed = 'D:/shot noise/' + sf.today_str() + '/try2/seed.csv'

        df = pd.read_csv(filename_excited, header=None)
        df_seed = pd.read_csv(filename_seed, header=None)

        wave_seed = df_seed.values.flatten()
        self.seed_intensity.append(np.sum(wave_seed))
        print('seedlight:', np.sum(wave_seed))

        wave = df.values.flatten()
        wave = sf.wave_normalization(wave)
        wave = np.maximum(wave, 0)
        sf.array_to_csv(filename=self.filename, array=wave)

        if self.path_today:
            savefile_name = self.path_today + str(count) + '.csv'
            seedfile = self.path_today + 'seedlight' + str(count) + '.csv'
            sf.array_to_csv(filename=savefile_name, array=wave)
            sf.array_to_csv(filename=seedfile, array=wave_seed)
Exemplo n.º 7
0
    def get_next_cost_dict(self, params_dict):
        # Get parameters from the provided dictionary
        params = params_dict['params']

        # input_inter = s.gaussian_interpolation(x)  # GPR interpolation
        input_inter = sf.wave_normalization(params)
        filename = 'E:/python/documents/wave_opt/gausswave.csv'
        sf.array_to_csv(filename=filename,
                        array=input_inter)  # save wave into a csv file

        # To optimize the gain, signal=[1] is to tell labview the optimizing target is gain
        signal = [1]
        gain = sf.convey_params_get_cost(signal)
        '''
        filename_seedlight = 'D:/shot noise/' + today_str() + '/try2/SQ.csv'
        df = pd.read_csv(filename_seedlight, header=None)
        wave = np.array(df)
        array_to_csv(filename=filename,array=wave)

        signal = [1]
        cost = sf.convey_params_get_cost(signal)  
        '''
        if gain > 0:
            cost = 10 * np.log10(
                1 / gain
            )  # the target is the minimum value of the cost, also is the maximum value of the gain
            uncer = 0
            bad = False
        else:
            cost = 10 * np.log10(-1 / gain)
            uncer = 0
            bad = True

        cost_dict = {'cost': cost, 'uncer': uncer, 'bad': bad}

        return cost_dict
def main():
    # create a file folder to save today's data
    today = str(datetime.date.today())
    path_today = 'E:/python/code/data/' + today
    if not os.path.exists(path_today):
        os.makedirs(path_today)

    n = 7
    interface_2 = CustomInterface_2(n)
    controller = mlc.create_controller(
        interface_2,
        #training_type='differential_evolution',
        controller_type='neural_net',
        max_num_runs=100,
        keep_prob=0.8,
        num_params=n,
        min_boundary=[0.2] * n,
        max_boundary=[1] * n)

    # start the second optimization
    controller.optimize()

    # create the file folder of this optimization
    start_datetime = str(controller.start_datetime)[11:19]
    start_time = sf.change_colon_into_dash(start_datetime)
    path_today += '/' + str(start_time)
    os.makedirs(path_today)

    # save the best wave
    res = sf.add_zero(controller.best_params)
    filename = path_today + '/best_paras_2_' + str(start_time) + '.csv'
    sf.array_to_csv(filename=filename, array=res)

    # save the predicted wave
    res = sf.add_zero(controller.predicted_best_parameters)

    filename = path_today + '/pre_best_paras_2_' + str(start_time) + '.csv'
    sf.array_to_csv(filename=filename, array=res)

    # save the seed light of the predicted wave
    res = sf.wave_normalization(res)

    filename = 'E:/python/documents/wave_opt/gausswave.csv'
    sf.array_to_csv(filename=filename, array=res)
    siganl = [1]
    gain = sf.convey_params_get_cost(siganl)

    res = res / gain * interface_2.paras_scale
    filename = 'E:/python/documents/wave_opt/gausswave.csv'
    sf.array_to_csv(filename=filename, array=res)

    signal = [-1]
    gate = sf.convey_params_get_cost(signal)

    filename_SQ = 'D:/shot noise/' + sf.today_str() + '/try/SQ.csv'
    filename_ASQ = 'D:/shot noise/' + sf.today_str() + '/try/ASQ.csv'
    df_SQ = pd.read_csv(filename_SQ, header=None).values
    df_ASQ = pd.read_csv(filename_ASQ, header=None).values
    res = df_SQ - df_ASQ
    df_SQ = df_SQ[np.where(np.abs(res - np.mean(res)) < 3 * gate)]
    res = res[np.where(np.abs(res - np.mean(res)) < 3 * gate)]
    cost = np.var(res) / np.mean(df_SQ)
    print('predicted best cost_2 is:', controller.predicted_best_cost)
    print('actual cost_2 is:', 10 * np.log10(cost))

    filename_seedlight = 'D:/shot noise/' + sf.today_str() + '/try2/seed.csv'
    filename = path_today + '/seedlight_2_' + str(start_time) + '.csv'
    df = pd.read_csv(filename_seedlight, header=None)
    df.to_csv(filename, header=0, index=0)

    # save the excited stokes light
    filename_excited = 'D:/shot noise/' + sf.today_str() + '/try/excited.csv'
    filename_SQ = 'D:/shot noise/' + sf.today_str() + '/try/SQ.csv'
    filename = path_today + '/excited_light_2_' + str(start_time) + '.csv'
    df = pd.read_csv(filename_excited, header=None)
    df.to_csv(filename, header=0, index=0)
    filename_SQ = 'D:/shot noise/' + sf.today_str() + '/try/SQ.csv'
    filename = path_today + '/SQ_2_' + str(start_time) + '.csv'
    df = pd.read_csv(filename_SQ, header=None)
    df.to_csv(filename, header=0, index=0)

    # save the anti-stokes light
    filename_excited = 'D:/shot noise/' + sf.today_str(
    ) + '/try/antistokes.csv'
    filename = path_today + '/antistokes_2_' + str(start_time) + '.csv'
    df = pd.read_csv(filename_excited, header=None)
    df.to_csv(filename, header=0, index=0)
    filename_ASQ = 'D:/shot noise/' + sf.today_str() + '/try/ASQ.csv'
    filename = path_today + '/ASQ_2_' + str(start_time) + '.csv'
    df = pd.read_csv(filename_ASQ, header=None)
    df.to_csv(filename, header=0, index=0)

    # show visualizations
    mlv.show_all_default_visualizations(controller)