def remove_masked(table, colname): """ Takes a CatalogTable and a column name and returns the unmasked elements. !!! not yet reviewed for functionality; original function from 2019 !!! Arguments: table [CatalogTable]: Table to access header [string]: column name Return: List of data values in the specified column that are not masked. """ data_list = [] # this will fail if "header" is not a valid column name try: for i in table[colname]: # does this actually work? if type(i) == np.float64: data_list.append(i) except: out("Specified column is not a member of the table:") out(colname) return (data_list)
def cluster_stats(cluster, param): """ Given a candidate cluster and a parameter, finds summary stats and plots that parameter for the cluster. Arguments: cluster [CatalogTable]: candidate cluster param [string]: parameter to analyze Return: [none] """ param_hist = [] try: param_error = param + "_error" param_data = cluster.table[param] param_error_data = cluster.table[param_error] except KeyError as e: out("Passed parameter does not exist in cluster table.") out(e.message) minval = min(param_data) maxval = max(param_data) param_vals = np.linspace(minval, maxval, num=5 * len(param_data)) for param in param_vals: loglikelihood = 0 for index in range(len(param_data)): loglikelihood += 0.5 * ( (param - param_data[index]) / param_error_data[index])**2 - param_error_data[index] param_hist.append(loglikelihood) param_hist /= min(param_hist) param_actual = [] for value in param_hist: param_actual.append(np.exp(-1.0 * value)) normalize = np.trapz(param_actual, param_vals) param_actual /= normalize most, low, high, _ = getCI(param_vals, param_actual, 0.68) out("best " + param + ": " + str(most)) out("68 percent interval: " + str(low) + " to " + str(high)) fig = plt.figure() fig.clf() ax = fig.add_subplot(1, 1, 1) # apply axis labels ax.set_xlabel(param) ax.set_ylabel('posterior probability') ax.plot(param_vals, param_actual) plt.show() """
def keep_likely_disks(table, join_method="and"): """ Returns a list of sources that are so red, they cannot be explained by a reddened photosphere (and are thus likely to have disks). Default criteria applied: W3-W4 > 1 W2-W3 > 0.8 and W2-W3 < 4 W1-W2 > 0.35 K-W4 > 1.2 and K-W4 < 10 K-W3 > 0.9 and K-W3 < 7 K-W2 > 0.6 K-W1 > 0.3 H-K > 1.5 and K-W2 > 2.8 Arguments: join_method [string, optional, default: "and"]: string designating how to combine the criteria. Typical values would be "and" and "or". Returns: cut_true, cut_false [tuple of astropy tables]: tables which satisfy and fail to satisfy the specified criteria, respectively. """ join_argument = " " + join_method + " " cut_string = "" master = Tk() master.title = "criteria menu" def var_states(): if var_w1w2.get(): val = val_w1w2.get() print("W1-W2 > " + val) cut_string += "W1-W2 > " + val + join_argument if var_w2w3g.get(): val = val_w2w3g.get() print("W2-W3 > " + val) cut_string += "W2-W3 > " + val + join_argument if var_w2w3l.get(): val = val_w2w3l.get() print("W2-W3 < " + val) cut_string += "W2-W3 < " + val + join_argument if var_w3w4.get(): val = val_w3w4.get() print("W3-W4 > " + val) cut_string += "W3-W4 > " + val + join_argument if var_kw1.get(): val = val_kw1.get() print("K-W1 > " + val) cut_string += "K-W1 > " + val + join_argument if var_kw2.get(): val = val_kw2.get() print("K-W2 > " + val) cut_string += "K-W2 > " + val + join_argument if var_kw3g.get(): val = val_kw3g.get() print("K-W3 > " + val) cut_string += "K-W3 > " + val + join_argument if var_kw3l.get(): val = val_kw3l.get() print("K-W3 < " + val) cut_string += "K-W3 < " + val + join_argument if var_kw4g.get(): val = val_kw4g.get() print("K-W4 > " + val) cut_string += "K-W4 > " + val + join_argument if var_kw4l.get(): val = val_kw4l.get() print("K-W4 < " + val) cut_string += "K-W4 < " + val + join_argument if var_hk.get(): val = val_hk.get() print("H-K > " + val) cut_string += "H-K > " + val + join_argument if var_custom.get(): val = val_custom.get() print(val) cut_string += val Label(master, text="Criteria to use:").grid(row=0, sticky=W) var_w1w2 = BooleanVar() var_w1w2.set(True) Checkbutton(master, text="W1-W2 > ", variable=var_w1w2).grid(row=1, column=1, sticky=W) val_w1w2 = StringVar(master, value="0.35") Entry(master, textvariable=val_w1w2).grid(row=1, column=2, sticky=W) var_w2w3g = BooleanVar() var_w2w3g.set(True) Checkbutton(master, text="W2-W3 > ", variable=var_w2w3g).grid(row=2, column=1, sticky=W) val_w2w3g = StringVar(master, value="0.8") Entry(master, textvariable=val_w2w3g).grid(row=2, column=2, sticky=W) var_w2w3l = BooleanVar() var_w2w3l.set(True) Checkbutton(master, text="W2-W3 < ", variable=var_w2w3l).grid(row=3, column=1, sticky=W) val_w2w3l = StringVar(master, value="4.0") Entry(master, textvariable=val_w2w3l).grid(row=3, column=2, sticky=W) var_w3w4 = BooleanVar() var_w3w4.set(True) Checkbutton(master, text="W3-W4 > ", variable=var_w3w4).grid(row=4, column=1, sticky=W) val_w3w4 = StringVar(master, value="1.0") Entry(master, textvariable=val_w3w4).grid(row=4, column=2, sticky=W) var_kw1 = BooleanVar() var_kw1.set(True) Checkbutton(master, text="K-W1 > ", variable=var_kw1).grid(row=5, column=1, sticky=W) val_kw1 = StringVar(master, value="0.3") Entry(master, textvariable=val_kw1).grid(row=5, column=2, sticky=W) var_kw2 = BooleanVar() var_kw2.set(True) Checkbutton(master, text="K-W2 > ", variable=var_kw2).grid(row=6, column=1, sticky=W) val_kw2 = StringVar(master, value="2.8") Entry(master, textvariable=val_kw2).grid(row=6, column=2, sticky=W) var_kw3g = BooleanVar() var_kw3g.set(True) Checkbutton(master, text="K-W3 > ", variable=var_kw3g).grid(row=7, column=1, sticky=W) val_kw3g = StringVar(master, value="0.9") Entry(master, textvariable=val_kw3g).grid(row=7, column=2, sticky=W) var_kw3l = BooleanVar() var_kw3l.set(True) Checkbutton(master, text="K-W3 < ", variable=var_kw3l).grid(row=8, column=1, sticky=W) val_kw3l = StringVar(master, value="7.0") Entry(master, textvariable=val_kw3l).grid(row=8, column=2, sticky=W) var_kw4g = BooleanVar() var_kw4g.set(True) Checkbutton(master, text="K-W4 > ", variable=var_kw4g).grid(row=9, column=1, sticky=W) val_kw4g = StringVar(master, value="1.2") Entry(master, textvariable=val_kw4g).grid(row=9, column=2, sticky=W) var_kw4l = BooleanVar() var_kw4l.set(True) Checkbutton(master, text="K-W4 < ", variable=var_kw4l).grid(row=10, column=1, sticky=W) val_kw4l = StringVar(master, value="10.0") Entry(master, textvariable=val_kw4l).grid(row=10, column=2, sticky=W) var_hk = BooleanVar() var_hk.set(True) Checkbutton(master, text="H-K > ", variable=var_hk).grid(row=11, column=1, sticky=W) val_hk = StringVar(master, value="1.5") Entry(master, textvariable=val_hk).grid(row=11, column=2, sticky=W) var_custom = BooleanVar() var_custom.set(False) Checkbutton(master, text="Custom: ", variable=var_custom).grid(row=12, column=1, sticky=W) val_custom = StringVar(master, value="") Entry(master, textvariable=val_custom).grid(row=12, column=2, sticky=W) #Button(master, text='Cancel', command=master.destroy).grid(row=9, sticky=W, pady=4) Button(master, text='Enter', command=lambda: [master.destroy(), var_states()]).grid(row=13, sticky=W, pady=4) master.mainloop() cut_string = cut_string.strip().strip('and').strip() out(cut_string) return apply_cut(table, cut_string)
def identify_clusters(table, columns, expected_clusters=None, verbose=False): """ Uses HDBSCAN to identify candidate clusters by specified parameters. Arguments: table [CatalogTable]: table to access columns [list of strings]: list of columns to use for cluster detection expected_clusters [int]: Parameter for HDBSCAN; number of clusters expected. If not provided, no default is passed. verbose [bool]: verbose flag option, set to True for more diagnostics Return: CatalogTable of cluster candidates, and list of cluster membership """ # HDBSCAN arguments can include: # min_samples # min_cluster_size # cluster_selection_epsilon # allow_single_cluster # alpha #data = [] #candidates_table = table[:] out("Organizing clustering data...") if verbose: out("Columns in supplied data table:") out(table.table.colnames) out("Columns to be used for clustering analysis:") out(columns) out("Initializing data structures...") data = [] candidates_table = table.table[:] count = 0 out("Selecting data...") for i in range(len(table.table[columns[0]])): datai = [] include_flag = True for c in columns: if not (table.table[c][i] != None): include_flag = False if include_flag: for c in columns: datai.append(table.table[c][i]) data.append(datai) else: candidates_table.remove_row(i - count) count += 1 if verbose: out("Columns in dataset: ") out(len(data[0])) out("Entries in dataset: ") out(len(data)) out("Calculating clusters...") #clusterer = hdbscan.HDBSCAN(min_cluster_size=(len(data_A) / 10)) #clusterer = hdbscan.HDBSCAN(allow_single_cluster=False, min_cluster_size=(len(data_A)/20)) if expected_clusters: out("Iterating minimum cluster size...") iteration = 1 while (1): out(str(iteration) + "%") min_cluster_size = abs(int( iteration * len(data) / 100)) + 1 # 1% of the data size clusterer = hdbscan.HDBSCAN(min_cluster_size=min_cluster_size, min_samples=10) clusterer.fit(data) membership = clusterer.labels_ if max(membership) < expected_clusters: break iteration += 1 else: clusterer = hdbscan.HDBSCAN(min_cluster_size=50, min_samples=10) clusterer.fit(data) membership = clusterer.labels_ out("Detected " + str(max(membership) + 1) + " clusters") out( "Clustering calculation complete." ) #\n Detected " + str(max(membership)) + " clusters in a population of " + str(len(data_A)) " objects.") return CatalogTable(table.catalogs, candidates_table), membership
def keep_reddest(table, join_method="and"): """ Returns a list of sources that are redder than the reddest photospheres. Default criteria applied: W1-W2 > 0.15 K-W2 > 0.3 K-W1 > 0.2 J-W2 > 1.3 J-W1 > 1.1 H-K > 0.4 J-K > 0.8 J-H > 0.7 Arguments: join_method [string, optional, default: "and"]: string designating how to combine the criteria. Typical values would be "and" and "or". Returns: cut_true, cut_false [tuple of astropy tables]: tables which satisfy and fail to satisfy the specified criteria, respectively. """ join_argument = " " + join_method + " " cut_string = "" master = Tk() master.title = "criteria menu" def var_states(): if var_w1w2.get(): val = val_w1w2.get() print("W1-W2 > " + val) cut_string += "W1-W2 > " + val + join_argument if var_kw2.get(): val = val_kw2.get() print("K-W2 > " + val) cut_string += "K-W2 > " + val + join_argument if var_kw1.get(): val = val_kw1.get() print("K-W1 > " + val) cut_string += "K-W1 > " + val + join_argument if var_jw2.get(): val = val_jw2.get() print("J-W2 > " + val) cut_string += "J-W2 > " + val + join_argument if var_jw1.get(): val = val_jw1.get() print("J-W1 > " + val) cut_string += "J-W1 > " + val + join_argument if var_hk.get(): val = val_hk.get() print("H-K > " + val) cut_string += "H-K > " + val + join_argument if var_jk.get(): val = val_jk.get() print("J-K > " + val) cut_string += "J-K > " + val + join_argument if var_jh.get(): val = val_jh.get() print("J-H > " + val) cut_string += "J-H > " + val + join_argument if var_custom.get(): val = val_custom.get() print(val) cut_string += val Label(master, text="Criteria to use:").grid(row=0, sticky=W) var_w1w2 = BooleanVar() var_w1w2.set(True) Checkbutton(master, text="W1-W2 > ", variable=var_w1w2).grid(row=1, column=1, sticky=W) val_w1w2 = StringVar(master, value="0.15") Entry(master, textvariable=val_w1w2).grid(row=1, column=2, sticky=W) var_kw2 = BooleanVar() var_kw2.set(True) Checkbutton(master, text="K-W2 > ", variable=var_kw2).grid(row=2, column=1, sticky=W) val_kw2 = StringVar(master, value="0.3") Entry(master, textvariable=val_kw2).grid(row=2, column=2, sticky=W) var_kw1 = BooleanVar() var_kw1.set(True) Checkbutton(master, text="K-W1 > ", variable=var_kw1).grid(row=3, column=1, sticky=W) val_kw1 = StringVar(master, value="0.2") Entry(master, textvariable=val_kw1).grid(row=3, column=2, sticky=W) var_jw2 = BooleanVar() var_jw2.set(True) Checkbutton(master, text="J-W2 > ", variable=var_jw2).grid(row=4, column=1, sticky=W) val_jw2 = StringVar(master, value="1.3") Entry(master, textvariable=val_jw2).grid(row=4, column=2, sticky=W) var_jw1 = BooleanVar() var_jw1.set(True) Checkbutton(master, text="J-W1 > ", variable=var_jw1).grid(row=5, column=1, sticky=W) val_jw1 = StringVar(master, value="1.1") Entry(master, textvariable=val_jw1).grid(row=5, column=2, sticky=W) var_hk = BooleanVar() var_hk.set(True) Checkbutton(master, text="H-K > ", variable=var_hk).grid(row=6, column=1, sticky=W) val_hk = StringVar(master, value="0.4") Entry(master, textvariable=val_hk).grid(row=6, column=2, sticky=W) var_jk = BooleanVar() var_jk.set(True) Checkbutton(master, text="J-K > ", variable=var_jk).grid(row=7, column=1, sticky=W) val_jk = StringVar(master, value="0.8") Entry(master, textvariable=val_jk).grid(row=7, column=2, sticky=W) var_jh = BooleanVar() var_jh.set(True) Checkbutton(master, text="J-H > ", variable=var_jh).grid(row=8, column=1, sticky=W) val_jh = StringVar(master, value="0.7") Entry(master, textvariable=val_jh).grid(row=8, column=2, sticky=W) var_custom = BooleanVar() var_custom.set(False) Checkbutton(master, text="Custom: ", variable=var_custom).grid(row=9, column=1, sticky=W) val_custom = StringVar(master, value="") Entry(master, textvariable=val_custom).grid(row=9, column=2, sticky=W) #Button(master, text='Cancel', command=master.destroy).grid(row=9, sticky=W, pady=4) Button(master, text='Enter', command=lambda: [master.destroy(), var_states()]).grid(row=10, sticky=W, pady=4) master.mainloop() cut_string = cut_string.strip().strip('and').strip() out(cut_string) return apply_cut(table, cut_string)