def test_binwidth_selector(self): # Check that the Knuth's bw is selected properly bw = utils.knuth_bw_selector(np.array([x])) self.assertTrue(bw.round(3) == BW_KNUTH)
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