def map( imbin_file, som_file, map_file, trans_file, som_width, som_height, cpu_only=False, nrot=360, numthreads=10, overwrite=False, log=True, ): # /// Map through SOM \\\ imgs = pu.ImageReader(imbin_file) if not os.path.exists(map_file) or overwrite: map_imbin( imbin_file, som_file, map_file, trans_file, som_width, som_height, numthreads=numthreads, cpu=cpu_only, nrot=nrot, log=log, ) som = pu.SOM(som_file) mapping = pu.Mapping(map_file) transform = pu.Transform(trans_file) somset = pu.SOMSet(som, mapping, transform) return somset
def preprocess( radio_cat, imbin_file, img_size=(2, 150, 150), tile_cutout_path="", remove_tile_cutouts=False, overwrite=False, ): # /// Preprocess Data \\\ if not os.path.exists(imbin_file) or overwrite: preprocessing.main(radio_cat, imbin_file, img_size, tile_cutout_path) if remove_tile_cutouts: shutil.rmtree(tile_cutout_path) imgs = pu.ImageReader(imbin_file) return imgs
return gaussian_2d(X, Y, 1, img_x / 2, img_y / 2, sig_x, sig_y, 0) def taper_img(img, **kwargs): return img * kernel(img.shape, **kwargs) def reweight(data, old_weights, new_weights): data = np.array(data) for chan in range(data.shape[1]): data[:, chan] *= new_weights[chan] / old_weights[chan] return data imgs = pu.ImageReader( "EMU_WISE_E95E05_Aegean_Components_Complex_EMUWISE_IslandNorm_Log_Reprojected.bin" ) # data = imgs.reweight(old_weights=(0.95, 0.05), new_weights=(1, 1)) data = reweight(imgs.data, (0.95, 0.05), (1, 1)) data = taper_img(data, fwhm_frac=0.5) i = np.random.randint(imgs.data.shape[0]) fig, axes = plt.subplots(2, 2, figsize=(8, 8)) axes[0, 0].imshow(imgs.data[i, 0]) axes[0, 1].imshow(imgs.data[i, 1]) axes[1, 0].imshow(data[i, 0]) axes[1, 1].imshow(data[i, 1]) rad_tot = data[:, 0].reshape(-1, 150 * 150).sum(axis=1) ir_tot = data[:, 1].reshape(-1, 150 * 150).sum(axis=1)
som_width, som_height, numthreads=cpu_count(), cpu=args.cpu, nrot=360, log=True, ) else: print( f"Mapping binary {map_file} and Transform file {trans_file} already exist...skipping." ) # CONTINUE HERE if Mapping was done on a different machine. # Update the component catalogue with the sidelobe probability imgs = pu.ImageReader(imbin_file) # Output a table of components that failed to preprocess # This is usually due to missing files failed = sample.iloc[list(set(sample.index).difference( imgs.records))].reset_index(drop=True) Table.from_pandas(failed).write(f"{cat_name}_failed.fits", overwrite=True) # Output a table of all preprocessed components. # This matches the length of the IMG and MAP binaries, which makes # it useful for inspecting the results. sample = sample.iloc[imgs.records].reset_index(drop=True) Table.from_pandas(sample).write(f"{cat_name}_preprocessed.fits", overwrite=True) del imgs
print(f" \centering", file=outfile) print(f" \includegraphics[width=0.75\\textwidth]{{{base}.png}}", file=outfile) print(f" \includegraphics[width=0.75\\textwidth]{{{base}_img0.png}}", file=outfile) print(f" \includegraphics[width=0.75\\textwidth]{{{base}_img1.png}}", file=outfile) print(f" \caption{{{caption}}}", file=outfile) print(f" \label{{fig:{label}}}", file=outfile) print(f"\end{{figure*}}", file=outfile) if len(sys.argv) < 5: print(f"USAGE: {sys.argv[0]} IMG SOM MAP TRANSFORM") imgs = pu.ImageReader(sys.argv[1]) som = pu.SOM(sys.argv[2]) mapping = pu.Mapping(sys.argv[3]) trans = pu.Transform(sys.argv[4]) somset = pu.SOMSet(som, mapping, trans) path = pu.PathHelper("Inspection") outfile = open("latex_imgs.tex", "w") ### SOM 2D histogram ### som_counts(somset, True) plt.savefig(f"som_stats.png") plt.clf() caption = f"The total number of images within the {imgs.data.shape[0]} input images mapped to each neuron in the SOM." latex_figure("som_stats.png", caption, "som_stats", outfile) print("", file=outfile)
axes[2].imshow(somset.som[bmu_idx][0]) axes[2].set_title(f"Best-matching neuron: {bmu_idx}", fontsize=12) # axes[2].imshow(somset.som[bmu_idx][1]) if df is not None: fig.suptitle(df.loc[idx]["Component_name"], fontsize=16) if len(sys.argv) != 3: print("USAGE: {} sample_file som_file".format(sys.argv[0])) sys.exit(-1) sample_file = sys.argv[1] df = pd.read_csv(sample_file) imbin = pu.ImageReader(sample_file.replace(".csv", "_imgs.bin")) som_file = sys.argv[2] som = pu.SOM(som_file) map_file = som_file.replace(".bin", "_Similarity.bin") mapping = pu.Mapping(map_file) trans_file = som_file.replace(".bin", "_Transform.bin") transform = pu.Transform(trans_file) somset = pu.SOMSet(som=som, mapping=mapping, transform=transform) ### Update the component catalogue ### bmus = mapping.bmu()
def source_from_catalogue( src_cat, radio_cat, imbin_path, som=None, show_nearby=False, show_bmu=False, idx=None, ): if idx is None: # idx = np.random.randint(len(src_cat)) idx = src_cat[src_cat.N_components > 1].sample(1).index[0] src = src_cat.loc[idx] comp_names = src.Component_names.split(";") tile_id = radio_cat[radio_cat.Component_name == comp_names[0]].Tile.iloc[0] radio_tile = radio_cat[radio_cat.Tile == tile_id].reset_index(drop=True) comps = radio_tile[radio_tile["Component_name"].isin(comp_names)] radio_ra = np.array(src.RA_components.split(";"), dtype=float) radio_dec = np.array(src.DEC_components.split(";"), dtype=float) radio_pos = SkyCoord(radio_ra, radio_dec, unit=u.deg) if src.N_host_candidates > 0: ir_ra = np.array(src.RA_host_candidates.split(";"), dtype=float) ir_dec = np.array(src.DEC_host_candidates.split(";"), dtype=float) ir_pos = SkyCoord(ir_ra, ir_dec, unit=u.deg) img_file, map_file, trans_file = binary_names(tile_id, imbin_path) imgs = pu.ImageReader(img_file) mapping = pu.Mapping(map_file) transform = pu.Transform(trans_file) if som is not None: somset = pu.SOMSet(som, mapping, transform) img_idx = comps.index[0] comp = comps.loc[img_idx] npix = imgs.data.shape[-1] wcs = create_wcs(comp.RA, comp.DEC, npix, 3 * u.arcmin / npix) if show_bmu: plot_image( imgs, img_idx, somset=somset, wcs=wcs, transform_neuron=True, show_bmu=show_bmu, ) else: plot_image(imgs, img_idx, wcs=wcs, show_bmu=False) axes = plt.gcf().axes if show_nearby: posn = SkyCoord(comp.RA, comp.DEC, unit=u.deg) coords = SkyCoord(radio_cat.RA, radio_cat.DEC, unit=u.deg) nearby = coords[posn.separation(coords) < 1.5 * u.arcmin] for ax in plt.gcf().axes: ax.scatter( nearby.RA, nearby.DEC, c="w", transform=ax.get_transform("world") ) # for ax in plt.gcf().axes: # ax.scatter(comps.RA, comps.DEC, c="r", transform=ax.get_transform("world")) axes[0].scatter( radio_pos.ra, radio_pos.dec, c="r", transform=axes[0].get_transform("world") ) if src.N_host_candidates > 0: axes[1].scatter( ir_pos.ra, ir_pos.dec, c="w", transform=axes[1].get_transform("world") ) plt.suptitle(f"Source ID: {idx}")
pandas=True) # Sample of pairs sample_path = "/home/adrian/CIRADA/Sample" sample_file = "Pairs_uniformNNdist_5asecbins_minSN10_cleansep.csv" radio_cat = vdl.load_vlass_catalogue( os.path.join(sample_path, sample_file), pandas=True, complex=False, ) ir_file = "Pairs_uniformNNdist_5asecbins_minSN10_cleansep_IR.fits" ir_cat = load_unwise(ir_file, path=sample_path) imbin_file = "IMG_uniformNNdist_5asecbins_minSN10_cleansep_compfilt_w7525.bin" imgs = pu.ImageReader(os.path.join(sample_path, imbin_file)) # SOM Input Info som_path = os.path.join("/home/adrian/CIRADA/SOM", "Pairs", "ComponentFiltered", "w7525") som_file = os.path.join(som_path, "SOM_B3_h10_w10_vlass.bin") map_file = "MAP_uniformNNdist_5asecbins_minSN10_cleansep_compfilt_w7525_ComponentFiltered_10x10.bin" trans_file = map_file.replace("MAP", "TRANSFORM") somset = pu.SOMSet( som_file, os.path.join(sample_path, map_file), os.path.join(sample_path, trans_file), ) somset.som.bmu_mask = None
default="Plots", type=str, ) args = parser.parse_args() return args if __name__ == "__main__": args = parse_args() # Initialize the sample DataFrame, ImageReader, and SOM # files = set_filenames(args.som_file, args.map_file, args.trans_file, args.outbase) # df = pd.read_csv(args.sample_file) imgs = pu.ImageReader(args.imbin_file) somset = init_somset(args.som_file, args.map_file, args.trans_file) path = pu.PathHelper(args.outpath) # Only keep the records that preprocessed successfully # df = df.loc[imgs.records].reset_index(drop=True) # df = update_component_catalogue(df, somset) som_counts(somset, show_counts=True) plt.savefig(f"{path}/bmu_frequency.png") # plot_image(imgs, df=df, somset=somset, show_bmu=True, apply_transform=True) # dist_hist(df, labels=True) # plt.savefig(f"{path}/euc_distance_hist.png") # dist_hist_2d(df, somset, bins=100, loglog=False)