<jupyter_start><jupyter_text>Data/sim comparison- Loop over APOGEE fields in RGB sample- Select sim particles to match distance distribution along each LOS<jupyter_code>g = GaiaData('../data/APOGEE-Alvaro-EDR3-RGB.fits')
g.data['FIELD'] = g.data['FIELD'].astype(str)
tmp_df = g.data['ra', 'dec', 'FIELD'].to_pandas()
for i, name in enumerate('xyz'):
    tmp_df[name] = g.get_skycoord(distance=False).cartesian.xyz.value[i]

tmp = tmp_df.groupby('FIELD').mean()
tmp_std = tmp_df.groupby('FIELD').std()
xyz_std = (np.sqrt(tmp_std['x']**2 + 
                   tmp_std['y']**2 + 
                   tmp_std['z']**2).values * u.rad).to_value(u.deg)
assert np.all(xyz_std[np.isfinite(xyz_std)] < 1.5)

mean_fields = at.Table()
mean_fields['FIELD'] = tmp.index.values.astype(str)

fieldcen = coord.CartesianRepresentation(
    tmp['x'].values, 
    tmp['y'].values, 
    tmp['z'].values).represent_as(coord.SphericalRepresentation)
mean_fields['ra'] = fieldcen.lon.degree
mean_fields['dec'] = fieldcen.lat.degree
mean_fields = mean_fields[np.isfinite(mean_fields['dec'])]

mean_fields_c = coord.SkyCoord(
    mean_fields['ra'], mean_fields['dec'], unit=u.deg)

len(mean_fields)
from sklearn.mixture import GaussianMixture
global_weights = np.zeros(len(df))
apogee_field_radius = 3 * u.deg
# apogee_field_radius = 5 * u.deg  # HACK

df_l_mask = (df.l > 90) & (df.l < 270)
field_l_mask = ((mean_fields_c.galactic.l > 90*u.deg) & 
                (mean_fields_c.galactic.l < 270*u.deg))

for i, (field, field_c) in tqdm(enumerate(
        zip(mean_fields['FIELD'][field_l_mask], 
            mean_fields_c[field_l_mask])), total=field_l_mask.sum()):
    field_mask = g.FIELD == field
    
    sep = angular_separation(field_c.ra, field_c.dec, 
                             df.ra.values*u.deg, df.dec.values*u.deg)
    sep_mask = (sep < apogee_field_radius) & df_l_mask
    
    if sep_mask.sum() < field_mask.sum():
        continue
        
    sim_field_dists = df.distance.values[sep_mask]
    
    # Target distribution:
    bw = 0.5
    kernel = 'epanechnikov'
    kde = KernelDensity(bandwidth=bw, kernel=kernel).fit(
        g.D2_med[field_mask].reshape(-1, 1))
    kde_lnprob = kde.score_samples(
        sim_field_dists.reshape(-1, 1))
    
    # Simulation distribution:
#     bg_kde = KernelDensity(bandwidth=bw, 
#                            kernel=kernel)
#     bg_kde.fit(sim_field_dists.reshape(-1, 1))
#     bg_lnprob = bg_kde.score_samples(
#         sim_field_dists.reshape(-1, 1))
    
    gmm = GaussianMixture(n_components=7)
    gmm.fit(sim_field_dists.reshape(-1, 1))
    bg_lnprob = gmm.score_samples(
        sim_field_dists.reshape(-1, 1))
    
    weights = np.exp(kde_lnprob - bg_lnprob)
    global_weights[sep_mask] = weights
(global_weights > 0).sum()
plt.scatter(mean_fields_c.galactic.l.degree,
            mean_fields_c.galactic.b.degree, s=4)
plt.scatter(field_c.galactic.l.degree,
            field_c.galactic.b.degree,
            color='r')
plt.xlim(360, 0)
plt.ylim(-90, 90)
plt.plot(df.l[global_weights>0],
         df.b[global_weights>0],
         marker='o', ls='none', ms=1)
plt.xlim(360, 0)
plt.ylim(-90, 90)
stat = binned_statistic_2d(df.R.values[global_weights > 0], 
                           -df.v_phi.values[global_weights > 0],
                           df.v_z.values[global_weights > 0],
                           bins=(np.arange(5, 20, 0.2),
                                 np.arange(125, 300, 4)))

fig, ax = plt.subplots(1, 1, figsize=(10, 5))
cs = ax.pcolormesh(
    stat.x_edge, stat.y_edge, stat.statistic.T,
    vmin=-8, vmax=8, 
    cmap='RdBu')
fig.colorbar(cs)

ax.set_xlabel('$R$ [kpc]')
ax.set_ylabel(r'$v_\phi$ [km/s]')

fig.set_facecolor('w')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15545430179576522, 'fraction_numerical': 0.020905923344947737, 'mean_word_length': 3.0345945945945947, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '334638', 'license': 'MIT', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1432, 'n_words': 209, 'path': 'notebooks/Jason-sims.ipynb', 'repo_name': 'adrn/apogee-outerdisk-edr3', 'size': '3731'}