def perform_convergence(conv_params, conv_1, conv_2, log_file): """Performs the convergence calculations for each convergence parameter and binary state Parameters ---------- conv_params : dict List of user supplied convergence parameters conv_1 : DataFrame Cumulative data set of conv arrays conv_2 : DataFrame Most recent data set of conv array from most recent BSE run log_file : file write File to log matches or if the convergence params are not appropriate e.g. eccentricity for a disrupted system Returns ------- match : array Matches for each cumulative data set """ match_all = [] for conv_param in conv_params: if (conv_param == 'ecc') and (np.all(conv_1[conv_param] < 1e-7)): log_file.write('{0} is circular or disrupted for all conv binaries\n'.format(conv_param)) match_all.append(-9) elif (conv_param == 'ecc') and (np.all(conv_1[conv_param] == -1.0)): log_file.write('{0} is the same for all disrupted binaries\n'.format(conv_param)) match_all.append(-9) elif (conv_param == 'ecc'): conv_1_ecc = conv_1.loc[conv_1.ecc > 0] conv_2_ecc = conv_2.loc[conv_2.ecc > 0] if len(conv_2_ecc) == len(conv_1_ecc): conv_2_ecc = conv_2[:int(len(conv_2_ecc)/2)] match_compute, bw = match([dat_transform(conv_1_ecc, [conv_param])[0].tolist(),\ dat_transform(conv_2_ecc, [conv_param])[0].tolist()]) match_all.append(match_compute) elif (conv_param == 'porb') and ((np.all(conv_1[conv_param] == 0.0)) or (np.all(conv_1[conv_param] == -1.0))): log_file.write('{0} is the same for all converging binaries\n'.format(conv_param)) match_all.append(-9) elif (conv_param == 'sep') and ((np.all(conv_1[conv_param] == 0.0)) or (np.all(conv_1[conv_param] == -1.0))): log_file.write('{0} is the same for all converging binaries\n'.format(conv_param)) match_all.append(-9) else: if len(conv_2) == len(conv_1): conv_2 = conv_2[:int(len(conv_2)/2)] match_compute, bw = match([dat_transform(conv_1, [conv_param])[0].tolist(),\ dat_transform(conv_2, [conv_param])[0].tolist()]) match_all.append(match_compute) log_file.write('matches for converging population are are: {0}\n'.format(match_all)) log_file.write('Number of binaries in converging population is: {0}\n'.format(len(conv_1))) log_file.write('Binwidth is: {0}\n'.format(bw)) return match_all
def sample_population(self): """Once the fixed population is evolved, we Monte-Carlo sample the binary parameters to generate a Milky Way realization. Parameters ---------- fixedpop : DataFrame Contains binary parameters from an evolved population generated by the runFixedPop executable n_samp : int The number of systems in the Milky Way realization gx_component : str Milky Way component for which we are generating the population realization; choose from 'ThinDisk', 'ThickDisk', 'Bulge' gx_model : str Model for spatial distribution of binaries in the Galactic component; Default='McMillan' dat_list : list List containing the parameters to MC sample to generate the Galactic realization Returns ------- realization : DataFrame Milky Way population realization of size n_samp """ if not hasattr(self, 'n_samp'): self.n_samp = self.compute_n_sample() # Based on the user supplied filter flags, filter the # population to reduce the sample to only the relevant population ####################################################################### # Transform the fixed population to have limits between 0 and 1 # then transform to logit space to maintain the population boundaries # and create a KDE using knuth's rule to select the bandwidth ####################################################################### dat_kde = utils.dat_transform(self.fixed_pop, self.dat_list) bw = utils.knuth_bw_selector(dat_kde) dat_kernel = stats.gaussian_kde(dat_kde, bw_method=bw) # Sample from the KDE ####################################################################### binary_dat_trans = dat_kernel.resample(self.n_samp) binary_dat = utils.dat_un_transform(binary_dat_trans, self.fixed_pop, self.dat_list) # Sample positions and orientations for each sampled binary ####################################################################### xGx, yGx, zGx, inc, OMEGA, omega = MC_sample.galactic_positions( self.gx_component, size=len(binary_dat[0]), model=self.gx_model) binary_sample_positions = np.vstack([xGx, yGx, zGx, inc, OMEGA, omega]) # Create a single DataFrame for the Galactic realization ####################################################################### if 'ecc' not in self.dat_list: full_sample = np.vstack( [binary_dat, np.zeros(self.n_samp), binary_sample_positions]).T column_list = self.dat_list + [ 'ecc', 'xGx', 'yGx', 'zGx', 'inc', 'OMEGA', 'omega' ] else: full_sample = np.concatenate([binary_dat, binary_sample_positions]).T column_list = self.dat_list + [ 'xGx', 'yGx', 'zGx', 'inc', 'OMEGA', 'omega' ] realization = pd.DataFrame(full_sample,\ columns = column_list) return realization
def test_dat_transform(self): # send in DataFrame of 10*x (defined above) x_trans = utils.dat_transform(10 * x_dat, ['x_dat', 'f_dat']) self.assertTrue(np.max(special.expit(x_trans[0])) < 1) self.assertTrue(np.min(special.expit(x_trans[0])) > 0)
def perform_convergence(conv_params, bin_states, conv_filter,\ bcm_save, bcm_save_filtered,\ bpp_save, final_kstar_1, final_kstar_2, log_file): """Performs the convergence calculations for each convergence parameter and binary state Parameters ---------- conv_params : dict List of user supplied convergence parameters bin_states : dict List of user supplied binary states conv_filter : dict List of user supplied convergence filters bcm_save : DataFrame Cumulative data set of bcm arrays bcm_save_filtered : DataFrame Most recent data set of bcm array from most recent BSE run bpp_save : DataFrame Cumulative data set of bpp arrays log_file : file write File to log matches or if the convergence params are not appropriate e.g. eccentricity for a disrupted system Returns ------- match : array Matches for each cumulative data set """ match_lists = [] for bin_state in bin_states: bcm_save_conv = bcm_save.loc[bcm_save.bin_state == bin_state] bcm_save_filtered_conv = bcm_save_filtered.loc[ bcm_save_filtered.bin_state == bin_state] bcm_conv_1, bcm_conv_2 = bcm_conv_select(bcm_save_conv, bcm_save_filtered_conv, conv_filter) if (bin_state == 1) or (bin_state == 2): # select the formation parameters of the mergers and disruptions # select out the bpp arrays of interest bpp_conv_1 = bpp_save.loc[bpp_save.bin_num.isin( bcm_conv_1.bin_num)] bpp_conv_2 = bpp_save.loc[bpp_save.bin_num.isin( bcm_conv_2.bin_num)] # note that we compute the match for values other than the bcm array if bin_state == 1: bcm_conv_1 = bpp_conv_1.loc[(bpp_conv_1.kstar_1.isin(final_kstar_1)) &\ (bpp_conv_1.kstar_2.isin(final_kstar_2)) &\ (bpp_conv_1.evol_type == 3)] bcm_conv_2 = bpp_conv_2.loc[(bpp_conv_2.kstar_1.isin(final_kstar_1)) &\ (bpp_conv_2.kstar_2.isin(final_kstar_2)) &\ (bpp_conv_2.evol_type == 3)] # select the formation parameters bcm_conv_1 = bcm_conv_1.groupby('bin_num').first() bcm_conv_2 = bcm_conv_2.groupby('bin_num').first() # Perform the Match calculations for all interested parameters # supplied by user in conv_params if len(bcm_conv_2) > 3: match_all = [] for conv_param in conv_params: close_test = np.isclose(bcm_conv_1[conv_param], bcm_conv_1[conv_param].mean()) if close_test.all(): log_file.write('convergence param: {0} for all bcm is {1}\n'.format(conv_param,\ np.mean(bcm_conv_1[conv_param]))) match_all.append(-9) else: match_compute, bw = match([dat_transform(bcm_conv_1, [conv_param])[0].tolist(),\ dat_transform(bcm_conv_2, [conv_param])[0].tolist()]) match_all.append(match_compute) log_file.write('matches for bin state {0} are: {1}\n'.format( bin_state, match_all)) log_file.write('Number of binaries is: {0}\n'.format( len(bcm_save_conv))) log_file.write('Binwidth is: {0}\n'.format(bw)) log_file.write('\n') match_lists.extend(match_all) else: log_file.write( 'The filtered bcm array for bin state: {0} does not have >3 values in it yet\n' .format(bin_state)) log_file.write('Consider larger Nstep sizes\n') log_file.write('\n') if len(match_lists) > 1: match_save = np.array(match_lists) else: match_save = [] return match_save