Exemplo n.º 1
0
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 main(mangaid, subject_id):
    gal, angle = read_curve.gu.get_galaxy_and_angle(subject_id)
    unit_converter = read_curve.convert_arcsec_to_km(gal)
    df = read_curve.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'])

    drawn_arms = gu.get_drawn_arms(subject_id)
    arm_pipeline = read_curve.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)

    sa_pas = []
    for i, (key, label) in enumerate(zip(keys, labels)):
        f = read_curve.tanh_model(df['R'].values, df[key].values)
        p = read_curve.least_squares(f, (160, 1E-17, 0),
                                     x_scale=(10, 1E-17, 0.1))['x']
        # Calculate shear from analytic solve of dln(Ω)/dln(R)
        shear = read_curve.shear_from_tanh(p[1], df['R'].values)
        sa_pa = np.rad2deg(read_curve.get_predicted_pa(shear))
        sa_pas.append(sa_pa)
        print('For key: {}'.format(key))
        print('\tRotation-predicted: {:.4f}°'.format(
            sa_pa[df['R'] > min_r].mean()))
        print('\tGZB measured PA: {:.4f} ± {:.4f}°'.format(
            gzb_pa, gzb_sigma_pa))

    r = df['R_arcsec'] / (float(gal['PETRO_THETA']) * 4)
    th = theta_from_pa(r.values, sa_pas[0])
    plt.plot(th, r)
    plt.show()
Exemplo n.º 5
0
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]
            sample_weight=point_weights[test]
        )
        params.append(clf.coef_)
        score += s / n_splits
    return score, params


if __name__ == '__main__':
    chosenId = 21097008
    # chosenId = 21686558
    gal, angle = gu.get_galaxy_and_angle(chosenId)
    pic_array, deprojected_image = gu.get_image(
        gal, chosenId, angle
    )

    drawn_arms = gu.get_drawn_arms(chosenId, gu.classifications)

    galaxy_object = GalaxySpirals(
        drawn_arms,
        ba=gal['SERSIC_BA'].iloc[0],
        phi=-angle
    )
    try:
        distances
    except NameError:
        distances = galaxy_object.calculate_distances()

    db = galaxy_object.cluster_lines(distances)

    dpj_arms = galaxy_object.deproject_arms()
Exemplo n.º 7
0
        if both_have_bulges:
            o_bulge = ash.sanitize_param_dict(original_components['bulge'])
            v_bulge = ash.sanitize_param_dict(validation_components['bulge'])
            res['original_bulge_ba'] = o_bulge['axRatio']
            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,
Exemplo n.º 8
0
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()