def get_log_spirals(subject_id, gal=None, angle=None, pic_array=None, bar_length=0): drawn_arms = gu.get_drawn_arms(subject_id, gu.classifications) if gal is None or angle is None: gal, angle = gu.get_galaxy_and_angle(subject_id) if pic_array is None: pic_array, deprojected_image = gu.get_image(gal, subject_id, angle) path_to_subject = './lib/distances/subject-{}.npy'.format(subject_id) distances = gu.get_distances(subject_id) if distances is None or distances.shape[0] != len( drawn_arms) or not os.path.exists(path_to_subject): # print('\t- Calculating distances') distances = metric.calculate_distance_matrix(drawn_arms) np.save('./lib/distances/subject-{}.npy'.format(subject_id), distances) p = Pipeline(drawn_arms, phi=angle, ba=gal['PETRO_BA90'], image_size=pic_array.shape[0], distances=distances) arms = p.get_arms(clean_points=True, bar_length=bar_length) # print('Identified {} spiral arms'.format(len(arms))) return [arm.reprojected_log_spiral for arm in arms]
def get_gal_pa(subject_id): try: p = Pipeline.load('lib/pipelines/{}.json'.format(subject_id)) except FileNotFoundError: drawn_arms = gu.get_drawn_arms(subject_id, gu.classifications) gal, angle = gu.get_galaxy_and_angle(subject_id) pic_array, deprojected_image = gu.get_image(gal, subject_id, angle) p = Pipeline(drawn_arms, phi=angle, ba=gal['PETRO_BA90'], image_size=pic_array.shape[0]) arms = (Arm.load(os.path.join('lib/spiral_arms', f)) for f in os.listdir('lib/spiral_arms') if re.match('^{}-[0-9]+.pickle$'.format(subject_id), f)) arms = [arm for arm in arms if not arm.FLAGGED_AS_BAD] pa = np.zeros(len(arms)) sigma_pa = np.zeros(pa.shape) length = np.zeros(pa.shape) for i, arm in enumerate(arms): pa[i] = arm.pa length[i] = arm.length sigma_pa[i] = arm.sigma_pa if len(arms) == 0: return (np.nan, np.nan, np.stack((np.tile(subject_id, len(pa)), pa, sigma_pa, length), axis=1)) combined_pa = (pa * length).sum() / length.sum() combined_sigma_pa = np.sqrt((length**2 * sigma_pa**2).sum()) / length.sum() return ( combined_pa, combined_sigma_pa, np.stack((np.tile(subject_id, len(pa)), pa, sigma_pa, length), axis=1), )
def make_combined_arms(): bar = Bar('Obtaining combined spirals', max=len(dr8ids), suffix='%(percent).1f%% - %(eta)ds') for i in range(len(dr8ids)): original_id = ss_ids[i] validation_id = validation_ids[i] gal, angle = gu.get_galaxy_and_angle(original_id) original_drawn_arms = gu.get_drawn_arms(original_id) validation_drawn_arms = gu.get_drawn_arms(validation_id) drawn_arms = np.array( list(original_drawn_arms) + list(validation_drawn_arms), ) p = Pipeline(drawn_arms, phi=angle, ba=gal['PETRO_BA90'], image_size=512, distances=None, parallel=True) arms = p.get_arms() for j, arm in enumerate(arms): arm.save('lib/duplicate_spiral_arms/{}-{}'.format(dr8ids[i], j)) bar.next() bar.finish()
def get_spiral_arms(subject_id, should_recreate=True): if ( (not os.path.exists('lib/pipelines/{}.json'.format(subject_id))) or should_recreate ): gal, angle = gu.get_galaxy_and_angle(subject_id) drawn_arms = gu.get_drawn_arms(subject_id, gu.classifications) p = Pipeline(drawn_arms, phi=angle, ba=gal['PETRO_BA90'], image_size=512, parallel=True) p.save('lib/pipelines/{}.json'.format(subject_id)) arms = p.get_arms() for i, arm in enumerate(arms): arm.save('lib/spiral_arms/{}-{}'.format(subject_id, i)) return arms else: arm_files = [ os.path.join('lib/spiral_arms', i) for i in os.listdir('lib/spiral_arms') if str(subject_id) in i ] return [Arm.load(a) for a in arm_files]
res['validation_bulge_ba'] = v_bulge['axRatio'] res['original_bulge_reff'] = o_bulge['rEff'] res['validation_bulge_reff'] = v_bulge['rEff'] if both_have_bars: o_bar = ash.sanitize_param_dict(original_components['bar']) v_bar = ash.sanitize_param_dict(validation_components['bar']) res['original_bar_ba'] = o_bar['axRatio'] res['validation_bar_ba'] = v_bar['axRatio'] res['original_bar_reff'] = o_bar['rEff'] res['validation_bar_reff'] = v_bar['rEff'] original_drawn_arms = gu.get_drawn_arms(original_id) validation_drawn_arms = gu.get_drawn_arms(validation_id) try: original_p = Pipeline.load( 'lib/pipelines/{}.json'.format(original_id)) original_arms = ( Arm.load(os.path.join('lib/spiral_arms', i)) for i in os.listdir('lib/spiral_arms') if re.match('^{}-[0-9]+.pickle$'.format(original_id), i)) original_arms = [ arm for arm in original_arms if not arm.FLAGGED_AS_BAD ] except IOError as e: print(e) original_p = Pipeline(original_drawn_arms, phi=angle, ba=gal['PETRO_BA90'], image_size=512, parallel=True) original_arms = original_p.get_arms()
def make_comparison(dr8id, ss_id, val_id): comp_file = 'model-variances/{}_components.pickle'.format(dr8id) pa_file = 'model-variances/{}_pa.pickle'.format(dr8id) if os.path.isfile(comp_file) and os.path.isfile(pa_file): return all_cls = gu.classifications.query( '(subject_ids == {}) or (subject_ids == {})'.format(ss_id, val_id)) all_models = all_cls['annotations'].apply(json.loads).apply( ash.remove_scaling).apply(pa.parse_annotation) all_geoms = pd.DataFrame(all_models.apply(gas.get_geoms).values.tolist(), columns=('disk', 'bulge', 'bar')) ss = ShuffleSplit(n_splits=20, test_size=0.5, random_state=0) split_models = [] pas = [] gal, angle = gu.get_galaxy_and_angle(ss_id) bar = Bar(str(dr8id), max=ss.n_splits, suffix='%(percent).1f%% - %(eta)ds') for i, (train_index, _) in enumerate(ss.split(all_geoms)): models = all_models.iloc[train_index] drawn_arms = get_drawn_arms((ss_id, val_id), all_cls.iloc[train_index]) if len(drawn_arms) > 1: p = Pipeline(drawn_arms, phi=angle, ba=gal['PETRO_BA90'], image_size=512, parallel=True) pas.append(p.get_pitch_angle(p.get_arms())) else: pas.append((np.nan, np.nan)) geoms = all_geoms.iloc[train_index] labels = list(map(np.array, gas.cluster_components(geoms))) comps = gas.get_aggregate_components(geoms, models, labels) aggregate_disk, aggregate_bulge, aggregate_bar = comps split_models.append({ 'disk': aggregate_disk if aggregate_disk else None, 'bulge': aggregate_bulge if aggregate_bulge else None, 'bar': aggregate_bar if aggregate_bar else None, }) bar.next() bar.finish() splits_df = [] for model in split_models: model_comps = {} for key in ('disk', 'bulge', 'bar'): if model.get(key, None) is None: model[key] = {} mu = model[key].get('mu', (np.nan, np.nan)) model_comps['{}-mux'.format(key)] = mu[0] model_comps['{}-muy'.format(key)] = mu[1] for param in ('roll', 'rEff', 'axRatio', 'i0', 'n', 'c'): model_comps['{}-{}'.format(key, param)] = (model[key].get( param, np.nan)) splits_df.append(model_comps) splits_df = pd.DataFrame(splits_df) pas = pd.DataFrame(pas, columns=('pa', 'sigma_pa'), index=pd.Series(range(len(pas)), name='split_index')) splits_df.to_pickle(comp_file) pas.to_pickle(pa_file)
def main(mangaid, subject_id): gal, angle = gu.get_galaxy_and_angle(subject_id) unit_converter = convert_arcsec_to_km(gal) df = read_file(mangaid) invalid_mask = df.values == -9999.0 mask = np.any(invalid_mask, axis=1) df.iloc[mask] = np.nan df = df.dropna() df['R-arcsec'] = df['R'] df['R'] = unit_converter(df['R']) scale = 4 * float(gal['PETRO_THETA']) zoo_coords_r = df['R-arcsec'].values / scale keys = ( 'GAS_IC-V', 'GAS___-V', 'BTH_IC-V', 'BTH___-V', ) labels = ( r'$H_\alpha$ velocity, fixed center & inclination', r'$H_\alpha$ velocity, varying center & inclination', r'$H_\alpha$ and stellar velocity, fixed center & inclination', r'$H_\alpha$ and stellar velocity, varying centre and inclination', ) drawn_arms = gu.get_drawn_arms(subject_id, gu.classifications) arm_pipeline = Pipeline(drawn_arms, phi=angle, ba=gal['PETRO_BA90'], image_size=512, parallel=True) arms = arm_pipeline.get_arms() gzb_pa, gzb_sigma_pa = arm_pipeline.get_pitch_angle(arms) arm_details = [{ 'pa': arm.pa, 'sigma_pa': arm.sigma_pa, 'min_r': unit_converter( np.linalg.norm(arm.log_spiral - (256, 256), axis=1).min() * float(gal['PETRO_THETA']) * 4 / 512), 'max_r': unit_converter( np.linalg.norm(arm.log_spiral - (256, 256), axis=1).max() * float(gal['PETRO_THETA']) * 4 / 512) } for arm in arms] min_r = min(a['min_r'] for a in arm_details) max_r = max(a['max_r'] for a in arm_details) fitted = {} fig, ax = plt.subplots(figsize=(8, 6)) sa_pas = [] sa_pa_datas = [] for i, (key, label) in enumerate(zip(keys, labels)): f = tanh_model(df['R'].values, df[key].values) p = least_squares(f, (160, 1E-17), x_scale=(10, 1E-17))['x'] fitted[key] = f(p) + df[key].values # Calculate shear from analytic solve of dln(Ω)/dln(R) shear = shear_from_tanh(p[1], df['R'].values) omega = df[key] / (2 * np.pi * df['R']) shear_data = get_shear(omega[:-1], df['R'].values[:-1]) plt.plot(df['R'], shear, c='C{}'.format(i % 10), label=label) plt.plot(np.stack((df['R'][:-1], df['R'][1:])).mean(axis=0), shear_data, '--', c='C{}'.format(i % 10)) sa_pa = np.rad2deg(get_predicted_pa(shear)) sa_pa_data = np.rad2deg(get_predicted_pa(shear_data)) sa_pas.append(sa_pa) sa_pa_datas.append(sa_pa_data) print('For key: {}'.format(key)) msk = (df['R'] > min_r) & (df['R'] < max_r) print('\tRotation-predicted: {:.4f}°'.format(sa_pa[msk].mean())) print('\tGZB measured PA: {:.4f} ± {:.4f}°'.format( gzb_pa, gzb_sigma_pa)) plt.plot([], [], 'k-', label=r'Analytic differentiation') plt.plot([], [], 'k--', label='Numerical differentiation') plt.xlabel('Distance from galaxy centre [km]') plt.ylabel(r'Shear rate, $\Gamma$') plt.legend() plt.savefig('{}_shear.pdf'.format(mangaid), bbox_inches='tight') plt.close() np.save('pavr', np.stack((zoo_coords_r, sa_pas[0]), axis=1)) imshow_kwargs = { 'cmap': 'gray', 'origin': 'lower', 'extent': [-0.5 * scale, 0.5 * scale] * 2, } pic_array, _ = gu.get_image(gal, subject_id, angle) fig, ax = plt.subplots(ncols=1, figsize=(5, 5)) plt.imshow(pic_array, **imshow_kwargs) for i, arm in enumerate(arms): varying_arm_t = fit_varying_pa(arm, zoo_coords_r, np.stack(sa_pas).mean(axis=0)) t_predict = np.linspace(varying_arm_t.min(), varying_arm_t.max(), 100) f = interp1d(varying_arm_t, zoo_coords_r) varying_arm = xy_from_r_theta(f(t_predict), t_predict) log_spiral = xy_from_r_theta(*np.flipud(arm.polar_logsp)) plt.plot(*arm.deprojected_coords.T * scale, '.', markersize=1, alpha=1) plt.plot(*log_spiral * scale, c='r', linewidth=3, alpha=0.8) plt.plot(*varying_arm * scale, c='g', linewidth=3, alpha=0.8) # plots for legend plt.plot([], [], c='g', linewidth=3, alpha=0.8, label='Swing-amplified spiral') plt.plot([], [], c='r', linewidth=3, alpha=0.8, label='Logarithmic spiral') plt.axis('equal') plt.xlabel('Arcseconds from galaxy centre') plt.ylabel('Arcseconds from galaxy centre') plt.xlim(-25, 25) plt.ylim(-25, 25) plt.legend() plt.savefig('{}_varying-pa.pdf'.format(mangaid), bbox_inches='tight') plt.close() return fig, ax = plt.subplots(figsize=(8, 6)) for sa_pa, label in zip(sa_pas, labels): plt.plot(df['R'], sa_pa, label=label) for row in arm_details: plt.hlines(row['pa'], row['min_r'], row['max_r']) plt.fill_between( np.linspace(row['min_r'], row['max_r'], 2), row['pa'] - row['sigma_pa'], row['pa'] + row['sigma_pa'], color='k', alpha=0.2, ) plt.legend() plt.xlabel('Distance from galaxy centre [km]') plt.ylabel('Pitch angle [degrees]') plt.savefig('{}_pa.pdf'.format(mangaid), bbox_inches='tight') plt.close() fig, ax = plt.subplots(figsize=(8, 6)) # df.plot('R', keys, label=labels, ax=ax) for i, key in enumerate(keys): plt.fill_between( df['R'].values, df[key].values - df[key + 'e'].values, df[key].values + df[key + 'e'].values, color='C{}'.format(i % 10), alpha=0.1, ) plt.plot(df['R'].values, df[key].values, '--', c='C{}'.format(i % 10)) plt.plot(df['R'].values, fitted[key], c='C{}'.format(i % 10)) for i, label in enumerate(labels): plt.plot([], [], c='C{}'.format(i % 10), label=label) plt.plot([], [], 'k-', label=r'$A\tanh(bR)$ model') plt.plot([], [], 'k--', label='Data') plt.legend() plt.xlabel('Distance from galaxy centre [km]') plt.ylabel(r'Rotational velocity [$\mathrm{km}\mathrm{s}^{-1}$]') plt.savefig('{}_rotational-velocity_2.pdf'.format(mangaid), bbox_inches='tight') plt.close()