def test_make_megeath_class_column():

    # Let's make sure that all the D's stay D's, all the P's stay P's,
    # and that all the na's transform into either na's or NDs.

    new_class_column = make_megeath_class_column()

    old_class_column = megeath2012_by_ukvar.Class

    assert (len(old_class_column[old_class_column == 'D']) ==
            len(new_class_column[new_class_column == 'D']))

    assert (len(old_class_column[old_class_column == 'P']) ==
            len(new_class_column[new_class_column == 'P']))

    assert (len(old_class_column[old_class_column == 'na']) ==
            len(new_class_column[new_class_column == 'ND']) +
            len(new_class_column[new_class_column == 'na']))

    # make sure you DID change SOMETHING
    assert (len(old_class_column[old_class_column == 'na']) !=
            len(new_class_column[new_class_column == 'na']))


    # and finally make sure you didn't swap the order of any D's or P's
    assert (old_class_column[(old_class_column == 'D') |
                             (old_class_column == 'P')] ==
            new_class_column[(new_class_column == 'D') |
                             (new_class_column == 'P')] ).all()
def f_periods_by_megeath_class(title="Periodic ONCvar stars, with class from Megeath et al. 2012"):
    """
    A histogram of periods for stars that have Megeath classes.

    Returns
    -------
    fig : plt.figure
    three np.ndarrays 
        These arrays contain the distribution of periods for 
        (a) protostars,
        (b) disked stars, and
        (c) non-disked stars
        (on the basis of Megeath2012 membership/Class). Use these
        for K-S tests if you gotta (see example)

    Example:
    In [15]: foo = f_periods_by_megeath_class()
    In [16]: red = foo[2]
    In [17]: blue = foo[3]
    
    In [18]: scipy.stats.ks_2samp(red, blue)
    Out[18]: (0.32200088003520139, 1.2771000514405625e-22)

    """ 

    megeath_class_column = make_megeath_class_column()

    fig = plt.figure()

    sub1 = plt.subplot(3,1,1)
    plt.hist(ukvar_periods[megeath_class_column == 'P'],
             range=[0,20], bins=40, color=color_dict['protostar'])

    sub2 = plt.subplot(3,1,2, sharex=sub1)
    plt.hist(ukvar_periods[megeath_class_column == 'D'],
             range=[0,20], bins=40, color=color_dict['disk'])

    sub3 = plt.subplot(3,1,3, sharex=sub1)
    plt.hist(ukvar_periods[megeath_class_column == 'ND'],
             range=[0,20], bins=40, color=color_dict['nondisk'])
    
    sub3.set_xlabel("Period (days)")

    sub1.text(0.5, 0.5, "Periodic Protostar sample", transform=sub1.transAxes)
    sub2.text(0.5, 0.5, "Periodic Disk sample", transform=sub2.transAxes)
    sub3.text(0.5, 0.5, "Periodic Non-disk sample", transform=sub3.transAxes)

    if title:
        sub1.set_title(title)

    plt.show()

    print "scipy.stats.ks_2samp(disks, no_disks):"
    print scipy.stats.ks_2samp(ukvar_periods[megeath_class_column == 'D'],
                               ukvar_periods[megeath_class_column == 'ND'])

    return (fig,
            ukvar_periods[megeath_class_column == 'P'], 
            ukvar_periods[megeath_class_column == 'D'], 
            ukvar_periods[megeath_class_column == 'ND'])
import matplotlib.pyplot as plt

from periodic_selector import long_periodic_selector, best_long_period
from orion_plot import OrionStarData
from tablemate_comparisons import ukvar_spread
from official_star_counter import maxvars_spread_per
from variables_data_filterer import variables_photometry
from lightcurve_maker import lc_and_phase_and_colors
from table_maker import make_megeath_class_column


# These can be broken into two groups:
# 1. Anyone with a period longer than 20 days from the official periodics (shorter than 20 days)
# 2. Long periods identified by hand greater than 50 days.

megeath_class_column = make_megeath_class_column()

path = os.path.expanduser("~/Dropbox/Bo_Tom/lightcurve_book/long_periods/")
path2 = os.path.expanduser("~/Dropbox/Bo_Tom/lightcurve_book/short_periods/")

def find_20_50_day_periodics_and_save_to_file():
	blp = best_long_period(long_periodic_selector(maxvars_spread_per, max_period=100), max_period=100)
	ukvar_blp = blp.where(np.in1d(blp.SOURCEID, ukvar_spread.SOURCEID))

	print "There are {0} stars with nominal periods between 50 and 100.".format(len(ukvar_blp))

	for sid, period in zip(ukvar_blp.SOURCEID, ukvar_blp.best_period):
	    sd = OrionStarData(variables_photometry, sid, ukvar_spread.UKvar_ID[ukvar_spread.SOURCEID==sid][0])
	    fig = lc_and_phase_and_colors(sd, period)
	    fig.ax_j_phase.set_title("ONCvar {}".format(sd.name))
	    fig.ax_j_lc.set_title(sid)
def f_magnitude_hists_by_class(threepanels=True, onepanels=False):
    """
    Makes a series of multipanel histograms of variability.
    Uses "strict" sources only for these.
    
    """

    megeath_class_column = make_megeath_class_column()
    
    strict_protostars = ukvar_spread.where(
        (ukvar_spread.strict == 1) & (megeath_class_column == 'P'))

    strict_disks = ukvar_spread.where(
        (ukvar_spread.strict == 1) & (megeath_class_column == 'D'))

    strict_nondisks = ukvar_spread.where(
        (ukvar_spread.strict == 1) & (megeath_class_column == 'ND'))

    print "Protostars: %d, Disks: %d, Nondisks: %d" % (
        len(strict_protostars), len(strict_disks), len(strict_nondisks) )

    # Let's test the J mag aspect of this, and then define some dicts or forloops to iterate through all "5" bands.

    names = ['J mag', 'H mag', 'K mag', '(J-H) color', '(H-K) color']
    bands = ['j', 'h', 'k', 'jmh', 'hmk']
    text_xposition = [0.375, 0.375, 0.375, 0.275, 0.275]

    figs = []

    hist_kwargs = {'range':(0,2), 'bins':20}

    if threepanels:
        for b, n, x in zip(bands, names, text_xposition):

            j_fig = plt.figure(figsize=(5,6))
            figs.append(j_fig)

            jsub1 = plt.subplot(3,1,1)
            jsub1.hist(strict_protostars['%s_ranger' % b], color=color_dict['protostar'], 
                       **hist_kwargs)
            jsub1.text(x, 0.65, "protostars \n"
                       r"median $\Delta %s: $%.2f \pm %.2f$" % (
                    n.replace(' ', '$ '), 
                    np.median(strict_protostars['%s_ranger' % b]),
                    rb.mad(strict_protostars['%s_ranger' % b])),
                       transform = jsub1.transAxes)

            jsub2 = plt.subplot(3,1,2, sharex=jsub1)
            jsub2.hist(strict_disks['%s_ranger' % b], color=color_dict['disk'], **hist_kwargs)
            jsub2.text(x, 0.65, "disks \n"
                       r"median $\Delta %s: $%.2f \pm %.2f$" % (
                    n.replace(' ', '$ '), 
                    np.median(strict_disks['%s_ranger' % b]),
                    rb.mad(strict_disks['%s_ranger' % b])),
                       transform = jsub2.transAxes)

            jsub3 = plt.subplot(3,1,3, sharex=jsub1)
            jsub3.hist(strict_nondisks['%s_ranger' % b], color=color_dict['nondisk'], 
                       **hist_kwargs)
            jsub3.text(x, 0.65, "non-disks \n"
                       r"median $\Delta %s: $%.2f \pm %.2f$" % (
                    n.replace(' ', '$ '), 
                    np.median(strict_nondisks['%s_ranger' % b]),
                    rb.mad(strict_nondisks['%s_ranger' % b])),
                       transform = jsub3.transAxes)

            jsub1.set_title("%s range for $Q=2$ variables"%n)
            jsub3.set_xlabel(r"$\Delta %s (outlier-proof)" % 
                             n.replace(' ', '$ '))


    if onepanels:

        fig = plt.figure()
        figs.append(fig)
        
        plt.hist(strict_nondisks['k_ranger'], 
                 color=color_dict['nondisk'], hatch='/', label='Megeath Non-disks',
                 **hist_kwargs)
        plt.hist(strict_disks['k_ranger'], 
                 color=color_dict['disk'], alpha=0.5, hatch='\\', label='Megeath Disks',
                 **hist_kwargs)
        plt.hist(strict_protostars['k_ranger'], 
                 color=color_dict['protostar'], hatch='--', label='Megeath Protostars',
                 **hist_kwargs)

        plt.title("K magnitude range (robust) for pristine-data variables")
        plt.xlabel(r"$\Delta K$ magnitude (outlier-proof)")

        plt.legend()

            
    plt.show()
    return figs
def f_cc_cmd_and_map_by_megeath_class(sample='all', title=True):
    """
    A color-color diagram where stars are colored by Megeath class.

    Also a CMD and a map.
    
    """

    megeath_class_column = make_megeath_class_column()

    # Nonperiodic stars only
    if 'non' in sample.lower():
        sample_boolean_criterion = np.isnan(ukvar_periods)
        sample_name = "Non-periodic"
    elif 'per' in sample.lower():
        sample_boolean_criterion = ~np.isnan(ukvar_periods)
        sample_name = "Periodic"
    elif 'strict' in sample.lower():
        sample_boolean_criterion = (ukvar_spread.strict == 1)
        sample_name = "Q=2"
    else:
        sample_boolean_criterion = (np.zeros_like(ukvar_periods) == 0) #all true
        sample_name = "All"

    # Now we want the rows in ukvar_spread that correspond to certain
    # Megeath subsamples.

    disk_indices = (
        # its Class is 'D'
        (megeath_class_column == 'D') &
        # and it matches our 'periodic/nonperiodic/all' cut.
         sample_boolean_criterion)

    protostar_indices = (
        (megeath_class_column == 'P') & sample_boolean_criterion)

    nondisk_indices = (
        (megeath_class_column == 'ND') & sample_boolean_criterion)

    unknown_indices = (
        (megeath_class_column == 'na') & sample_boolean_criterion)
    
        
    fig = plt.figure()
    ax = plt.gca()

    plot_trajectory_vanilla(ax, a_k=3)

    plt.plot(ukvar_spread.hmk_median[nondisk_indices],
             ukvar_spread.jmh_median[nondisk_indices],
             'o', color=color_dict['nondisk'], ms=4, label="Non-disks")
    plt.plot(ukvar_spread.hmk_median[disk_indices],
             ukvar_spread.jmh_median[disk_indices],
             'o', color=color_dict['disk'], ms=4, label="Disks")
    plt.plot(ukvar_spread.hmk_median[protostar_indices],
             ukvar_spread.jmh_median[protostar_indices],
             '*', color=color_dict['protostar'], ms=10, label="Protostars")

    plt.xlabel(r"median $H-K$")
    plt.ylabel(r"median $J-H$")

    plt.xlim(-0.1,3.1)
    plt.ylim(-0.2, 4.4)

    if title:
        plt.title(sample_name + " variables, colored by Megeath+2012 class")

    plt.legend(loc='lower right')


    fig3 = plt.figure()
    ax3 = plt.gca()

    plot_trajectory_vanilla(ax, a_k=3)

    plt.plot(ukvar_spread.hmk_median[nondisk_indices],
             ukvar_spread.k_median[nondisk_indices],
             'o', color=color_dict['nondisk'], ms=4, label="Non-disks")
    plt.plot(ukvar_spread.hmk_median[disk_indices],
             ukvar_spread.k_median[disk_indices],
             'o', color=color_dict['disk'], ms=4, label="Disks")
    plt.plot(ukvar_spread.hmk_median[protostar_indices],
             ukvar_spread.k_median[protostar_indices],
             '*', color=color_dict['protostar'], ms=10, label="Protostars")

    plt.xlabel(r"median $H-K$")
    plt.ylabel(r"median $K$ mag")

    plt.xlim(-0.1,3.1)
    plt.ylim(17, 8)
    
    plt.title(sample_name + " variables, colored by Megeath 2012 class")

    plt.legend(loc='upper right')


    fig2 = plt.figure()

    plt.plot(np.degrees(ukvar_spread.RA)[nondisk_indices], 
             np.degrees(ukvar_spread.DEC)[nondisk_indices], 
             'o', color=color_dict['nondisk'], ms=4, label="Non-disks")
    plt.plot(np.degrees(ukvar_spread.RA)[disk_indices], 
             np.degrees(ukvar_spread.DEC)[disk_indices], 
             'o', color=color_dict['disk'], ms=4, label="Disks")
    plt.plot(np.degrees(ukvar_spread.RA)[protostar_indices], 
             np.degrees(ukvar_spread.DEC)[protostar_indices], 
             '*', color=color_dict['protostar'], ms=10, label="Protostars")

    plt.legend()
    plt.gca().invert_xaxis()

    plt.show()

    return (fig, fig2, fig3)